* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6594 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_CAN_LOAD_FILES_'))
exit;
class Pagesnotfound extends Module
{
private $_html = '';
function __construct()
{
$this->name = 'pagesnotfound';
$this->tab = 'analytics_stats';
$this->version = 1.0;
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Pages not found');
$this->description = $this->l('Display the pages requested by your visitors but not found.');
}
function install()
{
if (!parent::install() OR !$this->registerHook('top') OR !$this->registerHook('AdminStatsModules'))
return false;
return Db::getInstance()->Execute('
CREATE TABLE `'._DB_PREFIX_.'pagenotfound` (
id_pagenotfound INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
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;');
}
function uninstall()
{
return (parent::uninstall() AND Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'pagenotfound`'));
}
private function getPages()
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT http_referer, request_uri, COUNT(*) as nb
FROM `'._DB_PREFIX_.'pagenotfound` p
WHERE p.date_add BETWEEN '.ModuleGraph::getDateBetween().'
GROUP BY http_referer, request_uri');
$pages = array();
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']) OR empty($row['http_referer']))
$row['http_referer'] = '--';
if (!isset($pages[$row['request_uri']]))
$pages[$row['request_uri']] = array('nb' => 0);
$pages[$row['request_uri']][$row['http_referer']] = $row['nb'];
$pages[$row['request_uri']]['nb'] += $row['nb'];
}
uasort($pages, 'pnfSort');
return $pages;
}
function hookAdminStatsModules()
{
if (Tools::isSubmit('submitTruncatePNF'))
{
Db::getInstance()->Execute('TRUNCATE `'._DB_PREFIX_.'pagenotfound`');
$this->_html .= '
'.$this->l('Pages not found has been emptied.').'
';
}
$this->_html .= '';
if (sizeof($pages))
$this->_html .= '
';
$this->_html .= '
';
return $this->_html;
}
function hookTop($params)
{
if (strstr($_SERVER['REQUEST_URI'], '404.php') AND isset($_SERVER['REDIRECT_URL']))
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL'];
if (!Validate::isUrl($request_uri = $_SERVER['REQUEST_URI']) OR strstr($_SERVER['REQUEST_URI'], '-admin404'))
return;
if (strstr($_SERVER['PHP_SELF'], '404.php') AND !strstr($_SERVER['REQUEST_URI'], '404.php'))
{
$http_referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if (empty($http_referer) OR Validate::isAbsoluteUrl($http_referer))
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'pagenotfound` (`request_uri`,`http_referer`,`date_add`) VALUES (\''.pSQL($request_uri).'\',\''.pSQL($http_referer).'\',NOW())');
}
}
}
function pnfSort($a, $b) {
if ($a['nb'] == $b['nb'])
return 0;
return ($a['nb'] > $b['nb']) ? -1 : 1;
}