/** * Checks if directories are writable and create them if they do not exist. * * @param array $directoriesToCheck array of directories to check - if not given default Piwik directories that needs write permission are checked * @return array directory name => true|false (is writable) */ public static function checkDirectoriesWritable($directoriesToCheck) { $resultCheck = array(); foreach ($directoriesToCheck as $directoryToCheck) { if (!preg_match('/^' . preg_quote(PIWIK_USER_PATH, '/') . '/', $directoryToCheck)) { $directoryToCheck = PIWIK_USER_PATH . $directoryToCheck; } if (strpos($directoryToCheck, '/tmp/') !== false) { $directoryToCheck = SettingsPiwik::rewriteTmpPathWithInstanceId($directoryToCheck); } Filesystem::mkdir($directoryToCheck); $directory = Filesystem::realpath($directoryToCheck); $resultCheck[$directory] = false; if ($directory !== false && is_writable($directoryToCheck)) { $resultCheck[$directory] = true; } } return $resultCheck; }
/** * Initializes Profiling via XHProf. * See: https://github.com/piwik/piwik/blob/master/tests/README.xhprof.md */ public static function setupProfilerXHProf($mainRun = false) { if (SettingsServer::isTrackerApiRequest()) { // do not profile Tracker return; } $path = PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php'; if (!file_exists($path)) { return; } if (!function_exists('xhprof_enable')) { return; } if (!is_writable(ini_get("xhprof.output_dir"))) { throw new \Exception("The profiler output dir '" . ini_get("xhprof.output_dir") . "' should exist and be writable."); } require_once $path; require_once PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php'; if (!function_exists('xhprof_error')) { function xhprof_error($out) { echo substr($out, 0, 300) . '...'; } } $currentGitBranch = SettingsPiwik::getCurrentGitBranch(); $profilerNamespace = "piwik"; if ($currentGitBranch != 'master') { $profilerNamespace .= "." . $currentGitBranch; } xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); if ($mainRun) { self::setProfilingRunIds(array()); } register_shutdown_function(function () use($profilerNamespace, $mainRun) { $xhprofData = xhprof_disable(); $xhprofRuns = new \XHProfRuns_Default(); $runId = $xhprofRuns->save_run($xhprofData, $profilerNamespace); if (empty($runId)) { die('could not write profiler run'); } $runs = self::getProfilingRunIds(); $runs[] = $runId; // $weights = array_fill(0, count($runs), 1); // $aggregate = xhprof_aggregate_runs($xhprofRuns, $runs, $weights, $profilerNamespace); // $runId = $xhprofRuns->save_run($aggregate, $profilerNamespace); if ($mainRun) { $runIds = implode(',', $runs); $out = "\n\n"; $baseUrl = "http://" . @$_SERVER['HTTP_HOST'] . "/" . @$_SERVER['REQUEST_URI']; $baseUrlStored = SettingsPiwik::getPiwikUrl(); if (strlen($baseUrlStored) > strlen($baseUrl)) { $baseUrl = $baseUrlStored; } $baseUrl = "\n" . $baseUrl . "tests/lib/xhprof-0.9.4/xhprof_html/?source={$profilerNamespace}&run="; $out .= "Profiler report is available at:"; $out .= $baseUrl . $runId; if ($runId != $runIds) { $out .= "\n\nProfiler Report aggregating all runs triggered from this process: "; $out .= $baseUrl . $runIds; } $out .= "\n\n"; echo $out; } else { self::setProfilingRunIds($runs); } }); }
/** * Get file integrity information (in PIWIK_INCLUDE_PATH). * * @return array(bool, string, ...) Return code (true/false), followed by zero or more error messages */ public static function getFileIntegrityInformation() { $messages = array(); $messages[] = true; $manifest = PIWIK_INCLUDE_PATH . '/config/manifest.inc.php'; if (file_exists($manifest)) { require_once $manifest; } if (!class_exists('Piwik\\Manifest')) { $git = SettingsPiwik::getCurrentGitBranch(); if (empty($git)) { $messages[] = Piwik::translate('General_WarningFileIntegrityNoManifest') . " If you are deploying Piwik from Git, this message is normal."; } return $messages; } $files = \Piwik\Manifest::$files; $hasMd5file = function_exists('md5_file'); $hasMd5 = function_exists('md5'); foreach ($files as $path => $props) { $file = PIWIK_INCLUDE_PATH . '/' . $path; if (!file_exists($file)) { $messages[] = Piwik::translate('General_ExceptionMissingFile', $file); } else { if (filesize($file) != $props[0]) { if (!$hasMd5 || in_array(substr($path, -4), array('.gif', '.ico', '.jpg', '.png', '.swf'))) { // files that contain binary data (e.g., images) must match the file size $messages[] = Piwik::translate('General_ExceptionFilesizeMismatch', array($file, $props[0], filesize($file))); } else { // convert end-of-line characters and re-test text files $content = @file_get_contents($file); $content = str_replace("\r\n", "\n", $content); if (strlen($content) != $props[0] || @md5($content) !== $props[1]) { $messages[] = Piwik::translate('General_ExceptionFilesizeMismatch', array($file, $props[0], filesize($file))); } } } else { if ($hasMd5file && @md5_file($file) !== $props[1]) { $messages[] = Piwik::translate('General_ExceptionFileIntegrity', $file); } } } } if (count($messages) > 1) { $messages[0] = false; } if (!$hasMd5file) { $messages[] = Piwik::translate('General_WarningFileIntegrityNoMd5file'); } return $messages; }
/** * Returns the string to save in the cookie from the $this->value array of values. * It goes through the array and generates the cookie content string. * * @return string Cookie content */ protected function generateContentString() { $cookieStr = ''; foreach ($this->value as $name => $value) { if (!is_numeric($value)) { $value = base64_encode(safe_serialize($value)); } $cookieStr .= "{$name}={$value}" . self::VALUE_SEPARATOR; } if (!empty($cookieStr)) { $cookieStr .= '_='; // sign cookie $signature = sha1($cookieStr . SettingsPiwik::getSalt()); return $cookieStr . $signature; } return ''; }
/** * @return bool */ public static function isSecureConnectionAssumedByPiwikButNotForcedYet() { $isSecureConnectionLikelyNotUsed = Url::isSecureConnectionLikelyNotUsed(); $hasSessionCookieSecureFlag = ProxyHttp::isHttps(); $isSecureConnectionAssumedByPiwikButNotForcedYet = Url::isPiwikConfiguredToAssumeSecureConnection() && !SettingsPiwik::isHttpsForced(); return $isSecureConnectionLikelyNotUsed && $hasSessionCookieSecureFlag && $isSecureConnectionAssumedByPiwikButNotForcedYet; }