2013-03-18 22:32:32 +01:00
|
|
|
<?php
|
2024-05-23 09:26:56 +02:00
|
|
|
|
2013-03-18 22:32:32 +01:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-03-18 22:32:32 +01:00
|
|
|
*/
|
|
|
|
namespace OC\Template;
|
|
|
|
|
2017-04-07 15:42:43 -05:00
|
|
|
use OCP\Defaults;
|
|
|
|
|
2013-03-18 22:32:32 +01:00
|
|
|
class Base {
|
|
|
|
private $template; // The template
|
2025-02-25 18:44:02 +01:00
|
|
|
private array $vars = [];
|
2016-04-12 12:49:11 +02:00
|
|
|
|
|
|
|
/** @var \OCP\IL10N */
|
|
|
|
private $l10n;
|
|
|
|
|
2017-04-07 15:42:43 -05:00
|
|
|
/** @var Defaults */
|
2016-04-12 12:49:11 +02:00
|
|
|
private $theme;
|
2013-03-18 22:32:32 +01:00
|
|
|
|
2014-02-06 16:30:58 +01:00
|
|
|
/**
|
|
|
|
* @param string $template
|
2016-01-25 17:15:54 +01:00
|
|
|
* @param string $requestToken
|
2016-04-12 12:49:11 +02:00
|
|
|
* @param \OCP\IL10N $l10n
|
2024-08-01 23:06:55 +02:00
|
|
|
* @param string $cspNonce
|
2017-04-07 15:42:43 -05:00
|
|
|
* @param Defaults $theme
|
2014-02-06 16:30:58 +01:00
|
|
|
*/
|
2024-08-01 23:06:55 +02:00
|
|
|
public function __construct($template, $requestToken, $l10n, $theme, $cspNonce) {
|
|
|
|
$this->vars = [
|
|
|
|
'cspNonce' => $cspNonce,
|
|
|
|
'requesttoken' => $requestToken,
|
|
|
|
];
|
2013-03-18 22:32:32 +01:00
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->template = $template;
|
2013-07-24 11:51:21 +02:00
|
|
|
$this->theme = $theme;
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 16:30:58 +01:00
|
|
|
/**
|
2016-04-12 12:49:11 +02:00
|
|
|
* @param string $serverRoot
|
2014-02-06 16:30:58 +01:00
|
|
|
* @param string|false $app_dir
|
2014-02-19 09:31:54 +01:00
|
|
|
* @param string $theme
|
|
|
|
* @param string $app
|
2016-04-14 11:50:06 +02:00
|
|
|
* @return string[]
|
2014-02-06 16:30:58 +01:00
|
|
|
*/
|
2016-04-12 12:49:11 +02:00
|
|
|
protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
|
2013-03-18 22:32:32 +01:00
|
|
|
// Check if the app is in the app folder or in the root
|
2021-07-30 19:28:52 +00:00
|
|
|
if ($app_dir !== false && file_exists($app_dir . '/templates/')) {
|
2016-04-12 12:49:11 +02:00
|
|
|
return [
|
|
|
|
$serverRoot . '/themes/' . $theme . '/apps/' . $app . '/templates/',
|
2013-03-18 22:32:32 +01:00
|
|
|
$app_dir . '/templates/',
|
2016-04-12 12:49:11 +02:00
|
|
|
];
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
2016-04-12 12:49:11 +02:00
|
|
|
return [
|
|
|
|
$serverRoot . '/themes/' . $theme . '/' . $app . '/templates/',
|
|
|
|
$serverRoot . '/' . $app . '/templates/',
|
|
|
|
];
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 16:30:58 +01:00
|
|
|
/**
|
2016-04-14 11:50:06 +02:00
|
|
|
* @return string[]
|
2014-02-06 16:30:58 +01:00
|
|
|
*/
|
2025-02-25 18:44:02 +01:00
|
|
|
protected function getCoreTemplateDirs(string $theme, string $serverRoot): array {
|
2016-04-12 12:49:11 +02:00
|
|
|
return [
|
|
|
|
$serverRoot . '/themes/' . $theme . '/core/templates/',
|
|
|
|
$serverRoot . '/core/templates/',
|
|
|
|
];
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 17:50:53 +02:00
|
|
|
* Assign variables
|
2013-03-18 22:32:32 +01:00
|
|
|
*
|
|
|
|
* This function assigns a variable. It can be accessed via $_[$key] in
|
|
|
|
* the template.
|
|
|
|
*
|
|
|
|
* If the key existed before, it will be overwritten
|
|
|
|
*/
|
2025-02-27 18:08:18 +01:00
|
|
|
public function assign(string $key, mixed $value): void {
|
2013-03-18 22:32:32 +01:00
|
|
|
$this->vars[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 17:50:53 +02:00
|
|
|
* Appends a variable
|
2013-03-18 22:32:32 +01: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.
|
|
|
|
*/
|
2025-02-27 18:08:18 +01:00
|
|
|
public function append(string $key, mixed $value): void {
|
2020-04-09 16:07:47 +02:00
|
|
|
if (array_key_exists($key, $this->vars)) {
|
2013-03-18 22:32:32 +01:00
|
|
|
$this->vars[$key][] = $value;
|
|
|
|
} else {
|
2020-03-26 09:30:18 +01:00
|
|
|
$this->vars[$key] = [ $value ];
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 17:50:53 +02:00
|
|
|
* Prints the proceeded template
|
2013-03-18 22:32:32 +01:00
|
|
|
*
|
|
|
|
* This function proceeds the template and prints its output.
|
|
|
|
*/
|
2025-02-25 18:44:02 +01:00
|
|
|
public function printPage(): void {
|
2013-03-18 22:32:32 +01:00
|
|
|
$data = $this->fetchPage();
|
2025-02-25 18:44:02 +01:00
|
|
|
print $data;
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 17:50:53 +02:00
|
|
|
* Process the template
|
2016-04-12 12:49:11 +02:00
|
|
|
*
|
2013-03-18 22:32:32 +01:00
|
|
|
* This function processes the template.
|
|
|
|
*/
|
2025-02-25 18:44:02 +01:00
|
|
|
public function fetchPage(?array $additionalParams = null): string {
|
2016-04-12 12:49:11 +02:00
|
|
|
return $this->load($this->template, $additionalParams);
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 17:50:53 +02:00
|
|
|
* doing the actual work
|
2016-04-12 12:49:11 +02:00
|
|
|
*
|
2013-03-18 22:32:32 +01:00
|
|
|
* Includes the template file, fetches its output
|
|
|
|
*/
|
2025-02-25 18:44:02 +01:00
|
|
|
protected function load(string $file, ?array $additionalParams = null): string {
|
2013-03-18 22:32:32 +01:00
|
|
|
// Register the variables
|
|
|
|
$_ = $this->vars;
|
|
|
|
$l = $this->l10n;
|
2013-07-24 11:51:21 +02:00
|
|
|
$theme = $this->theme;
|
2013-03-18 22:32:32 +01:00
|
|
|
|
2018-01-25 19:04:17 +01:00
|
|
|
if (!is_null($additionalParams)) {
|
2020-04-09 16:07:47 +02:00
|
|
|
$_ = array_merge($additionalParams, $this->vars);
|
2018-01-25 19:04:17 +01:00
|
|
|
foreach ($_ as $var => $value) {
|
2021-04-23 10:54:45 +02:00
|
|
|
if (!isset(${$var})) {
|
|
|
|
${$var} = $value;
|
|
|
|
}
|
2018-01-25 19:04:17 +01:00
|
|
|
}
|
2013-03-18 22:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include
|
|
|
|
ob_start();
|
2016-04-14 11:22:38 +02:00
|
|
|
try {
|
2025-03-11 11:05:31 +01:00
|
|
|
require_once __DIR__ . '/functions.php';
|
2016-04-14 11:22:38 +02:00
|
|
|
include $file;
|
|
|
|
$data = ob_get_contents();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
@ob_end_clean();
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-03-18 22:32:32 +01:00
|
|
|
@ob_end_clean();
|
|
|
|
|
|
|
|
// Return data
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|