* @copyright 2007-2016 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) { exit; } class PagesNotFound extends Module { private $html = ''; public function __construct() { $this->name = 'pagesnotfound'; $this->tab = 'administration'; $this->version = '2.0.3'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->trans('Pages not found', [], 'Modules.Pagesnotfound.Admin'); $this->description = $this->trans('Enrich your stats, display the pages requested by your visitors that could not be found.', [], 'Modules.Pagesnotfound.Admin'); $this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_]; } public function install() { if (!parent::install() || !$this->registerHook('displayTop') || !$this->registerHook('displayAdminStatsModules') ) { return false; } return Db::getInstance()->execute( 'CREATE TABLE `' . _DB_PREFIX_ . 'pagenotfound` ( id_pagenotfound INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, id_shop INTEGER UNSIGNED NOT NULL DEFAULT \'1\', id_shop_group INTEGER UNSIGNED NOT NULL DEFAULT \'1\', request_uri VARCHAR(256) NOT NULL, http_referer VARCHAR(256) NOT NULL, date_add DATETIME NOT NULL, PRIMARY KEY(id_pagenotfound), INDEX (`date_add`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;' ); } public function uninstall() { return parent::uninstall() && Db::getInstance()->execute('DROP TABLE `' . _DB_PREFIX_ . 'pagenotfound`'); } private function getPages() { $sql = 'SELECT http_referer, request_uri, COUNT(*) as nb FROM `' . _DB_PREFIX_ . 'pagenotfound` WHERE date_add BETWEEN ' . ModuleGraph::getDateBetween() . Shop::addSqlRestriction() . 'GROUP BY http_referer, request_uri'; $result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($sql); $pages = []; foreach ($result as $row) { $row['http_referer'] = parse_url($row['http_referer'], PHP_URL_HOST) . parse_url($row['http_referer'], PHP_URL_PATH); if (!isset($row['http_referer']) || empty($row['http_referer'])) { $row['http_referer'] = '--'; } if (!isset($pages[$row['request_uri']])) { $pages[$row['request_uri']] = ['nb' => 0]; } $pages[$row['request_uri']][$row['http_referer']] = $row['nb']; $pages[$row['request_uri']]['nb'] += $row['nb']; } uasort($pages, 'pnfSort'); return $pages; } public function hookDisplayAdminStatsModules() { $this->context->controller->addCSS($this->_path . 'views/css/stacking_responsive.css'); if (Tools::isSubmit('submitTruncatePNF')) { Db::getInstance()->execute('TRUNCATE `' . _DB_PREFIX_ . 'pagenotfound`'); $this->html .= '
' . $this->trans('The "pages not found" cache has been emptied.', [], 'Modules.Pagesnotfound.Admin') . '
'; } elseif (Tools::isSubmit('submitDeletePNF')) { Db::getInstance()->execute( 'DELETE FROM `' . _DB_PREFIX_ . 'pagenotfound` WHERE date_add BETWEEN ' . ModuleGraph::getDateBetween() ); $this->html .= '
' . $this->trans('The "pages not found" cache has been deleted.', [], 'Modules.Pagesnotfound.Admin') . '
'; } $this->html .= '
' . $this->displayName . '

' . $this->trans('Guide', [], 'Modules.Pagesnotfound.Admin') . '

' . $this->trans('404 errors', [], 'Modules.Pagesnotfound.Admin') . '

' . $this->trans('A 404 error is an HTTP error code which means that the file requested by the user cannot be found. In your case it means that one of your visitors entered a wrong URL in the address bar, or that you or another website has a dead link. When possible, the referrer is shown so you can find the page/site which contains the dead link. If not, it generally means that it is a direct access, so someone may have bookmarked a link which doesn\'t exist anymore.', [], 'Modules.Pagesnotfound.Admin') . '

 

' . $this->trans('How to catch these errors?', [], 'Modules.Pagesnotfound.Admin') . '

' . sprintf($this->trans('If your webhost supports .htaccess files, you can create one in the root directory of PrestaShop and insert the following line inside: "%s".', [], 'Modules.Pagesnotfound.Admin'), 'ErrorDocument 404 ' . __PS_BASE_URI__ . '404.php') . '
' . sprintf($this->trans('A user requesting a page which doesn\'t exist will be redirected to the following page: %s. This module logs access to this page.', [], 'Modules.Pagesnotfound.Admin'), __PS_BASE_URI__ . '404.php') . '

'; if (!file_exists($this->_normalizeDirectory(_PS_ROOT_DIR_) . '.htaccess')) { $this->html .= '
' . $this->trans('You must use a .htaccess file to redirect 404 errors to the "404.php" page.', [], 'Modules.Pagesnotfound.Admin') . '
'; } $pages = $this->getPages(); if (count($pages)) { $titlePage = $this->trans('Page', [], 'Modules.Pagesnotfound.Admin'); $titleReferer = $this->trans('Referrer', [], 'Modules.Pagesnotfound.Admin'); $titleCounter = $this->trans('Counter', [], 'Modules.Pagesnotfound.Admin'); $this->html .= '
'; foreach ($pages as $ru => $hrs) { foreach ($hrs as $hr => $counter) { if ($hr != 'nb') { $this->html .= ' '; } } } $this->html .= '
' . $titlePage . ' ' . $titleReferer . ' ' . $titleCounter . '
' . wordwrap($ru, 30, '
', true) . '
' . wordwrap($hr, 40, '
', true) . '
' . $counter . '
'; } else { $this->html .= '
' . $this->trans('No "page not found" issue registered for now.', [], 'Modules.Pagesnotfound.Admin') . '
'; } if (count($pages)) { $this->html .= '

' . $this->trans('Empty database', [], 'Modules.Pagesnotfound.Admin') . '

'; } return $this->html; } public function hookDisplayTop($params) { if (strstr($_SERVER['REQUEST_URI'], '404.php') && isset($_SERVER['REDIRECT_URL'])) { $_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL']; } if (!Validate::isUrl($request_uri = $_SERVER['REQUEST_URI']) || strstr($_SERVER['REQUEST_URI'], '-admin404')) { return; } if (get_class(Context::getContext()->controller) == 'PageNotFoundController') { $http_referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; if (empty($http_referer) || Validate::isAbsoluteUrl($http_referer)) { Db::getInstance()->execute( ' INSERT INTO `' . _DB_PREFIX_ . 'pagenotfound` (`request_uri`, `http_referer`, `date_add`, `id_shop`, `id_shop_group`) VALUES (\'' . pSQL($request_uri) . '\', \'' . pSQL($http_referer) . '\', NOW(), ' . (int) $this->context->shop->id . ', ' . (int) $this->context->shop->id_shop_group . ') ' ); } } } private function _normalizeDirectory($directory) { $last = $directory[strlen($directory) - 1]; if (in_array($last, ['/', '\\'])) { $directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR; return $directory; } $directory .= DIRECTORY_SEPARATOR; return $directory; } } function pnfSort($a, $b) { if ($a['nb'] == $b['nb']) { return 0; } return ($a['nb'] > $b['nb']) ? -1 : 1; }