Completed
Push — master ( e6780c...89b6ee )
by Morris
33:24 queued 16:36
created

Plugin::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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
				if (!$calendarHomePath) {
84
					return null;
85
				}
86
87
				if (strpos($principalUrl, 'principals/users') === 0) {
88
					$uri = CalDavBackend::PERSONAL_CALENDAR_URI;
89
					$displayname = CalDavBackend::PERSONAL_CALENDAR_NAME;
90
				} elseif (strpos($principalUrl, 'principals/calendar-resources') === 0 ||
91
						  strpos($principalUrl, 'principals/calendar-rooms') === 0) {
92
					$uri = CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI;
93
					$displayname = CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME;
94
				} else {
95
					// How did we end up here?
96
					// TODO - throw exception or just ignore?
97
					return null;
98
				}
99
100
				/** @var CalendarHome $calendarHome */
101
				$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
102
				if (!$calendarHome->childExists($uri)) {
103
					$calendarHome->getCalDAVBackend()->createCalendar($principalUrl, $uri, [
104
						'{DAV:}displayname' => $displayname,
105
					]);
106
				}
107
108
				$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1);
109
				if (empty($result)) {
110
					return null;
111
				}
112
113
				return new LocalHref($result[0]['href']);
114
			});
115
		}
116
	}
117
118
	/**
119
	 * This method is triggered whenever there was a calendar object gets
120
	 * created or updated.
121
	 *
122
	 * Basically just a copy of parent::calendarObjectChange, with the change
123
	 * from:
124
	 * $addresses = $this->getAddressesForPrincipal($calendarNode->getOwner());
125
	 * to:
126
	 * $addresses = $this->getAddressesForPrincipal($calendarNode->getPrincipalURI());
127
	 *
128
	 * @param RequestInterface $request HTTP request
129
	 * @param ResponseInterface $response HTTP Response
130
	 * @param VCalendar $vCal Parsed iCalendar object
131
	 * @param mixed $calendarPath Path to calendar collection
132
	 * @param mixed $modified The iCalendar object has been touched.
133
	 * @param mixed $isNew Whether this was a new item or we're updating one
134
	 * @return void
135
	 */
136
	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...
137
138
		if (!$this->scheduleReply($this->server->httpRequest)) {
139
			return;
140
		}
141
142
		$calendarNode = $this->server->tree->getNodeForPath($calendarPath);
143
144
		$addresses = $this->getAddressesForPrincipal(
145
			$calendarNode->getPrincipalURI()
146
		);
147
148
		if (!$isNew) {
149
			$node = $this->server->tree->getNodeForPath($request->getPath());
150
			$oldObj = Reader::read($node->get());
151
		} else {
152
			$oldObj = null;
153
		}
154
155
		$this->processICalendarChange($oldObj, $vCal, $addresses, [], $modified);
156
157
		if ($oldObj) {
158
			// Destroy circular references so PHP will GC the object.
159
			$oldObj->destroy();
160
		}
161
162
	}
163
164
	/**
165
	 * This method checks the 'Schedule-Reply' header
166
	 * and returns false if it's 'F', otherwise true.
167
	 *
168
	 * Copied from Sabre/DAV's Schedule plugin, because it's
169
	 * private for whatever reason
170
	 *
171
	 * @param RequestInterface $request
172
	 * @return bool
173
	 */
174
	private function scheduleReply(RequestInterface $request) {
175
176
		$scheduleReply = $request->getHeader('Schedule-Reply');
177
		return $scheduleReply !== 'F';
178
179
	}
180
}
181