1<?php
2
3/**
4 * DokuWiki WebDAV Plugin: Collection Base Class
5 *
6 * @author  Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @link    https://dokuwiki.org/plugin:webdav
9 */
10
11namespace dokuwiki\plugin\webdav\core\DAV;
12
13use Sabre\DAV\Collection;
14
15class AbstractDirectory extends Collection
16{
17    const ROOT      = null;
18    const DIRECTORY = null;
19
20    public $info = [];
21
22    /** @inheritdoc */
23    public function __construct($info = [])
24    {
25
26        if (!static::ROOT || !static::DIRECTORY) {
27            throw new \RuntimeException('Specify ROOT and DIRECTORY constant');
28        }
29
30        $this->info = $info;
31    }
32
33    /**
34     * Return collection class type
35     *
36     * @return string
37     */
38    public function getClassType()
39    {
40        return substr(get_class($this), 0, strrpos(get_class($this), '\\'));
41    }
42
43    /** @inheritdoc */
44    public function getName()
45    {
46        return (isset($this->info['dirname']) ? noNS($this->info['dirname']) : static::ROOT);
47    }
48
49    /** @inheritdoc */
50    public function getLastModified()
51    {
52        return (isset($this->info['mtime']) ? $this->info['mtime'] : null);
53    }
54
55    /** @inheritdoc */
56    public function getChildren()
57    {
58        global $conf;
59
60        $children   = [];
61        $data       = [];
62        $dir        = str_replace(':', '/', (isset($this->info['id']) ? $this->info['id'] : ':'));
63        $class_type = $this->getClassType();
64
65        search($data, $conf[static::DIRECTORY], ['dokuwiki\plugin\webdav\core\Utils', 'searchCallback'], ['dir' => static::DIRECTORY], $dir);
66
67        foreach ($data as $item) {
68            if ($item['type'] == 'd') {
69                $child_class = $class_type . '\\Directory';
70            } else {
71                $child_class = $class_type . '\\File';
72            }
73
74            $children[] = new $child_class($item);
75        }
76
77        return $children;
78    }
79}
80