1<?php
2
3namespace Sabre\DAV\Locks\Backend;
4
5use Sabre\DAV\Locks\LockInfo;
6
7/**
8 * Locks Mock backend.
9 *
10 * This backend stores lock information in memory. Mainly useful for testing.
11 *
12 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
13 * @author Evert Pot (http://evertpot.com/)
14 * @license http://sabre.io/license/ Modified BSD License
15 */
16class Mock extends AbstractBackend {
17
18    /**
19     * Returns a list of Sabre\DAV\Locks\LockInfo objects
20     *
21     * This method should return all the locks for a particular uri, including
22     * locks that might be set on a parent uri.
23     *
24     * If returnChildLocks is set to true, this method should also look for
25     * any locks in the subtree of the uri for locks.
26     *
27     * @param string $uri
28     * @param bool $returnChildLocks
29     * @return array
30     */
31    public function getLocks($uri, $returnChildLocks) {
32
33        $newLocks = array();
34
35        $locks = $this->getData();
36
37        foreach($locks as $lock) {
38
39            if ($lock->uri === $uri ||
40                //deep locks on parents
41                ($lock->depth!=0 && strpos($uri, $lock->uri . '/')===0) ||
42
43                // locks on children
44                ($returnChildLocks && (strpos($lock->uri, $uri . '/')===0)) ) {
45
46                $newLocks[] = $lock;
47
48            }
49
50        }
51
52        // Checking if we can remove any of these locks
53        foreach($newLocks as $k=>$lock) {
54            if (time() > $lock->timeout + $lock->created) unset($newLocks[$k]);
55        }
56        return $newLocks;
57
58    }
59
60    /**
61     * Locks a uri
62     *
63     * @param string $uri
64     * @param LockInfo $lockInfo
65     * @return bool
66     */
67    public function lock($uri, LockInfo $lockInfo) {
68
69        // We're making the lock timeout 30 minutes
70        $lockInfo->timeout = 1800;
71        $lockInfo->created = time();
72        $lockInfo->uri = $uri;
73
74        $locks = $this->getData();
75
76        foreach($locks as $k=>$lock) {
77            if (
78                ($lock->token == $lockInfo->token) ||
79                (time() > $lock->timeout + $lock->created)
80            ) {
81                unset($locks[$k]);
82            }
83        }
84        $locks[] = $lockInfo;
85        $this->putData($locks);
86        return true;
87
88    }
89
90    /**
91     * Removes a lock from a uri
92     *
93     * @param string $uri
94     * @param LockInfo $lockInfo
95     * @return bool
96     */
97    public function unlock($uri, LockInfo $lockInfo) {
98
99        $locks = $this->getData();
100        foreach($locks as $k=>$lock) {
101
102            if ($lock->token == $lockInfo->token) {
103
104                unset($locks[$k]);
105                $this->putData($locks);
106                return true;
107
108            }
109        }
110        return false;
111
112    }
113
114    protected $data = [];
115
116    /**
117     * Loads the lockdata from the filesystem.
118     *
119     * @return array
120     */
121    protected function getData() {
122
123        return $this->data;
124
125    }
126
127    /**
128     * Saves the lockdata
129     *
130     * @param array $newData
131     * @return void
132     */
133    protected function putData(array $newData) {
134
135        $this->data = $newData;
136
137    }
138
139}
140