Completed
Pull Request — master (#4043)
by Lukas
49:42 queued 37:57
created
lib/private/legacy/template.php 2 patches
Indentation   +325 added lines, -325 removed lines patch added patch discarded remove patch
@@ -44,329 +44,329 @@
 block discarded – undo
44 44
  */
45 45
 class OC_Template extends \OC\Template\Base {
46 46
 
47
-	/** @var string */
48
-	private $renderAs; // Create a full page?
49
-
50
-	/** @var string */
51
-	private $path; // The path to the template
52
-
53
-	/** @var array */
54
-	private $headers = array(); //custom headers
55
-
56
-	/** @var string */
57
-	protected $app; // app id
58
-
59
-	protected static $initTemplateEngineFirstRun = true;
60
-
61
-	/**
62
-	 * Constructor
63
-	 *
64
-	 * @param string $app app providing the template
65
-	 * @param string $name of the template file (without suffix)
66
-	 * @param string $renderAs If $renderAs is set, OC_Template will try to
67
-	 *                         produce a full page in the according layout. For
68
-	 *                         now, $renderAs can be set to "guest", "user" or
69
-	 *                         "admin".
70
-	 * @param bool $registerCall = true
71
-	 */
72
-	public function __construct( $app, $name, $renderAs = "", $registerCall = true ) {
73
-		// Read the selected theme from the config file
74
-		self::initTemplateEngine($renderAs);
75
-
76
-		$theme = OC_Util::getTheme();
77
-
78
-		$requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';
79
-
80
-		$parts = explode('/', $app); // fix translation when app is something like core/lostpassword
81
-		$l10n = \OC::$server->getL10N($parts[0]);
82
-		$themeDefaults = \OC::$server->getThemingDefaults();
83
-
84
-		list($path, $template) = $this->findTemplate($theme, $app, $name);
85
-
86
-		// Set the private data
87
-		$this->renderAs = $renderAs;
88
-		$this->path = $path;
89
-		$this->app = $app;
90
-
91
-		parent::__construct($template, $requestToken, $l10n, $themeDefaults);
92
-	}
93
-
94
-	/**
95
-	 * @param string $renderAs
96
-	 */
97
-	public static function initTemplateEngine($renderAs) {
98
-		if (self::$initTemplateEngineFirstRun){
99
-
100
-			//apps that started before the template initialization can load their own scripts/styles
101
-			//so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true
102
-			//meaning the last script/style in this list will be loaded first
103
-			if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
104
-				if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') {
105
-					OC_Util::addScript ( 'backgroundjobs', null, true );
106
-				}
107
-			}
108
-
109
-			OC_Util::addStyle('jquery-ui-fixes',null,true);
110
-			OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui',null,true);
111
-			OC_Util::addStyle('server', null, true);
112
-			OC_Util::addVendorStyle('select2/select2', null, true);
113
-			OC_Util::addStyle('jquery.ocdialog');
114
-			OC_Util::addTranslations("core", null, true);
115
-			OC_Util::addScript('search', 'search', true);
116
-			OC_Util::addScript('merged-template-prepend', null, true);
117
-			OC_Util::addScript('jquery-ui-fixes');
118
-			OC_Util::addScript('files/fileinfo');
119
-			OC_Util::addScript('files/client');
120
-
121
-			if (\OC::$server->getConfig()->getSystemValue('debug')) {
122
-				// Add the stuff we need always
123
-				// following logic will import all vendor libraries that are
124
-				// specified in core/js/core.json
125
-				$fileContent = file_get_contents(OC::$SERVERROOT . '/core/js/core.json');
126
-				if($fileContent !== false) {
127
-					$coreDependencies = json_decode($fileContent, true);
128
-					foreach(array_reverse($coreDependencies['vendor']) as $vendorLibrary) {
129
-						//remove trailing ".js" as addVendorScript will append it
130
-						OC_Util::addVendorScript(
131
-							substr($vendorLibrary, 0, strlen($vendorLibrary) - 3),null,true);
132
-						}
133
- 				} else {
134
-					throw new \Exception('Cannot read core/js/core.json');
135
-				}
136
-			} else {
137
-				// Import all (combined) default vendor libraries
138
-				OC_Util::addVendorScript('core', null, true);
139
-			}
140
-
141
-			if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) {
142
-				// polyfill for btoa/atob for IE friends
143
-				OC_Util::addVendorScript('base64/base64');
144
-				// shim for the davclient.js library
145
-				\OCP\Util::addScript('files/iedavclient');
146
-			}
147
-
148
-			self::$initTemplateEngineFirstRun = false;
149
-		}
150
-
151
-	}
152
-
153
-
154
-	/**
155
-	 * find the template with the given name
156
-	 * @param string $name of the template file (without suffix)
157
-	 *
158
-	 * Will select the template file for the selected theme.
159
-	 * Checking all the possible locations.
160
-	 * @param string $theme
161
-	 * @param string $app
162
-	 * @return string[]
163
-	 */
164
-	protected function findTemplate($theme, $app, $name) {
165
-		// Check if it is a app template or not.
166
-		if( $app !== '' ) {
167
-			$dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
168
-		} else {
169
-			$dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
170
-		}
171
-		$locator = new \OC\Template\TemplateFileLocator( $dirs );
172
-		$template = $locator->find($name);
173
-		$path = $locator->getPath();
174
-		return array($path, $template);
175
-	}
176
-
177
-	/**
178
-	 * Add a custom element to the header
179
-	 * @param string $tag tag name of the element
180
-	 * @param array $attributes array of attributes for the element
181
-	 * @param string $text the text content for the element. If $text is null then the
182
-	 * element will be written as empty element. So use "" to get a closing tag.
183
-	 */
184
-	public function addHeader($tag, $attributes, $text=null) {
185
-		$this->headers[]= array(
186
-			'tag' => $tag,
187
-			'attributes' => $attributes,
188
-			'text' => $text
189
-		);
190
-	}
191
-
192
-	/**
193
-	 * Process the template
194
-	 * @return boolean|string
195
-	 *
196
-	 * This function process the template. If $this->renderAs is set, it
197
-	 * will produce a full page.
198
-	 */
199
-	public function fetchPage($additionalParams = null) {
200
-		$data = parent::fetchPage($additionalParams);
201
-
202
-		if( $this->renderAs ) {
203
-			$page = new TemplateLayout($this->renderAs, $this->app);
204
-
205
-			// Add custom headers
206
-			$headers = '';
207
-			foreach(OC_Util::$headers as $header) {
208
-				$headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']);
209
-				foreach($header['attributes'] as $name=>$value) {
210
-					$headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"';
211
-				}
212
-				if ($header['text'] !== null) {
213
-					$headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>';
214
-				} else {
215
-					$headers .= '/>';
216
-				}
217
-			}
218
-
219
-			$page->assign('headers', $headers);
220
-
221
-			$page->assign('content', $data);
222
-			return $page->fetchPage();
223
-		}
224
-
225
-		return $data;
226
-	}
227
-
228
-	/**
229
-	 * Include template
230
-	 *
231
-	 * @param string $file
232
-	 * @param array|null $additionalParams
233
-	 * @return string returns content of included template
234
-	 *
235
-	 * Includes another template. use <?php echo $this->inc('template'); ?> to
236
-	 * do this.
237
-	 */
238
-	public function inc( $file, $additionalParams = null ) {
239
-		return $this->load($this->path.$file.'.php', $additionalParams);
240
-	}
241
-
242
-	/**
243
-	 * Shortcut to print a simple page for users
244
-	 * @param string $application The application we render the template for
245
-	 * @param string $name Name of the template
246
-	 * @param array $parameters Parameters for the template
247
-	 * @return boolean|null
248
-	 */
249
-	public static function printUserPage( $application, $name, $parameters = array() ) {
250
-		$content = new OC_Template( $application, $name, "user" );
251
-		foreach( $parameters as $key => $value ) {
252
-			$content->assign( $key, $value );
253
-		}
254
-		print $content->printPage();
255
-	}
256
-
257
-	/**
258
-	 * Shortcut to print a simple page for admins
259
-	 * @param string $application The application we render the template for
260
-	 * @param string $name Name of the template
261
-	 * @param array $parameters Parameters for the template
262
-	 * @return bool
263
-	 */
264
-	public static function printAdminPage( $application, $name, $parameters = array() ) {
265
-		$content = new OC_Template( $application, $name, "admin" );
266
-		foreach( $parameters as $key => $value ) {
267
-			$content->assign( $key, $value );
268
-		}
269
-		return $content->printPage();
270
-	}
271
-
272
-	/**
273
-	 * Shortcut to print a simple page for guests
274
-	 * @param string $application The application we render the template for
275
-	 * @param string $name Name of the template
276
-	 * @param array|string $parameters Parameters for the template
277
-	 * @return bool
278
-	 */
279
-	public static function printGuestPage( $application, $name, $parameters = array() ) {
280
-		$content = new OC_Template( $application, $name, "guest" );
281
-		foreach( $parameters as $key => $value ) {
282
-			$content->assign( $key, $value );
283
-		}
284
-		return $content->printPage();
285
-	}
286
-
287
-	/**
288
-		* Print a fatal error page and terminates the script
289
-		* @param string $error_msg The error message to show
290
-		* @param string $hint An optional hint message - needs to be properly escaped
291
-		*/
292
-	public static function printErrorPage( $error_msg, $hint = '' ) {
293
-		if ($error_msg === $hint) {
294
-			// If the hint is the same as the message there is no need to display it twice.
295
-			$hint = '';
296
-		}
297
-
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(self::getHttpProtocol() . ' 500 Internal Server Error');
309
-			header('Content-Type: text/plain; charset=utf-8');
310
-			print("$error_msg $hint");
311
-		}
312
-		die();
313
-	}
314
-
315
-	/**
316
-	 * print error page using Exception details
317
-	 * @param Exception | Throwable $exception
318
-	 */
319
-	public static function printExceptionErrorPage($exception, $fetchPage = false) {
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('trace', $exception->getTraceAsString());
329
-			$content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false));
330
-			$content->assign('remoteAddr', $request->getRemoteAddress());
331
-			$content->assign('requestID', $request->getId());
332
-			if ($fetchPage) {
333
-				return $content->fetchPage();
334
-			}
335
-			$content->printPage();
336
-		} catch (\Exception $e) {
337
-			$logger = \OC::$server->getLogger();
338
-			$logger->logException($exception, ['app' => 'core']);
339
-			$logger->logException($e, ['app' => 'core']);
340
-
341
-			header(self::getHttpProtocol() . ' 500 Internal Server Error');
342
-			header('Content-Type: text/plain; charset=utf-8');
343
-			print("Internal Server Error\n\n");
344
-			print("The server encountered an internal error and was unable to complete your request.\n");
345
-			print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
346
-			print("More details can be found in the server log.\n");
347
-		}
348
-		die();
349
-	}
350
-
351
-	/**
352
-	 * This is only here to reduce the dependencies in case of an exception to
353
-	 * still be able to print a plain error message.
354
-	 *
355
-	 * Returns the used HTTP protocol.
356
-	 *
357
-	 * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0.
358
-	 * @internal Don't use this - use AppFramework\Http\Request->getHttpProtocol instead
359
-	 */
360
-	protected static function getHttpProtocol() {
361
-		$claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
362
-		$validProtocols = [
363
-			'HTTP/1.0',
364
-			'HTTP/1.1',
365
-			'HTTP/2',
366
-		];
367
-		if(in_array($claimedProtocol, $validProtocols, true)) {
368
-			return $claimedProtocol;
369
-		}
370
-		return 'HTTP/1.1';
371
-	}
47
+    /** @var string */
48
+    private $renderAs; // Create a full page?
49
+
50
+    /** @var string */
51
+    private $path; // The path to the template
52
+
53
+    /** @var array */
54
+    private $headers = array(); //custom headers
55
+
56
+    /** @var string */
57
+    protected $app; // app id
58
+
59
+    protected static $initTemplateEngineFirstRun = true;
60
+
61
+    /**
62
+     * Constructor
63
+     *
64
+     * @param string $app app providing the template
65
+     * @param string $name of the template file (without suffix)
66
+     * @param string $renderAs If $renderAs is set, OC_Template will try to
67
+     *                         produce a full page in the according layout. For
68
+     *                         now, $renderAs can be set to "guest", "user" or
69
+     *                         "admin".
70
+     * @param bool $registerCall = true
71
+     */
72
+    public function __construct( $app, $name, $renderAs = "", $registerCall = true ) {
73
+        // Read the selected theme from the config file
74
+        self::initTemplateEngine($renderAs);
75
+
76
+        $theme = OC_Util::getTheme();
77
+
78
+        $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';
79
+
80
+        $parts = explode('/', $app); // fix translation when app is something like core/lostpassword
81
+        $l10n = \OC::$server->getL10N($parts[0]);
82
+        $themeDefaults = \OC::$server->getThemingDefaults();
83
+
84
+        list($path, $template) = $this->findTemplate($theme, $app, $name);
85
+
86
+        // Set the private data
87
+        $this->renderAs = $renderAs;
88
+        $this->path = $path;
89
+        $this->app = $app;
90
+
91
+        parent::__construct($template, $requestToken, $l10n, $themeDefaults);
92
+    }
93
+
94
+    /**
95
+     * @param string $renderAs
96
+     */
97
+    public static function initTemplateEngine($renderAs) {
98
+        if (self::$initTemplateEngineFirstRun){
99
+
100
+            //apps that started before the template initialization can load their own scripts/styles
101
+            //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true
102
+            //meaning the last script/style in this list will be loaded first
103
+            if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
104
+                if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') {
105
+                    OC_Util::addScript ( 'backgroundjobs', null, true );
106
+                }
107
+            }
108
+
109
+            OC_Util::addStyle('jquery-ui-fixes',null,true);
110
+            OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui',null,true);
111
+            OC_Util::addStyle('server', null, true);
112
+            OC_Util::addVendorStyle('select2/select2', null, true);
113
+            OC_Util::addStyle('jquery.ocdialog');
114
+            OC_Util::addTranslations("core", null, true);
115
+            OC_Util::addScript('search', 'search', true);
116
+            OC_Util::addScript('merged-template-prepend', null, true);
117
+            OC_Util::addScript('jquery-ui-fixes');
118
+            OC_Util::addScript('files/fileinfo');
119
+            OC_Util::addScript('files/client');
120
+
121
+            if (\OC::$server->getConfig()->getSystemValue('debug')) {
122
+                // Add the stuff we need always
123
+                // following logic will import all vendor libraries that are
124
+                // specified in core/js/core.json
125
+                $fileContent = file_get_contents(OC::$SERVERROOT . '/core/js/core.json');
126
+                if($fileContent !== false) {
127
+                    $coreDependencies = json_decode($fileContent, true);
128
+                    foreach(array_reverse($coreDependencies['vendor']) as $vendorLibrary) {
129
+                        //remove trailing ".js" as addVendorScript will append it
130
+                        OC_Util::addVendorScript(
131
+                            substr($vendorLibrary, 0, strlen($vendorLibrary) - 3),null,true);
132
+                        }
133
+                    } else {
134
+                    throw new \Exception('Cannot read core/js/core.json');
135
+                }
136
+            } else {
137
+                // Import all (combined) default vendor libraries
138
+                OC_Util::addVendorScript('core', null, true);
139
+            }
140
+
141
+            if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) {
142
+                // polyfill for btoa/atob for IE friends
143
+                OC_Util::addVendorScript('base64/base64');
144
+                // shim for the davclient.js library
145
+                \OCP\Util::addScript('files/iedavclient');
146
+            }
147
+
148
+            self::$initTemplateEngineFirstRun = false;
149
+        }
150
+
151
+    }
152
+
153
+
154
+    /**
155
+     * find the template with the given name
156
+     * @param string $name of the template file (without suffix)
157
+     *
158
+     * Will select the template file for the selected theme.
159
+     * Checking all the possible locations.
160
+     * @param string $theme
161
+     * @param string $app
162
+     * @return string[]
163
+     */
164
+    protected function findTemplate($theme, $app, $name) {
165
+        // Check if it is a app template or not.
166
+        if( $app !== '' ) {
167
+            $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
168
+        } else {
169
+            $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
170
+        }
171
+        $locator = new \OC\Template\TemplateFileLocator( $dirs );
172
+        $template = $locator->find($name);
173
+        $path = $locator->getPath();
174
+        return array($path, $template);
175
+    }
176
+
177
+    /**
178
+     * Add a custom element to the header
179
+     * @param string $tag tag name of the element
180
+     * @param array $attributes array of attributes for the element
181
+     * @param string $text the text content for the element. If $text is null then the
182
+     * element will be written as empty element. So use "" to get a closing tag.
183
+     */
184
+    public function addHeader($tag, $attributes, $text=null) {
185
+        $this->headers[]= array(
186
+            'tag' => $tag,
187
+            'attributes' => $attributes,
188
+            'text' => $text
189
+        );
190
+    }
191
+
192
+    /**
193
+     * Process the template
194
+     * @return boolean|string
195
+     *
196
+     * This function process the template. If $this->renderAs is set, it
197
+     * will produce a full page.
198
+     */
199
+    public function fetchPage($additionalParams = null) {
200
+        $data = parent::fetchPage($additionalParams);
201
+
202
+        if( $this->renderAs ) {
203
+            $page = new TemplateLayout($this->renderAs, $this->app);
204
+
205
+            // Add custom headers
206
+            $headers = '';
207
+            foreach(OC_Util::$headers as $header) {
208
+                $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']);
209
+                foreach($header['attributes'] as $name=>$value) {
210
+                    $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"';
211
+                }
212
+                if ($header['text'] !== null) {
213
+                    $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>';
214
+                } else {
215
+                    $headers .= '/>';
216
+                }
217
+            }
218
+
219
+            $page->assign('headers', $headers);
220
+
221
+            $page->assign('content', $data);
222
+            return $page->fetchPage();
223
+        }
224
+
225
+        return $data;
226
+    }
227
+
228
+    /**
229
+     * Include template
230
+     *
231
+     * @param string $file
232
+     * @param array|null $additionalParams
233
+     * @return string returns content of included template
234
+     *
235
+     * Includes another template. use <?php echo $this->inc('template'); ?> to
236
+     * do this.
237
+     */
238
+    public function inc( $file, $additionalParams = null ) {
239
+        return $this->load($this->path.$file.'.php', $additionalParams);
240
+    }
241
+
242
+    /**
243
+     * Shortcut to print a simple page for users
244
+     * @param string $application The application we render the template for
245
+     * @param string $name Name of the template
246
+     * @param array $parameters Parameters for the template
247
+     * @return boolean|null
248
+     */
249
+    public static function printUserPage( $application, $name, $parameters = array() ) {
250
+        $content = new OC_Template( $application, $name, "user" );
251
+        foreach( $parameters as $key => $value ) {
252
+            $content->assign( $key, $value );
253
+        }
254
+        print $content->printPage();
255
+    }
256
+
257
+    /**
258
+     * Shortcut to print a simple page for admins
259
+     * @param string $application The application we render the template for
260
+     * @param string $name Name of the template
261
+     * @param array $parameters Parameters for the template
262
+     * @return bool
263
+     */
264
+    public static function printAdminPage( $application, $name, $parameters = array() ) {
265
+        $content = new OC_Template( $application, $name, "admin" );
266
+        foreach( $parameters as $key => $value ) {
267
+            $content->assign( $key, $value );
268
+        }
269
+        return $content->printPage();
270
+    }
271
+
272
+    /**
273
+     * Shortcut to print a simple page for guests
274
+     * @param string $application The application we render the template for
275
+     * @param string $name Name of the template
276
+     * @param array|string $parameters Parameters for the template
277
+     * @return bool
278
+     */
279
+    public static function printGuestPage( $application, $name, $parameters = array() ) {
280
+        $content = new OC_Template( $application, $name, "guest" );
281
+        foreach( $parameters as $key => $value ) {
282
+            $content->assign( $key, $value );
283
+        }
284
+        return $content->printPage();
285
+    }
286
+
287
+    /**
288
+     * Print a fatal error page and terminates the script
289
+     * @param string $error_msg The error message to show
290
+     * @param string $hint An optional hint message - needs to be properly escaped
291
+     */
292
+    public static function printErrorPage( $error_msg, $hint = '' ) {
293
+        if ($error_msg === $hint) {
294
+            // If the hint is the same as the message there is no need to display it twice.
295
+            $hint = '';
296
+        }
297
+
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(self::getHttpProtocol() . ' 500 Internal Server Error');
309
+            header('Content-Type: text/plain; charset=utf-8');
310
+            print("$error_msg $hint");
311
+        }
312
+        die();
313
+    }
314
+
315
+    /**
316
+     * print error page using Exception details
317
+     * @param Exception | Throwable $exception
318
+     */
319
+    public static function printExceptionErrorPage($exception, $fetchPage = false) {
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('trace', $exception->getTraceAsString());
329
+            $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false));
330
+            $content->assign('remoteAddr', $request->getRemoteAddress());
331
+            $content->assign('requestID', $request->getId());
332
+            if ($fetchPage) {
333
+                return $content->fetchPage();
334
+            }
335
+            $content->printPage();
336
+        } catch (\Exception $e) {
337
+            $logger = \OC::$server->getLogger();
338
+            $logger->logException($exception, ['app' => 'core']);
339
+            $logger->logException($e, ['app' => 'core']);
340
+
341
+            header(self::getHttpProtocol() . ' 500 Internal Server Error');
342
+            header('Content-Type: text/plain; charset=utf-8');
343
+            print("Internal Server Error\n\n");
344
+            print("The server encountered an internal error and was unable to complete your request.\n");
345
+            print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
346
+            print("More details can be found in the server log.\n");
347
+        }
348
+        die();
349
+    }
350
+
351
+    /**
352
+     * This is only here to reduce the dependencies in case of an exception to
353
+     * still be able to print a plain error message.
354
+     *
355
+     * Returns the used HTTP protocol.
356
+     *
357
+     * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0.
358
+     * @internal Don't use this - use AppFramework\Http\Request->getHttpProtocol instead
359
+     */
360
+    protected static function getHttpProtocol() {
361
+        $claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
362
+        $validProtocols = [
363
+            'HTTP/1.0',
364
+            'HTTP/1.1',
365
+            'HTTP/2',
366
+        ];
367
+        if(in_array($claimedProtocol, $validProtocols, true)) {
368
+            return $claimedProtocol;
369
+        }
370
+        return 'HTTP/1.1';
371
+    }
372 372
 }
Please login to merge, or discard this patch.
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 	 *                         "admin".
70 70
 	 * @param bool $registerCall = true
71 71
 	 */
72
-	public function __construct( $app, $name, $renderAs = "", $registerCall = true ) {
72
+	public function __construct($app, $name, $renderAs = "", $registerCall = true) {
73 73
 		// Read the selected theme from the config file
74 74
 		self::initTemplateEngine($renderAs);
75 75
 
@@ -95,19 +95,19 @@  discard block
 block discarded – undo
95 95
 	 * @param string $renderAs
96 96
 	 */
97 97
 	public static function initTemplateEngine($renderAs) {
98
-		if (self::$initTemplateEngineFirstRun){
98
+		if (self::$initTemplateEngineFirstRun) {
99 99
 
100 100
 			//apps that started before the template initialization can load their own scripts/styles
101 101
 			//so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true
102 102
 			//meaning the last script/style in this list will be loaded first
103
-			if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
104
-				if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') {
105
-					OC_Util::addScript ( 'backgroundjobs', null, true );
103
+			if (\OC::$server->getSystemConfig()->getValue('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
104
+				if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
105
+					OC_Util::addScript('backgroundjobs', null, true);
106 106
 				}
107 107
 			}
108 108
 
109
-			OC_Util::addStyle('jquery-ui-fixes',null,true);
110
-			OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui',null,true);
109
+			OC_Util::addStyle('jquery-ui-fixes', null, true);
110
+			OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui', null, true);
111 111
 			OC_Util::addStyle('server', null, true);
112 112
 			OC_Util::addVendorStyle('select2/select2', null, true);
113 113
 			OC_Util::addStyle('jquery.ocdialog');
@@ -122,13 +122,13 @@  discard block
 block discarded – undo
122 122
 				// Add the stuff we need always
123 123
 				// following logic will import all vendor libraries that are
124 124
 				// specified in core/js/core.json
125
-				$fileContent = file_get_contents(OC::$SERVERROOT . '/core/js/core.json');
126
-				if($fileContent !== false) {
125
+				$fileContent = file_get_contents(OC::$SERVERROOT.'/core/js/core.json');
126
+				if ($fileContent !== false) {
127 127
 					$coreDependencies = json_decode($fileContent, true);
128
-					foreach(array_reverse($coreDependencies['vendor']) as $vendorLibrary) {
128
+					foreach (array_reverse($coreDependencies['vendor']) as $vendorLibrary) {
129 129
 						//remove trailing ".js" as addVendorScript will append it
130 130
 						OC_Util::addVendorScript(
131
-							substr($vendorLibrary, 0, strlen($vendorLibrary) - 3),null,true);
131
+							substr($vendorLibrary, 0, strlen($vendorLibrary) - 3), null, true);
132 132
 						}
133 133
  				} else {
134 134
 					throw new \Exception('Cannot read core/js/core.json');
@@ -163,12 +163,12 @@  discard block
 block discarded – undo
163 163
 	 */
164 164
 	protected function findTemplate($theme, $app, $name) {
165 165
 		// Check if it is a app template or not.
166
-		if( $app !== '' ) {
166
+		if ($app !== '') {
167 167
 			$dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
168 168
 		} else {
169 169
 			$dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
170 170
 		}
171
-		$locator = new \OC\Template\TemplateFileLocator( $dirs );
171
+		$locator = new \OC\Template\TemplateFileLocator($dirs);
172 172
 		$template = $locator->find($name);
173 173
 		$path = $locator->getPath();
174 174
 		return array($path, $template);
@@ -181,8 +181,8 @@  discard block
 block discarded – undo
181 181
 	 * @param string $text the text content for the element. If $text is null then the
182 182
 	 * element will be written as empty element. So use "" to get a closing tag.
183 183
 	 */
184
-	public function addHeader($tag, $attributes, $text=null) {
185
-		$this->headers[]= array(
184
+	public function addHeader($tag, $attributes, $text = null) {
185
+		$this->headers[] = array(
186 186
 			'tag' => $tag,
187 187
 			'attributes' => $attributes,
188 188
 			'text' => $text
@@ -199,14 +199,14 @@  discard block
 block discarded – undo
199 199
 	public function fetchPage($additionalParams = null) {
200 200
 		$data = parent::fetchPage($additionalParams);
201 201
 
202
-		if( $this->renderAs ) {
202
+		if ($this->renderAs) {
203 203
 			$page = new TemplateLayout($this->renderAs, $this->app);
204 204
 
205 205
 			// Add custom headers
206 206
 			$headers = '';
207
-			foreach(OC_Util::$headers as $header) {
207
+			foreach (OC_Util::$headers as $header) {
208 208
 				$headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']);
209
-				foreach($header['attributes'] as $name=>$value) {
209
+				foreach ($header['attributes'] as $name=>$value) {
210 210
 					$headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"';
211 211
 				}
212 212
 				if ($header['text'] !== null) {
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
 	 * Includes another template. use <?php echo $this->inc('template'); ?> to
236 236
 	 * do this.
237 237
 	 */
238
-	public function inc( $file, $additionalParams = null ) {
238
+	public function inc($file, $additionalParams = null) {
239 239
 		return $this->load($this->path.$file.'.php', $additionalParams);
240 240
 	}
241 241
 
@@ -246,10 +246,10 @@  discard block
 block discarded – undo
246 246
 	 * @param array $parameters Parameters for the template
247 247
 	 * @return boolean|null
248 248
 	 */
249
-	public static function printUserPage( $application, $name, $parameters = array() ) {
250
-		$content = new OC_Template( $application, $name, "user" );
251
-		foreach( $parameters as $key => $value ) {
252
-			$content->assign( $key, $value );
249
+	public static function printUserPage($application, $name, $parameters = array()) {
250
+		$content = new OC_Template($application, $name, "user");
251
+		foreach ($parameters as $key => $value) {
252
+			$content->assign($key, $value);
253 253
 		}
254 254
 		print $content->printPage();
255 255
 	}
@@ -261,10 +261,10 @@  discard block
 block discarded – undo
261 261
 	 * @param array $parameters Parameters for the template
262 262
 	 * @return bool
263 263
 	 */
264
-	public static function printAdminPage( $application, $name, $parameters = array() ) {
265
-		$content = new OC_Template( $application, $name, "admin" );
266
-		foreach( $parameters as $key => $value ) {
267
-			$content->assign( $key, $value );
264
+	public static function printAdminPage($application, $name, $parameters = array()) {
265
+		$content = new OC_Template($application, $name, "admin");
266
+		foreach ($parameters as $key => $value) {
267
+			$content->assign($key, $value);
268 268
 		}
269 269
 		return $content->printPage();
270 270
 	}
@@ -276,10 +276,10 @@  discard block
 block discarded – undo
276 276
 	 * @param array|string $parameters Parameters for the template
277 277
 	 * @return bool
278 278
 	 */
279
-	public static function printGuestPage( $application, $name, $parameters = array() ) {
280
-		$content = new OC_Template( $application, $name, "guest" );
281
-		foreach( $parameters as $key => $value ) {
282
-			$content->assign( $key, $value );
279
+	public static function printGuestPage($application, $name, $parameters = array()) {
280
+		$content = new OC_Template($application, $name, "guest");
281
+		foreach ($parameters as $key => $value) {
282
+			$content->assign($key, $value);
283 283
 		}
284 284
 		return $content->printPage();
285 285
 	}
@@ -289,23 +289,23 @@  discard block
 block discarded – undo
289 289
 		* @param string $error_msg The error message to show
290 290
 		* @param string $hint An optional hint message - needs to be properly escaped
291 291
 		*/
292
-	public static function printErrorPage( $error_msg, $hint = '' ) {
292
+	public static function printErrorPage($error_msg, $hint = '') {
293 293
 		if ($error_msg === $hint) {
294 294
 			// If the hint is the same as the message there is no need to display it twice.
295 295
 			$hint = '';
296 296
 		}
297 297
 
298 298
 		try {
299
-			$content = new \OC_Template( '', 'error', 'error', false );
299
+			$content = new \OC_Template('', 'error', 'error', false);
300 300
 			$errors = array(array('error' => $error_msg, 'hint' => $hint));
301
-			$content->assign( 'errors', $errors );
301
+			$content->assign('errors', $errors);
302 302
 			$content->printPage();
303 303
 		} catch (\Exception $e) {
304 304
 			$logger = \OC::$server->getLogger();
305 305
 			$logger->error("$error_msg $hint", ['app' => 'core']);
306 306
 			$logger->logException($e, ['app' => 'core']);
307 307
 
308
-			header(self::getHttpProtocol() . ' 500 Internal Server Error');
308
+			header(self::getHttpProtocol().' 500 Internal Server Error');
309 309
 			header('Content-Type: text/plain; charset=utf-8');
310 310
 			print("$error_msg $hint");
311 311
 		}
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
 			$logger->logException($exception, ['app' => 'core']);
339 339
 			$logger->logException($e, ['app' => 'core']);
340 340
 
341
-			header(self::getHttpProtocol() . ' 500 Internal Server Error');
341
+			header(self::getHttpProtocol().' 500 Internal Server Error');
342 342
 			header('Content-Type: text/plain; charset=utf-8');
343 343
 			print("Internal Server Error\n\n");
344 344
 			print("The server encountered an internal error and was unable to complete your request.\n");
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
 			'HTTP/1.1',
365 365
 			'HTTP/2',
366 366
 		];
367
-		if(in_array($claimedProtocol, $validProtocols, true)) {
367
+		if (in_array($claimedProtocol, $validProtocols, true)) {
368 368
 			return $claimedProtocol;
369 369
 		}
370 370
 		return 'HTTP/1.1';
Please login to merge, or discard this patch.