diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-09-19 11:06:41 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-09-19 11:06:41 +0400 |
commit | 39f12998a7f22682c9adb493f0335e1590f412fa (patch) | |
tree | 9c48370dfb49461d7677c7ab6d9e67de785d6537 /core | |
parent | 221a9e99bb3814bc9df340a392f43c70367ae157 (diff) |
Moving isPhpCli to Common
Diffstat (limited to 'core')
-rw-r--r-- | core/ArchiveProcessor/Rules.php | 3 | ||||
-rw-r--r-- | core/Common.php | 18 | ||||
-rw-r--r-- | core/FrontController.php | 7 | ||||
-rw-r--r-- | core/Log/MessageScreenFormatter.php | 6 | ||||
-rw-r--r-- | core/Log/ScreenFormatter.php | 4 | ||||
-rw-r--r-- | core/Session.php | 2 | ||||
-rw-r--r-- | core/Session/SessionNamespace.php | 4 | ||||
-rw-r--r-- | core/SettingsPiwik.php | 4 | ||||
-rw-r--r-- | core/SettingsServer.php | 13 | ||||
-rw-r--r-- | core/Tracker.php | 70 |
10 files changed, 78 insertions, 53 deletions
diff --git a/core/ArchiveProcessor/Rules.php b/core/ArchiveProcessor/Rules.php index 9b40be42bc..73949ee102 100644 --- a/core/ArchiveProcessor/Rules.php +++ b/core/ArchiveProcessor/Rules.php @@ -11,6 +11,7 @@ namespace Piwik\ArchiveProcessor; use Exception; +use Piwik\Common; use Piwik\Config; use Piwik\Date; use Piwik\Piwik; @@ -214,7 +215,7 @@ class Rules { return !self::$archivingDisabledByTests && (Rules::isBrowserTriggerEnabled() - || SettingsServer::isPhpCliMode() + || Common::isPhpCliMode() || (Piwik::isUserIsSuperUser() && SettingsServer::isArchivePhpTriggered())); } diff --git a/core/Common.php b/core/Common.php index a78378925e..b9344db59b 100644 --- a/core/Common.php +++ b/core/Common.php @@ -40,6 +40,7 @@ class Common */ const HTML_ENCODING_QUOTE_STYLE = ENT_QUOTES; + /* * Database */ @@ -114,6 +115,20 @@ class Common return PluginsManager::getInstance()->isPluginActivated('Goals'); } + /** + * Returns true if PHP was invoked from command-line interface (shell) + * + * @since added in 0.4.4 + * @return bool true if PHP invoked as a CGI or from CLI + */ + public static function isPhpCliMode() + { + $remoteAddr = @$_SERVER['REMOTE_ADDR']; + return PHP_SAPI == 'cli' || + (!strncmp(PHP_SAPI, 'cgi', 3) && empty($remoteAddr)); + } + + /* * String operations */ @@ -718,7 +733,7 @@ class Common if (is_null($browserLang)) { $browserLang = self::sanitizeInputValues(@$_SERVER['HTTP_ACCEPT_LANGUAGE']); - if (empty($browserLang) && SettingsServer::isPhpCliMode()) { + if (empty($browserLang) && self::isPhpCliMode()) { $browserLang = @getenv('LANG'); } } @@ -954,5 +969,6 @@ class Common } } } + } diff --git a/core/FrontController.php b/core/FrontController.php index 7a3660ee34..21032df160 100644 --- a/core/FrontController.php +++ b/core/FrontController.php @@ -15,7 +15,6 @@ use Piwik\API\Request; use Piwik\API\ResponseBuilder; use Piwik\Log; use Piwik\Session; -use Piwik\Profiler; use Zend_Registry; /** @@ -184,7 +183,7 @@ class FrontController // which load the HTML page of the installer with the error. // This is at least required for misc/cron/archive.php and useful to all other scripts return (defined('PIWIK_ENABLE_DISPATCH') && !PIWIK_ENABLE_DISPATCH) - || SettingsServer::isPhpCliMode() + || Common::isPhpCliMode() || SettingsServer::isArchivePhpTriggered(); } @@ -319,7 +318,7 @@ class FrontController protected function handleMaintenanceMode() { if (Config::getInstance()->General['maintenance_mode'] == 1 - && !SettingsServer::isPhpCliMode() + && !Common::isPhpCliMode() ) { $format = Common::getRequestVar('format', ''); @@ -344,7 +343,7 @@ class FrontController protected function handleSSLRedirection() { - if (!SettingsServer::isPhpCliMode() + if (!Common::isPhpCliMode() && Config::getInstance()->General['force_ssl'] == 1 && !ProxyHttp::isHttps() // Specifically disable for the opt out iframe diff --git a/core/Log/MessageScreenFormatter.php b/core/Log/MessageScreenFormatter.php index 26e48570b5..7b9ac48cdf 100644 --- a/core/Log/MessageScreenFormatter.php +++ b/core/Log/MessageScreenFormatter.php @@ -9,8 +9,8 @@ * @package Piwik */ namespace Piwik\Log; +use Piwik\Common; use Piwik\Profiler; -use Piwik\SettingsServer; /** * Format a standard message event to be displayed on the screen. @@ -34,14 +34,14 @@ class MessageScreenFormatter extends ScreenFormatter } else { $message = $event['message']; } - if (!SettingsServer::isPhpCliMode()) { + if (!Common::isPhpCliMode()) { $message .= "<br/>"; } $message .= "\n"; $memory = ''; // Hacky: let's hide the memory usage in CLI to hide from the archive.php output - if (!SettingsServer::isPhpCliMode()) { + if (!Common::isPhpCliMode()) { $memory = '[' . Profiler::getMemoryUsage() . '] '; } $message = '[' . $event['timestamp'] . '] [' . $event['requestKey'] . '] ' . $memory . $message; diff --git a/core/Log/ScreenFormatter.php b/core/Log/ScreenFormatter.php index a6326a126f..69b257d6c6 100644 --- a/core/Log/ScreenFormatter.php +++ b/core/Log/ScreenFormatter.php @@ -9,7 +9,7 @@ * @package Piwik */ namespace Piwik\Log; -use Piwik\SettingsServer; +use Piwik\Common; /** * @@ -49,7 +49,7 @@ class ScreenFormatter implements \Zend_Log_Formatter_Interface */ static public function getFormattedString($string) { - if (!SettingsServer::isPhpCliMode()) { + if (!Common::isPhpCliMode()) { @header('Content-Type: text/html; charset=utf-8'); } return $string; diff --git a/core/Session.php b/core/Session.php index 67c0cc75d7..6cf0ee21a8 100644 --- a/core/Session.php +++ b/core/Session.php @@ -45,7 +45,7 @@ class Session extends Zend_Session */ public static function start($options = false) { - if (SettingsServer::isPhpCliMode() + if (Common::isPhpCliMode() || self::$sessionStarted || (defined('PIWIK_ENABLE_SESSION_START') && !PIWIK_ENABLE_SESSION_START) ) { diff --git a/core/Session/SessionNamespace.php b/core/Session/SessionNamespace.php index 7b041ec461..63ede5f45e 100644 --- a/core/Session/SessionNamespace.php +++ b/core/Session/SessionNamespace.php @@ -10,7 +10,7 @@ */ namespace Piwik\Session; -use Piwik\SettingsServer; +use Piwik\Common; use Zend_Session_Namespace; /** @@ -27,7 +27,7 @@ class SessionNamespace extends Zend_Session_Namespace */ public function __construct($namespace = 'Default', $singleInstance = false) { - if (SettingsServer::isPhpCliMode()) { + if (Common::isPhpCliMode()) { self::$_readable = true; return; } diff --git a/core/SettingsPiwik.php b/core/SettingsPiwik.php index 5c48b5691b..f4efd30c0e 100644 --- a/core/SettingsPiwik.php +++ b/core/SettingsPiwik.php @@ -91,7 +91,7 @@ class SettingsPiwik public static function shouldLoggerLog() { try { - $shouldLog = (SettingsServer::isPhpCliMode() + $shouldLog = (Common::isPhpCliMode() || Config::getInstance()->log['log_only_when_cli'] == 0) && (Config::getInstance()->log['log_only_when_debug_parameter'] == 0 @@ -127,7 +127,7 @@ class SettingsPiwik $key = 'piwikUrl'; $url = Piwik_GetOption($key); - if (SettingsServer::isPhpCliMode() + if (Common::isPhpCliMode() // in case archive.php is triggered with domain localhost || SettingsServer::isArchivePhpTriggered() || defined('PIWIK_MODE_ARCHIVE') diff --git a/core/SettingsServer.php b/core/SettingsServer.php index 58208714b8..8f0f9d80bd 100644 --- a/core/SettingsServer.php +++ b/core/SettingsServer.php @@ -30,19 +30,6 @@ class SettingsServer } /** - * Returns true if PHP was invoked from command-line interface (shell) - * - * @since added in 0.4.4 - * @return bool true if PHP invoked as a CGI or from CLI - */ - public static function isPhpCliMode() - { - $remoteAddr = @$_SERVER['REMOTE_ADDR']; - return PHP_SAPI == 'cli' || - (!strncmp(PHP_SAPI, 'cgi', 3) && empty($remoteAddr)); - } - - /** * Returns true if running on Microsoft IIS 7 (or above) * * @return bool diff --git a/core/Tracker.php b/core/Tracker.php index d8f5950e52..996e3e97fd 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -86,6 +86,16 @@ class Tracker */ private $countOfLoggedRequests = 0; + protected function outputAccessControlHeaders() + { + $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; + if ($requestMethod !== 'GET') { + $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*'; + Common::sendHeader('Access-Control-Allow-Origin: ' . $origin); + Common::sendHeader('Access-Control-Allow-Credentials: true'); + } + } + public function clear() { self::$forcedIpString = null; @@ -231,6 +241,8 @@ class Tracker $this->exitWithException($ex, true); } + $this->initOutputBuffer(); + if (!empty($this->requests)) { foreach ($this->requests as $params) { $request = new Request($params, $tokenAuth); @@ -280,13 +292,31 @@ class Tracker $this->handleEmptyRequest(new Request($_GET + $_POST)); } $this->end(); + + $this->flushOutputBuffer(); + } + + protected function initOutputBuffer() + { + ob_start(); + } + + protected function flushOutputBuffer() + { + ob_end_flush(); + } + + protected function getOutputBuffer() + { + return ob_get_contents(); } + protected function shouldRunScheduledTasks() { // 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 - return !SettingsServer::isPhpCliMode() + return !Common::isPhpCliMode() && $this->getState() != self::STATE_LOGGING_DISABLE; } @@ -401,7 +431,7 @@ class Tracker || $authenticated) { $result['message'] = $this->getMessageFromException($e); } - $this->sendHeader('Content-Type: application/json'); + Common::sendHeader('Content-Type: application/json'); echo Common::json_encode($result); exit; } @@ -458,7 +488,7 @@ class Tracker 'status' => 'success', 'tracked' => $this->countOfLoggedRequests ); - $this->sendHeader('Content-Type: application/json'); + Common::sendHeader('Content-Type: application/json'); echo Common::json_encode($result); exit; } @@ -589,29 +619,21 @@ class Tracker protected function outputTransparentGif() { - if (!isset($GLOBALS['PIWIK_TRACKER_DEBUG']) || !$GLOBALS['PIWIK_TRACKER_DEBUG']) { - // If there was an error during tracker, do not display GIF - if(headers_sent()) { - return; - } - $trans_gif_64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="; - $this->sendHeader('Content-Type: image/gif'); - - $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; - - if ($requestMethod !== 'GET') { - $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*'; - $this->sendHeader('Access-Control-Allow-Origin: ' . $origin); - $this->sendHeader('Access-Control-Allow-Credentials: true'); - } - - print(base64_decode($trans_gif_64)); + if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) + && $GLOBALS['PIWIK_TRACKER_DEBUG']) { + return; } - } + // If there was an error during tracker, do not display GIF + // (we can't use headers_sent() for some reasons) + if(strlen( $this->getOutputBuffer() ) > 0) { +// return; + } + $transGifBase64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="; + Common::sendHeader('Content-Type: image/gif'); - protected function sendHeader($header) - { - Common::sendHeader($header); + $this->outputAccessControlHeaders(); + + print(base64_decode($transGifBase64)); } protected function isVisitValid() |