* @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.2.0 * @package WB * @subpackage base */ class WBStatistic_LinkTracker extends WBStatistic { /** * name of page view table */ const TABLE_STAT = 'linktracking'; /** * name of page view table */ const TABLE_LINK = 'tracklink'; /** * Current Track Link Id * @var string */ private $id = -1; /** * constructor * * Init user and request object. * */ public function __construct() { parent::__construct(); if (!$this->enabled) { return; } // see if feature is turned OFF if (!WBParam::get('wb/statistic/linktracking/use', 0)) { $this->enabled = false; return; } } /** * Extract Link Id From Request * * Extract only once and store in $this->id * * @see $id */ private function getLinkId() { // do only once if (-1 != $this->id) { return $this->id; } // request object is required! if (empty($this->req)) { $this->req = WBClass::create('WBRequest'); } $req = $this->req->export(); foreach ($req as $t => $v) { if (!empty($v)) { continue; } if (!is_numeric($t)) { continue; } $this->id = $t; break; } // use zero id if not found if (-1 == $this->id) { $this->id = ''; } return $this->id; } /** * add path to page views * * Save path in statistic table * * @param string $path */ public function add() { if (!$this->enabled) { return; } $id = $this->getLinkId(); if (empty($id)) { return; } $save = array( 'trackid' => $this->getTrackingId(), 'linkid' => $this->id ); $this->table->save(self::TABLE_STAT, '__new', $save); } /** * Redirect * * Load tracklink and return redirect URL or empty * * @return string */ public function redirect() { // return '[[SELF]]'; $id = $this->getLinkId(); if (empty($id)) { return ''; } if (empty($this->table)) { $this->table = WBClass::create('WBDatasource_Table'); } $data = $this->table->get(self::TABLE_LINK, $id); if (1 != count($data)) { return ''; } $data = array_shift($data); $urlId = $data[$this->table->getIdentifier('url')]; if (empty($urlId)) { return ''; } $url = WBClass::create('WBDictionary_URL'); $url->load($urlId); $uData = $url->get(); $target = ''; switch ($uData['protocol']) { case 'self': $target = ''; break; case 'http': case 'https': case 'ftp': $target = $uData['protocol'] . '://' . $uData['host']; break; // cannot redirect! default: return ''; break; } $target .= array_shift(explode('?', $uData['path'])); // don't redirect to same path if ('self' == $uData['protocol'] && $this->req->getMeta('PHP_SELF') == $target) { return ''; } $query = $this->req->export(); // remove tracklink unset($query[$id]); parse_str(parse_url('http://localhost/' . $uData['path'], PHP_URL_QUERY), $tmp); $query = array_merge($query, $tmp); if (empty($query)) { return $target; } return $target . '?' . http_build_query($query); } }