Completed
Push — master ( c3fc78...f5e16d )
by Morris
20:41
created

Plugin::propFindDefaultCalendarUrl()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 2
nop 2
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OCA\DAV\CalDAV\Schedule;
26
27
use OCA\DAV\CalDAV\CalDavBackend;
28
use OCA\DAV\CalDAV\CalendarHome;
29
use Sabre\DAV\INode;
30
use Sabre\DAV\PropFind;
31
use Sabre\DAV\Server;
32
use Sabre\DAV\Xml\Property\LocalHref;
33
use Sabre\DAVACL\IPrincipal;
34
use Sabre\HTTP\RequestInterface;
35
use Sabre\HTTP\ResponseInterface;
36
use Sabre\VObject\Component\VCalendar;
37
use Sabre\VObject\Reader;
38
39
class Plugin extends \Sabre\CalDAV\Schedule\Plugin {
40
41
	/**
42
	 * Initializes the plugin
43
	 *
44
	 * @param Server $server
45
	 * @return void
46
	 */
47
	function initialize(Server $server) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
		parent::initialize($server);
49
		$server->on('propFind', [$this, 'propFindDefaultCalendarUrl'], 90);
50
	}
51
52
	/**
53
	 * Returns a list of addresses that are associated with a principal.
54
	 *
55
	 * @param string $principal
56
	 * @return array
57
	 */
58
	protected function getAddressesForPrincipal($principal) {
59
		$result = parent::getAddressesForPrincipal($principal);
60
61
		if ($result === null) {
62
			$result = [];
63
		}
64
65
		return $result;
66
	}
67
68
	/**
69
	 * Always use the personal calendar as target for scheduled events
70
	 *
71
	 * @param PropFind $propFind
72
	 * @param INode $node
73
	 * @return void
74
	 */
75
	function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
		if ($node instanceof IPrincipal) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAVACL\IPrincipal does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
			$propFind->handle('{' . self::NS_CALDAV . '}schedule-default-calendar-URL', function() use ($node) {
78
				/** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */
79
				$caldavPlugin = $this->server->getPlugin('caldav');
80
				$principalUrl = $node->getPrincipalUrl();
81
82
				$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
83
84
				if (!$calendarHomePath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $calendarHomePath of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
85
					return null;
86
				}
87
88
				/** @var CalendarHome $calendarHome */
89
				$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
90
				if (!$calendarHome->childExists(CalDavBackend::PERSONAL_CALENDAR_URI)) {
91
					$calendarHome->getCalDAVBackend()->createCalendar($principalUrl, CalDavBackend::PERSONAL_CALENDAR_URI, [
92
						'{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
93
					]);
94
				}
95
96
				$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . CalDavBackend::PERSONAL_CALENDAR_URI, [], 1);
97
				if (empty($result)) {
98
					return null;
99
				}
100
101
				return new LocalHref($result[0]['href']);
102
			});
103
		}
104
	}
105
106
	/**
107
	 * This method is triggered whenever there was a calendar object gets
108
	 * created or updated.
109
	 *
110
	 * Basically just a copy of parent::calendarObjectChange, with the change
111
	 * from:
112
	 * $addresses = $this->getAddressesForPrincipal($calendarNode->getOwner());
113
	 * to:
114
	 * $addresses = $this->getAddressesForPrincipal($calendarNode->getPrincipalURI());
115
	 *
116
	 * @param RequestInterface $request HTTP request
117
	 * @param ResponseInterface $response HTTP Response
118
	 * @param VCalendar $vCal Parsed iCalendar object
119
	 * @param mixed $calendarPath Path to calendar collection
120
	 * @param mixed $modified The iCalendar object has been touched.
121
	 * @param mixed $isNew Whether this was a new item or we're updating one
122
	 * @return void
123
	 */
124
	function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $vCal, $calendarPath, &$modified, $isNew) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
126
		if (!$this->scheduleReply($this->server->httpRequest)) {
127
			return;
128
		}
129
130
		$calendarNode = $this->server->tree->getNodeForPath($calendarPath);
131
132
		$addresses = $this->getAddressesForPrincipal(
133
			$calendarNode->getPrincipalURI()
134
		);
135
136
		if (!$isNew) {
137
			$node = $this->server->tree->getNodeForPath($request->getPath());
138
			$oldObj = Reader::read($node->get());
139
		} else {
140
			$oldObj = null;
141
		}
142
143
		$this->processICalendarChange($oldObj, $vCal, $addresses, [], $modified);
144
145
		if ($oldObj) {
146
			// Destroy circular references so PHP will GC the object.
147
			$oldObj->destroy();
148
		}
149
150
	}
151
152
	/**
153
	 * This method checks the 'Schedule-Reply' header
154
	 * and returns false if it's 'F', otherwise true.
155
	 *
156
	 * Copied from Sabre/DAV's Schedule plugin, because it's
157
	 * private for whatever reason
158
	 *
159
	 * @param RequestInterface $request
160
	 * @return bool
161
	 */
162
	private function scheduleReply(RequestInterface $request) {
163
164
		$scheduleReply = $request->getHeader('Schedule-Reply');
165
		return $scheduleReply !== 'F';
166
167
	}
168
}
169