Exemplo n.º 1
0
	protected function removeHoursInFuture($table, $idSite, $period, $date)
	{
		$site = new Piwik_Site($idSite);
		
		if(	$period == 'day'
			&& ($date == 'today'
				||  $date == Piwik_Date::factory('now', $site->getTimezone())->toString()))
		{
			$currentHour = Piwik_Date::factory('now', $site->getTimezone())->toString('G');
			// If no data for today, this is an exception to the API output rule, as we normally return nothing:
			// we shall return all hours of the day, with nb_visits = 0
			if($table->getRowsCount() == 0)
			{
				for($hour = 0; $hour <= $currentHour; $hour++)
				{
					$table->addRowFromSimpleArray( array('label' => $hour, 'nb_visits' => 0));
				}
				return $table;
			}
			
			$idsToDelete = array();
			foreach($table->getRows() as $id => $row)
			{
				$hour = $row->getColumn('label');
				if($hour > $currentHour)
				{
					$idsToDelete[] = $id;
				}
			}
			$table->deleteRows($idsToDelete);
		}
		return $table;
	}
Exemplo n.º 2
0
	protected function setDateTodayIfWebsiteCreatedToday()
	{
		$date = Piwik_Common::getRequestVar('date', false);
		if($date == 'today'
			|| Piwik_Common::getRequestVar('period', false) == 'range') 
		{
			return;
		} 
		$websiteId = Piwik_Common::getRequestVar('idSite', false, 'int');
		if ($websiteId) 
		{
			$website = new Piwik_Site($websiteId);
			$datetimeCreationDate = $this->site->getCreationDate()->getDatetime();
			$creationDateLocalTimezone = Piwik_Date::factory($datetimeCreationDate, $website->getTimezone())->toString('Y-m-d');
			$todayLocalTimezone = Piwik_Date::factory('now', $website->getTimezone())->toString('Y-m-d');
			if( $creationDateLocalTimezone == $todayLocalTimezone ) 
			{
				Piwik::redirectToModule( 'CoreHome', 'index', 
										array(	'date' => 'today', 
												'idSite' => $websiteId, 
												'period' => Piwik_Common::getRequestVar('period')) 
				);
			}
		}
	}
Exemplo n.º 3
0
 protected function removeHoursInFuture($table, $idSite, $period, $date)
 {
     $site = new Piwik_Site($idSite);
     if ($period == 'day' && ($date == 'today' || $date == Piwik_Date::factory('now', $site->getTimezone())->toString())) {
         $currentHour = Piwik_Date::factory('now', $site->getTimezone())->toString('G');
         $idsToDelete = array();
         foreach ($table->getRows() as $id => $row) {
             $hour = $row->getColumn('label');
             if ($hour > $currentHour) {
                 $idsToDelete[] = $id;
             }
         }
         $table->deleteRows($idsToDelete);
     }
     return $table;
 }
Exemplo n.º 4
0
 /**
  * Builds an array of Piwik_Archive of a given date range
  *
  * @param Piwik_Site $oSite 
  * @param string $strPeriod eg. 'day' 'week' etc.
  * @param string $strDate A date range, eg. 'last10', 'previous5' or 'YYYY-MM-DD,YYYY-MM-DD'
  */
 function __construct(Piwik_Site $oSite, $strPeriod, $strDate, Piwik_Segment $segment)
 {
     $rangePeriod = new Piwik_Period_Range($strPeriod, $strDate, $oSite->getTimezone());
     foreach ($rangePeriod->getSubperiods() as $subPeriod) {
         $startDate = $subPeriod->getDateStart();
         $archive = Piwik_Archive::build($oSite->getId(), $strPeriod, $startDate, $segment->getString());
         $archive->setSegment($segment);
         $this->archives[] = $archive;
     }
 }
Exemplo n.º 5
0
	/**
	 * Sets general variables to the view that are used by various templates and Javascript.
	 * If any error happens, displays the login screen
	 * @param Piwik_View $view
	 * @return void
	 */
	protected function setGeneralVariablesView($view)
	{
		$view->date = $this->strDate;
		
		try {
			$view->idSite = $this->idSite;
			if(empty($this->site) || empty($this->idSite))
			{
				throw new Exception("The requested website idSite is not found in the request, or is invalid.
				Please check that you are logged in Piwik and have permission to access the specified website.");
			}
			$this->setPeriodVariablesView($view);
			
			$rawDate = Piwik_Common::getRequestVar('date');
			$periodStr = Piwik_Common::getRequestVar('period');
			if($periodStr != 'range')
			{
				$date = Piwik_Date::factory($this->strDate);
				$period = Piwik_Period::factory($periodStr, $date);
			}
			else
			{
				$period = new Piwik_Period_Range($periodStr, $rawDate, $this->site->getTimezone());
			}
			$view->rawDate = $rawDate;
			$view->prettyDate = $period->getPrettyString();
			$view->siteName = $this->site->getName();
			$view->siteMainUrl = $this->site->getMainUrl();
			
			$datetimeMinDate = $this->site->getCreationDate()->getDatetime();
			$minDate = Piwik_Date::factory($datetimeMinDate, $this->site->getTimezone());
			$this->setMinDateView($minDate, $view);

			$maxDate = Piwik_Date::factory('now', $this->site->getTimezone());
			$this->setMaxDateView($maxDate, $view);
			
			// Setting current period start & end dates, for pre-setting the calendar when "Date Range" is selected 
			$dateStart = $period->getDateStart();
			if($dateStart->isEarlier($minDate)) { $dateStart = $minDate; } 
			$dateEnd = $period->getDateEnd();
			if($dateEnd->isLater($maxDate)) { $dateEnd = $maxDate; }
			
			$view->startDate = $dateStart;
			$view->endDate = $dateEnd;
			
			$this->setBasicVariablesView($view);
		} catch(Exception $e) {
			Piwik_ExitWithMessage($e->getMessage());
		}
	}
Exemplo n.º 6
0
 /**
  * Sets general variables to the view that are used by
  * various templates and Javascript.
  * If any error happens, displays the login screen
  *
  * @param Piwik_View $view
  * @throws Exception
  * @return void
  */
 protected function setGeneralVariablesView($view)
 {
     $view->date = $this->strDate;
     try {
         $view->idSite = $this->idSite;
         if (empty($this->site) || empty($this->idSite)) {
             throw new Exception("The requested website idSite is not found in the request, or is invalid.\n\t\t\t\tPlease check that you are logged in Piwik and have permission to access the specified website.");
         }
         $this->setPeriodVariablesView($view);
         $rawDate = Piwik_Common::getRequestVar('date');
         $periodStr = Piwik_Common::getRequestVar('period');
         if ($periodStr != 'range') {
             $date = Piwik_Date::factory($this->strDate);
             $period = Piwik_Period::factory($periodStr, $date);
         } else {
             $period = new Piwik_Period_Range($periodStr, $rawDate, $this->site->getTimezone());
         }
         $view->rawDate = $rawDate;
         $view->prettyDate = self::getCalendarPrettyDate($period);
         $view->siteName = $this->site->getName();
         $view->siteMainUrl = $this->site->getMainUrl();
         $datetimeMinDate = $this->site->getCreationDate()->getDatetime();
         $minDate = Piwik_Date::factory($datetimeMinDate, $this->site->getTimezone());
         $this->setMinDateView($minDate, $view);
         $maxDate = Piwik_Date::factory('now', $this->site->getTimezone());
         $this->setMaxDateView($maxDate, $view);
         // Setting current period start & end dates, for pre-setting the calendar when "Date Range" is selected
         $dateStart = $period->getDateStart();
         if ($dateStart->isEarlier($minDate)) {
             $dateStart = $minDate;
         }
         $dateEnd = $period->getDateEnd();
         if ($dateEnd->isLater($maxDate)) {
             $dateEnd = $maxDate;
         }
         $view->startDate = $dateStart;
         $view->endDate = $dateEnd;
         $language = Piwik_LanguagesManager::getLanguageForSession();
         $view->language = !empty($language) ? $language : Piwik_LanguagesManager::getLanguageCodeForCurrentUser();
         $view->config_action_url_category_delimiter = Piwik_Config::getInstance()->General['action_url_category_delimiter'];
         $this->setBasicVariablesView($view);
         $view->topMenu = Piwik_GetTopMenu();
     } catch (Exception $e) {
         Piwik_ExitWithMessage($e->getMessage(), '');
     }
 }
Exemplo n.º 7
0
 /**
  * Builds an Archive object or returns the same archive if previously built.
  *
  * @param string|int idSite integer, or comma separated list of integer
  * @param string|Piwik_Date $date 'YYYY-MM-DD' or magic keywords 'today' @see Piwik_Date::factory()
  * @param string $period 'week' 'day' etc.
  * @param string Segment definition - defaults to false for Backward Compatibility
  * 
  * @return Piwik_Archive
  */
 public static function build($idSite, $period, $strDate, $segment = false)
 {
     if ($idSite === 'all') {
         $sites = Piwik_SitesManager_API::getInstance()->getSitesIdWithAtLeastViewAccess();
     } else {
         $sites = Piwik_Site::getIdSitesFromIdSitesString($idSite);
     }
     if (!$segment instanceof Piwik_Segment) {
         $segment = new Piwik_Segment($segment, $idSite);
     }
     // idSite=1,3 or idSite=all
     if (count($sites) > 1 || $idSite === 'all') {
         $archive = new Piwik_Archive_Array_IndexedBySite($sites, $period, $strDate, $segment);
     } elseif (is_string($strDate) && self::isMultiplePeriod($strDate, $period)) {
         $oSite = new Piwik_Site($idSite);
         $archive = new Piwik_Archive_Array_IndexedByDate($oSite, $period, $strDate, $segment);
     } else {
         $oSite = new Piwik_Site($idSite);
         if ($period == 'range') {
             $oPeriod = new Piwik_Period_Range('range', $strDate, $oSite->getTimezone(), Piwik_Date::factory('today', $oSite->getTimezone()));
         } else {
             if (is_string($strDate)) {
                 if ($strDate == 'now' || $strDate == 'today') {
                     $strDate = date('Y-m-d', Piwik_Date::factory('now', $oSite->getTimezone())->getTimestamp());
                 } elseif ($strDate == 'yesterday' || $strDate == 'yesterdaySameTime') {
                     $strDate = date('Y-m-d', Piwik_Date::factory('now', $oSite->getTimezone())->subDay(1)->getTimestamp());
                 }
                 $oDate = Piwik_Date::factory($strDate);
             } else {
                 $oDate = $strDate;
             }
             $date = $oDate->toString();
             $oPeriod = Piwik_Period::factory($period, $oDate);
         }
         $archive = new Piwik_Archive_Single();
         $archive->setPeriod($oPeriod);
         $archive->setSite($oSite);
         $archive->setSegment($segment);
     }
     return $archive;
 }
Exemplo n.º 8
0
	private function loadLastVisitorDetailsFromDatabase($idSite, $period = false, $date = false, $segment = false, $filter_limit = false, $maxIdVisit = false, $visitorId = false, $minTimestamp = false)
	{
//		var_dump($period); var_dump($date); var_dump($filter_limit); var_dump($maxIdVisit); var_dump($visitorId);
//var_dump($minTimestamp);
		if(empty($filter_limit))
		{
			$filter_limit = 100;
		}
		$where = $whereBind = array();
		$where[] = "log_visit.idsite = ? ";
		$whereBind[] = $idSite;
		$orderBy = "idsite, visit_last_action_time DESC";
		$orderByParent = "sub.visit_last_action_time DESC";
		if(!empty($visitorId))
		{
			$where[] = "log_visit.idvisitor = ? ";
			$whereBind[] = Piwik_Common::hex2bin($visitorId);
		}

		if(!empty($maxIdVisit))
		{
			$where[] = "log_visit.idvisit < ? ";
			$whereBind[] = $maxIdVisit;
			$orderBy = "idvisit DESC";
			$orderByParent = "sub.idvisit DESC";
		}
		
		if(!empty($minTimestamp))
		{
			$where[] = "log_visit.visit_last_action_time > ? ";
			$whereBind[] = date("Y-m-d H:i:s", $minTimestamp);
		}
		
		// If no other filter, only look at the last 24 hours of stats
		if(empty($visitorId)
			&& empty($maxIdVisit)
			&& empty($period) 
			&& empty($date))
		{
			$period = 'day';
			$date = 'yesterdaySameTime';
		}

		// SQL Filter with provided period
		if (!empty($period) && !empty($date))
		{
			$currentSite = new Piwik_Site($idSite);
			$currentTimezone = $currentSite->getTimezone();
		
			$dateString = $date;
			if($period == 'range') 
			{ 
				$processedPeriod = new Piwik_Period_Range('range', $date);
				if($parsedDate = Piwik_Period_Range::parseDateRange($date))
				{
					$dateString = $parsedDate[2];
				}
			}
			else
			{
				$processedDate = Piwik_Date::factory($date);
				if($date == 'today'
					|| $date == 'now'
					|| $processedDate->toString() == Piwik_Date::factory('now', $currentTimezone)->toString())
				{
					$processedDate = $processedDate->subDay(1);
				}
				$processedPeriod = Piwik_Period::factory($period, $processedDate); 
			}
			$dateStart = $processedPeriod->getDateStart()->setTimezone($currentTimezone);
			$where[] = "log_visit.visit_last_action_time >= ?";
			$whereBind[] = $dateStart->toString('Y-m-d H:i:s');
			
			if(!in_array($date, array('now', 'today', 'yesterdaySameTime'))
				&& strpos($date, 'last') === false
				&& strpos($date, 'previous') === false
				&& Piwik_Date::factory($dateString)->toString('Y-m-d') != Piwik_Date::factory('now', $currentTimezone)->toString())
			{
				$dateEnd = $processedPeriod->getDateEnd()->setTimezone($currentTimezone);
				$where[] = " log_visit.visit_last_action_time <= ?";
				$dateEndString = $dateEnd->addDay(1)->toString('Y-m-d H:i:s');
				$whereBind[] = $dateEndString;
			}
		}

		$sqlWhere = "";
		if(count($where) > 0)
		{
			$sqlWhere = "
			WHERE " . join(" 
				AND ", $where);
		}

		$segment = new Piwik_Segment($segment, $idSite);
		$segmentSql = $segment->getSql();
		$sqlSegment = $segmentSql['sql'];
		if(!empty($sqlSegment)) $sqlSegment = ' AND '.$sqlSegment;
		$whereBind = array_merge ( $whereBind, $segmentSql['bind'] );
		
		// Subquery to use the indexes for ORDER BY
		// Group by idvisit so that a visitor converting 2 goals only appears twice
		$sql = "
				SELECT sub.* 
				FROM ( 
					SELECT 	*
					FROM " . Piwik_Common::prefixTable('log_visit') . " AS log_visit
					$sqlWhere
					$sqlSegment
					ORDER BY $orderBy
					LIMIT ".(int)$filter_limit."
				) AS sub
				GROUP BY sub.idvisit
				ORDER BY $orderByParent
			"; 
		try {
			$data = Piwik_FetchAll($sql, $whereBind);
		} catch(Exception $e) {
			echo $e->getMessage();exit;
		}
		
//var_dump($whereBind);	echo($sql);
//var_dump($data);
		return $data;
	}
 /**
  * Returns the minimum archive processed datetime to look at
  *  
  * @return string Datetime string, or false if must look at any archive available
  */
 public function getMinTimeArchivedProcessed()
 {
     $this->temporaryArchive = false;
     // if the current archive is a DAY and if it's today,
     // we set this minDatetimeArchiveProcessedUTC that defines the lifetime value of today's archive
     if ($this->period->getNumberOfSubperiods() == 0 && ($this->startTimestampUTC <= $this->time && $this->endTimestampUTC > $this->time)) {
         $this->temporaryArchive = true;
         $minDatetimeArchiveProcessedUTC = $this->time - self::getTodayArchiveTimeToLive();
         // see #1150; if new archives are not triggered from the browser,
         // we still want to try and return the latest archive available for today (rather than return nothing)
         if ($this->isArchivingDisabled()) {
             return false;
         }
     } elseif ($this->endTimestampUTC <= $this->time) {
         $minDatetimeArchiveProcessedUTC = $this->endTimestampUTC;
     } else {
         $this->temporaryArchive = true;
         // We choose to only look at archives that are newer than the specified timeout
         $minDatetimeArchiveProcessedUTC = $this->time - self::getTodayArchiveTimeToLive();
         // However, if archiving is disabled for this request, we shall
         // accept any archive that was processed today after 00:00:01 this morning
         if ($this->isArchivingDisabled()) {
             $timezone = $this->site->getTimezone();
             $minDatetimeArchiveProcessedUTC = Piwik_Date::factory(Piwik_Date::factory('now', $timezone)->getDateStartUTC())->setTimezone($timezone)->getTimestamp();
         }
     }
     return $minDatetimeArchiveProcessedUTC;
 }
Exemplo n.º 10
0
 /**
  * Returns true if it is likely that the data for this report has been purged and if the
  * user should be told about that.
  * 
  * In order for this function to return true, the following must also be true:
  * - The data table for this report must either be empty or not have been fetched.
  * - The period of this report is not a multiple period.
  * - The date of this report must be older than the delete_reports_older_than config option.
  * @return bool
  */
 public function hasReportBeenPurged()
 {
     $strPeriod = Piwik_Common::getRequestVar('period', false);
     $strDate = Piwik_Common::getRequestVar('date', false);
     if ($strPeriod !== false && $strDate !== false && (is_null($this->dataTable) || $this->dataTable->getRowsCount() == 0)) {
         // if range, only look at the first date
         if ($strPeriod == 'range') {
             $idSite = Piwik_Common::getRequestVar('idSite', '');
             if (intval($idSite) != 0) {
                 $site = new Piwik_Site($idSite);
                 $timezone = $site->getTimezone();
             } else {
                 $timezone = 'UTC';
             }
             $period = new Piwik_Period_Range('range', $strDate, $timezone);
             $reportDate = $period->getDateStart();
         } else {
             if (Piwik_Archive::isMultiplePeriod($strDate, $strPeriod)) {
                 return false;
             } else {
                 $reportDate = Piwik_Date::factory($strDate);
             }
         }
         $reportYear = $reportDate->toString('Y');
         $reportMonth = $reportDate->toString('m');
         if (class_exists('Piwik_PrivacyManager') && Piwik_PrivacyManager::shouldReportBePurged($reportYear, $reportMonth)) {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 11
0
 function test_setDefaultTimezoneAndCurrencyAndExcludedQueryParametersAndExcludedIps()
 {
     // test that they return default values
     $defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone();
     $this->assertEqual($defaultTimezone, 'UTC');
     $defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency();
     $this->assertEqual($defaultCurrency, 'USD');
     $excludedIps = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal();
     $this->assertEqual($excludedIps, '');
     $excludedQueryParameters = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal();
     $this->assertEqual($excludedQueryParameters, '');
     // test that when not specified, defaults are set as expected
     $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1", array('http://example.org'));
     $site = new Piwik_Site($idsite);
     $this->assertEqual($site->getTimezone(), 'UTC');
     $this->assertEqual($site->getCurrency(), 'USD');
     $this->assertEqual($site->getExcludedQueryParameters(), '');
     $this->assertEqual($site->getExcludedIps(), '');
     $this->assertEqual($site->isEcommerceEnabled(), false);
     // set the global timezone and get it
     $newDefaultTimezone = 'UTC+5.5';
     Piwik_SitesManager_API::getInstance()->setDefaultTimezone($newDefaultTimezone);
     $defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone();
     $this->assertEqual($defaultTimezone, $newDefaultTimezone);
     // set the default currency and get it
     $newDefaultCurrency = 'EUR';
     Piwik_SitesManager_API::getInstance()->setDefaultCurrency($newDefaultCurrency);
     $defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency();
     $this->assertEqual($defaultCurrency, $newDefaultCurrency);
     // set the global IPs to exclude and get it
     $newGlobalExcludedIps = '1.1.1.*,1.1.*.*,150.1.1.1';
     Piwik_SitesManager_API::getInstance()->setGlobalExcludedIps($newGlobalExcludedIps);
     $globalExcludedIps = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal();
     $this->assertEqual($globalExcludedIps, $newGlobalExcludedIps);
     // set the global URL query params to exclude and get it
     $newGlobalExcludedQueryParameters = 'PHPSESSID,blabla, TesT';
     // removed the space
     $expectedGlobalExcludedQueryParameters = 'PHPSESSID,blabla,TesT';
     Piwik_SitesManager_API::getInstance()->setGlobalExcludedQueryParameters($newGlobalExcludedQueryParameters);
     $globalExcludedQueryParameters = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal();
     $this->assertEqual($globalExcludedQueryParameters, $expectedGlobalExcludedQueryParameters);
     // create a website and check that default currency and default timezone are set
     // however, excluded IPs and excluded query Params are not returned
     $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 1, '', '', $newDefaultTimezone);
     $site = new Piwik_Site($idsite);
     $this->assertEqual($site->getTimezone(), $newDefaultTimezone);
     $this->assertEqual($site->getCreationDate()->toString(), date('Y-m-d'));
     $this->assertEqual($site->getCurrency(), $newDefaultCurrency);
     $this->assertEqual($site->getExcludedIps(), '');
     $this->assertEqual($site->getExcludedQueryParameters(), '');
     $this->assertEqual($site->isEcommerceEnabled(), true);
 }
Exemplo n.º 12
0
 /**
  * Creates a period instance using a Piwik_Site instance and two strings describing
  * the period & date.
  * 
  * @param Piwik_Site $site
  * @param string $strPeriod The period string: day, week, month, year, range
  * @param string $strDate The date or date range string.
  * @return Piwik_Period
  */
 public static function makePeriodFromQueryParams($site, $strPeriod, $strDate)
 {
     $tz = $site->getTimezone();
     if ($strPeriod == 'range') {
         $oPeriod = new Piwik_Period_Range('range', $strDate, $tz, Piwik_Date::factory('today', $tz));
     } else {
         if (is_string($strDate)) {
             if ($strDate == 'now' || $strDate == 'today') {
                 $strDate = date('Y-m-d', Piwik_Date::factory('now', $tz)->getTimestamp());
             } elseif ($strDate == 'yesterday' || $strDate == 'yesterdaySameTime') {
                 $strDate = date('Y-m-d', Piwik_Date::factory('now', $tz)->subDay(1)->getTimestamp());
             }
             $oDate = Piwik_Date::factory($strDate);
         } else {
             $oDate = $strDate;
         }
         $date = $oDate->toString();
         $oPeriod = Piwik_Period::factory($strPeriod, $oDate);
     }
     return $oPeriod;
 }