2013-03-18 21:32:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Christopher Schäpers <kondou@ts.unde.re>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-03-18 21:32:32 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-03-18 21:32:32 +00:00
|
|
|
namespace OC\Template;
|
|
|
|
|
2017-04-07 20:42:43 +00:00
|
|
|
use OCP\Defaults;
|
|
|
|
|
2013-03-18 21:32:32 +00:00
|
|
|
class Base {
|
|
|
|
private $template; // The template
|
|
|
|
private $vars; // Vars
|
2016-04-12 10:49:11 +00:00
|
|
|
|
|
|
|
/** @var \OCP\IL10N */
|
|
|
|
private $l10n;
|
|
|
|
|
2017-04-07 20:42:43 +00:00
|
|
|
/** @var Defaults */
|
2016-04-12 10:49:11 +00:00
|
|
|
private $theme;
|
2013-03-18 21:32:32 +00:00
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $template
|
2016-01-25 16:15:54 +00:00
|
|
|
* @param string $requestToken
|
2016-04-12 10:49:11 +00:00
|
|
|
* @param \OCP\IL10N $l10n
|
2017-04-07 20:42:43 +00:00
|
|
|
* @param Defaults $theme
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2016-01-25 16:15:54 +00:00
|
|
|
public function __construct($template, $requestToken, $l10n, $theme ) {
|
2013-03-18 21:32:32 +00:00
|
|
|
$this->vars = array();
|
2016-01-25 16:15:54 +00:00
|
|
|
$this->vars['requesttoken'] = $requestToken;
|
2013-03-18 21:32:32 +00:00
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->template = $template;
|
2013-07-24 09:51:21 +00:00
|
|
|
$this->theme = $theme;
|
2013-03-18 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2016-04-12 10:49:11 +00:00
|
|
|
* @param string $serverRoot
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string|false $app_dir
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param string $theme
|
|
|
|
* @param string $app
|
2016-04-14 09:50:06 +00:00
|
|
|
* @return string[]
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2016-04-12 10:49:11 +00:00
|
|
|
protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
|
2013-03-18 21:32:32 +00:00
|
|
|
// Check if the app is in the app folder or in the root
|
|
|
|
if( file_exists($app_dir.'/templates/' )) {
|
2016-04-12 10:49:11 +00:00
|
|
|
return [
|
|
|
|
$serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
|
2013-03-18 21:32:32 +00:00
|
|
|
$app_dir.'/templates/',
|
2016-04-12 10:49:11 +00:00
|
|
|
];
|
2013-03-18 21:32:32 +00:00
|
|
|
}
|
2016-04-12 10:49:11 +00:00
|
|
|
return [
|
|
|
|
$serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
|
|
|
|
$serverRoot.'/'.$app.'/templates/',
|
|
|
|
];
|
2013-03-18 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2016-04-12 10:49:11 +00:00
|
|
|
* @param string $serverRoot
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param string $theme
|
2016-04-14 09:50:06 +00:00
|
|
|
* @return string[]
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2016-04-12 10:49:11 +00:00
|
|
|
protected function getCoreTemplateDirs($theme, $serverRoot) {
|
|
|
|
return [
|
|
|
|
$serverRoot.'/themes/'.$theme.'/core/templates/',
|
|
|
|
$serverRoot.'/core/templates/',
|
|
|
|
];
|
2013-03-18 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Assign variables
|
2013-03-18 21:32:32 +00:00
|
|
|
* @param string $key key
|
2014-03-03 16:32:29 +00:00
|
|
|
* @param array|bool|integer|string $value value
|
2013-03-18 21:32:32 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Appends a variable
|
2013-03-18 21:32:32 +00:00
|
|
|
* @param string $key key
|
2014-02-28 12:37:41 +00:00
|
|
|
* @param mixed $value value
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return boolean|null
|
2013-03-18 21:32:32 +00:00
|
|
|
*
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Prints the proceeded template
|
2013-03-18 21:32:32 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Process the template
|
2016-04-12 10:49:11 +00:00
|
|
|
*
|
|
|
|
* @param array|null $additionalParams
|
|
|
|
* @return string This function processes the template.
|
2013-03-18 21:32:32 +00:00
|
|
|
*
|
|
|
|
* This function processes the template.
|
|
|
|
*/
|
2016-04-12 10:49:11 +00:00
|
|
|
public function fetchPage($additionalParams = null) {
|
|
|
|
return $this->load($this->template, $additionalParams);
|
2013-03-18 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* doing the actual work
|
2016-04-12 10:49:11 +00:00
|
|
|
*
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param string $file
|
2016-04-12 10:49:11 +00:00
|
|
|
* @param array|null $additionalParams
|
2013-03-18 21:32:32 +00:00
|
|
|
* @return string content
|
|
|
|
*
|
|
|
|
* Includes the template file, fetches its output
|
|
|
|
*/
|
2016-04-12 10:49:11 +00:00
|
|
|
protected function load($file, $additionalParams = null) {
|
2013-03-18 21:32:32 +00:00
|
|
|
// Register the variables
|
|
|
|
$_ = $this->vars;
|
|
|
|
$l = $this->l10n;
|
2013-07-24 09:51:21 +00:00
|
|
|
$theme = $this->theme;
|
2013-03-18 21:32:32 +00:00
|
|
|
|
2018-01-25 18:04:17 +00:00
|
|
|
if(!is_null($additionalParams)) {
|
2016-04-12 10:49:11 +00:00
|
|
|
$_ = array_merge( $additionalParams, $this->vars );
|
2018-01-25 18:04:17 +00:00
|
|
|
foreach ($_ as $var => $value) {
|
|
|
|
${$var} = $value;
|
|
|
|
}
|
2013-03-18 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include
|
|
|
|
ob_start();
|
2016-04-14 09:22:38 +00:00
|
|
|
try {
|
|
|
|
include $file;
|
|
|
|
$data = ob_get_contents();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
@ob_end_clean();
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-03-18 21:32:32 +00:00
|
|
|
@ob_end_clean();
|
|
|
|
|
|
|
|
// Return data
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|