2013-03-19 01:32:32 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Christopher Schäpers <kondou@ts.unde.re>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-03-19 01:32:32 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-03-19 01:32:32 +04:00
|
|
|
namespace OC\Template;
|
|
|
|
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
|
|
|
|
2013-03-19 01:32:32 +04:00
|
|
|
class Base {
|
|
|
|
private $template; // The template
|
|
|
|
private $vars; // Vars
|
2016-04-12 13:49:11 +03:00
|
|
|
|
|
|
|
/** @var \OCP\IL10N */
|
|
|
|
private $l10n;
|
|
|
|
|
2017-04-07 23:42:43 +03:00
|
|
|
/** @var Defaults */
|
2016-04-12 13:49:11 +03:00
|
|
|
private $theme;
|
2013-03-19 01:32:32 +04:00
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $template
|
2016-01-25 19:15:54 +03:00
|
|
|
* @param string $requestToken
|
2016-04-12 13:49:11 +03:00
|
|
|
* @param \OCP\IL10N $l10n
|
2017-04-07 23:42:43 +03:00
|
|
|
* @param Defaults $theme
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function __construct($template, $requestToken, $l10n, $theme) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->vars = [];
|
2016-01-25 19:15:54 +03:00
|
|
|
$this->vars['requesttoken'] = $requestToken;
|
2013-03-19 01:32:32 +04:00
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->template = $template;
|
2013-07-24 13:51:21 +04:00
|
|
|
$this->theme = $theme;
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2016-04-12 13:49:11 +03:00
|
|
|
* @param string $serverRoot
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string|false $app_dir
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $theme
|
|
|
|
* @param string $app
|
2016-04-14 12:50:06 +03:00
|
|
|
* @return string[]
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2016-04-12 13:49:11 +03:00
|
|
|
protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
|
2013-03-19 01:32:32 +04:00
|
|
|
// Check if the app is in the app folder or in the root
|
2020-04-10 15:19:56 +03:00
|
|
|
if (file_exists($app_dir.'/templates/')) {
|
2016-04-12 13:49:11 +03:00
|
|
|
return [
|
|
|
|
$serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
|
2013-03-19 01:32:32 +04:00
|
|
|
$app_dir.'/templates/',
|
2016-04-12 13:49:11 +03:00
|
|
|
];
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
2016-04-12 13:49:11 +03:00
|
|
|
return [
|
|
|
|
$serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
|
|
|
|
$serverRoot.'/'.$app.'/templates/',
|
|
|
|
];
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2016-04-12 13:49:11 +03:00
|
|
|
* @param string $serverRoot
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $theme
|
2016-04-14 12:50:06 +03:00
|
|
|
* @return string[]
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2016-04-12 13:49:11 +03:00
|
|
|
protected function getCoreTemplateDirs($theme, $serverRoot) {
|
|
|
|
return [
|
|
|
|
$serverRoot.'/themes/'.$theme.'/core/templates/',
|
|
|
|
$serverRoot.'/core/templates/',
|
|
|
|
];
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Assign variables
|
2013-03-19 01:32:32 +04:00
|
|
|
* @param string $key key
|
2014-03-03 20:32:29 +04:00
|
|
|
* @param array|bool|integer|string $value value
|
2013-03-19 01:32:32 +04: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
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function assign($key, $value) {
|
2013-03-19 01:32:32 +04:00
|
|
|
$this->vars[$key] = $value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Appends a variable
|
2013-03-19 01:32:32 +04:00
|
|
|
* @param string $key key
|
2014-02-28 16:37:41 +04:00
|
|
|
* @param mixed $value value
|
2013-03-19 01:32:32 +04: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.
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function append($key, $value) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (array_key_exists($key, $this->vars)) {
|
2013-03-19 01:32:32 +04:00
|
|
|
$this->vars[$key][] = $value;
|
2020-04-10 15:19:56 +03:00
|
|
|
} else {
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->vars[$key] = [ $value ];
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Prints the proceeded template
|
2013-03-19 01:32:32 +04:00
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* This function proceeds the template and prints its output.
|
|
|
|
*/
|
|
|
|
public function printPage() {
|
|
|
|
$data = $this->fetchPage();
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($data === false) {
|
2013-03-19 01:32:32 +04:00
|
|
|
return false;
|
2020-04-10 15:19:56 +03:00
|
|
|
} else {
|
2013-03-19 01:32:32 +04:00
|
|
|
print $data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Process the template
|
2016-04-12 13:49:11 +03:00
|
|
|
*
|
|
|
|
* @param array|null $additionalParams
|
|
|
|
* @return string This function processes the template.
|
2013-03-19 01:32:32 +04:00
|
|
|
*
|
|
|
|
* This function processes the template.
|
|
|
|
*/
|
2016-04-12 13:49:11 +03:00
|
|
|
public function fetchPage($additionalParams = null) {
|
|
|
|
return $this->load($this->template, $additionalParams);
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* doing the actual work
|
2016-04-12 13:49:11 +03:00
|
|
|
*
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $file
|
2016-04-12 13:49:11 +03:00
|
|
|
* @param array|null $additionalParams
|
2013-03-19 01:32:32 +04:00
|
|
|
* @return string content
|
|
|
|
*
|
|
|
|
* Includes the template file, fetches its output
|
|
|
|
*/
|
2016-04-12 13:49:11 +03:00
|
|
|
protected function load($file, $additionalParams = null) {
|
2013-03-19 01:32:32 +04:00
|
|
|
// Register the variables
|
|
|
|
$_ = $this->vars;
|
|
|
|
$l = $this->l10n;
|
2013-07-24 13:51:21 +04:00
|
|
|
$theme = $this->theme;
|
2013-03-19 01:32:32 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($additionalParams)) {
|
2020-04-09 17:07:47 +03:00
|
|
|
$_ = array_merge($additionalParams, $this->vars);
|
2018-01-25 21:04:17 +03:00
|
|
|
foreach ($_ as $var => $value) {
|
|
|
|
${$var} = $value;
|
|
|
|
}
|
2013-03-19 01:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include
|
|
|
|
ob_start();
|
2016-04-14 12:22:38 +03:00
|
|
|
try {
|
|
|
|
include $file;
|
|
|
|
$data = ob_get_contents();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
@ob_end_clean();
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-03-19 01:32:32 +04:00
|
|
|
@ob_end_clean();
|
|
|
|
|
|
|
|
// Return data
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|