@@ -5,18 +5,18 @@ |
||
5 | 5 | style('core', ['styles', 'header']); |
6 | 6 | |
7 | 7 | function print_exception(Throwable $e, \OCP\IL10N $l): void { |
8 | - print_unescaped('<pre>'); |
|
9 | - p($e->getTraceAsString()); |
|
10 | - print_unescaped('</pre>'); |
|
8 | + print_unescaped('<pre>'); |
|
9 | + p($e->getTraceAsString()); |
|
10 | + print_unescaped('</pre>'); |
|
11 | 11 | |
12 | - if ($e->getPrevious() !== null) { |
|
13 | - print_unescaped('<br />'); |
|
14 | - print_unescaped('<h4>'); |
|
15 | - p($l->t('Previous')); |
|
16 | - print_unescaped('</h4>'); |
|
12 | + if ($e->getPrevious() !== null) { |
|
13 | + print_unescaped('<br />'); |
|
14 | + print_unescaped('<h4>'); |
|
15 | + p($l->t('Previous')); |
|
16 | + print_unescaped('</h4>'); |
|
17 | 17 | |
18 | - print_exception($e->getPrevious(), $l); |
|
19 | - } |
|
18 | + print_exception($e->getPrevious(), $l); |
|
19 | + } |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | ?> |
@@ -48,311 +48,311 @@ |
||
48 | 48 | */ |
49 | 49 | class OC_Template extends \OC\Template\Base { |
50 | 50 | |
51 | - /** @var string */ |
|
52 | - private $renderAs; // Create a full page? |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $path; // The path to the template |
|
56 | - |
|
57 | - /** @var array */ |
|
58 | - private $headers = []; //custom headers |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - protected $app; // app id |
|
62 | - |
|
63 | - protected static $initTemplateEngineFirstRun = true; |
|
64 | - |
|
65 | - /** |
|
66 | - * Constructor |
|
67 | - * |
|
68 | - * @param string $app app providing the template |
|
69 | - * @param string $name of the template file (without suffix) |
|
70 | - * @param string $renderAs If $renderAs is set, OC_Template will try to |
|
71 | - * produce a full page in the according layout. For |
|
72 | - * now, $renderAs can be set to "guest", "user" or |
|
73 | - * "admin". |
|
74 | - * @param bool $registerCall = true |
|
75 | - */ |
|
76 | - public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS_BLANK, $registerCall = true) { |
|
77 | - // Read the selected theme from the config file |
|
78 | - self::initTemplateEngine($renderAs); |
|
79 | - |
|
80 | - $theme = OC_Util::getTheme(); |
|
81 | - |
|
82 | - $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; |
|
83 | - |
|
84 | - $parts = explode('/', $app); // fix translation when app is something like core/lostpassword |
|
85 | - $l10n = \OC::$server->getL10N($parts[0]); |
|
86 | - /** @var \OCP\Defaults $themeDefaults */ |
|
87 | - $themeDefaults = \OC::$server->query(\OCP\Defaults::class); |
|
88 | - |
|
89 | - list($path, $template) = $this->findTemplate($theme, $app, $name); |
|
90 | - |
|
91 | - // Set the private data |
|
92 | - $this->renderAs = $renderAs; |
|
93 | - $this->path = $path; |
|
94 | - $this->app = $app; |
|
95 | - |
|
96 | - parent::__construct($template, $requestToken, $l10n, $themeDefaults); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @param string $renderAs |
|
101 | - */ |
|
102 | - public static function initTemplateEngine($renderAs) { |
|
103 | - if (self::$initTemplateEngineFirstRun) { |
|
104 | - |
|
105 | - //apps that started before the template initialization can load their own scripts/styles |
|
106 | - //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true |
|
107 | - //meaning the last script/style in this list will be loaded first |
|
108 | - if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== TemplateResponse::RENDER_AS_ERROR && !\OCP\Util::needUpgrade()) { |
|
109 | - if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') { |
|
110 | - OC_Util::addScript('backgroundjobs', null, true); |
|
111 | - } |
|
112 | - } |
|
113 | - OC_Util::addStyle('css-variables', null, true); |
|
114 | - OC_Util::addStyle('server', null, true); |
|
115 | - OC_Util::addTranslations('core', null, true); |
|
116 | - |
|
117 | - if (\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
118 | - OC_Util::addScript('merged-template-prepend', null, true); |
|
119 | - OC_Util::addScript('dist/files_client', null, true); |
|
120 | - OC_Util::addScript('dist/files_fileinfo', null, true); |
|
121 | - } |
|
122 | - OC_Util::addScript('core', 'dist/main', true); |
|
123 | - |
|
124 | - if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) { |
|
125 | - // shim for the davclient.js library |
|
126 | - \OCP\Util::addScript('dist/files_iedavclient'); |
|
127 | - } |
|
128 | - |
|
129 | - self::$initTemplateEngineFirstRun = false; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * find the template with the given name |
|
136 | - * @param string $name of the template file (without suffix) |
|
137 | - * |
|
138 | - * Will select the template file for the selected theme. |
|
139 | - * Checking all the possible locations. |
|
140 | - * @param string $theme |
|
141 | - * @param string $app |
|
142 | - * @return string[] |
|
143 | - */ |
|
144 | - protected function findTemplate($theme, $app, $name) { |
|
145 | - // Check if it is a app template or not. |
|
146 | - if ($app !== '') { |
|
147 | - $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app)); |
|
148 | - } else { |
|
149 | - $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT); |
|
150 | - } |
|
151 | - $locator = new \OC\Template\TemplateFileLocator($dirs); |
|
152 | - $template = $locator->find($name); |
|
153 | - $path = $locator->getPath(); |
|
154 | - return [$path, $template]; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Add a custom element to the header |
|
159 | - * @param string $tag tag name of the element |
|
160 | - * @param array $attributes array of attributes for the element |
|
161 | - * @param string $text the text content for the element. If $text is null then the |
|
162 | - * element will be written as empty element. So use "" to get a closing tag. |
|
163 | - */ |
|
164 | - public function addHeader($tag, $attributes, $text = null) { |
|
165 | - $this->headers[] = [ |
|
166 | - 'tag' => $tag, |
|
167 | - 'attributes' => $attributes, |
|
168 | - 'text' => $text |
|
169 | - ]; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Process the template |
|
174 | - * @return string |
|
175 | - * |
|
176 | - * This function process the template. If $this->renderAs is set, it |
|
177 | - * will produce a full page. |
|
178 | - */ |
|
179 | - public function fetchPage($additionalParams = null) { |
|
180 | - $data = parent::fetchPage($additionalParams); |
|
181 | - |
|
182 | - if ($this->renderAs) { |
|
183 | - $page = new TemplateLayout($this->renderAs, $this->app); |
|
184 | - |
|
185 | - if (is_array($additionalParams)) { |
|
186 | - foreach ($additionalParams as $key => $value) { |
|
187 | - $page->assign($key, $value); |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - // Add custom headers |
|
192 | - $headers = ''; |
|
193 | - foreach (OC_Util::$headers as $header) { |
|
194 | - $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']); |
|
195 | - if (strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes'])))) { |
|
196 | - $headers .= ' defer'; |
|
197 | - } |
|
198 | - foreach ($header['attributes'] as $name => $value) { |
|
199 | - $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"'; |
|
200 | - } |
|
201 | - if ($header['text'] !== null) { |
|
202 | - $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>'; |
|
203 | - } else { |
|
204 | - $headers .= '/>'; |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - $page->assign('headers', $headers); |
|
209 | - |
|
210 | - $page->assign('content', $data); |
|
211 | - return $page->fetchPage($additionalParams); |
|
212 | - } |
|
213 | - |
|
214 | - return $data; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Include template |
|
219 | - * |
|
220 | - * @param string $file |
|
221 | - * @param array|null $additionalParams |
|
222 | - * @return string returns content of included template |
|
223 | - * |
|
224 | - * Includes another template. use <?php echo $this->inc('template'); ?> to |
|
225 | - * do this. |
|
226 | - */ |
|
227 | - public function inc($file, $additionalParams = null) { |
|
228 | - return $this->load($this->path.$file.'.php', $additionalParams); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Shortcut to print a simple page for users |
|
233 | - * @param string $application The application we render the template for |
|
234 | - * @param string $name Name of the template |
|
235 | - * @param array $parameters Parameters for the template |
|
236 | - * @return boolean|null |
|
237 | - */ |
|
238 | - public static function printUserPage($application, $name, $parameters = []) { |
|
239 | - $content = new OC_Template($application, $name, "user"); |
|
240 | - foreach ($parameters as $key => $value) { |
|
241 | - $content->assign($key, $value); |
|
242 | - } |
|
243 | - print $content->printPage(); |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Shortcut to print a simple page for admins |
|
248 | - * @param string $application The application we render the template for |
|
249 | - * @param string $name Name of the template |
|
250 | - * @param array $parameters Parameters for the template |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - public static function printAdminPage($application, $name, $parameters = []) { |
|
254 | - $content = new OC_Template($application, $name, "admin"); |
|
255 | - foreach ($parameters as $key => $value) { |
|
256 | - $content->assign($key, $value); |
|
257 | - } |
|
258 | - return $content->printPage(); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Shortcut to print a simple page for guests |
|
263 | - * @param string $application The application we render the template for |
|
264 | - * @param string $name Name of the template |
|
265 | - * @param array|string $parameters Parameters for the template |
|
266 | - * @return bool |
|
267 | - */ |
|
268 | - public static function printGuestPage($application, $name, $parameters = []) { |
|
269 | - $content = new OC_Template($application, $name, $name === 'error' ? $name : 'guest'); |
|
270 | - foreach ($parameters as $key => $value) { |
|
271 | - $content->assign($key, $value); |
|
272 | - } |
|
273 | - return $content->printPage(); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Print a fatal error page and terminates the script |
|
278 | - * @param string $error_msg The error message to show |
|
279 | - * @param string $hint An optional hint message - needs to be properly escape |
|
280 | - * @param int $statusCode |
|
281 | - * @suppress PhanAccessMethodInternal |
|
282 | - */ |
|
283 | - public static function printErrorPage($error_msg, $hint = '', $statusCode = 500) { |
|
284 | - if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) { |
|
285 | - \OC_App::loadApp('theming'); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - if ($error_msg === $hint) { |
|
290 | - // If the hint is the same as the message there is no need to display it twice. |
|
291 | - $hint = ''; |
|
292 | - } |
|
293 | - |
|
294 | - http_response_code($statusCode); |
|
295 | - try { |
|
296 | - $content = new \OC_Template('', 'error', 'error', false); |
|
297 | - $errors = [['error' => $error_msg, 'hint' => $hint]]; |
|
298 | - $content->assign('errors', $errors); |
|
299 | - $content->printPage(); |
|
300 | - } catch (\Exception $e) { |
|
301 | - $logger = \OC::$server->getLogger(); |
|
302 | - $logger->error("$error_msg $hint", ['app' => 'core']); |
|
303 | - $logger->logException($e, ['app' => 'core']); |
|
304 | - |
|
305 | - header('Content-Type: text/plain; charset=utf-8'); |
|
306 | - print("$error_msg $hint"); |
|
307 | - } |
|
308 | - die(); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * print error page using Exception details |
|
313 | - * @param Exception|Throwable $exception |
|
314 | - * @param int $statusCode |
|
315 | - * @return bool|string |
|
316 | - * @suppress PhanAccessMethodInternal |
|
317 | - */ |
|
318 | - public static function printExceptionErrorPage($exception, $statusCode = 503) { |
|
319 | - http_response_code($statusCode); |
|
320 | - try { |
|
321 | - $request = \OC::$server->getRequest(); |
|
322 | - $content = new \OC_Template('', 'exception', 'error', false); |
|
323 | - $content->assign('errorClass', get_class($exception)); |
|
324 | - $content->assign('errorMsg', $exception->getMessage()); |
|
325 | - $content->assign('errorCode', $exception->getCode()); |
|
326 | - $content->assign('file', $exception->getFile()); |
|
327 | - $content->assign('line', $exception->getLine()); |
|
328 | - $content->assign('exception', $exception); |
|
329 | - $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false)); |
|
330 | - $content->assign('remoteAddr', $request->getRemoteAddress()); |
|
331 | - $content->assign('requestID', $request->getId()); |
|
332 | - $content->printPage(); |
|
333 | - } catch (\Exception $e) { |
|
334 | - try { |
|
335 | - $logger = \OC::$server->getLogger(); |
|
336 | - $logger->logException($exception, ['app' => 'core']); |
|
337 | - $logger->logException($e, ['app' => 'core']); |
|
338 | - } catch (Throwable $e) { |
|
339 | - // no way to log it properly - but to avoid a white page of death we send some output |
|
340 | - header('Content-Type: text/plain; charset=utf-8'); |
|
341 | - print("Internal Server Error\n\n"); |
|
342 | - print("The server encountered an internal error and was unable to complete your request.\n"); |
|
343 | - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
344 | - print("More details can be found in the server log.\n"); |
|
345 | - |
|
346 | - // and then throw it again to log it at least to the web server error log |
|
347 | - throw $e; |
|
348 | - } |
|
349 | - |
|
350 | - header('Content-Type: text/plain; charset=utf-8'); |
|
351 | - print("Internal Server Error\n\n"); |
|
352 | - print("The server encountered an internal error and was unable to complete your request.\n"); |
|
353 | - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
354 | - print("More details can be found in the server log.\n"); |
|
355 | - } |
|
356 | - die(); |
|
357 | - } |
|
51 | + /** @var string */ |
|
52 | + private $renderAs; // Create a full page? |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $path; // The path to the template |
|
56 | + |
|
57 | + /** @var array */ |
|
58 | + private $headers = []; //custom headers |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + protected $app; // app id |
|
62 | + |
|
63 | + protected static $initTemplateEngineFirstRun = true; |
|
64 | + |
|
65 | + /** |
|
66 | + * Constructor |
|
67 | + * |
|
68 | + * @param string $app app providing the template |
|
69 | + * @param string $name of the template file (without suffix) |
|
70 | + * @param string $renderAs If $renderAs is set, OC_Template will try to |
|
71 | + * produce a full page in the according layout. For |
|
72 | + * now, $renderAs can be set to "guest", "user" or |
|
73 | + * "admin". |
|
74 | + * @param bool $registerCall = true |
|
75 | + */ |
|
76 | + public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS_BLANK, $registerCall = true) { |
|
77 | + // Read the selected theme from the config file |
|
78 | + self::initTemplateEngine($renderAs); |
|
79 | + |
|
80 | + $theme = OC_Util::getTheme(); |
|
81 | + |
|
82 | + $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; |
|
83 | + |
|
84 | + $parts = explode('/', $app); // fix translation when app is something like core/lostpassword |
|
85 | + $l10n = \OC::$server->getL10N($parts[0]); |
|
86 | + /** @var \OCP\Defaults $themeDefaults */ |
|
87 | + $themeDefaults = \OC::$server->query(\OCP\Defaults::class); |
|
88 | + |
|
89 | + list($path, $template) = $this->findTemplate($theme, $app, $name); |
|
90 | + |
|
91 | + // Set the private data |
|
92 | + $this->renderAs = $renderAs; |
|
93 | + $this->path = $path; |
|
94 | + $this->app = $app; |
|
95 | + |
|
96 | + parent::__construct($template, $requestToken, $l10n, $themeDefaults); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @param string $renderAs |
|
101 | + */ |
|
102 | + public static function initTemplateEngine($renderAs) { |
|
103 | + if (self::$initTemplateEngineFirstRun) { |
|
104 | + |
|
105 | + //apps that started before the template initialization can load their own scripts/styles |
|
106 | + //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true |
|
107 | + //meaning the last script/style in this list will be loaded first |
|
108 | + if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== TemplateResponse::RENDER_AS_ERROR && !\OCP\Util::needUpgrade()) { |
|
109 | + if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') { |
|
110 | + OC_Util::addScript('backgroundjobs', null, true); |
|
111 | + } |
|
112 | + } |
|
113 | + OC_Util::addStyle('css-variables', null, true); |
|
114 | + OC_Util::addStyle('server', null, true); |
|
115 | + OC_Util::addTranslations('core', null, true); |
|
116 | + |
|
117 | + if (\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
118 | + OC_Util::addScript('merged-template-prepend', null, true); |
|
119 | + OC_Util::addScript('dist/files_client', null, true); |
|
120 | + OC_Util::addScript('dist/files_fileinfo', null, true); |
|
121 | + } |
|
122 | + OC_Util::addScript('core', 'dist/main', true); |
|
123 | + |
|
124 | + if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) { |
|
125 | + // shim for the davclient.js library |
|
126 | + \OCP\Util::addScript('dist/files_iedavclient'); |
|
127 | + } |
|
128 | + |
|
129 | + self::$initTemplateEngineFirstRun = false; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * find the template with the given name |
|
136 | + * @param string $name of the template file (without suffix) |
|
137 | + * |
|
138 | + * Will select the template file for the selected theme. |
|
139 | + * Checking all the possible locations. |
|
140 | + * @param string $theme |
|
141 | + * @param string $app |
|
142 | + * @return string[] |
|
143 | + */ |
|
144 | + protected function findTemplate($theme, $app, $name) { |
|
145 | + // Check if it is a app template or not. |
|
146 | + if ($app !== '') { |
|
147 | + $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app)); |
|
148 | + } else { |
|
149 | + $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT); |
|
150 | + } |
|
151 | + $locator = new \OC\Template\TemplateFileLocator($dirs); |
|
152 | + $template = $locator->find($name); |
|
153 | + $path = $locator->getPath(); |
|
154 | + return [$path, $template]; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Add a custom element to the header |
|
159 | + * @param string $tag tag name of the element |
|
160 | + * @param array $attributes array of attributes for the element |
|
161 | + * @param string $text the text content for the element. If $text is null then the |
|
162 | + * element will be written as empty element. So use "" to get a closing tag. |
|
163 | + */ |
|
164 | + public function addHeader($tag, $attributes, $text = null) { |
|
165 | + $this->headers[] = [ |
|
166 | + 'tag' => $tag, |
|
167 | + 'attributes' => $attributes, |
|
168 | + 'text' => $text |
|
169 | + ]; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Process the template |
|
174 | + * @return string |
|
175 | + * |
|
176 | + * This function process the template. If $this->renderAs is set, it |
|
177 | + * will produce a full page. |
|
178 | + */ |
|
179 | + public function fetchPage($additionalParams = null) { |
|
180 | + $data = parent::fetchPage($additionalParams); |
|
181 | + |
|
182 | + if ($this->renderAs) { |
|
183 | + $page = new TemplateLayout($this->renderAs, $this->app); |
|
184 | + |
|
185 | + if (is_array($additionalParams)) { |
|
186 | + foreach ($additionalParams as $key => $value) { |
|
187 | + $page->assign($key, $value); |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + // Add custom headers |
|
192 | + $headers = ''; |
|
193 | + foreach (OC_Util::$headers as $header) { |
|
194 | + $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']); |
|
195 | + if (strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes'])))) { |
|
196 | + $headers .= ' defer'; |
|
197 | + } |
|
198 | + foreach ($header['attributes'] as $name => $value) { |
|
199 | + $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"'; |
|
200 | + } |
|
201 | + if ($header['text'] !== null) { |
|
202 | + $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>'; |
|
203 | + } else { |
|
204 | + $headers .= '/>'; |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + $page->assign('headers', $headers); |
|
209 | + |
|
210 | + $page->assign('content', $data); |
|
211 | + return $page->fetchPage($additionalParams); |
|
212 | + } |
|
213 | + |
|
214 | + return $data; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Include template |
|
219 | + * |
|
220 | + * @param string $file |
|
221 | + * @param array|null $additionalParams |
|
222 | + * @return string returns content of included template |
|
223 | + * |
|
224 | + * Includes another template. use <?php echo $this->inc('template'); ?> to |
|
225 | + * do this. |
|
226 | + */ |
|
227 | + public function inc($file, $additionalParams = null) { |
|
228 | + return $this->load($this->path.$file.'.php', $additionalParams); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Shortcut to print a simple page for users |
|
233 | + * @param string $application The application we render the template for |
|
234 | + * @param string $name Name of the template |
|
235 | + * @param array $parameters Parameters for the template |
|
236 | + * @return boolean|null |
|
237 | + */ |
|
238 | + public static function printUserPage($application, $name, $parameters = []) { |
|
239 | + $content = new OC_Template($application, $name, "user"); |
|
240 | + foreach ($parameters as $key => $value) { |
|
241 | + $content->assign($key, $value); |
|
242 | + } |
|
243 | + print $content->printPage(); |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Shortcut to print a simple page for admins |
|
248 | + * @param string $application The application we render the template for |
|
249 | + * @param string $name Name of the template |
|
250 | + * @param array $parameters Parameters for the template |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + public static function printAdminPage($application, $name, $parameters = []) { |
|
254 | + $content = new OC_Template($application, $name, "admin"); |
|
255 | + foreach ($parameters as $key => $value) { |
|
256 | + $content->assign($key, $value); |
|
257 | + } |
|
258 | + return $content->printPage(); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Shortcut to print a simple page for guests |
|
263 | + * @param string $application The application we render the template for |
|
264 | + * @param string $name Name of the template |
|
265 | + * @param array|string $parameters Parameters for the template |
|
266 | + * @return bool |
|
267 | + */ |
|
268 | + public static function printGuestPage($application, $name, $parameters = []) { |
|
269 | + $content = new OC_Template($application, $name, $name === 'error' ? $name : 'guest'); |
|
270 | + foreach ($parameters as $key => $value) { |
|
271 | + $content->assign($key, $value); |
|
272 | + } |
|
273 | + return $content->printPage(); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Print a fatal error page and terminates the script |
|
278 | + * @param string $error_msg The error message to show |
|
279 | + * @param string $hint An optional hint message - needs to be properly escape |
|
280 | + * @param int $statusCode |
|
281 | + * @suppress PhanAccessMethodInternal |
|
282 | + */ |
|
283 | + public static function printErrorPage($error_msg, $hint = '', $statusCode = 500) { |
|
284 | + if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) { |
|
285 | + \OC_App::loadApp('theming'); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + if ($error_msg === $hint) { |
|
290 | + // If the hint is the same as the message there is no need to display it twice. |
|
291 | + $hint = ''; |
|
292 | + } |
|
293 | + |
|
294 | + http_response_code($statusCode); |
|
295 | + try { |
|
296 | + $content = new \OC_Template('', 'error', 'error', false); |
|
297 | + $errors = [['error' => $error_msg, 'hint' => $hint]]; |
|
298 | + $content->assign('errors', $errors); |
|
299 | + $content->printPage(); |
|
300 | + } catch (\Exception $e) { |
|
301 | + $logger = \OC::$server->getLogger(); |
|
302 | + $logger->error("$error_msg $hint", ['app' => 'core']); |
|
303 | + $logger->logException($e, ['app' => 'core']); |
|
304 | + |
|
305 | + header('Content-Type: text/plain; charset=utf-8'); |
|
306 | + print("$error_msg $hint"); |
|
307 | + } |
|
308 | + die(); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * print error page using Exception details |
|
313 | + * @param Exception|Throwable $exception |
|
314 | + * @param int $statusCode |
|
315 | + * @return bool|string |
|
316 | + * @suppress PhanAccessMethodInternal |
|
317 | + */ |
|
318 | + public static function printExceptionErrorPage($exception, $statusCode = 503) { |
|
319 | + http_response_code($statusCode); |
|
320 | + try { |
|
321 | + $request = \OC::$server->getRequest(); |
|
322 | + $content = new \OC_Template('', 'exception', 'error', false); |
|
323 | + $content->assign('errorClass', get_class($exception)); |
|
324 | + $content->assign('errorMsg', $exception->getMessage()); |
|
325 | + $content->assign('errorCode', $exception->getCode()); |
|
326 | + $content->assign('file', $exception->getFile()); |
|
327 | + $content->assign('line', $exception->getLine()); |
|
328 | + $content->assign('exception', $exception); |
|
329 | + $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false)); |
|
330 | + $content->assign('remoteAddr', $request->getRemoteAddress()); |
|
331 | + $content->assign('requestID', $request->getId()); |
|
332 | + $content->printPage(); |
|
333 | + } catch (\Exception $e) { |
|
334 | + try { |
|
335 | + $logger = \OC::$server->getLogger(); |
|
336 | + $logger->logException($exception, ['app' => 'core']); |
|
337 | + $logger->logException($e, ['app' => 'core']); |
|
338 | + } catch (Throwable $e) { |
|
339 | + // no way to log it properly - but to avoid a white page of death we send some output |
|
340 | + header('Content-Type: text/plain; charset=utf-8'); |
|
341 | + print("Internal Server Error\n\n"); |
|
342 | + print("The server encountered an internal error and was unable to complete your request.\n"); |
|
343 | + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
344 | + print("More details can be found in the server log.\n"); |
|
345 | + |
|
346 | + // and then throw it again to log it at least to the web server error log |
|
347 | + throw $e; |
|
348 | + } |
|
349 | + |
|
350 | + header('Content-Type: text/plain; charset=utf-8'); |
|
351 | + print("Internal Server Error\n\n"); |
|
352 | + print("The server encountered an internal error and was unable to complete your request.\n"); |
|
353 | + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
354 | + print("More details can be found in the server log.\n"); |
|
355 | + } |
|
356 | + die(); |
|
357 | + } |
|
358 | 358 | } |
@@ -34,157 +34,157 @@ |
||
34 | 34 | use Throwable; |
35 | 35 | |
36 | 36 | class Base { |
37 | - private $template; // The template |
|
38 | - private $vars; // Vars |
|
39 | - |
|
40 | - /** @var \OCP\IL10N */ |
|
41 | - private $l10n; |
|
42 | - |
|
43 | - /** @var Defaults */ |
|
44 | - private $theme; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param string $template |
|
48 | - * @param string $requestToken |
|
49 | - * @param \OCP\IL10N $l10n |
|
50 | - * @param Defaults $theme |
|
51 | - */ |
|
52 | - public function __construct($template, $requestToken, $l10n, $theme) { |
|
53 | - $this->vars = []; |
|
54 | - $this->vars['requesttoken'] = $requestToken; |
|
55 | - $this->l10n = $l10n; |
|
56 | - $this->template = $template; |
|
57 | - $this->theme = $theme; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $serverRoot |
|
62 | - * @param string|false $app_dir |
|
63 | - * @param string $theme |
|
64 | - * @param string $app |
|
65 | - * @return string[] |
|
66 | - */ |
|
67 | - protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
68 | - // Check if the app is in the app folder or in the root |
|
69 | - if (file_exists($app_dir.'/templates/')) { |
|
70 | - return [ |
|
71 | - $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
72 | - $app_dir.'/templates/', |
|
73 | - ]; |
|
74 | - } |
|
75 | - return [ |
|
76 | - $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
77 | - $serverRoot.'/'.$app.'/templates/', |
|
78 | - ]; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @param string $serverRoot |
|
83 | - * @param string $theme |
|
84 | - * @return string[] |
|
85 | - */ |
|
86 | - protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
87 | - return [ |
|
88 | - $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
89 | - $serverRoot.'/core/templates/', |
|
90 | - ]; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Assign variables |
|
95 | - * @param string $key key |
|
96 | - * @param array|bool|integer|string|Throwable $value value |
|
97 | - * @return bool |
|
98 | - * |
|
99 | - * This function assigns a variable. It can be accessed via $_[$key] in |
|
100 | - * the template. |
|
101 | - * |
|
102 | - * If the key existed before, it will be overwritten |
|
103 | - */ |
|
104 | - public function assign($key, $value) { |
|
105 | - $this->vars[$key] = $value; |
|
106 | - return true; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Appends a variable |
|
111 | - * @param string $key key |
|
112 | - * @param mixed $value value |
|
113 | - * |
|
114 | - * This function assigns a variable in an array context. If the key already |
|
115 | - * exists, the value will be appended. It can be accessed via |
|
116 | - * $_[$key][$position] in the template. |
|
117 | - */ |
|
118 | - public function append($key, $value) { |
|
119 | - if (array_key_exists($key, $this->vars)) { |
|
120 | - $this->vars[$key][] = $value; |
|
121 | - } else { |
|
122 | - $this->vars[$key] = [ $value ]; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Prints the proceeded template |
|
128 | - * @return bool |
|
129 | - * |
|
130 | - * This function proceeds the template and prints its output. |
|
131 | - */ |
|
132 | - public function printPage() { |
|
133 | - $data = $this->fetchPage(); |
|
134 | - if ($data === false) { |
|
135 | - return false; |
|
136 | - } else { |
|
137 | - print $data; |
|
138 | - return true; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Process the template |
|
144 | - * |
|
145 | - * @param array|null $additionalParams |
|
146 | - * @return string This function processes the template. |
|
147 | - * |
|
148 | - * This function processes the template. |
|
149 | - */ |
|
150 | - public function fetchPage($additionalParams = null) { |
|
151 | - return $this->load($this->template, $additionalParams); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * doing the actual work |
|
156 | - * |
|
157 | - * @param string $file |
|
158 | - * @param array|null $additionalParams |
|
159 | - * @return string content |
|
160 | - * |
|
161 | - * Includes the template file, fetches its output |
|
162 | - */ |
|
163 | - protected function load($file, $additionalParams = null) { |
|
164 | - // Register the variables |
|
165 | - $_ = $this->vars; |
|
166 | - $l = $this->l10n; |
|
167 | - $theme = $this->theme; |
|
168 | - |
|
169 | - if (!is_null($additionalParams)) { |
|
170 | - $_ = array_merge($additionalParams, $this->vars); |
|
171 | - foreach ($_ as $var => $value) { |
|
172 | - ${$var} = $value; |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - // Include |
|
177 | - ob_start(); |
|
178 | - try { |
|
179 | - include $file; |
|
180 | - $data = ob_get_contents(); |
|
181 | - } catch (\Exception $e) { |
|
182 | - @ob_end_clean(); |
|
183 | - throw $e; |
|
184 | - } |
|
185 | - @ob_end_clean(); |
|
186 | - |
|
187 | - // Return data |
|
188 | - return $data; |
|
189 | - } |
|
37 | + private $template; // The template |
|
38 | + private $vars; // Vars |
|
39 | + |
|
40 | + /** @var \OCP\IL10N */ |
|
41 | + private $l10n; |
|
42 | + |
|
43 | + /** @var Defaults */ |
|
44 | + private $theme; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param string $template |
|
48 | + * @param string $requestToken |
|
49 | + * @param \OCP\IL10N $l10n |
|
50 | + * @param Defaults $theme |
|
51 | + */ |
|
52 | + public function __construct($template, $requestToken, $l10n, $theme) { |
|
53 | + $this->vars = []; |
|
54 | + $this->vars['requesttoken'] = $requestToken; |
|
55 | + $this->l10n = $l10n; |
|
56 | + $this->template = $template; |
|
57 | + $this->theme = $theme; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $serverRoot |
|
62 | + * @param string|false $app_dir |
|
63 | + * @param string $theme |
|
64 | + * @param string $app |
|
65 | + * @return string[] |
|
66 | + */ |
|
67 | + protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
68 | + // Check if the app is in the app folder or in the root |
|
69 | + if (file_exists($app_dir.'/templates/')) { |
|
70 | + return [ |
|
71 | + $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
72 | + $app_dir.'/templates/', |
|
73 | + ]; |
|
74 | + } |
|
75 | + return [ |
|
76 | + $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
77 | + $serverRoot.'/'.$app.'/templates/', |
|
78 | + ]; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @param string $serverRoot |
|
83 | + * @param string $theme |
|
84 | + * @return string[] |
|
85 | + */ |
|
86 | + protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
87 | + return [ |
|
88 | + $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
89 | + $serverRoot.'/core/templates/', |
|
90 | + ]; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Assign variables |
|
95 | + * @param string $key key |
|
96 | + * @param array|bool|integer|string|Throwable $value value |
|
97 | + * @return bool |
|
98 | + * |
|
99 | + * This function assigns a variable. It can be accessed via $_[$key] in |
|
100 | + * the template. |
|
101 | + * |
|
102 | + * If the key existed before, it will be overwritten |
|
103 | + */ |
|
104 | + public function assign($key, $value) { |
|
105 | + $this->vars[$key] = $value; |
|
106 | + return true; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Appends a variable |
|
111 | + * @param string $key key |
|
112 | + * @param mixed $value value |
|
113 | + * |
|
114 | + * This function assigns a variable in an array context. If the key already |
|
115 | + * exists, the value will be appended. It can be accessed via |
|
116 | + * $_[$key][$position] in the template. |
|
117 | + */ |
|
118 | + public function append($key, $value) { |
|
119 | + if (array_key_exists($key, $this->vars)) { |
|
120 | + $this->vars[$key][] = $value; |
|
121 | + } else { |
|
122 | + $this->vars[$key] = [ $value ]; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Prints the proceeded template |
|
128 | + * @return bool |
|
129 | + * |
|
130 | + * This function proceeds the template and prints its output. |
|
131 | + */ |
|
132 | + public function printPage() { |
|
133 | + $data = $this->fetchPage(); |
|
134 | + if ($data === false) { |
|
135 | + return false; |
|
136 | + } else { |
|
137 | + print $data; |
|
138 | + return true; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Process the template |
|
144 | + * |
|
145 | + * @param array|null $additionalParams |
|
146 | + * @return string This function processes the template. |
|
147 | + * |
|
148 | + * This function processes the template. |
|
149 | + */ |
|
150 | + public function fetchPage($additionalParams = null) { |
|
151 | + return $this->load($this->template, $additionalParams); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * doing the actual work |
|
156 | + * |
|
157 | + * @param string $file |
|
158 | + * @param array|null $additionalParams |
|
159 | + * @return string content |
|
160 | + * |
|
161 | + * Includes the template file, fetches its output |
|
162 | + */ |
|
163 | + protected function load($file, $additionalParams = null) { |
|
164 | + // Register the variables |
|
165 | + $_ = $this->vars; |
|
166 | + $l = $this->l10n; |
|
167 | + $theme = $this->theme; |
|
168 | + |
|
169 | + if (!is_null($additionalParams)) { |
|
170 | + $_ = array_merge($additionalParams, $this->vars); |
|
171 | + foreach ($_ as $var => $value) { |
|
172 | + ${$var} = $value; |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + // Include |
|
177 | + ob_start(); |
|
178 | + try { |
|
179 | + include $file; |
|
180 | + $data = ob_get_contents(); |
|
181 | + } catch (\Exception $e) { |
|
182 | + @ob_end_clean(); |
|
183 | + throw $e; |
|
184 | + } |
|
185 | + @ob_end_clean(); |
|
186 | + |
|
187 | + // Return data |
|
188 | + return $data; |
|
189 | + } |
|
190 | 190 | } |