1<?php
2
3/**
4 * DokuWiki WebDAV Plugin: Exception Plugin for Sabre DAV
5 *
6 * @copyright Copyright (C) 2019-2020
7 * @author Giuseppe Di Terlizzi (giuseppe.diterlizzi@gmail.com)
8 * @license GNU GPL 2
9 */
10
11namespace dokuwiki\plugin\webdav\core\Plugin;
12
13use dokuwiki\plugin\webdav\core\Utils;
14use Sabre\DAV\Server;
15use Sabre\DAV\ServerPlugin;
16
17class Exception extends ServerPlugin
18{
19    /**
20     * This initializes the plugin.
21     *
22     * This function is called by \Sabre\DAV\Server, after
23     * addPlugin is called.
24     *
25     * This method should set up the required event subscriptions.
26     *
27     * @param \Sabre\DAV\Server $server
28     * @return void
29     */
30    public function initialize(Server $server)
31    {
32        $server->on('exception', [$this, 'logException'], 10);
33    }
34
35    /**
36     * Log exception
37     *
38     * @param Exception|Error $e
39     */
40    public function logException($e)
41    {
42        if (!preg_match("/No 'Authorization: (Basic|Digest)' header found./", $e->getMessage())) {
43            Utils::log('fatal', "[{class}] {message} in {file}({line})", [
44                'class'   => get_class($e),
45                'message' => $e->getMessage(),
46                'file'    => $e->getFile(),
47                'line'    => $e->getLine(),
48            ]);
49
50            Utils::log('debug', 'User-Agent: {agent}', ['agent' => @$_SERVER['HTTP_USER_AGENT']]);
51            Utils::log('debug', 'Remote-User: {user}', ['user' => @$_SERVER['REMOTE_USER']]);
52            Utils::log('debug', 'Request-URI: {uri}', ['uri' => @$_SERVER['REQUEST_URI']]);
53            Utils::log('debug', 'Request-Method: {method}', ['method' => @$_SERVER['REQUEST_METHOD']]);
54        }
55    }
56
57    /**
58     * Returns a plugin name.
59     *
60     * Using this name other plugins will be able to access other plugins
61     * using Sabre\DAV\Server::getPlugin
62     *
63     * @return string
64     */
65    public function getPluginName()
66    {
67        return 'exception';
68    }
69
70    /**
71     * Returns a bunch of meta-data about the plugin.
72     *
73     * Providing this information is optional, and is mainly displayed by the
74     * Browser plugin.
75     *
76     * The description key in the returned array may contain html and will not
77     * be sanitized.
78     *
79     * @return array
80     */
81    public function getPluginInfo()
82    {
83        return [
84            'name'        => $this->getPluginName(),
85            'description' => 'WebDAV plugin exception for DokuWiki',
86            'link'        => 'https://dokuwiki.org/plugin:webdav',
87        ];
88    }
89}
90