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