* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBStatistic'); /** * Statistic module for page and content views * * Simple interface to save page statistic * * @version 0.1.2 * @package WB * @subpackage base */ class WBStatistic_Session extends WBStatistic { /** * name of session view table */ const TABLE = 'sessionview'; const UNDEFINED = 'undefined'; /** * Browser info * @var WBInfo_Browser */ private $bi; /** * Store new session * * Store browser information for each new session * * @param string $path * @param string $namespace */ public function add() { if (!$this->enabled) { return; } WBClass::load('WBException_Config'); try{ $this->table->getTableInfo(self::TABLE); } catch(WBException_Config $e) { return; } $uid = $this->user->getId(); if (!$uid) { $uid = 0; } $save = array( 'trackid' => $this->getTrackingId(), 'addr' => $this->getAddress(), $this->uPrimary => $uid, 'useragent' => self::UNDEFINED, 'acceptlang' => self::UNDEFINED, ); if (isset($_SERVER['HTTP_USER_AGENT'])) { $save['useragent'] = $_SERVER['HTTP_USER_AGENT']; } if (isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $save['acceptlang'] = $_SERVER['HTTP_ACCEPT_LANGUAGE']; } $save['preferedlang'] = $this->detectPreferdLang($save['acceptlang']); $this->addBrowserInfo($save, $save['useragent']); $this->table->save(self::TABLE, '__new', $save); } /** * Detect browser and add info * * Controlled by WBParam "wb/statistic/browser/detect". * * @see WBInfo_Browser * @see WBParam * @param array $data * @param string $useragent */ private function addBrowserInfo(&$data, $useragent) { if (self::UNDEFINED == $useragent) { return ''; } if (!WBParam::get('wb/statistic/browser/detect', 1)) { return; } $this->bi = WBClass::create('WBInfo_Browser'); $this->bi->detect($useragent); if ($this->bi->isCrawler()) { $data['hint'] = 'spider'; $this->sess->set(WBStatistic::SESS_KEY_HINT, 'spider'); } $data['browser'] = $this->bi->getName(); $data['platform'] = $this->bi->getPlatform(); $data['majorversion'] = $this->bi->getVersion(WBInfo_Browser::VER_TYPE_MAJOR); $data['minorversion'] = $this->bi->getVersion(WBInfo_Browser::VER_TYPE_MINOR); if (empty($data['browser'])) { $data['browser'] = ''; } if (empty($data['platform'])) { $data['platform'] = ''; } } /** * Get prefered language * * Extract first language from accept language and return normalized 5-letter * lang code. * * @param string $acceptlang * @return string */ private function detectPreferdLang($acceptlang) { if (self::UNDEFINED == $acceptlang) { return ''; } $lang = explode(',', $acceptlang); $lang = trim($lang[0]); if (empty($lang)) { return ''; } $lang = explode('-', $lang); $lang[0] = strtolower($lang[0]); if (2 != count($lang)) { return $lang[0]; } $lang[1] = strtoupper($lang[1]); return $lang[0] . '_' . $lang[1]; } }