diff options
author | Côme Chilliet <come.chilliet@nextcloud.com> | 2025-02-27 10:24:59 +0100 |
---|---|---|
committer | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2025-03-06 15:49:25 +0100 |
commit | f19ddd55255a881de6efabdfc4468b2680ffc52d (patch) | |
tree | 7ea7455e008b85f0e45bd1650d9a78e3130b87a1 | |
parent | 71dc34c03c3415b639fe2236ef8456381adc5fcd (diff) | |
download | nextcloud-server-f19ddd55255a881de6efabdfc4468b2680ffc52d.tar.gz nextcloud-server-f19ddd55255a881de6efabdfc4468b2680ffc52d.zip |
fix: Add missing ITemplate interface and clean code in Template class
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 4 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 4 | ||||
-rw-r--r-- | lib/private/Server.php | 1 | ||||
-rw-r--r-- | lib/private/Template/Template.php | 90 | ||||
-rw-r--r-- | lib/private/legacy/OC_Util.php | 3 | ||||
-rw-r--r-- | lib/public/Template/ITemplate.php | 38 | ||||
-rw-r--r-- | lib/public/Util.php | 2 |
7 files changed, 88 insertions, 54 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 29da548198d..8b397620963 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -843,6 +843,8 @@ return array( 'OCP\\Teams\\Team' => $baseDir . '/lib/public/Teams/Team.php', 'OCP\\Teams\\TeamResource' => $baseDir . '/lib/public/Teams/TeamResource.php', 'OCP\\Template' => $baseDir . '/lib/public/Template.php', + 'OCP\\Template\\ITemplate' => $baseDir . '/lib/public/Template/ITemplate.php', + 'OCP\\Template\\ITemplateManager' => $baseDir . '/lib/public/Template/ITemplateManager.php', 'OCP\\TextProcessing\\Events\\AbstractTextProcessingEvent' => $baseDir . '/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php', 'OCP\\TextProcessing\\Events\\TaskFailedEvent' => $baseDir . '/lib/public/TextProcessing/Events/TaskFailedEvent.php', 'OCP\\TextProcessing\\Events\\TaskSuccessfulEvent' => $baseDir . '/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php', @@ -2041,7 +2043,9 @@ return array( 'OC\\Template\\JSResourceLocator' => $baseDir . '/lib/private/Template/JSResourceLocator.php', 'OC\\Template\\ResourceLocator' => $baseDir . '/lib/private/Template/ResourceLocator.php', 'OC\\Template\\ResourceNotFoundException' => $baseDir . '/lib/private/Template/ResourceNotFoundException.php', + 'OC\\Template\\Template' => $baseDir . '/lib/private/Template/Template.php', 'OC\\Template\\TemplateFileLocator' => $baseDir . '/lib/private/Template/TemplateFileLocator.php', + 'OC\\Template\\TemplateManager' => $baseDir . '/lib/private/Template/TemplateManager.php', 'OC\\TextProcessing\\Db\\Task' => $baseDir . '/lib/private/TextProcessing/Db/Task.php', 'OC\\TextProcessing\\Db\\TaskMapper' => $baseDir . '/lib/private/TextProcessing/Db/TaskMapper.php', 'OC\\TextProcessing\\Manager' => $baseDir . '/lib/private/TextProcessing/Manager.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d9451e5294f..4072df51ee1 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -892,6 +892,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Teams\\Team' => __DIR__ . '/../../..' . '/lib/public/Teams/Team.php', 'OCP\\Teams\\TeamResource' => __DIR__ . '/../../..' . '/lib/public/Teams/TeamResource.php', 'OCP\\Template' => __DIR__ . '/../../..' . '/lib/public/Template.php', + 'OCP\\Template\\ITemplate' => __DIR__ . '/../../..' . '/lib/public/Template/ITemplate.php', + 'OCP\\Template\\ITemplateManager' => __DIR__ . '/../../..' . '/lib/public/Template/ITemplateManager.php', 'OCP\\TextProcessing\\Events\\AbstractTextProcessingEvent' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php', 'OCP\\TextProcessing\\Events\\TaskFailedEvent' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Events/TaskFailedEvent.php', 'OCP\\TextProcessing\\Events\\TaskSuccessfulEvent' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php', @@ -2090,7 +2092,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Template\\JSResourceLocator' => __DIR__ . '/../../..' . '/lib/private/Template/JSResourceLocator.php', 'OC\\Template\\ResourceLocator' => __DIR__ . '/../../..' . '/lib/private/Template/ResourceLocator.php', 'OC\\Template\\ResourceNotFoundException' => __DIR__ . '/../../..' . '/lib/private/Template/ResourceNotFoundException.php', + 'OC\\Template\\Template' => __DIR__ . '/../../..' . '/lib/private/Template/Template.php', 'OC\\Template\\TemplateFileLocator' => __DIR__ . '/../../..' . '/lib/private/Template/TemplateFileLocator.php', + 'OC\\Template\\TemplateManager' => __DIR__ . '/../../..' . '/lib/private/Template/TemplateManager.php', 'OC\\TextProcessing\\Db\\Task' => __DIR__ . '/../../..' . '/lib/private/TextProcessing/Db/Task.php', 'OC\\TextProcessing\\Db\\TaskMapper' => __DIR__ . '/../../..' . '/lib/private/TextProcessing/Db/TaskMapper.php', 'OC\\TextProcessing\\Manager' => __DIR__ . '/../../..' . '/lib/private/TextProcessing/Manager.php', diff --git a/lib/private/Server.php b/lib/private/Server.php index 8c5ec8ed252..523fc7b9914 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -286,6 +286,7 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(\OCP\DirectEditing\IManager::class, \OC\DirectEditing\Manager::class); $this->registerAlias(ITemplateManager::class, TemplateManager::class); + $this->registerAlias(\OCP\Template\ITemplateManager::class, \OC\Template\TemplateManager::class); $this->registerAlias(IActionFactory::class, ActionFactory::class); diff --git a/lib/private/Template/Template.php b/lib/private/Template/Template.php index 78865ceee5c..9b486e10f7d 100644 --- a/lib/private/Template/Template.php +++ b/lib/private/Template/Template.php @@ -10,64 +10,52 @@ declare(strict_types=1); namespace OC\Template; +use OC\Security\CSP\ContentSecurityPolicyNonceManager; use OC\TemplateLayout; +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; use OCP\AppFramework\Http\TemplateResponse; +use OCP\Defaults; +use OCP\Server; use OCP\Template\ITemplate; +use OCP\Util; require_once __DIR__ . '/../legacy/template/functions.php'; class Template extends Base implements ITemplate { - /** @var string */ - private $renderAs; // Create a full page? - - /** @var string */ - private $path; // The path to the template - - /** @var array */ - private $headers = []; //custom headers - - /** @var string */ - protected $app; // app id + private string $path; + private array $headers = []; /** - * Constructor - * * @param string $app app providing the template * @param string $name of the template file (without suffix) - * @param string $renderAs If $renderAs is set, will try to - * produce a full page in the according layout. For - * now, $renderAs can be set to "guest", "user" or - * "admin". - * @param bool $registerCall = true + * @param TemplateResponse::RENDER_AS_* $renderAs If $renderAs is set, will try to + * produce a full page in the according layout. */ public function __construct( - $app, - $name, - $renderAs = TemplateResponse::RENDER_AS_BLANK, - $registerCall = true, + protected string $app, + string $name, + private string $renderAs = TemplateResponse::RENDER_AS_BLANK, + bool $registerCall = true, ) { - $theme = OC_Util::getTheme(); + $theme = \OC_Util::getTheme(); - $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; - $cspNonce = \OCP\Server::get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class)->getNonce(); + $requestToken = ($registerCall ? Util::callRegister() : ''); + $cspNonce = Server::get(ContentSecurityPolicyNonceManager::class)->getNonce(); - $parts = explode('/', $app); // fix translation when app is something like core/lostpassword - $l10n = \OC::$server->getL10N($parts[0]); - /** @var \OCP\Defaults $themeDefaults */ - $themeDefaults = \OCP\Server::get(\OCP\Defaults::class); + // fix translation when app is something like core/lostpassword + $parts = explode('/', $app); + $l10n = Util::getL10N($parts[0]); [$path, $template] = $this->findTemplate($theme, $app, $name); - // Set the private data - $this->renderAs = $renderAs; $this->path = $path; - $this->app = $app; parent::__construct( $template, $requestToken, $l10n, - $themeDefaults, + Server::get(Defaults::class), $cspNonce, ); } @@ -75,22 +63,26 @@ class Template extends Base implements ITemplate { /** * find the template with the given name - * @param string $name of the template file (without suffix) * * Will select the template file for the selected theme. * Checking all the possible locations. - * @param string $theme - * @param string $app + * + * @param string $name of the template file (without suffix) * @return string[] */ - protected function findTemplate($theme, $app, $name) { + protected function findTemplate(string $theme, string $app, string $name): array { // Check if it is a app template or not. if ($app !== '') { - $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app)); + try { + $appDir = Server::get(IAppManager::class)->getAppPath($app); + } catch (AppPathNotFoundException) { + $appDir = false; + } + $dirs = $this->getAppTemplateDirs($theme, $app, \OC::$SERVERROOT, $appDir); } else { - $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT); + $dirs = $this->getCoreTemplateDirs($theme, \OC::$SERVERROOT); } - $locator = new \OC\Template\TemplateFileLocator($dirs); + $locator = new TemplateFileLocator($dirs); $template = $locator->find($name); $path = $locator->getPath(); return [$path, $template]; @@ -103,7 +95,7 @@ class Template extends Base implements ITemplate { * @param string $text the text content for the element. If $text is null then the * element will be written as empty element. So use "" to get a closing tag. */ - public function addHeader($tag, $attributes, $text = null) { + public function addHeader(string $tag, array $attributes, ?string $text = null): void { $this->headers[] = [ 'tag' => $tag, 'attributes' => $attributes, @@ -113,12 +105,11 @@ class Template extends Base implements ITemplate { /** * Process the template - * @return string * * This function process the template. If $this->renderAs is set, it * will produce a full page. */ - public function fetchPage($additionalParams = null) { + public function fetchPage(?array $additionalParams = null): string { $data = parent::fetchPage($additionalParams); if ($this->renderAs) { @@ -132,23 +123,22 @@ class Template extends Base implements ITemplate { // Add custom headers $headers = ''; - foreach (OC_Util::$headers as $header) { - $headers .= '<' . \OCP\Util::sanitizeHTML($header['tag']); + foreach (\OC_Util::$headers as $header) { + $headers .= '<' . Util::sanitizeHTML($header['tag']); if (strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes'])))) { $headers .= ' defer'; } foreach ($header['attributes'] as $name => $value) { - $headers .= ' ' . \OCP\Util::sanitizeHTML($name) . '="' . \OCP\Util::sanitizeHTML($value) . '"'; + $headers .= ' ' . Util::sanitizeHTML($name) . '="' . Util::sanitizeHTML($value) . '"'; } if ($header['text'] !== null) { - $headers .= '>' . \OCP\Util::sanitizeHTML($header['text']) . '</' . \OCP\Util::sanitizeHTML($header['tag']) . '>'; + $headers .= '>' . Util::sanitizeHTML($header['text']) . '</' . Util::sanitizeHTML($header['tag']) . '>'; } else { $headers .= '/>'; } } $page->assign('headers', $headers); - $page->assign('content', $data); return $page->fetchPage($additionalParams); } @@ -159,14 +149,12 @@ class Template extends Base implements ITemplate { /** * Include template * - * @param string $file - * @param array|null $additionalParams * @return string returns content of included template * * Includes another template. use <?php echo $this->inc('template'); ?> to * do this. */ - public function inc($file, $additionalParams = null) { + public function inc(string $file, ?array $additionalParams = null): string { return $this->load($this->path . $file . '.php', $additionalParams); } } diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php index bf60fed0389..36a121094cc 100644 --- a/lib/private/legacy/OC_Util.php +++ b/lib/private/legacy/OC_Util.php @@ -725,11 +725,10 @@ class OC_Util { * string or array of strings before displaying it on a web page. * * @param string|string[] $value - * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter. + * @return ($value is array ? string[] : string) */ public static function sanitizeHTML($value) { if (is_array($value)) { - /** @var string[] $value */ $value = array_map(function ($value) { return self::sanitizeHTML($value); }, $value); diff --git a/lib/public/Template/ITemplate.php b/lib/public/Template/ITemplate.php new file mode 100644 index 00000000000..a37f7d12337 --- /dev/null +++ b/lib/public/Template/ITemplate.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\Template; + +/** + * @since 32.0.0 + */ +interface ITemplate { + /** + * Process the template + * @since 32.0.0 + */ + public function fetchPage(?array $additionalParams = null): string; + + /** + * Proceed the template and print its output. + * @since 32.0.0 + */ + public function printPage(): void; + + /** + * Assign variables + * + * This function assigns a variable. It can be accessed via $_[$key] in + * the template. + * + * If the key existed before, it will be overwritten + * @since 32.0.0 + */ + public function assign(string $key, float|array|bool|int|string|\Throwable $value): void; +} diff --git a/lib/public/Util.php b/lib/public/Util.php index 28da91c9a0f..d7f7bbe4f94 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -408,7 +408,7 @@ class Util { * string or array of strings before displaying it on a web page. * * @param string|string[] $value - * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter. + * @return ($value is array ? string[] : string) an array of sanitized strings or a single sanitized string, depends on the input parameter. * @since 4.5.0 */ public static function sanitizeHTML($value) { |