1<?php
2
3/**
4 * DokuWiki WebDAV Plugin - Media 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\Media;
12
13use dokuwiki\plugin\webdav\core\DAV\AbstractDirectory;
14use Sabre\DAV\Exception\Forbidden;
15use Sabre\DAV\Exception\NotFound;
16
17class Directory extends AbstractDirectory
18{
19    const ROOT      = 'media';
20    const DIRECTORY = 'mediadir';
21
22    public function createDirectory($name)
23    {
24        global $conf;
25
26        if (auth_quickaclcheck($this->ns . ':*') < AUTH_CREATE) {
27            throw new Forbidden('Insufficient Permissions');
28        }
29
30        // no dir hierarchies
31        $sanitized_name = strtr($name, [
32            ':' => $conf['sepchar'],
33            '/' => $conf['sepchar'],
34            ';' => $conf['sepchar'],
35        ]);
36
37        $id      = cleanID($this->info['ns'] . ':' . $sanitized_name);
38        $fake_id = cleanID("$id:fake"); //add fake pageid
39
40        io_createNamespace($fake_id, 'media');
41
42        // save the original directory name
43        io_saveFile(mediametaFN($id, '.dirname'), serialize([
44            'dirname' => $name,
45        ]));
46    }
47
48    public function delete()
49    {
50        $dir = dirname(mediaFN($this->info['id'] . ':fake'));
51
52        if (@!file_exists($dir)) {
53            throw new NotFound('Directory does not exist');
54        }
55
56        $files = glob("$dir/*");
57
58        if (count($files)) {
59            throw new Forbidden('Directory not empty');
60        }
61
62        if (!rmdir($dir)) {
63            throw new Forbidden('Failed to delete directory');
64        }
65    }
66
67    public function createFile($name, $data = null)
68    {
69        $info = $this->info;
70
71        $info['id']       = $this->info['id'] . ':' . cleanID($name);
72        $info['path']     = mediaFN($info['id']);
73        $info['perm']     = auth_quickaclcheck($info['id']);
74        $info['type']     = 'f';
75        $info['filename'] = $name;
76        $info['metafile'] = mediametaFN($info['id'], '.filename');
77
78        $file = new File($info);
79        $file->put($data);
80    }
81}
82