1<?php
2
3/**
4 * DokuWiki WebDAV Plugin - Pages Directory Type
5 *
6 * @link     https://dokuwiki.org/plugin:webdav
7 * @author   Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
8 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 */
10
11namespace dokuwiki\plugin\webdav\core\DAV\Collection\Pages;
12
13use dokuwiki\plugin\webdav;
14use dokuwiki\plugin\webdav\core\Utils;
15use Sabre\DAV\Exception\Forbidden;
16use Sabre\DAV\Exception\NotFound;
17
18class Directory extends webdav\core\DAV\AbstractDirectory
19{
20    const ROOT      = 'pages';
21    const DIRECTORY = 'datadir';
22
23    public function createDirectory($name)
24    {
25        global $conf;
26
27        Utils::log('debug', "Create directory $name");
28
29        if (auth_quickaclcheck($this->ns . ':*') < AUTH_CREATE) {
30            throw new Forbidden('Insufficient Permissions');
31        }
32
33        // no dir hierarchies
34        $name = strtr($name, [
35            ':' => $conf['sepchar'],
36            '/' => $conf['sepchar'],
37            ';' => $conf['sepchar'],
38        ]);
39
40        $name = cleanID($this->info['ns'] . ':' . $name . ':fake'); //add fake pageid
41
42        io_createNamespace($name, 'pages');
43    }
44
45    public function delete()
46    {
47        $dir = dirname(wikiFN($this->info['ns'] . ':fake'));
48
49        Utils::log('debug', "Delete directory");
50
51        if (@!file_exists($dir)) {
52            throw new NotFound('Directory does not exist');
53        }
54
55        $files = glob("$dir/*");
56
57        if (count($files)) {
58            throw new Forbidden('Directory not empty');
59        }
60
61        if (!rmdir($dir)) {
62            throw new Forbidden('Failed to delete directory');
63        }
64    }
65
66    public function createFile($name, $data = null)
67    {
68        $id = $this->info['ns'] . ':' . preg_replace('#\.txt$#', '', cleanID($name));
69        Utils::log('debug', "Create page $id - $name");
70
71        Utils::saveWikiText($id, $data, 'create');
72    }
73}
74