예제 #1
0
 static function update()
 {
     // force regeneration of cache files following #648
     Piwik::setUserHasSuperUserAccess();
     $allSiteIds = API::getInstance()->getAllSitesId();
     Cache::regenerateCacheWebsiteAttributes($allSiteIds);
 }
예제 #2
0
파일: APITest.php 프로젝트: cemo/piwik
 public function setUp()
 {
     parent::setUp();
     $this->api = API::getInstance();
     Fixture::createAccessInstance();
     Piwik::setUserHasSuperUserAccess();
     Fixture::createWebsite('2014-01-01 00:00:00');
     Fixture::createWebsite('2014-01-01 00:00:00');
 }
예제 #3
0
파일: SqlDump.php 프로젝트: a4tunado/piwik
 public function setUp()
 {
     // drop all tables
     Db::dropAllTables();
     // download data dump if url supplied
     if (is_file($this->dumpUrl)) {
         $dumpPath = $this->dumpUrl;
     } else {
         $dumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql.gz';
         $bufferSize = 1024 * 1024;
         $dump = fopen($this->dumpUrl, 'rb');
         $outfile = fopen($dumpPath, 'wb');
         $bytesRead = 0;
         while (!feof($dump)) {
             fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
             $bytesRead += $bufferSize;
         }
         fclose($dump);
         fclose($outfile);
         if ($bytesRead <= 40 * 1024 * 1024) {
             // sanity check
             throw new Exception("Could not download sql dump!");
         }
     }
     // unzip the dump
     if (substr($dumpPath, -3) === ".gz") {
         $deflatedDumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql';
         // TODO: should depend on name of URL
         exec("gunzip -c \"" . $dumpPath . "\" > \"{$deflatedDumpPath}\"", $output, $return);
         if ($return !== 0) {
             throw new Exception("gunzip failed: " . implode("\n", $output));
         }
     } else {
         $deflatedDumpPath = $dumpPath;
     }
     // load the data into the correct database
     $user = Config::getInstance()->database['username'];
     $password = Config::getInstance()->database['password'];
     Config::getInstance()->database['tables_prefix'] = $this->tablesPrefix;
     $cmd = "mysql -u \"{$user}\" \"--password={$password}\" {$this->dbName} < \"" . $deflatedDumpPath . "\" 2>&1";
     exec($cmd, $output, $return);
     if ($return !== 0) {
         throw new Exception("Failed to load sql dump: " . implode("\n", $output));
     }
     // make sure archiving will be called
     Rules::setBrowserTriggerArchiving(true);
     // reload access
     Piwik::setUserHasSuperUserAccess();
     $this->getTestEnvironment()->configOverride = array('database' => array('tables_prefix' => $this->tablesPrefix));
     $this->getTestEnvironment()->save();
 }
예제 #4
0
파일: Cache.php 프로젝트: carriercomm/piwik
 /**
  * Returns array containing data about the website: goals, URLs, etc.
  *
  * @param int $idSite
  * @return array
  */
 static function getCacheWebsiteAttributes($idSite)
 {
     if ($idSite == 'all') {
         return array();
     }
     $idSite = (int) $idSite;
     if ($idSite <= 0) {
         return array();
     }
     $cache = self::getInstance();
     if (($cacheContent = $cache->get($idSite)) !== false) {
         return $cacheContent;
     }
     Tracker::initCorePiwikInTrackerMode();
     // save current user privilege and temporarily assume Super User privilege
     $isSuperUser = Piwik::hasUserSuperUserAccess();
     Piwik::setUserHasSuperUserAccess();
     $content = array();
     /**
      * Triggered to get the attributes of a site entity that might be used by the
      * Tracker.
      * 
      * Plugins add new site attributes for use in other tracking events must
      * use this event to put those attributes in the Tracker Cache.
      * 
      * **Example**
      * 
      *     public function getSiteAttributes($content, $idSite)
      *     {
      *         $sql = "SELECT info FROM " . Common::prefixTable('myplugin_extra_site_info') . " WHERE idsite = ?";
      *         $content['myplugin_site_data'] = Db::fetchOne($sql, array($idSite));
      *     }
      * 
      * @param array &$content Array mapping of site attribute names with values.
      * @param int $idSite The site ID to get attributes for.
      */
     Piwik::postEvent('Tracker.Cache.getSiteAttributes', array(&$content, $idSite));
     Common::printDebug("Website {$idSite} tracker cache was re-created.");
     // restore original user privilege
     Piwik::setUserHasSuperUserAccess($isSuperUser);
     // if nothing is returned from the plugins, we don't save the content
     // this is not expected: all websites are expected to have at least one URL
     if (!empty($content)) {
         $cache->set($idSite, $content);
     }
     return $content;
 }
예제 #5
0
파일: 0.6.2.php 프로젝트: carriercomm/piwik
 static function update()
 {
     $obsoleteFiles = array(PIWIK_INCLUDE_PATH . '/core/Db/Mysqli.php');
     foreach ($obsoleteFiles as $obsoleteFile) {
         if (file_exists($obsoleteFile)) {
             @unlink($obsoleteFile);
         }
     }
     $obsoleteDirectories = array(PIWIK_INCLUDE_PATH . '/core/Db/Pdo');
     foreach ($obsoleteDirectories as $dir) {
         if (file_exists($dir)) {
             Filesystem::unlinkRecursive($dir, true);
         }
     }
     // force regeneration of cache files
     Piwik::setUserHasSuperUserAccess();
     $allSiteIds = API::getInstance()->getAllSitesId();
     Cache::regenerateCacheWebsiteAttributes($allSiteIds);
 }
예제 #6
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);
         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');
 }
예제 #7
0
 /**
  * @return array|bool
  */
 protected function updateComponents()
 {
     Access::getInstance();
     Piwik::setUserHasSuperUserAccess();
     $updater = new Updater();
     $componentsWithUpdateFile = CoreUpdater::getComponentUpdates($updater);
     if (empty($componentsWithUpdateFile)) {
         return false;
     }
     $result = CoreUpdater::updateComponents($updater, $componentsWithUpdateFile);
     return $result;
 }
예제 #8
0
파일: Tracker.php 프로젝트: Abine/piwik
 /**
  * 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
  */
 protected static 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 = Config::getInstance()->Tracker['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 (isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'] || $cache['lastTrackerCronRun'] === false || $nextRunTime < $now) {
         $cache['lastTrackerCronRun'] = $now;
         Cache::setCacheGeneral($cache);
         self::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();
         // While each plugins should ensure that necessary languages are loaded,
         // we ensure English translations at least are loaded
         Translate::loadEnglishTranslation();
         ob_start();
         CronArchive::$url = SettingsPiwik::getPiwikUrl();
         $cronArchive = new CronArchive();
         $cronArchive->runScheduledTasksInTrackerMode();
         $resultTasks = ob_get_contents();
         ob_clean();
         // restore original user privilege
         Piwik::setUserHasSuperUserAccess($isSuperUser);
         foreach (explode('</pre>', $resultTasks) as $resultTask) {
             Common::printDebug(str_replace('<pre>', '', $resultTask));
         }
         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');
 }
예제 #9
0
파일: Fixture.php 프로젝트: igorclark/piwik
 public function performSetUp($setupEnvironmentOnly = false)
 {
     try {
         if ($this->createConfig) {
             Config::getInstance()->setTestEnvironment();
         }
         $this->dbName = $this->getDbName();
         if ($this->persistFixtureData) {
             $this->dropDatabaseInSetUp = false;
             $this->dropDatabaseInTearDown = false;
             $this->overwriteExisting = false;
             $this->removeExistingSuperUser = false;
             Config::getInstance()->database_tests['dbname'] = Config::getInstance()->database['dbname'] = $this->dbName;
             $this->getTestEnvironment()->dbName = $this->dbName;
         }
         if ($this->dbName === false) {
             // must be after test config is created
             $this->dbName = Config::getInstance()->database['dbname'];
         }
         static::connectWithoutDatabase();
         if ($this->dropDatabaseInSetUp || $this->resetPersistedFixture) {
             $this->dropDatabase();
         }
         DbHelper::createDatabase($this->dbName);
         DbHelper::disconnectDatabase();
         // reconnect once we're sure the database exists
         Config::getInstance()->database['dbname'] = $this->dbName;
         Db::createDatabaseObject();
         Db::get()->query("SET wait_timeout=28800;");
         DbHelper::createTables();
         \Piwik\Plugin\Manager::getInstance()->unloadPlugins();
     } catch (Exception $e) {
         static::fail("TEST INITIALIZATION FAILED: " . $e->getMessage() . "\n" . $e->getTraceAsString());
     }
     include "DataFiles/SearchEngines.php";
     include "DataFiles/Socials.php";
     include "DataFiles/Languages.php";
     include "DataFiles/Countries.php";
     include "DataFiles/Currencies.php";
     include "DataFiles/LanguageToCountry.php";
     include "DataFiles/Providers.php";
     if (!$this->isFixtureSetUp()) {
         DbHelper::truncateAllTables();
     }
     static::createAccessInstance();
     // We need to be SU to create websites for tests
     Piwik::setUserHasSuperUserAccess();
     Cache::deleteTrackerCache();
     static::loadAllPlugins($this->getTestEnvironment(), $this->testCaseClass, $this->extraPluginsToLoad);
     self::updateDatabase();
     self::installAndActivatePlugins();
     $_GET = $_REQUEST = array();
     $_SERVER['HTTP_REFERER'] = '';
     // Make sure translations are loaded to check messages in English
     if ($this->loadTranslations) {
         Translate::reloadLanguage('en');
         APILanguageManager::getInstance()->setLanguageForUser('superUserLogin', 'en');
     }
     FakeAccess::$superUserLogin = '******';
     \Piwik\SettingsPiwik::$cachedKnownSegmentsToArchive = null;
     \Piwik\CacheFile::$invalidateOpCacheBeforeRead = true;
     if ($this->configureComponents) {
         \Piwik\Plugins\PrivacyManager\IPAnonymizer::deactivate();
         \Piwik\Plugins\PrivacyManager\DoNotTrackHeaderChecker::deactivate();
     }
     if ($this->createSuperUser) {
         self::createSuperUser($this->removeExistingSuperUser);
     }
     if ($setupEnvironmentOnly) {
         return;
     }
     $this->getTestEnvironment()->save();
     $this->getTestEnvironment()->executeSetupTestEnvHook();
     Piwik_TestingEnvironment::addSendMailHook();
     if ($this->overwriteExisting || !$this->isFixtureSetUp()) {
         $this->setUp();
         $this->markFixtureSetUp();
         $this->log("Database {$this->dbName} marked as successfully set up.");
     } else {
         $this->log("Using existing database {$this->dbName}.");
     }
 }
예제 #10
0
 public function setUp()
 {
     parent::setUp();
     \Piwik\Piwik::setUserHasSuperUserAccess(true);
     Fixture::createWebsite('2014-02-04');
 }
예제 #11
0
파일: Controller.php 프로젝트: bnkems/piwik
 /**
  * Get user information
  *
  * @param string $loginMail user login or email address
  * @return array ("login" => '...', "email" => '...', "password" => '...') or null, if user not found
  */
 protected function getUserInformation($loginMail)
 {
     Piwik::setUserHasSuperUserAccess();
     $user = null;
     if (API::getInstance()->userExists($loginMail)) {
         $user = API::getInstance()->getUser($loginMail);
     } else {
         if (API::getInstance()->userEmailExists($loginMail)) {
             $user = API::getInstance()->getUserByEmail($loginMail);
         }
     }
     return $user;
 }
예제 #12
0
 public function init()
 {
     // Note: the order of methods call matters here.
     $this->initCore();
     $this->initTokenAuth();
     $this->initCheckCli();
     $this->initStateFromParameters();
     Piwik::setUserHasSuperUserAccess(true);
     $this->logInitInfo();
     $this->checkPiwikUrlIsValid();
     $this->logArchiveTimeoutInfo();
     // record archiving start time
     Option::set(self::OPTION_ARCHIVING_STARTED_TS, time());
     $this->segments = $this->initSegmentsToArchive();
     $this->allWebsites = APISitesManager::getInstance()->getAllSitesId();
     if (!empty($this->shouldArchiveOnlySpecificPeriods)) {
         $this->log("- Will process the following periods: " . implode(", ", $this->shouldArchiveOnlySpecificPeriods) . " (--force-periods)");
     }
     $websitesIds = $this->initWebsiteIds();
     $this->filterWebsiteIds($websitesIds);
     if (!empty($this->shouldArchiveSpecifiedSites) || !empty($this->shouldArchiveAllSites) || !SharedSiteIds::isSupported()) {
         $this->websites = new FixedSiteIds($websitesIds);
     } else {
         $this->websites = new SharedSiteIds($websitesIds);
         if ($this->websites->getInitialSiteIds() != $websitesIds) {
             $this->log('Will ignore websites and help finish a previous started queue instead. IDs: ' . implode(', ', $this->websites->getInitialSiteIds()));
         }
     }
     if ($this->shouldStartProfiler) {
         \Piwik\Profiler::setupProfilerXHProf($mainRun = true);
         $this->log("XHProf profiling is enabled.");
     }
     /**
      * This event is triggered after a CronArchive instance is initialized.
      *
      * @param array $websiteIds The list of website IDs this CronArchive instance is processing.
      *                          This will be the entire list of IDs regardless of whether some have
      *                          already been processed.
      */
     Piwik::postEvent('CronArchive.init.finish', array($this->websites->getInitialSiteIds()));
 }
<?php

// Script that creates 100 websites, then outputs a IMG that records a pageview in each website
// Used initially to test how to handle cookies for this use case (see http://dev.piwik.org/trac/ticket/409)
use Piwik\Common;
use Piwik\FrontController;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API;
exit;
define('PIWIK_INCLUDE_PATH', '../..');
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
require_once PIWIK_INCLUDE_PATH . "/libs/PiwikTracker/PiwikTracker.php";
FrontController::getInstance()->init();
Piwik::setUserHasSuperUserAccess();
$count = 100;
for ($i = 0; $i <= $count; $i++) {
    $id = API::getInstance()->addSite(Common::getRandomString(), 'http://piwik.org');
    $t = new PiwikTracker($id, 'http://localhost/trunk/piwik.php');
    echo $id . " <img width=100 height=10 border=1 src='" . $t->getUrlTrackPageView('title') . "'><br/>";
}