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