]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Locks/Backend/FS.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Locks / Backend / FS.php
1 <?php
2
3 /**
4  * This Lock Backend stores all its data in the filesystem in separate file per
5  * node.
6  *
7  * This Lock Manager is now deprecated. It has a bug that allows parent
8  * collections to be deletes when children deeper in the tree are locked.
9  *
10  * This also means that using this backend means you will not pass the Neon
11  * Litmus test.
12  *
13  * You are recommended to use either the PDO or the File backend instead.
14  *
15  * @package Sabre
16  * @subpackage DAV
17  * @deprecated
18  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
19  * @author Evert Pot (http://www.rooftopsolutions.nl/)
20  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
21  */
22 class Sabre_DAV_Locks_Backend_FS extends Sabre_DAV_Locks_Backend_Abstract {
23
24     /**
25      * The default data directory
26      *
27      * @var string
28      */
29     private $dataDir;
30
31     public function __construct($dataDir) {
32
33         $this->dataDir = $dataDir;
34
35     }
36
37     protected function getFileNameForUri($uri) {
38
39         return $this->dataDir . '/sabredav_' . md5($uri) . '.locks';
40
41     }
42
43
44     /**
45      * Returns a list of Sabre_DAV_Locks_LockInfo objects
46      *
47      * This method should return all the locks for a particular uri, including
48      * locks that might be set on a parent uri.
49      *
50      * If returnChildLocks is set to true, this method should also look for
51      * any locks in the subtree of the uri for locks.
52      *
53      * @param string $uri
54      * @param bool $returnChildLocks
55      * @return array
56      */
57     public function getLocks($uri, $returnChildLocks) {
58
59         $lockList = array();
60         $currentPath = '';
61
62         foreach(explode('/',$uri) as $uriPart) {
63
64             // weird algorithm that can probably be improved, but we're traversing the path top down
65             if ($currentPath) $currentPath.='/';
66             $currentPath.=$uriPart;
67
68             $uriLocks = $this->getData($currentPath);
69
70             foreach($uriLocks as $uriLock) {
71
72                 // Unless we're on the leaf of the uri-tree we should ignore locks with depth 0
73                 if($uri==$currentPath || $uriLock->depth!=0) {
74                     $uriLock->uri = $currentPath;
75                     $lockList[] = $uriLock;
76                 }
77
78             }
79
80         }
81
82         // Checking if we can remove any of these locks
83         foreach($lockList as $k=>$lock) {
84             if (time() > $lock->timeout + $lock->created) unset($lockList[$k]);
85         }
86         return $lockList;
87
88     }
89
90     /**
91      * Locks a uri
92      *
93      * @param string $uri
94      * @param Sabre_DAV_Locks_LockInfo $lockInfo
95      * @return bool
96      */
97     public function lock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
98
99         // We're making the lock timeout 30 minutes
100         $lockInfo->timeout = 1800;
101         $lockInfo->created = time();
102
103         $locks = $this->getLocks($uri,false);
104         foreach($locks as $k=>$lock) {
105             if ($lock->token == $lockInfo->token) unset($locks[$k]);
106         }
107         $locks[] = $lockInfo;
108         $this->putData($uri,$locks);
109         return true;
110
111     }
112
113     /**
114      * Removes a lock from a uri
115      *
116      * @param string $uri
117      * @param Sabre_DAV_Locks_LockInfo $lockInfo
118      * @return bool
119      */
120     public function unlock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
121
122         $locks = $this->getLocks($uri,false);
123         foreach($locks as $k=>$lock) {
124
125             if ($lock->token == $lockInfo->token) {
126
127                 unset($locks[$k]);
128                 $this->putData($uri,$locks);
129                 return true;
130
131             }
132         }
133         return false;
134
135     }
136
137     /**
138      * Returns the stored data for a uri
139      *
140      * @param string $uri
141      * @return array
142      */
143     protected function getData($uri) {
144
145         $path = $this->getFilenameForUri($uri);
146         if (!file_exists($path)) return array();
147
148         // opening up the file, and creating a shared lock
149         $handle = fopen($path,'r');
150         flock($handle,LOCK_SH);
151         $data = '';
152
153         // Reading data until the eof
154         while(!feof($handle)) {
155             $data.=fread($handle,8192);
156         }
157
158         // We're all good
159         fclose($handle);
160
161         // Unserializing and checking if the resource file contains data for this file
162         $data = unserialize($data);
163         if (!$data) return array();
164         return $data;
165
166     }
167
168     /**
169      * Updates the lock information
170      *
171      * @param string $uri
172      * @param array $newData
173      * @return void
174      */
175     protected function putData($uri,array $newData) {
176
177         $path = $this->getFileNameForUri($uri);
178
179         // opening up the file, and creating a shared lock
180         $handle = fopen($path,'a+');
181         flock($handle,LOCK_EX);
182         ftruncate($handle,0);
183         rewind($handle);
184
185         fwrite($handle,serialize($newData));
186         fclose($handle);
187
188     }
189
190 }
191