Completed
Push — master ( d60977...1e7efd )
by Robin
02:37
created

SearchHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 0
loc 86
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B handleSearchRequest() 0 26 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\SearchResult;
30
use SearchDAV\XML\BasicSearch;
31
32
class SearchHandler {
33
	/** @var ISearchBackend */
34
	private $searchBackend;
35
36
	/** @var PathHelper */
37
	private $pathHelper;
38
39
	/** @var Server */
40
	private $server;
41
42
	/**
43
	 * @param ISearchBackend $searchBackend
44
	 * @param PathHelper $pathHelper
45
	 * @param Server $server
46
	 */
47
	public function __construct(ISearchBackend $searchBackend, PathHelper $pathHelper, Server $server) {
48
		$this->searchBackend = $searchBackend;
49
		$this->pathHelper = $pathHelper;
50
		$this->server = $server;
51
	}
52
53
	public function handleSearchRequest($xml, ResponseInterface $response) {
54
		if (!$xml['{DAV:}basicsearch']) {
55
			throw new BadRequest('Unexpected xml content for searchrequest, expected basicsearch');
56
		}
57
		/** @var BasicSearch $query */
58
		$query = $xml['{DAV:}basicsearch'];
59
		if (!$query->where) {
60
			$response->setStatus(400);
61
			$response->setBody('Parse error: Missing {DAV:}where from {DAV:}basicsearch');
62
			return false;
63
		}
64
		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...
65
			$response->setStatus(400);
66
			$response->setBody('Parse error: Missing {DAV:}select from {DAV:}basicsearch');
67
			return false;
68
		}
69
		$response->setStatus(207);
70
		$response->setHeader('Content-Type', 'application/xml; charset="utf-8"');
71
		foreach ($query->from as $scope) {
72
			$scope->path = $this->pathHelper->getPathFromUri($scope->href);
73
		}
74
		$results = $this->searchBackend->search($query);
75
		$data = $this->server->generateMultiStatus(iterator_to_array($this->getPropertiesIteratorResults($results, $query->select)), false);
76
		$response->setBody($data);
77
		return false;
78
	}
79
80
	/**
81
	 * Returns a list of properties for a given path
82
	 *
83
	 * The path that should be supplied should have the baseUrl stripped out
84
	 * The list of properties should be supplied in Clark notation. If the list is empty
85
	 * 'allprops' is assumed.
86
	 *
87
	 * If a depth of 1 is requested child elements will also be returned.
88
	 *
89
	 * @param SearchResult[] $results
90
	 * @param array $propertyNames
91
	 * @param int $depth
92
	 * @return \Iterator
93
	 */
94
	private function getPropertiesIteratorResults($results, $propertyNames = [], $depth = 0) {
95
		$propFindType = $propertyNames ? PropFind::NORMAL : PropFind::ALLPROPS;
96
97
		foreach ($results as $result) {
98
			$node = $result->node;
99
			$propFind = new PropFind($result->href, (array)$propertyNames, $depth, $propFindType);
100
			$r = $this->server->getPropertiesByNode($propFind, $node);
101
			if ($r) {
102
				$result = $propFind->getResultForMultiStatus();
103
				$result['href'] = $propFind->getPath();
104
105
				// WebDAV recommends adding a slash to the path, if the path is
106
				// a collection.
107
				// Furthermore, iCal also demands this to be the case for
108
				// principals. This is non-standard, but we support it.
109
				$resourceType = $this->server->getResourceTypeForNode($node);
110
				if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
111
					$result['href'] .= '/';
112
				}
113
				yield $result;
114
			}
115
		}
116
	}
117
}
118