@@ -22,138 +22,138 @@ |
||
| 22 | 22 | use OCP\Util; |
| 23 | 23 | |
| 24 | 24 | class Template extends Base implements ITemplate { |
| 25 | - private string $path; |
|
| 26 | - private array $headers = []; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @param string $app app providing the template |
|
| 30 | - * @param string $name of the template file (without suffix) |
|
| 31 | - * @param TemplateResponse::RENDER_AS_* $renderAs If $renderAs is set, will try to |
|
| 32 | - * produce a full page in the according layout. |
|
| 33 | - * @throws TemplateNotFoundException |
|
| 34 | - */ |
|
| 35 | - public function __construct( |
|
| 36 | - protected string $app, |
|
| 37 | - string $name, |
|
| 38 | - private string $renderAs = TemplateResponse::RENDER_AS_BLANK, |
|
| 39 | - bool $registerCall = true, |
|
| 40 | - ) { |
|
| 41 | - $theme = \OC_Util::getTheme(); |
|
| 42 | - |
|
| 43 | - $requestToken = ($registerCall ? Util::callRegister() : ''); |
|
| 44 | - $cspNonce = Server::get(ContentSecurityPolicyNonceManager::class)->getNonce(); |
|
| 45 | - |
|
| 46 | - // fix translation when app is something like core/lostpassword |
|
| 47 | - $parts = explode('/', $app); |
|
| 48 | - $l10n = Util::getL10N($parts[0]); |
|
| 49 | - |
|
| 50 | - [$path, $template] = $this->findTemplate($theme, $app, $name); |
|
| 51 | - |
|
| 52 | - $this->path = $path; |
|
| 53 | - |
|
| 54 | - parent::__construct( |
|
| 55 | - $template, |
|
| 56 | - $requestToken, |
|
| 57 | - $l10n, |
|
| 58 | - Server::get(Defaults::class), |
|
| 59 | - $cspNonce, |
|
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * find the template with the given name |
|
| 66 | - * |
|
| 67 | - * Will select the template file for the selected theme. |
|
| 68 | - * Checking all the possible locations. |
|
| 69 | - * |
|
| 70 | - * @param string $name of the template file (without suffix) |
|
| 71 | - * @return array{string,string} Directory path and filename |
|
| 72 | - * @throws TemplateNotFoundException |
|
| 73 | - */ |
|
| 74 | - protected function findTemplate(string $theme, string $app, string $name): array { |
|
| 75 | - // Check if it is a app template or not. |
|
| 76 | - if ($app !== '') { |
|
| 77 | - try { |
|
| 78 | - $appDir = Server::get(IAppManager::class)->getAppPath($app); |
|
| 79 | - } catch (AppPathNotFoundException) { |
|
| 80 | - $appDir = false; |
|
| 81 | - } |
|
| 82 | - $dirs = $this->getAppTemplateDirs($theme, $app, \OC::$SERVERROOT, $appDir); |
|
| 83 | - } else { |
|
| 84 | - $dirs = $this->getCoreTemplateDirs($theme, \OC::$SERVERROOT); |
|
| 85 | - } |
|
| 86 | - $locator = new TemplateFileLocator($dirs); |
|
| 87 | - return $locator->find($name); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Add a custom element to the header |
|
| 92 | - * @param string $tag tag name of the element |
|
| 93 | - * @param array $attributes array of attributes for the element |
|
| 94 | - * @param string $text the text content for the element. If $text is null then the |
|
| 95 | - * element will be written as empty element. So use "" to get a closing tag. |
|
| 96 | - */ |
|
| 97 | - public function addHeader(string $tag, array $attributes, ?string $text = null): void { |
|
| 98 | - $this->headers[] = [ |
|
| 99 | - 'tag' => $tag, |
|
| 100 | - 'attributes' => $attributes, |
|
| 101 | - 'text' => $text |
|
| 102 | - ]; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Process the template |
|
| 107 | - * |
|
| 108 | - * This function process the template. If $this->renderAs is set, it |
|
| 109 | - * will produce a full page. |
|
| 110 | - */ |
|
| 111 | - public function fetchPage(?array $additionalParams = null): string { |
|
| 112 | - $data = parent::fetchPage($additionalParams); |
|
| 113 | - |
|
| 114 | - if ($this->renderAs) { |
|
| 115 | - $page = Server::get(TemplateLayout::class)->getPageTemplate($this->renderAs, $this->app); |
|
| 116 | - |
|
| 117 | - if (is_array($additionalParams)) { |
|
| 118 | - foreach ($additionalParams as $key => $value) { |
|
| 119 | - $page->assign($key, $value); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // Add custom headers |
|
| 124 | - $headers = ''; |
|
| 125 | - foreach (array_merge(\OC_Util::$headers, $this->headers) as $header) { |
|
| 126 | - $headers .= '<' . Util::sanitizeHTML($header['tag']); |
|
| 127 | - if (strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes'])))) { |
|
| 128 | - $headers .= ' defer'; |
|
| 129 | - } |
|
| 130 | - foreach ($header['attributes'] as $name => $value) { |
|
| 131 | - $headers .= ' ' . Util::sanitizeHTML($name) . '="' . Util::sanitizeHTML($value) . '"'; |
|
| 132 | - } |
|
| 133 | - if ($header['text'] !== null) { |
|
| 134 | - $headers .= '>' . Util::sanitizeHTML($header['text']) . '</' . Util::sanitizeHTML($header['tag']) . '>'; |
|
| 135 | - } else { |
|
| 136 | - $headers .= '/>'; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $page->assign('headers', $headers); |
|
| 141 | - $page->assign('content', $data); |
|
| 142 | - return $page->fetchPage($additionalParams); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return $data; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Include template |
|
| 150 | - * |
|
| 151 | - * @return string returns content of included template |
|
| 152 | - * |
|
| 153 | - * Includes another template. use <?php echo $this->inc('template'); ?> to |
|
| 154 | - * do this. |
|
| 155 | - */ |
|
| 156 | - public function inc(string $file, ?array $additionalParams = null): string { |
|
| 157 | - return $this->load($this->path . $file . '.php', $additionalParams); |
|
| 158 | - } |
|
| 25 | + private string $path; |
|
| 26 | + private array $headers = []; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @param string $app app providing the template |
|
| 30 | + * @param string $name of the template file (without suffix) |
|
| 31 | + * @param TemplateResponse::RENDER_AS_* $renderAs If $renderAs is set, will try to |
|
| 32 | + * produce a full page in the according layout. |
|
| 33 | + * @throws TemplateNotFoundException |
|
| 34 | + */ |
|
| 35 | + public function __construct( |
|
| 36 | + protected string $app, |
|
| 37 | + string $name, |
|
| 38 | + private string $renderAs = TemplateResponse::RENDER_AS_BLANK, |
|
| 39 | + bool $registerCall = true, |
|
| 40 | + ) { |
|
| 41 | + $theme = \OC_Util::getTheme(); |
|
| 42 | + |
|
| 43 | + $requestToken = ($registerCall ? Util::callRegister() : ''); |
|
| 44 | + $cspNonce = Server::get(ContentSecurityPolicyNonceManager::class)->getNonce(); |
|
| 45 | + |
|
| 46 | + // fix translation when app is something like core/lostpassword |
|
| 47 | + $parts = explode('/', $app); |
|
| 48 | + $l10n = Util::getL10N($parts[0]); |
|
| 49 | + |
|
| 50 | + [$path, $template] = $this->findTemplate($theme, $app, $name); |
|
| 51 | + |
|
| 52 | + $this->path = $path; |
|
| 53 | + |
|
| 54 | + parent::__construct( |
|
| 55 | + $template, |
|
| 56 | + $requestToken, |
|
| 57 | + $l10n, |
|
| 58 | + Server::get(Defaults::class), |
|
| 59 | + $cspNonce, |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * find the template with the given name |
|
| 66 | + * |
|
| 67 | + * Will select the template file for the selected theme. |
|
| 68 | + * Checking all the possible locations. |
|
| 69 | + * |
|
| 70 | + * @param string $name of the template file (without suffix) |
|
| 71 | + * @return array{string,string} Directory path and filename |
|
| 72 | + * @throws TemplateNotFoundException |
|
| 73 | + */ |
|
| 74 | + protected function findTemplate(string $theme, string $app, string $name): array { |
|
| 75 | + // Check if it is a app template or not. |
|
| 76 | + if ($app !== '') { |
|
| 77 | + try { |
|
| 78 | + $appDir = Server::get(IAppManager::class)->getAppPath($app); |
|
| 79 | + } catch (AppPathNotFoundException) { |
|
| 80 | + $appDir = false; |
|
| 81 | + } |
|
| 82 | + $dirs = $this->getAppTemplateDirs($theme, $app, \OC::$SERVERROOT, $appDir); |
|
| 83 | + } else { |
|
| 84 | + $dirs = $this->getCoreTemplateDirs($theme, \OC::$SERVERROOT); |
|
| 85 | + } |
|
| 86 | + $locator = new TemplateFileLocator($dirs); |
|
| 87 | + return $locator->find($name); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Add a custom element to the header |
|
| 92 | + * @param string $tag tag name of the element |
|
| 93 | + * @param array $attributes array of attributes for the element |
|
| 94 | + * @param string $text the text content for the element. If $text is null then the |
|
| 95 | + * element will be written as empty element. So use "" to get a closing tag. |
|
| 96 | + */ |
|
| 97 | + public function addHeader(string $tag, array $attributes, ?string $text = null): void { |
|
| 98 | + $this->headers[] = [ |
|
| 99 | + 'tag' => $tag, |
|
| 100 | + 'attributes' => $attributes, |
|
| 101 | + 'text' => $text |
|
| 102 | + ]; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Process the template |
|
| 107 | + * |
|
| 108 | + * This function process the template. If $this->renderAs is set, it |
|
| 109 | + * will produce a full page. |
|
| 110 | + */ |
|
| 111 | + public function fetchPage(?array $additionalParams = null): string { |
|
| 112 | + $data = parent::fetchPage($additionalParams); |
|
| 113 | + |
|
| 114 | + if ($this->renderAs) { |
|
| 115 | + $page = Server::get(TemplateLayout::class)->getPageTemplate($this->renderAs, $this->app); |
|
| 116 | + |
|
| 117 | + if (is_array($additionalParams)) { |
|
| 118 | + foreach ($additionalParams as $key => $value) { |
|
| 119 | + $page->assign($key, $value); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // Add custom headers |
|
| 124 | + $headers = ''; |
|
| 125 | + foreach (array_merge(\OC_Util::$headers, $this->headers) as $header) { |
|
| 126 | + $headers .= '<' . Util::sanitizeHTML($header['tag']); |
|
| 127 | + if (strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes'])))) { |
|
| 128 | + $headers .= ' defer'; |
|
| 129 | + } |
|
| 130 | + foreach ($header['attributes'] as $name => $value) { |
|
| 131 | + $headers .= ' ' . Util::sanitizeHTML($name) . '="' . Util::sanitizeHTML($value) . '"'; |
|
| 132 | + } |
|
| 133 | + if ($header['text'] !== null) { |
|
| 134 | + $headers .= '>' . Util::sanitizeHTML($header['text']) . '</' . Util::sanitizeHTML($header['tag']) . '>'; |
|
| 135 | + } else { |
|
| 136 | + $headers .= '/>'; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $page->assign('headers', $headers); |
|
| 141 | + $page->assign('content', $data); |
|
| 142 | + return $page->fetchPage($additionalParams); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return $data; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Include template |
|
| 150 | + * |
|
| 151 | + * @return string returns content of included template |
|
| 152 | + * |
|
| 153 | + * Includes another template. use <?php echo $this->inc('template'); ?> to |
|
| 154 | + * do this. |
|
| 155 | + */ |
|
| 156 | + public function inc(string $file, ?array $additionalParams = null): string { |
|
| 157 | + return $this->load($this->path . $file . '.php', $additionalParams); |
|
| 158 | + } |
|
| 159 | 159 | } |
@@ -123,15 +123,15 @@ discard block |
||
| 123 | 123 | // Add custom headers |
| 124 | 124 | $headers = ''; |
| 125 | 125 | foreach (array_merge(\OC_Util::$headers, $this->headers) as $header) { |
| 126 | - $headers .= '<' . Util::sanitizeHTML($header['tag']); |
|
| 126 | + $headers .= '<'.Util::sanitizeHTML($header['tag']); |
|
| 127 | 127 | if (strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes'])))) { |
| 128 | 128 | $headers .= ' defer'; |
| 129 | 129 | } |
| 130 | 130 | foreach ($header['attributes'] as $name => $value) { |
| 131 | - $headers .= ' ' . Util::sanitizeHTML($name) . '="' . Util::sanitizeHTML($value) . '"'; |
|
| 131 | + $headers .= ' '.Util::sanitizeHTML($name).'="'.Util::sanitizeHTML($value).'"'; |
|
| 132 | 132 | } |
| 133 | 133 | if ($header['text'] !== null) { |
| 134 | - $headers .= '>' . Util::sanitizeHTML($header['text']) . '</' . Util::sanitizeHTML($header['tag']) . '>'; |
|
| 134 | + $headers .= '>'.Util::sanitizeHTML($header['text']).'</'.Util::sanitizeHTML($header['tag']).'>'; |
|
| 135 | 135 | } else { |
| 136 | 136 | $headers .= '/>'; |
| 137 | 137 | } |
@@ -154,6 +154,6 @@ discard block |
||
| 154 | 154 | * do this. |
| 155 | 155 | */ |
| 156 | 156 | public function inc(string $file, ?array $additionalParams = null): string { |
| 157 | - return $this->load($this->path . $file . '.php', $additionalParams); |
|
| 157 | + return $this->load($this->path.$file.'.php', $additionalParams); |
|
| 158 | 158 | } |
| 159 | 159 | } |