77 */
88namespace OCA \DAV \Files ;
99
10+ use OC \AppFramework \Http \Request ;
1011use OC_Template ;
1112use 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 ErrorPagePlugin extends ServerPlugin {
19- private ?Server $ server = null ;
20-
21- public function __construct (
22- private IRequest $ request ,
23- private IConfig $ config ,
24- ) {
25- }
18+ class BrowserErrorPagePlugin extends ServerPlugin {
19+ /** @var Server */
20+ private $ server ;
2621
2722 /**
2823 * This initializes the plugin.
@@ -31,12 +26,35 @@ public function __construct(
3126 * addPlugin is called.
3227 *
3328 * This method should set up the required event subscriptions.
29+ *
30+ * @param Server $server
31+ * @return void
3432 */
35- public function initialize (Server $ server ): void {
33+ public function initialize (Server $ server ) {
3634 $ this ->server = $ server ;
3735 $ server ->on ('exception ' , [$ this , 'logException ' ], 1000 );
3836 }
3937
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+ */
4058 public function logException (\Throwable $ ex ): void {
4159 if ($ ex instanceof Exception) {
4260 $ httpCode = $ ex ->getHTTPCode ();
@@ -47,7 +65,7 @@ public function logException(\Throwable $ex): void {
4765 }
4866 $ this ->server ->httpResponse ->addHeaders ($ headers );
4967 $ this ->server ->httpResponse ->setStatus ($ httpCode );
50- $ body = $ this ->generateBody ($ ex , $ httpCode );
68+ $ body = $ this ->generateBody ($ httpCode );
5169 $ this ->server ->httpResponse ->setBody ($ body );
5270 $ csp = new ContentSecurityPolicy ();
5371 $ this ->server ->httpResponse ->addHeader ('Content-Security-Policy ' , $ csp ->buildPolicy ());
@@ -58,32 +76,18 @@ public function logException(\Throwable $ex): void {
5876 * @codeCoverageIgnore
5977 * @return bool|string
6078 */
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 ' );
72- }
79+ public function generateBody (int $ httpCode ) {
80+ $ request = \OC ::$ server ->getRequest ();
7381
74- $ debug = $ this ->config ->getSystemValueBool ('debug ' , false );
82+ $ templateName = 'exception ' ;
83+ if ($ httpCode === 403 || $ httpCode === 404 ) {
84+ $ templateName = (string )$ httpCode ;
85+ }
7586
76- $ content = new OC_Template ('core ' , $ templateName , $ renderAs );
87+ $ content = new OC_Template ('core ' , $ templateName , ' guest ' );
7788 $ content ->assign ('title ' , $ this ->server ->httpResponse ->getStatusText ());
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 );
89+ $ content ->assign ('remoteAddr ' , $ request ->getRemoteAddress ());
90+ $ content ->assign ('requestID ' , $ request ->getId ());
8791 return $ content ->fetchPage ();
8892 }
8993
@@ -94,14 +98,4 @@ public function sendResponse() {
9498 $ this ->server ->sapi ->sendResponse ($ this ->server ->httpResponse );
9599 exit ();
96100 }
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- }
107101}
0 commit comments