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