diff --git a/config/global.ini.php b/config/global.ini.php index cafd4ba47fe..e3a929d3e7c 100644 --- a/config/global.ini.php +++ b/config/global.ini.php @@ -105,7 +105,7 @@ ; The list of periods that are available in the Piwik calendar ; Example use case: custom date range requests are processed in real time, -; so they may take a few minutes on very high traffic website. You may remove "range" below. +; so they may take a few minutes on very high traffic website: you may remove "range" below to disable this period enabled_periods_UI = "day,week,month,year,range" enabled_periods_API = "day,week,month,year,range" diff --git a/core/Archive.php b/core/Archive.php index f4732c2751c..a271f409056 100644 --- a/core/Archive.php +++ b/core/Archive.php @@ -201,7 +201,7 @@ public static function build($idSites, $period, $strDate, $segment = false, $_re $websiteIds = Site::getIdSitesFromIdSitesString($idSites, $_restrictSitesToLogin); if (Period::isMultiplePeriod($strDate, $period)) { - $oPeriod = new Range($period, $strDate); + $oPeriod = Factory::build($period, $strDate); $allPeriods = $oPeriod->getSubperiods(); } else { $timezone = count($websiteIds) == 1 ? Site::getTimezoneFor($websiteIds[0]) : false; diff --git a/core/Period.php b/core/Period.php index 8ec4bf33457..9fc808e13df 100644 --- a/core/Period.php +++ b/core/Period.php @@ -62,7 +62,7 @@ public function __construct(Date $date) } /** - * @deprecated + * @deprecated Use Factory::build instead * @param $period * @param $date * @return Period diff --git a/core/Period/Factory.php b/core/Period/Factory.php index 7a909a182e0..2494da0a507 100644 --- a/core/Period/Factory.php +++ b/core/Period/Factory.php @@ -28,15 +28,15 @@ class Factory */ static public function build($period, $date) { + self::checkPeriodIsEnabled($period); + if (is_string($date)) { if (Period::isMultiplePeriod($date, $period) || $period == 'range') { - self::checkPeriodIsEnabled('range'); return new Range($period, $date); } $date = Date::factory($date); } - self::checkPeriodIsEnabled($period); switch ($period) { case 'day': @@ -57,7 +57,7 @@ static public function build($period, $date) } } - private static function checkPeriodIsEnabled($period) + public static function checkPeriodIsEnabled($period) { if(!self::isPeriodEnabledForAPI($period)) { self::throwExceptionInvalidPeriod($period); @@ -70,7 +70,9 @@ private static function checkPeriodIsEnabled($period) */ private static function throwExceptionInvalidPeriod($strPeriod) { - $message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, 'day, week, month, year, range')); + $periods = self::getPeriodsEnabledForAPI(); + $periods = implode(", ", $periods); + $message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, $periods)); throw new Exception($message); } @@ -92,7 +94,8 @@ public static function makePeriodFromQueryParams($timezone, $period, $date) } if ($period == 'range') { - $oPeriod = new Period\Range('range', $date, $timezone, Date::factory('today', $timezone)); + self::checkPeriodIsEnabled('range'); + $oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone)); } else { if (!($date instanceof Date)) { if ($date == 'now' || $date == 'today') { @@ -112,11 +115,19 @@ public static function makePeriodFromQueryParams($timezone, $period, $date) * @return bool */ public static function isPeriodEnabledForAPI($period) + { + $enabledPeriodsInAPI = self::getPeriodsEnabledForAPI(); + return in_array($period, $enabledPeriodsInAPI); + } + + /** + * @return array + */ + private static function getPeriodsEnabledForAPI() { $enabledPeriodsInAPI = Config::getInstance()->General['enabled_periods_API']; $enabledPeriodsInAPI = explode(",", $enabledPeriodsInAPI); $enabledPeriodsInAPI = array_map('trim', $enabledPeriodsInAPI); - - return in_array($period, $enabledPeriodsInAPI); + return $enabledPeriodsInAPI; } } \ No newline at end of file diff --git a/plugins/Live/Controller.php b/plugins/Live/Controller.php index 900010f157e..06adb63627c 100644 --- a/plugins/Live/Controller.php +++ b/plugins/Live/Controller.php @@ -105,7 +105,9 @@ public function getLastVisitsStart() { // hack, ensure we load today's visits by default $_GET['date'] = 'today'; + \Piwik\Period\Factory::checkPeriodIsEnabled('day'); $_GET['period'] = 'day'; + $view = new View('@Live/getLastVisitsStart'); $view->idSite = $this->idSite; $api = new Request("method=Live.getLastVisitsDetails&idSite={$this->idSite}&filter_limit=10&format=php&serialize=0&disable_generic_filters=1");