property1 = "a view property"; * $view->property2 = "another view property"; * return $view->render(); * } * * * @api */ class View implements ViewInterface { private $template = ''; /** * Instance * @var Twig_Environment */ private $twig; private $templateVars = array(); private $contentType = 'text/html; charset=utf-8'; private $xFrameOptions = null; /** * Constructor. * * @param string $templateFile The template file to load. Must be in the following format: * `"@MyPlugin/templateFileName"`. Note the absence of .twig * from the end of the name. */ public function __construct($templateFile) { $templateExt = '.twig'; if (substr($templateFile, -strlen($templateExt)) !== $templateExt) { $templateFile .= $templateExt; } $this->template = $templateFile; $this->initializeTwig(); $this->piwik_version = Version::VERSION; $this->piwikUrl = Common::sanitizeInputValue(Url::getCurrentUrlWithoutFileName()); } /** * Returns the template filename. * * @return string */ public function getTemplateFile() { return $this->template; } /** * Returns the variables to bind to the template when rendering. * * @return array */ public function getTemplateVars() { return $this->templateVars; } /** * Directly assigns a variable to the view script. * Variable names may not be prefixed with '_'. * * @param string $key The variable name. * @param mixed $val The variable value. */ public function __set($key, $val) { $this->templateVars[$key] = $val; } /** * Retrieves an assigned variable. * Variable names may not be prefixed with '_'. * * @param string $key The variable name. * @return mixed The variable value. */ public function &__get($key) { return $this->templateVars[$key]; } private function initializeTwig() { $piwikTwig = new Twig(); $this->twig = $piwikTwig->getTwigEnvironment(); } /** * Renders the current view. Also sends the stored 'Content-Type' HTML header. * See {@link setContentType()}. * * @return string Generated template. */ public function render() { try { $this->currentModule = Piwik::getModule(); $this->currentAction = Piwik::getAction(); $userLogin = Piwik::getCurrentUserLogin(); $this->userLogin = $userLogin; $count = SettingsPiwik::getWebsitesCountToDisplay(); $sites = APISitesManager::getInstance()->getSitesWithAtLeastViewAccess($count); usort($sites, function ($site1, $site2) { return strcasecmp($site1["name"], $site2["name"]); }); $this->sites = $sites; $this->url = Common::sanitizeInputValue(Url::getCurrentUrl()); $this->token_auth = Piwik::getCurrentUserTokenAuth(); $this->userHasSomeAdminAccess = Piwik::isUserHasSomeAdminAccess(); $this->userIsSuperUser = Piwik::hasUserSuperUserAccess(); $this->latest_version_available = UpdateCheck::isNewestVersionAvailable(); $this->disableLink = Common::getRequestVar('disableLink', 0, 'int'); $this->isWidget = Common::getRequestVar('widget', 0, 'int'); if (Config::getInstance()->General['autocomplete_min_sites'] <= count($sites)) { $this->show_autocompleter = true; } else { $this->show_autocompleter = false; } $this->loginModule = Piwik::getLoginPluginName(); $user = APIUsersManager::getInstance()->getUser($userLogin); $this->userAlias = $user['alias']; } catch (Exception $e) { // can fail, for example at installation (no plugin loaded yet) } try { $this->totalTimeGeneration = Registry::get('timer')->getTime(); $this->totalNumberOfQueries = Profiler::getQueryCount(); } catch (Exception $e) { $this->totalNumberOfQueries = 0; } ProxyHttp::overrideCacheControlHeaders('no-store'); @header('Content-Type: ' . $this->contentType); // always sending this header, sometimes empty, to ensure that Dashboard embed loads (which could call this header() multiple times, the last one will prevail) @header('X-Frame-Options: ' . (string)$this->xFrameOptions); return $this->renderTwigTemplate(); } protected function renderTwigTemplate() { $output = $this->twig->render($this->template, $this->templateVars); $output = $this->applyFilter_cacheBuster($output); $helper = new Theme; $output = $helper->rewriteAssetsPathToTheme($output); return $output; } protected function applyFilter_cacheBuster($output) { $cacheBuster = UIAssetCacheBuster::getInstance()->piwikVersionBasedCacheBuster(); $tag = 'cb=' . $cacheBuster; $pattern = array( '~