summaryrefslogtreecommitdiff
path: root/lib/file_locks.php
blob: 300d3b186905833b1c3ff3d425ec97209e104ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab File API                                  |
 |                                                                          |
 | Copyright (C) 2012-2013, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

/**
 * The Lock manager allows you to handle all file-locks centrally.
 * It stores all its data in a sql database. Derived from SabreDAV's
 * PDO Lock manager.
 */
class file_locks {

    const SHARED    = 1;
    const EXCLUSIVE = 2;
    const INFINITE  = -1;

    /**
     * The database connection object
     *
     * @var rcube_db
     */
    private $db;

    /**
     * The tablename this backend uses.
     *
     * @var string
     */
    protected $table;

    /**
     * Internal cache
     *
     * @var array
     */
    protected $icache = array();

    /**
     * Constructor
     *
     * @param string $table Table name
     */
    public function __construct($table = 'chwala_locks')
    {
        $rcube = rcube::get_instance();

        $this->db    = $rcube->get_dbh();
        $this->table = $this->db->table_name($table);

        if ($rcube->session) {
            $rcube->session->register_gc_handler(array($this, 'gc'));
        }
        else {
            // run garbage collector with probability based on
            // session settings if session does not exist.
            $probability = (int) ini_get('session.gc_probability');
            $divisor     = (int) ini_get('session.gc_divisor');

            if ($divisor > 0 && $probability > 0) {
                $random = mt_rand(1, $divisor);
                if ($random <= $probability) {
                    $this->gc();
                }
            }
        }
    }

    /**
     * Returns a list of locks
     *
     * This method should return all the locks for a particular URI, including
     * locks that might be set on a parent URI.
     *
     * If child_locks is set to true, this method should also look for
     * any locks in the subtree of the URI for locks.
     *
     * @param string $uri         URI
     * @param bool   $child_locks Enables subtree checks
     *
     * @return array List of locks
     */
    public function lock_list($uri, $child_locks = false)
    {
        if ($this->icache['uri'] == $uri && $this->icache['child'] == $child_locks) {
            return $this->icache['list'];
        }

        $query  = "SELECT * FROM " . $this->db->quote_identifier($this->table) . " WHERE (uri = ?";
        $params = array($uri);

        if ($child_locks) {
            $query   .= " OR uri LIKE ?";
            $params[] = $uri . '/%';
        }

        $path = '';
        $key  = $uri;
        $list = array();

        // in case uri contains protocol/host specification e.g. imap://user@host/
        // handle prefix separately
        if (preg_match('~^([a-z]+://[^/]+/)~i', $uri, $matches)) {
            $path = $matches[1];
            $uri  = substr($uri, strlen($matches[1]));
        }

        // We need to check locks for every part in the path
        $path_parts = explode('/', $uri);

        // We already covered the last part of the uri
        array_pop($path_parts);

        if (!empty($path_parts)) {
            $root_path = $path . implode('/', $path_parts);

            // this path is already cached, extract locks from cached result
            // we do this because it is a common scenario to request
            // for lock on every file/folder in specified location
            if ($this->icache['root_path'] == $root_path) {
                $length = strlen($root_path);
                foreach ($this->icache['list'] as $lock) {
                    if ($lock['depth'] != 0 && strlen($lock['token']) <= $length) {
                        $list[] = $lock;
                    }
                }
            }
            else {
                foreach ($path_parts as $part) {
                    $path     .= $part;
                    $params[]  = $path;
                    $path     .= '/';
                }

                $query .= " OR (uri IN (" . implode(',', array_pad(array(), count($path_parts), '?')) . ") AND depth <> 0)";
            }
        }

        // finally, skip expired locks
        $query .= ") AND expires > " . $this->db->now();

        // run the query and parse result
        $result = $this->db->query($query, $params);

        while ($row = $this->db->fetch_assoc($result)) {
            $created = strtotime($row['expires']) - $row['timeout'];
            $list[] = array(
                'uri'     => $row['uri'],
                'owner'   => $row['owner'],
                'token'   => $row['token'],
                'timeout' => (int) $row['timeout'],
                'created' => (int) $created,
                'scope'   => $row['scope'] == self::EXCLUSIVE ? file_storage::LOCK_EXCLUSIVE : file_storage::LOCK_SHARED,
                'depth'   => $row['depth'] == self::INFINITE ? file_storage::LOCK_INFINITE : (int) $row['depth'],
            );
        }

        // remember last result in memory, sometimes we need it (or part of it) again
        $this->icache['list']        = $list;
        $this->icache['uri']         = $key;
        $this->icache['root_path']   = $root_path;
        $this->icache['child_locks'] = $child_locks;

        return $list;
    }

    /**
     * Locks a uri
     *
     * @param string $uri  URI
     * @param array  $lock Lock data
     *
     * @return bool
     */
    public function lock($uri, $lock)
    {
        // We're making the lock timeout max. 30 minutes
        $timeout = min($lock['timeout'], 30*60);

        $data = array(
            $this->db->quote_identifier('uri')     => $uri,
            $this->db->quote_identifier('owner')   => $lock['owner'],
            $this->db->quote_identifier('scope')   => $lock['scope'] == file_storage::LOCK_EXCLUSIVE ? self::EXCLUSIVE : self::SHARED,
            $this->db->quote_identifier('depth')   => $lock['depth'] == file_storage::LOCK_INFINITE ? self::INFINITE : 0,
            $this->db->quote_identifier('timeout') => $timeout,
        );

        // check if lock exists
        $locks  = $this->lock_list($uri, false);
        $exists = false;

        foreach ($locks as $l) {
            if ($l['token'] == $lock['token']) {
                $exists = true;
                break;
            }
        }

        if ($exists) {
            foreach (array_keys($data) as $key) {
                $update_cols[] = "$key = ?";
            }

            $result = $this->db->query("UPDATE " . $this->db->quote_identifier($this->table)
                . " SET " . implode(', ', $update_cols)
                    . ", " . $this->db->quote_identifier('expires') . " = " . $this->db->now($timeout)
                . " WHERE token = ?",
                array_merge(array_values($data), array($lock['token']))
            );
        }
        else {
            $data[$this->db->quote_identifier('token')] = $lock['token'];

            $result = $this->db->query("INSERT INTO " . $this->db->quote_identifier($this->table)
                . " (".join(', ', array_keys($data)) . ", " . $this->db->quote_identifier('expires') . ")"
                . " VALUES (" . str_repeat('?, ', count($data)) . $this->db->now($timeout) . ")",
                array_values($data)
            );
        }

        return $this->db->affected_rows();
    }

    /**
     * Removes a lock from a URI
     *
     * @param string $path URI
     * @param array  $lock Lock data
     *
     * @return bool
     */
    public function unlock($uri, $lock)
    {
        $stmt = $this->db->query("DELETE FROM " . $this->db->quote_identifier($this->table)
            . " WHERE uri = ? AND token = ?",
            $uri, $lock['token']);

        return $this->db->affected_rows();
    }

    /**
     * Remove expired locks
     */
    public function gc()
    {
        $this->db->query("DELETE FROM " . $this->db->quote_identifier($this->table)
            . " WHERE expires < " . $this->db->now());
    }
}