1<?php
2
3namespace Sabre\DAV\FS;
4
5use Sabre\DAV;
6
7/**
8 * Directory class
9 *
10 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
11 * @author Evert Pot (http://evertpot.com/)
12 * @license http://sabre.io/license/ Modified BSD License
13 */
14class Directory extends Node implements DAV\ICollection, DAV\IQuota {
15
16    /**
17     * Creates a new file in the directory
18     *
19     * Data will either be supplied as a stream resource, or in certain cases
20     * as a string. Keep in mind that you may have to support either.
21     *
22     * After successful creation of the file, you may choose to return the ETag
23     * of the new file here.
24     *
25     * The returned ETag must be surrounded by double-quotes (The quotes should
26     * be part of the actual string).
27     *
28     * If you cannot accurately determine the ETag, you should not return it.
29     * If you don't store the file exactly as-is (you're transforming it
30     * somehow) you should also not return an ETag.
31     *
32     * This means that if a subsequent GET to this new file does not exactly
33     * return the same contents of what was submitted here, you are strongly
34     * recommended to omit the ETag.
35     *
36     * @param string $name Name of the file
37     * @param resource|string $data Initial payload
38     * @return null|string
39     */
40    function createFile($name, $data = null) {
41
42        $newPath = $this->path . '/' . $name;
43        file_put_contents($newPath, $data);
44        clearstatcache(true, $newPath);
45
46    }
47
48    /**
49     * Creates a new subdirectory
50     *
51     * @param string $name
52     * @return void
53     */
54    function createDirectory($name) {
55
56        $newPath = $this->path . '/' . $name;
57        mkdir($newPath);
58        clearstatcache(true, $newPath);
59
60    }
61
62    /**
63     * Returns a specific child node, referenced by its name
64     *
65     * This method must throw DAV\Exception\NotFound if the node does not
66     * exist.
67     *
68     * @param string $name
69     * @throws DAV\Exception\NotFound
70     * @return DAV\INode
71     */
72    function getChild($name) {
73
74        $path = $this->path . '/' . $name;
75
76        if (!file_exists($path)) throw new DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
77
78        if (is_dir($path)) {
79
80            return new self($path);
81
82        } else {
83
84            return new File($path);
85
86        }
87
88    }
89
90    /**
91     * Returns an array with all the child nodes
92     *
93     * @return DAV\INode[]
94     */
95    function getChildren() {
96
97        $nodes = [];
98        $iterator = new \FilesystemIterator(
99            $this->path,
100            \FilesystemIterator::CURRENT_AS_SELF
101          | \FilesystemIterator::SKIP_DOTS
102        );
103        foreach ($iterator as $entry) {
104
105            $nodes[] = $this->getChild($entry->getFilename());
106
107        }
108        return $nodes;
109
110    }
111
112    /**
113     * Checks if a child exists.
114     *
115     * @param string $name
116     * @return bool
117     */
118    function childExists($name) {
119
120        $path = $this->path . '/' . $name;
121        return file_exists($path);
122
123    }
124
125    /**
126     * Deletes all files in this directory, and then itself
127     *
128     * @return void
129     */
130    function delete() {
131
132        foreach ($this->getChildren() as $child) $child->delete();
133        rmdir($this->path);
134
135    }
136
137    /**
138     * Returns available diskspace information
139     *
140     * @return array
141     */
142    function getQuotaInfo() {
143
144        return [
145            disk_total_space($this->path) - disk_free_space($this->path),
146            disk_free_space($this->path)
147        ];
148
149    }
150
151}
152