* @copyright 2007-2014 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 StatsLive extends Module { private $html = ''; public function __construct() { $this->name = 'statslive'; $this->tab = 'analytics_stats'; $this->version = '1.2.2'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Visitors online'); $this->description = $this->l('Adds a list of customers and visitors who are currently online to the Stats dashboard.'); $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); } public function install() { return parent::install() && $this->registerHook('AdminStatsModules'); } /** * Get the number of online customers * * @return array(array, int) array of online customers entries, number of online customers */ private function getCustomersOnline() { if ($maintenance_ips = Configuration::get('PS_MAINTENANCE_IP')) $maintenance_ips = implode(',', array_map('ip2long', array_map('trim', explode(',', $maintenance_ips)))); if (Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) { $sql = 'SELECT u.id_customer, u.firstname, u.lastname, pt.name as page FROM `'._DB_PREFIX_.'connections` c LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections LEFT JOIN `'._DB_PREFIX_.'page` p ON p.id_page = cp.id_page LEFT JOIN `'._DB_PREFIX_.'page_type` pt ON p.id_page_type = pt.id_page_type INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest INNER JOIN `'._DB_PREFIX_.'customer` u ON u.id_customer = g.id_customer WHERE cp.`time_end` IS NULL '.Shop::addSqlRestriction(false, 'c').' AND TIME_TO_SEC(TIMEDIFF(\''.pSQL(date('Y-m-d H:i:00', time())).'\', cp.`time_start`)) < 900 '.($maintenance_ips ? 'AND c.ip_address NOT IN ('.preg_replace('/[^,0-9]/', '', $maintenance_ips).')' : '').' GROUP BY u.id_customer ORDER BY u.firstname, u.lastname'; } else { $sql = 'SELECT u.id_customer, u.firstname, u.lastname, "-" as page FROM `'._DB_PREFIX_.'connections` c INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest INNER JOIN `'._DB_PREFIX_.'customer` u ON u.id_customer = g.id_customer WHERE TIME_TO_SEC(TIMEDIFF(\''.pSQL(date('Y-m-d H:i:00', time())).'\', c.`date_add`)) < 900 '.Shop::addSqlRestriction(false, 'c').' '.($maintenance_ips ? 'AND c.ip_address NOT IN ('.preg_replace('/[^,0-9]/', '', $maintenance_ips).')' : '').' GROUP BY u.id_customer ORDER BY u.firstname, u.lastname'; } $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); return array($results, Db::getInstance()->NumRows()); } /** * Get the number of online visitors * * @return array(array, int) array of online visitors entries, number of online visitors */ private function getVisitorsOnline() { if ($maintenance_ips = Configuration::get('PS_MAINTENANCE_IP')) $maintenance_ips = implode(',', array_map('ip2long', array_filter(array_map('trim', explode(',', $maintenance_ips))))); if (Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) { $sql = 'SELECT c.id_guest, c.ip_address, c.date_add, c.http_referer, pt.name as page FROM `'._DB_PREFIX_.'connections` c LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections LEFT JOIN `'._DB_PREFIX_.'page` p ON p.id_page = cp.id_page LEFT JOIN `'._DB_PREFIX_.'page_type` pt ON p.id_page_type = pt.id_page_type INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest WHERE (g.id_customer IS NULL OR g.id_customer = 0) '.Shop::addSqlRestriction(false, 'c').' AND cp.`time_end` IS NULL AND TIME_TO_SEC(TIMEDIFF(\''.pSQL(date('Y-m-d H:i:00', time())).'\', cp.`time_start`)) < 900 '.($maintenance_ips ? 'AND c.ip_address NOT IN ('.preg_replace('/[^,0-9]/', '', $maintenance_ips).')' : '').' GROUP BY c.id_connections ORDER BY c.date_add DESC'; } else { $sql = 'SELECT c.id_guest, c.ip_address, c.date_add, c.http_referer, "-" as page FROM `'._DB_PREFIX_.'connections` c INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest WHERE (g.id_customer IS NULL OR g.id_customer = 0) '.Shop::addSqlRestriction(false, 'c').' AND TIME_TO_SEC(TIMEDIFF(\''.pSQL(date('Y-m-d H:i:00', time())).'\', c.`date_add`)) < 900 '.($maintenance_ips ? 'AND c.ip_address NOT IN ('.preg_replace('/[^,0-9]/', '', $maintenance_ips).')' : '').' ORDER BY c.date_add DESC'; } $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); return array($results, Db::getInstance()->NumRows()); } public function hookAdminStatsModules($params) { list($customers, $total_customers) = $this->getCustomersOnline(); list($visitors, $total_visitors) = $this->getVisitorsOnline(); $irow = 0; $this->html .= ''; if (!Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) $this->html .= '
'.$this->l('There are no active customers online right now.').'
'; $this->html .= ''.$this->l('Guest ID').' | '.$this->l('IP').' | '.$this->l('Last activity').' | '.$this->l('Current page').' | '.$this->l('Referrer').' |
---|---|---|---|---|
'.$visitor['id_guest'].' | '.long2ip($visitor['ip_address']).' | '.Tools::substr($visitor['date_add'], 11).' | '.(isset($visitor['page']) ? $visitor['page'] : $this->l('Undefined')).' | '.(empty($visitor['http_referer']) ? $this->l('None') : parse_url($visitor['http_referer'], PHP_URL_HOST)).' |
'.$this->l('There are no visitors online.').'
'; $this->html .= ''.$this->l('Maintenance IPs are excluded from the online visitors.').'
'.$this->l('Add or remove an IP address.').' '; return $this->html; } }