Пример #1
0
 public static function setUpBeforeClass()
 {
     $dbName = false;
     if (!empty($GLOBALS['PIWIK_BENCHMARK_DATABASE'])) {
         $dbName = $GLOBALS['PIWIK_BENCHMARK_DATABASE'];
     }
     // connect to database
     self::createTestConfig();
     self::connectWithoutDatabase();
     // create specified fixture (global var not set, use default no-data fixture (see end of this file))
     if (empty($GLOBALS['PIWIK_BENCHMARK_FIXTURE'])) {
         $fixtureName = 'Piwik_Test_Fixture_EmptyOneSite';
     } else {
         $fixtureName = 'Piwik_Test_Fixture_' . $GLOBALS['PIWIK_BENCHMARK_FIXTURE'];
     }
     self::$fixture = new $fixtureName();
     // figure out if the desired fixture has already been setup, and if not empty the database
     $installedFixture = false;
     try {
         if (isset(self::$fixture->tablesPrefix)) {
             Config::getInstance()->database['tables_prefix'] = self::$fixture->tablesPrefix;
         }
         Db::query("USE " . $dbName);
         $installedFixture = \Piwik\Option::get('benchmark_fixture_name');
     } catch (Exception $ex) {
         // ignore
     }
     $createEmptyDatabase = $fixtureName != $installedFixture;
     parent::_setUpBeforeClass($dbName, $createEmptyDatabase);
     // if we created an empty database, setup the fixture
     if ($createEmptyDatabase) {
         self::$fixture->setUp();
         \Piwik\Option::set('benchmark_fixture_name', $fixtureName);
     }
 }
Пример #2
0
 /**
  * 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 (!self::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);
         $parameters = array('piwik_version' => Version::VERSION, 'php_version' => PHP_VERSION, 'url' => Url::getCurrentUrlWithoutQueryString(), 'trigger' => Common::getRequestVar('module', '', 'string'), 'timezone' => API::getInstance()->getDefaultTimezone());
         $url = Config::getInstance()->General['api_service_url'] . '/1.0/getLatestVersion/' . '?' . http_build_query($parameters, '', '&');
         $timeout = self::SOCKET_TIMEOUT;
         if (@Config::getInstance()->Debug['allow_upgrades_to_beta']) {
             $url = 'http://builds.piwik.org/LATEST_BETA';
         }
         try {
             $latestVersion = Http::sendHttpRequest($url, $timeout);
             if (!preg_match('~^[0-9][0-9a-zA-Z_.-]*$~D', $latestVersion)) {
                 $latestVersion = '';
             }
         } catch (Exception $e) {
             // e.g., disable_functions = fsockopen; allow_url_open = Off
             $latestVersion = '';
         }
         Option::set(self::LATEST_VERSION, $latestVersion);
     }
 }
Пример #3
0
 /**
  * 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);
         Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']);
         Common::printDebug('-> Scheduled Tasks: Starting...');
         $invokeScheduledTasksUrl = "?module=API&format=csv&convertToUnicode=0&method=CoreAdminHome.runScheduledTasks&trigger=archivephp";
         $cliMulti = new CliMulti();
         $cliMulti->runAsSuperUser();
         $responses = $cliMulti->request(array($invokeScheduledTasksUrl));
         $resultTasks = reset($responses);
         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');
 }
Пример #4
0
 public function testWebArchiving()
 {
     if (self::isMysqli() && self::isTravisCI()) {
         $this->markTestSkipped('Skipping on Mysqli as it randomly fails.');
     }
     $host = Fixture::getRootUrl();
     $token = Fixture::getTokenAuth();
     $urlTmp = Option::get('piwikUrl');
     Option::set('piwikUrl', $host . 'tests/PHPUnit/proxy/index.php');
     $url = $host . 'tests/PHPUnit/proxy/archive.php?token_auth=' . $token;
     $output = Http::sendHttpRequest($url, 600);
     // ignore random build issues
     if (empty($output) || strpos($output, \Piwik\CronArchive::NO_ERROR) === false) {
         $message = "This test has failed. Because it sometimes randomly fails, we skip the test, and ignore this failure.\n";
         $message .= "If you see this message often, or in every build, please investigate as this should only be a random and rare occurence!\n";
         $message .= "\n\narchive web failed: " . $output . "\n\nurl used: {$url}";
         $this->markTestSkipped($message);
     }
     if (!empty($urlTmp)) {
         Option::set('piwikUrl', $urlTmp);
     } else {
         Option::delete('piwikUrl');
     }
     $this->assertContains('Starting Piwik reports archiving...', $output);
     $this->assertContains('Archived website id = 1', $output);
     $this->assertContains('Done archiving!', $output);
     $this->compareArchivePhpOutputAgainstExpected($output);
 }
Пример #5
0
 /**
  * Record version of successfully completed component update
  *
  * @param string $name
  * @param string $version
  */
 public static function recordComponentSuccessfullyUpdated($name, $version)
 {
     try {
         Option::set(self::getNameInOptionTable($name), $version, $autoLoad = 1);
     } catch (\Exception $e) {
         // case when the option table is not yet created (before 0.2.10)
     }
 }
Пример #6
0
 static function update()
 {
     // when updating from pre-3.0 versions, set use_ldap_for_authentication to 0 and make sure
     // a warning displays in the UI to not set it to 1
     \Piwik\Config::getInstance()->LoginLdap['use_ldap_for_authentication'] = 0;
     \Piwik\Config::getInstance()->forceSave();
     Option::set('LoginLdap_updatedFromPre3_0', 1);
 }
Пример #7
0
 public function setSiteIdsToArchive($siteIds)
 {
     if (!empty($siteIds)) {
         Option::set('SharedSiteIdsToArchive', implode(',', $siteIds));
     } else {
         Option::delete('SharedSiteIdsToArchive');
     }
 }
Пример #8
0
 /**
  * Caches the intermediate DataTables used in the getIndividualReportsSummary and
  * getIndividualMetricsSummary reports in the option table.
  */
 public function cacheDataByArchiveNameReports()
 {
     $api = API::getInstance();
     $api->getIndividualReportsSummary(true);
     $api->getIndividualMetricsSummary(true);
     $now = Date::now()->getLocalized(Date::DATE_FORMAT_SHORT);
     Option::set(DBStats::TIME_OF_LAST_TASK_RUN_OPTION, $now);
 }
Пример #9
0
 public function setSiteIdsToArchive($siteIds)
 {
     if (!empty($siteIds)) {
         Option::set($this->optionName, implode(',', $siteIds));
     } else {
         Option::delete($this->optionName);
     }
 }
Пример #10
0
 public function setUp()
 {
     parent::setUp();
     $this->manager = new PrivacyManager();
     \Piwik\Option::set('delete_logs_enable', 1);
     \Piwik\Option::set('delete_logs_older_than', 270);
     \Piwik\Option::set('delete_reports_keep_week_reports', 1);
 }
 public function setUp()
 {
     parent::setUp();
     self::updateDatabase();
     self::createWebsite("2012-01-01 00:00:00");
     $fakeTasks = array('Piwik\\Plugins\\ExamplePlugin\\Tasks.myTask' => Date::factory("2012-01-02 02:33:45")->getTimestampUTC(), 'Piwik\\Plugins\\CoreUpdater\\Tasks.sendNotificationIfUpdateAvailable' => Date::factory("2012-03-04 13:45:56")->getTimestampUTC(), 'Piwik\\Plugins\\CorePluginsAdmin\\Tasks.sendNotificationIfUpdatesAvailable' => Date::factory("2012-02-12 15:12:10")->getTimestampUTC());
     Option::set('TaskScheduler.timetable', serialize($fakeTasks));
 }
Пример #12
0
 /**
  * @param string|null|false $licenseKey `null` or `false` will delete an existing a license key
  */
 public function set($licenseKey)
 {
     if (!isset($licenseKey) || $licenseKey === false) {
         $this->delete();
     } else {
         Option::set('marketplace_license_key', (string) $licenseKey);
     }
 }
Пример #13
0
 /**
  * Caches the intermediate DataTables used in the getIndividualReportsSummary and
  * getIndividualMetricsSummary reports in the option table.
  */
 public function cacheDataByArchiveNameReports()
 {
     $api = API::getInstance();
     $api->getIndividualReportsSummary(true);
     $api->getIndividualMetricsSummary(true);
     $now = Date::now()->getLocalized("%longYear%, %shortMonth% %day%");
     Option::set(DBStats::TIME_OF_LAST_TASK_RUN_OPTION, $now);
 }
Пример #14
0
 public function test_UpdateCommand_DoesNotExecuteUpdate_IfPiwikUpToDate()
 {
     Option::set('version_core', Version::VERSION);
     $result = $this->applicationTester->run(array('command' => 'core:update', '--yes' => true));
     $this->assertEquals(0, $result, $this->getCommandDisplayOutputErrorMessage());
     // check no update occurred
     $this->assertContains("Everything is already up to date.", $this->applicationTester->getDisplay());
     $this->assertEquals(Version::VERSION, Option::get('version_core'));
 }
 public function test_construct_CorrectlyConvertsOldListValues()
 {
     $oldItems = array('2015_01' => array(1, 2, 3), '2013_02' => array(3), 3 => '2015_03', '2014_01' => array(), 4 => '2015_06');
     Option::set(ArchivesToPurgeDistributedList::OPTION_INVALIDATED_DATES_SITES_TO_PURGE, serialize($oldItems));
     $list = new ArchivesToPurgeDistributedList();
     $items = $list->getAll();
     $expected = array('2015_03', '2015_06', '2015_01', '2013_02', '2014_01');
     $this->assertEquals($expected, array_values($items));
 }
Пример #16
0
 /**
  * Update the social definitions
  *
  * @see https://github.com/piwik/searchengine-and-social-list
  */
 public function updateSocials()
 {
     $url = 'https://raw.githubusercontent.com/piwik/searchengine-and-social-list/master/Socials.yml';
     $list = Http::sendHttpRequest($url, 30);
     $socials = Social::getInstance()->loadYmlData($list);
     if (count($socials) < 50) {
         return;
     }
     Option::set(Social::OPTION_STORAGE_NAME, base64_encode(serialize($socials)));
 }
Пример #17
0
 private function set($name, $value, $config)
 {
     if ('boolean' == $config['type']) {
         $value = $value ? '1' : '0';
     } else {
         settype($value, $config['type']);
     }
     Option::set($this->prefix($name), $value);
     Cache::clearCacheGeneral();
 }
Пример #18
0
 /**
  * Sets the contents of the list in the option table.
  *
  * @param string[] $items
  */
 public function setAll($items)
 {
     foreach ($items as $key => &$item) {
         if (is_array($item)) {
             throw new \InvalidArgumentException("Array item encountered in DistributedList::setAll() [ key = {$key} ].");
         } else {
             $item = (string) $item;
         }
     }
     Option::set($this->optionName, serialize($items));
 }
Пример #19
0
 public function setUp()
 {
     foreach ($this->fixtures as $fixture) {
         $fixture->setUp();
     }
     Option::set("Tests.forcedNowTimestamp", $this->now->getTimestamp());
     // launch archiving so tests don't run out of time
     $date = Date::factory($this->dateTime)->toString();
     VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date);
     VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date, urlencode($this->segment));
 }
Пример #20
0
 public function rememberToInvalidateArchivedReportsLater($idSite, Date $date)
 {
     $key = $this->buildRememberArchivedReportId($idSite, $date->toString());
     $value = Option::get($key);
     // we do not really have to get the value first. we could simply always try to call set() and it would update or
     // insert the record if needed but we do not want to lock the table (especially since there are still some
     // MyISAM installations)
     if (false === $value) {
         Option::set($key, '1');
     }
 }
Пример #21
0
 private static function updateIPAnonymizationSettings()
 {
     $optionName = 'PrivacyManager.ipAnonymizerEnabled';
     $value = Option::get($optionName);
     if ($value !== false) {
         // If the config is defined, nothing to do
         return;
     }
     // We disable IP anonymization if it wasn't configured (because by default it has gone from disabled to enabled)
     Option::set($optionName, '0');
 }
 /**
  * @test
  */
 public function should_load_spammer_list_from_options_if_exists()
 {
     // We store google.com in the spammer blacklist
     $list = serialize(array('google.com'));
     Option::set(ReferrerSpamFilter::OPTION_STORAGE_NAME, $list);
     $request = new Request(array('urlref' => 'semalt.com'));
     $this->assertFalse($this->filter->isSpam($request));
     // Now Google is blacklisted
     $request = new Request(array('urlref' => 'google.com'));
     $this->assertTrue($this->filter->isSpam($request));
     Option::delete(ReferrerSpamFilter::OPTION_STORAGE_NAME);
 }
Пример #23
0
 public function test_getViewDataTableParameters_removesNonOverridableParameter()
 {
     $params = array('flat' => '0', 'filters' => 'this is not overridable param and should fail', 'viewDataTable' => 'tableAllColumns');
     // 'filters' was removed
     $paramsExpectedWhenFetched = array('flat' => '0', 'viewDataTable' => 'tableAllColumns');
     $login = '******';
     $controllerAction = 'API.get';
     // simulate an invalid list of parameters (contains 'filters')
     $paramsKey = sprintf('viewDataTableParameters_%s_%s', $login, $controllerAction);
     Option::set($paramsKey, json_encode($params));
     // check the invalid list is fetched without the overridable parameter
     $processed = ViewDataTableManager::getViewDataTableParameters($login, $controllerAction);
     $this->assertEquals($paramsExpectedWhenFetched, $processed);
 }
Пример #24
0
 public function setUp()
 {
     parent::setUp();
     // make sure site has an early enough creation date (for period selector tests)
     Db::get()->update(Common::prefixTable("site"), array('ts_created' => '2011-01-01'), "idsite = 1");
     $this->addOverlayVisits();
     $this->addNewSitesForSiteSelector();
     DbHelper::createAnonymousUser();
     UsersManagerAPI::getInstance()->setSuperUserAccess('superUserLogin', true);
     Option::set("Tests.forcedNowTimestamp", $this->now->getTimestamp());
     // launch archiving so tests don't run out of time
     $date = Date::factory($this->dateTime)->toString();
     VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date);
     VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date, urlencode($this->segment));
 }
Пример #25
0
 private function loadDefinitions()
 {
     if ($this->definitionList === null) {
         // Read first from the auto-updated list in database
         $list = Option::get(self::OPTION_STORAGE_NAME);
         if ($list) {
             $this->definitionList = unserialize(base64_decode($list));
         } else {
             // Fallback to reading the bundled list
             $yml = file_get_contents(PIWIK_INCLUDE_PATH . self::DEFINITION_FILE);
             $this->definitionList = $this->loadYmlData($yml);
             Option::set(self::OPTION_STORAGE_NAME, base64_encode(serialize($this->definitionList)));
         }
     }
     Piwik::postEvent('Referrer.addSocialUrls', array(&$this->definitionList));
     return $this->definitionList;
 }
Пример #26
0
 /**
  * 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);
     }
 }
Пример #27
0
 /**
  * Saves the current time for a user as an option if the current request is for something
  * in the reporting UI, the current user is not anonymous and the time hasn't been saved
  * in the last 5 minutes.
  */
 public function logCurrentUserLastSeenTime()
 {
     $module = Common::getRequestVar('module', false);
     $currentUserLogin = Piwik::getCurrentUserLogin();
     // only log time for non-anonymous visits to the reporting UI
     if ($module == 'API' || $module == 'Proxy' || $currentUserLogin == 'anonymous') {
         return;
     }
     // get the last known time
     $optionName = self::OPTION_PREFIX . $currentUserLogin;
     $lastSeen = Option::get($optionName);
     // do not log if last known time is less than N minutes from now (so we don't make too many
     // queries)
     if (time() - $lastSeen <= self::LAST_TIME_SAVE_DELTA) {
         return;
     }
     // log last seen time (Note: autoload is important so the Option::get above does not result in
     // a separate query)
     Option::set($optionName, time(), $autoload = 1);
 }
Пример #28
0
 /**
  * Marks a component as successfully updated to a specific version in the database. Sets an option
  * that looks like `"version_$componentName"`.
  *
  * @param string $name The component name. Eg, a plugin name, `'core'` or dimension column name.
  * @param string $version The component version (should use semantic versioning).
  * @param bool   $isNew indicates if the component is a new one (for plugins)
  */
 public function markComponentSuccessfullyUpdated($name, $version, $isNew = false)
 {
     try {
         Option::set(self::getNameInOptionTable($name), $version, $autoLoad = 1);
     } catch (\Exception $e) {
         // case when the option table is not yet created (before 0.2.10)
     }
     if ($isNew) {
         /**
          * Event triggered after a new component has been installed.
          *
          * @param string $name The component that has been installed.
          */
         Piwik::postEvent('Updater.componentInstalled', array($name));
         return;
     }
     /**
      * Event triggered after a component has been updated.
      *
      * Can be used to handle logic that should be done after a component was updated
      *
      * **Example**
      *
      *     Piwik::addAction('Updater.componentUpdated', function ($componentName, $updatedVersion) {
      *          $mail = new Mail();
      *          $mail->setDefaultFromPiwik();
      *          $mail->addTo('*****@*****.**');
      *          $mail->setSubject('Component was updated);
      *          $message = sprintf(
      *              'Component %1$s has been updated to version %2$s',
      *              $componentName, $updatedVersion
      *          );
      *          $mail->setBodyText($message);
      *          $mail->send();
      *     });
      *
      * @param string $componentName 'core', plugin name or dimension name
      * @param string $updatedVersion version updated to
      */
     Piwik::postEvent('Updater.componentUpdated', array($name, $version));
 }
Пример #29
0
 public function test_WebArchiving()
 {
     if (self::isMysqli() && self::isTravisCI()) {
         $this->markTestSkipped('Skipping on Mysqli as it randomly fails.');
     }
     $host = Fixture::getRootUrl();
     $token = Fixture::getTokenAuth();
     $urlTmp = Option::get('piwikUrl');
     Option::set('piwikUrl', $host . 'tests/PHPUnit/proxy/index.php');
     $url = $host . 'tests/PHPUnit/proxy/archive.php?token_auth=' . $token;
     $output = Http::sendHttpRequest($url, 600);
     // ignore random build issues
     if (empty($output) || strpos($output, \Piwik\CronArchive::NO_ERROR) === false) {
         $this->fail("archive web failed: " . $output . "\n\nurl used: {$url}");
     }
     if (!empty($urlTmp)) {
         Option::set('piwikUrl', $urlTmp);
     } else {
         Option::delete('piwikUrl');
     }
     $this->assertWebArchivingDone($output);
 }
Пример #30
0
 private static function migrateExistingMobileMessagingOptions()
 {
     if (Option::get(MobileMessaging::DELEGATED_MANAGEMENT_OPTION) == 'true') {
         return;
     }
     // copy $superUserLogin_MobileMessagingSettings -> _MobileMessagingSettings as settings are managed globally
     $optionName = MobileMessaging::USER_SETTINGS_POSTFIX_OPTION;
     $superUsers = UsersManagerApi::getInstance()->getUsersHavingSuperUserAccess();
     if (empty($superUsers)) {
         return;
     }
     $firstSuperUser = array_shift($superUsers);
     if (empty($firstSuperUser)) {
         return;
     }
     $superUserLogin = $firstSuperUser['login'];
     $optionPrefixed = $superUserLogin . $optionName;
     // $superUserLogin_MobileMessagingSettings
     $value = Option::get($optionPrefixed);
     if (false !== $value) {
         // _MobileMessagingSettings
         Option::set($optionName, $value);
     }
 }