2016-03-24 16:29:55 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-03-24 16:29:55 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-03-24 16:29:55 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Files;
|
|
|
|
|
|
|
|
use OC\AppFramework\Http\Request;
|
|
|
|
use OC_Template;
|
2020-03-20 01:44:14 +03:00
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
2016-03-24 16:29:55 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use Sabre\DAV\Exception;
|
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
|
|
|
|
class BrowserErrorPagePlugin extends ServerPlugin {
|
|
|
|
/** @var Server */
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This initializes the plugin.
|
|
|
|
*
|
|
|
|
* This function is called by Sabre\DAV\Server, after
|
|
|
|
* addPlugin is called.
|
|
|
|
*
|
|
|
|
* This method should set up the required event subscriptions.
|
|
|
|
*
|
|
|
|
* @param Server $server
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function initialize(Server $server) {
|
|
|
|
$this->server = $server;
|
2020-03-26 11:30:18 +03:00
|
|
|
$server->on('exception', [$this, 'logException'], 1000);
|
2016-03-24 16:29:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IRequest $request
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isBrowserRequest(IRequest $request) {
|
|
|
|
if ($request->getMethod() !== 'GET') {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-24 16:59:47 +03:00
|
|
|
return $request->isUserAgent([
|
|
|
|
Request::USER_AGENT_IE,
|
|
|
|
Request::USER_AGENT_MS_EDGE,
|
|
|
|
Request::USER_AGENT_CHROME,
|
|
|
|
Request::USER_AGENT_FIREFOX,
|
|
|
|
Request::USER_AGENT_SAFARI,
|
|
|
|
]);
|
2016-03-24 16:29:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Exception $ex
|
|
|
|
*/
|
|
|
|
public function logException(\Exception $ex) {
|
|
|
|
if ($ex instanceof Exception) {
|
|
|
|
$httpCode = $ex->getHTTPCode();
|
|
|
|
$headers = $ex->getHTTPHeaders($this->server);
|
|
|
|
} else {
|
|
|
|
$httpCode = 500;
|
|
|
|
$headers = [];
|
|
|
|
}
|
|
|
|
$this->server->httpResponse->addHeaders($headers);
|
|
|
|
$this->server->httpResponse->setStatus($httpCode);
|
2020-03-20 13:21:54 +03:00
|
|
|
$body = $this->generateBody($httpCode);
|
2016-03-24 16:29:55 +03:00
|
|
|
$this->server->httpResponse->setBody($body);
|
2020-03-20 01:44:14 +03:00
|
|
|
$csp = new ContentSecurityPolicy();
|
|
|
|
$this->server->httpResponse->addHeader('Content-Security-Policy', $csp->buildPolicy());
|
2016-03-24 16:29:55 +03:00
|
|
|
$this->sendResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
2020-03-20 13:21:54 +03:00
|
|
|
public function generateBody(int $httpCode) {
|
2016-03-24 16:29:55 +03:00
|
|
|
$request = \OC::$server->getRequest();
|
2020-03-20 13:21:54 +03:00
|
|
|
|
|
|
|
$templateName = 'exception';
|
|
|
|
if($httpCode === 403 || $httpCode === 404) {
|
|
|
|
$templateName = (string)$httpCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = new OC_Template('core', $templateName, 'guest');
|
2016-03-24 16:29:55 +03:00
|
|
|
$content->assign('title', $this->server->httpResponse->getStatusText());
|
|
|
|
$content->assign('remoteAddr', $request->getRemoteAddress());
|
|
|
|
$content->assign('requestID', $request->getId());
|
|
|
|
return $content->fetchPage();
|
|
|
|
}
|
|
|
|
|
2016-09-09 14:52:34 +03:00
|
|
|
/**
|
2016-03-24 16:29:55 +03:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function sendResponse() {
|
|
|
|
$this->server->sapi->sendResponse($this->server->httpResponse);
|
2016-09-09 14:52:34 +03:00
|
|
|
exit();
|
2016-03-24 16:29:55 +03:00
|
|
|
}
|
|
|
|
}
|