4 * The Lock manager allows you to handle all file-locks centrally.
6 * This Lock Manager stores all its data in a single file.
8 * Note that this is not nearly as robust as a database, you are encouraged
9 * to use the PDO backend instead.
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
17 class Sabre_DAV_Locks_Backend_File extends Sabre_DAV_Locks_Backend_Abstract {
29 * @param string $locksFile path to file
31 public function __construct($locksFile) {
33 $this->locksFile = $locksFile;
38 * Returns a list of Sabre_DAV_Locks_LockInfo objects
40 * This method should return all the locks for a particular uri, including
41 * locks that might be set on a parent uri.
43 * If returnChildLocks is set to true, this method should also look for
44 * any locks in the subtree of the uri for locks.
47 * @param bool $returnChildLocks
50 public function getLocks($uri, $returnChildLocks) {
54 $locks = $this->getData();
56 foreach($locks as $lock) {
58 if ($lock->uri === $uri ||
59 //deep locks on parents
60 ($lock->depth!=0 && strpos($uri, $lock->uri . '/')===0) ||
63 ($returnChildLocks && (strpos($lock->uri, $uri . '/')===0)) ) {
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]);
83 * @param Sabre_DAV_Locks_LockInfo $lockInfo
86 public function lock($uri, Sabre_DAV_Locks_LockInfo $lockInfo) {
88 // We're making the lock timeout 30 minutes
89 $lockInfo->timeout = 1800;
90 $lockInfo->created = time();
91 $lockInfo->uri = $uri;
93 $locks = $this->getData();
95 foreach($locks as $k=>$lock) {
97 ($lock->token == $lockInfo->token) ||
98 (time() > $lock->timeout + $lock->created)
103 $locks[] = $lockInfo;
104 $this->putData($locks);
110 * Removes a lock from a uri
113 * @param Sabre_DAV_Locks_LockInfo $lockInfo
116 public function unlock($uri, Sabre_DAV_Locks_LockInfo $lockInfo) {
118 $locks = $this->getData();
119 foreach($locks as $k=>$lock) {
121 if ($lock->token == $lockInfo->token) {
124 $this->putData($locks);
134 * Loads the lockdata from the filesystem.
138 protected function getData() {
140 if (!file_exists($this->locksFile)) return array();
142 // opening up the file, and creating a shared lock
143 $handle = fopen($this->locksFile,'r');
144 flock($handle,LOCK_SH);
146 // Reading data until the eof
147 $data = stream_get_contents($handle);
152 // Unserializing and checking if the resource file contains data for this file
153 $data = unserialize($data);
154 if (!$data) return array();
162 * @param array $newData
165 protected function putData(array $newData) {
167 // opening up the file, and creating an exclusive lock
168 $handle = fopen($this->locksFile,'a+');
169 flock($handle,LOCK_EX);
171 // We can only truncate and rewind once the lock is acquired.
172 ftruncate($handle,0);
175 fwrite($handle,serialize($newData));