Completed
Branch develop (c24f26)
by
unknown
26:13
created
swiftmailer/egulias/email-validator/EmailValidator/Parser/DomainPart.php 2 patches
Indentation   +406 added lines, -406 removed lines patch added patch discarded remove patch
@@ -34,410 +34,410 @@
 block discarded – undo
34 34
 
35 35
 class DomainPart extends Parser
36 36
 {
37
-    const DOMAIN_MAX_LENGTH = 254;
38
-    const LABEL_MAX_LENGTH = 63;
39
-
40
-    /**
41
-     * @var string
42
-     */
43
-    protected $domainPart = '';
44
-
45
-    public function parse($domainPart)
46
-    {
47
-        $this->lexer->moveNext();
48
-
49
-        $this->performDomainStartChecks();
50
-
51
-        $domain = $this->doParseDomainPart();
52
-
53
-        $prev = $this->lexer->getPrevious();
54
-        $length = strlen($domain);
55
-
56
-        if ($prev['type'] === EmailLexer::S_DOT) {
57
-            throw new DotAtEnd();
58
-        }
59
-        if ($prev['type'] === EmailLexer::S_HYPHEN) {
60
-            throw new DomainHyphened();
61
-        }
62
-        if ($length > self::DOMAIN_MAX_LENGTH) {
63
-            $this->warnings[DomainTooLong::CODE] = new DomainTooLong();
64
-        }
65
-        if ($prev['type'] === EmailLexer::S_CR) {
66
-            throw new CRLFAtTheEnd();
67
-        }
68
-        $this->domainPart = $domain;
69
-    }
70
-
71
-    private function performDomainStartChecks()
72
-    {
73
-        $this->checkInvalidTokensAfterAT();
74
-        $this->checkEmptyDomain();
75
-
76
-        if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
77
-            $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
78
-            $this->parseDomainComments();
79
-        }
80
-    }
81
-
82
-    private function checkEmptyDomain()
83
-    {
84
-        $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY ||
85
-            ($this->lexer->token['type'] === EmailLexer::S_SP &&
86
-            !$this->lexer->isNextToken(EmailLexer::GENERIC));
87
-
88
-        if ($thereIsNoDomain) {
89
-            throw new NoDomainPart();
90
-        }
91
-    }
92
-
93
-    private function checkInvalidTokensAfterAT()
94
-    {
95
-        if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
96
-            throw new DotAtStart();
97
-        }
98
-        if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
99
-            throw new DomainHyphened();
100
-        }
101
-    }
102
-
103
-    /**
104
-     * @return string
105
-     */
106
-    public function getDomainPart()
107
-    {
108
-        return $this->domainPart;
109
-    }
110
-
111
-    /**
112
-     * @param string $addressLiteral
113
-     * @param int $maxGroups
114
-     */
115
-    public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
116
-    {
117
-        $prev = $this->lexer->getPrevious();
118
-        if ($prev['type'] === EmailLexer::S_COLON) {
119
-            $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd();
120
-        }
121
-
122
-        $IPv6       = substr($addressLiteral, 5);
123
-        //Daniel Marschall's new IPv6 testing strategy
124
-        $matchesIP  = explode(':', $IPv6);
125
-        $groupCount = count($matchesIP);
126
-        $colons     = strpos($IPv6, '::');
127
-
128
-        if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) {
129
-            $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar();
130
-        }
131
-
132
-        if ($colons === false) {
133
-            // We need exactly the right number of groups
134
-            if ($groupCount !== $maxGroups) {
135
-                $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount();
136
-            }
137
-            return;
138
-        }
139
-
140
-        if ($colons !== strrpos($IPv6, '::')) {
141
-            $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon();
142
-            return;
143
-        }
144
-
145
-        if ($colons === 0 || $colons === (strlen($IPv6) - 2)) {
146
-            // RFC 4291 allows :: at the start or end of an address
147
-            //with 7 other groups in addition
148
-            ++$maxGroups;
149
-        }
150
-
151
-        if ($groupCount > $maxGroups) {
152
-            $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups();
153
-        } elseif ($groupCount === $maxGroups) {
154
-            $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated();
155
-        }
156
-    }
157
-
158
-    /**
159
-     * @return string
160
-     */
161
-    protected function doParseDomainPart()
162
-    {
163
-        $domain = '';
164
-        $label = '';
165
-        $openedParenthesis = 0;
166
-        do {
167
-            $prev = $this->lexer->getPrevious();
168
-
169
-            $this->checkNotAllowedChars($this->lexer->token);
170
-
171
-            if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
172
-                $this->parseComments();
173
-                $openedParenthesis += $this->getOpenedParenthesis();
174
-                $this->lexer->moveNext();
175
-                $tmpPrev = $this->lexer->getPrevious();
176
-                if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
177
-                    $openedParenthesis--;
178
-                }
179
-            }
180
-            if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
181
-                if ($openedParenthesis === 0) {
182
-                    throw new UnopenedComment();
183
-                } else {
184
-                    $openedParenthesis--;
185
-                }
186
-            }
187
-
188
-            $this->checkConsecutiveDots();
189
-            $this->checkDomainPartExceptions($prev);
190
-
191
-            if ($this->hasBrackets()) {
192
-                $this->parseDomainLiteral();
193
-            }
194
-
195
-            if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
196
-                $this->checkLabelLength($label);
197
-                $label = '';
198
-            } else {
199
-                $label .= $this->lexer->token['value'];
200
-            }
201
-
202
-            if ($this->isFWS()) {
203
-                $this->parseFWS();
204
-            }
205
-
206
-            $domain .= $this->lexer->token['value'];
207
-            $this->lexer->moveNext();
208
-            if ($this->lexer->token['type'] === EmailLexer::S_SP) {
209
-                throw new CharNotAllowed();
210
-            }
211
-        } while (null !== $this->lexer->token['type']);
212
-
213
-        $this->checkLabelLength($label);
214
-
215
-        return $domain;
216
-    }
217
-
218
-    private function checkNotAllowedChars(array $token)
219
-    {
220
-        $notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
221
-        if (isset($notAllowed[$token['type']])) {
222
-            throw new CharNotAllowed();
223
-        }
224
-    }
225
-
226
-    /**
227
-     * @return string|false
228
-     */
229
-    protected function parseDomainLiteral()
230
-    {
231
-        if ($this->lexer->isNextToken(EmailLexer::S_COLON)) {
232
-            $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart();
233
-        }
234
-        if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) {
235
-            $lexer = clone $this->lexer;
236
-            $lexer->moveNext();
237
-            if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) {
238
-                $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart();
239
-            }
240
-        }
241
-
242
-        return $this->doParseDomainLiteral();
243
-    }
244
-
245
-    /**
246
-     * @return string|false
247
-     */
248
-    protected function doParseDomainLiteral()
249
-    {
250
-        $IPv6TAG = false;
251
-        $addressLiteral = '';
252
-        do {
253
-            if ($this->lexer->token['type'] === EmailLexer::C_NUL) {
254
-                throw new ExpectingDTEXT();
255
-            }
256
-
257
-            if ($this->lexer->token['type'] === EmailLexer::INVALID ||
258
-                $this->lexer->token['type'] === EmailLexer::C_DEL   ||
259
-                $this->lexer->token['type'] === EmailLexer::S_LF
260
-            ) {
261
-                $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT();
262
-            }
263
-
264
-            if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENQBRACKET, EmailLexer::S_OPENBRACKET))) {
265
-                throw new ExpectingDTEXT();
266
-            }
267
-
268
-            if ($this->lexer->isNextTokenAny(
269
-                array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF)
270
-            )) {
271
-                $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
272
-                $this->parseFWS();
273
-            }
274
-
275
-            if ($this->lexer->isNextToken(EmailLexer::S_CR)) {
276
-                throw new CRNoLF();
277
-            }
278
-
279
-            if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) {
280
-                $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT();
281
-                $addressLiteral .= $this->lexer->token['value'];
282
-                $this->lexer->moveNext();
283
-                $this->validateQuotedPair();
284
-            }
285
-            if ($this->lexer->token['type'] === EmailLexer::S_IPV6TAG) {
286
-                $IPv6TAG = true;
287
-            }
288
-            if ($this->lexer->token['type'] === EmailLexer::S_CLOSEQBRACKET) {
289
-                break;
290
-            }
291
-
292
-            $addressLiteral .= $this->lexer->token['value'];
293
-
294
-        } while ($this->lexer->moveNext());
295
-
296
-        $addressLiteral = str_replace('[', '', $addressLiteral);
297
-        $addressLiteral = $this->checkIPV4Tag($addressLiteral);
298
-
299
-        if (false === $addressLiteral) {
300
-            return $addressLiteral;
301
-        }
302
-
303
-        if (!$IPv6TAG) {
304
-            $this->warnings[DomainLiteral::CODE] = new DomainLiteral();
305
-            return $addressLiteral;
306
-        }
307
-
308
-        $this->warnings[AddressLiteral::CODE] = new AddressLiteral();
309
-
310
-        $this->checkIPV6Tag($addressLiteral);
311
-
312
-        return $addressLiteral;
313
-    }
314
-
315
-    /**
316
-     * @param string $addressLiteral
317
-     *
318
-     * @return string|false
319
-     */
320
-    protected function checkIPV4Tag($addressLiteral)
321
-    {
322
-        $matchesIP  = array();
323
-
324
-        // Extract IPv4 part from the end of the address-literal (if there is one)
325
-        if (preg_match(
326
-            '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/',
327
-            $addressLiteral,
328
-            $matchesIP
329
-        ) > 0
330
-        ) {
331
-            $index = strrpos($addressLiteral, $matchesIP[0]);
332
-            if ($index === 0) {
333
-                $this->warnings[AddressLiteral::CODE] = new AddressLiteral();
334
-                return false;
335
-            }
336
-            // Convert IPv4 part to IPv6 format for further testing
337
-            $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0';
338
-        }
339
-
340
-        return $addressLiteral;
341
-    }
342
-
343
-    protected function checkDomainPartExceptions(array $prev)
344
-    {
345
-        $invalidDomainTokens = array(
346
-            EmailLexer::S_DQUOTE => true,
347
-            EmailLexer::S_SQUOTE => true,
348
-            EmailLexer::S_BACKTICK => true,
349
-            EmailLexer::S_SEMICOLON => true,
350
-            EmailLexer::S_GREATERTHAN => true,
351
-            EmailLexer::S_LOWERTHAN => true,
352
-        );
353
-
354
-        if (isset($invalidDomainTokens[$this->lexer->token['type']])) {
355
-            throw new ExpectingATEXT();
356
-        }
357
-
358
-        if ($this->lexer->token['type'] === EmailLexer::S_COMMA) {
359
-            throw new CommaInDomain();
360
-        }
361
-
362
-        if ($this->lexer->token['type'] === EmailLexer::S_AT) {
363
-            throw new ConsecutiveAt();
364
-        }
365
-
366
-        if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) {
367
-            throw new ExpectingATEXT();
368
-        }
369
-
370
-        if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
371
-            throw new DomainHyphened();
372
-        }
373
-
374
-        if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH
375
-            && $this->lexer->isNextToken(EmailLexer::GENERIC)) {
376
-            throw new ExpectingATEXT();
377
-        }
378
-    }
379
-
380
-    /**
381
-     * @return bool
382
-     */
383
-    protected function hasBrackets()
384
-    {
385
-        if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) {
386
-            return false;
387
-        }
388
-
389
-        try {
390
-            $this->lexer->find(EmailLexer::S_CLOSEBRACKET);
391
-        } catch (\RuntimeException $e) {
392
-            throw new ExpectingDomainLiteralClose();
393
-        }
394
-
395
-        return true;
396
-    }
397
-
398
-    /**
399
-     * @param string $label
400
-     */
401
-    protected function checkLabelLength($label)
402
-    {
403
-        if ($this->isLabelTooLong($label)) {
404
-            $this->warnings[LabelTooLong::CODE] = new LabelTooLong();
405
-        }
406
-    }
407
-
408
-    /**
409
-     * @param string $label
410
-     * @return bool
411
-     */
412
-    private function isLabelTooLong($label)
413
-    {
414
-        if (preg_match('/[^\x00-\x7F]/', $label)) {
415
-            idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
416
-
417
-            return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
418
-        }
419
-
420
-        return strlen($label) > self::LABEL_MAX_LENGTH;
421
-    }
422
-
423
-    protected function parseDomainComments()
424
-    {
425
-        $this->isUnclosedComment();
426
-        while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
427
-            $this->warnEscaping();
428
-            $this->lexer->moveNext();
429
-        }
430
-
431
-        $this->lexer->moveNext();
432
-        if ($this->lexer->isNextToken(EmailLexer::S_DOT)) {
433
-            throw new ExpectingATEXT();
434
-        }
435
-    }
436
-
437
-    protected function addTLDWarnings()
438
-    {
439
-        if ($this->warnings[DomainLiteral::CODE]) {
440
-            $this->warnings[TLD::CODE] = new TLD();
441
-        }
442
-    }
37
+	const DOMAIN_MAX_LENGTH = 254;
38
+	const LABEL_MAX_LENGTH = 63;
39
+
40
+	/**
41
+	 * @var string
42
+	 */
43
+	protected $domainPart = '';
44
+
45
+	public function parse($domainPart)
46
+	{
47
+		$this->lexer->moveNext();
48
+
49
+		$this->performDomainStartChecks();
50
+
51
+		$domain = $this->doParseDomainPart();
52
+
53
+		$prev = $this->lexer->getPrevious();
54
+		$length = strlen($domain);
55
+
56
+		if ($prev['type'] === EmailLexer::S_DOT) {
57
+			throw new DotAtEnd();
58
+		}
59
+		if ($prev['type'] === EmailLexer::S_HYPHEN) {
60
+			throw new DomainHyphened();
61
+		}
62
+		if ($length > self::DOMAIN_MAX_LENGTH) {
63
+			$this->warnings[DomainTooLong::CODE] = new DomainTooLong();
64
+		}
65
+		if ($prev['type'] === EmailLexer::S_CR) {
66
+			throw new CRLFAtTheEnd();
67
+		}
68
+		$this->domainPart = $domain;
69
+	}
70
+
71
+	private function performDomainStartChecks()
72
+	{
73
+		$this->checkInvalidTokensAfterAT();
74
+		$this->checkEmptyDomain();
75
+
76
+		if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
77
+			$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
78
+			$this->parseDomainComments();
79
+		}
80
+	}
81
+
82
+	private function checkEmptyDomain()
83
+	{
84
+		$thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY ||
85
+			($this->lexer->token['type'] === EmailLexer::S_SP &&
86
+			!$this->lexer->isNextToken(EmailLexer::GENERIC));
87
+
88
+		if ($thereIsNoDomain) {
89
+			throw new NoDomainPart();
90
+		}
91
+	}
92
+
93
+	private function checkInvalidTokensAfterAT()
94
+	{
95
+		if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
96
+			throw new DotAtStart();
97
+		}
98
+		if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
99
+			throw new DomainHyphened();
100
+		}
101
+	}
102
+
103
+	/**
104
+	 * @return string
105
+	 */
106
+	public function getDomainPart()
107
+	{
108
+		return $this->domainPart;
109
+	}
110
+
111
+	/**
112
+	 * @param string $addressLiteral
113
+	 * @param int $maxGroups
114
+	 */
115
+	public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
116
+	{
117
+		$prev = $this->lexer->getPrevious();
118
+		if ($prev['type'] === EmailLexer::S_COLON) {
119
+			$this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd();
120
+		}
121
+
122
+		$IPv6       = substr($addressLiteral, 5);
123
+		//Daniel Marschall's new IPv6 testing strategy
124
+		$matchesIP  = explode(':', $IPv6);
125
+		$groupCount = count($matchesIP);
126
+		$colons     = strpos($IPv6, '::');
127
+
128
+		if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) {
129
+			$this->warnings[IPV6BadChar::CODE] = new IPV6BadChar();
130
+		}
131
+
132
+		if ($colons === false) {
133
+			// We need exactly the right number of groups
134
+			if ($groupCount !== $maxGroups) {
135
+				$this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount();
136
+			}
137
+			return;
138
+		}
139
+
140
+		if ($colons !== strrpos($IPv6, '::')) {
141
+			$this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon();
142
+			return;
143
+		}
144
+
145
+		if ($colons === 0 || $colons === (strlen($IPv6) - 2)) {
146
+			// RFC 4291 allows :: at the start or end of an address
147
+			//with 7 other groups in addition
148
+			++$maxGroups;
149
+		}
150
+
151
+		if ($groupCount > $maxGroups) {
152
+			$this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups();
153
+		} elseif ($groupCount === $maxGroups) {
154
+			$this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated();
155
+		}
156
+	}
157
+
158
+	/**
159
+	 * @return string
160
+	 */
161
+	protected function doParseDomainPart()
162
+	{
163
+		$domain = '';
164
+		$label = '';
165
+		$openedParenthesis = 0;
166
+		do {
167
+			$prev = $this->lexer->getPrevious();
168
+
169
+			$this->checkNotAllowedChars($this->lexer->token);
170
+
171
+			if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
172
+				$this->parseComments();
173
+				$openedParenthesis += $this->getOpenedParenthesis();
174
+				$this->lexer->moveNext();
175
+				$tmpPrev = $this->lexer->getPrevious();
176
+				if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
177
+					$openedParenthesis--;
178
+				}
179
+			}
180
+			if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
181
+				if ($openedParenthesis === 0) {
182
+					throw new UnopenedComment();
183
+				} else {
184
+					$openedParenthesis--;
185
+				}
186
+			}
187
+
188
+			$this->checkConsecutiveDots();
189
+			$this->checkDomainPartExceptions($prev);
190
+
191
+			if ($this->hasBrackets()) {
192
+				$this->parseDomainLiteral();
193
+			}
194
+
195
+			if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
196
+				$this->checkLabelLength($label);
197
+				$label = '';
198
+			} else {
199
+				$label .= $this->lexer->token['value'];
200
+			}
201
+
202
+			if ($this->isFWS()) {
203
+				$this->parseFWS();
204
+			}
205
+
206
+			$domain .= $this->lexer->token['value'];
207
+			$this->lexer->moveNext();
208
+			if ($this->lexer->token['type'] === EmailLexer::S_SP) {
209
+				throw new CharNotAllowed();
210
+			}
211
+		} while (null !== $this->lexer->token['type']);
212
+
213
+		$this->checkLabelLength($label);
214
+
215
+		return $domain;
216
+	}
217
+
218
+	private function checkNotAllowedChars(array $token)
219
+	{
220
+		$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
221
+		if (isset($notAllowed[$token['type']])) {
222
+			throw new CharNotAllowed();
223
+		}
224
+	}
225
+
226
+	/**
227
+	 * @return string|false
228
+	 */
229
+	protected function parseDomainLiteral()
230
+	{
231
+		if ($this->lexer->isNextToken(EmailLexer::S_COLON)) {
232
+			$this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart();
233
+		}
234
+		if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) {
235
+			$lexer = clone $this->lexer;
236
+			$lexer->moveNext();
237
+			if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) {
238
+				$this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart();
239
+			}
240
+		}
241
+
242
+		return $this->doParseDomainLiteral();
243
+	}
244
+
245
+	/**
246
+	 * @return string|false
247
+	 */
248
+	protected function doParseDomainLiteral()
249
+	{
250
+		$IPv6TAG = false;
251
+		$addressLiteral = '';
252
+		do {
253
+			if ($this->lexer->token['type'] === EmailLexer::C_NUL) {
254
+				throw new ExpectingDTEXT();
255
+			}
256
+
257
+			if ($this->lexer->token['type'] === EmailLexer::INVALID ||
258
+				$this->lexer->token['type'] === EmailLexer::C_DEL   ||
259
+				$this->lexer->token['type'] === EmailLexer::S_LF
260
+			) {
261
+				$this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT();
262
+			}
263
+
264
+			if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENQBRACKET, EmailLexer::S_OPENBRACKET))) {
265
+				throw new ExpectingDTEXT();
266
+			}
267
+
268
+			if ($this->lexer->isNextTokenAny(
269
+				array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF)
270
+			)) {
271
+				$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
272
+				$this->parseFWS();
273
+			}
274
+
275
+			if ($this->lexer->isNextToken(EmailLexer::S_CR)) {
276
+				throw new CRNoLF();
277
+			}
278
+
279
+			if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) {
280
+				$this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT();
281
+				$addressLiteral .= $this->lexer->token['value'];
282
+				$this->lexer->moveNext();
283
+				$this->validateQuotedPair();
284
+			}
285
+			if ($this->lexer->token['type'] === EmailLexer::S_IPV6TAG) {
286
+				$IPv6TAG = true;
287
+			}
288
+			if ($this->lexer->token['type'] === EmailLexer::S_CLOSEQBRACKET) {
289
+				break;
290
+			}
291
+
292
+			$addressLiteral .= $this->lexer->token['value'];
293
+
294
+		} while ($this->lexer->moveNext());
295
+
296
+		$addressLiteral = str_replace('[', '', $addressLiteral);
297
+		$addressLiteral = $this->checkIPV4Tag($addressLiteral);
298
+
299
+		if (false === $addressLiteral) {
300
+			return $addressLiteral;
301
+		}
302
+
303
+		if (!$IPv6TAG) {
304
+			$this->warnings[DomainLiteral::CODE] = new DomainLiteral();
305
+			return $addressLiteral;
306
+		}
307
+
308
+		$this->warnings[AddressLiteral::CODE] = new AddressLiteral();
309
+
310
+		$this->checkIPV6Tag($addressLiteral);
311
+
312
+		return $addressLiteral;
313
+	}
314
+
315
+	/**
316
+	 * @param string $addressLiteral
317
+	 *
318
+	 * @return string|false
319
+	 */
320
+	protected function checkIPV4Tag($addressLiteral)
321
+	{
322
+		$matchesIP  = array();
323
+
324
+		// Extract IPv4 part from the end of the address-literal (if there is one)
325
+		if (preg_match(
326
+			'/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/',
327
+			$addressLiteral,
328
+			$matchesIP
329
+		) > 0
330
+		) {
331
+			$index = strrpos($addressLiteral, $matchesIP[0]);
332
+			if ($index === 0) {
333
+				$this->warnings[AddressLiteral::CODE] = new AddressLiteral();
334
+				return false;
335
+			}
336
+			// Convert IPv4 part to IPv6 format for further testing
337
+			$addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0';
338
+		}
339
+
340
+		return $addressLiteral;
341
+	}
342
+
343
+	protected function checkDomainPartExceptions(array $prev)
344
+	{
345
+		$invalidDomainTokens = array(
346
+			EmailLexer::S_DQUOTE => true,
347
+			EmailLexer::S_SQUOTE => true,
348
+			EmailLexer::S_BACKTICK => true,
349
+			EmailLexer::S_SEMICOLON => true,
350
+			EmailLexer::S_GREATERTHAN => true,
351
+			EmailLexer::S_LOWERTHAN => true,
352
+		);
353
+
354
+		if (isset($invalidDomainTokens[$this->lexer->token['type']])) {
355
+			throw new ExpectingATEXT();
356
+		}
357
+
358
+		if ($this->lexer->token['type'] === EmailLexer::S_COMMA) {
359
+			throw new CommaInDomain();
360
+		}
361
+
362
+		if ($this->lexer->token['type'] === EmailLexer::S_AT) {
363
+			throw new ConsecutiveAt();
364
+		}
365
+
366
+		if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) {
367
+			throw new ExpectingATEXT();
368
+		}
369
+
370
+		if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
371
+			throw new DomainHyphened();
372
+		}
373
+
374
+		if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH
375
+			&& $this->lexer->isNextToken(EmailLexer::GENERIC)) {
376
+			throw new ExpectingATEXT();
377
+		}
378
+	}
379
+
380
+	/**
381
+	 * @return bool
382
+	 */
383
+	protected function hasBrackets()
384
+	{
385
+		if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) {
386
+			return false;
387
+		}
388
+
389
+		try {
390
+			$this->lexer->find(EmailLexer::S_CLOSEBRACKET);
391
+		} catch (\RuntimeException $e) {
392
+			throw new ExpectingDomainLiteralClose();
393
+		}
394
+
395
+		return true;
396
+	}
397
+
398
+	/**
399
+	 * @param string $label
400
+	 */
401
+	protected function checkLabelLength($label)
402
+	{
403
+		if ($this->isLabelTooLong($label)) {
404
+			$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
405
+		}
406
+	}
407
+
408
+	/**
409
+	 * @param string $label
410
+	 * @return bool
411
+	 */
412
+	private function isLabelTooLong($label)
413
+	{
414
+		if (preg_match('/[^\x00-\x7F]/', $label)) {
415
+			idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);
416
+
417
+			return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
418
+		}
419
+
420
+		return strlen($label) > self::LABEL_MAX_LENGTH;
421
+	}
422
+
423
+	protected function parseDomainComments()
424
+	{
425
+		$this->isUnclosedComment();
426
+		while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
427
+			$this->warnEscaping();
428
+			$this->lexer->moveNext();
429
+		}
430
+
431
+		$this->lexer->moveNext();
432
+		if ($this->lexer->isNextToken(EmailLexer::S_DOT)) {
433
+			throw new ExpectingATEXT();
434
+		}
435
+	}
436
+
437
+	protected function addTLDWarnings()
438
+	{
439
+		if ($this->warnings[DomainLiteral::CODE]) {
440
+			$this->warnings[TLD::CODE] = new TLD();
441
+		}
442
+	}
443 443
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
             }
256 256
 
257 257
             if ($this->lexer->token['type'] === EmailLexer::INVALID ||
258
-                $this->lexer->token['type'] === EmailLexer::C_DEL   ||
258
+                $this->lexer->token['type'] === EmailLexer::C_DEL ||
259 259
                 $this->lexer->token['type'] === EmailLexer::S_LF
260 260
             ) {
261 261
                 $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT();
@@ -319,7 +319,7 @@  discard block
 block discarded – undo
319 319
      */
320 320
     protected function checkIPV4Tag($addressLiteral)
321 321
     {
322
-        $matchesIP  = array();
322
+        $matchesIP = array();
323 323
 
324 324
         // Extract IPv4 part from the end of the address-literal (if there is one)
325 325
         if (preg_match(
@@ -334,7 +334,7 @@  discard block
 block discarded – undo
334 334
                 return false;
335 335
             }
336 336
             // Convert IPv4 part to IPv6 format for further testing
337
-            $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0';
337
+            $addressLiteral = substr($addressLiteral, 0, (int) $index).'0:0';
338 338
         }
339 339
 
340 340
         return $addressLiteral;
Please login to merge, or discard this patch.
swiftmailer/egulias/email-validator/EmailValidator/EmailValidator.php 1 patch
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -7,61 +7,61 @@
 block discarded – undo
7 7
 
8 8
 class EmailValidator
9 9
 {
10
-    /**
11
-     * @var EmailLexer
12
-     */
13
-    private $lexer;
10
+	/**
11
+	 * @var EmailLexer
12
+	 */
13
+	private $lexer;
14 14
 
15
-    /**
16
-     * @var Warning\Warning[]
17
-     */
18
-    protected $warnings = [];
15
+	/**
16
+	 * @var Warning\Warning[]
17
+	 */
18
+	protected $warnings = [];
19 19
 
20
-    /**
21
-     * @var InvalidEmail|null
22
-     */
23
-    protected $error;
20
+	/**
21
+	 * @var InvalidEmail|null
22
+	 */
23
+	protected $error;
24 24
 
25
-    public function __construct()
26
-    {
27
-        $this->lexer = new EmailLexer();
28
-    }
25
+	public function __construct()
26
+	{
27
+		$this->lexer = new EmailLexer();
28
+	}
29 29
 
30
-    /**
31
-     * @param string          $email
32
-     * @param EmailValidation $emailValidation
33
-     * @return bool
34
-     */
35
-    public function isValid($email, EmailValidation $emailValidation)
36
-    {
37
-        $isValid = $emailValidation->isValid($email, $this->lexer);
38
-        $this->warnings = $emailValidation->getWarnings();
39
-        $this->error = $emailValidation->getError();
30
+	/**
31
+	 * @param string          $email
32
+	 * @param EmailValidation $emailValidation
33
+	 * @return bool
34
+	 */
35
+	public function isValid($email, EmailValidation $emailValidation)
36
+	{
37
+		$isValid = $emailValidation->isValid($email, $this->lexer);
38
+		$this->warnings = $emailValidation->getWarnings();
39
+		$this->error = $emailValidation->getError();
40 40
 
41
-        return $isValid;
42
-    }
41
+		return $isValid;
42
+	}
43 43
 
44
-    /**
45
-     * @return boolean
46
-     */
47
-    public function hasWarnings()
48
-    {
49
-        return !empty($this->warnings);
50
-    }
44
+	/**
45
+	 * @return boolean
46
+	 */
47
+	public function hasWarnings()
48
+	{
49
+		return !empty($this->warnings);
50
+	}
51 51
 
52
-    /**
53
-     * @return array
54
-     */
55
-    public function getWarnings()
56
-    {
57
-        return $this->warnings;
58
-    }
52
+	/**
53
+	 * @return array
54
+	 */
55
+	public function getWarnings()
56
+	{
57
+		return $this->warnings;
58
+	}
59 59
 
60
-    /**
61
-     * @return InvalidEmail|null
62
-     */
63
-    public function getError()
64
-    {
65
-        return $this->error;
66
-    }
60
+	/**
61
+	 * @return InvalidEmail|null
62
+	 */
63
+	public function getError()
64
+	{
65
+		return $this->error;
66
+	}
67 67
 }
Please login to merge, or discard this patch.
htdocs/includes/swiftmailer/egulias/email-validator/AutoLoader.php 2 patches
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -9,74 +9,74 @@
 block discarded – undo
9 9
  */
10 10
 class EguliasAutoLoader
11 11
 {
12
-    /**
13
-     * @var string The namespace prefix for this instance.
14
-     */
15
-    protected $namespace = '';
12
+	/**
13
+	 * @var string The namespace prefix for this instance.
14
+	 */
15
+	protected $namespace = '';
16 16
 
17
-    /**
18
-     * @var string The filesystem prefix to use for this instance
19
-     */
20
-    protected $path = '';
17
+	/**
18
+	 * @var string The filesystem prefix to use for this instance
19
+	 */
20
+	protected $path = '';
21 21
 
22
-    /**
23
-     * Build the instance of the autoloader
24
-     *
25
-     * @param string $namespace The prefixed namespace this instance will load
26
-     * @param string $path      The filesystem path to the root of the namespace
27
-     */
28
-    public function __construct($namespace, $path)
29
-    {
30
-        $this->namespace = ltrim($namespace, '\\');
31
-        $this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
32
-    }
22
+	/**
23
+	 * Build the instance of the autoloader
24
+	 *
25
+	 * @param string $namespace The prefixed namespace this instance will load
26
+	 * @param string $path      The filesystem path to the root of the namespace
27
+	 */
28
+	public function __construct($namespace, $path)
29
+	{
30
+		$this->namespace = ltrim($namespace, '\\');
31
+		$this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
32
+	}
33 33
 
34
-    /**
35
-     * Try to load a class
36
-     *
37
-     * @param string $class The class name to load
38
-     *
39
-     * @return boolean If the loading was successful
40
-     */
41
-    public function load($class)
42
-    {
43
-        $class = ltrim($class, '\\');
44
-        if (strpos($class, $this->namespace) === 0) {
45
-            $nsparts = explode('\\', $class);
46
-            $class = array_pop($nsparts);
47
-            $path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/';
48
-            $max=count($nsparts);
49
-            for ($i=2; $i<$max;$i++) {
50
-                $path .= $nsparts[$i].'/';
51
-            }
52
-            $nsparts = array();
53
-            $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
54
-            if (file_exists($path)) {
55
-                require $path;
56
-                return true;
57
-            }
58
-        }
34
+	/**
35
+	 * Try to load a class
36
+	 *
37
+	 * @param string $class The class name to load
38
+	 *
39
+	 * @return boolean If the loading was successful
40
+	 */
41
+	public function load($class)
42
+	{
43
+		$class = ltrim($class, '\\');
44
+		if (strpos($class, $this->namespace) === 0) {
45
+			$nsparts = explode('\\', $class);
46
+			$class = array_pop($nsparts);
47
+			$path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/';
48
+			$max=count($nsparts);
49
+			for ($i=2; $i<$max;$i++) {
50
+				$path .= $nsparts[$i].'/';
51
+			}
52
+			$nsparts = array();
53
+			$path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
54
+			if (file_exists($path)) {
55
+				require $path;
56
+				return true;
57
+			}
58
+		}
59 59
 
60
-        return false;
61
-    }
60
+		return false;
61
+	}
62 62
 
63
-    /**
64
-     * Register the autoloader to PHP
65
-     *
66
-     * @return boolean The status of the registration
67
-     */
68
-    public function register()
69
-    {
70
-        return spl_autoload_register(array($this, 'load'));
71
-    }
63
+	/**
64
+	 * Register the autoloader to PHP
65
+	 *
66
+	 * @return boolean The status of the registration
67
+	 */
68
+	public function register()
69
+	{
70
+		return spl_autoload_register(array($this, 'load'));
71
+	}
72 72
 
73
-    /**
74
-     * Unregister the autoloader to PHP
75
-     *
76
-     * @return boolean The status of the unregistration
77
-     */
78
-    public function unregister()
79
-    {
80
-        return spl_autoload_unregister(array($this, 'load'));
81
-    }
73
+	/**
74
+	 * Unregister the autoloader to PHP
75
+	 *
76
+	 * @return boolean The status of the unregistration
77
+	 */
78
+	public function unregister()
79
+	{
80
+		return spl_autoload_unregister(array($this, 'load'));
81
+	}
82 82
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -28,7 +28,7 @@  discard block
 block discarded – undo
28 28
     public function __construct($namespace, $path)
29 29
     {
30 30
         $this->namespace = ltrim($namespace, '\\');
31
-        $this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
31
+        $this->path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
32 32
     }
33 33
 
34 34
     /**
@@ -44,13 +44,13 @@  discard block
 block discarded – undo
44 44
         if (strpos($class, $this->namespace) === 0) {
45 45
             $nsparts = explode('\\', $class);
46 46
             $class = array_pop($nsparts);
47
-            $path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/';
48
-            $max=count($nsparts);
49
-            for ($i=2; $i<$max;$i++) {
47
+            $path = $this->path.'swiftmailer/egulias/email-validator/EmailValidator/';
48
+            $max = count($nsparts);
49
+            for ($i = 2; $i < $max; $i++) {
50 50
                 $path .= $nsparts[$i].'/';
51 51
             }
52 52
             $nsparts = array();
53
-            $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
53
+            $path .= str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
54 54
             if (file_exists($path)) {
55 55
                 require $path;
56 56
                 return true;
Please login to merge, or discard this patch.
includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php 2 patches
Indentation   +300 added lines, -300 removed lines patch added patch discarded remove patch
@@ -29,304 +29,304 @@
 block discarded – undo
29 29
  */
30 30
 abstract class AbstractLexer
31 31
 {
32
-    /**
33
-     * Lexer original input string.
34
-     *
35
-     * @var string
36
-     */
37
-    private $input;
38
-
39
-    /**
40
-     * Array of scanned tokens.
41
-     *
42
-     * Each token is an associative array containing three items:
43
-     *  - 'value'    : the string value of the token in the input string
44
-     *  - 'type'     : the type of the token (identifier, numeric, string, input
45
-     *                 parameter, none)
46
-     *  - 'position' : the position of the token in the input string
47
-     *
48
-     * @var array
49
-     */
50
-    private $tokens = array();
51
-
52
-    /**
53
-     * Current lexer position in input string.
54
-     *
55
-     * @var integer
56
-     */
57
-    private $position = 0;
58
-
59
-    /**
60
-     * Current peek of current lexer position.
61
-     *
62
-     * @var integer
63
-     */
64
-    private $peek = 0;
65
-
66
-    /**
67
-     * The next token in the input.
68
-     *
69
-     * @var array
70
-     */
71
-    public $lookahead;
72
-
73
-    /**
74
-     * The last matched/seen token.
75
-     *
76
-     * @var array
77
-     */
78
-    public $token;
79
-
80
-    /**
81
-     * Sets the input data to be tokenized.
82
-     *
83
-     * The Lexer is immediately reset and the new input tokenized.
84
-     * Any unprocessed tokens from any previous input are lost.
85
-     *
86
-     * @param string $input The input to be tokenized.
87
-     *
88
-     * @return void
89
-     */
90
-    public function setInput($input)
91
-    {
92
-        $this->input  = $input;
93
-        $this->tokens = array();
94
-
95
-        $this->reset();
96
-        $this->scan($input);
97
-    }
98
-
99
-    /**
100
-     * Resets the lexer.
101
-     *
102
-     * @return void
103
-     */
104
-    public function reset()
105
-    {
106
-        $this->lookahead = null;
107
-        $this->token = null;
108
-        $this->peek = 0;
109
-        $this->position = 0;
110
-    }
111
-
112
-    /**
113
-     * Resets the peek pointer to 0.
114
-     *
115
-     * @return void
116
-     */
117
-    public function resetPeek()
118
-    {
119
-        $this->peek = 0;
120
-    }
121
-
122
-    /**
123
-     * Resets the lexer position on the input to the given position.
124
-     *
125
-     * @param integer $position Position to place the lexical scanner.
126
-     *
127
-     * @return void
128
-     */
129
-    public function resetPosition($position = 0)
130
-    {
131
-        $this->position = $position;
132
-    }
133
-
134
-    /**
135
-     * Retrieve the original lexer's input until a given position. 
136
-     *
137
-     * @param integer $position
138
-     *
139
-     * @return string
140
-     */
141
-    public function getInputUntilPosition($position)
142
-    {
143
-        return substr($this->input, 0, $position);
144
-    }
145
-
146
-    /**
147
-     * Checks whether a given token matches the current lookahead.
148
-     *
149
-     * @param integer|string $token
150
-     *
151
-     * @return boolean
152
-     */
153
-    public function isNextToken($token)
154
-    {
155
-        return null !== $this->lookahead && $this->lookahead['type'] === $token;
156
-    }
157
-
158
-    /**
159
-     * Checks whether any of the given tokens matches the current lookahead.
160
-     *
161
-     * @param array $tokens
162
-     *
163
-     * @return boolean
164
-     */
165
-    public function isNextTokenAny(array $tokens)
166
-    {
167
-        return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
168
-    }
169
-
170
-    /**
171
-     * Moves to the next token in the input string.
172
-     *
173
-     * @return boolean
174
-     */
175
-    public function moveNext()
176
-    {
177
-        $this->peek = 0;
178
-        $this->token = $this->lookahead;
179
-        $this->lookahead = (isset($this->tokens[$this->position]))
180
-            ? $this->tokens[$this->position++] : null;
181
-
182
-        return $this->lookahead !== null;
183
-    }
184
-
185
-    /**
186
-     * Tells the lexer to skip input tokens until it sees a token with the given value.
187
-     *
188
-     * @param string $type The token type to skip until.
189
-     *
190
-     * @return void
191
-     */
192
-    public function skipUntil($type)
193
-    {
194
-        while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
195
-            $this->moveNext();
196
-        }
197
-    }
198
-
199
-    /**
200
-     * Checks if given value is identical to the given token.
201
-     *
202
-     * @param mixed   $value
203
-     * @param integer $token
204
-     *
205
-     * @return boolean
206
-     */
207
-    public function isA($value, $token)
208
-    {
209
-        return $this->getType($value) === $token;
210
-    }
211
-
212
-    /**
213
-     * Moves the lookahead token forward.
214
-     *
215
-     * @return array|null The next token or NULL if there are no more tokens ahead.
216
-     */
217
-    public function peek()
218
-    {
219
-        if (isset($this->tokens[$this->position + $this->peek])) {
220
-            return $this->tokens[$this->position + $this->peek++];
221
-        } else {
222
-            return null;
223
-        }
224
-    }
225
-
226
-    /**
227
-     * Peeks at the next token, returns it and immediately resets the peek.
228
-     *
229
-     * @return array|null The next token or NULL if there are no more tokens ahead.
230
-     */
231
-    public function glimpse()
232
-    {
233
-        $peek = $this->peek();
234
-        $this->peek = 0;
235
-        return $peek;
236
-    }
237
-
238
-    /**
239
-     * Scans the input string for tokens.
240
-     *
241
-     * @param string $input A query string.
242
-     *
243
-     * @return void
244
-     */
245
-    protected function scan($input)
246
-    {
247
-        static $regex;
248
-
249
-        if ( ! isset($regex)) {
250
-            $regex = sprintf(
251
-                '/(%s)|%s/%s',
252
-                implode(')|(', $this->getCatchablePatterns()),
253
-                implode('|', $this->getNonCatchablePatterns()),
254
-                $this->getModifiers()
255
-            );
256
-        }
257
-
258
-        $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
259
-        $matches = preg_split($regex, $input, -1, $flags);
260
-
261
-        if (false === $matches) {
262
-            // Work around https://bugs.php.net/78122
263
-            $matches = array(array($input, 0));
264
-        }
265
-
266
-        foreach ($matches as $match) {
267
-            // Must remain before 'value' assignment since it can change content
268
-            $type = $this->getType($match[0]);
269
-
270
-            $this->tokens[] = array(
271
-                'value' => $match[0],
272
-                'type'  => $type,
273
-                'position' => $match[1],
274
-            );
275
-        }
276
-    }
277
-
278
-    /**
279
-     * Gets the literal for a given token.
280
-     *
281
-     * @param integer $token
282
-     *
283
-     * @return string
284
-     */
285
-    public function getLiteral($token)
286
-    {
287
-        $className = get_class($this);
288
-        $reflClass = new \ReflectionClass($className);
289
-        $constants = $reflClass->getConstants();
290
-
291
-        foreach ($constants as $name => $value) {
292
-            if ($value === $token) {
293
-                return $className . '::' . $name;
294
-            }
295
-        }
296
-
297
-        return $token;
298
-    }
299
-
300
-    /**
301
-     * Regex modifiers
302
-     *
303
-     * @return string
304
-     */
305
-    protected function getModifiers()
306
-    {
307
-        return 'i';
308
-    }
309
-
310
-    /**
311
-     * Lexical catchable patterns.
312
-     *
313
-     * @return array
314
-     */
315
-    abstract protected function getCatchablePatterns();
316
-
317
-    /**
318
-     * Lexical non-catchable patterns.
319
-     *
320
-     * @return array
321
-     */
322
-    abstract protected function getNonCatchablePatterns();
323
-
324
-    /**
325
-     * Retrieve token type. Also processes the token value if necessary.
326
-     *
327
-     * @param string $value
328
-     *
329
-     * @return integer
330
-     */
331
-    abstract protected function getType(&$value);
32
+	/**
33
+	 * Lexer original input string.
34
+	 *
35
+	 * @var string
36
+	 */
37
+	private $input;
38
+
39
+	/**
40
+	 * Array of scanned tokens.
41
+	 *
42
+	 * Each token is an associative array containing three items:
43
+	 *  - 'value'    : the string value of the token in the input string
44
+	 *  - 'type'     : the type of the token (identifier, numeric, string, input
45
+	 *                 parameter, none)
46
+	 *  - 'position' : the position of the token in the input string
47
+	 *
48
+	 * @var array
49
+	 */
50
+	private $tokens = array();
51
+
52
+	/**
53
+	 * Current lexer position in input string.
54
+	 *
55
+	 * @var integer
56
+	 */
57
+	private $position = 0;
58
+
59
+	/**
60
+	 * Current peek of current lexer position.
61
+	 *
62
+	 * @var integer
63
+	 */
64
+	private $peek = 0;
65
+
66
+	/**
67
+	 * The next token in the input.
68
+	 *
69
+	 * @var array
70
+	 */
71
+	public $lookahead;
72
+
73
+	/**
74
+	 * The last matched/seen token.
75
+	 *
76
+	 * @var array
77
+	 */
78
+	public $token;
79
+
80
+	/**
81
+	 * Sets the input data to be tokenized.
82
+	 *
83
+	 * The Lexer is immediately reset and the new input tokenized.
84
+	 * Any unprocessed tokens from any previous input are lost.
85
+	 *
86
+	 * @param string $input The input to be tokenized.
87
+	 *
88
+	 * @return void
89
+	 */
90
+	public function setInput($input)
91
+	{
92
+		$this->input  = $input;
93
+		$this->tokens = array();
94
+
95
+		$this->reset();
96
+		$this->scan($input);
97
+	}
98
+
99
+	/**
100
+	 * Resets the lexer.
101
+	 *
102
+	 * @return void
103
+	 */
104
+	public function reset()
105
+	{
106
+		$this->lookahead = null;
107
+		$this->token = null;
108
+		$this->peek = 0;
109
+		$this->position = 0;
110
+	}
111
+
112
+	/**
113
+	 * Resets the peek pointer to 0.
114
+	 *
115
+	 * @return void
116
+	 */
117
+	public function resetPeek()
118
+	{
119
+		$this->peek = 0;
120
+	}
121
+
122
+	/**
123
+	 * Resets the lexer position on the input to the given position.
124
+	 *
125
+	 * @param integer $position Position to place the lexical scanner.
126
+	 *
127
+	 * @return void
128
+	 */
129
+	public function resetPosition($position = 0)
130
+	{
131
+		$this->position = $position;
132
+	}
133
+
134
+	/**
135
+	 * Retrieve the original lexer's input until a given position. 
136
+	 *
137
+	 * @param integer $position
138
+	 *
139
+	 * @return string
140
+	 */
141
+	public function getInputUntilPosition($position)
142
+	{
143
+		return substr($this->input, 0, $position);
144
+	}
145
+
146
+	/**
147
+	 * Checks whether a given token matches the current lookahead.
148
+	 *
149
+	 * @param integer|string $token
150
+	 *
151
+	 * @return boolean
152
+	 */
153
+	public function isNextToken($token)
154
+	{
155
+		return null !== $this->lookahead && $this->lookahead['type'] === $token;
156
+	}
157
+
158
+	/**
159
+	 * Checks whether any of the given tokens matches the current lookahead.
160
+	 *
161
+	 * @param array $tokens
162
+	 *
163
+	 * @return boolean
164
+	 */
165
+	public function isNextTokenAny(array $tokens)
166
+	{
167
+		return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
168
+	}
169
+
170
+	/**
171
+	 * Moves to the next token in the input string.
172
+	 *
173
+	 * @return boolean
174
+	 */
175
+	public function moveNext()
176
+	{
177
+		$this->peek = 0;
178
+		$this->token = $this->lookahead;
179
+		$this->lookahead = (isset($this->tokens[$this->position]))
180
+			? $this->tokens[$this->position++] : null;
181
+
182
+		return $this->lookahead !== null;
183
+	}
184
+
185
+	/**
186
+	 * Tells the lexer to skip input tokens until it sees a token with the given value.
187
+	 *
188
+	 * @param string $type The token type to skip until.
189
+	 *
190
+	 * @return void
191
+	 */
192
+	public function skipUntil($type)
193
+	{
194
+		while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
195
+			$this->moveNext();
196
+		}
197
+	}
198
+
199
+	/**
200
+	 * Checks if given value is identical to the given token.
201
+	 *
202
+	 * @param mixed   $value
203
+	 * @param integer $token
204
+	 *
205
+	 * @return boolean
206
+	 */
207
+	public function isA($value, $token)
208
+	{
209
+		return $this->getType($value) === $token;
210
+	}
211
+
212
+	/**
213
+	 * Moves the lookahead token forward.
214
+	 *
215
+	 * @return array|null The next token or NULL if there are no more tokens ahead.
216
+	 */
217
+	public function peek()
218
+	{
219
+		if (isset($this->tokens[$this->position + $this->peek])) {
220
+			return $this->tokens[$this->position + $this->peek++];
221
+		} else {
222
+			return null;
223
+		}
224
+	}
225
+
226
+	/**
227
+	 * Peeks at the next token, returns it and immediately resets the peek.
228
+	 *
229
+	 * @return array|null The next token or NULL if there are no more tokens ahead.
230
+	 */
231
+	public function glimpse()
232
+	{
233
+		$peek = $this->peek();
234
+		$this->peek = 0;
235
+		return $peek;
236
+	}
237
+
238
+	/**
239
+	 * Scans the input string for tokens.
240
+	 *
241
+	 * @param string $input A query string.
242
+	 *
243
+	 * @return void
244
+	 */
245
+	protected function scan($input)
246
+	{
247
+		static $regex;
248
+
249
+		if ( ! isset($regex)) {
250
+			$regex = sprintf(
251
+				'/(%s)|%s/%s',
252
+				implode(')|(', $this->getCatchablePatterns()),
253
+				implode('|', $this->getNonCatchablePatterns()),
254
+				$this->getModifiers()
255
+			);
256
+		}
257
+
258
+		$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
259
+		$matches = preg_split($regex, $input, -1, $flags);
260
+
261
+		if (false === $matches) {
262
+			// Work around https://bugs.php.net/78122
263
+			$matches = array(array($input, 0));
264
+		}
265
+
266
+		foreach ($matches as $match) {
267
+			// Must remain before 'value' assignment since it can change content
268
+			$type = $this->getType($match[0]);
269
+
270
+			$this->tokens[] = array(
271
+				'value' => $match[0],
272
+				'type'  => $type,
273
+				'position' => $match[1],
274
+			);
275
+		}
276
+	}
277
+
278
+	/**
279
+	 * Gets the literal for a given token.
280
+	 *
281
+	 * @param integer $token
282
+	 *
283
+	 * @return string
284
+	 */
285
+	public function getLiteral($token)
286
+	{
287
+		$className = get_class($this);
288
+		$reflClass = new \ReflectionClass($className);
289
+		$constants = $reflClass->getConstants();
290
+
291
+		foreach ($constants as $name => $value) {
292
+			if ($value === $token) {
293
+				return $className . '::' . $name;
294
+			}
295
+		}
296
+
297
+		return $token;
298
+	}
299
+
300
+	/**
301
+	 * Regex modifiers
302
+	 *
303
+	 * @return string
304
+	 */
305
+	protected function getModifiers()
306
+	{
307
+		return 'i';
308
+	}
309
+
310
+	/**
311
+	 * Lexical catchable patterns.
312
+	 *
313
+	 * @return array
314
+	 */
315
+	abstract protected function getCatchablePatterns();
316
+
317
+	/**
318
+	 * Lexical non-catchable patterns.
319
+	 *
320
+	 * @return array
321
+	 */
322
+	abstract protected function getNonCatchablePatterns();
323
+
324
+	/**
325
+	 * Retrieve token type. Also processes the token value if necessary.
326
+	 *
327
+	 * @param string $value
328
+	 *
329
+	 * @return integer
330
+	 */
331
+	abstract protected function getType(&$value);
332 332
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -246,7 +246,7 @@  discard block
 block discarded – undo
246 246
     {
247 247
         static $regex;
248 248
 
249
-        if ( ! isset($regex)) {
249
+        if (!isset($regex)) {
250 250
             $regex = sprintf(
251 251
                 '/(%s)|%s/%s',
252 252
                 implode(')|(', $this->getCatchablePatterns()),
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
 
291 291
         foreach ($constants as $name => $value) {
292 292
             if ($value === $token) {
293
-                return $className . '::' . $name;
293
+                return $className.'::'.$name;
294 294
             }
295 295
         }
296 296
 
Please login to merge, or discard this patch.
htdocs/includes/sabre/psr/log/Psr/Log/LogLevel.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -7,12 +7,12 @@
 block discarded – undo
7 7
  */
8 8
 class LogLevel
9 9
 {
10
-    const EMERGENCY = 'emergency';
11
-    const ALERT     = 'alert';
12
-    const CRITICAL  = 'critical';
13
-    const ERROR     = 'error';
14
-    const WARNING   = 'warning';
15
-    const NOTICE    = 'notice';
16
-    const INFO      = 'info';
17
-    const DEBUG     = 'debug';
10
+	const EMERGENCY = 'emergency';
11
+	const ALERT     = 'alert';
12
+	const CRITICAL  = 'critical';
13
+	const ERROR     = 'error';
14
+	const WARNING   = 'warning';
15
+	const NOTICE    = 'notice';
16
+	const INFO      = 'info';
17
+	const DEBUG     = 'debug';
18 18
 }
Please login to merge, or discard this patch.
htdocs/includes/sabre/psr/log/Psr/Log/LoggerAwareInterface.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -7,12 +7,12 @@
 block discarded – undo
7 7
  */
8 8
 interface LoggerAwareInterface
9 9
 {
10
-    /**
11
-     * Sets a logger instance on the object.
12
-     *
13
-     * @param LoggerInterface $logger
14
-     *
15
-     * @return void
16
-     */
17
-    public function setLogger(LoggerInterface $logger);
10
+	/**
11
+	 * Sets a logger instance on the object.
12
+	 *
13
+	 * @param LoggerInterface $logger
14
+	 *
15
+	 * @return void
16
+	 */
17
+	public function setLogger(LoggerInterface $logger);
18 18
 }
Please login to merge, or discard this patch.
htdocs/includes/sabre/sabre/dav/lib/CalDAV/Plugin.php 2 patches
Indentation   +969 added lines, -969 removed lines patch added patch discarded remove patch
@@ -30,918 +30,918 @@  discard block
 block discarded – undo
30 30
  */
31 31
 class Plugin extends DAV\ServerPlugin
32 32
 {
33
-    /**
34
-     * This is the official CalDAV namespace.
35
-     */
36
-    const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav';
37
-
38
-    /**
39
-     * This is the namespace for the proprietary calendarserver extensions.
40
-     */
41
-    const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
42
-
43
-    /**
44
-     * The hardcoded root for calendar objects. It is unfortunate
45
-     * that we're stuck with it, but it will have to do for now.
46
-     */
47
-    const CALENDAR_ROOT = 'calendars';
48
-
49
-    /**
50
-     * Reference to server object.
51
-     *
52
-     * @var DAV\Server
53
-     */
54
-    protected $server;
55
-
56
-    /**
57
-     * The default PDO storage uses a MySQL MEDIUMBLOB for iCalendar data,
58
-     * which can hold up to 2^24 = 16777216 bytes. This is plenty. We're
59
-     * capping it to 10M here.
60
-     */
61
-    protected $maxResourceSize = 10000000;
62
-
63
-    /**
64
-     * Use this method to tell the server this plugin defines additional
65
-     * HTTP methods.
66
-     *
67
-     * This method is passed a uri. It should only return HTTP methods that are
68
-     * available for the specified uri.
69
-     *
70
-     * @param string $uri
71
-     *
72
-     * @return array
73
-     */
74
-    public function getHTTPMethods($uri)
75
-    {
76
-        // The MKCALENDAR is only available on unmapped uri's, whose
77
-        // parents extend IExtendedCollection
78
-        list($parent, $name) = Uri\split($uri);
79
-
80
-        if ('' === $uri) {
81
-            $parent = '';
82
-        }
83
-
84
-        $node = $this->server->tree->getNodeForPath($parent);
85
-
86
-        if ($node instanceof DAV\IExtendedCollection) {
87
-            try {
88
-                $node->getChild($name);
89
-            } catch (DAV\Exception\NotFound $e) {
90
-                return ['MKCALENDAR'];
91
-            }
92
-        }
93
-
94
-        return [];
95
-    }
96
-
97
-    /**
98
-     * Returns the path to a principal's calendar home.
99
-     *
100
-     * The return url must not end with a slash.
101
-     * This function should return null in case a principal did not have
102
-     * a calendar home.
103
-     *
104
-     * @param string $principalUrl
105
-     *
106
-     * @return string
107
-     */
108
-    public function getCalendarHomeForPrincipal($principalUrl)
109
-    {
110
-        // The default behavior for most sabre/dav servers is that there is a
111
-        // principals root node, which contains users directly under it.
112
-        //
113
-        // This function assumes that there are two components in a principal
114
-        // path. If there's more, we don't return a calendar home. This
115
-        // excludes things like the calendar-proxy-read principal (which it
116
-        // should).
117
-        $parts = explode('/', trim($principalUrl, '/'));
118
-        if (2 !== count($parts)) {
119
-            return;
120
-        }
121
-        if ('principals' !== $parts[0]) {
122
-            return;
123
-        }
124
-
125
-        return self::CALENDAR_ROOT.'/'.$parts[1];
126
-    }
127
-
128
-    /**
129
-     * Returns a list of features for the DAV: HTTP header.
130
-     *
131
-     * @return array
132
-     */
133
-    public function getFeatures()
134
-    {
135
-        return ['calendar-access', 'calendar-proxy'];
136
-    }
137
-
138
-    /**
139
-     * Returns a plugin name.
140
-     *
141
-     * Using this name other plugins will be able to access other plugins
142
-     * using DAV\Server::getPlugin
143
-     *
144
-     * @return string
145
-     */
146
-    public function getPluginName()
147
-    {
148
-        return 'caldav';
149
-    }
150
-
151
-    /**
152
-     * Returns a list of reports this plugin supports.
153
-     *
154
-     * This will be used in the {DAV:}supported-report-set property.
155
-     * Note that you still need to subscribe to the 'report' event to actually
156
-     * implement them
157
-     *
158
-     * @param string $uri
159
-     *
160
-     * @return array
161
-     */
162
-    public function getSupportedReportSet($uri)
163
-    {
164
-        $node = $this->server->tree->getNodeForPath($uri);
165
-
166
-        $reports = [];
167
-        if ($node instanceof ICalendarObjectContainer || $node instanceof ICalendarObject) {
168
-            $reports[] = '{'.self::NS_CALDAV.'}calendar-multiget';
169
-            $reports[] = '{'.self::NS_CALDAV.'}calendar-query';
170
-        }
171
-        if ($node instanceof ICalendar) {
172
-            $reports[] = '{'.self::NS_CALDAV.'}free-busy-query';
173
-        }
174
-        // iCal has a bug where it assumes that sync support is enabled, only
175
-        // if we say we support it on the calendar-home, even though this is
176
-        // not actually the case.
177
-        if ($node instanceof CalendarHome && $this->server->getPlugin('sync')) {
178
-            $reports[] = '{DAV:}sync-collection';
179
-        }
180
-
181
-        return $reports;
182
-    }
183
-
184
-    /**
185
-     * Initializes the plugin.
186
-     */
187
-    public function initialize(DAV\Server $server)
188
-    {
189
-        $this->server = $server;
190
-
191
-        $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']);
192
-        $server->on('report', [$this, 'report']);
193
-        $server->on('propFind', [$this, 'propFind']);
194
-        $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']);
195
-        $server->on('beforeCreateFile', [$this, 'beforeCreateFile']);
196
-        $server->on('beforeWriteContent', [$this, 'beforeWriteContent']);
197
-        $server->on('afterMethod:GET', [$this, 'httpAfterGET']);
198
-        $server->on('getSupportedPrivilegeSet', [$this, 'getSupportedPrivilegeSet']);
199
-
200
-        $server->xml->namespaceMap[self::NS_CALDAV] = 'cal';
201
-        $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs';
202
-
203
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet';
204
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}calendar-query'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarQueryReport';
205
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}calendar-multiget'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarMultiGetReport';
206
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}free-busy-query'] = 'Sabre\\CalDAV\\Xml\\Request\\FreeBusyQueryReport';
207
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}mkcalendar'] = 'Sabre\\CalDAV\\Xml\\Request\\MkCalendar';
208
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Xml\\Property\\ScheduleCalendarTransp';
209
-        $server->xml->elementMap['{'.self::NS_CALDAV.'}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet';
210
-
211
-        $server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar';
212
-
213
-        $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read';
214
-        $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write';
215
-
216
-        array_push($server->protectedProperties,
217
-            '{'.self::NS_CALDAV.'}supported-calendar-component-set',
218
-            '{'.self::NS_CALDAV.'}supported-calendar-data',
219
-            '{'.self::NS_CALDAV.'}max-resource-size',
220
-            '{'.self::NS_CALDAV.'}min-date-time',
221
-            '{'.self::NS_CALDAV.'}max-date-time',
222
-            '{'.self::NS_CALDAV.'}max-instances',
223
-            '{'.self::NS_CALDAV.'}max-attendees-per-instance',
224
-            '{'.self::NS_CALDAV.'}calendar-home-set',
225
-            '{'.self::NS_CALDAV.'}supported-collation-set',
226
-            '{'.self::NS_CALDAV.'}calendar-data',
227
-
228
-            // CalendarServer extensions
229
-            '{'.self::NS_CALENDARSERVER.'}getctag',
230
-            '{'.self::NS_CALENDARSERVER.'}calendar-proxy-read-for',
231
-            '{'.self::NS_CALENDARSERVER.'}calendar-proxy-write-for'
232
-        );
233
-
234
-        if ($aclPlugin = $server->getPlugin('acl')) {
235
-            $aclPlugin->principalSearchPropertySet['{'.self::NS_CALDAV.'}calendar-user-address-set'] = 'Calendar address';
236
-        }
237
-    }
238
-
239
-    /**
240
-     * This functions handles REPORT requests specific to CalDAV.
241
-     *
242
-     * @param string $reportName
243
-     * @param mixed  $report
244
-     * @param mixed  $path
245
-     *
246
-     * @return bool|null
247
-     */
248
-    public function report($reportName, $report, $path)
249
-    {
250
-        switch ($reportName) {
251
-            case '{'.self::NS_CALDAV.'}calendar-multiget':
252
-                $this->server->transactionType = 'report-calendar-multiget';
253
-                $this->calendarMultiGetReport($report);
254
-
255
-                return false;
256
-            case '{'.self::NS_CALDAV.'}calendar-query':
257
-                $this->server->transactionType = 'report-calendar-query';
258
-                $this->calendarQueryReport($report);
259
-
260
-                return false;
261
-            case '{'.self::NS_CALDAV.'}free-busy-query':
262
-                $this->server->transactionType = 'report-free-busy-query';
263
-                $this->freeBusyQueryReport($report);
264
-
265
-                return false;
266
-        }
267
-    }
268
-
269
-    /**
270
-     * This function handles the MKCALENDAR HTTP method, which creates
271
-     * a new calendar.
272
-     *
273
-     * @return bool
274
-     */
275
-    public function httpMkCalendar(RequestInterface $request, ResponseInterface $response)
276
-    {
277
-        $body = $request->getBodyAsString();
278
-        $path = $request->getPath();
279
-
280
-        $properties = [];
281
-
282
-        if ($body) {
283
-            try {
284
-                $mkcalendar = $this->server->xml->expect(
285
-                    '{urn:ietf:params:xml:ns:caldav}mkcalendar',
286
-                    $body
287
-                );
288
-            } catch (\Sabre\Xml\ParseException $e) {
289
-                throw new BadRequest($e->getMessage(), 0, $e);
290
-            }
291
-            $properties = $mkcalendar->getProperties();
292
-        }
293
-
294
-        // iCal abuses MKCALENDAR since iCal 10.9.2 to create server-stored
295
-        // subscriptions. Before that it used MKCOL which was the correct way
296
-        // to do this.
297
-        //
298
-        // If the body had a {DAV:}resourcetype, it means we stumbled upon this
299
-        // request, and we simply use it instead of the pre-defined list.
300
-        if (isset($properties['{DAV:}resourcetype'])) {
301
-            $resourceType = $properties['{DAV:}resourcetype']->getValue();
302
-        } else {
303
-            $resourceType = ['{DAV:}collection', '{urn:ietf:params:xml:ns:caldav}calendar'];
304
-        }
305
-
306
-        $this->server->createCollection($path, new MkCol($resourceType, $properties));
307
-
308
-        $response->setStatus(201);
309
-        $response->setHeader('Content-Length', 0);
310
-
311
-        // This breaks the method chain.
312
-        return false;
313
-    }
314
-
315
-    /**
316
-     * PropFind.
317
-     *
318
-     * This method handler is invoked before any after properties for a
319
-     * resource are fetched. This allows us to add in any CalDAV specific
320
-     * properties.
321
-     */
322
-    public function propFind(DAV\PropFind $propFind, DAV\INode $node)
323
-    {
324
-        $ns = '{'.self::NS_CALDAV.'}';
325
-
326
-        if ($node instanceof ICalendarObjectContainer) {
327
-            $propFind->handle($ns.'max-resource-size', $this->maxResourceSize);
328
-            $propFind->handle($ns.'supported-calendar-data', function () {
329
-                return new Xml\Property\SupportedCalendarData();
330
-            });
331
-            $propFind->handle($ns.'supported-collation-set', function () {
332
-                return new Xml\Property\SupportedCollationSet();
333
-            });
334
-        }
335
-
336
-        if ($node instanceof DAVACL\IPrincipal) {
337
-            $principalUrl = $node->getPrincipalUrl();
338
-
339
-            $propFind->handle('{'.self::NS_CALDAV.'}calendar-home-set', function () use ($principalUrl) {
340
-                $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl);
341
-                if (is_null($calendarHomePath)) {
342
-                    return null;
343
-                }
344
-
345
-                return new LocalHref($calendarHomePath.'/');
346
-            });
347
-            // The calendar-user-address-set property is basically mapped to
348
-            // the {DAV:}alternate-URI-set property.
349
-            $propFind->handle('{'.self::NS_CALDAV.'}calendar-user-address-set', function () use ($node) {
350
-                $addresses = $node->getAlternateUriSet();
351
-                $addresses[] = $this->server->getBaseUri().$node->getPrincipalUrl().'/';
352
-
353
-                return new LocalHref($addresses);
354
-            });
355
-            // For some reason somebody thought it was a good idea to add
356
-            // another one of these properties. We're supporting it too.
357
-            $propFind->handle('{'.self::NS_CALENDARSERVER.'}email-address-set', function () use ($node) {
358
-                $addresses = $node->getAlternateUriSet();
359
-                $emails = [];
360
-                foreach ($addresses as $address) {
361
-                    if ('mailto:' === substr($address, 0, 7)) {
362
-                        $emails[] = substr($address, 7);
363
-                    }
364
-                }
365
-
366
-                return new Xml\Property\EmailAddressSet($emails);
367
-            });
368
-
369
-            // These two properties are shortcuts for ical to easily find
370
-            // other principals this principal has access to.
371
-            $propRead = '{'.self::NS_CALENDARSERVER.'}calendar-proxy-read-for';
372
-            $propWrite = '{'.self::NS_CALENDARSERVER.'}calendar-proxy-write-for';
373
-
374
-            if (404 === $propFind->getStatus($propRead) || 404 === $propFind->getStatus($propWrite)) {
375
-                $aclPlugin = $this->server->getPlugin('acl');
376
-                $membership = $aclPlugin->getPrincipalMembership($propFind->getPath());
377
-                $readList = [];
378
-                $writeList = [];
379
-
380
-                foreach ($membership as $group) {
381
-                    $groupNode = $this->server->tree->getNodeForPath($group);
382
-
383
-                    $listItem = Uri\split($group)[0].'/';
384
-
385
-                    // If the node is either ap proxy-read or proxy-write
386
-                    // group, we grab the parent principal and add it to the
387
-                    // list.
388
-                    if ($groupNode instanceof Principal\IProxyRead) {
389
-                        $readList[] = $listItem;
390
-                    }
391
-                    if ($groupNode instanceof Principal\IProxyWrite) {
392
-                        $writeList[] = $listItem;
393
-                    }
394
-                }
395
-
396
-                $propFind->set($propRead, new LocalHref($readList));
397
-                $propFind->set($propWrite, new LocalHref($writeList));
398
-            }
399
-        } // instanceof IPrincipal
400
-
401
-        if ($node instanceof ICalendarObject) {
402
-            // The calendar-data property is not supposed to be a 'real'
403
-            // property, but in large chunks of the spec it does act as such.
404
-            // Therefore we simply expose it as a property.
405
-            $propFind->handle('{'.self::NS_CALDAV.'}calendar-data', function () use ($node) {
406
-                $val = $node->get();
407
-                if (is_resource($val)) {
408
-                    $val = stream_get_contents($val);
409
-                }
410
-
411
-                // Taking out \r to not screw up the xml output
412
-                return str_replace("\r", '', $val);
413
-            });
414
-        }
415
-    }
416
-
417
-    /**
418
-     * This function handles the calendar-multiget REPORT.
419
-     *
420
-     * This report is used by the client to fetch the content of a series
421
-     * of urls. Effectively avoiding a lot of redundant requests.
422
-     *
423
-     * @param CalendarMultiGetReport $report
424
-     */
425
-    public function calendarMultiGetReport($report)
426
-    {
427
-        $needsJson = 'application/calendar+json' === $report->contentType;
428
-
429
-        $timeZones = [];
430
-        $propertyList = [];
431
-
432
-        $paths = array_map(
433
-            [$this->server, 'calculateUri'],
434
-            $report->hrefs
435
-        );
436
-
437
-        foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $uri => $objProps) {
438
-            if (($needsJson || $report->expand) && isset($objProps[200]['{'.self::NS_CALDAV.'}calendar-data'])) {
439
-                $vObject = VObject\Reader::read($objProps[200]['{'.self::NS_CALDAV.'}calendar-data']);
440
-
441
-                if ($report->expand) {
442
-                    // We're expanding, and for that we need to figure out the
443
-                    // calendar's timezone.
444
-                    list($calendarPath) = Uri\split($uri);
445
-                    if (!isset($timeZones[$calendarPath])) {
446
-                        // Checking the calendar-timezone property.
447
-                        $tzProp = '{'.self::NS_CALDAV.'}calendar-timezone';
448
-                        $tzResult = $this->server->getProperties($calendarPath, [$tzProp]);
449
-                        if (isset($tzResult[$tzProp])) {
450
-                            // This property contains a VCALENDAR with a single
451
-                            // VTIMEZONE.
452
-                            $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]);
453
-                            $timeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
454
-                        } else {
455
-                            // Defaulting to UTC.
456
-                            $timeZone = new DateTimeZone('UTC');
457
-                        }
458
-                        $timeZones[$calendarPath] = $timeZone;
459
-                    }
460
-
461
-                    $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $timeZones[$calendarPath]);
462
-                }
463
-                if ($needsJson) {
464
-                    $objProps[200]['{'.self::NS_CALDAV.'}calendar-data'] = json_encode($vObject->jsonSerialize());
465
-                } else {
466
-                    $objProps[200]['{'.self::NS_CALDAV.'}calendar-data'] = $vObject->serialize();
467
-                }
468
-                // Destroy circular references so PHP will garbage collect the
469
-                // object.
470
-                $vObject->destroy();
471
-            }
472
-
473
-            $propertyList[] = $objProps;
474
-        }
475
-
476
-        $prefer = $this->server->getHTTPPrefer();
477
-
478
-        $this->server->httpResponse->setStatus(207);
479
-        $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
480
-        $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
481
-        $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, 'minimal' === $prefer['return']));
482
-    }
483
-
484
-    /**
485
-     * This function handles the calendar-query REPORT.
486
-     *
487
-     * This report is used by clients to request calendar objects based on
488
-     * complex conditions.
489
-     *
490
-     * @param Xml\Request\CalendarQueryReport $report
491
-     */
492
-    public function calendarQueryReport($report)
493
-    {
494
-        $path = $this->server->getRequestUri();
495
-
496
-        $needsJson = 'application/calendar+json' === $report->contentType;
497
-
498
-        $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
499
-        $depth = $this->server->getHTTPDepth(0);
500
-
501
-        // The default result is an empty array
502
-        $result = [];
503
-
504
-        $calendarTimeZone = null;
505
-        if ($report->expand) {
506
-            // We're expanding, and for that we need to figure out the
507
-            // calendar's timezone.
508
-            $tzProp = '{'.self::NS_CALDAV.'}calendar-timezone';
509
-            $tzResult = $this->server->getProperties($path, [$tzProp]);
510
-            if (isset($tzResult[$tzProp])) {
511
-                // This property contains a VCALENDAR with a single
512
-                // VTIMEZONE.
513
-                $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]);
514
-                $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
515
-
516
-                // Destroy circular references so PHP will garbage collect the
517
-                // object.
518
-                $vtimezoneObj->destroy();
519
-            } else {
520
-                // Defaulting to UTC.
521
-                $calendarTimeZone = new DateTimeZone('UTC');
522
-            }
523
-        }
524
-
525
-        // The calendarobject was requested directly. In this case we handle
526
-        // this locally.
527
-        if (0 == $depth && $node instanceof ICalendarObject) {
528
-            $requestedCalendarData = true;
529
-            $requestedProperties = $report->properties;
530
-
531
-            if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) {
532
-                // We always retrieve calendar-data, as we need it for filtering.
533
-                $requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data';
534
-
535
-                // If calendar-data wasn't explicitly requested, we need to remove
536
-                // it after processing.
537
-                $requestedCalendarData = false;
538
-            }
539
-
540
-            $properties = $this->server->getPropertiesForPath(
541
-                $path,
542
-                $requestedProperties,
543
-                0
544
-            );
545
-
546
-            // This array should have only 1 element, the first calendar
547
-            // object.
548
-            $properties = current($properties);
549
-
550
-            // If there wasn't any calendar-data returned somehow, we ignore
551
-            // this.
552
-            if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) {
553
-                $validator = new CalendarQueryValidator();
554
-
555
-                $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
556
-                if ($validator->validate($vObject, $report->filters)) {
557
-                    // If the client didn't require the calendar-data property,
558
-                    // we won't give it back.
559
-                    if (!$requestedCalendarData) {
560
-                        unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
561
-                    } else {
562
-                        if ($report->expand) {
563
-                            $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone);
564
-                        }
565
-                        if ($needsJson) {
566
-                            $properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = json_encode($vObject->jsonSerialize());
567
-                        } elseif ($report->expand) {
568
-                            $properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = $vObject->serialize();
569
-                        }
570
-                    }
571
-
572
-                    $result = [$properties];
573
-                }
574
-                // Destroy circular references so PHP will garbage collect the
575
-                // object.
576
-                $vObject->destroy();
577
-            }
578
-        }
579
-
580
-        if ($node instanceof ICalendarObjectContainer && 0 === $depth) {
581
-            if (0 === strpos((string) $this->server->httpRequest->getHeader('User-Agent'), 'MSFT-')) {
582
-                // Microsoft clients incorrectly supplied depth as 0, when it actually
583
-                // should have set depth to 1. We're implementing a workaround here
584
-                // to deal with this.
585
-                //
586
-                // This targets at least the following clients:
587
-                //   Windows 10
588
-                //   Windows Phone 8, 10
589
-                $depth = 1;
590
-            } else {
591
-                throw new BadRequest('A calendar-query REPORT on a calendar with a Depth: 0 is undefined. Set Depth to 1');
592
-            }
593
-        }
594
-
595
-        // If we're dealing with a calendar, the calendar itself is responsible
596
-        // for the calendar-query.
597
-        if ($node instanceof ICalendarObjectContainer && 1 == $depth) {
598
-            $nodePaths = $node->calendarQuery($report->filters);
599
-
600
-            foreach ($nodePaths as $path) {
601
-                list($properties) =
602
-                    $this->server->getPropertiesForPath($this->server->getRequestUri().'/'.$path, $report->properties);
603
-
604
-                if (($needsJson || $report->expand)) {
605
-                    $vObject = VObject\Reader::read($properties[200]['{'.self::NS_CALDAV.'}calendar-data']);
606
-
607
-                    if ($report->expand) {
608
-                        $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone);
609
-                    }
610
-
611
-                    if ($needsJson) {
612
-                        $properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = json_encode($vObject->jsonSerialize());
613
-                    } else {
614
-                        $properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = $vObject->serialize();
615
-                    }
616
-
617
-                    // Destroy circular references so PHP will garbage collect the
618
-                    // object.
619
-                    $vObject->destroy();
620
-                }
621
-                $result[] = $properties;
622
-            }
623
-        }
624
-
625
-        $prefer = $this->server->getHTTPPrefer();
626
-
627
-        $this->server->httpResponse->setStatus(207);
628
-        $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
629
-        $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
630
-        $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, 'minimal' === $prefer['return']));
631
-    }
632
-
633
-    /**
634
-     * This method is responsible for parsing the request and generating the
635
-     * response for the CALDAV:free-busy-query REPORT.
636
-     */
637
-    protected function freeBusyQueryReport(Xml\Request\FreeBusyQueryReport $report)
638
-    {
639
-        $uri = $this->server->getRequestUri();
640
-
641
-        $acl = $this->server->getPlugin('acl');
642
-        if ($acl) {
643
-            $acl->checkPrivileges($uri, '{'.self::NS_CALDAV.'}read-free-busy');
644
-        }
645
-
646
-        $calendar = $this->server->tree->getNodeForPath($uri);
647
-        if (!$calendar instanceof ICalendar) {
648
-            throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars');
649
-        }
650
-
651
-        $tzProp = '{'.self::NS_CALDAV.'}calendar-timezone';
652
-
653
-        // Figuring out the default timezone for the calendar, for floating
654
-        // times.
655
-        $calendarProps = $this->server->getProperties($uri, [$tzProp]);
656
-
657
-        if (isset($calendarProps[$tzProp])) {
658
-            $vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]);
659
-            $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
660
-            // Destroy circular references so PHP will garbage collect the object.
661
-            $vtimezoneObj->destroy();
662
-        } else {
663
-            $calendarTimeZone = new DateTimeZone('UTC');
664
-        }
665
-
666
-        // Doing a calendar-query first, to make sure we get the most
667
-        // performance.
668
-        $urls = $calendar->calendarQuery([
669
-            'name' => 'VCALENDAR',
670
-            'comp-filters' => [
671
-                [
672
-                    'name' => 'VEVENT',
673
-                    'comp-filters' => [],
674
-                    'prop-filters' => [],
675
-                    'is-not-defined' => false,
676
-                    'time-range' => [
677
-                        'start' => $report->start,
678
-                        'end' => $report->end,
679
-                    ],
680
-                ],
681
-            ],
682
-            'prop-filters' => [],
683
-            'is-not-defined' => false,
684
-            'time-range' => null,
685
-        ]);
686
-
687
-        $objects = array_map(function ($url) use ($calendar) {
688
-            $obj = $calendar->getChild($url)->get();
689
-
690
-            return $obj;
691
-        }, $urls);
692
-
693
-        $generator = new VObject\FreeBusyGenerator();
694
-        $generator->setObjects($objects);
695
-        $generator->setTimeRange($report->start, $report->end);
696
-        $generator->setTimeZone($calendarTimeZone);
697
-        $result = $generator->getResult();
698
-        $result = $result->serialize();
699
-
700
-        $this->server->httpResponse->setStatus(200);
701
-        $this->server->httpResponse->setHeader('Content-Type', 'text/calendar');
702
-        $this->server->httpResponse->setHeader('Content-Length', strlen($result));
703
-        $this->server->httpResponse->setBody($result);
704
-    }
705
-
706
-    /**
707
-     * This method is triggered before a file gets updated with new content.
708
-     *
709
-     * This plugin uses this method to ensure that CalDAV objects receive
710
-     * valid calendar data.
711
-     *
712
-     * @param string   $path
713
-     * @param resource $data
714
-     * @param bool     $modified should be set to true, if this event handler
715
-     *                           changed &$data
716
-     */
717
-    public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified)
718
-    {
719
-        if (!$node instanceof ICalendarObject) {
720
-            return;
721
-        }
722
-
723
-        // We're onyl interested in ICalendarObject nodes that are inside of a
724
-        // real calendar. This is to avoid triggering validation and scheduling
725
-        // for non-calendars (such as an inbox).
726
-        list($parent) = Uri\split($path);
727
-        $parentNode = $this->server->tree->getNodeForPath($parent);
728
-
729
-        if (!$parentNode instanceof ICalendar) {
730
-            return;
731
-        }
732
-
733
-        $this->validateICalendar(
734
-            $data,
735
-            $path,
736
-            $modified,
737
-            $this->server->httpRequest,
738
-            $this->server->httpResponse,
739
-            false
740
-        );
741
-    }
742
-
743
-    /**
744
-     * This method is triggered before a new file is created.
745
-     *
746
-     * This plugin uses this method to ensure that newly created calendar
747
-     * objects contain valid calendar data.
748
-     *
749
-     * @param string   $path
750
-     * @param resource $data
751
-     * @param bool     $modified should be set to true, if this event handler
752
-     *                           changed &$data
753
-     */
754
-    public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified)
755
-    {
756
-        if (!$parentNode instanceof ICalendar) {
757
-            return;
758
-        }
759
-
760
-        $this->validateICalendar(
761
-            $data,
762
-            $path,
763
-            $modified,
764
-            $this->server->httpRequest,
765
-            $this->server->httpResponse,
766
-            true
767
-        );
768
-    }
769
-
770
-    /**
771
-     * Checks if the submitted iCalendar data is in fact, valid.
772
-     *
773
-     * An exception is thrown if it's not.
774
-     *
775
-     * @param resource|string   $data
776
-     * @param string            $path
777
-     * @param bool              $modified should be set to true, if this event handler
778
-     *                                    changed &$data
779
-     * @param RequestInterface  $request  the http request
780
-     * @param ResponseInterface $response the http response
781
-     * @param bool              $isNew    is the item a new one, or an update
782
-     */
783
-    protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew)
784
-    {
785
-        // If it's a stream, we convert it to a string first.
786
-        if (is_resource($data)) {
787
-            $data = stream_get_contents($data);
788
-        }
789
-
790
-        $before = $data;
791
-
792
-        try {
793
-            // If the data starts with a [, we can reasonably assume we're dealing
794
-            // with a jCal object.
795
-            if ('[' === substr($data, 0, 1)) {
796
-                $vobj = VObject\Reader::readJson($data);
797
-
798
-                // Converting $data back to iCalendar, as that's what we
799
-                // technically support everywhere.
800
-                $data = $vobj->serialize();
801
-                $modified = true;
802
-            } else {
803
-                $vobj = VObject\Reader::read($data);
804
-            }
805
-        } catch (VObject\ParseException $e) {
806
-            throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: '.$e->getMessage());
807
-        }
808
-
809
-        if ('VCALENDAR' !== $vobj->name) {
810
-            throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.');
811
-        }
812
-
813
-        $sCCS = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
814
-
815
-        // Get the Supported Components for the target calendar
816
-        list($parentPath) = Uri\split($path);
817
-        $calendarProperties = $this->server->getProperties($parentPath, [$sCCS]);
818
-
819
-        if (isset($calendarProperties[$sCCS])) {
820
-            $supportedComponents = $calendarProperties[$sCCS]->getValue();
821
-        } else {
822
-            $supportedComponents = ['VJOURNAL', 'VTODO', 'VEVENT'];
823
-        }
824
-
825
-        $foundType = null;
826
-
827
-        foreach ($vobj->getComponents() as $component) {
828
-            switch ($component->name) {
829
-                case 'VTIMEZONE':
830
-                    continue 2;
831
-                case 'VEVENT':
832
-                case 'VTODO':
833
-                case 'VJOURNAL':
834
-                    $foundType = $component->name;
835
-                    break;
836
-            }
837
-        }
838
-
839
-        if (!$foundType || !in_array($foundType, $supportedComponents)) {
840
-            throw new Exception\InvalidComponentType('iCalendar objects must at least have a component of type '.implode(', ', $supportedComponents));
841
-        }
842
-
843
-        $options = VObject\Node::PROFILE_CALDAV;
844
-        $prefer = $this->server->getHTTPPrefer();
845
-
846
-        if ('strict' !== $prefer['handling']) {
847
-            $options |= VObject\Node::REPAIR;
848
-        }
849
-
850
-        $messages = $vobj->validate($options);
851
-
852
-        $highestLevel = 0;
853
-        $warningMessage = null;
854
-
855
-        // $messages contains a list of problems with the vcard, along with
856
-        // their severity.
857
-        foreach ($messages as $message) {
858
-            if ($message['level'] > $highestLevel) {
859
-                // Recording the highest reported error level.
860
-                $highestLevel = $message['level'];
861
-                $warningMessage = $message['message'];
862
-            }
863
-            switch ($message['level']) {
864
-                case 1:
865
-                    // Level 1 means that there was a problem, but it was repaired.
866
-                    $modified = true;
867
-                    break;
868
-                case 2:
869
-                    // Level 2 means a warning, but not critical
870
-                    break;
871
-                case 3:
872
-                    // Level 3 means a critical error
873
-                    throw new DAV\Exception\UnsupportedMediaType('Validation error in iCalendar: '.$message['message']);
874
-            }
875
-        }
876
-        if ($warningMessage) {
877
-            $response->setHeader(
878
-                'X-Sabre-Ew-Gross',
879
-                'iCalendar validation warning: '.$warningMessage
880
-            );
881
-        }
882
-
883
-        // We use an extra variable to allow event handles to tell us whether
884
-        // the object was modified or not.
885
-        //
886
-        // This helps us determine if we need to re-serialize the object.
887
-        $subModified = false;
888
-
889
-        $this->server->emit(
890
-            'calendarObjectChange',
891
-            [
892
-                $request,
893
-                $response,
894
-                $vobj,
895
-                $parentPath,
896
-                &$subModified,
897
-                $isNew,
898
-            ]
899
-        );
900
-
901
-        if ($modified || $subModified) {
902
-            // An event handler told us that it modified the object.
903
-            $data = $vobj->serialize();
904
-
905
-            // Using md5 to figure out if there was an *actual* change.
906
-            if (!$modified && 0 !== strcmp($data, $before)) {
907
-                $modified = true;
908
-            }
909
-        }
910
-
911
-        // Destroy circular references so PHP will garbage collect the object.
912
-        $vobj->destroy();
913
-    }
914
-
915
-    /**
916
-     * This method is triggered whenever a subsystem reqeuests the privileges
917
-     * that are supported on a particular node.
918
-     */
919
-    public function getSupportedPrivilegeSet(INode $node, array &$supportedPrivilegeSet)
920
-    {
921
-        if ($node instanceof ICalendar) {
922
-            $supportedPrivilegeSet['{DAV:}read']['aggregates']['{'.self::NS_CALDAV.'}read-free-busy'] = [
923
-                'abstract' => false,
924
-                'aggregates' => [],
925
-            ];
926
-        }
927
-    }
928
-
929
-    /**
930
-     * This method is used to generate HTML output for the
931
-     * DAV\Browser\Plugin. This allows us to generate an interface users
932
-     * can use to create new calendars.
933
-     *
934
-     * @param string $output
935
-     *
936
-     * @return bool
937
-     */
938
-    public function htmlActionsPanel(DAV\INode $node, &$output)
939
-    {
940
-        if (!$node instanceof CalendarHome) {
941
-            return;
942
-        }
943
-
944
-        $output .= '<tr><td colspan="2"><form method="post" action="">
33
+	/**
34
+	 * This is the official CalDAV namespace.
35
+	 */
36
+	const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav';
37
+
38
+	/**
39
+	 * This is the namespace for the proprietary calendarserver extensions.
40
+	 */
41
+	const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
42
+
43
+	/**
44
+	 * The hardcoded root for calendar objects. It is unfortunate
45
+	 * that we're stuck with it, but it will have to do for now.
46
+	 */
47
+	const CALENDAR_ROOT = 'calendars';
48
+
49
+	/**
50
+	 * Reference to server object.
51
+	 *
52
+	 * @var DAV\Server
53
+	 */
54
+	protected $server;
55
+
56
+	/**
57
+	 * The default PDO storage uses a MySQL MEDIUMBLOB for iCalendar data,
58
+	 * which can hold up to 2^24 = 16777216 bytes. This is plenty. We're
59
+	 * capping it to 10M here.
60
+	 */
61
+	protected $maxResourceSize = 10000000;
62
+
63
+	/**
64
+	 * Use this method to tell the server this plugin defines additional
65
+	 * HTTP methods.
66
+	 *
67
+	 * This method is passed a uri. It should only return HTTP methods that are
68
+	 * available for the specified uri.
69
+	 *
70
+	 * @param string $uri
71
+	 *
72
+	 * @return array
73
+	 */
74
+	public function getHTTPMethods($uri)
75
+	{
76
+		// The MKCALENDAR is only available on unmapped uri's, whose
77
+		// parents extend IExtendedCollection
78
+		list($parent, $name) = Uri\split($uri);
79
+
80
+		if ('' === $uri) {
81
+			$parent = '';
82
+		}
83
+
84
+		$node = $this->server->tree->getNodeForPath($parent);
85
+
86
+		if ($node instanceof DAV\IExtendedCollection) {
87
+			try {
88
+				$node->getChild($name);
89
+			} catch (DAV\Exception\NotFound $e) {
90
+				return ['MKCALENDAR'];
91
+			}
92
+		}
93
+
94
+		return [];
95
+	}
96
+
97
+	/**
98
+	 * Returns the path to a principal's calendar home.
99
+	 *
100
+	 * The return url must not end with a slash.
101
+	 * This function should return null in case a principal did not have
102
+	 * a calendar home.
103
+	 *
104
+	 * @param string $principalUrl
105
+	 *
106
+	 * @return string
107
+	 */
108
+	public function getCalendarHomeForPrincipal($principalUrl)
109
+	{
110
+		// The default behavior for most sabre/dav servers is that there is a
111
+		// principals root node, which contains users directly under it.
112
+		//
113
+		// This function assumes that there are two components in a principal
114
+		// path. If there's more, we don't return a calendar home. This
115
+		// excludes things like the calendar-proxy-read principal (which it
116
+		// should).
117
+		$parts = explode('/', trim($principalUrl, '/'));
118
+		if (2 !== count($parts)) {
119
+			return;
120
+		}
121
+		if ('principals' !== $parts[0]) {
122
+			return;
123
+		}
124
+
125
+		return self::CALENDAR_ROOT.'/'.$parts[1];
126
+	}
127
+
128
+	/**
129
+	 * Returns a list of features for the DAV: HTTP header.
130
+	 *
131
+	 * @return array
132
+	 */
133
+	public function getFeatures()
134
+	{
135
+		return ['calendar-access', 'calendar-proxy'];
136
+	}
137
+
138
+	/**
139
+	 * Returns a plugin name.
140
+	 *
141
+	 * Using this name other plugins will be able to access other plugins
142
+	 * using DAV\Server::getPlugin
143
+	 *
144
+	 * @return string
145
+	 */
146
+	public function getPluginName()
147
+	{
148
+		return 'caldav';
149
+	}
150
+
151
+	/**
152
+	 * Returns a list of reports this plugin supports.
153
+	 *
154
+	 * This will be used in the {DAV:}supported-report-set property.
155
+	 * Note that you still need to subscribe to the 'report' event to actually
156
+	 * implement them
157
+	 *
158
+	 * @param string $uri
159
+	 *
160
+	 * @return array
161
+	 */
162
+	public function getSupportedReportSet($uri)
163
+	{
164
+		$node = $this->server->tree->getNodeForPath($uri);
165
+
166
+		$reports = [];
167
+		if ($node instanceof ICalendarObjectContainer || $node instanceof ICalendarObject) {
168
+			$reports[] = '{'.self::NS_CALDAV.'}calendar-multiget';
169
+			$reports[] = '{'.self::NS_CALDAV.'}calendar-query';
170
+		}
171
+		if ($node instanceof ICalendar) {
172
+			$reports[] = '{'.self::NS_CALDAV.'}free-busy-query';
173
+		}
174
+		// iCal has a bug where it assumes that sync support is enabled, only
175
+		// if we say we support it on the calendar-home, even though this is
176
+		// not actually the case.
177
+		if ($node instanceof CalendarHome && $this->server->getPlugin('sync')) {
178
+			$reports[] = '{DAV:}sync-collection';
179
+		}
180
+
181
+		return $reports;
182
+	}
183
+
184
+	/**
185
+	 * Initializes the plugin.
186
+	 */
187
+	public function initialize(DAV\Server $server)
188
+	{
189
+		$this->server = $server;
190
+
191
+		$server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']);
192
+		$server->on('report', [$this, 'report']);
193
+		$server->on('propFind', [$this, 'propFind']);
194
+		$server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']);
195
+		$server->on('beforeCreateFile', [$this, 'beforeCreateFile']);
196
+		$server->on('beforeWriteContent', [$this, 'beforeWriteContent']);
197
+		$server->on('afterMethod:GET', [$this, 'httpAfterGET']);
198
+		$server->on('getSupportedPrivilegeSet', [$this, 'getSupportedPrivilegeSet']);
199
+
200
+		$server->xml->namespaceMap[self::NS_CALDAV] = 'cal';
201
+		$server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs';
202
+
203
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet';
204
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}calendar-query'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarQueryReport';
205
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}calendar-multiget'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarMultiGetReport';
206
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}free-busy-query'] = 'Sabre\\CalDAV\\Xml\\Request\\FreeBusyQueryReport';
207
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}mkcalendar'] = 'Sabre\\CalDAV\\Xml\\Request\\MkCalendar';
208
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Xml\\Property\\ScheduleCalendarTransp';
209
+		$server->xml->elementMap['{'.self::NS_CALDAV.'}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet';
210
+
211
+		$server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar';
212
+
213
+		$server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read';
214
+		$server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write';
215
+
216
+		array_push($server->protectedProperties,
217
+			'{'.self::NS_CALDAV.'}supported-calendar-component-set',
218
+			'{'.self::NS_CALDAV.'}supported-calendar-data',
219
+			'{'.self::NS_CALDAV.'}max-resource-size',
220
+			'{'.self::NS_CALDAV.'}min-date-time',
221
+			'{'.self::NS_CALDAV.'}max-date-time',
222
+			'{'.self::NS_CALDAV.'}max-instances',
223
+			'{'.self::NS_CALDAV.'}max-attendees-per-instance',
224
+			'{'.self::NS_CALDAV.'}calendar-home-set',
225
+			'{'.self::NS_CALDAV.'}supported-collation-set',
226
+			'{'.self::NS_CALDAV.'}calendar-data',
227
+
228
+			// CalendarServer extensions
229
+			'{'.self::NS_CALENDARSERVER.'}getctag',
230
+			'{'.self::NS_CALENDARSERVER.'}calendar-proxy-read-for',
231
+			'{'.self::NS_CALENDARSERVER.'}calendar-proxy-write-for'
232
+		);
233
+
234
+		if ($aclPlugin = $server->getPlugin('acl')) {
235
+			$aclPlugin->principalSearchPropertySet['{'.self::NS_CALDAV.'}calendar-user-address-set'] = 'Calendar address';
236
+		}
237
+	}
238
+
239
+	/**
240
+	 * This functions handles REPORT requests specific to CalDAV.
241
+	 *
242
+	 * @param string $reportName
243
+	 * @param mixed  $report
244
+	 * @param mixed  $path
245
+	 *
246
+	 * @return bool|null
247
+	 */
248
+	public function report($reportName, $report, $path)
249
+	{
250
+		switch ($reportName) {
251
+			case '{'.self::NS_CALDAV.'}calendar-multiget':
252
+				$this->server->transactionType = 'report-calendar-multiget';
253
+				$this->calendarMultiGetReport($report);
254
+
255
+				return false;
256
+			case '{'.self::NS_CALDAV.'}calendar-query':
257
+				$this->server->transactionType = 'report-calendar-query';
258
+				$this->calendarQueryReport($report);
259
+
260
+				return false;
261
+			case '{'.self::NS_CALDAV.'}free-busy-query':
262
+				$this->server->transactionType = 'report-free-busy-query';
263
+				$this->freeBusyQueryReport($report);
264
+
265
+				return false;
266
+		}
267
+	}
268
+
269
+	/**
270
+	 * This function handles the MKCALENDAR HTTP method, which creates
271
+	 * a new calendar.
272
+	 *
273
+	 * @return bool
274
+	 */
275
+	public function httpMkCalendar(RequestInterface $request, ResponseInterface $response)
276
+	{
277
+		$body = $request->getBodyAsString();
278
+		$path = $request->getPath();
279
+
280
+		$properties = [];
281
+
282
+		if ($body) {
283
+			try {
284
+				$mkcalendar = $this->server->xml->expect(
285
+					'{urn:ietf:params:xml:ns:caldav}mkcalendar',
286
+					$body
287
+				);
288
+			} catch (\Sabre\Xml\ParseException $e) {
289
+				throw new BadRequest($e->getMessage(), 0, $e);
290
+			}
291
+			$properties = $mkcalendar->getProperties();
292
+		}
293
+
294
+		// iCal abuses MKCALENDAR since iCal 10.9.2 to create server-stored
295
+		// subscriptions. Before that it used MKCOL which was the correct way
296
+		// to do this.
297
+		//
298
+		// If the body had a {DAV:}resourcetype, it means we stumbled upon this
299
+		// request, and we simply use it instead of the pre-defined list.
300
+		if (isset($properties['{DAV:}resourcetype'])) {
301
+			$resourceType = $properties['{DAV:}resourcetype']->getValue();
302
+		} else {
303
+			$resourceType = ['{DAV:}collection', '{urn:ietf:params:xml:ns:caldav}calendar'];
304
+		}
305
+
306
+		$this->server->createCollection($path, new MkCol($resourceType, $properties));
307
+
308
+		$response->setStatus(201);
309
+		$response->setHeader('Content-Length', 0);
310
+
311
+		// This breaks the method chain.
312
+		return false;
313
+	}
314
+
315
+	/**
316
+	 * PropFind.
317
+	 *
318
+	 * This method handler is invoked before any after properties for a
319
+	 * resource are fetched. This allows us to add in any CalDAV specific
320
+	 * properties.
321
+	 */
322
+	public function propFind(DAV\PropFind $propFind, DAV\INode $node)
323
+	{
324
+		$ns = '{'.self::NS_CALDAV.'}';
325
+
326
+		if ($node instanceof ICalendarObjectContainer) {
327
+			$propFind->handle($ns.'max-resource-size', $this->maxResourceSize);
328
+			$propFind->handle($ns.'supported-calendar-data', function () {
329
+				return new Xml\Property\SupportedCalendarData();
330
+			});
331
+			$propFind->handle($ns.'supported-collation-set', function () {
332
+				return new Xml\Property\SupportedCollationSet();
333
+			});
334
+		}
335
+
336
+		if ($node instanceof DAVACL\IPrincipal) {
337
+			$principalUrl = $node->getPrincipalUrl();
338
+
339
+			$propFind->handle('{'.self::NS_CALDAV.'}calendar-home-set', function () use ($principalUrl) {
340
+				$calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl);
341
+				if (is_null($calendarHomePath)) {
342
+					return null;
343
+				}
344
+
345
+				return new LocalHref($calendarHomePath.'/');
346
+			});
347
+			// The calendar-user-address-set property is basically mapped to
348
+			// the {DAV:}alternate-URI-set property.
349
+			$propFind->handle('{'.self::NS_CALDAV.'}calendar-user-address-set', function () use ($node) {
350
+				$addresses = $node->getAlternateUriSet();
351
+				$addresses[] = $this->server->getBaseUri().$node->getPrincipalUrl().'/';
352
+
353
+				return new LocalHref($addresses);
354
+			});
355
+			// For some reason somebody thought it was a good idea to add
356
+			// another one of these properties. We're supporting it too.
357
+			$propFind->handle('{'.self::NS_CALENDARSERVER.'}email-address-set', function () use ($node) {
358
+				$addresses = $node->getAlternateUriSet();
359
+				$emails = [];
360
+				foreach ($addresses as $address) {
361
+					if ('mailto:' === substr($address, 0, 7)) {
362
+						$emails[] = substr($address, 7);
363
+					}
364
+				}
365
+
366
+				return new Xml\Property\EmailAddressSet($emails);
367
+			});
368
+
369
+			// These two properties are shortcuts for ical to easily find
370
+			// other principals this principal has access to.
371
+			$propRead = '{'.self::NS_CALENDARSERVER.'}calendar-proxy-read-for';
372
+			$propWrite = '{'.self::NS_CALENDARSERVER.'}calendar-proxy-write-for';
373
+
374
+			if (404 === $propFind->getStatus($propRead) || 404 === $propFind->getStatus($propWrite)) {
375
+				$aclPlugin = $this->server->getPlugin('acl');
376
+				$membership = $aclPlugin->getPrincipalMembership($propFind->getPath());
377
+				$readList = [];
378
+				$writeList = [];
379
+
380
+				foreach ($membership as $group) {
381
+					$groupNode = $this->server->tree->getNodeForPath($group);
382
+
383
+					$listItem = Uri\split($group)[0].'/';
384
+
385
+					// If the node is either ap proxy-read or proxy-write
386
+					// group, we grab the parent principal and add it to the
387
+					// list.
388
+					if ($groupNode instanceof Principal\IProxyRead) {
389
+						$readList[] = $listItem;
390
+					}
391
+					if ($groupNode instanceof Principal\IProxyWrite) {
392
+						$writeList[] = $listItem;
393
+					}
394
+				}
395
+
396
+				$propFind->set($propRead, new LocalHref($readList));
397
+				$propFind->set($propWrite, new LocalHref($writeList));
398
+			}
399
+		} // instanceof IPrincipal
400
+
401
+		if ($node instanceof ICalendarObject) {
402
+			// The calendar-data property is not supposed to be a 'real'
403
+			// property, but in large chunks of the spec it does act as such.
404
+			// Therefore we simply expose it as a property.
405
+			$propFind->handle('{'.self::NS_CALDAV.'}calendar-data', function () use ($node) {
406
+				$val = $node->get();
407
+				if (is_resource($val)) {
408
+					$val = stream_get_contents($val);
409
+				}
410
+
411
+				// Taking out \r to not screw up the xml output
412
+				return str_replace("\r", '', $val);
413
+			});
414
+		}
415
+	}
416
+
417
+	/**
418
+	 * This function handles the calendar-multiget REPORT.
419
+	 *
420
+	 * This report is used by the client to fetch the content of a series
421
+	 * of urls. Effectively avoiding a lot of redundant requests.
422
+	 *
423
+	 * @param CalendarMultiGetReport $report
424
+	 */
425
+	public function calendarMultiGetReport($report)
426
+	{
427
+		$needsJson = 'application/calendar+json' === $report->contentType;
428
+
429
+		$timeZones = [];
430
+		$propertyList = [];
431
+
432
+		$paths = array_map(
433
+			[$this->server, 'calculateUri'],
434
+			$report->hrefs
435
+		);
436
+
437
+		foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $uri => $objProps) {
438
+			if (($needsJson || $report->expand) && isset($objProps[200]['{'.self::NS_CALDAV.'}calendar-data'])) {
439
+				$vObject = VObject\Reader::read($objProps[200]['{'.self::NS_CALDAV.'}calendar-data']);
440
+
441
+				if ($report->expand) {
442
+					// We're expanding, and for that we need to figure out the
443
+					// calendar's timezone.
444
+					list($calendarPath) = Uri\split($uri);
445
+					if (!isset($timeZones[$calendarPath])) {
446
+						// Checking the calendar-timezone property.
447
+						$tzProp = '{'.self::NS_CALDAV.'}calendar-timezone';
448
+						$tzResult = $this->server->getProperties($calendarPath, [$tzProp]);
449
+						if (isset($tzResult[$tzProp])) {
450
+							// This property contains a VCALENDAR with a single
451
+							// VTIMEZONE.
452
+							$vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]);
453
+							$timeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
454
+						} else {
455
+							// Defaulting to UTC.
456
+							$timeZone = new DateTimeZone('UTC');
457
+						}
458
+						$timeZones[$calendarPath] = $timeZone;
459
+					}
460
+
461
+					$vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $timeZones[$calendarPath]);
462
+				}
463
+				if ($needsJson) {
464
+					$objProps[200]['{'.self::NS_CALDAV.'}calendar-data'] = json_encode($vObject->jsonSerialize());
465
+				} else {
466
+					$objProps[200]['{'.self::NS_CALDAV.'}calendar-data'] = $vObject->serialize();
467
+				}
468
+				// Destroy circular references so PHP will garbage collect the
469
+				// object.
470
+				$vObject->destroy();
471
+			}
472
+
473
+			$propertyList[] = $objProps;
474
+		}
475
+
476
+		$prefer = $this->server->getHTTPPrefer();
477
+
478
+		$this->server->httpResponse->setStatus(207);
479
+		$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
480
+		$this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
481
+		$this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, 'minimal' === $prefer['return']));
482
+	}
483
+
484
+	/**
485
+	 * This function handles the calendar-query REPORT.
486
+	 *
487
+	 * This report is used by clients to request calendar objects based on
488
+	 * complex conditions.
489
+	 *
490
+	 * @param Xml\Request\CalendarQueryReport $report
491
+	 */
492
+	public function calendarQueryReport($report)
493
+	{
494
+		$path = $this->server->getRequestUri();
495
+
496
+		$needsJson = 'application/calendar+json' === $report->contentType;
497
+
498
+		$node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
499
+		$depth = $this->server->getHTTPDepth(0);
500
+
501
+		// The default result is an empty array
502
+		$result = [];
503
+
504
+		$calendarTimeZone = null;
505
+		if ($report->expand) {
506
+			// We're expanding, and for that we need to figure out the
507
+			// calendar's timezone.
508
+			$tzProp = '{'.self::NS_CALDAV.'}calendar-timezone';
509
+			$tzResult = $this->server->getProperties($path, [$tzProp]);
510
+			if (isset($tzResult[$tzProp])) {
511
+				// This property contains a VCALENDAR with a single
512
+				// VTIMEZONE.
513
+				$vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]);
514
+				$calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
515
+
516
+				// Destroy circular references so PHP will garbage collect the
517
+				// object.
518
+				$vtimezoneObj->destroy();
519
+			} else {
520
+				// Defaulting to UTC.
521
+				$calendarTimeZone = new DateTimeZone('UTC');
522
+			}
523
+		}
524
+
525
+		// The calendarobject was requested directly. In this case we handle
526
+		// this locally.
527
+		if (0 == $depth && $node instanceof ICalendarObject) {
528
+			$requestedCalendarData = true;
529
+			$requestedProperties = $report->properties;
530
+
531
+			if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) {
532
+				// We always retrieve calendar-data, as we need it for filtering.
533
+				$requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data';
534
+
535
+				// If calendar-data wasn't explicitly requested, we need to remove
536
+				// it after processing.
537
+				$requestedCalendarData = false;
538
+			}
539
+
540
+			$properties = $this->server->getPropertiesForPath(
541
+				$path,
542
+				$requestedProperties,
543
+				0
544
+			);
545
+
546
+			// This array should have only 1 element, the first calendar
547
+			// object.
548
+			$properties = current($properties);
549
+
550
+			// If there wasn't any calendar-data returned somehow, we ignore
551
+			// this.
552
+			if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) {
553
+				$validator = new CalendarQueryValidator();
554
+
555
+				$vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
556
+				if ($validator->validate($vObject, $report->filters)) {
557
+					// If the client didn't require the calendar-data property,
558
+					// we won't give it back.
559
+					if (!$requestedCalendarData) {
560
+						unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']);
561
+					} else {
562
+						if ($report->expand) {
563
+							$vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone);
564
+						}
565
+						if ($needsJson) {
566
+							$properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = json_encode($vObject->jsonSerialize());
567
+						} elseif ($report->expand) {
568
+							$properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = $vObject->serialize();
569
+						}
570
+					}
571
+
572
+					$result = [$properties];
573
+				}
574
+				// Destroy circular references so PHP will garbage collect the
575
+				// object.
576
+				$vObject->destroy();
577
+			}
578
+		}
579
+
580
+		if ($node instanceof ICalendarObjectContainer && 0 === $depth) {
581
+			if (0 === strpos((string) $this->server->httpRequest->getHeader('User-Agent'), 'MSFT-')) {
582
+				// Microsoft clients incorrectly supplied depth as 0, when it actually
583
+				// should have set depth to 1. We're implementing a workaround here
584
+				// to deal with this.
585
+				//
586
+				// This targets at least the following clients:
587
+				//   Windows 10
588
+				//   Windows Phone 8, 10
589
+				$depth = 1;
590
+			} else {
591
+				throw new BadRequest('A calendar-query REPORT on a calendar with a Depth: 0 is undefined. Set Depth to 1');
592
+			}
593
+		}
594
+
595
+		// If we're dealing with a calendar, the calendar itself is responsible
596
+		// for the calendar-query.
597
+		if ($node instanceof ICalendarObjectContainer && 1 == $depth) {
598
+			$nodePaths = $node->calendarQuery($report->filters);
599
+
600
+			foreach ($nodePaths as $path) {
601
+				list($properties) =
602
+					$this->server->getPropertiesForPath($this->server->getRequestUri().'/'.$path, $report->properties);
603
+
604
+				if (($needsJson || $report->expand)) {
605
+					$vObject = VObject\Reader::read($properties[200]['{'.self::NS_CALDAV.'}calendar-data']);
606
+
607
+					if ($report->expand) {
608
+						$vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone);
609
+					}
610
+
611
+					if ($needsJson) {
612
+						$properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = json_encode($vObject->jsonSerialize());
613
+					} else {
614
+						$properties[200]['{'.self::NS_CALDAV.'}calendar-data'] = $vObject->serialize();
615
+					}
616
+
617
+					// Destroy circular references so PHP will garbage collect the
618
+					// object.
619
+					$vObject->destroy();
620
+				}
621
+				$result[] = $properties;
622
+			}
623
+		}
624
+
625
+		$prefer = $this->server->getHTTPPrefer();
626
+
627
+		$this->server->httpResponse->setStatus(207);
628
+		$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
629
+		$this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
630
+		$this->server->httpResponse->setBody($this->server->generateMultiStatus($result, 'minimal' === $prefer['return']));
631
+	}
632
+
633
+	/**
634
+	 * This method is responsible for parsing the request and generating the
635
+	 * response for the CALDAV:free-busy-query REPORT.
636
+	 */
637
+	protected function freeBusyQueryReport(Xml\Request\FreeBusyQueryReport $report)
638
+	{
639
+		$uri = $this->server->getRequestUri();
640
+
641
+		$acl = $this->server->getPlugin('acl');
642
+		if ($acl) {
643
+			$acl->checkPrivileges($uri, '{'.self::NS_CALDAV.'}read-free-busy');
644
+		}
645
+
646
+		$calendar = $this->server->tree->getNodeForPath($uri);
647
+		if (!$calendar instanceof ICalendar) {
648
+			throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars');
649
+		}
650
+
651
+		$tzProp = '{'.self::NS_CALDAV.'}calendar-timezone';
652
+
653
+		// Figuring out the default timezone for the calendar, for floating
654
+		// times.
655
+		$calendarProps = $this->server->getProperties($uri, [$tzProp]);
656
+
657
+		if (isset($calendarProps[$tzProp])) {
658
+			$vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]);
659
+			$calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
660
+			// Destroy circular references so PHP will garbage collect the object.
661
+			$vtimezoneObj->destroy();
662
+		} else {
663
+			$calendarTimeZone = new DateTimeZone('UTC');
664
+		}
665
+
666
+		// Doing a calendar-query first, to make sure we get the most
667
+		// performance.
668
+		$urls = $calendar->calendarQuery([
669
+			'name' => 'VCALENDAR',
670
+			'comp-filters' => [
671
+				[
672
+					'name' => 'VEVENT',
673
+					'comp-filters' => [],
674
+					'prop-filters' => [],
675
+					'is-not-defined' => false,
676
+					'time-range' => [
677
+						'start' => $report->start,
678
+						'end' => $report->end,
679
+					],
680
+				],
681
+			],
682
+			'prop-filters' => [],
683
+			'is-not-defined' => false,
684
+			'time-range' => null,
685
+		]);
686
+
687
+		$objects = array_map(function ($url) use ($calendar) {
688
+			$obj = $calendar->getChild($url)->get();
689
+
690
+			return $obj;
691
+		}, $urls);
692
+
693
+		$generator = new VObject\FreeBusyGenerator();
694
+		$generator->setObjects($objects);
695
+		$generator->setTimeRange($report->start, $report->end);
696
+		$generator->setTimeZone($calendarTimeZone);
697
+		$result = $generator->getResult();
698
+		$result = $result->serialize();
699
+
700
+		$this->server->httpResponse->setStatus(200);
701
+		$this->server->httpResponse->setHeader('Content-Type', 'text/calendar');
702
+		$this->server->httpResponse->setHeader('Content-Length', strlen($result));
703
+		$this->server->httpResponse->setBody($result);
704
+	}
705
+
706
+	/**
707
+	 * This method is triggered before a file gets updated with new content.
708
+	 *
709
+	 * This plugin uses this method to ensure that CalDAV objects receive
710
+	 * valid calendar data.
711
+	 *
712
+	 * @param string   $path
713
+	 * @param resource $data
714
+	 * @param bool     $modified should be set to true, if this event handler
715
+	 *                           changed &$data
716
+	 */
717
+	public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified)
718
+	{
719
+		if (!$node instanceof ICalendarObject) {
720
+			return;
721
+		}
722
+
723
+		// We're onyl interested in ICalendarObject nodes that are inside of a
724
+		// real calendar. This is to avoid triggering validation and scheduling
725
+		// for non-calendars (such as an inbox).
726
+		list($parent) = Uri\split($path);
727
+		$parentNode = $this->server->tree->getNodeForPath($parent);
728
+
729
+		if (!$parentNode instanceof ICalendar) {
730
+			return;
731
+		}
732
+
733
+		$this->validateICalendar(
734
+			$data,
735
+			$path,
736
+			$modified,
737
+			$this->server->httpRequest,
738
+			$this->server->httpResponse,
739
+			false
740
+		);
741
+	}
742
+
743
+	/**
744
+	 * This method is triggered before a new file is created.
745
+	 *
746
+	 * This plugin uses this method to ensure that newly created calendar
747
+	 * objects contain valid calendar data.
748
+	 *
749
+	 * @param string   $path
750
+	 * @param resource $data
751
+	 * @param bool     $modified should be set to true, if this event handler
752
+	 *                           changed &$data
753
+	 */
754
+	public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified)
755
+	{
756
+		if (!$parentNode instanceof ICalendar) {
757
+			return;
758
+		}
759
+
760
+		$this->validateICalendar(
761
+			$data,
762
+			$path,
763
+			$modified,
764
+			$this->server->httpRequest,
765
+			$this->server->httpResponse,
766
+			true
767
+		);
768
+	}
769
+
770
+	/**
771
+	 * Checks if the submitted iCalendar data is in fact, valid.
772
+	 *
773
+	 * An exception is thrown if it's not.
774
+	 *
775
+	 * @param resource|string   $data
776
+	 * @param string            $path
777
+	 * @param bool              $modified should be set to true, if this event handler
778
+	 *                                    changed &$data
779
+	 * @param RequestInterface  $request  the http request
780
+	 * @param ResponseInterface $response the http response
781
+	 * @param bool              $isNew    is the item a new one, or an update
782
+	 */
783
+	protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew)
784
+	{
785
+		// If it's a stream, we convert it to a string first.
786
+		if (is_resource($data)) {
787
+			$data = stream_get_contents($data);
788
+		}
789
+
790
+		$before = $data;
791
+
792
+		try {
793
+			// If the data starts with a [, we can reasonably assume we're dealing
794
+			// with a jCal object.
795
+			if ('[' === substr($data, 0, 1)) {
796
+				$vobj = VObject\Reader::readJson($data);
797
+
798
+				// Converting $data back to iCalendar, as that's what we
799
+				// technically support everywhere.
800
+				$data = $vobj->serialize();
801
+				$modified = true;
802
+			} else {
803
+				$vobj = VObject\Reader::read($data);
804
+			}
805
+		} catch (VObject\ParseException $e) {
806
+			throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: '.$e->getMessage());
807
+		}
808
+
809
+		if ('VCALENDAR' !== $vobj->name) {
810
+			throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.');
811
+		}
812
+
813
+		$sCCS = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
814
+
815
+		// Get the Supported Components for the target calendar
816
+		list($parentPath) = Uri\split($path);
817
+		$calendarProperties = $this->server->getProperties($parentPath, [$sCCS]);
818
+
819
+		if (isset($calendarProperties[$sCCS])) {
820
+			$supportedComponents = $calendarProperties[$sCCS]->getValue();
821
+		} else {
822
+			$supportedComponents = ['VJOURNAL', 'VTODO', 'VEVENT'];
823
+		}
824
+
825
+		$foundType = null;
826
+
827
+		foreach ($vobj->getComponents() as $component) {
828
+			switch ($component->name) {
829
+				case 'VTIMEZONE':
830
+					continue 2;
831
+				case 'VEVENT':
832
+				case 'VTODO':
833
+				case 'VJOURNAL':
834
+					$foundType = $component->name;
835
+					break;
836
+			}
837
+		}
838
+
839
+		if (!$foundType || !in_array($foundType, $supportedComponents)) {
840
+			throw new Exception\InvalidComponentType('iCalendar objects must at least have a component of type '.implode(', ', $supportedComponents));
841
+		}
842
+
843
+		$options = VObject\Node::PROFILE_CALDAV;
844
+		$prefer = $this->server->getHTTPPrefer();
845
+
846
+		if ('strict' !== $prefer['handling']) {
847
+			$options |= VObject\Node::REPAIR;
848
+		}
849
+
850
+		$messages = $vobj->validate($options);
851
+
852
+		$highestLevel = 0;
853
+		$warningMessage = null;
854
+
855
+		// $messages contains a list of problems with the vcard, along with
856
+		// their severity.
857
+		foreach ($messages as $message) {
858
+			if ($message['level'] > $highestLevel) {
859
+				// Recording the highest reported error level.
860
+				$highestLevel = $message['level'];
861
+				$warningMessage = $message['message'];
862
+			}
863
+			switch ($message['level']) {
864
+				case 1:
865
+					// Level 1 means that there was a problem, but it was repaired.
866
+					$modified = true;
867
+					break;
868
+				case 2:
869
+					// Level 2 means a warning, but not critical
870
+					break;
871
+				case 3:
872
+					// Level 3 means a critical error
873
+					throw new DAV\Exception\UnsupportedMediaType('Validation error in iCalendar: '.$message['message']);
874
+			}
875
+		}
876
+		if ($warningMessage) {
877
+			$response->setHeader(
878
+				'X-Sabre-Ew-Gross',
879
+				'iCalendar validation warning: '.$warningMessage
880
+			);
881
+		}
882
+
883
+		// We use an extra variable to allow event handles to tell us whether
884
+		// the object was modified or not.
885
+		//
886
+		// This helps us determine if we need to re-serialize the object.
887
+		$subModified = false;
888
+
889
+		$this->server->emit(
890
+			'calendarObjectChange',
891
+			[
892
+				$request,
893
+				$response,
894
+				$vobj,
895
+				$parentPath,
896
+				&$subModified,
897
+				$isNew,
898
+			]
899
+		);
900
+
901
+		if ($modified || $subModified) {
902
+			// An event handler told us that it modified the object.
903
+			$data = $vobj->serialize();
904
+
905
+			// Using md5 to figure out if there was an *actual* change.
906
+			if (!$modified && 0 !== strcmp($data, $before)) {
907
+				$modified = true;
908
+			}
909
+		}
910
+
911
+		// Destroy circular references so PHP will garbage collect the object.
912
+		$vobj->destroy();
913
+	}
914
+
915
+	/**
916
+	 * This method is triggered whenever a subsystem reqeuests the privileges
917
+	 * that are supported on a particular node.
918
+	 */
919
+	public function getSupportedPrivilegeSet(INode $node, array &$supportedPrivilegeSet)
920
+	{
921
+		if ($node instanceof ICalendar) {
922
+			$supportedPrivilegeSet['{DAV:}read']['aggregates']['{'.self::NS_CALDAV.'}read-free-busy'] = [
923
+				'abstract' => false,
924
+				'aggregates' => [],
925
+			];
926
+		}
927
+	}
928
+
929
+	/**
930
+	 * This method is used to generate HTML output for the
931
+	 * DAV\Browser\Plugin. This allows us to generate an interface users
932
+	 * can use to create new calendars.
933
+	 *
934
+	 * @param string $output
935
+	 *
936
+	 * @return bool
937
+	 */
938
+	public function htmlActionsPanel(DAV\INode $node, &$output)
939
+	{
940
+		if (!$node instanceof CalendarHome) {
941
+			return;
942
+		}
943
+
944
+		$output .= '<tr><td colspan="2"><form method="post" action="">
945 945
             <h3>Create new calendar</h3>
946 946
             <input type="hidden" name="sabreAction" value="mkcol" />
947 947
             <input type="hidden" name="resourceType" value="{DAV:}collection,{'.self::NS_CALDAV.'}calendar" />
@@ -951,61 +951,61 @@  discard block
 block discarded – undo
951 951
             </form>
952 952
             </td></tr>';
953 953
 
954
-        return false;
955
-    }
956
-
957
-    /**
958
-     * This event is triggered after GET requests.
959
-     *
960
-     * This is used to transform data into jCal, if this was requested.
961
-     */
962
-    public function httpAfterGet(RequestInterface $request, ResponseInterface $response)
963
-    {
964
-        $contentType = $response->getHeader('Content-Type');
965
-        if (null === $contentType || false === strpos($contentType, 'text/calendar')) {
966
-            return;
967
-        }
968
-
969
-        $result = HTTP\negotiateContentType(
970
-            $request->getHeader('Accept'),
971
-            ['text/calendar', 'application/calendar+json']
972
-        );
973
-
974
-        if ('application/calendar+json' !== $result) {
975
-            // Do nothing
976
-            return;
977
-        }
978
-
979
-        // Transforming.
980
-        $vobj = VObject\Reader::read($response->getBody());
981
-
982
-        $jsonBody = json_encode($vobj->jsonSerialize());
983
-        $response->setBody($jsonBody);
984
-
985
-        // Destroy circular references so PHP will garbage collect the object.
986
-        $vobj->destroy();
987
-
988
-        $response->setHeader('Content-Type', 'application/calendar+json');
989
-        $response->setHeader('Content-Length', strlen($jsonBody));
990
-    }
991
-
992
-    /**
993
-     * Returns a bunch of meta-data about the plugin.
994
-     *
995
-     * Providing this information is optional, and is mainly displayed by the
996
-     * Browser plugin.
997
-     *
998
-     * The description key in the returned array may contain html and will not
999
-     * be sanitized.
1000
-     *
1001
-     * @return array
1002
-     */
1003
-    public function getPluginInfo()
1004
-    {
1005
-        return [
1006
-            'name' => $this->getPluginName(),
1007
-            'description' => 'Adds support for CalDAV (rfc4791)',
1008
-            'link' => 'http://sabre.io/dav/caldav/',
1009
-        ];
1010
-    }
954
+		return false;
955
+	}
956
+
957
+	/**
958
+	 * This event is triggered after GET requests.
959
+	 *
960
+	 * This is used to transform data into jCal, if this was requested.
961
+	 */
962
+	public function httpAfterGet(RequestInterface $request, ResponseInterface $response)
963
+	{
964
+		$contentType = $response->getHeader('Content-Type');
965
+		if (null === $contentType || false === strpos($contentType, 'text/calendar')) {
966
+			return;
967
+		}
968
+
969
+		$result = HTTP\negotiateContentType(
970
+			$request->getHeader('Accept'),
971
+			['text/calendar', 'application/calendar+json']
972
+		);
973
+
974
+		if ('application/calendar+json' !== $result) {
975
+			// Do nothing
976
+			return;
977
+		}
978
+
979
+		// Transforming.
980
+		$vobj = VObject\Reader::read($response->getBody());
981
+
982
+		$jsonBody = json_encode($vobj->jsonSerialize());
983
+		$response->setBody($jsonBody);
984
+
985
+		// Destroy circular references so PHP will garbage collect the object.
986
+		$vobj->destroy();
987
+
988
+		$response->setHeader('Content-Type', 'application/calendar+json');
989
+		$response->setHeader('Content-Length', strlen($jsonBody));
990
+	}
991
+
992
+	/**
993
+	 * Returns a bunch of meta-data about the plugin.
994
+	 *
995
+	 * Providing this information is optional, and is mainly displayed by the
996
+	 * Browser plugin.
997
+	 *
998
+	 * The description key in the returned array may contain html and will not
999
+	 * be sanitized.
1000
+	 *
1001
+	 * @return array
1002
+	 */
1003
+	public function getPluginInfo()
1004
+	{
1005
+		return [
1006
+			'name' => $this->getPluginName(),
1007
+			'description' => 'Adds support for CalDAV (rfc4791)',
1008
+			'link' => 'http://sabre.io/dav/caldav/',
1009
+		];
1010
+	}
1011 1011
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -325,10 +325,10 @@  discard block
 block discarded – undo
325 325
 
326 326
         if ($node instanceof ICalendarObjectContainer) {
327 327
             $propFind->handle($ns.'max-resource-size', $this->maxResourceSize);
328
-            $propFind->handle($ns.'supported-calendar-data', function () {
328
+            $propFind->handle($ns.'supported-calendar-data', function() {
329 329
                 return new Xml\Property\SupportedCalendarData();
330 330
             });
331
-            $propFind->handle($ns.'supported-collation-set', function () {
331
+            $propFind->handle($ns.'supported-collation-set', function() {
332 332
                 return new Xml\Property\SupportedCollationSet();
333 333
             });
334 334
         }
@@ -336,7 +336,7 @@  discard block
 block discarded – undo
336 336
         if ($node instanceof DAVACL\IPrincipal) {
337 337
             $principalUrl = $node->getPrincipalUrl();
338 338
 
339
-            $propFind->handle('{'.self::NS_CALDAV.'}calendar-home-set', function () use ($principalUrl) {
339
+            $propFind->handle('{'.self::NS_CALDAV.'}calendar-home-set', function() use ($principalUrl) {
340 340
                 $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl);
341 341
                 if (is_null($calendarHomePath)) {
342 342
                     return null;
@@ -346,7 +346,7 @@  discard block
 block discarded – undo
346 346
             });
347 347
             // The calendar-user-address-set property is basically mapped to
348 348
             // the {DAV:}alternate-URI-set property.
349
-            $propFind->handle('{'.self::NS_CALDAV.'}calendar-user-address-set', function () use ($node) {
349
+            $propFind->handle('{'.self::NS_CALDAV.'}calendar-user-address-set', function() use ($node) {
350 350
                 $addresses = $node->getAlternateUriSet();
351 351
                 $addresses[] = $this->server->getBaseUri().$node->getPrincipalUrl().'/';
352 352
 
@@ -354,7 +354,7 @@  discard block
 block discarded – undo
354 354
             });
355 355
             // For some reason somebody thought it was a good idea to add
356 356
             // another one of these properties. We're supporting it too.
357
-            $propFind->handle('{'.self::NS_CALENDARSERVER.'}email-address-set', function () use ($node) {
357
+            $propFind->handle('{'.self::NS_CALENDARSERVER.'}email-address-set', function() use ($node) {
358 358
                 $addresses = $node->getAlternateUriSet();
359 359
                 $emails = [];
360 360
                 foreach ($addresses as $address) {
@@ -402,7 +402,7 @@  discard block
 block discarded – undo
402 402
             // The calendar-data property is not supposed to be a 'real'
403 403
             // property, but in large chunks of the spec it does act as such.
404 404
             // Therefore we simply expose it as a property.
405
-            $propFind->handle('{'.self::NS_CALDAV.'}calendar-data', function () use ($node) {
405
+            $propFind->handle('{'.self::NS_CALDAV.'}calendar-data', function() use ($node) {
406 406
                 $val = $node->get();
407 407
                 if (is_resource($val)) {
408 408
                     $val = stream_get_contents($val);
@@ -684,7 +684,7 @@  discard block
 block discarded – undo
684 684
             'time-range' => null,
685 685
         ]);
686 686
 
687
-        $objects = array_map(function ($url) use ($calendar) {
687
+        $objects = array_map(function($url) use ($calendar) {
688 688
             $obj = $calendar->getChild($url)->get();
689 689
 
690 690
             return $obj;
Please login to merge, or discard this patch.
htdocs/includes/sabre/sabre/dav/lib/CalDAV/Principal/ProxyRead.php 1 patch
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -20,142 +20,142 @@
 block discarded – undo
20 20
  */
21 21
 class ProxyRead implements IProxyRead
22 22
 {
23
-    /**
24
-     * Principal information from the parent principal.
25
-     *
26
-     * @var array
27
-     */
28
-    protected $principalInfo;
29
-
30
-    /**
31
-     * Principal backend.
32
-     *
33
-     * @var DAVACL\PrincipalBackend\BackendInterface
34
-     */
35
-    protected $principalBackend;
36
-
37
-    /**
38
-     * Creates the object.
39
-     *
40
-     * Note that you MUST supply the parent principal information.
41
-     */
42
-    public function __construct(DAVACL\PrincipalBackend\BackendInterface $principalBackend, array $principalInfo)
43
-    {
44
-        $this->principalInfo = $principalInfo;
45
-        $this->principalBackend = $principalBackend;
46
-    }
47
-
48
-    /**
49
-     * Returns this principals name.
50
-     *
51
-     * @return string
52
-     */
53
-    public function getName()
54
-    {
55
-        return 'calendar-proxy-read';
56
-    }
57
-
58
-    /**
59
-     * Returns the last modification time.
60
-     */
61
-    public function getLastModified()
62
-    {
63
-        return null;
64
-    }
65
-
66
-    /**
67
-     * Deletes the current node.
68
-     *
69
-     * @throws DAV\Exception\Forbidden
70
-     */
71
-    public function delete()
72
-    {
73
-        throw new DAV\Exception\Forbidden('Permission denied to delete node');
74
-    }
75
-
76
-    /**
77
-     * Renames the node.
78
-     *
79
-     * @param string $name The new name
80
-     *
81
-     * @throws DAV\Exception\Forbidden
82
-     */
83
-    public function setName($name)
84
-    {
85
-        throw new DAV\Exception\Forbidden('Permission denied to rename file');
86
-    }
87
-
88
-    /**
89
-     * Returns a list of alternative urls for a principal.
90
-     *
91
-     * This can for example be an email address, or ldap url.
92
-     *
93
-     * @return array
94
-     */
95
-    public function getAlternateUriSet()
96
-    {
97
-        return [];
98
-    }
99
-
100
-    /**
101
-     * Returns the full principal url.
102
-     *
103
-     * @return string
104
-     */
105
-    public function getPrincipalUrl()
106
-    {
107
-        return $this->principalInfo['uri'].'/'.$this->getName();
108
-    }
109
-
110
-    /**
111
-     * Returns the list of group members.
112
-     *
113
-     * If this principal is a group, this function should return
114
-     * all member principal uri's for the group.
115
-     *
116
-     * @return array
117
-     */
118
-    public function getGroupMemberSet()
119
-    {
120
-        return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl());
121
-    }
122
-
123
-    /**
124
-     * Returns the list of groups this principal is member of.
125
-     *
126
-     * If this principal is a member of a (list of) groups, this function
127
-     * should return a list of principal uri's for it's members.
128
-     *
129
-     * @return array
130
-     */
131
-    public function getGroupMembership()
132
-    {
133
-        return $this->principalBackend->getGroupMembership($this->getPrincipalUrl());
134
-    }
135
-
136
-    /**
137
-     * Sets a list of group members.
138
-     *
139
-     * If this principal is a group, this method sets all the group members.
140
-     * The list of members is always overwritten, never appended to.
141
-     *
142
-     * This method should throw an exception if the members could not be set.
143
-     */
144
-    public function setGroupMemberSet(array $principals)
145
-    {
146
-        $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals);
147
-    }
148
-
149
-    /**
150
-     * Returns the displayname.
151
-     *
152
-     * This should be a human readable name for the principal.
153
-     * If none is available, return the nodename.
154
-     *
155
-     * @return string
156
-     */
157
-    public function getDisplayName()
158
-    {
159
-        return $this->getName();
160
-    }
23
+	/**
24
+	 * Principal information from the parent principal.
25
+	 *
26
+	 * @var array
27
+	 */
28
+	protected $principalInfo;
29
+
30
+	/**
31
+	 * Principal backend.
32
+	 *
33
+	 * @var DAVACL\PrincipalBackend\BackendInterface
34
+	 */
35
+	protected $principalBackend;
36
+
37
+	/**
38
+	 * Creates the object.
39
+	 *
40
+	 * Note that you MUST supply the parent principal information.
41
+	 */
42
+	public function __construct(DAVACL\PrincipalBackend\BackendInterface $principalBackend, array $principalInfo)
43
+	{
44
+		$this->principalInfo = $principalInfo;
45
+		$this->principalBackend = $principalBackend;
46
+	}
47
+
48
+	/**
49
+	 * Returns this principals name.
50
+	 *
51
+	 * @return string
52
+	 */
53
+	public function getName()
54
+	{
55
+		return 'calendar-proxy-read';
56
+	}
57
+
58
+	/**
59
+	 * Returns the last modification time.
60
+	 */
61
+	public function getLastModified()
62
+	{
63
+		return null;
64
+	}
65
+
66
+	/**
67
+	 * Deletes the current node.
68
+	 *
69
+	 * @throws DAV\Exception\Forbidden
70
+	 */
71
+	public function delete()
72
+	{
73
+		throw new DAV\Exception\Forbidden('Permission denied to delete node');
74
+	}
75
+
76
+	/**
77
+	 * Renames the node.
78
+	 *
79
+	 * @param string $name The new name
80
+	 *
81
+	 * @throws DAV\Exception\Forbidden
82
+	 */
83
+	public function setName($name)
84
+	{
85
+		throw new DAV\Exception\Forbidden('Permission denied to rename file');
86
+	}
87
+
88
+	/**
89
+	 * Returns a list of alternative urls for a principal.
90
+	 *
91
+	 * This can for example be an email address, or ldap url.
92
+	 *
93
+	 * @return array
94
+	 */
95
+	public function getAlternateUriSet()
96
+	{
97
+		return [];
98
+	}
99
+
100
+	/**
101
+	 * Returns the full principal url.
102
+	 *
103
+	 * @return string
104
+	 */
105
+	public function getPrincipalUrl()
106
+	{
107
+		return $this->principalInfo['uri'].'/'.$this->getName();
108
+	}
109
+
110
+	/**
111
+	 * Returns the list of group members.
112
+	 *
113
+	 * If this principal is a group, this function should return
114
+	 * all member principal uri's for the group.
115
+	 *
116
+	 * @return array
117
+	 */
118
+	public function getGroupMemberSet()
119
+	{
120
+		return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl());
121
+	}
122
+
123
+	/**
124
+	 * Returns the list of groups this principal is member of.
125
+	 *
126
+	 * If this principal is a member of a (list of) groups, this function
127
+	 * should return a list of principal uri's for it's members.
128
+	 *
129
+	 * @return array
130
+	 */
131
+	public function getGroupMembership()
132
+	{
133
+		return $this->principalBackend->getGroupMembership($this->getPrincipalUrl());
134
+	}
135
+
136
+	/**
137
+	 * Sets a list of group members.
138
+	 *
139
+	 * If this principal is a group, this method sets all the group members.
140
+	 * The list of members is always overwritten, never appended to.
141
+	 *
142
+	 * This method should throw an exception if the members could not be set.
143
+	 */
144
+	public function setGroupMemberSet(array $principals)
145
+	{
146
+		$this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals);
147
+	}
148
+
149
+	/**
150
+	 * Returns the displayname.
151
+	 *
152
+	 * This should be a human readable name for the principal.
153
+	 * If none is available, return the nodename.
154
+	 *
155
+	 * @return string
156
+	 */
157
+	public function getDisplayName()
158
+	{
159
+		return $this->getName();
160
+	}
161 161
 }
Please login to merge, or discard this patch.
htdocs/includes/sabre/sabre/dav/lib/CalDAV/Principal/Collection.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -20,13 +20,13 @@
 block discarded – undo
20 20
  */
21 21
 class Collection extends DAVACL\PrincipalCollection
22 22
 {
23
-    /**
24
-     * Returns a child object based on principal information.
25
-     *
26
-     * @return User
27
-     */
28
-    public function getChildForPrincipal(array $principalInfo)
29
-    {
30
-        return new User($this->principalBackend, $principalInfo);
31
-    }
23
+	/**
24
+	 * Returns a child object based on principal information.
25
+	 *
26
+	 * @return User
27
+	 */
28
+	public function getChildForPrincipal(array $principalInfo)
29
+	{
30
+		return new User($this->principalBackend, $principalInfo);
31
+	}
32 32
 }
Please login to merge, or discard this patch.