private function loadJsonMetadata($path) { if (!file_exists($path)) { return array(); } $json = file_get_contents($path); if (!$json) { return array(); } $info = Common::json_decode($json, $assoc = true); if (!is_array($info) || empty($info)) { throw new Exception("Invalid JSON file: {$path}"); } return $info; }
/** * Returns backlink statistics including the count of backlinks and count of * referrer domains (domains with backlinks). * * This method issues an HTTP request and waits for it to return. * * @param string $siteDomain The domain of the website to get stats for. * @param int $timeout The number of seconds to wait before aborting * the HTTP request. * @return array An array containing the backlink count and referrer * domain count: * array( * 'backlink_count' => X, * 'referrer_domains_count' => Y * ) * If either stat is false, either the API returned an * error, or the IP was blocked for this request. */ public function getBacklinkStats($siteDomain, $timeout = 300) { $apiUrl = $this->getApiUrl($method = 'GetBacklinkStats', $args = array('items' => '1', 'item0' => $siteDomain)); $apiResponse = Http::sendHttpRequest($apiUrl, $timeout); $result = array('backlink_count' => false, 'referrer_domains_count' => false); $apiResponse = Common::json_decode($apiResponse, $assoc = true); if (!empty($apiResponse) && !empty($apiResponse['Data'])) { $siteSeoStats = reset($apiResponse['Data']); if (isset($siteSeoStats['ExtBackLinks']) && $siteSeoStats['ExtBackLinks'] !== -1) { $result['backlink_count'] = $siteSeoStats['ExtBackLinks']; } if (isset($siteSeoStats['RefDomains']) && $siteSeoStats['RefDomains'] !== -1) { $result['referrer_domains_count'] = $siteSeoStats['RefDomains']; } } return $result; }
private function getUserSettings($user) { $optionIndex = $user . MobileMessaging::USER_SETTINGS_POSTFIX_OPTION; $userSettings = Option::get($optionIndex); if (empty($userSettings)) { $userSettings = array(); } else { $userSettings = Common::json_decode($userSettings, true); } return $userSettings; }
/** * Returns Items read from the request string * @return array|bool */ private function getEcommerceItemsFromRequest() { $items = Common::unsanitizeInputValue($this->request->getParam('ec_items')); if (empty($items)) { Common::printDebug("There are no Ecommerce items in the request"); // we still record an Ecommerce order without any item in it return array(); } $items = Common::json_decode($items, $assoc = true); if (!is_array($items)) { Common::printDebug("Error while json_decode the Ecommerce items = " . var_export($items, true)); return false; } $cleanedItems = $this->getCleanedEcommerceItems($items); return $cleanedItems; }
/** * @param string $key * @param string $value * @param $row * @return bool True if the $row metrics were already added to the ->metrics */ protected function aggregateEcommerceCategories($key, $value, $row) { $ecommerceCategoriesAggregated = false; if ($key == '_pkc' && $value[0] == '[' && $value[1] == '"') { // In case categories were truncated, try closing the array if (substr($value, -2) != '"]') { $value .= '"]'; } $decoded = @Common::json_decode($value); if (is_array($decoded)) { $count = 0; foreach ($decoded as $category) { if (empty($category) || $count >= GoalManager::MAXIMUM_PRODUCT_CATEGORIES) { continue; } $this->aggregateActionByKeyAndValue($key, $category, $row); $ecommerceCategoriesAggregated = true; $count++; } } } return $ecommerceCategoriesAggregated; }
/** * Generates a report file. * * @param int $idReport ID of the report to generate. * @param string $date YYYY-MM-DD * @param bool|false|string $language If not passed, will use default language. * @param bool|false|int $outputType 1 = download report, 2 = save report to disk, 3 = output report in browser, 4 = return report content to caller, defaults to download * @param bool|false|string $period Defaults to 'day'. If not specified, will default to the report's period set when creating the report * @param bool|false|string $reportFormat 'pdf', 'html' or any other format provided via the ScheduledReports.getReportFormats hook * @param bool|false|array $parameters array of parameters * @return array|void */ public function generateReport($idReport, $date, $language = false, $outputType = false, $period = false, $reportFormat = false, $parameters = false) { Piwik::checkUserIsNotAnonymous(); // load specified language if (empty($language)) { $language = Translate::getLanguageDefault(); } Translate::reloadLanguage($language); $reports = $this->getReports($idSite = false, $_period = false, $idReport); $report = reset($reports); $idSite = $report['idsite']; $login = $report['login']; $reportType = $report['type']; $this->checkUserHasViewPermission($login, $idSite); // override report period if (empty($period)) { $period = $report['period']; } // override report format if (!empty($reportFormat)) { self::validateReportFormat($reportType, $reportFormat); $report['format'] = $reportFormat; } else { $reportFormat = $report['format']; } // override and/or validate report parameters $report['parameters'] = Common::json_decode(self::validateReportParameters($reportType, empty($parameters) ? $report['parameters'] : $parameters), true); // available reports $availableReportMetadata = \Piwik\Plugins\API\API::getInstance()->getReportMetadata($idSite); // we need to lookup which reports metadata are registered in this report $reportMetadata = array(); foreach ($availableReportMetadata as $metadata) { if (in_array($metadata['uniqueId'], $report['reports'])) { $reportMetadata[] = $metadata; } } // the report will be rendered with the first 23 rows and will aggregate other rows in a summary row // 23 rows table fits in one portrait page $initialFilterTruncate = Common::getRequestVar('filter_truncate', false); $_GET['filter_truncate'] = self::REPORT_TRUNCATE; $prettyDate = null; $processedReports = array(); $segment = self::getSegment($report['idsegment']); foreach ($reportMetadata as $action) { $apiModule = $action['module']; $apiAction = $action['action']; $apiParameters = array(); if (isset($action['parameters'])) { $apiParameters = $action['parameters']; } $mustRestoreGET = false; // all Websites dashboard should not be truncated in the report if ($apiModule == 'MultiSites') { $mustRestoreGET = $_GET; $_GET['enhanced'] = true; if ($apiAction == 'getAll') { $_GET['filter_truncate'] = false; // when a view/admin user created a report, workaround the fact that "Super User" // is enforced in Scheduled tasks, and ensure Multisites.getAll only return the websites that this user can access $userLogin = $report['login']; if (!empty($userLogin) && !Piwik::hasTheUserSuperUserAccess($userLogin)) { $_GET['_restrictSitesToLogin'] = $userLogin; } } } $processedReport = \Piwik\Plugins\API\API::getInstance()->getProcessedReport($idSite, $period, $date, $apiModule, $apiAction, $segment != null ? urlencode($segment['definition']) : false, $apiParameters, $idGoal = false, $language); $processedReport['segment'] = $segment; // TODO add static method getPrettyDate($period, $date) in Period $prettyDate = $processedReport['prettyDate']; if ($mustRestoreGET) { $_GET = $mustRestoreGET; } $processedReports[] = $processedReport; } // restore filter truncate parameter value if ($initialFilterTruncate !== false) { $_GET['filter_truncate'] = $initialFilterTruncate; } /** * Triggered when generating the content of scheduled reports. * * This event can be used to modify the report data or report metadata of one or more reports * in a scheduled report, before the scheduled report is rendered and delivered. * * TODO: list data available in $report or make it a new class that can be documented (same for * all other events that use a $report) * * @param array &$processedReports The list of processed reports in the scheduled * report. Entries includes report data and metadata for each report. * @param string $reportType A string ID describing how the scheduled report will be sent, eg, * `'sms'` or `'email'`. * @param string $outputType The output format of the report, eg, `'html'`, `'pdf'`, etc. * @param array $report An array describing the scheduled report that is being * generated. */ Piwik::postEvent(self::PROCESS_REPORTS_EVENT, array(&$processedReports, $reportType, $outputType, $report)); $reportRenderer = null; /** * Triggered when obtaining a renderer instance based on the scheduled report output format. * * Plugins that provide new scheduled report output formats should use this event to * handle their new report formats. * * @param ReportRenderer &$reportRenderer This variable should be set to an instance that * extends {@link Piwik\ReportRenderer} by one of the event * subscribers. * @param string $reportType A string ID describing how the report is sent, eg, * `'sms'` or `'email'`. * @param string $outputType The output format of the report, eg, `'html'`, `'pdf'`, etc. * @param array $report An array describing the scheduled report that is being * generated. */ Piwik::postEvent(self::GET_RENDERER_INSTANCE_EVENT, array(&$reportRenderer, $reportType, $outputType, $report)); if (is_null($reportRenderer)) { throw new Exception("A report renderer was not supplied in the event " . self::GET_RENDERER_INSTANCE_EVENT); } // init report renderer $reportRenderer->setLocale($language); // render report $description = str_replace(array("\r", "\n"), ' ', $report['description']); list($reportSubject, $reportTitle) = self::getReportSubjectAndReportTitle(Site::getNameFor($idSite), $report['reports']); $filename = "{$reportTitle} - {$prettyDate} - {$description}"; $reportRenderer->renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata, $segment); array_walk($processedReports, array($reportRenderer, 'renderReport')); switch ($outputType) { case self::OUTPUT_SAVE_ON_DISK: $outputFilename = strtoupper($reportFormat) . ' ' . ucfirst($reportType) . ' Report - ' . $idReport . '.' . $date . '.' . $idSite . '.' . $language; $outputFilename = $reportRenderer->sendToDisk($outputFilename); $additionalFiles = $this->getAttachments($reportRenderer, $report, $processedReports, $prettyDate); return array($outputFilename, $prettyDate, $reportSubject, $reportTitle, $additionalFiles); break; case self::OUTPUT_INLINE: $reportRenderer->sendToBrowserInline($filename); break; case self::OUTPUT_RETURN: return $reportRenderer->getRenderedReport(); break; default: case self::OUTPUT_DOWNLOAD: $reportRenderer->sendToBrowserDownload($filename); break; } }
public function decodeLayout($layout) { if ($this->isAlreadyDecodedLayout($layout)) { return $layout; } $layout = html_entity_decode($layout); $layout = str_replace("\\\"", "\"", $layout); $layout = str_replace("\n", "", $layout); return Common::json_decode($layout, $assoc = false); }