Completed
Push — master ( 95139b...282bde )
by Robin
06:20 queued 04:36
created

SearchHandler::getQueryForXML()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 1
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace SearchDAV\DAV;
23
24
use Sabre\DAV\Exception\BadRequest;
25
use Sabre\DAV\PropFind;
26
use Sabre\DAV\Server;
27
use Sabre\HTTP\ResponseInterface;
28
use SearchDAV\Backend\ISearchBackend;
29
use SearchDAV\Backend\SearchPropertyDefinition;
30
use SearchDAV\Backend\SearchResult;
31
use SearchDAV\Query\Operator;
32
use SearchDAV\Query\Order;
33
use SearchDAV\Query\Query;
34
use SearchDAV\XML\BasicSearch;
35
36
class SearchHandler {
37
	/** @var ISearchBackend */
38
	private $searchBackend;
39
40
	/** @var PathHelper */
41
	private $pathHelper;
42
43
	/** @var Server */
44
	private $server;
45
46
	/**
47
	 * @param ISearchBackend $searchBackend
48
	 * @param PathHelper $pathHelper
49
	 * @param Server $server
50
	 */
51
	public function __construct(ISearchBackend $searchBackend, PathHelper $pathHelper, Server $server) {
52
		$this->searchBackend = $searchBackend;
53
		$this->pathHelper = $pathHelper;
54
		$this->server = $server;
55
	}
56
57
	public function handleSearchRequest($xml, ResponseInterface $response) {
58
		if (!isset($xml['{DAV:}basicsearch'])) {
59
			$response->setStatus(400);
60
			$response->setBody('Unexpected xml content for searchrequest, expected basicsearch');
61
			return false;
62
		}
63
		/** @var BasicSearch $query */
64
		$query = $xml['{DAV:}basicsearch'];
65
		if (!$query->where) {
66
			$response->setStatus(400);
67
			$response->setBody('Parse error: Missing {DAV:}where from {DAV:}basicsearch');
68
			return false;
69
		}
70
		if (!$query->select) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query->select of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
			$response->setStatus(400);
72
			$response->setBody('Parse error: Missing {DAV:}select from {DAV:}basicsearch');
73
			return false;
74
		}
75
		$response->setStatus(207);
76
		$response->setHeader('Content-Type', 'application/xml; charset="utf-8"');
77
		$allProps = [];
78
		foreach ($query->from as $scope) {
79
			$scope->path = $this->pathHelper->getPathFromUri($scope->href);
80
			$props = $this->searchBackend->getPropertyDefinitionsForScope($scope->href, $scope->path);
81
			foreach ($props as $prop) {
82
				$allProps[$prop->name] = $prop;
83
			}
84
		}
85
		try {
86
			$results = $this->searchBackend->search($this->getQueryForXML($query, $allProps));
87
		} catch (BadRequest $e) {
88
			$response->setStatus(400);
89
			$response->setBody($e->getMessage());
90
			return false;
91
		}
92
		$data = $this->server->generateMultiStatus(iterator_to_array($this->getPropertiesIteratorResults($results, $query->select)), false);
93
		$response->setBody($data);
94
		return false;
95
	}
96
97
	/**
98
	 * @param BasicSearch $xml
99
	 * @param SearchPropertyDefinition[] $allProps
100
	 * @return Query
101
	 */
102
	private function getQueryForXML(BasicSearch $xml, array $allProps) {
103
		$orderBy = array_map(function (\SearchDAV\XML\Order $order) use ($allProps) {
104
			if (!isset($allProps[$order->property])) {
105
				throw new BadRequest('requested order by property is not a valid property for this scope');
106
			}
107
			$prop = $allProps[$order->property];
108
			if (!$prop->sortable) {
109
				throw new BadRequest('requested order by property is not sortable');
110
			}
111
			return new Order($prop, $order->order);
112
		}, $xml->orderBy);
113
		$select = array_map(function ($propName) use ($allProps) {
114
			if (!isset($allProps[$propName])) {
115
				throw new BadRequest('requested property is not a valid property for this scope');
116
			}
117
			$prop = $allProps[$propName];
118
			if (!$prop->selectable) {
119
				throw new BadRequest('requested property is not selectable');
120
			}
121
			return $prop;
122
		}, $xml->select);
123
124
		$where = $this->transformOperator($xml->where, $allProps);
125
126
		return new Query($select, $xml->from, $where, $orderBy, $xml->limit);
127
	}
128
129
	private function transformOperator(\SearchDAV\XML\Operator $operator, array $allProps) {
130
		$arguments = array_map(function ($argument) use ($allProps) {
131
			if (is_string($argument)) {
132
				if (!isset($allProps[$argument])) {
133
					throw new BadRequest('requested search property is not a valid property for this scope');
134
				}
135
				$prop = $allProps[$argument];
136
				if (!$prop->searchable) {
137
					throw new BadRequest('requested search property is not searchable');
138
				}
139
				return $prop;
140
			} else if ($argument instanceof \SearchDAV\XML\Operator) {
141
				return $this->transformOperator($argument, $allProps);
142
			} else {
143
				return $argument;
144
			}
145
		}, $operator->arguments);
146
147
		return new Operator($operator->type, $arguments);
148
	}
149
150
	/**
151
	 * Returns a list of properties for a given path
152
	 *
153
	 * The path that should be supplied should have the baseUrl stripped out
154
	 * The list of properties should be supplied in Clark notation. If the list is empty
155
	 * 'allprops' is assumed.
156
	 *
157
	 * If a depth of 1 is requested child elements will also be returned.
158
	 *
159
	 * @param SearchResult[] $results
160
	 * @param array $propertyNames
161
	 * @param int $depth
162
	 * @return \Iterator
163
	 */
164
	private function getPropertiesIteratorResults($results, $propertyNames = [], $depth = 0) {
165
		$propFindType = $propertyNames ? PropFind::NORMAL : PropFind::ALLPROPS;
166
167
		foreach ($results as $result) {
168
			$node = $result->node;
169
			$propFind = new PropFind($result->href, (array)$propertyNames, $depth, $propFindType);
170
			$r = $this->server->getPropertiesByNode($propFind, $node);
171
			if ($r) {
172
				$result = $propFind->getResultForMultiStatus();
173
				$result['href'] = $propFind->getPath();
174
175
				// WebDAV recommends adding a slash to the path, if the path is
176
				// a collection.
177
				// Furthermore, iCal also demands this to be the case for
178
				// principals. This is non-standard, but we support it.
179
				$resourceType = $this->server->getResourceTypeForNode($node);
180
				if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
181
					$result['href'] .= '/';
182
				}
183
				yield $result;
184
			}
185
		}
186
	}
187
}
188