]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Locks/Backend/PDO.php
Merge branch 'master' of ../../save/merge/frio_hovercard into frio
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Locks / Backend / PDO.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 database. You must pass a PDO
7  * connection object in the constructor.
8  *
9  * @package Sabre
10  * @subpackage DAV
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
14  */
15 class Sabre_DAV_Locks_Backend_PDO extends Sabre_DAV_Locks_Backend_Abstract {
16
17     /**
18      * The PDO connection object
19      *
20      * @var pdo
21      */
22     private $pdo;
23
24     /**
25      * The PDO tablename this backend uses.
26      *
27      * @var string
28      */
29     protected $tableName;
30
31     /**
32      * Constructor
33      *
34      * @param PDO $pdo
35      * @param string $tableName
36      */
37     public function __construct(PDO $pdo, $tableName = 'locks') {
38
39         $this->pdo = $pdo;
40         $this->tableName = $tableName;
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         // 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);
64
65         // We need to check locks for every part in the uri.
66         $uriParts = explode('/',$uri);
67
68         // We already covered the last part of the uri
69         array_pop($uriParts);
70
71         $currentPath='';
72
73         foreach($uriParts as $part) {
74
75             if ($currentPath) $currentPath.='/';
76             $currentPath.=$part;
77
78             $query.=' OR (depth!=0 AND uri = ?)';
79             $params[] = $currentPath;
80
81         }
82
83         if ($returnChildLocks) {
84
85             $query.=' OR (uri LIKE ?)';
86             $params[] = $uri . '/%';
87
88         }
89         $query.=')';
90
91         $stmt = $this->pdo->prepare($query);
92         $stmt->execute($params);
93         $result = $stmt->fetchAll();
94
95         $lockList = array();
96         foreach($result as $row) {
97
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;
107
108         }
109
110         return $lockList;
111
112     }
113
114     /**
115      * Locks a uri
116      *
117      * @param string $uri
118      * @param Sabre_DAV_Locks_LockInfo $lockInfo
119      * @return bool
120      */
121     public function lock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
122
123         // We're making the lock timeout 30 minutes
124         $lockInfo->timeout = 30*60;
125         $lockInfo->created = time();
126         $lockInfo->uri = $uri;
127
128         $locks = $this->getLocks($uri,false);
129         $exists = false;
130         foreach($locks as $lock) {
131             if ($lock->token == $lockInfo->token) $exists = true;
132         }
133
134         if ($exists) {
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));
137         } else {
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));
140         }
141
142         return true;
143
144     }
145
146
147
148     /**
149      * Removes a lock from a uri
150      *
151      * @param string $uri
152      * @param Sabre_DAV_Locks_LockInfo $lockInfo
153      * @return bool
154      */
155     public function unlock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
156
157         $stmt = $this->pdo->prepare('DELETE FROM '.$this->tableName.' WHERE uri = ? AND token = ?');
158         $stmt->execute(array($uri,$lockInfo->token));
159
160         return $stmt->rowCount()===1;
161
162     }
163
164 }
165