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; protected $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->userLogin = Piwik::getCurrentUserLogin(); $this->isSuperUser = Access::getInstance()->hasSuperUserAccess(); try { $this->piwikUrl = SettingsPiwik::getPiwikUrl(); } catch (Exception $ex) { // pass (occurs when DB cannot be connected to, perhaps piwik URL cache should be stored in config file...) } } /** * Returns the template filename. * * @return string */ public function getTemplateFile() { return $this->template; } /** * Returns the variables to bind to the template when rendering. * * @param array $override Template variable override values. Mainly useful * when including View templates in other templates. * @return array */ public function getTemplateVars($override = array()) { return $override + $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]; } /** * Returns true if a template variable has been set or not. * * @param string $name The name of the template variable. * @return bool */ public function __isset($name) { return isset($this->templateVars[$name]); } 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(); $this->url = Common::sanitizeInputValue(Url::getCurrentUrl()); $this->token_auth = Piwik::getCurrentUserTokenAuth(); $this->userHasSomeAdminAccess = Piwik::isUserHasSomeAdminAccess(); $this->userIsAnonymous = Piwik::isUserIsAnonymous(); $this->userIsSuperUser = Piwik::hasUserSuperUserAccess(); $this->latest_version_available = UpdateCheck::isNewestVersionAvailable(); $this->disableLink = Common::getRequestVar('disableLink', 0, 'int'); $this->isWidget = Common::getRequestVar('widget', 0, 'int'); $piwikAds = StaticContainer::get('Piwik\ProfessionalServices\Advertising'); $this->areAdsForProfessionalServicesEnabled = $piwikAds->areAdsForProfessionalServicesEnabled(); if (Development::isEnabled()) { $cacheBuster = rand(0, 10000); } else { $cacheBuster = UIAssetCacheBuster::getInstance()->piwikVersionBasedCacheBuster(); } $this->cacheBuster = $cacheBuster; $this->loginModule = Piwik::getLoginPluginName(); $user = APIUsersManager::getInstance()->getUser($this->userLogin); $this->userAlias = $user['alias']; } catch (Exception $e) { Log::debug($e); // can fail, for example at installation (no plugin loaded yet) } ProxyHttp::overrideCacheControlHeaders('no-store'); Common::sendHeader('Content-Type: ' . $this->contentType); // always sending this header, sometimes empty, to ensure that Dashboard embed loads // - when calling sendHeader() multiple times, the last one prevails Common::sendHeader('X-Frame-Options: ' . (string)$this->xFrameOptions); return $this->renderTwigTemplate(); } protected function renderTwigTemplate() { try { $output = $this->twig->render($this->getTemplateFile(), $this->getTemplateVars()); } catch (Exception $ex) { // twig does not rethrow exceptions, it wraps them so we log the cause if we can find it $cause = $ex->getPrevious(); Log::debug($cause === null ? $ex : $cause); throw $ex; } $output = $this->applyFilter_cacheBuster($output); $helper = new Theme; $output = $helper->rewriteAssetsPathToTheme($output); return $output; } protected function applyFilter_cacheBuster($output) { $assetManager = AssetManager::getInstance(); $stylesheet = $assetManager->getMergedStylesheetAsset(); if ($stylesheet->exists()) { $content = $stylesheet->getContent(); } else { $content = $assetManager->getMergedStylesheet()->getContent(); } $cacheBuster = UIAssetCacheBuster::getInstance(); $tagJs = 'cb=' . $cacheBuster->piwikVersionBasedCacheBuster(); $tagCss = 'cb=' . $cacheBuster->md5BasedCacheBuster($content); $pattern = array( '~