]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Tree.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Tree.php
1 <?php
2
3 /**
4  * Abstract tree object
5  *
6  * @package Sabre
7  * @subpackage DAV
8  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9  * @author Evert Pot (http://www.rooftopsolutions.nl/)
10  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
11  */
12 abstract class Sabre_DAV_Tree {
13
14     /**
15      * This function must return an INode object for a path
16      * If a Path doesn't exist, thrown a Exception_NotFound
17      *
18      * @param string $path
19      * @throws Sabre_DAV_Exception_NotFound
20      * @return Sabre_DAV_INode
21      */
22     abstract function getNodeForPath($path);
23
24     /**
25      * This function allows you to check if a node exists.
26      *
27      * Implementors of this class should override this method to make
28      * it cheaper.
29      *
30      * @param string $path
31      * @return bool
32      */
33     public function nodeExists($path) {
34
35         try {
36
37             $this->getNodeForPath($path);
38             return true;
39
40         } catch (Sabre_DAV_Exception_NotFound $e) {
41
42             return false;
43
44         }
45
46     }
47
48     /**
49      * Copies a file from path to another
50      *
51      * @param string $sourcePath The source location
52      * @param string $destinationPath The full destination path
53      * @return void
54      */
55     public function copy($sourcePath, $destinationPath) {
56
57         $sourceNode = $this->getNodeForPath($sourcePath);
58
59         // grab the dirname and basename components
60         list($destinationDir, $destinationName) = Sabre_DAV_URLUtil::splitPath($destinationPath);
61
62         $destinationParent = $this->getNodeForPath($destinationDir);
63         $this->copyNode($sourceNode,$destinationParent,$destinationName);
64
65         $this->markDirty($destinationDir);
66
67     }
68
69     /**
70      * Moves a file from one location to another
71      *
72      * @param string $sourcePath The path to the file which should be moved
73      * @param string $destinationPath The full destination path, so not just the destination parent node
74      * @return int
75      */
76     public function move($sourcePath, $destinationPath) {
77
78         list($sourceDir, $sourceName) = Sabre_DAV_URLUtil::splitPath($sourcePath);
79         list($destinationDir, $destinationName) = Sabre_DAV_URLUtil::splitPath($destinationPath);
80
81         if ($sourceDir===$destinationDir) {
82             $renameable = $this->getNodeForPath($sourcePath);
83             $renameable->setName($destinationName);
84         } else {
85             $this->copy($sourcePath,$destinationPath);
86             $this->getNodeForPath($sourcePath)->delete();
87         }
88         $this->markDirty($sourceDir);
89         $this->markDirty($destinationDir);
90
91     }
92
93     /**
94      * Deletes a node from the tree
95      *
96      * @param string $path
97      * @return void
98      */
99     public function delete($path) {
100
101         $node = $this->getNodeForPath($path);
102         $node->delete();
103
104         list($parent) = Sabre_DAV_URLUtil::splitPath($path);
105         $this->markDirty($parent);
106
107     }
108
109     /**
110      * Returns a list of childnodes for a given path.
111      *
112      * @param string $path
113      * @return array
114      */
115     public function getChildren($path) {
116
117         $node = $this->getNodeForPath($path);
118         return $node->getChildren();
119
120     }
121
122     /**
123      * This method is called with every tree update
124      *
125      * Examples of tree updates are:
126      *   * node deletions
127      *   * node creations
128      *   * copy
129      *   * move
130      *   * renaming nodes
131      *
132      * If Tree classes implement a form of caching, this will allow
133      * them to make sure caches will be expired.
134      *
135      * If a path is passed, it is assumed that the entire subtree is dirty
136      *
137      * @param string $path
138      * @return void
139      */
140     public function markDirty($path) {
141
142
143     }
144
145     /**
146      * copyNode
147      *
148      * @param Sabre_DAV_INode $source
149      * @param Sabre_DAV_ICollection $destinationParent
150      * @param string $destinationName
151      * @return void
152      */
153     protected function copyNode(Sabre_DAV_INode $source,Sabre_DAV_ICollection $destinationParent,$destinationName = null) {
154
155         if (!$destinationName) $destinationName = $source->getName();
156
157         if ($source instanceof Sabre_DAV_IFile) {
158
159             $data = $source->get();
160
161             // If the body was a string, we need to convert it to a stream
162             if (is_string($data)) {
163                 $stream = fopen('php://temp','r+');
164                 fwrite($stream,$data);
165                 rewind($stream);
166                 $data = $stream;
167             }
168             $destinationParent->createFile($destinationName,$data);
169             $destination = $destinationParent->getChild($destinationName);
170
171         } elseif ($source instanceof Sabre_DAV_ICollection) {
172
173             $destinationParent->createDirectory($destinationName);
174
175             $destination = $destinationParent->getChild($destinationName);
176             foreach($source->getChildren() as $child) {
177
178                 $this->copyNode($child,$destination);
179
180             }
181
182         }
183         if ($source instanceof Sabre_DAV_IProperties && $destination instanceof Sabre_DAV_IProperties) {
184
185             $props = $source->getProperties(array());
186             $destination->updateProperties($props);
187
188         }
189
190     }
191
192 }
193