Skip to content
This repository
tag: 1.5.1
Fetching contributors…

Cannot retrieve contributors at this time

file 477 lines (419 sloc) 11.83 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik
* @package Piwik
*/

/**
* Class used by the logging script piwik.php called by the javascript tag.
* Handles the visitor & his/her actions on the website, saves the data in the DB,
* saves information in the cookie, etc.
*
* We try to include as little files as possible (no dependency on 3rd party modules).
*
* @package Piwik
* @subpackage Piwik_Tracker
*/
class Piwik_Tracker
{
protected $stateValid = self::STATE_NOTHING_TO_NOTICE;
/**
* @var Piwik_Tracker_Db
*/
protected static $db = null;

const STATE_NOTHING_TO_NOTICE = 1;
const STATE_LOGGING_DISABLE = 10;
const STATE_EMPTY_REQUEST = 11;
const STATE_NOSCRIPT_REQUEST = 13;

// We use hex ID that are 16 chars in length, ie. 64 bits IDs
const LENGTH_HEX_ID_STRING = 16;
const LENGTH_BINARY_ID = 8;

// These are also hardcoded in the Javascript
const MAX_CUSTOM_VARIABLES = 5;
const MAX_LENGTH_CUSTOM_VARIABLE = 100;

protected $authenticated = false;
static protected $forcedDateTime = null;
static protected $forcedIpString = null;
static protected $forcedVisitorId = null;

static protected $pluginsNotToLoad = array();

public function __construct($args = null)
{
$this->request = $args ? $args : $_GET + $_POST;
}
public static function setForceIp($ipString)
{
self::$forcedIpString = $ipString;
}
public static function setForceDateTime( $dateTime )
{
self::$forcedDateTime = $dateTime;
}

public static function setForceVisitorId($visitorId)
{
self::$forcedVisitorId = $visitorId;
}

public function getCurrentTimestamp()
{
if(!is_null(self::$forcedDateTime))
{
     return strtotime(self::$forcedDateTime);
}
return time();
}

/**
* Do not load the specified plugins (used during testing, to disable Provider plugin)
* @param array $plugins
*/
static public function setPluginsNotToLoad($plugins)
{
self::$pluginsNotToLoad = $plugins;
}

/**
* Get list of plugins to not load
*
* @return array
*/
static public function getPluginsNotToLoad()
{
return self::$pluginsNotToLoad;
}

/**
* Main
*/
public function main()
{
$this->init();

try {
if( $this->isVisitValid() )
{
self::connectDatabase();

$visit = $this->getNewVisitObject();
$visit->setRequest($this->request);
$visit->handle();
unset($visit);
}

// don't run scheduled tasks in CLI mode from Tracker, this is the case
// where we bulk load logs & don't want to lose time with tasks
if(!Piwik_Common::isPhpCliMode()
&& !$this->authenticated)
{
Piwik_Common::runScheduledTasks($now = $this->getCurrentTimestamp());
}
} catch (Piwik_Tracker_Db_Exception $e) {
printDebug("<b>".$e->getMessage()."</b>");
} catch(Piwik_Tracker_Visit_Excluded $e) {
} catch(Exception $e) {
Piwik_Tracker_ExitWithException($e);
}

$this->end();
}

/**
* Returns the date in the "Y-m-d H:i:s" PHP format
*
* @param int $timstamp
* @return string
*/
public static function getDatetimeFromTimestamp($timestamp)
{
return date("Y-m-d H:i:s", $timestamp);
}

/**
* Initialization
*/
protected function init()
{
$this->handleTrackingApi();
$this->loadTrackerPlugins();
$this->handleDisabledTracker();
$this->handleEmptyRequest();

printDebug("Current datetime: ".date("Y-m-d H:i:s", $this->getCurrentTimestamp()));
}

/**
* Cleanup
*/
protected function end()
{
switch($this->getState())
{
case self::STATE_LOGGING_DISABLE:
printDebug("Logging disabled, display transparent logo");
$this->outputTransparentGif();
break;

case self::STATE_EMPTY_REQUEST:
printDebug("Empty request => Piwik page");
echo "<a href='/'>Piwik</a> is a free open source <a href='http://piwik.org'>web analytics</a> alternative to Google analytics.";
break;

case self::STATE_NOSCRIPT_REQUEST:
case self::STATE_NOTHING_TO_NOTICE:
default:
printDebug("Nothing to notice => default behaviour");
$this->outputTransparentGif();
break;
}
printDebug("End of the page.");

if($GLOBALS['PIWIK_TRACKER_DEBUG'] === true)
{
if(isset(self::$db)) {
self::$db->recordProfiling();
Piwik::printSqlProfilingReportTracker(self::$db);
}
}

self::disconnectDatabase();
}

/**
* Factory to create database objects
*
* @param array $configDb Database configuration
* @return Piwik_Tracker_Db_*
*/
public static function factory($configDb)
{
switch($configDb['adapter'])
{
case 'PDO_MYSQL':
require_once PIWIK_INCLUDE_PATH .'/core/Tracker/Db/Pdo/Mysql.php';
return new Piwik_Tracker_Db_Pdo_Mysql($configDb);

case 'MYSQLI':
require_once PIWIK_INCLUDE_PATH .'/core/Tracker/Db/Mysqli.php';
return new Piwik_Tracker_Db_Mysqli($configDb);
}

throw new Exception('Unsupported database adapter '.$configDb['adapter']);
}

public static function connectPiwikTrackerDb()
{
$db = null;
$configDb = Piwik_Tracker_Config::getInstance()->database;

if(!isset($configDb['port']))
{
// before 0.2.4 there is no port specified in config file
$configDb['port'] = '3306';
}

$db = self::factory( $configDb );
$db->connect();

return $db;
}

public static function connectDatabase()
{
if( !is_null(self::$db))
{
return;
}

$db = null;
Piwik_PostEvent('Tracker.createDatabase', $db);
if(is_null($db))
{
$db = self::connectPiwikTrackerDb();
}
self::$db = $db;
}

/**
* @return Piwik_Tracker_Db
*/
public static function getDatabase()
{
return self::$db;
}

public static function disconnectDatabase()
{
if(isset(self::$db))
{
self::$db->disconnect();
self::$db = null;
}
}

/**
* Returns the Tracker_Visit object.
* This method can be overwritten to use a different Tracker_Visit object
*
* @return Piwik_Tracker_Visit
*/
protected function getNewVisitObject()
{
$visit = null;
Piwik_PostEvent('Tracker.getNewVisitObject', $visit);

if(is_null($visit))
{
$visit = new Piwik_Tracker_Visit( self::$forcedIpString, self::$forcedDateTime );
$visit->setForcedVisitorId(self::$forcedVisitorId);
}
elseif(!($visit instanceof Piwik_Tracker_Visit_Interface ))
{
throw new Exception("The Visit object set in the plugin must implement Piwik_Tracker_Visit_Interface");
}
return $visit;
}

protected function outputTransparentGif()
{
if( !isset($GLOBALS['PIWIK_TRACKER_DEBUG']) || !$GLOBALS['PIWIK_TRACKER_DEBUG'] )
{
$trans_gif_64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
header('Content-Type: image/gif');
header('Access-Control-Allow-Origin: *');
print(base64_decode($trans_gif_64));
}
}

protected function sendHeader($header)
{
header($header);
}

protected function isVisitValid()
{
return $this->stateValid !== self::STATE_LOGGING_DISABLE
&& $this->stateValid !== self::STATE_EMPTY_REQUEST;
}

protected function getState()
{
return $this->stateValid;
}

protected function setState( $value )
{
$this->stateValid = $value;
}

protected function loadTrackerPlugins()
{
if(isset($this->request['dp'])
&& $this->authenticated)
{
Piwik_Tracker::setPluginsNotToLoad(array('Provider'));
}
try{
$pluginsTracker = Piwik_Tracker_Config::getInstance()->Plugins_Tracker;
if(is_array($pluginsTracker)
&& count($pluginsTracker) != 0)
{
$pluginsTracker['Plugins_Tracker'] = array_diff($pluginsTracker['Plugins_Tracker'], self::getPluginsNotToLoad());
Piwik_PluginsManager::getInstance()->doNotLoadAlwaysActivatedPlugins();
Piwik_PluginsManager::getInstance()->loadPlugins( $pluginsTracker['Plugins_Tracker'] );

printDebug("Loading plugins: { ". implode(",", $pluginsTracker['Plugins_Tracker']) . " }");
}
} catch(Exception $e) {
printDebug("ERROR: ".$e->getMessage());
}
}

protected function handleEmptyRequest()
{
$countParameters = count($this->request);
if($countParameters == 0)
{
$this->setState(self::STATE_EMPTY_REQUEST);
}
if($countParameters == 1 )
{
$this->setState(self::STATE_NOSCRIPT_REQUEST);
}
}

protected function handleDisabledTracker()
{
$saveStats = Piwik_Tracker_Config::getInstance()->Tracker['record_statistics'];
if($saveStats == 0)
{
$this->setState(self::STATE_LOGGING_DISABLE);
}
}

protected function authenticateSuperUserOrAdmin()
{
$tokenAuth = Piwik_Common::getRequestVar('token_auth', false);

if( $tokenAuth )
{
$superUserLogin = Piwik_Tracker_Config::getInstance()->superuser['login'];
$superUserPassword = Piwik_Tracker_Config::getInstance()->superuser['password'];
if( md5($superUserLogin . $superUserPassword ) == $tokenAuth )
{
$this->authenticated = true;
return true;
}

// Now checking the list of admin token_auth cached in the Tracker config file
$idSite = Piwik_Common::getRequestVar('idsite', false, 'int', $this->request);
if(!empty($idSite)
&& $idSite > 0)
{
$website = Piwik_Common::getCacheWebsiteAttributes( $idSite );
$adminTokenAuth = $website['admin_token_auth'];
if(in_array($tokenAuth, $adminTokenAuth))
{
$this->authenticated = true;
return true;
}
}
printDebug("token_auth = $tokenAuth - Warning: Super User / Admin was NOT authenticated");
}
return false;
}

/**
* This method allows to set custom IP + server time when using Tracking API.
* These two attributes can be only set by the Super User (passing token_auth).
*/
protected function handleTrackingApi()
{
if(!$this->authenticateSuperUserOrAdmin())
{
return;
}
printDebug("token_auth is authenticated!");

// Custom IP to use for this visitor
$customIp = Piwik_Common::getRequestVar('cip', false, 'string', $this->request);
if(!empty($customIp))
{
$this->setForceIp($customIp);
}

// Custom server date time to use
$customDatetime = Piwik_Common::getRequestVar('cdt', false, 'string', $this->request);
if(!empty($customDatetime))
{
$this->setForceDateTime($customDatetime);
}

// Forced Visitor ID to record the visit / action
$customVisitorId = Piwik_Common::getRequestVar('cid', false, 'string', $this->request);
if(!empty($customVisitorId))
{
$this->setForceVisitorId($customVisitorId);
}
}
}

if(!function_exists('printDebug'))
{
    function printDebug( $info = '' )
    {
     if(isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG'])
     {
     if(is_array($info))
     {
     print("<pre>");
     print(htmlspecialchars(var_export($info,true), ENT_QUOTES));
     print("</pre>");
     }
     else
     {
     print(htmlspecialchars($info, ENT_QUOTES) . "<br />\n");
     }
     }
    }
}

/**
* Displays exception in a friendly UI and exits.
*
* @param Exception $e
*/
function Piwik_Tracker_ExitWithException($e)
{
header('Content-Type: text/html; charset=utf-8');
if(isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG'])
{
$trailer = '<font color="#888888">Backtrace:<br /><pre>'.$e->getTraceAsString().'</pre></font>';
}
else
{
$trailer = '<p>Edit the following line in piwik.php to enable tracker debugging and display a backtrace:</p>
<blockquote><pre>$GLOBALS[\'PIWIK_TRACKER_DEBUG\'] = true;</pre></blockquote>';
}

$headerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/themes/default/simple_structure_header.tpl');
$footerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/themes/default/simple_structure_footer.tpl');
$headerPage = str_replace('{$HTML_TITLE}', 'Piwik &rsaquo; Error', $headerPage);

echo $headerPage . '<p>' . $e->getMessage() . '</p>' . $trailer . $footerPage;

exit;
}
Something went wrong with that request. Please try again.