/** * Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs, * as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs. * * @param string $tableName PREFIXED table name! you must call Common::prefixTable() before passing the table name * @param array $fields array of unquoted field names * @param array $values array of data to be inserted * @param bool $throwException Whether to throw an exception that was caught while trying * LOAD DATA INFILE, or not. * @throws Exception * @return bool True if the bulk LOAD was used, false if we fallback to plain INSERTs */ public static function tableInsertBatch($tableName, $fields, $values, $throwException = false) { $filePath = PIWIK_USER_PATH . '/tmp/assets/' . $tableName . '-' . Common::generateUniqId() . '.csv'; $filePath = SettingsPiwik::rewriteTmpPathWithInstanceId($filePath); $loadDataInfileEnabled = Config::getInstance()->General['enable_load_data_infile']; if ($loadDataInfileEnabled && Db::get()->hasBulkLoader()) { try { $fileSpec = array('delim' => "\t", 'quote' => '"', 'escape' => '\\\\', 'escapespecial_cb' => function ($str) { return str_replace(array(chr(92), chr(34)), array(chr(92) . chr(92), chr(92) . chr(34)), $str); }, 'eol' => "\r\n", 'null' => 'NULL'); // hack for charset mismatch if (!DbHelper::isDatabaseConnectionUTF8() && !isset(Config::getInstance()->database['charset'])) { $fileSpec['charset'] = 'latin1'; } self::createCSVFile($filePath, $fileSpec, $values); if (!is_readable($filePath)) { throw new Exception("File {$filePath} could not be read."); } $rc = self::createTableFromCSVFile($tableName, $fields, $filePath, $fileSpec); if ($rc) { unlink($filePath); return true; } } catch (Exception $e) { Log::info("LOAD DATA INFILE failed or not supported, falling back to normal INSERTs... Error was: %s", $e->getMessage()); if ($throwException) { throw $e; } } } // if all else fails, fallback to a series of INSERTs @unlink($filePath); self::tableInsertBatchIterate($tableName, $fields, $values); return false; }
public function isInstalled() { if (is_null($this->isInstalled)) { $this->isInstalled = SettingsPiwik::isPiwikInstalled(); } return $this->isInstalled; }
/** * Generate hash on user info and password * * @param string $userInfo User name, email, etc * @param string $password * @return string */ private function generateHash($userInfo, $password) { // mitigate rainbow table attack $passwordLen = strlen($password) / 2; $hash = Common::hash($userInfo . substr($password, 0, $passwordLen) . SettingsPiwik::getSalt() . substr($password, $passwordLen)); return $hash; }
/** * Cache buster based on * - Piwik version * - Loaded plugins * - Super user salt * - Latest * * @param string[] $pluginNames * @return string */ public function piwikVersionBasedCacheBuster($pluginNames = false) { $currentGitHash = @file_get_contents(PIWIK_INCLUDE_PATH . '/.git/refs/heads/master'); $pluginList = md5(implode(",", !$pluginNames ? Manager::getInstance()->getLoadedPluginsName() : $pluginNames)); $cacheBuster = md5(SettingsPiwik::getSalt() . $pluginList . PHP_VERSION . Version::VERSION . trim($currentGitHash)); return $cacheBuster; }
protected function sendNotifications() { $latestVersion = $this->getLatestVersion(); $host = SettingsPiwik::getPiwikUrl(); $subject = Piwik::translate('CoreUpdater_NotificationSubjectAvailableCoreUpdate', $latestVersion); $message = Piwik::translate('ScheduledReports_EmailHello'); $message .= "\n\n"; $message .= Piwik::translate('CoreUpdater_ThereIsNewVersionAvailableForUpdate'); $message .= "\n\n"; $message .= Piwik::translate('CoreUpdater_YouCanUpgradeAutomaticallyOrDownloadPackage', $latestVersion); $message .= "\n"; $message .= $host . 'index.php?module=CoreUpdater&action=newVersionAvailable'; $message .= "\n\n"; $version = new Version(); if ($version->isStableVersion($latestVersion)) { $message .= Piwik::translate('CoreUpdater_ViewVersionChangelog'); $message .= "\n"; $message .= $this->getLinkToChangeLog($latestVersion); $message .= "\n\n"; } $message .= Piwik::translate('CoreUpdater_FeedbackRequest'); $message .= "\n"; $message .= 'http://piwik.org/contact/'; $this->sendEmailNotification($subject, $message); }
public function __construct() { $loader = $this->getDefaultThemeLoader(); $this->addPluginNamespaces($loader); // If theme != default we need to chain $chainLoader = new Twig_Loader_Chain(array($loader)); // Create new Twig Environment and set cache dir $templatesCompiledPath = PIWIK_USER_PATH . '/tmp/templates_c'; $templatesCompiledPath = SettingsPiwik::rewriteTmpPathWithHostname($templatesCompiledPath); $this->twig = new Twig_Environment($chainLoader, array('debug' => true, 'strict_variables' => true, 'cache' => $templatesCompiledPath)); $this->twig->addExtension(new Twig_Extension_Debug()); $this->twig->clearTemplateCache(); $this->addFilter_translate(); $this->addFilter_urlRewriteWithParameters(); $this->addFilter_sumTime(); $this->addFilter_money(); $this->addFilter_truncate(); $this->addFilter_notificiation(); $this->addFilter_percentage(); $this->twig->addFilter(new Twig_SimpleFilter('implode', 'implode')); $this->twig->addFilter(new Twig_SimpleFilter('ucwords', 'ucwords')); $this->addFunction_includeAssets(); $this->addFunction_linkTo(); $this->addFunction_sparkline(); $this->addFunction_postEvent(); $this->addFunction_isPluginLoaded(); $this->addFunction_getJavascriptTranslations(); }
/** * Computes the output for the given data table * * @param DataTable $table * @return string * @throws Exception */ protected function renderTable($table) { if (!$table instanceof DataTable\Map || $table->getKeyName() != 'date') { throw new Exception("RSS feeds can be generated for one specific website &idSite=X." . "\nPlease specify only one idSite or consider using &format=XML instead."); } $idSite = Common::getRequestVar('idSite', 1, 'int'); $period = Common::getRequestVar('period'); $piwikUrl = SettingsPiwik::getPiwikUrl() . "?module=CoreHome&action=index&idSite=" . $idSite . "&period=" . $period; $out = ""; $moreRecentFirst = array_reverse($table->getDataTables(), true); foreach ($moreRecentFirst as $date => $subtable) { /** @var DataTable $subtable */ $timestamp = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getDateStart()->getTimestamp(); $site = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_SITE_INDEX); $pudDate = date('r', $timestamp); $dateInSiteTimezone = Date::factory($timestamp); if ($site) { $dateInSiteTimezone = $dateInSiteTimezone->setTimezone($site->getTimezone()); } $dateInSiteTimezone = $dateInSiteTimezone->toString('Y-m-d'); $thisPiwikUrl = Common::sanitizeInputValue($piwikUrl . "&date={$dateInSiteTimezone}"); $siteName = $site ? $site->getName() : ''; $title = $siteName . " on " . $date; $out .= "\t<item>\n\t\t<pubDate>{$pudDate}</pubDate>\n\t\t<guid>{$thisPiwikUrl}</guid>\n\t\t<link>{$thisPiwikUrl}</link>\n\t\t<title>{$title}</title>\n\t\t<author>http://piwik.org</author>\n\t\t<description>"; $out .= Common::sanitizeInputValue($this->renderDataTable($subtable)); $out .= "</description>\n\t</item>\n"; } $header = $this->getRssHeader(); $footer = $this->getRssFooter(); return $header . $out . $footer; }
public function configureTopMenu(MenuTop $menu) { if (Piwik::isUserIsAnonymous() || !SettingsPiwik::isPiwikInstalled()) { $langManager = new LanguagesManager(); $menu->addHtml('LanguageSelector', $langManager->getLanguagesSelector(), true, $order = 30, false); } }
protected function execute(InputInterface $input, OutputInterface $output) { $path = sprintf('%s/tmp/logs/', PIWIK_DOCUMENT_ROOT); $path = SettingsPiwik::rewriteTmpPathWithInstanceId($path); $cmd = sprintf('tail -f %s*.log', $path); $output->writeln('Executing command: ' . $cmd); passthru($cmd); }
/** * Returns a 64-bit hash of all the configuration settings * @param $os * @param $browserName * @param $browserVersion * @param $plugin_Flash * @param $plugin_Java * @param $plugin_Director * @param $plugin_Quicktime * @param $plugin_RealPlayer * @param $plugin_PDF * @param $plugin_WindowsMedia * @param $plugin_Gears * @param $plugin_Silverlight * @param $plugin_Cookie * @param $ip * @param $browserLang * @return string */ protected function getConfigHash($os, $browserName, $browserVersion, $plugin_Flash, $plugin_Java, $plugin_Director, $plugin_Quicktime, $plugin_RealPlayer, $plugin_PDF, $plugin_WindowsMedia, $plugin_Gears, $plugin_Silverlight, $plugin_Cookie, $ip, $browserLang) { // prevent the config hash from being the same, across different Piwik instances // (limits ability of different Piwik instances to cross-match users) $salt = SettingsPiwik::getSalt(); $configString = $os . $browserName . $browserVersion . $plugin_Flash . $plugin_Java . $plugin_Director . $plugin_Quicktime . $plugin_RealPlayer . $plugin_PDF . $plugin_WindowsMedia . $plugin_Gears . $plugin_Silverlight . $plugin_Cookie . $ip . $browserLang . $salt; $hash = md5($configString, $raw_output = true); return substr($hash, 0, Tracker::LENGTH_BINARY_ID); }
/** * @param string $directory directory to use * @param int $timeToLiveInSeconds TTL */ public function __construct($directory, $timeToLiveInSeconds = 300) { $cachePath = PIWIK_USER_PATH . '/tmp/cache/' . $directory . '/'; $this->cachePath = SettingsPiwik::rewriteTmpPathWithHostname($cachePath); if ($timeToLiveInSeconds < self::MINIMUM_TTL) { $timeToLiveInSeconds = self::MINIMUM_TTL; } $this->ttl = $timeToLiveInSeconds; }
function checkPiwikSetupForTests() { if (empty($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI'] == '@REQUEST_URI@') { echo "WARNING: for tests to pass, you must first:\n1) Install webserver on localhost, eg. apache\n2) Make these Piwik files available on the webserver, at eg. http://localhost/dev/piwik/\n3) Install Piwik by going through the installation process\n4) Copy phpunit.xml.dist to phpunit.xml\n5) Edit in phpunit.xml the @REQUEST_URI@ and replace with the webserver path to Piwik, eg. '/dev/piwik/'\n\nTry again.\n-> If you still get this message, you can work around it by specifying Host + Request_Uri at the top of this file tests/PHPUnit/bootstrap.php. <-"; exit(1); } $baseUrl = \Piwik\Tests\Framework\Fixture::getRootUrl(); \Piwik\SettingsPiwik::checkPiwikServerWorking($baseUrl, $acceptInvalidSSLCertificates = true); }
/** * @param $idSites * @return array */ private static function getSegmentsToProcess($idSites) { $knownSegmentsToArchiveAllSites = SettingsPiwik::getKnownSegmentsToArchive(); $segmentsToProcess = $knownSegmentsToArchiveAllSites; foreach ($idSites as $idSite) { $segmentForThisWebsite = SettingsPiwik::getKnownSegmentsToArchiveForSite($idSite); $segmentsToProcess = array_merge($segmentsToProcess, $segmentForThisWebsite); } $segmentsToProcess = array_unique($segmentsToProcess); return $segmentsToProcess; }
/** * Adds Referrer widgets */ function addWidgets() { WidgetsList::add('Referrers_Referrers', 'Referrers_WidgetKeywords', 'Referrers', 'getKeywords'); WidgetsList::add('Referrers_Referrers', 'Referrers_WidgetExternalWebsites', 'Referrers', 'getWebsites'); WidgetsList::add('Referrers_Referrers', 'Referrers_WidgetSocials', 'Referrers', 'getSocials'); WidgetsList::add('Referrers_Referrers', 'Referrers_SearchEngines', 'Referrers', 'getSearchEngines'); WidgetsList::add('Referrers_Referrers', 'Referrers_Campaigns', 'Referrers', 'getCampaigns'); WidgetsList::add('Referrers_Referrers', 'General_Overview', 'Referrers', 'getReferrerType'); WidgetsList::add('Referrers_Referrers', 'Referrers_WidgetGetAll', 'Referrers', 'getAll'); if (SettingsPiwik::isSegmentationEnabled()) { WidgetsList::add('SEO', 'Referrers_WidgetTopKeywordsForPages', 'Referrers', 'getKeywordsForPage'); } }
/** * Sets the sender. * * @param string $email Email address of the sender. * @param null|string $name Name of the sender. * @return Zend_Mail */ public function setFrom($email, $name = null) { $hostname = Config::getInstance()->mail['defaultHostnameIfEmpty']; $piwikHost = Url::getCurrentHost($hostname); // If known Piwik URL, use it instead of "localhost" $piwikUrl = SettingsPiwik::getPiwikUrl(); $url = parse_url($piwikUrl); if (isset($url['host']) && $url['host'] != 'localhost' && $url['host'] != '127.0.0.1') { $piwikHost = $url['host']; } $email = str_replace('{DOMAIN}', $piwikHost, $email); return parent::setFrom($email, $name); }
/** * Constructor. * * @param string $segmentCondition The segment condition, eg, `'browserCode=ff;countryCode=CA'`. * @param array $idSites The list of sites the segment will be used with. Some segments are * dependent on the site, such as goal segments. * @throws */ public function __construct($segmentCondition, $idSites) { $segmentCondition = trim($segmentCondition); if (!SettingsPiwik::isSegmentationEnabled() && !empty($segmentCondition)) { throw new Exception("The Super User has disabled the Segmentation feature."); } // First try with url decoded value. If that fails, try with raw value. // If that also fails, it will throw the exception try { $this->initializeSegment(urldecode($segmentCondition), $idSites); } catch (Exception $e) { $this->initializeSegment($segmentCondition, $idSites); } }
public function setUp() { parent::setUp(); Fixture::createWebsite('2014-02-04'); $testingEnvironment = new \Piwik\Tests\Framework\TestingEnvironmentVariables(); $testingEnvironment->testCaseClass = null; $testingEnvironment->addFailingScheduledTask = false; $testingEnvironment->addScheduledTask = false; $testingEnvironment->save(); Option::delete(self::TASKS_STARTED_OPTION_NAME); Option::delete(self::TASKS_FINISHED_OPTION_NAME); Option::delete(Timetable::TIMETABLE_OPTION_STRING); SettingsPiwik::overwritePiwikUrl(self::$fixture->getRootUrl() . "tests/PHPUnit/proxy"); }
public function configure(WidgetsList $widgetsList) { $category = 'Referrers_Referrers'; $controller = 'Referrers'; $widgetsList->add($category, 'Referrers_WidgetKeywords', $controller, 'getKeywords'); $widgetsList->add($category, 'Referrers_WidgetExternalWebsites', $controller, 'getWebsites'); $widgetsList->add($category, 'Referrers_WidgetSocials', $controller, 'getSocials'); $widgetsList->add($category, 'Referrers_SearchEngines', $controller, 'getSearchEngines'); $widgetsList->add($category, 'Referrers_Campaigns', $controller, 'getCampaigns'); $widgetsList->add($category, 'General_Overview', $controller, 'getReferrerType'); $widgetsList->add($category, 'Referrers_WidgetGetAll', $controller, 'getAll'); if (SettingsPiwik::isSegmentationEnabled()) { $widgetsList->add('SEO', 'Referrers_WidgetTopKeywordsForPages', $controller, 'getKeywordsForPage'); } }
public static function shouldProcessReportsAllPlugins(Segment $segment, $periodLabel) { if ($segment->isEmpty() && $periodLabel != 'range') { return true; } $segmentsToProcess = SettingsPiwik::getKnownSegmentsToArchive(); if (!empty($segmentsToProcess)) { // If the requested segment is one of the segments to pre-process // we ensure that any call to the API will trigger archiving of all reports for this segment $segment = $segment->getString(); if (in_array($segment, $segmentsToProcess)) { return true; } } return false; }
/** * Returns an existing nonce by ID. If none exists, a new nonce will be generated. * * @param string $id Unique id to avoid namespace conflicts, e.g., `'ModuleName.ActionName'`. * @param int $ttl Optional time-to-live in seconds; default is 5 minutes. (ie, in 5 minutes, * the nonce will no longer be valid). * @return string */ public static function getNonce($id, $ttl = 600) { // save session-dependent nonce $ns = new SessionNamespace($id); $nonce = $ns->nonce; // re-use an unexpired nonce (a small deviation from the "used only once" principle, so long as we do not reset the expiration) // to handle browser pre-fetch or double fetch caused by some browser add-ons/extensions if (empty($nonce)) { // generate a new nonce $nonce = md5(SettingsPiwik::getSalt() . time() . Common::generateUniqId()); $ns->nonce = $nonce; } // extend lifetime if nonce is requested again to prevent from early timeout if nonce is requested again // a few seconds before timeout $ns->setExpirationSeconds($ttl, 'nonce'); return $nonce; }
function prepareServerVariables(Config $config) { $testConfig = $config->tests; if ('@REQUEST_URI@' === $testConfig['request_uri']) { // config not done yet, if Piwik is installed we can automatically configure request_uri and http_host $url = \Piwik\SettingsPiwik::getPiwikUrl(); if (!empty($url)) { $parsedUrl = parse_url($url); $testConfig['request_uri'] = $parsedUrl['path']; $testConfig['http_host'] = $parsedUrl['host']; $config->tests = $testConfig; $config->forceSave(); } } $_SERVER['HTTP_HOST'] = $testConfig['http_host']; $_SERVER['REQUEST_URI'] = $testConfig['request_uri']; $_SERVER['REMOTE_ADDR'] = $testConfig['remote_addr']; }
/** * @internal For Debugging only * Call metadata reports and draw the default graph for each report. */ public function index() { Piwik::checkUserHasSomeAdminAccess(); $idSite = Common::getRequestVar('idSite', 1, 'int'); $period = Common::getRequestVar('period', 'day', 'string'); $date = Common::getRequestVar('date', 'today', 'string'); $_GET['token_auth'] = Piwik::getCurrentUserTokenAuth(); $reports = APIPlugins::getInstance()->getReportMetadata($idSite, $period, $date); $plot = array(); foreach ($reports as $report) { if (!empty($report['imageGraphUrl'])) { $plot[] = array($report['category'] . ' › ' . $report['name'], SettingsPiwik::getPiwikUrl() . $report['imageGraphUrl']); } } $view = new View('@ImageGraph/index'); $view->titleAndUrls = $plot; return $view->render(); }
public function __construct() { $loader = $this->getDefaultThemeLoader(); $this->addPluginNamespaces($loader); //get current theme $manager = Plugin\Manager::getInstance(); $theme = $manager->getThemeEnabled(); $loaders = array(); //create loader for custom theme to overwrite twig templates if ($theme && $theme->getPluginName() != \Piwik\Plugin\Manager::DEFAULT_THEME) { $customLoader = $this->getCustomThemeLoader($theme); if ($customLoader) { //make it possible to overwrite plugin templates $this->addCustomPluginNamespaces($customLoader, $theme->getPluginName()); $loaders[] = $customLoader; } } $loaders[] = $loader; $chainLoader = new Twig_Loader_Chain($loaders); // Create new Twig Environment and set cache dir $templatesCompiledPath = PIWIK_USER_PATH . '/tmp/templates_c'; $templatesCompiledPath = SettingsPiwik::rewriteTmpPathWithInstanceId($templatesCompiledPath); $this->twig = new Twig_Environment($chainLoader, array('debug' => true, 'strict_variables' => true, 'cache' => $templatesCompiledPath)); $this->twig->addExtension(new Twig_Extension_Debug()); $this->twig->clearTemplateCache(); $this->addFilter_translate(); $this->addFilter_urlRewriteWithParameters(); $this->addFilter_sumTime(); $this->addFilter_money(); $this->addFilter_truncate(); $this->addFilter_notification(); $this->addFilter_percentage(); $this->addFilter_prettyDate(); $this->addFilter_safeDecodeRaw(); $this->twig->addFilter(new Twig_SimpleFilter('implode', 'implode')); $this->twig->addFilter(new Twig_SimpleFilter('ucwords', 'ucwords')); $this->addFunction_includeAssets(); $this->addFunction_linkTo(); $this->addFunction_sparkline(); $this->addFunction_postEvent(); $this->addFunction_isPluginLoaded(); $this->addFunction_getJavascriptTranslations(); $this->twig->addTokenParser(new RenderTokenParser()); }
public function validate() { $this->checkConfigFileExists($this->settingsProvider->getPathGlobal()); if (SettingsPiwik::isPiwikInstalled()) { $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller = false); return; } $startInstaller = true; if (SettingsServer::isTrackerApiRequest()) { // if Piwik is not installed yet, the piwik.php should do nothing and not return an error throw new NotYetInstalledException("As Piwik is not installed yet, the Tracking API cannot proceed and will exit without error."); } if (Common::isPhpCliMode()) { // in CLI, do not start/redirect to installer, simply output the exception at the top $startInstaller = false; } // Start the installation when config file not found $this->checkConfigFileExists($this->settingsProvider->getPathLocal(), $startInstaller); }
/** * Check for a newer version * * @param bool $force Force check * @param int $interval Interval used for update checks */ public static function check($force = false, $interval = null) { if (!SettingsPiwik::isAutoUpdateEnabled()) { return; } if ($interval === null) { $interval = self::CHECK_INTERVAL; } $lastTimeChecked = Option::get(self::LAST_TIME_CHECKED); if ($force || $lastTimeChecked === false || time() - $interval > $lastTimeChecked) { // set the time checked first, so that parallel Piwik requests don't all trigger the http requests Option::set(self::LAST_TIME_CHECKED, time(), $autoLoad = 1); $latestVersion = self::getLatestAvailableVersionNumber(); $latestVersion = trim((string) $latestVersion); if (!preg_match('~^[0-9][0-9a-zA-Z_.-]*$~D', $latestVersion)) { $latestVersion = ''; } Option::set(self::LATEST_VERSION, $latestVersion); } }
public function getTargets() { $targets = array(); if ($this->settings->trackToPiwik->getValue()) { $targets[] = array('url' => 'http://demo-anonymous.piwik.org/piwik.php', 'idSite' => 1, 'useAnonymization' => true); } $ownSiteId = $this->settings->ownPiwikSiteId->getValue(); if ($ownSiteId) { $piwikUrl = SettingsPiwik::getPiwikUrl(); if (!Common::stringEndsWith($piwikUrl, '/')) { $piwikUrl .= '/'; } $targets[] = array('url' => $piwikUrl . 'piwik.php', 'idSite' => (int) $ownSiteId, 'useAnonymization' => $this->settings->anonymizeSelfPiwik->getValue()); } $customUrl = $this->settings->customPiwikSiteUrl->getValue(); $customSiteId = $this->settings->customPiwikSiteId->getValue(); if ($customUrl && $customSiteId) { $targets[] = array('url' => $customUrl, 'idSite' => (int) $customSiteId, 'useAnonymization' => $this->settings->anonymizeCustomPiwik->getValue()); } return $targets; }
/** * Tracker requests will automatically trigger the Scheduled tasks. * This is useful for users who don't setup the cron, * but still want daily/weekly/monthly PDF reports emailed automatically. * * This is similar to calling the API CoreAdminHome.runScheduledTasks */ public function runScheduledTasks() { $now = time(); // Currently, there are no hourly tasks. When there are some, // this could be too aggressive minimum interval (some hours would be skipped in case of low traffic) $minimumInterval = TrackerConfig::getConfigValue('scheduled_tasks_min_interval'); // If the user disabled browser archiving, he has already setup a cron // To avoid parallel requests triggering the Scheduled Tasks, // Get last time tasks started executing $cache = Cache::getCacheGeneral(); if ($minimumInterval <= 0 || empty($cache['isBrowserTriggerEnabled'])) { Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled."); return; } $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval; if (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS || $cache['lastTrackerCronRun'] === false || $nextRunTime < $now) { $cache['lastTrackerCronRun'] = $now; Cache::setCacheGeneral($cache); Tracker::initCorePiwikInTrackerMode(); Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']); Common::printDebug('-> Scheduled Tasks: Starting...'); // save current user privilege and temporarily assume Super User privilege $isSuperUser = Piwik::hasUserSuperUserAccess(); // Scheduled tasks assume Super User is running Piwik::setUserHasSuperUserAccess(); $tokens = CronArchive::getSuperUserTokenAuths(); $tokenAuth = reset($tokens); $invokeScheduledTasksUrl = SettingsPiwik::getPiwikUrl() . "?module=API&format=csv&convertToUnicode=0&method=CoreAdminHome.runScheduledTasks&trigger=archivephp&token_auth={$tokenAuth}"; $cliMulti = new CliMulti(); $responses = $cliMulti->request(array($invokeScheduledTasksUrl)); $resultTasks = reset($responses); // restore original user privilege Piwik::setUserHasSuperUserAccess($isSuperUser); Common::printDebug($resultTasks); Common::printDebug('Finished Scheduled Tasks.'); } else { Common::printDebug("-> Scheduled tasks not triggered."); } Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC'); }
public function installOrUpdatePluginFromFile($pathToZip) { $tmpPluginFolder = PIWIK_USER_PATH . self::PATH_TO_DOWNLOAD . $this->pluginName; $tmpPluginFolder = SettingsPiwik::rewriteTmpPathWithHostname($tmpPluginFolder); try { $this->makeSureFoldersAreWritable(); $this->extractPluginFiles($pathToZip, $tmpPluginFolder); $this->makeSurePluginJsonExists($tmpPluginFolder); $metadata = $this->getPluginMetadataIfValid($tmpPluginFolder); $this->makeSureThereAreNoMissingRequirements($metadata); $this->pluginName = $metadata->name; $this->fixPluginFolderIfNeeded($tmpPluginFolder); $this->copyPluginToDestination($tmpPluginFolder); } catch (\Exception $e) { $this->removeFileIfExists($pathToZip); $this->removeFolderIfExists($tmpPluginFolder); throw $e; } $this->removeFileIfExists($pathToZip); $this->removeFolderIfExists($tmpPluginFolder); return $metadata; }
function getKeywordsForPage() { Piwik::checkUserHasViewAccess($this->idSite); $requestUrl = '&date=previous1' . '&period=week' . '&idSite=' . $this->idSite; $topPageUrlRequest = $requestUrl . '&method=Actions.getPageUrls' . '&filter_limit=50' . '&format=original'; $request = new Request($topPageUrlRequest); $request = $request->process(); /** @var $request Map */ $tables = $request->getDataTables(); $topPageUrl = false; $first = key($tables); if (!empty($first)) { $topPageUrls = $tables[$first]; $topPageUrls = $topPageUrls->getRowsMetadata('url'); $tmpTopPageUrls = array_values($topPageUrls); $topPageUrl = current($tmpTopPageUrls); } if (empty($topPageUrl)) { $topPageUrl = $this->site->getMainUrl(); } $url = $topPageUrl; // HTML $api = SettingsPiwik::getPiwikUrl() . '?module=API&method=Referrers.getKeywordsForPageUrl' . '&format=php' . '&filter_limit=10' . '&token_auth=' . Piwik::getCurrentUserTokenAuth(); $api .= $requestUrl; $code = ' // This function will call the API to get best keyword for current URL. // Then it writes the list of best keywords in a HTML list function DisplayTopKeywords($url = "") { // Do not spend more than 1 second fetching the data @ini_set("default_socket_timeout", $timeout = 1); // Get the Keywords data $url = empty($url) ? "http://". $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : $url; $api = "' . $api . '&url=" . urlencode($url); $keywords = @unserialize(file_get_contents($api)); if($keywords === false || isset($keywords["result"])) { // DEBUG ONLY: uncomment for troubleshooting an empty output (the URL output reveals the token_auth) // echo "Error while fetching the <a href=\'$api\'>Top Keywords from Piwik</a>"; return; } // Display the list in HTML $url = htmlspecialchars($url, ENT_QUOTES); $output = "<h2>Top Keywords for <a href=\'$url\'>$url</a></h2><ul>"; foreach($keywords as $keyword) { $output .= "<li>". $keyword . "</li>"; } if(empty($keywords)) { $output .= "Nothing yet..."; } $output .= "</ul>"; echo $output; } '; $jsonRequest = str_replace('format=php', 'format=json', $api); echo "<p>This widget is designed to work in your website directly.\n\t\tThis widget makes it easy to use Piwik to <i>automatically display the list of Top Keywords</i>, for each of your website Page URLs.</p>\n\t\t<p>\n\t\t<b>Example API URL</b> - For example if you would like to get the top 10 keywords, used last week, to land on the page <a target='_blank' href='{$topPageUrl}'>{$topPageUrl}</a>,\n\t\tin format JSON: you would dynamically fetch the data using <a target='_blank' href='{$jsonRequest}&url=" . urlencode($topPageUrl) . "'>this API request URL</a>. Make sure you encode the 'url' parameter in the URL.</p>\n\n\t\t<p><b>PHP Function ready to use!</b> - If you use PHP on your website, we have prepared a small code snippet that you can copy paste in your Website PHP files. You can then simply call the function <code>DisplayTopKeywords();</code> anywhere in your template, at the bottom of the content or in your blog sidebar.\n\t\tIf you run this code in your page {$topPageUrl}, it would output the following:"; echo "<div style='width:400px;margin-left:20px;padding:10px;border:1px solid black;'>"; function DisplayTopKeywords($url = "", $api) { // Do not spend more than 1 second fetching the data @ini_set("default_socket_timeout", $timeout = 1); // Get the Keywords data $url = empty($url) ? "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : $url; $api = $api . "&url=" . urlencode($url); $keywords = @unserialize(file_get_contents($api)); if ($keywords === false || isset($keywords["result"])) { // DEBUG ONLY: uncomment for troubleshooting an empty output (the URL output reveals the token_auth) //echo "Error while fetching the <a href=\'".$api."\'>Top Keywords from Piwik</a>"; return; } // Display the list in HTML $url = htmlspecialchars($url, ENT_QUOTES); $output = "<h2>Top Keywords for <a href=\\'{$url}\\'>{$url}</a></h2><ul>"; foreach ($keywords as $keyword) { $output .= "<li>" . $keyword . "</li>"; } if (empty($keywords)) { $output .= "Nothing yet..."; } $output .= "</ul>"; echo $output; } DisplayTopKeywords($topPageUrl, $api); echo "</div><br/>\n\t\t<p>Here is the PHP function that you can paste in your pages:</P>\n\t\t<textarea cols=60 rows=8><?php\n" . htmlspecialchars($code) . "\n DisplayTopKeywords();</textarea>\n\t\t"; echo "\n\t\t<p><strong>Notes</strong>: You can for example edit the code to to make the Top search keywords link to your Website search result pages.\n\t\t<br/>On medium to large traffic websites, we recommend to cache this data, as to minimize the performance impact of calling the Piwik API on each page view.\n\t\t</p>\n\t\t"; }
public function isEnabled() { return SettingsPiwik::isGitDeployment(); }