stateValid = self::STATE_NOTHING_TO_NOTICE; } // create the database object function connectDatabase() { $configDb = Piwik_LogStats_Config::getInstance()->database; // we decode the password. Password is html encoded because it's enclosed between " double quotes $configDb['password'] = htmlspecialchars_decode($configDb['password']); $this->db = new Piwik_LogStats_Db( $configDb['host'], $configDb['username'], $configDb['password'], $configDb['dbname'] ); $this->db->connect(); } private function initProcess() { try{ $pluginsLogStats = Piwik_LogStats_Config::getInstance()->Plugins_LogStats; if(is_array($pluginsLogStats) && count($pluginsLogStats) != 0) { Piwik_PluginsManager::getInstance()->doNotLoadAlwaysActivatedPlugins(); Piwik_PluginsManager::getInstance()->setPluginsToLoad( $pluginsLogStats['Plugins_LogStats'] ); } } catch(Exception $e) { } $saveStats = Piwik_LogStats_Config::getInstance()->LogStats['record_statistics']; if($saveStats == 0) { $this->setState(self::STATE_LOGGING_DISABLE); } if( count($_GET) == 0) { $this->setState(self::STATE_NO_GET_VARIABLE); } $downloadVariableName = Piwik_LogStats_Config::getInstance()->LogStats['download_url_var_name']; $urlDownload = Piwik_Common::getRequestVar( $downloadVariableName, '', 'string'); if( !empty($urlDownload) ) { if( Piwik_Common::getRequestVar( 'redirect', 1, 'int') == 1) { $this->setState( self::STATE_TO_REDIRECT_URL ); } $this->setUrlToRedirect ( $urlDownload); } $outlinkVariableName = Piwik_LogStats_Config::getInstance()->LogStats['outlink_url_var_name']; $urlOutlink = Piwik_Common::getRequestVar( $outlinkVariableName, '', 'string'); if( !empty($urlOutlink) ) { if( Piwik_Common::getRequestVar( 'redirect', 1, 'int') == 1) { $this->setState( self::STATE_TO_REDIRECT_URL ); } $this->setUrlToRedirect ( $urlOutlink); } } private function processVisit() { return $this->stateValid !== self::STATE_LOGGING_DISABLE && $this->stateValid !== self::STATE_NO_GET_VARIABLE; } private function getState() { return $this->stateValid; } private function setUrlToRedirect( $url ) { $this->urlToRedirect = $url; } private function getUrlToRedirect() { return $this->urlToRedirect; } private function setState( $value ) { $this->stateValid = $value; } /** * Returns the LogStats_Visit object. * This method can be overwritten so that we use a different LogStats_Visit object * * @return LogStats_Visit */ protected function getNewVisitObject() { return new Piwik_LogStats_Visit($this->db); } // main algorithm // => input : variables filtered // => action : read cookie, read database, database logging, cookie writing function main() { $this->initProcess(); if( $this->processVisit() ) { try { $this->connectDatabase(); $visit = $this->getNewVisitObject(); $visit->handle(); } catch (PDOException $e) { $this->setState(self::STATE_LOGGING_DISABLE); } } $this->endProcess(); } // display the logo or pixel 1*1 GIF // or a marketing page if no parameters in the url // or redirect to a url // or load a URL (rss feed) (transmit the cookie as well) protected function endProcess() { switch($this->getState()) { case self::STATE_LOGGING_DISABLE: printDebug("Logging disabled, display transparent logo"); $this->outputTransparentGif(); break; case self::STATE_NO_GET_VARIABLE: printDebug("No get variables => piwik page"); echo "Piwik is a free open source web analytics alternative to Google analytics."; break; case self::STATE_TO_REDIRECT_URL: $this->sendHeader('Location: ' . $this->getUrlToRedirect()); break; case self::STATE_NOTHING_TO_NOTICE: default: printDebug("Nothing to notice => default behaviour"); $this->outputTransparentGif(); break; } printDebug("End of the page."); if($GLOBALS['DEBUGPIWIK'] === true) { Piwik::printSqlProfilingReportLogStats($this->db); } $this->disconnectDb(); } protected function disconnectDb() { if(isset($this->db)) { $this->db->disconnect(); } } protected function outputTransparentGif() { if( !isset($GLOBALS['DEBUGPIWIK']) || !$GLOBALS['DEBUGPIWIK'] ) { $trans_gif_64 = "R0lGODlhAQABAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAABAAEAAAICVAEAOw=="; header("Content-type: image/gif"); print(base64_decode($trans_gif_64)); } } protected function sendHeader($header) { header($header); } } function printDebug( $info = '' ) { if(isset($GLOBALS['DEBUGPIWIK']) && $GLOBALS['DEBUGPIWIK']) { if(is_array($info)) { print("
"); print(var_export($info,true)); print(""); } else { print($info . "