Completed
Push — master ( 424d62...2a7733 )
by Morris
11:53
created

SearchPlugin::report()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Georg Ehrke <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017 Georg Ehrke <[email protected]>
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OCA\DAV\CalDAV\Search;
22
23
use Sabre\DAV\Server;
24
use Sabre\DAV\ServerPlugin;
25
use OCA\DAV\CalDAV\CalendarHome;
26
27
class SearchPlugin extends ServerPlugin {
28
	const NS_Nextcloud = 'http://nextcloud.com/ns';
29
30
	/**
31
	 * Reference to SabreDAV server object.
32
	 *
33
	 * @var \Sabre\DAV\Server
34
	 */
35
	protected $server;
36
37
	/**
38
	 * This method should return a list of server-features.
39
	 *
40
	 * This is for example 'versioning' and is added to the DAV: header
41
	 * in an OPTIONS response.
42
	 *
43
	 * @return string[]
44
	 */
45
	public function getFeatures() {
46
		// May have to be changed to be detected
47
		return ['nc-calendar-search'];
48
	}
49
50
	/**
51
	 * Returns a plugin name.
52
	 *
53
	 * Using this name other plugins will be able to access other plugins
54
	 * using Sabre\DAV\Server::getPlugin
55
	 *
56
	 * @return string
57
	 */
58
	public function getPluginName() {
59
		return 'nc-calendar-search';
60
	}
61
62
	/**
63
	 * This initializes the plugin.
64
	 *
65
	 * This function is called by Sabre\DAV\Server, after
66
	 * addPlugin is called.
67
	 *
68
	 * This method should set up the required event subscriptions.
69
	 *
70
	 * @param Server $server
71
	 */
72
	public function initialize(Server $server) {
73
		$this->server = $server;
74
75
		$server->on('report', [$this, 'report']);
76
77
		$server->xml->elementMap['{' . self::NS_Nextcloud . '}calendar-search'] =
78
			'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport';
79
	}
80
81
	/**
82
	 * This functions handles REPORT requests specific to CalDAV
83
	 *
84
	 * @param string $reportName
85
	 * @param mixed $report
86
	 * @param mixed $path
87
	 * @return bool
88
	 */
89
	public function report($reportName, $report, $path) {
90
		switch ($reportName) {
91
			case '{' . self::NS_Nextcloud . '}calendar-search':
92
				$this->server->transactionType = 'report-nc-calendar-search';
93
				$this->calendarSearch($report);
94
				return false;
95
		}
96
	}
97
98
	/**
99
	 * Returns a list of reports this plugin supports.
100
	 *
101
	 * This will be used in the {DAV:}supported-report-set property.
102
	 * Note that you still need to subscribe to the 'report' event to actually
103
	 * implement them
104
	 *
105
	 * @param string $uri
106
	 * @return array
107
	 */
108
	public function getSupportedReportSet($uri) {
109
		$node = $this->server->tree->getNodeForPath($uri);
110
111
		$reports = [];
112
		if ($node instanceof CalendarHome) {
113
			$reports[] = '{' . self::NS_Nextcloud . '}calendar-search';
114
		}
115
116
		return $reports;
117
	}
118
119
	/**
120
	 * This function handles the calendar-query REPORT
121
	 *
122
	 * This report is used by clients to request calendar objects based on
123
	 * complex conditions.
124
	 *
125
	 * @param Xml\Request\CalendarSearchReport $report
126
	 * @return void
127
	 */
128
	private function calendarSearch($report) {
129
		$node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
130
		$depth = $this->server->getHTTPDepth(2);
131
132
		// The default result is an empty array
133
		$result = [];
134
135
		// If we're dealing with the calendar home, the calendar home itself is
136
		// responsible for the calendar-query
137
		if ($node instanceof CalendarHome && $depth == 2) {
138
139
			$nodePaths = $node->calendarSearch($report->filters, $report->limit, $report->offset);
140
141
			foreach ($nodePaths as $path) {
142
				list($properties) = $this->server->getPropertiesForPath(
143
					$this->server->getRequestUri() . '/' . $path,
144
					$report->properties);
145
				$result[] = $properties;
146
			}
147
		}
148
149
		$prefer = $this->server->getHTTPPrefer();
150
151
		$this->server->httpResponse->setStatus(207);
152
		$this->server->httpResponse->setHeader('Content-Type',
153
			'application/xml; charset=utf-8');
154
		$this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
155
		$this->server->httpResponse->setBody(
156
			$this->server->generateMultiStatus($result,
157
				$prefer['return'] === 'minimal'));
158
	}
159
}