6 * The node class implements the method used by both the File and the Directory classes
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
14 abstract class Sabre_DAV_FSExt_Node extends Sabre_DAV_FS_Node implements Sabre_DAV_IProperties {
17 * Updates properties on this node,
19 * @param array $properties
20 * @see Sabre_DAV_IProperties::updateProperties
23 public function updateProperties($properties) {
25 $resourceData = $this->getResourceData();
27 foreach($properties as $propertyName=>$propertyValue) {
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]);
35 $resourceData['properties'][$propertyName] = $propertyValue;
40 $this->putResourceData($resourceData);
45 * Returns a list of properties for this nodes.;
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
50 * @param array $properties
53 function getProperties($properties) {
55 $resourceData = $this->getResourceData();
57 // if the array was empty, we need to return everything
58 if (!$properties) return $resourceData['properties'];
61 foreach($properties as $property) {
62 if (isset($resourceData['properties'][$property])) $props[$property] = $resourceData['properties'][$property];
70 * Returns the path to the resource file
74 protected function getResourceInfoPath() {
76 list($parentDir) = Sabre_DAV_URLUtil::splitPath($this->path);
77 return $parentDir . '/.sabredav';
82 * Returns all the stored resource information
86 protected function getResourceData() {
88 $path = $this->getResourceInfoPath();
89 if (!file_exists($path)) return array('properties' => array());
91 // opening up the file, and creating a shared lock
92 $handle = fopen($path,'r');
93 flock($handle,LOCK_SH);
96 // Reading data until the eof
97 while(!feof($handle)) {
98 $data.=fread($handle,8192);
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());
110 $data = $data[$this->getName()];
111 if (!isset($data['properties'])) $data['properties'] = array();
117 * Updates the resource information
119 * @param array $newData
122 protected function putResourceData(array $newData) {
124 $path = $this->getResourceInfoPath();
126 // opening up the file, and creating a shared lock
127 $handle = fopen($path,'a+');
128 flock($handle,LOCK_EX);
133 // Reading data until the eof
134 while(!feof($handle)) {
135 $data.=fread($handle,8192);
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);
144 fwrite($handle,serialize($data));
152 * @param string $name The new name
155 public function setName($name) {
157 list($parentPath, ) = Sabre_DAV_URLUtil::splitPath($this->path);
158 list(, $newName) = Sabre_DAV_URLUtil::splitPath($name);
159 $newPath = $parentPath . '/' . $newName;
161 // We're deleting the existing resourcedata, and recreating it
163 $resourceData = $this->getResourceData();
164 $this->deleteResourceData();
166 rename($this->path,$newPath);
167 $this->path = $newPath;
168 $this->putResourceData($resourceData);
176 public function deleteResourceData() {
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;
182 // opening up the file, and creating a shared lock
183 $handle = fopen($path,'a+');
184 flock($handle,LOCK_EX);
189 // Reading data until the eof
190 while(!feof($handle)) {
191 $data.=fread($handle,8192);
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);
199 fwrite($handle,serialize($data));
205 public function delete() {
207 return $this->deleteResourceData();