1<?php
2
3/**
4 * DokuWiki WebDAV Plugin: Plugin for SabreDAV
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 Sabre\DAV\Inode;
14use Sabre\DAV\PropFind;
15use Sabre\DAV\Server;
16use Sabre\DAV\ServerPlugin;
17
18class DokuWiki extends ServerPlugin
19{
20    const NS_DOKUWIKI = 'http://dokuwiki.org/ns';
21
22    const DAV_ID_PROPERTY           = '{DAV:}id';
23    const DAV_DISPLAYNAME_PROPERTY  = '{DAV:}displayname';
24    const DAV_ISHIDDEN_PROPERTY     = '{DAV:}ishidden';
25    const DAV_ISFOLDER_PROPERTY     = '{DAV:}isfolder';
26    const DAV_ISCOLLECTION_PROPERTY = '{DAV:}iscollection';
27    const DAV_CREATIONDATE_PROPERTY = '{DAV:}creationdate';
28
29    const DW_DESCRIPTION_PROPERTY = '{http://dokuwiki.org/ns}description';
30    const DW_TITLE_PROPERTY       = '{http://dokuwiki.org/ns}title';
31    const DW_TAGS_PROPERTY        = '{http://dokuwiki.org/ns}tags';
32    const DW_ID_PROPERTY          = '{http://dokuwiki.org/ns}id';
33
34    /**
35     * Initializes the plugin
36     *
37     * @param DAV\Server $server
38     * @return void
39     */
40    public function initialize(Server $server)
41    {
42        $server->xml->namespaceMap[self::NS_DOKUWIKI] = 'dw';
43        $server->on('propFind', [$this, 'propFind']);
44    }
45
46    /**
47     * Our PROPFIND handler
48     *
49     * @param PropFind $propFind
50     * @param INode $node
51     * @return void
52     */
53    public function propFind(PropFind $propFind, INode $node)
54    {
55        if (!isset($node->info)) {
56            return;
57        }
58
59        $info = $node->info;
60
61        $properties = [];
62
63        if ($info['type'] == 'd') {
64            $properties[self::DAV_ISFOLDER_PROPERTY]     = 't';
65            $properties[self::DAV_ISCOLLECTION_PROPERTY] = '1';
66        }
67
68        if ($info['type'] == 'f') {
69            $dw_id = $info['id'];
70
71            $properties[self::DAV_ID_PROPERTY] = $dw_id;
72            $properties[self::DW_ID_PROPERTY]  = $dw_id;
73
74            if ($info['dir'] == 'datadir') {
75                $dw_meta = p_get_metadata($dw_id);
76
77                $properties[self::DAV_DISPLAYNAME_PROPERTY]  = @$dw_meta['title'];
78                $properties[self::DAV_ISHIDDEN_PROPERTY]     = (isHiddenPage($dw_id) ? '1' : '0');
79                $properties[self::DAV_CREATIONDATE_PROPERTY] = date(DATE_ISO8601, @$dw_meta['date']['created']);
80                $properties[self::DW_TITLE_PROPERTY]         = @$dw_meta['title'];
81                $properties[self::DW_DESCRIPTION_PROPERTY]   = @$dw_meta['description']['abstract'];
82
83                if ($tag_helper = plugin_load('helper', 'tag')) {
84                    $properties[self::DW_TAGS_PROPERTY] = join(',', $tag_helper->_getSubjectMetadata($dw_id));
85                }
86            }
87        }
88
89        foreach ($properties as $propname => $propvalue) {
90            $propFind->handle($propname, $propvalue);
91        }
92    }
93
94    /**
95     * Returns a plugin name.
96     *
97     * Using this name other plugins will be able to access other plugins
98     * using Sabre\DAV\Server::getPlugin
99     *
100     * @return string
101     */
102    public function getPluginName()
103    {
104        return 'dokuwiki';
105    }
106
107    /**
108     * Returns a bunch of meta-data about the plugin.
109     *
110     * Providing this information is optional, and is mainly displayed by the
111     * Browser plugin.
112     *
113     * The description key in the returned array may contain html and will not
114     * be sanitized.
115     *
116     * @return array
117     */
118    public function getPluginInfo()
119    {
120        return [
121            'name'        => $this->getPluginName(),
122            'description' => 'WebDAV plugin helper for DokuWiki',
123            'link'        => 'https://dokuwiki.org/plugin:webdav',
124        ];
125    }
126}
127