From 3de8570e3547116b422e6c578ee4f50c53ed1855 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 18 Mar 2013 22:32:32 +0100 Subject: [PATCH] Move basic template functions to their own class --- lib/template.php | 196 +++++---------------------- lib/template/base.php | 131 ++++++++++++++++++ lib/template/templatefilelocator.php | 43 ++++++ 3 files changed, 205 insertions(+), 165 deletions(-) create mode 100644 lib/template/base.php create mode 100644 lib/template/templatefilelocator.php diff --git a/lib/template.php b/lib/template.php index f1b03d6883..fc3c6b6d54 100644 --- a/lib/template.php +++ b/lib/template.php @@ -26,12 +26,9 @@ require_once __DIR__.'/template/functions.php'; /** * This class provides the templates for owncloud. */ -class OC_Template{ +class OC_Template extends \OC\Template\Base { private $renderas; // Create a full page? - private $application; // template Application - private $vars; // Vars - private $template; // The path to the template - private $l10n; // The l10n-Object + private $path; // The path to the template private $headers=array(); //custom headers /** @@ -48,13 +45,24 @@ class OC_Template{ * "admin". */ public function __construct( $app, $name, $renderas = "" ) { + // Read the selected theme from the config file + $theme = OC_Util::getTheme(); + + // Read the detected formfactor and use the right file name. + $fext = self::getFormFactorExtension(); + + $requesttoken = OC::$session ? OC_Util::callRegister() : ''; + + $parts = explode('/', $app); // fix translation when app is something like core/lostpassword + $l10n = OC_L10N::get($parts[0]); + + list($path, $template) = $this->findTemplate($theme, $app, $name, $fext); + // Set the private data $this->renderas = $renderas; - $this->application = $app; - $this->vars = array(); - $this->vars['requesttoken'] = OC::$session ? OC_Util::callRegister() : ''; - $parts = explode('/', $app); // fix translation when app is something like core/lostpassword - $this->l10n = OC_L10N::get($parts[0]); + $this->path = $path; + + parent::__construct($template, $requesttoken, $l10n); // Some headers to enhance security header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters @@ -78,7 +86,6 @@ class OC_Template{ .'media-src *'); header('Content-Security-Policy:'.$policy); // Standard - $this->findTemplate($name); } /** @@ -149,106 +156,17 @@ class OC_Template{ * Will select the template file for the selected theme and formfactor. * Checking all the possible locations. */ - protected function findTemplate($name) - { - // Read the selected theme from the config file - $theme = OC_Util::getTheme(); - - // Read the detected formfactor and use the right file name. - $fext = self::getFormFactorExtension(); - - $app = $this->application; + protected function findTemplate($theme, $app, $name, $fext) { // Check if it is a app template or not. - if( $app != "" ) { - // Check if the app is in the app folder or in the root - if( file_exists(OC_App::getAppPath($app)."/templates/" )) { - // Check if the template is overwritten by the selected theme - if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/apps/$app/templates/", $name, $fext)) { - }elseif ($this->checkPathForTemplate(OC_App::getAppPath($app)."/templates/", $name, $fext)) { - } - }else{ - // Check if the template is overwritten by the selected theme - if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/$app/templates/", $name, $fext)) { - }elseif ($this->checkPathForTemplate(OC::$SERVERROOT."/$app/templates/", $name, $fext)) { - }else{ - echo('template not found: template:'.$name.' formfactor:'.$fext - .' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT); - die(); - } - - } - }else{ - // Check if the template is overwritten by the selected theme - if ($this->checkPathForTemplate(OC::$SERVERROOT."/themes/$theme/core/templates/", $name, $fext)) { - } elseif ($this->checkPathForTemplate(OC::$SERVERROOT."/core/templates/", $name, $fext)) { - }else{ - echo('template not found: template:'.$name.' formfactor:'.$fext - .' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT); - die(); - } - } - } - - /** - * @brief check Path For Template with and without $fext - * @param string $path to check - * @param string $name of the template file (without suffix) - * @param string $fext formfactor extension - * @return bool true when found - * - * Will set $this->template and $this->path if there is a template at - * the specific $path - */ - protected function checkPathForTemplate($path, $name, $fext) - { - if ($name =='') return false; - $template = null; - if( is_file( $path.$name.$fext.'.php' )) { - $template = $path.$name.$fext.'.php'; - }elseif( is_file( $path.$name.'.php' )) { - $template = $path.$name.'.php'; - } - if ($template) { - $this->template = $template; - $this->path = $path; - return true; - } - return false; - } - - /** - * @brief Assign variables - * @param string $key key - * @param string $value value - * @return bool - * - * This function assigns a variable. It can be accessed via $_[$key] in - * the template. - * - * If the key existed before, it will be overwritten - */ - public function assign( $key, $value) { - $this->vars[$key] = $value; - return true; - } - - /** - * @brief Appends a variable - * @param string $key key - * @param string $value value - * @return bool - * - * This function assigns a variable in an array context. If the key already - * exists, the value will be appended. It can be accessed via - * $_[$key][$position] in the template. - */ - public function append( $key, $value ) { - if( array_key_exists( $key, $this->vars )) { - $this->vars[$key][] = $value; - } - else{ - $this->vars[$key] = array( $value ); + if( $app != '' ) { + $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app)); + } else { + $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT); } + $locator = new \OC\Template\TemplateFileLocator( $fext, $dirs ); + $template = $locator->find($name); + $path = $locator->getPath(); + return array($path, $template); } /** @@ -262,31 +180,14 @@ class OC_Template{ } /** - * @brief Prints the proceeded template + * @brief Process the template * @return bool * - * This function proceeds the template and prints its output. - */ - public function printPage() { - $data = $this->fetchPage(); - if( $data === false ) { - return false; - } - else{ - print $data; - return true; - } - } - - /** - * @brief Proceeds the template - * @return bool - * - * This function proceeds the template. If $this->renderas is set, it + * This function process the template. If $this->renderas is set, it * will produce a full page. */ public function fetchPage() { - $data = $this->_fetch(); + $data = parent::fetchPage(); if( $this->renderas ) { $page = new OC_TemplateLayout($this->renderas); @@ -305,27 +206,6 @@ class OC_Template{ } } - /** - * @brief doing the actual work - * @return string content - * - * Includes the template file, fetches its output - */ - private function _fetch() { - // Register the variables - $_ = $this->vars; - $l = $this->l10n; - - // Execute the template - ob_start(); - include $this->template; // <-- we have to use include because we pass $_! - $data = ob_get_contents(); - @ob_end_clean(); - - // return the data - return $data; - } - /** * @brief Include template * @return string returns content of included template @@ -334,21 +214,7 @@ class OC_Template{ * do this. */ public function inc( $file, $additionalparams = null ) { - $_ = $this->vars; - $l = $this->l10n; - - if( !is_null($additionalparams)) { - $_ = array_merge( $additionalparams, $this->vars ); - } - - // Include - ob_start(); - include $this->path.$file.'.php'; - $data = ob_get_contents(); - @ob_end_clean(); - - // Return data - return $data; + return $this->load($this->path.$file.'.php', $additionalparams); } /** diff --git a/lib/template/base.php b/lib/template/base.php new file mode 100644 index 0000000000..c7ddaa2e8e --- /dev/null +++ b/lib/template/base.php @@ -0,0 +1,131 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class Base { + private $template; // The template + private $vars; // Vars + private $l10n; // The l10n-Object + + public function __construct( $template, $requesttoken, $l10n ) { + $this->vars = array(); + $this->vars['requesttoken'] = $requesttoken; + $this->l10n = $l10n; + $this->template = $template; + } + + protected function getAppTemplateDirs($theme, $app, $serverroot, $app_dir) { + // Check if the app is in the app folder or in the root + if( file_exists($app_dir.'/templates/' )) { + return array( + $serverroot.'/themes/'.$theme.'/apps/'.$app.'/templates/', + $app_dir.'/templates/', + ); + } + return array( + $serverroot.'/themes/'.$theme.'/'.$app.'/templates/', + $serverroot.'/'.$app.'/templates/', + ); + } + + protected function getCoreTemplateDirs($theme, $serverroot) { + return array( + $serverroot.'/themes/'.$theme.'/core/templates/', + $serverroot.'/core/templates/', + ); + } + + /** + * @brief Assign variables + * @param string $key key + * @param string $value value + * @return bool + * + * This function assigns a variable. It can be accessed via $_[$key] in + * the template. + * + * If the key existed before, it will be overwritten + */ + public function assign( $key, $value) { + $this->vars[$key] = $value; + return true; + } + + /** + * @brief Appends a variable + * @param string $key key + * @param string $value value + * @return bool + * + * This function assigns a variable in an array context. If the key already + * exists, the value will be appended. It can be accessed via + * $_[$key][$position] in the template. + */ + public function append( $key, $value ) { + if( array_key_exists( $key, $this->vars )) { + $this->vars[$key][] = $value; + } + else{ + $this->vars[$key] = array( $value ); + } + } + + /** + * @brief Prints the proceeded template + * @return bool + * + * This function proceeds the template and prints its output. + */ + public function printPage() { + $data = $this->fetchPage(); + if( $data === false ) { + return false; + } + else{ + print $data; + return true; + } + } + + /** + * @brief Process the template + * @return bool + * + * This function processes the template. + */ + public function fetchPage() { + return $this->load($this->template); + } + + /** + * @brief doing the actual work + * @return string content + * + * Includes the template file, fetches its output + */ + protected function load( $file, $additionalparams = null ) { + // Register the variables + $_ = $this->vars; + $l = $this->l10n; + + if( !is_null($additionalparams)) { + $_ = array_merge( $additionalparams, $this->vars ); + } + + // Include + ob_start(); + include $file; + $data = ob_get_contents(); + @ob_end_clean(); + + // Return data + return $data; + } + +} diff --git a/lib/template/templatefilelocator.php b/lib/template/templatefilelocator.php new file mode 100644 index 0000000000..eeb27c139a --- /dev/null +++ b/lib/template/templatefilelocator.php @@ -0,0 +1,43 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class TemplateFileLocator { + protected $form_factor; + protected $dirs; + + public function __construct( $form_factor, $dirs ) { + $this->form_factor = $form_factor; + $this->dirs = $dirs; + } + + public function find( $template ) { + if ($template == '') { + throw new \InvalidArgumentException('Empty template name'); + } + + foreach($this->dirs as $dir) { + $file = $dir.$template.$this->form_factor.'.php'; + if (is_file($file)) { + $this->path = $dir; + return $file; + } + $file = $dir.$template.'.php'; + if (is_file($file)) { + $this->path = $dir; + return $file; + } + } + throw new \Exception('template file not found: template:'.$template.' formfactor:'.$this->form_factor); + } + + public function getPath() { + return $this->path; + } +}