1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Juan Pablo Villafañez <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
6
|
|
|
* @license AGPL-3.0 |
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
|
|
|
|
22
|
|
|
namespace OCA\DAV\Connector\Sabre; |
23
|
|
|
|
24
|
|
|
use Sabre\DAV\Server; |
|
|
|
|
25
|
|
|
use Sabre\DAV\ServerPlugin; |
26
|
|
|
use Sabre\DAV\PropFind; |
27
|
|
|
use Sabre\DAV\Exception\BadRequest; |
28
|
|
|
use Sabre\DAV\Exception\NotImplemented; |
29
|
|
|
use OCA\DAV\Files\Xml\SearchRequest; |
30
|
|
|
use OCP\ISearch; |
31
|
|
|
use OC\Search\Result\File as FileResult; |
32
|
|
|
|
33
|
|
|
class FilesSearchReportPlugin extends ServerPlugin { |
34
|
|
|
// namespace |
35
|
|
|
const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
36
|
|
|
const REPORT_NAME = '{http://owncloud.org/ns}search-files'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Reference to main server object |
40
|
|
|
* |
41
|
|
|
* @var Server |
42
|
|
|
*/ |
43
|
|
|
private $server; |
44
|
|
|
|
45
|
|
|
/** @var ISearch */ |
46
|
|
|
private $searchService; |
47
|
|
|
|
48
|
|
|
public function __construct(ISearch $searchService) { |
49
|
|
|
$this->searchService = $searchService; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This initializes the plugin. |
54
|
|
|
* |
55
|
|
|
* This function is called by \Sabre\DAV\Server, after |
56
|
|
|
* addPlugin is called. |
57
|
|
|
* |
58
|
|
|
* This method should set up the required event subscriptions. |
59
|
|
|
* |
60
|
|
|
* @param Server $server |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function initialize(Server $server) { |
64
|
|
|
$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
65
|
|
|
|
66
|
|
|
$server->xml->elementMap[self::REPORT_NAME] = SearchRequest::class; |
67
|
|
|
|
68
|
|
|
$this->server = $server; |
69
|
|
|
$this->server->on('report', [$this, 'onReport']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a list of reports this plugin supports. |
74
|
|
|
* |
75
|
|
|
* This will be used in the {DAV:}supported-report-set property. |
76
|
|
|
* |
77
|
|
|
* @param string $uri |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getSupportedReportSet($uri) { |
81
|
|
|
return [self::REPORT_NAME]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* REPORT operations to look for files |
86
|
|
|
* |
87
|
|
|
* @param string $reportName |
88
|
|
|
* @param mixed $report |
89
|
|
|
* @param string $uri |
90
|
|
|
* @return bool |
91
|
|
|
* @throws BadRequest |
92
|
|
|
* @throws NotImplemented |
93
|
|
|
* @internal param $ [] $report |
94
|
|
|
*/ |
95
|
|
|
public function onReport($reportName, $report, $uri) { |
96
|
|
|
$reportTargetNode = $this->server->tree->getNodeForPath($uri); |
97
|
|
|
if (!$reportTargetNode instanceof Directory || |
98
|
|
|
$reportName !== self::REPORT_NAME) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($reportTargetNode->getPath() !== '/') { |
103
|
|
|
throw new NotImplemented('Search report only available in the root folder of the user'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$requestedProps = $report->properties; |
107
|
|
|
$searchInfo = $report->searchInfo; |
108
|
|
|
|
109
|
|
|
if (!isset($searchInfo['pattern'])) { |
110
|
|
|
throw new BadRequest('Search pattern cannot be empty'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$limit = 30; |
114
|
|
|
if (isset($searchInfo['limit'])) { |
115
|
|
|
$limit = $searchInfo['limit']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$searchResults = $this->searchService->searchPaged( |
119
|
|
|
$searchInfo['pattern'], |
120
|
|
|
['files'], |
121
|
|
|
1, |
122
|
|
|
$limit |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath()); |
126
|
|
|
|
127
|
|
|
$xml = $this->server->generateMultiStatus( |
128
|
|
|
$this->getSearchResultIterator($filesUri, $searchResults, $requestedProps) |
129
|
|
|
); |
130
|
|
|
$this->server->httpResponse->setStatus(207); |
131
|
|
|
$this->server->httpResponse->setHeader( |
132
|
|
|
'Content-Type', |
133
|
|
|
'application/xml; charset=utf-8' |
134
|
|
|
); |
135
|
|
|
$this->server->httpResponse->setBody($xml); |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $filesUri the base uri for this user's files directory, |
142
|
|
|
* usually /files/username |
143
|
|
|
* @param File[] $searchResults the results coming from the search service, |
144
|
|
|
* within the files app |
145
|
|
|
* @param array $requestedProps the list of requested webDAV properties |
146
|
|
|
* @return \Generator a generator to traverse over the properties of the |
147
|
|
|
* search result, suitable for server's multistatus response |
148
|
|
|
*/ |
149
|
|
|
private function getSearchResultIterator($filesUri, $searchResults, $requestedProps) { |
150
|
|
|
$paths = \array_map(function ($searchResult) use ($filesUri) { |
151
|
|
|
return $filesUri . $searchResult->path; |
152
|
|
|
}, $searchResults); |
153
|
|
|
|
154
|
|
|
$nodes = $this->server->tree->getMultipleNodes($paths); |
155
|
|
|
|
156
|
|
|
$propFindType = $requestedProps ? PropFind::NORMAL : PropFind::ALLPROPS; |
157
|
|
|
|
158
|
|
View Code Duplication |
foreach ($nodes as $path => $node) { |
|
|
|
|
159
|
|
|
$propFind = new PropFind( |
160
|
|
|
$path, |
161
|
|
|
$requestedProps, |
162
|
|
|
0, |
163
|
|
|
$propFindType |
164
|
|
|
); |
165
|
|
|
$this->server->getPropertiesByNode($propFind, $node); |
166
|
|
|
|
167
|
|
|
$result = $propFind->getResultForMultiStatus(); |
168
|
|
|
$result['href'] = $propFind->getPath(); |
169
|
|
|
yield $result; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the base uri of the files root by removing |
175
|
|
|
* the subpath from the URI |
176
|
|
|
* |
177
|
|
|
* @param string $uri URI from this request |
178
|
|
|
* @param string $subPath subpath to remove from the URI |
179
|
|
|
* |
180
|
|
|
* @return string files base uri |
181
|
|
|
*/ |
182
|
|
|
private function getFilesBaseUri($uri, $subPath) { |
183
|
|
|
$uri = \trim($uri, '/'); |
184
|
|
|
$subPath = \trim($subPath, '/'); |
185
|
|
View Code Duplication |
if ($subPath === '') { |
|
|
|
|
186
|
|
|
$filesUri = $uri; |
187
|
|
|
} else { |
188
|
|
|
$filesUri = \substr($uri, 0, \strlen($uri) - \strlen($subPath)); |
189
|
|
|
} |
190
|
|
|
$filesUri = \trim($filesUri, '/'); |
191
|
|
|
if ($filesUri === '') { |
192
|
|
|
return ''; |
193
|
|
|
} |
194
|
|
|
return '/' . $filesUri; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: