Completed
Push — master ( 634f9c...d18387 )
by Robin
04:16
created

SearchHandler   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 15
dl 0
loc 166
rs 9.1666
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C handleSearchRequest() 0 39 7
C getQueryForXML() 0 40 8
B transformOperator() 0 20 5
B getPropertiesIteratorResults() 0 23 6
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
		$arguments = array_map(function ($argument) use ($allProps) {
0 ignored issues
show
Unused Code introduced by
$arguments is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
			if (is_string($argument)) {
125
				if (!isset($allProps[$argument])) {
126
					throw new BadRequest('requested search property is not a valid property for this scope');
127
				}
128
				$prop = $allProps[$argument];
129
				if (!$prop->searchable) {
130
					throw new BadRequest('requested search property is not searchable');
131
				}
132
				return $prop;
133
			} else {
134
				return $argument;
135
			}
136
		}, $xml->where->arguments);
137
138
		$where = $this->transformOperator($xml->where, $allProps);
139
140
		return new Query($select, $xml->from, $where, $orderBy, $xml->limit);
141
	}
142
143
	private function transformOperator(\SearchDAV\XML\Operator $operator, array $allProps) {
144
		$arguments = array_map(function ($argument) use ($allProps) {
145
			if (is_string($argument)) {
146
				if (!isset($allProps[$argument])) {
147
					throw new BadRequest('requested search property is not a valid property for this scope');
148
				}
149
				$prop = $allProps[$argument];
150
				if (!$prop->searchable) {
151
					throw new BadRequest('requested search property is not searchable');
152
				}
153
				return $prop;
154
			} else if ($argument instanceof \SearchDAV\XML\Operator) {
155
				return $this->transformOperator($argument, $allProps);
156
			} else {
157
				return $argument;
158
			}
159
		}, $operator->arguments);
160
161
		return new Operator($operator->type, $arguments);
162
	}
163
164
	/**
165
	 * Returns a list of properties for a given path
166
	 *
167
	 * The path that should be supplied should have the baseUrl stripped out
168
	 * The list of properties should be supplied in Clark notation. If the list is empty
169
	 * 'allprops' is assumed.
170
	 *
171
	 * If a depth of 1 is requested child elements will also be returned.
172
	 *
173
	 * @param SearchResult[] $results
174
	 * @param array $propertyNames
175
	 * @param int $depth
176
	 * @return \Iterator
177
	 */
178
	private function getPropertiesIteratorResults($results, $propertyNames = [], $depth = 0) {
179
		$propFindType = $propertyNames ? PropFind::NORMAL : PropFind::ALLPROPS;
180
181
		foreach ($results as $result) {
182
			$node = $result->node;
183
			$propFind = new PropFind($result->href, (array)$propertyNames, $depth, $propFindType);
184
			$r = $this->server->getPropertiesByNode($propFind, $node);
185
			if ($r) {
186
				$result = $propFind->getResultForMultiStatus();
187
				$result['href'] = $propFind->getPath();
188
189
				// WebDAV recommends adding a slash to the path, if the path is
190
				// a collection.
191
				// Furthermore, iCal also demands this to be the case for
192
				// principals. This is non-standard, but we support it.
193
				$resourceType = $this->server->getResourceTypeForNode($node);
194
				if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
195
					$result['href'] .= '/';
196
				}
197
				yield $result;
198
			}
199
		}
200
	}
201
}
202