77 */
88namespace OCA \DAV \Files ;
99
10- use OC \AppFramework \Http \Request ;
1110use OC_Template ;
1211use OCP \AppFramework \Http \ContentSecurityPolicy ;
12+ use OCP \IConfig ;
1313use OCP \IRequest ;
1414use Sabre \DAV \Exception ;
1515use Sabre \DAV \Server ;
1616use Sabre \DAV \ServerPlugin ;
1717
18- class BrowserErrorPagePlugin extends ServerPlugin {
19- /** @var Server */
20- private $ server ;
18+ class ErrorPagePlugin extends ServerPlugin {
19+ private ?Server $ server = null ;
20+
21+ public function __construct (
22+ private IRequest $ request ,
23+ private IConfig $ config ,
24+ ) {
25+ }
2126
2227 /**
2328 * This initializes the plugin.
@@ -26,35 +31,12 @@ class BrowserErrorPagePlugin extends ServerPlugin {
2631 * addPlugin is called.
2732 *
2833 * This method should set up the required event subscriptions.
29- *
30- * @param Server $server
31- * @return void
3234 */
33- public function initialize (Server $ server ) {
35+ public function initialize (Server $ server ): void {
3436 $ this ->server = $ server ;
3537 $ server ->on ('exception ' , [$ this , 'logException ' ], 1000 );
3638 }
3739
38- /**
39- * @param IRequest $request
40- * @return bool
41- */
42- public static function isBrowserRequest (IRequest $ request ) {
43- if ($ request ->getMethod () !== 'GET ' ) {
44- return false ;
45- }
46- return $ request ->isUserAgent ([
47- Request::USER_AGENT_IE ,
48- Request::USER_AGENT_MS_EDGE ,
49- Request::USER_AGENT_CHROME ,
50- Request::USER_AGENT_FIREFOX ,
51- Request::USER_AGENT_SAFARI ,
52- ]);
53- }
54-
55- /**
56- * @param \Throwable $ex
57- */
5840 public function logException (\Throwable $ ex ): void {
5941 if ($ ex instanceof Exception) {
6042 $ httpCode = $ ex ->getHTTPCode ();
@@ -65,7 +47,7 @@ public function logException(\Throwable $ex): void {
6547 }
6648 $ this ->server ->httpResponse ->addHeaders ($ headers );
6749 $ this ->server ->httpResponse ->setStatus ($ httpCode );
68- $ body = $ this ->generateBody ($ httpCode );
50+ $ body = $ this ->generateBody ($ ex , $ httpCode );
6951 $ this ->server ->httpResponse ->setBody ($ body );
7052 $ csp = new ContentSecurityPolicy ();
7153 $ this ->server ->httpResponse ->addHeader ('Content-Security-Policy ' , $ csp ->buildPolicy ());
@@ -76,18 +58,32 @@ public function logException(\Throwable $ex): void {
7658 * @codeCoverageIgnore
7759 * @return bool|string
7860 */
79- public function generateBody (int $ httpCode ) {
80- $ request = \OC ::$ server ->getRequest ();
81-
82- $ templateName = 'exception ' ;
83- if ($ httpCode === 403 || $ httpCode === 404 ) {
84- $ templateName = (string )$ httpCode ;
61+ public function generateBody (\Throwable $ ex , int $ httpCode ): mixed {
62+ if ($ this ->acceptHtml ()) {
63+ $ templateName = 'exception ' ;
64+ $ renderAs = 'guest ' ;
65+ if ($ httpCode === 403 || $ httpCode === 404 ) {
66+ $ templateName = (string )$ httpCode ;
67+ }
68+ } else {
69+ $ templateName = 'xml_exception ' ;
70+ $ renderAs = null ;
71+ $ this ->server ->httpResponse ->setHeader ('Content-Type ' , 'application/xml; charset=utf-8 ' );
8572 }
8673
87- $ content = new OC_Template ('core ' , $ templateName , 'guest ' );
74+ $ debug = $ this ->config ->getSystemValueBool ('debug ' , false );
75+
76+ $ content = new OC_Template ('core ' , $ templateName , $ renderAs );
8877 $ content ->assign ('title ' , $ this ->server ->httpResponse ->getStatusText ());
89- $ content ->assign ('remoteAddr ' , $ request ->getRemoteAddress ());
90- $ content ->assign ('requestID ' , $ request ->getId ());
78+ $ content ->assign ('remoteAddr ' , $ this ->request ->getRemoteAddress ());
79+ $ content ->assign ('requestID ' , $ this ->request ->getId ());
80+ $ content ->assign ('debugMode ' , $ debug );
81+ $ content ->assign ('errorClass ' , get_class ($ ex ));
82+ $ content ->assign ('errorMsg ' , $ ex ->getMessage ());
83+ $ content ->assign ('errorCode ' , $ ex ->getCode ());
84+ $ content ->assign ('file ' , $ ex ->getFile ());
85+ $ content ->assign ('line ' , $ ex ->getLine ());
86+ $ content ->assign ('exception ' , $ ex );
9187 return $ content ->fetchPage ();
9288 }
9389
@@ -98,4 +94,14 @@ public function sendResponse() {
9894 $ this ->server ->sapi ->sendResponse ($ this ->server ->httpResponse );
9995 exit ();
10096 }
97+
98+ private function acceptHtml (): bool {
99+ foreach (explode (', ' , $ this ->request ->getHeader ('Accept ' )) as $ part ) {
100+ $ subparts = explode ('; ' , $ part );
101+ if (str_ends_with ($ subparts [0 ], '/html ' )) {
102+ return true ;
103+ }
104+ }
105+ return false ;
106+ }
101107}
0 commit comments