2424 */
2525namespace OCA \DAV \Files ;
2626
27- use OC \AppFramework \Http \Request ;
2827use OC_Template ;
2928use OCP \AppFramework \Http \ContentSecurityPolicy ;
29+ use OCP \IConfig ;
3030use OCP \IRequest ;
3131use Sabre \DAV \Exception ;
3232use Sabre \DAV \Server ;
3333use Sabre \DAV \ServerPlugin ;
3434
35- class BrowserErrorPagePlugin extends ServerPlugin {
36- /** @var Server */
37- private $ server ;
35+ class ErrorPagePlugin extends ServerPlugin {
36+ private ?Server $ server = null ;
37+
38+ public function __construct (
39+ private IRequest $ request ,
40+ private IConfig $ config ,
41+ ) {
42+ }
3843
3944 /**
4045 * This initializes the plugin.
@@ -43,35 +48,12 @@ class BrowserErrorPagePlugin extends ServerPlugin {
4348 * addPlugin is called.
4449 *
4550 * This method should set up the required event subscriptions.
46- *
47- * @param Server $server
48- * @return void
4951 */
50- public function initialize (Server $ server ) {
52+ public function initialize (Server $ server ): void {
5153 $ this ->server = $ server ;
5254 $ server ->on ('exception ' , [$ this , 'logException ' ], 1000 );
5355 }
5456
55- /**
56- * @param IRequest $request
57- * @return bool
58- */
59- public static function isBrowserRequest (IRequest $ request ) {
60- if ($ request ->getMethod () !== 'GET ' ) {
61- return false ;
62- }
63- return $ request ->isUserAgent ([
64- Request::USER_AGENT_IE ,
65- Request::USER_AGENT_MS_EDGE ,
66- Request::USER_AGENT_CHROME ,
67- Request::USER_AGENT_FIREFOX ,
68- Request::USER_AGENT_SAFARI ,
69- ]);
70- }
71-
72- /**
73- * @param \Throwable $ex
74- */
7557 public function logException (\Throwable $ ex ): void {
7658 if ($ ex instanceof Exception) {
7759 $ httpCode = $ ex ->getHTTPCode ();
@@ -82,7 +64,7 @@ public function logException(\Throwable $ex): void {
8264 }
8365 $ this ->server ->httpResponse ->addHeaders ($ headers );
8466 $ this ->server ->httpResponse ->setStatus ($ httpCode );
85- $ body = $ this ->generateBody ($ httpCode );
67+ $ body = $ this ->generateBody ($ ex , $ httpCode );
8668 $ this ->server ->httpResponse ->setBody ($ body );
8769 $ csp = new ContentSecurityPolicy ();
8870 $ this ->server ->httpResponse ->addHeader ('Content-Security-Policy ' , $ csp ->buildPolicy ());
@@ -93,18 +75,32 @@ public function logException(\Throwable $ex): void {
9375 * @codeCoverageIgnore
9476 * @return bool|string
9577 */
96- public function generateBody (int $ httpCode ) {
97- $ request = \OC ::$ server ->getRequest ();
98-
99- $ templateName = 'exception ' ;
100- if ($ httpCode === 403 || $ httpCode === 404 ) {
101- $ templateName = (string )$ httpCode ;
78+ public function generateBody (\Throwable $ ex , int $ httpCode ): mixed {
79+ if ($ this ->acceptHtml ()) {
80+ $ templateName = 'exception ' ;
81+ $ renderAs = 'guest ' ;
82+ if ($ httpCode === 403 || $ httpCode === 404 ) {
83+ $ templateName = (string )$ httpCode ;
84+ }
85+ } else {
86+ $ templateName = 'xml_exception ' ;
87+ $ renderAs = null ;
88+ $ this ->server ->httpResponse ->setHeader ('Content-Type ' , 'application/xml; charset=utf-8 ' );
10289 }
10390
104- $ content = new OC_Template ('core ' , $ templateName , 'guest ' );
91+ $ debug = $ this ->config ->getSystemValueBool ('debug ' , false );
92+
93+ $ content = new OC_Template ('core ' , $ templateName , $ renderAs );
10594 $ content ->assign ('title ' , $ this ->server ->httpResponse ->getStatusText ());
106- $ content ->assign ('remoteAddr ' , $ request ->getRemoteAddress ());
107- $ content ->assign ('requestID ' , $ request ->getId ());
95+ $ content ->assign ('remoteAddr ' , $ this ->request ->getRemoteAddress ());
96+ $ content ->assign ('requestID ' , $ this ->request ->getId ());
97+ $ content ->assign ('debugMode ' , $ debug );
98+ $ content ->assign ('errorClass ' , get_class ($ ex ));
99+ $ content ->assign ('errorMsg ' , $ ex ->getMessage ());
100+ $ content ->assign ('errorCode ' , $ ex ->getCode ());
101+ $ content ->assign ('file ' , $ ex ->getFile ());
102+ $ content ->assign ('line ' , $ ex ->getLine ());
103+ $ content ->assign ('exception ' , $ ex );
108104 return $ content ->fetchPage ();
109105 }
110106
@@ -115,4 +111,14 @@ public function sendResponse() {
115111 $ this ->server ->sapi ->sendResponse ($ this ->server ->httpResponse );
116112 exit ();
117113 }
114+
115+ private function acceptHtml (): bool {
116+ foreach (explode (', ' , $ this ->request ->getHeader ('Accept ' )) as $ part ) {
117+ $ subparts = explode ('; ' , $ part );
118+ if (str_ends_with ($ subparts [0 ], '/html ' )) {
119+ return true ;
120+ }
121+ }
122+ return false ;
123+ }
118124}
0 commit comments