]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/FSExt/Node.php
privacy_image_cache: checking if the cached file really is an image
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / FSExt / Node.php
1 <?php
2
3 /**
4  * Base node-class
5  *
6  * The node class implements the method used by both the File and the Directory classes
7  *
8  * @package Sabre
9  * @subpackage DAV
10  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
11  * @author Evert Pot (http://www.rooftopsolutions.nl/)
12  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
13  */
14 abstract class Sabre_DAV_FSExt_Node extends Sabre_DAV_FS_Node implements Sabre_DAV_IProperties {
15
16     /**
17      * Updates properties on this node,
18      *
19      * @param array $properties
20      * @see Sabre_DAV_IProperties::updateProperties
21      * @return bool|array
22      */
23     public function updateProperties($properties) {
24
25         $resourceData = $this->getResourceData();
26
27         foreach($properties as $propertyName=>$propertyValue) {
28
29             // If it was null, we need to delete the property
30             if (is_null($propertyValue)) {
31                 if (isset($resourceData['properties'][$propertyName])) {
32                     unset($resourceData['properties'][$propertyName]);
33                 }
34             } else {
35                 $resourceData['properties'][$propertyName] = $propertyValue;
36             }
37
38         }
39
40         $this->putResourceData($resourceData);
41         return true;
42     }
43
44     /**
45      * Returns a list of properties for this nodes.;
46      *
47      * The properties list is a list of propertynames the client requested, encoded as xmlnamespace#tagName, for example: http://www.example.org/namespace#author
48      * If the array is empty, all properties should be returned
49      *
50      * @param array $properties
51      * @return array
52      */
53     function getProperties($properties) {
54
55         $resourceData = $this->getResourceData();
56
57         // if the array was empty, we need to return everything
58         if (!$properties) return $resourceData['properties'];
59
60         $props = array();
61         foreach($properties as $property) {
62             if (isset($resourceData['properties'][$property])) $props[$property] = $resourceData['properties'][$property];
63         }
64
65         return $props;
66
67     }
68
69     /**
70      * Returns the path to the resource file
71      *
72      * @return string
73      */
74     protected function getResourceInfoPath() {
75
76         list($parentDir) = Sabre_DAV_URLUtil::splitPath($this->path);
77         return $parentDir . '/.sabredav';
78
79     }
80
81     /**
82      * Returns all the stored resource information
83      *
84      * @return array
85      */
86     protected function getResourceData() {
87
88         $path = $this->getResourceInfoPath();
89         if (!file_exists($path)) return array('properties' => array());
90
91         // opening up the file, and creating a shared lock
92         $handle = fopen($path,'r');
93         flock($handle,LOCK_SH);
94         $data = '';
95
96         // Reading data until the eof
97         while(!feof($handle)) {
98             $data.=fread($handle,8192);
99         }
100
101         // We're all good
102         fclose($handle);
103
104         // Unserializing and checking if the resource file contains data for this file
105         $data = unserialize($data);
106         if (!isset($data[$this->getName()])) {
107             return array('properties' => array());
108         }
109
110         $data = $data[$this->getName()];
111         if (!isset($data['properties'])) $data['properties'] = array();
112         return $data;
113
114     }
115
116     /**
117      * Updates the resource information
118      *
119      * @param array $newData
120      * @return void
121      */
122     protected function putResourceData(array $newData) {
123
124         $path = $this->getResourceInfoPath();
125
126         // opening up the file, and creating a shared lock
127         $handle = fopen($path,'a+');
128         flock($handle,LOCK_EX);
129         $data = '';
130
131         rewind($handle);
132
133         // Reading data until the eof
134         while(!feof($handle)) {
135             $data.=fread($handle,8192);
136         }
137
138         // Unserializing and checking if the resource file contains data for this file
139         $data = unserialize($data);
140         $data[$this->getName()] = $newData;
141         ftruncate($handle,0);
142         rewind($handle);
143
144         fwrite($handle,serialize($data));
145         fclose($handle);
146
147     }
148
149     /**
150      * Renames the node
151      *
152      * @param string $name The new name
153      * @return void
154      */
155     public function setName($name) {
156
157         list($parentPath, ) = Sabre_DAV_URLUtil::splitPath($this->path);
158         list(, $newName) = Sabre_DAV_URLUtil::splitPath($name);
159         $newPath = $parentPath . '/' . $newName;
160
161         // We're deleting the existing resourcedata, and recreating it
162         // for the new path.
163         $resourceData = $this->getResourceData();
164         $this->deleteResourceData();
165
166         rename($this->path,$newPath);
167         $this->path = $newPath;
168         $this->putResourceData($resourceData);
169
170
171     }
172
173     /**
174      * @return bool
175      */
176     public function deleteResourceData() {
177
178         // When we're deleting this node, we also need to delete any resource information
179         $path = $this->getResourceInfoPath();
180         if (!file_exists($path)) return true;
181
182         // opening up the file, and creating a shared lock
183         $handle = fopen($path,'a+');
184         flock($handle,LOCK_EX);
185         $data = '';
186
187         rewind($handle);
188
189         // Reading data until the eof
190         while(!feof($handle)) {
191             $data.=fread($handle,8192);
192         }
193
194         // Unserializing and checking if the resource file contains data for this file
195         $data = unserialize($data);
196         if (isset($data[$this->getName()])) unset($data[$this->getName()]);
197         ftruncate($handle,0);
198         rewind($handle);
199         fwrite($handle,serialize($data));
200         fclose($handle);
201
202         return true;
203     }
204
205     public function delete() {
206
207         return $this->deleteResourceData();
208
209     }
210
211 }
212