]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Locks/Backend/File.php
removed community home addon
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Locks / Backend / File.php
1 <?php
2
3 /**
4  * The Lock manager allows you to handle all file-locks centrally.
5  *
6  * This Lock Manager stores all its data in a single file.
7  *
8  * Note that this is not nearly as robust as a database, you are encouraged
9  * to use the PDO backend instead.
10  *
11  * @package Sabre
12  * @subpackage DAV
13  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
14  * @author Evert Pot (http://www.rooftopsolutions.nl/)
15  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
16  */
17 class Sabre_DAV_Locks_Backend_File extends Sabre_DAV_Locks_Backend_Abstract {
18
19     /**
20      * The storage file
21      *
22      * @var string
23      */
24     private $locksFile;
25
26     /**
27      * Constructor
28      *
29      * @param string $locksFile path to file
30      */
31     public function __construct($locksFile) {
32
33         $this->locksFile = $locksFile;
34
35     }
36
37     /**
38      * Returns a list of Sabre_DAV_Locks_LockInfo objects
39      *
40      * This method should return all the locks for a particular uri, including
41      * locks that might be set on a parent uri.
42      *
43      * If returnChildLocks is set to true, this method should also look for
44      * any locks in the subtree of the uri for locks.
45      *
46      * @param string $uri
47      * @param bool $returnChildLocks
48      * @return array
49      */
50     public function getLocks($uri, $returnChildLocks) {
51
52         $newLocks = array();
53
54         $locks = $this->getData();
55
56         foreach($locks as $lock) {
57
58             if ($lock->uri === $uri ||
59                 //deep locks on parents
60                 ($lock->depth!=0 && strpos($uri, $lock->uri . '/')===0) ||
61
62                 // locks on children
63                 ($returnChildLocks && (strpos($lock->uri, $uri . '/')===0)) ) {
64
65                 $newLocks[] = $lock;
66
67             }
68
69         }
70
71         // Checking if we can remove any of these locks
72         foreach($newLocks as $k=>$lock) {
73             if (time() > $lock->timeout + $lock->created) unset($newLocks[$k]);
74         }
75         return $newLocks;
76
77     }
78
79     /**
80      * Locks a uri
81      *
82      * @param string $uri
83      * @param Sabre_DAV_Locks_LockInfo $lockInfo
84      * @return bool
85      */
86     public function lock($uri, Sabre_DAV_Locks_LockInfo $lockInfo) {
87
88         // We're making the lock timeout 30 minutes
89         $lockInfo->timeout = 1800;
90         $lockInfo->created = time();
91         $lockInfo->uri = $uri;
92
93         $locks = $this->getData();
94
95         foreach($locks as $k=>$lock) {
96             if (
97                 ($lock->token == $lockInfo->token) ||
98                 (time() > $lock->timeout + $lock->created)
99             ) {
100                 unset($locks[$k]);
101             }
102         }
103         $locks[] = $lockInfo;
104         $this->putData($locks);
105         return true;
106
107     }
108
109     /**
110      * Removes a lock from a uri
111      *
112      * @param string $uri
113      * @param Sabre_DAV_Locks_LockInfo $lockInfo
114      * @return bool
115      */
116     public function unlock($uri, Sabre_DAV_Locks_LockInfo $lockInfo) {
117
118         $locks = $this->getData();
119         foreach($locks as $k=>$lock) {
120
121             if ($lock->token == $lockInfo->token) {
122
123                 unset($locks[$k]);
124                 $this->putData($locks);
125                 return true;
126
127             }
128         }
129         return false;
130
131     }
132
133     /**
134      * Loads the lockdata from the filesystem.
135      *
136      * @return array
137      */
138     protected function getData() {
139
140         if (!file_exists($this->locksFile)) return array();
141
142         // opening up the file, and creating a shared lock
143         $handle = fopen($this->locksFile,'r');
144         flock($handle,LOCK_SH);
145
146         // Reading data until the eof
147         $data = stream_get_contents($handle);
148
149         // We're all good
150         fclose($handle);
151
152         // Unserializing and checking if the resource file contains data for this file
153         $data = unserialize($data);
154         if (!$data) return array();
155         return $data;
156
157     }
158
159     /**
160      * Saves the lockdata
161      *
162      * @param array $newData
163      * @return void
164      */
165     protected function putData(array $newData) {
166
167         // opening up the file, and creating an exclusive lock
168         $handle = fopen($this->locksFile,'a+');
169         flock($handle,LOCK_EX);
170
171         // We can only truncate and rewind once the lock is acquired.
172         ftruncate($handle,0);
173         rewind($handle);
174
175         fwrite($handle,serialize($newData));
176         fclose($handle);
177
178     }
179
180 }
181