summaryrefslogtreecommitdiff
path: root/lib/Kolab/DAV/Locks/File.php
blob: 0d8d17b396518e8fd452340ed85c5791bac821a7 (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
<?php

/**
 * File-based Lock manager for the Kolab WebDAV service.
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
 *
 * 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/>.
 */

namespace Kolab\DAV\Locks;

use Kolab\DAV\Auth\HTTPBasic;
use Sabre\DAV\Locks\LockInfo;
use Sabre\DAV\Locks\Backend\AbstractBackend;

/**
 * The Lock manager that maintains a lock file per user in the local file system.
 */
class File extends AbstractBackend
{
    /**
     * The storage file prefix
     *
     * @var string
     */
    private $basePath;

    /**
     * Constructor
     *
     * @param string $basePath base path to store lock files
     */
    public function __construct($basePath)
    {
        $this->basePath = $basePath;
    }

    /**
     * Returns a list of Sabre\DAV\Locks\LockInfo objects
     *
     * This method should return all the locks for a particular uri, including
     * locks that might be set on a parent uri.
     *
     * If returnChildLocks is set to true, this method should also look for
     * any locks in the subtree of the uri for locks.
     *
     * @param string $uri
     * @param bool $returnChildLocks
     * @return array
     */
    public function getLocks($uri, $returnChildLocks)
    {
        console(__METHOD__, $uri, $returnChildLocks);

        $newLocks = array();
        $locks = $this->getData();

        foreach ($locks as $lock) {
            if ($lock->uri === $uri ||
                // deep locks on parents
                ($lock->depth != 0 && strpos($uri, $lock->uri . '/') === 0) ||
                // locks on children
                ($returnChildLocks && (strpos($lock->uri, $uri . '/') === 0)) ) {
                $newLocks[] = $lock;
            }
        }

        // Checking if we can remove any of these locks
        foreach ($newLocks as $k => $lock) {
            if (time() > $lock->timeout + $lock->created)
                unset($newLocks[$k]);
        }

        return $newLocks;
    }

    /**
     * Locks a uri
     *
     * @param string $uri
     * @param LockInfo $lockInfo
     * @return bool
     */
    public function lock($uri, LockInfo $lockInfo)
    {
        console(__METHOD__, $uri, $lockInfo);

        // We're making the lock timeout 30 minutes
        $lockInfo->timeout = 1800;
        $lockInfo->created = time();
        $lockInfo->uri = $uri;
        $lockInfo->owner = trim($lockInfo->owner);

        $locks = $this->getData();
        $now = time();

        foreach ($locks as $k => $lock) {
            if (($lock->token == $lockInfo->token) || ($now > $lock->timeout + $lock->created)) {
                unset($locks[$k]);
            }
        }
        $locks[] = $lockInfo;
        $this->putData($locks);

        return true;
    }

    /**
     * Removes a lock from a uri
     *
     * @param string $uri
     * @param LockInfo $lockInfo
     * @return bool
     */
    public function unlock($uri, LockInfo $lockInfo)
    {
        console(__METHOD__, $uri, $lockInfo);

        $locks = $this->getData();
        foreach ($locks as $k => $lock) {
            if ($lock->token == $lockInfo->token) {
                unset($locks[$k]);
                $this->putData($locks);
                return true;
            }
        }

        return false;
    }

    /**
     * Loads the lockdata from the filesystem.
     *
     * @return array
     */
    protected function getData()
    {
        $locksFile = $this->getLocksFile();
        if (!file_exists($locksFile))
            return array();

        // opening up the file, and creating a shared lock
        $handle = fopen($locksFile, 'r');
        flock($handle, LOCK_SH);

        // Reading data until the eof
        $data = stream_get_contents($handle);

        // We're all good
        fclose($handle);

        // Unserializing and checking if the resource file contains data for this file
        $data = unserialize($data);
        return $data ?: array();

    }

    /**
     * Compose a full path to the lock file of the current user
     */
    protected function getLocksFile()
    {
        return $this->basePath . '_' . str_replace('@', '_', HTTPBasic::$current_user);
    }

    /**
     * Saves the lockdata
     *
     * @param array $newData
     * @return void
     */
    protected function putData(array $newData)
    {
        // opening up the file, and creating an exclusive lock
        $handle = fopen($this->getLocksFile(), 'a+');
        flock($handle, LOCK_EX);

        // We can only truncate and rewind once the lock is acquired.
        ftruncate($handle, 0);
        rewind($handle);

        fwrite($handle, serialize($newData));
        fclose($handle);
    }

}