Move basic template functions to their own class
This commit is contained in:
parent
2593395561
commit
3de8570e35
196
lib/template.php
196
lib/template.php
|
@ -26,12 +26,9 @@ require_once __DIR__.'/template/functions.php';
|
||||||
/**
|
/**
|
||||||
* This class provides the templates for owncloud.
|
* This class provides the templates for owncloud.
|
||||||
*/
|
*/
|
||||||
class OC_Template{
|
class OC_Template extends \OC\Template\Base {
|
||||||
private $renderas; // Create a full page?
|
private $renderas; // Create a full page?
|
||||||
private $application; // template Application
|
private $path; // The path to the template
|
||||||
private $vars; // Vars
|
|
||||||
private $template; // The path to the template
|
|
||||||
private $l10n; // The l10n-Object
|
|
||||||
private $headers=array(); //custom headers
|
private $headers=array(); //custom headers
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,13 +45,24 @@ class OC_Template{
|
||||||
* "admin".
|
* "admin".
|
||||||
*/
|
*/
|
||||||
public function __construct( $app, $name, $renderas = "" ) {
|
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
|
// Set the private data
|
||||||
$this->renderas = $renderas;
|
$this->renderas = $renderas;
|
||||||
$this->application = $app;
|
$this->path = $path;
|
||||||
$this->vars = array();
|
|
||||||
$this->vars['requesttoken'] = OC::$session ? OC_Util::callRegister() : '';
|
parent::__construct($template, $requesttoken, $l10n);
|
||||||
$parts = explode('/', $app); // fix translation when app is something like core/lostpassword
|
|
||||||
$this->l10n = OC_L10N::get($parts[0]);
|
|
||||||
|
|
||||||
// Some headers to enhance security
|
// Some headers to enhance security
|
||||||
header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
|
header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
|
||||||
|
@ -78,7 +86,6 @@ class OC_Template{
|
||||||
.'media-src *');
|
.'media-src *');
|
||||||
header('Content-Security-Policy:'.$policy); // Standard
|
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.
|
* Will select the template file for the selected theme and formfactor.
|
||||||
* Checking all the possible locations.
|
* Checking all the possible locations.
|
||||||
*/
|
*/
|
||||||
protected function findTemplate($name)
|
protected function findTemplate($theme, $app, $name, $fext) {
|
||||||
{
|
|
||||||
// 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;
|
|
||||||
// Check if it is a app template or not.
|
// Check if it is a app template or not.
|
||||||
if( $app != "" ) {
|
if( $app != '' ) {
|
||||||
// Check if the app is in the app folder or in the root
|
$dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
|
||||||
if( file_exists(OC_App::getAppPath($app)."/templates/" )) {
|
} else {
|
||||||
// Check if the template is overwritten by the selected theme
|
$dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
|
||||||
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 );
|
|
||||||
}
|
}
|
||||||
|
$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
|
* @return bool
|
||||||
*
|
*
|
||||||
* This function proceeds the template and prints its output.
|
* This function process the template. If $this->renderas is set, it
|
||||||
*/
|
|
||||||
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
|
|
||||||
* will produce a full page.
|
* will produce a full page.
|
||||||
*/
|
*/
|
||||||
public function fetchPage() {
|
public function fetchPage() {
|
||||||
$data = $this->_fetch();
|
$data = parent::fetchPage();
|
||||||
|
|
||||||
if( $this->renderas ) {
|
if( $this->renderas ) {
|
||||||
$page = new OC_TemplateLayout($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
|
* @brief Include template
|
||||||
* @return string returns content of included template
|
* @return string returns content of included template
|
||||||
|
@ -334,21 +214,7 @@ class OC_Template{
|
||||||
* do this.
|
* do this.
|
||||||
*/
|
*/
|
||||||
public function inc( $file, $additionalparams = null ) {
|
public function inc( $file, $additionalparams = null ) {
|
||||||
$_ = $this->vars;
|
return $this->load($this->path.$file.'.php', $additionalparams);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,131 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue