]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAVACL/MockPrincipalBackend.php
removed community home addon
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAVACL / MockPrincipalBackend.php
1 <?php
2
3 class Sabre_DAVACL_MockPrincipalBackend implements Sabre_DAVACL_IPrincipalBackend {
4
5     public $groupMembers = array();
6     public $principals;
7
8     function __construct() {
9
10         $this->principals = array(
11                 array(
12                     'uri' => 'principals/user1',
13                     '{DAV:}displayname' => 'User 1',
14                     '{http://sabredav.org/ns}email-address' => 'user1.sabredav@sabredav.org',
15                     '{http://sabredav.org/ns}vcard-url' => 'addressbooks/user1/book1/vcard1.vcf',
16                 ),
17                 array(
18                     'uri' => 'principals/admin',
19                     '{DAV:}displayname' => 'Admin',
20                 ),
21                 array(
22                     'uri' => 'principals/user2',
23                     '{DAV:}displayname' => 'User 2',
24                     '{http://sabredav.org/ns}email-address' => 'user2.sabredav@sabredav.org',
25                 ),
26             );
27
28
29     }
30
31     function getPrincipalsByPrefix($prefix) {
32
33         $prefix = trim($prefix,'/') . '/';
34         $return = array();
35
36         foreach($this->principals as $principal) {
37
38             if (strpos($principal['uri'], $prefix)!==0) continue;
39
40             $return[] = $principal;
41
42         }
43
44         return $return;
45
46     }
47
48     function addPrincipal(array $principal) {
49
50         $this->principals[] = $principal;
51
52     }
53
54     function getPrincipalByPath($path) {
55
56         foreach($this->getPrincipalsByPrefix('principals') as $principal) {
57             if ($principal['uri'] === $path) return $principal;
58         }
59
60     }
61
62     function searchPrincipals($prefixPath, array $searchProperties) {
63
64         $matches = array();
65         foreach($this->getPrincipalsByPrefix($prefixPath) as $principal) {
66
67             foreach($searchProperties as $key=>$value) {
68
69                 if (!isset($principal[$key])) {
70                     continue 2;
71                 }
72                 if (mb_stripos($principal[$key],$value, 0, 'UTF-8')===false) {
73                     continue 2;
74                 }
75
76             }
77             $matches[] = $principal['uri'];
78
79         }
80         return $matches;
81
82     }
83
84     function getGroupMemberSet($path) {
85
86         return isset($this->groupMembers[$path]) ? $this->groupMembers[$path] : array();
87
88     }
89
90     function getGroupMembership($path) {
91
92         $membership = array();
93         foreach($this->groupMembers as $group=>$members) {
94             if (in_array($path, $members)) $membership[] = $group;
95         }
96         return $membership;
97
98     }
99
100     function setGroupMemberSet($path, array $members) {
101
102         $this->groupMembers[$path] = $members;
103
104     }
105
106     /**
107      * Updates one ore more webdav properties on a principal.
108      *
109      * The list of mutations is supplied as an array. Each key in the array is
110      * a propertyname, such as {DAV:}displayname.
111      *
112      * Each value is the actual value to be updated. If a value is null, it
113      * must be deleted.
114      *
115      * This method should be atomic. It must either completely succeed, or
116      * completely fail. Success and failure can simply be returned as 'true' or
117      * 'false'.
118      *
119      * It is also possible to return detailed failure information. In that case
120      * an array such as this should be returned:
121      *
122      * array(
123      *   200 => array(
124      *      '{DAV:}prop1' => null,
125      *   ),
126      *   201 => array(
127      *      '{DAV:}prop2' => null,
128      *   ),
129      *   403 => array(
130      *      '{DAV:}prop3' => null,
131      *   ),
132      *   424 => array(
133      *      '{DAV:}prop4' => null,
134      *   ),
135      * );
136      *
137      * In this previous example prop1 was successfully updated or deleted, and
138      * prop2 was succesfully created.
139      *
140      * prop3 failed to update due to '403 Forbidden' and because of this prop4
141      * also could not be updated with '424 Failed dependency'.
142      *
143      * This last example was actually incorrect. While 200 and 201 could appear
144      * in 1 response, if there's any error (403) the other properties should
145      * always fail with 423 (failed dependency).
146      *
147      * But anyway, if you don't want to scratch your head over this, just
148      * return true or false.
149      *
150      * @param string $path
151      * @param array $mutations
152      * @return array|bool
153      */
154     public function updatePrincipal($path, $mutations) {
155
156         $value = null;
157         foreach($this->principals as $principalIndex=>$value) {
158             if ($value['uri'] === $path) {
159                 $principal = $value;
160                 break;
161             }
162         }
163         if (!$principal) return false;
164
165         foreach($mutations as $prop=>$value) {
166
167             if (is_null($value) && isset($principal[$prop])) {
168                 unset($principal[$prop]);
169             } else {
170                 $principal[$prop] = $value;
171             }
172
173         }
174
175         $this->principals[$principalIndex] = $principal;
176
177         return true;
178
179     }
180
181
182 }