Completed
Pull Request — master (#5300)
by Georg
14:46
created
apps/dav/lib/CalDAV/Calendar.php 1 patch
Indentation   +275 added lines, -275 removed lines patch added patch discarded remove patch
@@ -38,280 +38,280 @@
 block discarded – undo
38 38
  */
39 39
 class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
40 40
 
41
-	public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) {
42
-		parent::__construct($caldavBackend, $calendarInfo);
43
-
44
-		if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
45
-			$this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
46
-		}
47
-		if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
48
-			$this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
49
-			$this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
50
-		}
51
-	}
52
-
53
-	/**
54
-	 * Updates the list of shares.
55
-	 *
56
-	 * The first array is a list of people that are to be added to the
57
-	 * resource.
58
-	 *
59
-	 * Every element in the add array has the following properties:
60
-	 *   * href - A url. Usually a mailto: address
61
-	 *   * commonName - Usually a first and last name, or false
62
-	 *   * summary - A description of the share, can also be false
63
-	 *   * readOnly - A boolean value
64
-	 *
65
-	 * Every element in the remove array is just the address string.
66
-	 *
67
-	 * @param array $add
68
-	 * @param array $remove
69
-	 * @return void
70
-	 * @throws Forbidden
71
-	 */
72
-	public function updateShares(array $add, array $remove) {
73
-		if ($this->isShared()) {
74
-			throw new Forbidden();
75
-		}
76
-		$this->caldavBackend->updateShares($this, $add, $remove);
77
-	}
78
-
79
-	/**
80
-	 * Returns the list of people whom this resource is shared with.
81
-	 *
82
-	 * Every element in this array should have the following properties:
83
-	 *   * href - Often a mailto: address
84
-	 *   * commonName - Optional, for example a first + last name
85
-	 *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
86
-	 *   * readOnly - boolean
87
-	 *   * summary - Optional, a description for the share
88
-	 *
89
-	 * @return array
90
-	 */
91
-	public function getShares() {
92
-		if ($this->isShared()) {
93
-			return [];
94
-		}
95
-		return $this->caldavBackend->getShares($this->getResourceId());
96
-	}
97
-
98
-	/**
99
-	 * @return int
100
-	 */
101
-	public function getResourceId() {
102
-		return $this->calendarInfo['id'];
103
-	}
104
-
105
-	/**
106
-	 * @return string
107
-	 */
108
-	public function getPrincipalURI() {
109
-		return $this->calendarInfo['principaluri'];
110
-	}
111
-
112
-	public function getACL() {
113
-		$acl =  [
114
-			[
115
-				'privilege' => '{DAV:}read',
116
-				'principal' => $this->getOwner(),
117
-				'protected' => true,
118
-			]];
119
-		if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
120
-			$acl[] = [
121
-				'privilege' => '{DAV:}write',
122
-				'principal' => $this->getOwner(),
123
-				'protected' => true,
124
-			];
125
-		}
126
-		if ($this->getOwner() !== parent::getOwner()) {
127
-			$acl[] =  [
128
-					'privilege' => '{DAV:}read',
129
-					'principal' => parent::getOwner(),
130
-					'protected' => true,
131
-				];
132
-			if ($this->canWrite()) {
133
-				$acl[] = [
134
-					'privilege' => '{DAV:}write',
135
-					'principal' => parent::getOwner(),
136
-					'protected' => true,
137
-				];
138
-			} else {
139
-				$acl[] = [
140
-					'privilege' => '{DAV:}write-properties',
141
-					'principal' => parent::getOwner(),
142
-					'protected' => true,
143
-				];
144
-			}
145
-		}
146
-		if ($this->isPublic()) {
147
-			$acl[] = [
148
-				'privilege' => '{DAV:}read',
149
-				'principal' => 'principals/system/public',
150
-				'protected' => true,
151
-			];
152
-		}
153
-
154
-		$acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
155
-
156
-		if (!$this->isShared()) {
157
-			return $acl;
158
-		}
159
-
160
-		$allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public'];
161
-		return array_filter($acl, function($rule) use ($allowedPrincipals) {
162
-			return in_array($rule['principal'], $allowedPrincipals);
163
-		});
164
-	}
165
-
166
-	public function getChildACL() {
167
-		return $this->getACL();
168
-	}
169
-
170
-	public function getOwner() {
171
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
172
-			return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
173
-		}
174
-		return parent::getOwner();
175
-	}
176
-
177
-	public function delete() {
178
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
179
-			$this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
180
-			$principal = 'principal:' . parent::getOwner();
181
-			$shares = $this->caldavBackend->getShares($this->getResourceId());
182
-			$shares = array_filter($shares, function($share) use ($principal){
183
-				return $share['href'] === $principal;
184
-			});
185
-			if (empty($shares)) {
186
-				throw new Forbidden();
187
-			}
188
-
189
-			$this->caldavBackend->updateShares($this, [], [
190
-				'href' => $principal
191
-			]);
192
-			return;
193
-		}
194
-		parent::delete();
195
-	}
196
-
197
-	public function propPatch(PropPatch $propPatch) {
198
-		// parent::propPatch will only update calendars table
199
-		// if calendar is shared, changes have to be made to the properties table
200
-		if (!$this->isShared()) {
201
-			parent::propPatch($propPatch);
202
-		}
203
-	}
204
-
205
-	public function getChild($name) {
206
-
207
-		$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
208
-
209
-		if (!$obj) {
210
-			throw new NotFound('Calendar object not found');
211
-		}
212
-
213
-		if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
214
-			throw new NotFound('Calendar object not found');
215
-		}
216
-
217
-		$obj['acl'] = $this->getChildACL();
218
-
219
-		return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
220
-
221
-	}
222
-
223
-	public function getChildren() {
224
-
225
-		$objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
226
-		$children = [];
227
-		foreach ($objs as $obj) {
228
-			if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
229
-				continue;
230
-			}
231
-			$obj['acl'] = $this->getChildACL();
232
-			$children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
233
-		}
234
-		return $children;
235
-
236
-	}
237
-
238
-	public function getMultipleChildren(array $paths) {
239
-
240
-		$objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
241
-		$children = [];
242
-		foreach ($objs as $obj) {
243
-			if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
244
-				continue;
245
-			}
246
-			$obj['acl'] = $this->getChildACL();
247
-			$children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
248
-		}
249
-		return $children;
250
-
251
-	}
252
-
253
-	public function childExists($name) {
254
-		$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
255
-		if (!$obj) {
256
-			return false;
257
-		}
258
-		if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
259
-			return false;
260
-		}
261
-
262
-		return true;
263
-	}
264
-
265
-	public function calendarQuery(array $filters) {
266
-
267
-		$uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
268
-		if ($this->isShared()) {
269
-			return array_filter($uris, function ($uri) {
270
-				return $this->childExists($uri);
271
-			});
272
-		}
273
-
274
-		return $uris;
275
-	}
276
-
277
-	/**
278
-	 * @param boolean $value
279
-	 * @return string|null
280
-	 */
281
-	public function setPublishStatus($value) {
282
-		$publicUri = $this->caldavBackend->setPublishStatus($value, $this);
283
-		$this->calendarInfo['publicuri'] = $publicUri;
284
-		return $publicUri;
285
-	}
286
-
287
-	/**
288
-	 * @return mixed $value
289
-	 */
290
-	public function getPublishStatus() {
291
-		return $this->caldavBackend->getPublishStatus($this);
292
-	}
293
-
294
-	private function canWrite() {
295
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
296
-			return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
297
-		}
298
-		return true;
299
-	}
300
-
301
-	private function isPublic() {
302
-		return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
303
-	}
304
-
305
-	protected function isShared() {
306
-		if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
307
-			return false;
308
-		}
309
-
310
-		return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
311
-	}
312
-
313
-	public function isSubscription() {
314
-		return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
315
-	}
41
+    public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) {
42
+        parent::__construct($caldavBackend, $calendarInfo);
43
+
44
+        if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
45
+            $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
46
+        }
47
+        if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
48
+            $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
49
+            $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
50
+        }
51
+    }
52
+
53
+    /**
54
+     * Updates the list of shares.
55
+     *
56
+     * The first array is a list of people that are to be added to the
57
+     * resource.
58
+     *
59
+     * Every element in the add array has the following properties:
60
+     *   * href - A url. Usually a mailto: address
61
+     *   * commonName - Usually a first and last name, or false
62
+     *   * summary - A description of the share, can also be false
63
+     *   * readOnly - A boolean value
64
+     *
65
+     * Every element in the remove array is just the address string.
66
+     *
67
+     * @param array $add
68
+     * @param array $remove
69
+     * @return void
70
+     * @throws Forbidden
71
+     */
72
+    public function updateShares(array $add, array $remove) {
73
+        if ($this->isShared()) {
74
+            throw new Forbidden();
75
+        }
76
+        $this->caldavBackend->updateShares($this, $add, $remove);
77
+    }
78
+
79
+    /**
80
+     * Returns the list of people whom this resource is shared with.
81
+     *
82
+     * Every element in this array should have the following properties:
83
+     *   * href - Often a mailto: address
84
+     *   * commonName - Optional, for example a first + last name
85
+     *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
86
+     *   * readOnly - boolean
87
+     *   * summary - Optional, a description for the share
88
+     *
89
+     * @return array
90
+     */
91
+    public function getShares() {
92
+        if ($this->isShared()) {
93
+            return [];
94
+        }
95
+        return $this->caldavBackend->getShares($this->getResourceId());
96
+    }
97
+
98
+    /**
99
+     * @return int
100
+     */
101
+    public function getResourceId() {
102
+        return $this->calendarInfo['id'];
103
+    }
104
+
105
+    /**
106
+     * @return string
107
+     */
108
+    public function getPrincipalURI() {
109
+        return $this->calendarInfo['principaluri'];
110
+    }
111
+
112
+    public function getACL() {
113
+        $acl =  [
114
+            [
115
+                'privilege' => '{DAV:}read',
116
+                'principal' => $this->getOwner(),
117
+                'protected' => true,
118
+            ]];
119
+        if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
120
+            $acl[] = [
121
+                'privilege' => '{DAV:}write',
122
+                'principal' => $this->getOwner(),
123
+                'protected' => true,
124
+            ];
125
+        }
126
+        if ($this->getOwner() !== parent::getOwner()) {
127
+            $acl[] =  [
128
+                    'privilege' => '{DAV:}read',
129
+                    'principal' => parent::getOwner(),
130
+                    'protected' => true,
131
+                ];
132
+            if ($this->canWrite()) {
133
+                $acl[] = [
134
+                    'privilege' => '{DAV:}write',
135
+                    'principal' => parent::getOwner(),
136
+                    'protected' => true,
137
+                ];
138
+            } else {
139
+                $acl[] = [
140
+                    'privilege' => '{DAV:}write-properties',
141
+                    'principal' => parent::getOwner(),
142
+                    'protected' => true,
143
+                ];
144
+            }
145
+        }
146
+        if ($this->isPublic()) {
147
+            $acl[] = [
148
+                'privilege' => '{DAV:}read',
149
+                'principal' => 'principals/system/public',
150
+                'protected' => true,
151
+            ];
152
+        }
153
+
154
+        $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
155
+
156
+        if (!$this->isShared()) {
157
+            return $acl;
158
+        }
159
+
160
+        $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public'];
161
+        return array_filter($acl, function($rule) use ($allowedPrincipals) {
162
+            return in_array($rule['principal'], $allowedPrincipals);
163
+        });
164
+    }
165
+
166
+    public function getChildACL() {
167
+        return $this->getACL();
168
+    }
169
+
170
+    public function getOwner() {
171
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
172
+            return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
173
+        }
174
+        return parent::getOwner();
175
+    }
176
+
177
+    public function delete() {
178
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
179
+            $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
180
+            $principal = 'principal:' . parent::getOwner();
181
+            $shares = $this->caldavBackend->getShares($this->getResourceId());
182
+            $shares = array_filter($shares, function($share) use ($principal){
183
+                return $share['href'] === $principal;
184
+            });
185
+            if (empty($shares)) {
186
+                throw new Forbidden();
187
+            }
188
+
189
+            $this->caldavBackend->updateShares($this, [], [
190
+                'href' => $principal
191
+            ]);
192
+            return;
193
+        }
194
+        parent::delete();
195
+    }
196
+
197
+    public function propPatch(PropPatch $propPatch) {
198
+        // parent::propPatch will only update calendars table
199
+        // if calendar is shared, changes have to be made to the properties table
200
+        if (!$this->isShared()) {
201
+            parent::propPatch($propPatch);
202
+        }
203
+    }
204
+
205
+    public function getChild($name) {
206
+
207
+        $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
208
+
209
+        if (!$obj) {
210
+            throw new NotFound('Calendar object not found');
211
+        }
212
+
213
+        if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
214
+            throw new NotFound('Calendar object not found');
215
+        }
216
+
217
+        $obj['acl'] = $this->getChildACL();
218
+
219
+        return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
220
+
221
+    }
222
+
223
+    public function getChildren() {
224
+
225
+        $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
226
+        $children = [];
227
+        foreach ($objs as $obj) {
228
+            if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
229
+                continue;
230
+            }
231
+            $obj['acl'] = $this->getChildACL();
232
+            $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
233
+        }
234
+        return $children;
235
+
236
+    }
237
+
238
+    public function getMultipleChildren(array $paths) {
239
+
240
+        $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
241
+        $children = [];
242
+        foreach ($objs as $obj) {
243
+            if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
244
+                continue;
245
+            }
246
+            $obj['acl'] = $this->getChildACL();
247
+            $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
248
+        }
249
+        return $children;
250
+
251
+    }
252
+
253
+    public function childExists($name) {
254
+        $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
255
+        if (!$obj) {
256
+            return false;
257
+        }
258
+        if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
259
+            return false;
260
+        }
261
+
262
+        return true;
263
+    }
264
+
265
+    public function calendarQuery(array $filters) {
266
+
267
+        $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
268
+        if ($this->isShared()) {
269
+            return array_filter($uris, function ($uri) {
270
+                return $this->childExists($uri);
271
+            });
272
+        }
273
+
274
+        return $uris;
275
+    }
276
+
277
+    /**
278
+     * @param boolean $value
279
+     * @return string|null
280
+     */
281
+    public function setPublishStatus($value) {
282
+        $publicUri = $this->caldavBackend->setPublishStatus($value, $this);
283
+        $this->calendarInfo['publicuri'] = $publicUri;
284
+        return $publicUri;
285
+    }
286
+
287
+    /**
288
+     * @return mixed $value
289
+     */
290
+    public function getPublishStatus() {
291
+        return $this->caldavBackend->getPublishStatus($this);
292
+    }
293
+
294
+    private function canWrite() {
295
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
296
+            return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
297
+        }
298
+        return true;
299
+    }
300
+
301
+    private function isPublic() {
302
+        return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
303
+    }
304
+
305
+    protected function isShared() {
306
+        if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
307
+            return false;
308
+        }
309
+
310
+        return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
311
+    }
312
+
313
+    public function isSubscription() {
314
+        return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
315
+    }
316 316
 
317 317
 }
Please login to merge, or discard this patch.