public function sendHeader() { if ($this->isJsonp()) { Common::sendHeader('Content-Type: application/javascript; charset=utf-8'); } else { Renderer\Json::sendHeaderJSON(); } ProxyHttp::overrideCacheControlHeaders(); }
public function setPluginSettings() { Piwik::checkUserIsNotAnonymous(); Json::sendHeaderJSON(); $nonce = Common::getRequestVar('nonce', null, 'string'); if (!Nonce::verifyNonce(static::SET_PLUGIN_SETTINGS_NONCE, $nonce)) { return json_encode(array('result' => 'error', 'message' => Piwik::translate('General_ExceptionNonceMismatch'))); } $pluginsSettings = SettingsManager::getPluginSettingsForCurrentUser(); try { foreach ($pluginsSettings as $pluginName => $pluginSetting) { foreach ($pluginSetting->getSettingsForCurrentUser() as $setting) { $value = $this->findSettingValueFromRequest($pluginName, $setting->getKey()); if (!is_null($value)) { $setting->setValue($value); } } } foreach ($pluginsSettings as $pluginSetting) { $pluginSetting->save(); } } catch (Exception $e) { $message = html_entity_decode($e->getMessage(), ENT_QUOTES, 'UTF-8'); return json_encode(array('result' => 'error', 'message' => $message)); } Nonce::discardNonce(static::SET_PLUGIN_SETTINGS_NONCE); return json_encode(array('result' => 'success')); }
/** * @group Core */ public function testRenderArray5() { $data = array('a' => 'b'); $render = new Json(); $render->setTable($data); $expected = '[{"a":"b"}]'; $this->assertEquals($expected, $render->render()); }
public function copyDashboardToUser() { $this->checkTokenInUrl(); if (!Piwik::hasUserSuperUserAccess()) { return '0'; } $login = Piwik::getCurrentUserLogin(); $name = urldecode(Common::getRequestVar('name', '', 'string')); $user = urldecode(Common::getRequestVar('user', '', 'string')); $idDashboard = Common::getRequestVar('dashboardId', 0, 'int'); $layout = $this->dashboard->getLayoutForUser($login, $idDashboard); if ($layout !== false) { $nextId = $this->getNextIdDashboard($user); $query = sprintf('INSERT INTO %s (login, iddashboard, name, layout) VALUES (?, ?, ?, ?)', Common::prefixTable('user_dashboard')); Db::query($query, array($user, $nextId, $name, $layout)); Json::sendHeaderJSON(); return Common::json_encode($nextId); } }
/** * Render a multidimensional array to Json * Handle DataTable|Set elements in the first dimension only, following case does not work: * array( * array( * DataTable, * 2 => array( * 1, * 2 * ), * ), * ); * * @param array $array can contain scalar, arrays, DataTable and Set * @return string */ public static function convertMultiDimensionalArrayToJson($array) { $jsonRenderer = new Json(); $jsonRenderer->setTable($array); $renderedReport = $jsonRenderer->render(); return $renderedReport; }
public function sendHeader() { Renderer\Json::sendHeaderJSON(); ProxyHttp::overrideCacheControlHeaders(); }
/** * Starts or continues a download for a missing GeoIP database. A database is missing if * it has an update URL configured, but the actual database is not available in the misc * directory. * * Input: * 'url' - The URL to download the database from. * 'continue' - 1 if we're continuing a download, 0 if we're starting one. * * Output: * 'error' - If an error occurs this describes the error. * 'to_download' - The URL of a missing database that should be downloaded next (if any). * 'to_download_label' - The label to use w/ the progress bar that describes what we're * downloading. * 'current_size' - Size of the current file on disk. * 'expected_file_size' - Size of the completely downloaded file. */ public function downloadMissingGeoIpDb() { $this->dieIfGeolocationAdminIsDisabled(); Piwik::checkUserHasSuperUserAccess(); if ($_SERVER["REQUEST_METHOD"] == "POST") { try { $this->checkTokenInUrl(); Json::sendHeaderJSON(); // based on the database type (provided by the 'key' query param) determine the // url & output file name $key = Common::getRequestVar('key', null, 'string'); $url = GeoIPAutoUpdater::getConfiguredUrl($key); $ext = GeoIPAutoUpdater::getGeoIPUrlExtension($url); $filename = GeoIp::$dbNames[$key][0] . '.' . $ext; if (substr($filename, 0, 15) == 'GeoLiteCity.dat') { $filename = 'GeoIPCity.dat' . substr($filename, 15); } $outputPath = GeoIp::getPathForGeoIpDatabase($filename); // download part of the file $result = Http::downloadChunk($url, $outputPath, Common::getRequestVar('continue', true, 'int')); // if the file is done if ($result['current_size'] >= $result['expected_file_size']) { GeoIPAutoUpdater::unzipDownloadedFile($outputPath, $unlink = true); $info = $this->getNextMissingDbUrlInfo(); if ($info !== false) { return json_encode($info); } } return json_encode($result); } catch (Exception $ex) { return json_encode(array('error' => $ex->getMessage())); } } }
public function copyDashboardToUser() { $this->checkTokenInUrl(); if (!Piwik::hasUserSuperUserAccess()) { return '0'; } $login = Piwik::getCurrentUserLogin(); $name = urldecode(Common::getRequestVar('name', '', 'string')); $user = urldecode(Common::getRequestVar('user', '', 'string')); $idDashboard = Common::getRequestVar('dashboardId', 0, 'int'); $layout = $this->dashboard->getLayoutForUser($login, $idDashboard); if ($layout !== false) { $nextId = $this->getModel()->createNewDashboardForUser($user, $name, $layout); Json::sendHeaderJSON(); return Common::json_encode($nextId); } }
public function hasConversions() { $this->checkSitePermission(); $idGoal = Common::getRequestVar('idGoal', '', 'string'); $idSite = Common::getRequestVar('idSite', null, 'int'); $period = Common::getRequestVar('period', null, 'string'); $date = Common::getRequestVar('date', null, 'string'); Piwik::checkUserHasViewAccess($idSite); $conversions = new Conversions(); Json::sendHeaderJSON(); $numConversions = $conversions->getConversionForGoal($idGoal, $idSite, $period, $date); return json_encode($numConversions > 0); }
function getSitesForAutocompleter() { $pattern = Common::getRequestVar('term'); $sites = API::getInstance()->getPatternMatchSites($pattern); $pattern = str_replace('%', '', $pattern); if (!count($sites)) { $results[] = array('label' => Piwik::translate('SitesManager_NotFound') . " <span class='autocompleteMatched'>{$pattern}</span>.", 'id' => '#'); } else { if (strpos($pattern, '/') !== false && strpos($pattern, '\\/') === false) { $pattern = str_replace('/', '\\/', $pattern); } foreach ($sites as $s) { $siteName = Site::getNameFor($s['idsite']); $label = $siteName; if (strlen($pattern) > 0) { @preg_match_all("/{$pattern}+/i", $label, $matches); if (is_array($matches[0]) && count($matches[0]) >= 1) { foreach ($matches[0] as $match) { $label = str_replace($match, '<span class="autocompleteMatched">' . $match . '</span>', $siteName); } } } $results[] = array('label' => $label, 'id' => $s['idsite'], 'name' => $siteName); } } Json::sendHeaderJSON(); print Common::json_encode($results); }