4 * The Lock manager allows you to handle all file-locks centrally.
6 * This Lock Manager stores all its data in a database. You must pass a PDO
7 * connection object in the constructor.
11 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
12 * @author Evert Pot (http://www.rooftopsolutions.nl/)
13 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
15 class Sabre_DAV_Locks_Backend_PDO extends Sabre_DAV_Locks_Backend_Abstract {
18 * The PDO connection object
25 * The PDO tablename this backend uses.
35 * @param string $tableName
37 public function __construct(PDO $pdo, $tableName = 'locks') {
40 $this->tableName = $tableName;
45 * Returns a list of Sabre_DAV_Locks_LockInfo objects
47 * This method should return all the locks for a particular uri, including
48 * locks that might be set on a parent uri.
50 * If returnChildLocks is set to true, this method should also look for
51 * any locks in the subtree of the uri for locks.
54 * @param bool $returnChildLocks
57 public function getLocks($uri, $returnChildLocks) {
59 // NOTE: the following 10 lines or so could be easily replaced by
60 // pure sql. MySQL's non-standard string concatenation prevents us
61 // from doing this though.
62 $query = 'SELECT owner, token, timeout, created, scope, depth, uri FROM '.$this->tableName.' WHERE ((created + timeout) > CAST(? AS UNSIGNED INTEGER)) AND ((uri = ?)';
63 $params = array(time(),$uri);
65 // We need to check locks for every part in the uri.
66 $uriParts = explode('/',$uri);
68 // We already covered the last part of the uri
73 foreach($uriParts as $part) {
75 if ($currentPath) $currentPath.='/';
78 $query.=' OR (depth!=0 AND uri = ?)';
79 $params[] = $currentPath;
83 if ($returnChildLocks) {
85 $query.=' OR (uri LIKE ?)';
86 $params[] = $uri . '/%';
91 $stmt = $this->pdo->prepare($query);
92 $stmt->execute($params);
93 $result = $stmt->fetchAll();
96 foreach($result as $row) {
98 $lockInfo = new Sabre_DAV_Locks_LockInfo();
99 $lockInfo->owner = $row['owner'];
100 $lockInfo->token = $row['token'];
101 $lockInfo->timeout = $row['timeout'];
102 $lockInfo->created = $row['created'];
103 $lockInfo->scope = $row['scope'];
104 $lockInfo->depth = $row['depth'];
105 $lockInfo->uri = $row['uri'];
106 $lockList[] = $lockInfo;
118 * @param Sabre_DAV_Locks_LockInfo $lockInfo
121 public function lock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
123 // We're making the lock timeout 30 minutes
124 $lockInfo->timeout = 30*60;
125 $lockInfo->created = time();
126 $lockInfo->uri = $uri;
128 $locks = $this->getLocks($uri,false);
130 foreach($locks as $lock) {
131 if ($lock->token == $lockInfo->token) $exists = true;
135 $stmt = $this->pdo->prepare('UPDATE '.$this->tableName.' SET owner = ?, timeout = ?, scope = ?, depth = ?, uri = ?, created = ? WHERE token = ?');
136 $stmt->execute(array($lockInfo->owner,$lockInfo->timeout,$lockInfo->scope,$lockInfo->depth,$uri,$lockInfo->created,$lockInfo->token));
138 $stmt = $this->pdo->prepare('INSERT INTO '.$this->tableName.' (owner,timeout,scope,depth,uri,created,token) VALUES (?,?,?,?,?,?,?)');
139 $stmt->execute(array($lockInfo->owner,$lockInfo->timeout,$lockInfo->scope,$lockInfo->depth,$uri,$lockInfo->created,$lockInfo->token));
149 * Removes a lock from a uri
152 * @param Sabre_DAV_Locks_LockInfo $lockInfo
155 public function unlock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
157 $stmt = $this->pdo->prepare('DELETE FROM '.$this->tableName.' WHERE uri = ? AND token = ?');
158 $stmt->execute(array($uri,$lockInfo->token));
160 return $stmt->rowCount()===1;