2424 */
2525namespace OCA \DAV \Files ;
2626
27+ use OC \AppFramework \Http \Request ;
2728use OC_Template ;
2829use 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 ErrorPagePlugin extends ServerPlugin {
36- private ?Server $ server = null ;
37-
38- public function __construct (
39- private IRequest $ request ,
40- private IConfig $ config ,
41- ) {
42- }
35+ class BrowserErrorPagePlugin extends ServerPlugin {
36+ /** @var Server */
37+ private $ server ;
4338
4439 /**
4540 * This initializes the plugin.
@@ -48,12 +43,35 @@ public function __construct(
4843 * addPlugin is called.
4944 *
5045 * This method should set up the required event subscriptions.
46+ *
47+ * @param Server $server
48+ * @return void
5149 */
52- public function initialize (Server $ server ): void {
50+ public function initialize (Server $ server ) {
5351 $ this ->server = $ server ;
5452 $ server ->on ('exception ' , [$ this , 'logException ' ], 1000 );
5553 }
5654
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+ */
5775 public function logException (\Throwable $ ex ): void {
5876 if ($ ex instanceof Exception) {
5977 $ httpCode = $ ex ->getHTTPCode ();
@@ -64,7 +82,7 @@ public function logException(\Throwable $ex): void {
6482 }
6583 $ this ->server ->httpResponse ->addHeaders ($ headers );
6684 $ this ->server ->httpResponse ->setStatus ($ httpCode );
67- $ body = $ this ->generateBody ($ ex , $ httpCode );
85+ $ body = $ this ->generateBody ($ httpCode );
6886 $ this ->server ->httpResponse ->setBody ($ body );
6987 $ csp = new ContentSecurityPolicy ();
7088 $ this ->server ->httpResponse ->addHeader ('Content-Security-Policy ' , $ csp ->buildPolicy ());
@@ -75,32 +93,18 @@ public function logException(\Throwable $ex): void {
7593 * @codeCoverageIgnore
7694 * @return bool|string
7795 */
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 ' );
89- }
96+ public function generateBody (int $ httpCode ) {
97+ $ request = \OC ::$ server ->getRequest ();
9098
91- $ debug = $ this ->config ->getSystemValueBool ('debug ' , false );
99+ $ templateName = 'exception ' ;
100+ if ($ httpCode === 403 || $ httpCode === 404 ) {
101+ $ templateName = (string )$ httpCode ;
102+ }
92103
93- $ content = new OC_Template ('core ' , $ templateName , $ renderAs );
104+ $ content = new OC_Template ('core ' , $ templateName , ' guest ' );
94105 $ content ->assign ('title ' , $ this ->server ->httpResponse ->getStatusText ());
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 );
106+ $ content ->assign ('remoteAddr ' , $ request ->getRemoteAddress ());
107+ $ content ->assign ('requestID ' , $ request ->getId ());
104108 return $ content ->fetchPage ();
105109 }
106110
@@ -109,15 +113,6 @@ public function generateBody(\Throwable $ex, int $httpCode): mixed {
109113 */
110114 public function sendResponse () {
111115 $ this ->server ->sapi ->sendResponse ($ this ->server ->httpResponse );
112- }
113-
114- private function acceptHtml (): bool {
115- foreach (explode (', ' , $ this ->request ->getHeader ('Accept ' )) as $ part ) {
116- $ subparts = explode ('; ' , $ part );
117- if (str_ends_with ($ subparts [0 ], '/html ' )) {
118- return true ;
119- }
120- }
121- return false ;
116+ exit ();
122117 }
123118}
0 commit comments