@@ -26,404 +26,404 @@ discard block |
||
26 | 26 | */ |
27 | 27 | class Plugin extends DAV\ServerPlugin |
28 | 28 | { |
29 | - /** |
|
30 | - * reference to server class. |
|
31 | - * |
|
32 | - * @var DAV\Server |
|
33 | - */ |
|
34 | - protected $server; |
|
35 | - |
|
36 | - /** |
|
37 | - * enablePost turns on the 'actions' panel, which allows people to create |
|
38 | - * folders and upload files straight from a browser. |
|
39 | - * |
|
40 | - * @var bool |
|
41 | - */ |
|
42 | - protected $enablePost = true; |
|
43 | - |
|
44 | - /** |
|
45 | - * A list of properties that are usually not interesting. This can cut down |
|
46 | - * the browser output a bit by removing the properties that most people |
|
47 | - * will likely not want to see. |
|
48 | - * |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - public $uninterestingProperties = [ |
|
52 | - '{DAV:}supportedlock', |
|
53 | - '{DAV:}acl-restrictions', |
|
29 | + /** |
|
30 | + * reference to server class. |
|
31 | + * |
|
32 | + * @var DAV\Server |
|
33 | + */ |
|
34 | + protected $server; |
|
35 | + |
|
36 | + /** |
|
37 | + * enablePost turns on the 'actions' panel, which allows people to create |
|
38 | + * folders and upload files straight from a browser. |
|
39 | + * |
|
40 | + * @var bool |
|
41 | + */ |
|
42 | + protected $enablePost = true; |
|
43 | + |
|
44 | + /** |
|
45 | + * A list of properties that are usually not interesting. This can cut down |
|
46 | + * the browser output a bit by removing the properties that most people |
|
47 | + * will likely not want to see. |
|
48 | + * |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + public $uninterestingProperties = [ |
|
52 | + '{DAV:}supportedlock', |
|
53 | + '{DAV:}acl-restrictions', |
|
54 | 54 | // '{DAV:}supported-privilege-set', |
55 | - '{DAV:}supported-method-set', |
|
56 | - ]; |
|
57 | - |
|
58 | - /** |
|
59 | - * Creates the object. |
|
60 | - * |
|
61 | - * By default it will allow file creation and uploads. |
|
62 | - * Specify the first argument as false to disable this |
|
63 | - * |
|
64 | - * @param bool $enablePost |
|
65 | - */ |
|
66 | - public function __construct($enablePost = true) |
|
67 | - { |
|
68 | - $this->enablePost = $enablePost; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Initializes the plugin and subscribes to events. |
|
73 | - */ |
|
74 | - public function initialize(DAV\Server $server) |
|
75 | - { |
|
76 | - $this->server = $server; |
|
77 | - $this->server->on('method:GET', [$this, 'httpGetEarly'], 90); |
|
78 | - $this->server->on('method:GET', [$this, 'httpGet'], 200); |
|
79 | - $this->server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel'], 200); |
|
80 | - if ($this->enablePost) { |
|
81 | - $this->server->on('method:POST', [$this, 'httpPOST']); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * This method intercepts GET requests that have ?sabreAction=info |
|
87 | - * appended to the URL. |
|
88 | - */ |
|
89 | - public function httpGetEarly(RequestInterface $request, ResponseInterface $response) |
|
90 | - { |
|
91 | - $params = $request->getQueryParameters(); |
|
92 | - if (isset($params['sabreAction']) && 'info' === $params['sabreAction']) { |
|
93 | - return $this->httpGet($request, $response); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * This method intercepts GET requests to collections and returns the html. |
|
99 | - * |
|
100 | - * @return bool |
|
101 | - */ |
|
102 | - public function httpGet(RequestInterface $request, ResponseInterface $response) |
|
103 | - { |
|
104 | - // We're not using straight-up $_GET, because we want everything to be |
|
105 | - // unit testable. |
|
106 | - $getVars = $request->getQueryParameters(); |
|
107 | - |
|
108 | - // CSP headers |
|
109 | - $response->setHeader('Content-Security-Policy', "default-src 'none'; img-src 'self'; style-src 'self'; font-src 'self';"); |
|
110 | - |
|
111 | - $sabreAction = isset($getVars['sabreAction']) ? $getVars['sabreAction'] : null; |
|
112 | - |
|
113 | - switch ($sabreAction) { |
|
114 | - case 'asset': |
|
115 | - // Asset handling, such as images |
|
116 | - $this->serveAsset(isset($getVars['assetName']) ? $getVars['assetName'] : null); |
|
117 | - |
|
118 | - return false; |
|
119 | - default: |
|
120 | - case 'info': |
|
121 | - try { |
|
122 | - $this->server->tree->getNodeForPath($request->getPath()); |
|
123 | - } catch (DAV\Exception\NotFound $e) { |
|
124 | - // We're simply stopping when the file isn't found to not interfere |
|
125 | - // with other plugins. |
|
126 | - return; |
|
127 | - } |
|
128 | - |
|
129 | - $response->setStatus(200); |
|
130 | - $response->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
131 | - |
|
132 | - $response->setBody( |
|
133 | - $this->generateDirectoryIndex($request->getPath()) |
|
134 | - ); |
|
135 | - |
|
136 | - return false; |
|
137 | - |
|
138 | - case 'plugins': |
|
139 | - $response->setStatus(200); |
|
140 | - $response->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
141 | - |
|
142 | - $response->setBody( |
|
143 | - $this->generatePluginListing() |
|
144 | - ); |
|
145 | - |
|
146 | - return false; |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Handles POST requests for tree operations. |
|
152 | - * |
|
153 | - * @return bool |
|
154 | - */ |
|
155 | - public function httpPOST(RequestInterface $request, ResponseInterface $response) |
|
156 | - { |
|
157 | - $contentType = $request->getHeader('Content-Type'); |
|
158 | - if (!\is_string($contentType)) { |
|
159 | - return; |
|
160 | - } |
|
161 | - list($contentType) = explode(';', $contentType); |
|
162 | - if ('application/x-www-form-urlencoded' !== $contentType && |
|
163 | - 'multipart/form-data' !== $contentType) { |
|
164 | - return; |
|
165 | - } |
|
166 | - $postVars = $request->getPostData(); |
|
167 | - |
|
168 | - if (!isset($postVars['sabreAction'])) { |
|
169 | - return; |
|
170 | - } |
|
171 | - |
|
172 | - $uri = $request->getPath(); |
|
173 | - |
|
174 | - if ($this->server->emit('onBrowserPostAction', [$uri, $postVars['sabreAction'], $postVars])) { |
|
175 | - switch ($postVars['sabreAction']) { |
|
176 | - case 'mkcol': |
|
177 | - if (isset($postVars['name']) && trim($postVars['name'])) { |
|
178 | - // Using basename() because we won't allow slashes |
|
179 | - list(, $folderName) = Uri\split(trim($postVars['name'])); |
|
180 | - |
|
181 | - if (isset($postVars['resourceType'])) { |
|
182 | - $resourceType = explode(',', $postVars['resourceType']); |
|
183 | - } else { |
|
184 | - $resourceType = ['{DAV:}collection']; |
|
185 | - } |
|
186 | - |
|
187 | - $properties = []; |
|
188 | - foreach ($postVars as $varName => $varValue) { |
|
189 | - // Any _POST variable in clark notation is treated |
|
190 | - // like a property. |
|
191 | - if ('{' === $varName[0]) { |
|
192 | - // PHP will convert any dots to underscores. |
|
193 | - // This leaves us with no way to differentiate |
|
194 | - // the two. |
|
195 | - // Therefore we replace the string *DOT* with a |
|
196 | - // real dot. * is not allowed in uris so we |
|
197 | - // should be good. |
|
198 | - $varName = str_replace('*DOT*', '.', $varName); |
|
199 | - $properties[$varName] = $varValue; |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - $mkCol = new MkCol( |
|
204 | - $resourceType, |
|
205 | - $properties |
|
206 | - ); |
|
207 | - $this->server->createCollection($uri.'/'.$folderName, $mkCol); |
|
208 | - } |
|
209 | - break; |
|
210 | - |
|
211 | - // @codeCoverageIgnoreStart |
|
212 | - case 'put': |
|
213 | - if ($_FILES) { |
|
214 | - $file = current($_FILES); |
|
215 | - } else { |
|
216 | - break; |
|
217 | - } |
|
218 | - |
|
219 | - list(, $newName) = Uri\split(trim($file['name'])); |
|
220 | - if (isset($postVars['name']) && trim($postVars['name'])) { |
|
221 | - $newName = trim($postVars['name']); |
|
222 | - } |
|
223 | - |
|
224 | - // Making sure we only have a 'basename' component |
|
225 | - list(, $newName) = Uri\split($newName); |
|
226 | - |
|
227 | - if (is_uploaded_file($file['tmp_name'])) { |
|
228 | - $this->server->createFile($uri.'/'.$newName, fopen($file['tmp_name'], 'r')); |
|
229 | - } |
|
230 | - break; |
|
231 | - // @codeCoverageIgnoreEnd |
|
232 | - } |
|
233 | - } |
|
234 | - $response->setHeader('Location', $request->getUrl()); |
|
235 | - $response->setStatus(302); |
|
236 | - |
|
237 | - return false; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Escapes a string for html. |
|
242 | - * |
|
243 | - * @param string $value |
|
244 | - * |
|
245 | - * @return string |
|
246 | - */ |
|
247 | - public function escapeHTML($value) |
|
248 | - { |
|
249 | - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Generates the html directory index for a given url. |
|
254 | - * |
|
255 | - * @param string $path |
|
256 | - * |
|
257 | - * @return string |
|
258 | - */ |
|
259 | - public function generateDirectoryIndex($path) |
|
260 | - { |
|
261 | - $html = $this->generateHeader($path ? $path : '/', $path); |
|
262 | - |
|
263 | - $node = $this->server->tree->getNodeForPath($path); |
|
264 | - if ($node instanceof DAV\ICollection) { |
|
265 | - $html .= "<section><h1>Nodes</h1>\n"; |
|
266 | - $html .= '<table class="nodeTable">'; |
|
267 | - |
|
268 | - $subNodes = $this->server->getPropertiesForChildren($path, [ |
|
269 | - '{DAV:}displayname', |
|
270 | - '{DAV:}resourcetype', |
|
271 | - '{DAV:}getcontenttype', |
|
272 | - '{DAV:}getcontentlength', |
|
273 | - '{DAV:}getlastmodified', |
|
274 | - ]); |
|
275 | - |
|
276 | - foreach ($subNodes as $subPath => $subProps) { |
|
277 | - $subNode = $this->server->tree->getNodeForPath($subPath); |
|
278 | - $fullPath = $this->server->getBaseUri().HTTP\encodePath($subPath); |
|
279 | - list(, $displayPath) = Uri\split($subPath); |
|
280 | - |
|
281 | - $subNodes[$subPath]['subNode'] = $subNode; |
|
282 | - $subNodes[$subPath]['fullPath'] = $fullPath; |
|
283 | - $subNodes[$subPath]['displayPath'] = $displayPath; |
|
284 | - } |
|
285 | - uasort($subNodes, [$this, 'compareNodes']); |
|
286 | - |
|
287 | - foreach ($subNodes as $subProps) { |
|
288 | - $type = [ |
|
289 | - 'string' => 'Unknown', |
|
290 | - 'icon' => 'cog', |
|
291 | - ]; |
|
292 | - if (isset($subProps['{DAV:}resourcetype'])) { |
|
293 | - $type = $this->mapResourceType($subProps['{DAV:}resourcetype']->getValue(), $subProps['subNode']); |
|
294 | - } |
|
295 | - |
|
296 | - $html .= '<tr>'; |
|
297 | - $html .= '<td class="nameColumn"><a href="'.$this->escapeHTML($subProps['fullPath']).'"><span class="oi" data-glyph="'.$this->escapeHTML($type['icon']).'"></span> '.$this->escapeHTML($subProps['displayPath']).'</a></td>'; |
|
298 | - $html .= '<td class="typeColumn">'.$this->escapeHTML($type['string']).'</td>'; |
|
299 | - $html .= '<td>'; |
|
300 | - if (isset($subProps['{DAV:}getcontentlength'])) { |
|
301 | - $html .= $this->escapeHTML($subProps['{DAV:}getcontentlength'].' bytes'); |
|
302 | - } |
|
303 | - $html .= '</td><td>'; |
|
304 | - if (isset($subProps['{DAV:}getlastmodified'])) { |
|
305 | - $lastMod = $subProps['{DAV:}getlastmodified']->getTime(); |
|
306 | - $html .= $this->escapeHTML($lastMod->format('F j, Y, g:i a')); |
|
307 | - } |
|
308 | - $html .= '</td><td>'; |
|
309 | - if (isset($subProps['{DAV:}displayname'])) { |
|
310 | - $html .= $this->escapeHTML($subProps['{DAV:}displayname']); |
|
311 | - } |
|
312 | - $html .= '</td>'; |
|
313 | - |
|
314 | - $buttonActions = ''; |
|
315 | - if ($subProps['subNode'] instanceof DAV\IFile) { |
|
316 | - $buttonActions = '<a href="'.$this->escapeHTML($subProps['fullPath']).'?sabreAction=info"><span class="oi" data-glyph="info"></span></a>'; |
|
317 | - } |
|
318 | - $this->server->emit('browserButtonActions', [$subProps['fullPath'], $subProps['subNode'], &$buttonActions]); |
|
319 | - |
|
320 | - $html .= '<td>'.$buttonActions.'</td>'; |
|
321 | - $html .= '</tr>'; |
|
322 | - } |
|
323 | - |
|
324 | - $html .= '</table>'; |
|
325 | - } |
|
326 | - |
|
327 | - $html .= '</section>'; |
|
328 | - $html .= '<section><h1>Properties</h1>'; |
|
329 | - $html .= '<table class="propTable">'; |
|
330 | - |
|
331 | - // Allprops request |
|
332 | - $propFind = new PropFindAll($path); |
|
333 | - $properties = $this->server->getPropertiesByNode($propFind, $node); |
|
334 | - |
|
335 | - $properties = $propFind->getResultForMultiStatus()[200]; |
|
336 | - |
|
337 | - foreach ($properties as $propName => $propValue) { |
|
338 | - if (!in_array($propName, $this->uninterestingProperties)) { |
|
339 | - $html .= $this->drawPropertyRow($propName, $propValue); |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - $html .= '</table>'; |
|
344 | - $html .= '</section>'; |
|
345 | - |
|
346 | - /* Start of generating actions */ |
|
347 | - |
|
348 | - $output = ''; |
|
349 | - if ($this->enablePost) { |
|
350 | - $this->server->emit('onHTMLActionsPanel', [$node, &$output, $path]); |
|
351 | - } |
|
352 | - |
|
353 | - if ($output) { |
|
354 | - $html .= '<section><h1>Actions</h1>'; |
|
355 | - $html .= "<div class=\"actions\">\n"; |
|
356 | - $html .= $output; |
|
357 | - $html .= "</div>\n"; |
|
358 | - $html .= "</section>\n"; |
|
359 | - } |
|
360 | - |
|
361 | - $html .= $this->generateFooter(); |
|
362 | - |
|
363 | - $this->server->httpResponse->setHeader('Content-Security-Policy', "default-src 'none'; img-src 'self'; style-src 'self'; font-src 'self';"); |
|
364 | - |
|
365 | - return $html; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Generates the 'plugins' page. |
|
370 | - * |
|
371 | - * @return string |
|
372 | - */ |
|
373 | - public function generatePluginListing() |
|
374 | - { |
|
375 | - $html = $this->generateHeader('Plugins'); |
|
376 | - |
|
377 | - $html .= '<section><h1>Plugins</h1>'; |
|
378 | - $html .= '<table class="propTable">'; |
|
379 | - foreach ($this->server->getPlugins() as $plugin) { |
|
380 | - $info = $plugin->getPluginInfo(); |
|
381 | - $html .= '<tr><th>'.$info['name'].'</th>'; |
|
382 | - $html .= '<td>'.$info['description'].'</td>'; |
|
383 | - $html .= '<td>'; |
|
384 | - if (isset($info['link']) && $info['link']) { |
|
385 | - $html .= '<a href="'.$this->escapeHTML($info['link']).'"><span class="oi" data-glyph="book"></span></a>'; |
|
386 | - } |
|
387 | - $html .= '</td></tr>'; |
|
388 | - } |
|
389 | - $html .= '</table>'; |
|
390 | - $html .= '</section>'; |
|
391 | - |
|
392 | - /* Start of generating actions */ |
|
393 | - |
|
394 | - $html .= $this->generateFooter(); |
|
395 | - |
|
396 | - return $html; |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Generates the first block of HTML, including the <head> tag and page |
|
401 | - * header. |
|
402 | - * |
|
403 | - * Returns footer. |
|
404 | - * |
|
405 | - * @param string $title |
|
406 | - * @param string $path |
|
407 | - * |
|
408 | - * @return string |
|
409 | - */ |
|
410 | - public function generateHeader($title, $path = null) |
|
411 | - { |
|
412 | - $version = ''; |
|
413 | - if (DAV\Server::$exposeVersion) { |
|
414 | - $version = DAV\Version::VERSION; |
|
415 | - } |
|
416 | - |
|
417 | - $vars = [ |
|
418 | - 'title' => $this->escapeHTML($title), |
|
419 | - 'favicon' => $this->escapeHTML($this->getAssetUrl('favicon.ico')), |
|
420 | - 'style' => $this->escapeHTML($this->getAssetUrl('sabredav.css')), |
|
421 | - 'iconstyle' => $this->escapeHTML($this->getAssetUrl('openiconic/open-iconic.css')), |
|
422 | - 'logo' => $this->escapeHTML($this->getAssetUrl('sabredav.png')), |
|
423 | - 'baseUrl' => $this->server->getBaseUri(), |
|
424 | - ]; |
|
425 | - |
|
426 | - $html = <<<HTML |
|
55 | + '{DAV:}supported-method-set', |
|
56 | + ]; |
|
57 | + |
|
58 | + /** |
|
59 | + * Creates the object. |
|
60 | + * |
|
61 | + * By default it will allow file creation and uploads. |
|
62 | + * Specify the first argument as false to disable this |
|
63 | + * |
|
64 | + * @param bool $enablePost |
|
65 | + */ |
|
66 | + public function __construct($enablePost = true) |
|
67 | + { |
|
68 | + $this->enablePost = $enablePost; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Initializes the plugin and subscribes to events. |
|
73 | + */ |
|
74 | + public function initialize(DAV\Server $server) |
|
75 | + { |
|
76 | + $this->server = $server; |
|
77 | + $this->server->on('method:GET', [$this, 'httpGetEarly'], 90); |
|
78 | + $this->server->on('method:GET', [$this, 'httpGet'], 200); |
|
79 | + $this->server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel'], 200); |
|
80 | + if ($this->enablePost) { |
|
81 | + $this->server->on('method:POST', [$this, 'httpPOST']); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * This method intercepts GET requests that have ?sabreAction=info |
|
87 | + * appended to the URL. |
|
88 | + */ |
|
89 | + public function httpGetEarly(RequestInterface $request, ResponseInterface $response) |
|
90 | + { |
|
91 | + $params = $request->getQueryParameters(); |
|
92 | + if (isset($params['sabreAction']) && 'info' === $params['sabreAction']) { |
|
93 | + return $this->httpGet($request, $response); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * This method intercepts GET requests to collections and returns the html. |
|
99 | + * |
|
100 | + * @return bool |
|
101 | + */ |
|
102 | + public function httpGet(RequestInterface $request, ResponseInterface $response) |
|
103 | + { |
|
104 | + // We're not using straight-up $_GET, because we want everything to be |
|
105 | + // unit testable. |
|
106 | + $getVars = $request->getQueryParameters(); |
|
107 | + |
|
108 | + // CSP headers |
|
109 | + $response->setHeader('Content-Security-Policy', "default-src 'none'; img-src 'self'; style-src 'self'; font-src 'self';"); |
|
110 | + |
|
111 | + $sabreAction = isset($getVars['sabreAction']) ? $getVars['sabreAction'] : null; |
|
112 | + |
|
113 | + switch ($sabreAction) { |
|
114 | + case 'asset': |
|
115 | + // Asset handling, such as images |
|
116 | + $this->serveAsset(isset($getVars['assetName']) ? $getVars['assetName'] : null); |
|
117 | + |
|
118 | + return false; |
|
119 | + default: |
|
120 | + case 'info': |
|
121 | + try { |
|
122 | + $this->server->tree->getNodeForPath($request->getPath()); |
|
123 | + } catch (DAV\Exception\NotFound $e) { |
|
124 | + // We're simply stopping when the file isn't found to not interfere |
|
125 | + // with other plugins. |
|
126 | + return; |
|
127 | + } |
|
128 | + |
|
129 | + $response->setStatus(200); |
|
130 | + $response->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
131 | + |
|
132 | + $response->setBody( |
|
133 | + $this->generateDirectoryIndex($request->getPath()) |
|
134 | + ); |
|
135 | + |
|
136 | + return false; |
|
137 | + |
|
138 | + case 'plugins': |
|
139 | + $response->setStatus(200); |
|
140 | + $response->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
141 | + |
|
142 | + $response->setBody( |
|
143 | + $this->generatePluginListing() |
|
144 | + ); |
|
145 | + |
|
146 | + return false; |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Handles POST requests for tree operations. |
|
152 | + * |
|
153 | + * @return bool |
|
154 | + */ |
|
155 | + public function httpPOST(RequestInterface $request, ResponseInterface $response) |
|
156 | + { |
|
157 | + $contentType = $request->getHeader('Content-Type'); |
|
158 | + if (!\is_string($contentType)) { |
|
159 | + return; |
|
160 | + } |
|
161 | + list($contentType) = explode(';', $contentType); |
|
162 | + if ('application/x-www-form-urlencoded' !== $contentType && |
|
163 | + 'multipart/form-data' !== $contentType) { |
|
164 | + return; |
|
165 | + } |
|
166 | + $postVars = $request->getPostData(); |
|
167 | + |
|
168 | + if (!isset($postVars['sabreAction'])) { |
|
169 | + return; |
|
170 | + } |
|
171 | + |
|
172 | + $uri = $request->getPath(); |
|
173 | + |
|
174 | + if ($this->server->emit('onBrowserPostAction', [$uri, $postVars['sabreAction'], $postVars])) { |
|
175 | + switch ($postVars['sabreAction']) { |
|
176 | + case 'mkcol': |
|
177 | + if (isset($postVars['name']) && trim($postVars['name'])) { |
|
178 | + // Using basename() because we won't allow slashes |
|
179 | + list(, $folderName) = Uri\split(trim($postVars['name'])); |
|
180 | + |
|
181 | + if (isset($postVars['resourceType'])) { |
|
182 | + $resourceType = explode(',', $postVars['resourceType']); |
|
183 | + } else { |
|
184 | + $resourceType = ['{DAV:}collection']; |
|
185 | + } |
|
186 | + |
|
187 | + $properties = []; |
|
188 | + foreach ($postVars as $varName => $varValue) { |
|
189 | + // Any _POST variable in clark notation is treated |
|
190 | + // like a property. |
|
191 | + if ('{' === $varName[0]) { |
|
192 | + // PHP will convert any dots to underscores. |
|
193 | + // This leaves us with no way to differentiate |
|
194 | + // the two. |
|
195 | + // Therefore we replace the string *DOT* with a |
|
196 | + // real dot. * is not allowed in uris so we |
|
197 | + // should be good. |
|
198 | + $varName = str_replace('*DOT*', '.', $varName); |
|
199 | + $properties[$varName] = $varValue; |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + $mkCol = new MkCol( |
|
204 | + $resourceType, |
|
205 | + $properties |
|
206 | + ); |
|
207 | + $this->server->createCollection($uri.'/'.$folderName, $mkCol); |
|
208 | + } |
|
209 | + break; |
|
210 | + |
|
211 | + // @codeCoverageIgnoreStart |
|
212 | + case 'put': |
|
213 | + if ($_FILES) { |
|
214 | + $file = current($_FILES); |
|
215 | + } else { |
|
216 | + break; |
|
217 | + } |
|
218 | + |
|
219 | + list(, $newName) = Uri\split(trim($file['name'])); |
|
220 | + if (isset($postVars['name']) && trim($postVars['name'])) { |
|
221 | + $newName = trim($postVars['name']); |
|
222 | + } |
|
223 | + |
|
224 | + // Making sure we only have a 'basename' component |
|
225 | + list(, $newName) = Uri\split($newName); |
|
226 | + |
|
227 | + if (is_uploaded_file($file['tmp_name'])) { |
|
228 | + $this->server->createFile($uri.'/'.$newName, fopen($file['tmp_name'], 'r')); |
|
229 | + } |
|
230 | + break; |
|
231 | + // @codeCoverageIgnoreEnd |
|
232 | + } |
|
233 | + } |
|
234 | + $response->setHeader('Location', $request->getUrl()); |
|
235 | + $response->setStatus(302); |
|
236 | + |
|
237 | + return false; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Escapes a string for html. |
|
242 | + * |
|
243 | + * @param string $value |
|
244 | + * |
|
245 | + * @return string |
|
246 | + */ |
|
247 | + public function escapeHTML($value) |
|
248 | + { |
|
249 | + return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Generates the html directory index for a given url. |
|
254 | + * |
|
255 | + * @param string $path |
|
256 | + * |
|
257 | + * @return string |
|
258 | + */ |
|
259 | + public function generateDirectoryIndex($path) |
|
260 | + { |
|
261 | + $html = $this->generateHeader($path ? $path : '/', $path); |
|
262 | + |
|
263 | + $node = $this->server->tree->getNodeForPath($path); |
|
264 | + if ($node instanceof DAV\ICollection) { |
|
265 | + $html .= "<section><h1>Nodes</h1>\n"; |
|
266 | + $html .= '<table class="nodeTable">'; |
|
267 | + |
|
268 | + $subNodes = $this->server->getPropertiesForChildren($path, [ |
|
269 | + '{DAV:}displayname', |
|
270 | + '{DAV:}resourcetype', |
|
271 | + '{DAV:}getcontenttype', |
|
272 | + '{DAV:}getcontentlength', |
|
273 | + '{DAV:}getlastmodified', |
|
274 | + ]); |
|
275 | + |
|
276 | + foreach ($subNodes as $subPath => $subProps) { |
|
277 | + $subNode = $this->server->tree->getNodeForPath($subPath); |
|
278 | + $fullPath = $this->server->getBaseUri().HTTP\encodePath($subPath); |
|
279 | + list(, $displayPath) = Uri\split($subPath); |
|
280 | + |
|
281 | + $subNodes[$subPath]['subNode'] = $subNode; |
|
282 | + $subNodes[$subPath]['fullPath'] = $fullPath; |
|
283 | + $subNodes[$subPath]['displayPath'] = $displayPath; |
|
284 | + } |
|
285 | + uasort($subNodes, [$this, 'compareNodes']); |
|
286 | + |
|
287 | + foreach ($subNodes as $subProps) { |
|
288 | + $type = [ |
|
289 | + 'string' => 'Unknown', |
|
290 | + 'icon' => 'cog', |
|
291 | + ]; |
|
292 | + if (isset($subProps['{DAV:}resourcetype'])) { |
|
293 | + $type = $this->mapResourceType($subProps['{DAV:}resourcetype']->getValue(), $subProps['subNode']); |
|
294 | + } |
|
295 | + |
|
296 | + $html .= '<tr>'; |
|
297 | + $html .= '<td class="nameColumn"><a href="'.$this->escapeHTML($subProps['fullPath']).'"><span class="oi" data-glyph="'.$this->escapeHTML($type['icon']).'"></span> '.$this->escapeHTML($subProps['displayPath']).'</a></td>'; |
|
298 | + $html .= '<td class="typeColumn">'.$this->escapeHTML($type['string']).'</td>'; |
|
299 | + $html .= '<td>'; |
|
300 | + if (isset($subProps['{DAV:}getcontentlength'])) { |
|
301 | + $html .= $this->escapeHTML($subProps['{DAV:}getcontentlength'].' bytes'); |
|
302 | + } |
|
303 | + $html .= '</td><td>'; |
|
304 | + if (isset($subProps['{DAV:}getlastmodified'])) { |
|
305 | + $lastMod = $subProps['{DAV:}getlastmodified']->getTime(); |
|
306 | + $html .= $this->escapeHTML($lastMod->format('F j, Y, g:i a')); |
|
307 | + } |
|
308 | + $html .= '</td><td>'; |
|
309 | + if (isset($subProps['{DAV:}displayname'])) { |
|
310 | + $html .= $this->escapeHTML($subProps['{DAV:}displayname']); |
|
311 | + } |
|
312 | + $html .= '</td>'; |
|
313 | + |
|
314 | + $buttonActions = ''; |
|
315 | + if ($subProps['subNode'] instanceof DAV\IFile) { |
|
316 | + $buttonActions = '<a href="'.$this->escapeHTML($subProps['fullPath']).'?sabreAction=info"><span class="oi" data-glyph="info"></span></a>'; |
|
317 | + } |
|
318 | + $this->server->emit('browserButtonActions', [$subProps['fullPath'], $subProps['subNode'], &$buttonActions]); |
|
319 | + |
|
320 | + $html .= '<td>'.$buttonActions.'</td>'; |
|
321 | + $html .= '</tr>'; |
|
322 | + } |
|
323 | + |
|
324 | + $html .= '</table>'; |
|
325 | + } |
|
326 | + |
|
327 | + $html .= '</section>'; |
|
328 | + $html .= '<section><h1>Properties</h1>'; |
|
329 | + $html .= '<table class="propTable">'; |
|
330 | + |
|
331 | + // Allprops request |
|
332 | + $propFind = new PropFindAll($path); |
|
333 | + $properties = $this->server->getPropertiesByNode($propFind, $node); |
|
334 | + |
|
335 | + $properties = $propFind->getResultForMultiStatus()[200]; |
|
336 | + |
|
337 | + foreach ($properties as $propName => $propValue) { |
|
338 | + if (!in_array($propName, $this->uninterestingProperties)) { |
|
339 | + $html .= $this->drawPropertyRow($propName, $propValue); |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + $html .= '</table>'; |
|
344 | + $html .= '</section>'; |
|
345 | + |
|
346 | + /* Start of generating actions */ |
|
347 | + |
|
348 | + $output = ''; |
|
349 | + if ($this->enablePost) { |
|
350 | + $this->server->emit('onHTMLActionsPanel', [$node, &$output, $path]); |
|
351 | + } |
|
352 | + |
|
353 | + if ($output) { |
|
354 | + $html .= '<section><h1>Actions</h1>'; |
|
355 | + $html .= "<div class=\"actions\">\n"; |
|
356 | + $html .= $output; |
|
357 | + $html .= "</div>\n"; |
|
358 | + $html .= "</section>\n"; |
|
359 | + } |
|
360 | + |
|
361 | + $html .= $this->generateFooter(); |
|
362 | + |
|
363 | + $this->server->httpResponse->setHeader('Content-Security-Policy', "default-src 'none'; img-src 'self'; style-src 'self'; font-src 'self';"); |
|
364 | + |
|
365 | + return $html; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Generates the 'plugins' page. |
|
370 | + * |
|
371 | + * @return string |
|
372 | + */ |
|
373 | + public function generatePluginListing() |
|
374 | + { |
|
375 | + $html = $this->generateHeader('Plugins'); |
|
376 | + |
|
377 | + $html .= '<section><h1>Plugins</h1>'; |
|
378 | + $html .= '<table class="propTable">'; |
|
379 | + foreach ($this->server->getPlugins() as $plugin) { |
|
380 | + $info = $plugin->getPluginInfo(); |
|
381 | + $html .= '<tr><th>'.$info['name'].'</th>'; |
|
382 | + $html .= '<td>'.$info['description'].'</td>'; |
|
383 | + $html .= '<td>'; |
|
384 | + if (isset($info['link']) && $info['link']) { |
|
385 | + $html .= '<a href="'.$this->escapeHTML($info['link']).'"><span class="oi" data-glyph="book"></span></a>'; |
|
386 | + } |
|
387 | + $html .= '</td></tr>'; |
|
388 | + } |
|
389 | + $html .= '</table>'; |
|
390 | + $html .= '</section>'; |
|
391 | + |
|
392 | + /* Start of generating actions */ |
|
393 | + |
|
394 | + $html .= $this->generateFooter(); |
|
395 | + |
|
396 | + return $html; |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Generates the first block of HTML, including the <head> tag and page |
|
401 | + * header. |
|
402 | + * |
|
403 | + * Returns footer. |
|
404 | + * |
|
405 | + * @param string $title |
|
406 | + * @param string $path |
|
407 | + * |
|
408 | + * @return string |
|
409 | + */ |
|
410 | + public function generateHeader($title, $path = null) |
|
411 | + { |
|
412 | + $version = ''; |
|
413 | + if (DAV\Server::$exposeVersion) { |
|
414 | + $version = DAV\Version::VERSION; |
|
415 | + } |
|
416 | + |
|
417 | + $vars = [ |
|
418 | + 'title' => $this->escapeHTML($title), |
|
419 | + 'favicon' => $this->escapeHTML($this->getAssetUrl('favicon.ico')), |
|
420 | + 'style' => $this->escapeHTML($this->getAssetUrl('sabredav.css')), |
|
421 | + 'iconstyle' => $this->escapeHTML($this->getAssetUrl('openiconic/open-iconic.css')), |
|
422 | + 'logo' => $this->escapeHTML($this->getAssetUrl('sabredav.png')), |
|
423 | + 'baseUrl' => $this->server->getBaseUri(), |
|
424 | + ]; |
|
425 | + |
|
426 | + $html = <<<HTML |
|
427 | 427 | <!DOCTYPE html> |
428 | 428 | <html> |
429 | 429 | <head> |
@@ -443,67 +443,67 @@ discard block |
||
443 | 443 | <nav> |
444 | 444 | HTML; |
445 | 445 | |
446 | - // If the path is empty, there's no parent. |
|
447 | - if ($path) { |
|
448 | - list($parentUri) = Uri\split($path); |
|
449 | - $fullPath = $this->server->getBaseUri().HTTP\encodePath($parentUri); |
|
450 | - $html .= '<a href="'.$fullPath.'" class="btn">⇤ Go to parent</a>'; |
|
451 | - } else { |
|
452 | - $html .= '<span class="btn disabled">⇤ Go to parent</span>'; |
|
453 | - } |
|
454 | - |
|
455 | - $html .= ' <a href="?sabreAction=plugins" class="btn"><span class="oi" data-glyph="puzzle-piece"></span> Plugins</a>'; |
|
456 | - |
|
457 | - $html .= '</nav>'; |
|
458 | - |
|
459 | - return $html; |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Generates the page footer. |
|
464 | - * |
|
465 | - * Returns html. |
|
466 | - * |
|
467 | - * @return string |
|
468 | - */ |
|
469 | - public function generateFooter() |
|
470 | - { |
|
471 | - $version = ''; |
|
472 | - if (DAV\Server::$exposeVersion) { |
|
473 | - $version = DAV\Version::VERSION; |
|
474 | - } |
|
475 | - $year = date('Y'); |
|
476 | - |
|
477 | - return <<<HTML |
|
446 | + // If the path is empty, there's no parent. |
|
447 | + if ($path) { |
|
448 | + list($parentUri) = Uri\split($path); |
|
449 | + $fullPath = $this->server->getBaseUri().HTTP\encodePath($parentUri); |
|
450 | + $html .= '<a href="'.$fullPath.'" class="btn">⇤ Go to parent</a>'; |
|
451 | + } else { |
|
452 | + $html .= '<span class="btn disabled">⇤ Go to parent</span>'; |
|
453 | + } |
|
454 | + |
|
455 | + $html .= ' <a href="?sabreAction=plugins" class="btn"><span class="oi" data-glyph="puzzle-piece"></span> Plugins</a>'; |
|
456 | + |
|
457 | + $html .= '</nav>'; |
|
458 | + |
|
459 | + return $html; |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Generates the page footer. |
|
464 | + * |
|
465 | + * Returns html. |
|
466 | + * |
|
467 | + * @return string |
|
468 | + */ |
|
469 | + public function generateFooter() |
|
470 | + { |
|
471 | + $version = ''; |
|
472 | + if (DAV\Server::$exposeVersion) { |
|
473 | + $version = DAV\Version::VERSION; |
|
474 | + } |
|
475 | + $year = date('Y'); |
|
476 | + |
|
477 | + return <<<HTML |
|
478 | 478 | <footer>Generated by SabreDAV $version (c)2007-$year <a href="http://sabre.io/">http://sabre.io/</a></footer> |
479 | 479 | </body> |
480 | 480 | </html> |
481 | 481 | HTML; |
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * This method is used to generate the 'actions panel' output for |
|
486 | - * collections. |
|
487 | - * |
|
488 | - * This specifically generates the interfaces for creating new files, and |
|
489 | - * creating new directories. |
|
490 | - * |
|
491 | - * @param mixed $output |
|
492 | - * @param string $path |
|
493 | - */ |
|
494 | - public function htmlActionsPanel(DAV\INode $node, &$output, $path) |
|
495 | - { |
|
496 | - if (!$node instanceof DAV\ICollection) { |
|
497 | - return; |
|
498 | - } |
|
499 | - |
|
500 | - // We also know fairly certain that if an object is a non-extended |
|
501 | - // SimpleCollection, we won't need to show the panel either. |
|
502 | - if ('Sabre\\DAV\\SimpleCollection' === get_class($node)) { |
|
503 | - return; |
|
504 | - } |
|
505 | - |
|
506 | - $output .= <<<HTML |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * This method is used to generate the 'actions panel' output for |
|
486 | + * collections. |
|
487 | + * |
|
488 | + * This specifically generates the interfaces for creating new files, and |
|
489 | + * creating new directories. |
|
490 | + * |
|
491 | + * @param mixed $output |
|
492 | + * @param string $path |
|
493 | + */ |
|
494 | + public function htmlActionsPanel(DAV\INode $node, &$output, $path) |
|
495 | + { |
|
496 | + if (!$node instanceof DAV\ICollection) { |
|
497 | + return; |
|
498 | + } |
|
499 | + |
|
500 | + // We also know fairly certain that if an object is a non-extended |
|
501 | + // SimpleCollection, we won't need to show the panel either. |
|
502 | + if ('Sabre\\DAV\\SimpleCollection' === get_class($node)) { |
|
503 | + return; |
|
504 | + } |
|
505 | + |
|
506 | + $output .= <<<HTML |
|
507 | 507 | <form method="post" action=""> |
508 | 508 | <h3>Create new folder</h3> |
509 | 509 | <input type="hidden" name="sabreAction" value="mkcol" /> |
@@ -518,272 +518,272 @@ discard block |
||
518 | 518 | <input type="submit" value="upload" /> |
519 | 519 | </form> |
520 | 520 | HTML; |
521 | - } |
|
522 | - |
|
523 | - /** |
|
524 | - * This method takes a path/name of an asset and turns it into url |
|
525 | - * suiteable for http access. |
|
526 | - * |
|
527 | - * @param string $assetName |
|
528 | - * |
|
529 | - * @return string |
|
530 | - */ |
|
531 | - protected function getAssetUrl($assetName) |
|
532 | - { |
|
533 | - return $this->server->getBaseUri().'?sabreAction=asset&assetName='.urlencode($assetName); |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * This method returns a local pathname to an asset. |
|
538 | - * |
|
539 | - * @param string $assetName |
|
540 | - * |
|
541 | - * @throws DAV\Exception\NotFound |
|
542 | - * |
|
543 | - * @return string |
|
544 | - */ |
|
545 | - protected function getLocalAssetPath($assetName) |
|
546 | - { |
|
547 | - $assetDir = __DIR__.'/assets/'; |
|
548 | - $path = $assetDir.$assetName; |
|
549 | - |
|
550 | - // Making sure people aren't trying to escape from the base path. |
|
551 | - $path = str_replace('\\', '/', $path); |
|
552 | - if (false !== strpos($path, '/../') || '/..' === strrchr($path, '/')) { |
|
553 | - throw new DAV\Exception\NotFound('Path does not exist, or escaping from the base path was detected'); |
|
554 | - } |
|
555 | - $realPath = realpath($path); |
|
556 | - if ($realPath && 0 === strpos($realPath, realpath($assetDir)) && file_exists($path)) { |
|
557 | - return $path; |
|
558 | - } |
|
559 | - throw new DAV\Exception\NotFound('Path does not exist, or escaping from the base path was detected'); |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * This method reads an asset from disk and generates a full http response. |
|
564 | - * |
|
565 | - * @param string $assetName |
|
566 | - */ |
|
567 | - protected function serveAsset($assetName) |
|
568 | - { |
|
569 | - $assetPath = $this->getLocalAssetPath($assetName); |
|
570 | - |
|
571 | - // Rudimentary mime type detection |
|
572 | - $mime = 'application/octet-stream'; |
|
573 | - $map = [ |
|
574 | - 'ico' => 'image/vnd.microsoft.icon', |
|
575 | - 'png' => 'image/png', |
|
576 | - 'css' => 'text/css', |
|
577 | - ]; |
|
578 | - |
|
579 | - $ext = substr($assetName, strrpos($assetName, '.') + 1); |
|
580 | - if (isset($map[$ext])) { |
|
581 | - $mime = $map[$ext]; |
|
582 | - } |
|
583 | - |
|
584 | - $this->server->httpResponse->setHeader('Content-Type', $mime); |
|
585 | - $this->server->httpResponse->setHeader('Content-Length', filesize($assetPath)); |
|
586 | - $this->server->httpResponse->setHeader('Cache-Control', 'public, max-age=1209600'); |
|
587 | - $this->server->httpResponse->setStatus(200); |
|
588 | - $this->server->httpResponse->setBody(fopen($assetPath, 'r')); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Sort helper function: compares two directory entries based on type and |
|
593 | - * display name. Collections sort above other types. |
|
594 | - * |
|
595 | - * @param array $a |
|
596 | - * @param array $b |
|
597 | - * |
|
598 | - * @return int |
|
599 | - */ |
|
600 | - protected function compareNodes($a, $b) |
|
601 | - { |
|
602 | - $typeA = (isset($a['{DAV:}resourcetype'])) |
|
603 | - ? (in_array('{DAV:}collection', $a['{DAV:}resourcetype']->getValue())) |
|
604 | - : false; |
|
605 | - |
|
606 | - $typeB = (isset($b['{DAV:}resourcetype'])) |
|
607 | - ? (in_array('{DAV:}collection', $b['{DAV:}resourcetype']->getValue())) |
|
608 | - : false; |
|
609 | - |
|
610 | - // If same type, sort alphabetically by filename: |
|
611 | - if ($typeA === $typeB) { |
|
612 | - return strnatcasecmp($a['displayPath'], $b['displayPath']); |
|
613 | - } |
|
614 | - |
|
615 | - return ($typeA < $typeB) ? 1 : -1; |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * Maps a resource type to a human-readable string and icon. |
|
620 | - * |
|
621 | - * @param DAV\INode $node |
|
622 | - * |
|
623 | - * @return array |
|
624 | - */ |
|
625 | - private function mapResourceType(array $resourceTypes, $node) |
|
626 | - { |
|
627 | - if (!$resourceTypes) { |
|
628 | - if ($node instanceof DAV\IFile) { |
|
629 | - return [ |
|
630 | - 'string' => 'File', |
|
631 | - 'icon' => 'file', |
|
632 | - ]; |
|
633 | - } else { |
|
634 | - return [ |
|
635 | - 'string' => 'Unknown', |
|
636 | - 'icon' => 'cog', |
|
637 | - ]; |
|
638 | - } |
|
639 | - } |
|
640 | - |
|
641 | - $types = [ |
|
642 | - '{http://calendarserver.org/ns/}calendar-proxy-write' => [ |
|
643 | - 'string' => 'Proxy-Write', |
|
644 | - 'icon' => 'people', |
|
645 | - ], |
|
646 | - '{http://calendarserver.org/ns/}calendar-proxy-read' => [ |
|
647 | - 'string' => 'Proxy-Read', |
|
648 | - 'icon' => 'people', |
|
649 | - ], |
|
650 | - '{urn:ietf:params:xml:ns:caldav}schedule-outbox' => [ |
|
651 | - 'string' => 'Outbox', |
|
652 | - 'icon' => 'inbox', |
|
653 | - ], |
|
654 | - '{urn:ietf:params:xml:ns:caldav}schedule-inbox' => [ |
|
655 | - 'string' => 'Inbox', |
|
656 | - 'icon' => 'inbox', |
|
657 | - ], |
|
658 | - '{urn:ietf:params:xml:ns:caldav}calendar' => [ |
|
659 | - 'string' => 'Calendar', |
|
660 | - 'icon' => 'calendar', |
|
661 | - ], |
|
662 | - '{http://calendarserver.org/ns/}shared-owner' => [ |
|
663 | - 'string' => 'Shared', |
|
664 | - 'icon' => 'calendar', |
|
665 | - ], |
|
666 | - '{http://calendarserver.org/ns/}subscribed' => [ |
|
667 | - 'string' => 'Subscription', |
|
668 | - 'icon' => 'calendar', |
|
669 | - ], |
|
670 | - '{urn:ietf:params:xml:ns:carddav}directory' => [ |
|
671 | - 'string' => 'Directory', |
|
672 | - 'icon' => 'globe', |
|
673 | - ], |
|
674 | - '{urn:ietf:params:xml:ns:carddav}addressbook' => [ |
|
675 | - 'string' => 'Address book', |
|
676 | - 'icon' => 'book', |
|
677 | - ], |
|
678 | - '{DAV:}principal' => [ |
|
679 | - 'string' => 'Principal', |
|
680 | - 'icon' => 'person', |
|
681 | - ], |
|
682 | - '{DAV:}collection' => [ |
|
683 | - 'string' => 'Collection', |
|
684 | - 'icon' => 'folder', |
|
685 | - ], |
|
686 | - ]; |
|
687 | - |
|
688 | - $info = [ |
|
689 | - 'string' => [], |
|
690 | - 'icon' => 'cog', |
|
691 | - ]; |
|
692 | - foreach ($resourceTypes as $k => $resourceType) { |
|
693 | - if (isset($types[$resourceType])) { |
|
694 | - $info['string'][] = $types[$resourceType]['string']; |
|
695 | - } else { |
|
696 | - $info['string'][] = $resourceType; |
|
697 | - } |
|
698 | - } |
|
699 | - foreach ($types as $key => $resourceInfo) { |
|
700 | - if (in_array($key, $resourceTypes)) { |
|
701 | - $info['icon'] = $resourceInfo['icon']; |
|
702 | - break; |
|
703 | - } |
|
704 | - } |
|
705 | - $info['string'] = implode(', ', $info['string']); |
|
706 | - |
|
707 | - return $info; |
|
708 | - } |
|
709 | - |
|
710 | - /** |
|
711 | - * Draws a table row for a property. |
|
712 | - * |
|
713 | - * @param string $name |
|
714 | - * @param mixed $value |
|
715 | - * |
|
716 | - * @return string |
|
717 | - */ |
|
718 | - private function drawPropertyRow($name, $value) |
|
719 | - { |
|
720 | - $html = new HtmlOutputHelper( |
|
721 | - $this->server->getBaseUri(), |
|
722 | - $this->server->xml->namespaceMap |
|
723 | - ); |
|
724 | - |
|
725 | - return '<tr><th>'.$html->xmlName($name).'</th><td>'.$this->drawPropertyValue($html, $value).'</td></tr>'; |
|
726 | - } |
|
727 | - |
|
728 | - /** |
|
729 | - * Draws a table row for a property. |
|
730 | - * |
|
731 | - * @param HtmlOutputHelper $html |
|
732 | - * @param mixed $value |
|
733 | - * |
|
734 | - * @return string |
|
735 | - */ |
|
736 | - private function drawPropertyValue($html, $value) |
|
737 | - { |
|
738 | - if (is_scalar($value)) { |
|
739 | - return $html->h($value); |
|
740 | - } elseif ($value instanceof HtmlOutput) { |
|
741 | - return $value->toHtml($html); |
|
742 | - } elseif ($value instanceof \Sabre\Xml\XmlSerializable) { |
|
743 | - // There's no default html output for this property, we're going |
|
744 | - // to output the actual xml serialization instead. |
|
745 | - $xml = $this->server->xml->write('{DAV:}root', $value, $this->server->getBaseUri()); |
|
746 | - // removing first and last line, as they contain our root |
|
747 | - // element. |
|
748 | - $xml = explode("\n", $xml); |
|
749 | - $xml = array_slice($xml, 2, -2); |
|
750 | - |
|
751 | - return '<pre>'.$html->h(implode("\n", $xml)).'</pre>'; |
|
752 | - } else { |
|
753 | - return '<em>unknown</em>'; |
|
754 | - } |
|
755 | - } |
|
756 | - |
|
757 | - /** |
|
758 | - * Returns a plugin name. |
|
759 | - * |
|
760 | - * Using this name other plugins will be able to access other plugins; |
|
761 | - * using \Sabre\DAV\Server::getPlugin |
|
762 | - * |
|
763 | - * @return string |
|
764 | - */ |
|
765 | - public function getPluginName() |
|
766 | - { |
|
767 | - return 'browser'; |
|
768 | - } |
|
769 | - |
|
770 | - /** |
|
771 | - * Returns a bunch of meta-data about the plugin. |
|
772 | - * |
|
773 | - * Providing this information is optional, and is mainly displayed by the |
|
774 | - * Browser plugin. |
|
775 | - * |
|
776 | - * The description key in the returned array may contain html and will not |
|
777 | - * be sanitized. |
|
778 | - * |
|
779 | - * @return array |
|
780 | - */ |
|
781 | - public function getPluginInfo() |
|
782 | - { |
|
783 | - return [ |
|
784 | - 'name' => $this->getPluginName(), |
|
785 | - 'description' => 'Generates HTML indexes and debug information for your sabre/dav server', |
|
786 | - 'link' => 'http://sabre.io/dav/browser-plugin/', |
|
787 | - ]; |
|
788 | - } |
|
521 | + } |
|
522 | + |
|
523 | + /** |
|
524 | + * This method takes a path/name of an asset and turns it into url |
|
525 | + * suiteable for http access. |
|
526 | + * |
|
527 | + * @param string $assetName |
|
528 | + * |
|
529 | + * @return string |
|
530 | + */ |
|
531 | + protected function getAssetUrl($assetName) |
|
532 | + { |
|
533 | + return $this->server->getBaseUri().'?sabreAction=asset&assetName='.urlencode($assetName); |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * This method returns a local pathname to an asset. |
|
538 | + * |
|
539 | + * @param string $assetName |
|
540 | + * |
|
541 | + * @throws DAV\Exception\NotFound |
|
542 | + * |
|
543 | + * @return string |
|
544 | + */ |
|
545 | + protected function getLocalAssetPath($assetName) |
|
546 | + { |
|
547 | + $assetDir = __DIR__.'/assets/'; |
|
548 | + $path = $assetDir.$assetName; |
|
549 | + |
|
550 | + // Making sure people aren't trying to escape from the base path. |
|
551 | + $path = str_replace('\\', '/', $path); |
|
552 | + if (false !== strpos($path, '/../') || '/..' === strrchr($path, '/')) { |
|
553 | + throw new DAV\Exception\NotFound('Path does not exist, or escaping from the base path was detected'); |
|
554 | + } |
|
555 | + $realPath = realpath($path); |
|
556 | + if ($realPath && 0 === strpos($realPath, realpath($assetDir)) && file_exists($path)) { |
|
557 | + return $path; |
|
558 | + } |
|
559 | + throw new DAV\Exception\NotFound('Path does not exist, or escaping from the base path was detected'); |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * This method reads an asset from disk and generates a full http response. |
|
564 | + * |
|
565 | + * @param string $assetName |
|
566 | + */ |
|
567 | + protected function serveAsset($assetName) |
|
568 | + { |
|
569 | + $assetPath = $this->getLocalAssetPath($assetName); |
|
570 | + |
|
571 | + // Rudimentary mime type detection |
|
572 | + $mime = 'application/octet-stream'; |
|
573 | + $map = [ |
|
574 | + 'ico' => 'image/vnd.microsoft.icon', |
|
575 | + 'png' => 'image/png', |
|
576 | + 'css' => 'text/css', |
|
577 | + ]; |
|
578 | + |
|
579 | + $ext = substr($assetName, strrpos($assetName, '.') + 1); |
|
580 | + if (isset($map[$ext])) { |
|
581 | + $mime = $map[$ext]; |
|
582 | + } |
|
583 | + |
|
584 | + $this->server->httpResponse->setHeader('Content-Type', $mime); |
|
585 | + $this->server->httpResponse->setHeader('Content-Length', filesize($assetPath)); |
|
586 | + $this->server->httpResponse->setHeader('Cache-Control', 'public, max-age=1209600'); |
|
587 | + $this->server->httpResponse->setStatus(200); |
|
588 | + $this->server->httpResponse->setBody(fopen($assetPath, 'r')); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Sort helper function: compares two directory entries based on type and |
|
593 | + * display name. Collections sort above other types. |
|
594 | + * |
|
595 | + * @param array $a |
|
596 | + * @param array $b |
|
597 | + * |
|
598 | + * @return int |
|
599 | + */ |
|
600 | + protected function compareNodes($a, $b) |
|
601 | + { |
|
602 | + $typeA = (isset($a['{DAV:}resourcetype'])) |
|
603 | + ? (in_array('{DAV:}collection', $a['{DAV:}resourcetype']->getValue())) |
|
604 | + : false; |
|
605 | + |
|
606 | + $typeB = (isset($b['{DAV:}resourcetype'])) |
|
607 | + ? (in_array('{DAV:}collection', $b['{DAV:}resourcetype']->getValue())) |
|
608 | + : false; |
|
609 | + |
|
610 | + // If same type, sort alphabetically by filename: |
|
611 | + if ($typeA === $typeB) { |
|
612 | + return strnatcasecmp($a['displayPath'], $b['displayPath']); |
|
613 | + } |
|
614 | + |
|
615 | + return ($typeA < $typeB) ? 1 : -1; |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * Maps a resource type to a human-readable string and icon. |
|
620 | + * |
|
621 | + * @param DAV\INode $node |
|
622 | + * |
|
623 | + * @return array |
|
624 | + */ |
|
625 | + private function mapResourceType(array $resourceTypes, $node) |
|
626 | + { |
|
627 | + if (!$resourceTypes) { |
|
628 | + if ($node instanceof DAV\IFile) { |
|
629 | + return [ |
|
630 | + 'string' => 'File', |
|
631 | + 'icon' => 'file', |
|
632 | + ]; |
|
633 | + } else { |
|
634 | + return [ |
|
635 | + 'string' => 'Unknown', |
|
636 | + 'icon' => 'cog', |
|
637 | + ]; |
|
638 | + } |
|
639 | + } |
|
640 | + |
|
641 | + $types = [ |
|
642 | + '{http://calendarserver.org/ns/}calendar-proxy-write' => [ |
|
643 | + 'string' => 'Proxy-Write', |
|
644 | + 'icon' => 'people', |
|
645 | + ], |
|
646 | + '{http://calendarserver.org/ns/}calendar-proxy-read' => [ |
|
647 | + 'string' => 'Proxy-Read', |
|
648 | + 'icon' => 'people', |
|
649 | + ], |
|
650 | + '{urn:ietf:params:xml:ns:caldav}schedule-outbox' => [ |
|
651 | + 'string' => 'Outbox', |
|
652 | + 'icon' => 'inbox', |
|
653 | + ], |
|
654 | + '{urn:ietf:params:xml:ns:caldav}schedule-inbox' => [ |
|
655 | + 'string' => 'Inbox', |
|
656 | + 'icon' => 'inbox', |
|
657 | + ], |
|
658 | + '{urn:ietf:params:xml:ns:caldav}calendar' => [ |
|
659 | + 'string' => 'Calendar', |
|
660 | + 'icon' => 'calendar', |
|
661 | + ], |
|
662 | + '{http://calendarserver.org/ns/}shared-owner' => [ |
|
663 | + 'string' => 'Shared', |
|
664 | + 'icon' => 'calendar', |
|
665 | + ], |
|
666 | + '{http://calendarserver.org/ns/}subscribed' => [ |
|
667 | + 'string' => 'Subscription', |
|
668 | + 'icon' => 'calendar', |
|
669 | + ], |
|
670 | + '{urn:ietf:params:xml:ns:carddav}directory' => [ |
|
671 | + 'string' => 'Directory', |
|
672 | + 'icon' => 'globe', |
|
673 | + ], |
|
674 | + '{urn:ietf:params:xml:ns:carddav}addressbook' => [ |
|
675 | + 'string' => 'Address book', |
|
676 | + 'icon' => 'book', |
|
677 | + ], |
|
678 | + '{DAV:}principal' => [ |
|
679 | + 'string' => 'Principal', |
|
680 | + 'icon' => 'person', |
|
681 | + ], |
|
682 | + '{DAV:}collection' => [ |
|
683 | + 'string' => 'Collection', |
|
684 | + 'icon' => 'folder', |
|
685 | + ], |
|
686 | + ]; |
|
687 | + |
|
688 | + $info = [ |
|
689 | + 'string' => [], |
|
690 | + 'icon' => 'cog', |
|
691 | + ]; |
|
692 | + foreach ($resourceTypes as $k => $resourceType) { |
|
693 | + if (isset($types[$resourceType])) { |
|
694 | + $info['string'][] = $types[$resourceType]['string']; |
|
695 | + } else { |
|
696 | + $info['string'][] = $resourceType; |
|
697 | + } |
|
698 | + } |
|
699 | + foreach ($types as $key => $resourceInfo) { |
|
700 | + if (in_array($key, $resourceTypes)) { |
|
701 | + $info['icon'] = $resourceInfo['icon']; |
|
702 | + break; |
|
703 | + } |
|
704 | + } |
|
705 | + $info['string'] = implode(', ', $info['string']); |
|
706 | + |
|
707 | + return $info; |
|
708 | + } |
|
709 | + |
|
710 | + /** |
|
711 | + * Draws a table row for a property. |
|
712 | + * |
|
713 | + * @param string $name |
|
714 | + * @param mixed $value |
|
715 | + * |
|
716 | + * @return string |
|
717 | + */ |
|
718 | + private function drawPropertyRow($name, $value) |
|
719 | + { |
|
720 | + $html = new HtmlOutputHelper( |
|
721 | + $this->server->getBaseUri(), |
|
722 | + $this->server->xml->namespaceMap |
|
723 | + ); |
|
724 | + |
|
725 | + return '<tr><th>'.$html->xmlName($name).'</th><td>'.$this->drawPropertyValue($html, $value).'</td></tr>'; |
|
726 | + } |
|
727 | + |
|
728 | + /** |
|
729 | + * Draws a table row for a property. |
|
730 | + * |
|
731 | + * @param HtmlOutputHelper $html |
|
732 | + * @param mixed $value |
|
733 | + * |
|
734 | + * @return string |
|
735 | + */ |
|
736 | + private function drawPropertyValue($html, $value) |
|
737 | + { |
|
738 | + if (is_scalar($value)) { |
|
739 | + return $html->h($value); |
|
740 | + } elseif ($value instanceof HtmlOutput) { |
|
741 | + return $value->toHtml($html); |
|
742 | + } elseif ($value instanceof \Sabre\Xml\XmlSerializable) { |
|
743 | + // There's no default html output for this property, we're going |
|
744 | + // to output the actual xml serialization instead. |
|
745 | + $xml = $this->server->xml->write('{DAV:}root', $value, $this->server->getBaseUri()); |
|
746 | + // removing first and last line, as they contain our root |
|
747 | + // element. |
|
748 | + $xml = explode("\n", $xml); |
|
749 | + $xml = array_slice($xml, 2, -2); |
|
750 | + |
|
751 | + return '<pre>'.$html->h(implode("\n", $xml)).'</pre>'; |
|
752 | + } else { |
|
753 | + return '<em>unknown</em>'; |
|
754 | + } |
|
755 | + } |
|
756 | + |
|
757 | + /** |
|
758 | + * Returns a plugin name. |
|
759 | + * |
|
760 | + * Using this name other plugins will be able to access other plugins; |
|
761 | + * using \Sabre\DAV\Server::getPlugin |
|
762 | + * |
|
763 | + * @return string |
|
764 | + */ |
|
765 | + public function getPluginName() |
|
766 | + { |
|
767 | + return 'browser'; |
|
768 | + } |
|
769 | + |
|
770 | + /** |
|
771 | + * Returns a bunch of meta-data about the plugin. |
|
772 | + * |
|
773 | + * Providing this information is optional, and is mainly displayed by the |
|
774 | + * Browser plugin. |
|
775 | + * |
|
776 | + * The description key in the returned array may contain html and will not |
|
777 | + * be sanitized. |
|
778 | + * |
|
779 | + * @return array |
|
780 | + */ |
|
781 | + public function getPluginInfo() |
|
782 | + { |
|
783 | + return [ |
|
784 | + 'name' => $this->getPluginName(), |
|
785 | + 'description' => 'Generates HTML indexes and debug information for your sabre/dav server', |
|
786 | + 'link' => 'http://sabre.io/dav/browser-plugin/', |
|
787 | + ]; |
|
788 | + } |
|
789 | 789 | } |
@@ -17,18 +17,18 @@ |
||
17 | 17 | */ |
18 | 18 | interface HtmlOutput |
19 | 19 | { |
20 | - /** |
|
21 | - * Generate html representation for this value. |
|
22 | - * |
|
23 | - * The html output is 100% trusted, and no effort is being made to sanitize |
|
24 | - * it. It's up to the implementor to sanitize user provided values. |
|
25 | - * |
|
26 | - * The output must be in UTF-8. |
|
27 | - * |
|
28 | - * The baseUri parameter is a url to the root of the application, and can |
|
29 | - * be used to construct local links. |
|
30 | - * |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function toHtml(HtmlOutputHelper $html); |
|
20 | + /** |
|
21 | + * Generate html representation for this value. |
|
22 | + * |
|
23 | + * The html output is 100% trusted, and no effort is being made to sanitize |
|
24 | + * it. It's up to the implementor to sanitize user provided values. |
|
25 | + * |
|
26 | + * The output must be in UTF-8. |
|
27 | + * |
|
28 | + * The baseUri parameter is a url to the root of the application, and can |
|
29 | + * be used to construct local links. |
|
30 | + * |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function toHtml(HtmlOutputHelper $html); |
|
34 | 34 | } |
@@ -20,39 +20,39 @@ |
||
20 | 20 | */ |
21 | 21 | class MapGetToPropFind extends DAV\ServerPlugin |
22 | 22 | { |
23 | - /** |
|
24 | - * reference to server class. |
|
25 | - * |
|
26 | - * @var DAV\Server |
|
27 | - */ |
|
28 | - protected $server; |
|
29 | - |
|
30 | - /** |
|
31 | - * Initializes the plugin and subscribes to events. |
|
32 | - */ |
|
33 | - public function initialize(DAV\Server $server) |
|
34 | - { |
|
35 | - $this->server = $server; |
|
36 | - $this->server->on('method:GET', [$this, 'httpGet'], 90); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * This method intercepts GET requests to non-files, and changes it into an HTTP PROPFIND request. |
|
41 | - * |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function httpGet(RequestInterface $request, ResponseInterface $response) |
|
45 | - { |
|
46 | - $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
47 | - if ($node instanceof DAV\IFile) { |
|
48 | - return; |
|
49 | - } |
|
50 | - |
|
51 | - $subRequest = clone $request; |
|
52 | - $subRequest->setMethod('PROPFIND'); |
|
53 | - |
|
54 | - $this->server->invokeMethod($subRequest, $response); |
|
55 | - |
|
56 | - return false; |
|
57 | - } |
|
23 | + /** |
|
24 | + * reference to server class. |
|
25 | + * |
|
26 | + * @var DAV\Server |
|
27 | + */ |
|
28 | + protected $server; |
|
29 | + |
|
30 | + /** |
|
31 | + * Initializes the plugin and subscribes to events. |
|
32 | + */ |
|
33 | + public function initialize(DAV\Server $server) |
|
34 | + { |
|
35 | + $this->server = $server; |
|
36 | + $this->server->on('method:GET', [$this, 'httpGet'], 90); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * This method intercepts GET requests to non-files, and changes it into an HTTP PROPFIND request. |
|
41 | + * |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function httpGet(RequestInterface $request, ResponseInterface $response) |
|
45 | + { |
|
46 | + $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
47 | + if ($node instanceof DAV\IFile) { |
|
48 | + return; |
|
49 | + } |
|
50 | + |
|
51 | + $subRequest = clone $request; |
|
52 | + $subRequest->setMethod('PROPFIND'); |
|
53 | + |
|
54 | + $this->server->invokeMethod($subRequest, $response); |
|
55 | + |
|
56 | + return false; |
|
57 | + } |
|
58 | 58 | } |
@@ -17,102 +17,102 @@ |
||
17 | 17 | */ |
18 | 18 | class HtmlOutputHelper |
19 | 19 | { |
20 | - /** |
|
21 | - * Link to the root of the application. |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $baseUri; |
|
20 | + /** |
|
21 | + * Link to the root of the application. |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $baseUri; |
|
26 | 26 | |
27 | - /** |
|
28 | - * List of xml namespaces. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $namespaceMap; |
|
27 | + /** |
|
28 | + * List of xml namespaces. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $namespaceMap; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Creates the object. |
|
36 | - * |
|
37 | - * baseUri must point to the root of the application. This will be used to |
|
38 | - * easily generate links. |
|
39 | - * |
|
40 | - * The namespaceMap contains an array with the list of xml namespaces and |
|
41 | - * their prefixes. WebDAV uses a lot of XML with complex namespaces, so |
|
42 | - * that can be used to make output a lot shorter. |
|
43 | - * |
|
44 | - * @param string $baseUri |
|
45 | - */ |
|
46 | - public function __construct($baseUri, array $namespaceMap) |
|
47 | - { |
|
48 | - $this->baseUri = $baseUri; |
|
49 | - $this->namespaceMap = $namespaceMap; |
|
50 | - } |
|
34 | + /** |
|
35 | + * Creates the object. |
|
36 | + * |
|
37 | + * baseUri must point to the root of the application. This will be used to |
|
38 | + * easily generate links. |
|
39 | + * |
|
40 | + * The namespaceMap contains an array with the list of xml namespaces and |
|
41 | + * their prefixes. WebDAV uses a lot of XML with complex namespaces, so |
|
42 | + * that can be used to make output a lot shorter. |
|
43 | + * |
|
44 | + * @param string $baseUri |
|
45 | + */ |
|
46 | + public function __construct($baseUri, array $namespaceMap) |
|
47 | + { |
|
48 | + $this->baseUri = $baseUri; |
|
49 | + $this->namespaceMap = $namespaceMap; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Generates a 'full' url based on a relative one. |
|
54 | - * |
|
55 | - * For relative urls, the base of the application is taken as the reference |
|
56 | - * url, not the 'current url of the current request'. |
|
57 | - * |
|
58 | - * Absolute urls are left alone. |
|
59 | - * |
|
60 | - * @param string $path |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public function fullUrl($path) |
|
65 | - { |
|
66 | - return Uri\resolve($this->baseUri, $path); |
|
67 | - } |
|
52 | + /** |
|
53 | + * Generates a 'full' url based on a relative one. |
|
54 | + * |
|
55 | + * For relative urls, the base of the application is taken as the reference |
|
56 | + * url, not the 'current url of the current request'. |
|
57 | + * |
|
58 | + * Absolute urls are left alone. |
|
59 | + * |
|
60 | + * @param string $path |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public function fullUrl($path) |
|
65 | + { |
|
66 | + return Uri\resolve($this->baseUri, $path); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Escape string for HTML output. |
|
71 | - * |
|
72 | - * @param scalar $input |
|
73 | - * |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public function h($input) |
|
77 | - { |
|
78 | - return htmlspecialchars((string) $input, ENT_COMPAT, 'UTF-8'); |
|
79 | - } |
|
69 | + /** |
|
70 | + * Escape string for HTML output. |
|
71 | + * |
|
72 | + * @param scalar $input |
|
73 | + * |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public function h($input) |
|
77 | + { |
|
78 | + return htmlspecialchars((string) $input, ENT_COMPAT, 'UTF-8'); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Generates a full <a>-tag. |
|
83 | - * |
|
84 | - * Url is automatically expanded. If label is not specified, we re-use the |
|
85 | - * url. |
|
86 | - * |
|
87 | - * @param string $url |
|
88 | - * @param string $label |
|
89 | - * |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function link($url, $label = null) |
|
93 | - { |
|
94 | - $url = $this->h($this->fullUrl($url)); |
|
81 | + /** |
|
82 | + * Generates a full <a>-tag. |
|
83 | + * |
|
84 | + * Url is automatically expanded. If label is not specified, we re-use the |
|
85 | + * url. |
|
86 | + * |
|
87 | + * @param string $url |
|
88 | + * @param string $label |
|
89 | + * |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function link($url, $label = null) |
|
93 | + { |
|
94 | + $url = $this->h($this->fullUrl($url)); |
|
95 | 95 | |
96 | - return '<a href="'.$url.'">'.($label ? $this->h($label) : $url).'</a>'; |
|
97 | - } |
|
96 | + return '<a href="'.$url.'">'.($label ? $this->h($label) : $url).'</a>'; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * This method takes an xml element in clark-notation, and turns it into a |
|
101 | - * shortened version with a prefix, if it was a known namespace. |
|
102 | - * |
|
103 | - * @param string $element |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function xmlName($element) |
|
108 | - { |
|
109 | - list($ns, $localName) = XmlService::parseClarkNotation($element); |
|
110 | - if (isset($this->namespaceMap[$ns])) { |
|
111 | - $propName = $this->namespaceMap[$ns].':'.$localName; |
|
112 | - } else { |
|
113 | - $propName = $element; |
|
114 | - } |
|
99 | + /** |
|
100 | + * This method takes an xml element in clark-notation, and turns it into a |
|
101 | + * shortened version with a prefix, if it was a known namespace. |
|
102 | + * |
|
103 | + * @param string $element |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function xmlName($element) |
|
108 | + { |
|
109 | + list($ns, $localName) = XmlService::parseClarkNotation($element); |
|
110 | + if (isset($this->namespaceMap[$ns])) { |
|
111 | + $propName = $this->namespaceMap[$ns].':'.$localName; |
|
112 | + } else { |
|
113 | + $propName = $element; |
|
114 | + } |
|
115 | 115 | |
116 | - return '<span title="'.$this->h($element).'">'.$this->h($propName).'</span>'; |
|
117 | - } |
|
116 | + return '<span title="'.$this->h($element).'">'.$this->h($propName).'</span>'; |
|
117 | + } |
|
118 | 118 | } |
@@ -16,113 +16,113 @@ |
||
16 | 16 | */ |
17 | 17 | class PropFindAll extends PropFind |
18 | 18 | { |
19 | - /** |
|
20 | - * Creates the PROPFIND object. |
|
21 | - * |
|
22 | - * @param string $path |
|
23 | - */ |
|
24 | - public function __construct($path) |
|
25 | - { |
|
26 | - parent::__construct($path, []); |
|
27 | - } |
|
19 | + /** |
|
20 | + * Creates the PROPFIND object. |
|
21 | + * |
|
22 | + * @param string $path |
|
23 | + */ |
|
24 | + public function __construct($path) |
|
25 | + { |
|
26 | + parent::__construct($path, []); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Handles a specific property. |
|
31 | - * |
|
32 | - * This method checks whether the specified property was requested in this |
|
33 | - * PROPFIND request, and if so, it will call the callback and use the |
|
34 | - * return value for it's value. |
|
35 | - * |
|
36 | - * Example: |
|
37 | - * |
|
38 | - * $propFind->handle('{DAV:}displayname', function() { |
|
39 | - * return 'hello'; |
|
40 | - * }); |
|
41 | - * |
|
42 | - * Note that handle will only work the first time. If null is returned, the |
|
43 | - * value is ignored. |
|
44 | - * |
|
45 | - * It's also possible to not pass a callback, but immediately pass a value |
|
46 | - * |
|
47 | - * @param string $propertyName |
|
48 | - * @param mixed $valueOrCallBack |
|
49 | - */ |
|
50 | - public function handle($propertyName, $valueOrCallBack) |
|
51 | - { |
|
52 | - if (is_callable($valueOrCallBack)) { |
|
53 | - $value = $valueOrCallBack(); |
|
54 | - } else { |
|
55 | - $value = $valueOrCallBack; |
|
56 | - } |
|
57 | - if (!is_null($value)) { |
|
58 | - $this->result[$propertyName] = [200, $value]; |
|
59 | - } |
|
60 | - } |
|
29 | + /** |
|
30 | + * Handles a specific property. |
|
31 | + * |
|
32 | + * This method checks whether the specified property was requested in this |
|
33 | + * PROPFIND request, and if so, it will call the callback and use the |
|
34 | + * return value for it's value. |
|
35 | + * |
|
36 | + * Example: |
|
37 | + * |
|
38 | + * $propFind->handle('{DAV:}displayname', function() { |
|
39 | + * return 'hello'; |
|
40 | + * }); |
|
41 | + * |
|
42 | + * Note that handle will only work the first time. If null is returned, the |
|
43 | + * value is ignored. |
|
44 | + * |
|
45 | + * It's also possible to not pass a callback, but immediately pass a value |
|
46 | + * |
|
47 | + * @param string $propertyName |
|
48 | + * @param mixed $valueOrCallBack |
|
49 | + */ |
|
50 | + public function handle($propertyName, $valueOrCallBack) |
|
51 | + { |
|
52 | + if (is_callable($valueOrCallBack)) { |
|
53 | + $value = $valueOrCallBack(); |
|
54 | + } else { |
|
55 | + $value = $valueOrCallBack; |
|
56 | + } |
|
57 | + if (!is_null($value)) { |
|
58 | + $this->result[$propertyName] = [200, $value]; |
|
59 | + } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Sets the value of the property. |
|
64 | - * |
|
65 | - * If status is not supplied, the status will default to 200 for non-null |
|
66 | - * properties, and 404 for null properties. |
|
67 | - * |
|
68 | - * @param string $propertyName |
|
69 | - * @param mixed $value |
|
70 | - * @param int $status |
|
71 | - */ |
|
72 | - public function set($propertyName, $value, $status = null) |
|
73 | - { |
|
74 | - if (is_null($status)) { |
|
75 | - $status = is_null($value) ? 404 : 200; |
|
76 | - } |
|
77 | - $this->result[$propertyName] = [$status, $value]; |
|
78 | - } |
|
62 | + /** |
|
63 | + * Sets the value of the property. |
|
64 | + * |
|
65 | + * If status is not supplied, the status will default to 200 for non-null |
|
66 | + * properties, and 404 for null properties. |
|
67 | + * |
|
68 | + * @param string $propertyName |
|
69 | + * @param mixed $value |
|
70 | + * @param int $status |
|
71 | + */ |
|
72 | + public function set($propertyName, $value, $status = null) |
|
73 | + { |
|
74 | + if (is_null($status)) { |
|
75 | + $status = is_null($value) ? 404 : 200; |
|
76 | + } |
|
77 | + $this->result[$propertyName] = [$status, $value]; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Returns the current value for a property. |
|
82 | - * |
|
83 | - * @param string $propertyName |
|
84 | - * |
|
85 | - * @return mixed |
|
86 | - */ |
|
87 | - public function get($propertyName) |
|
88 | - { |
|
89 | - return isset($this->result[$propertyName]) ? $this->result[$propertyName][1] : null; |
|
90 | - } |
|
80 | + /** |
|
81 | + * Returns the current value for a property. |
|
82 | + * |
|
83 | + * @param string $propertyName |
|
84 | + * |
|
85 | + * @return mixed |
|
86 | + */ |
|
87 | + public function get($propertyName) |
|
88 | + { |
|
89 | + return isset($this->result[$propertyName]) ? $this->result[$propertyName][1] : null; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Returns the current status code for a property name. |
|
94 | - * |
|
95 | - * If the property does not appear in the list of requested properties, |
|
96 | - * null will be returned. |
|
97 | - * |
|
98 | - * @param string $propertyName |
|
99 | - * |
|
100 | - * @return int|null |
|
101 | - */ |
|
102 | - public function getStatus($propertyName) |
|
103 | - { |
|
104 | - return isset($this->result[$propertyName]) ? $this->result[$propertyName][0] : 404; |
|
105 | - } |
|
92 | + /** |
|
93 | + * Returns the current status code for a property name. |
|
94 | + * |
|
95 | + * If the property does not appear in the list of requested properties, |
|
96 | + * null will be returned. |
|
97 | + * |
|
98 | + * @param string $propertyName |
|
99 | + * |
|
100 | + * @return int|null |
|
101 | + */ |
|
102 | + public function getStatus($propertyName) |
|
103 | + { |
|
104 | + return isset($this->result[$propertyName]) ? $this->result[$propertyName][0] : 404; |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Returns all propertynames that have a 404 status, and thus don't have a |
|
109 | - * value yet. |
|
110 | - * |
|
111 | - * @return array |
|
112 | - */ |
|
113 | - public function get404Properties() |
|
114 | - { |
|
115 | - $result = []; |
|
116 | - foreach ($this->result as $propertyName => $stuff) { |
|
117 | - if (404 === $stuff[0]) { |
|
118 | - $result[] = $propertyName; |
|
119 | - } |
|
120 | - } |
|
121 | - // If there's nothing in this list, we're adding one fictional item. |
|
122 | - if (!$result) { |
|
123 | - $result[] = '{http://sabredav.org/ns}idk'; |
|
124 | - } |
|
107 | + /** |
|
108 | + * Returns all propertynames that have a 404 status, and thus don't have a |
|
109 | + * value yet. |
|
110 | + * |
|
111 | + * @return array |
|
112 | + */ |
|
113 | + public function get404Properties() |
|
114 | + { |
|
115 | + $result = []; |
|
116 | + foreach ($this->result as $propertyName => $stuff) { |
|
117 | + if (404 === $stuff[0]) { |
|
118 | + $result[] = $propertyName; |
|
119 | + } |
|
120 | + } |
|
121 | + // If there's nothing in this list, we're adding one fictional item. |
|
122 | + if (!$result) { |
|
123 | + $result[] = '{http://sabredav.org/ns}idk'; |
|
124 | + } |
|
125 | 125 | |
126 | - return $result; |
|
127 | - } |
|
126 | + return $result; |
|
127 | + } |
|
128 | 128 | } |
@@ -26,68 +26,68 @@ |
||
26 | 26 | */ |
27 | 27 | class GuessContentType extends DAV\ServerPlugin |
28 | 28 | { |
29 | - /** |
|
30 | - * List of recognized file extensions. |
|
31 | - * |
|
32 | - * Feel free to add more |
|
33 | - * |
|
34 | - * @var array |
|
35 | - */ |
|
36 | - public $extensionMap = [ |
|
37 | - // images |
|
38 | - 'jpg' => 'image/jpeg', |
|
39 | - 'gif' => 'image/gif', |
|
40 | - 'png' => 'image/png', |
|
29 | + /** |
|
30 | + * List of recognized file extensions. |
|
31 | + * |
|
32 | + * Feel free to add more |
|
33 | + * |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + public $extensionMap = [ |
|
37 | + // images |
|
38 | + 'jpg' => 'image/jpeg', |
|
39 | + 'gif' => 'image/gif', |
|
40 | + 'png' => 'image/png', |
|
41 | 41 | |
42 | - // groupware |
|
43 | - 'ics' => 'text/calendar', |
|
44 | - 'vcf' => 'text/vcard', |
|
42 | + // groupware |
|
43 | + 'ics' => 'text/calendar', |
|
44 | + 'vcf' => 'text/vcard', |
|
45 | 45 | |
46 | - // text |
|
47 | - 'txt' => 'text/plain', |
|
48 | - ]; |
|
46 | + // text |
|
47 | + 'txt' => 'text/plain', |
|
48 | + ]; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Initializes the plugin. |
|
52 | - */ |
|
53 | - public function initialize(DAV\Server $server) |
|
54 | - { |
|
55 | - // Using a relatively low priority (200) to allow other extensions |
|
56 | - // to set the content-type first. |
|
57 | - $server->on('propFind', [$this, 'propFind'], 200); |
|
58 | - } |
|
50 | + /** |
|
51 | + * Initializes the plugin. |
|
52 | + */ |
|
53 | + public function initialize(DAV\Server $server) |
|
54 | + { |
|
55 | + // Using a relatively low priority (200) to allow other extensions |
|
56 | + // to set the content-type first. |
|
57 | + $server->on('propFind', [$this, 'propFind'], 200); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Our PROPFIND handler. |
|
62 | - * |
|
63 | - * Here we set a contenttype, if the node didn't already have one. |
|
64 | - */ |
|
65 | - public function propFind(PropFind $propFind, INode $node) |
|
66 | - { |
|
67 | - $propFind->handle('{DAV:}getcontenttype', function () use ($propFind) { |
|
68 | - list(, $fileName) = Uri\split($propFind->getPath()); |
|
60 | + /** |
|
61 | + * Our PROPFIND handler. |
|
62 | + * |
|
63 | + * Here we set a contenttype, if the node didn't already have one. |
|
64 | + */ |
|
65 | + public function propFind(PropFind $propFind, INode $node) |
|
66 | + { |
|
67 | + $propFind->handle('{DAV:}getcontenttype', function () use ($propFind) { |
|
68 | + list(, $fileName) = Uri\split($propFind->getPath()); |
|
69 | 69 | |
70 | - return $this->getContentType($fileName); |
|
71 | - }); |
|
72 | - } |
|
70 | + return $this->getContentType($fileName); |
|
71 | + }); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Simple method to return the contenttype. |
|
76 | - * |
|
77 | - * @param string $fileName |
|
78 | - * |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - protected function getContentType($fileName) |
|
82 | - { |
|
83 | - if (null !== $fileName) { |
|
84 | - // Just grabbing the extension |
|
85 | - $extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1)); |
|
86 | - if (isset($this->extensionMap[$extension])) { |
|
87 | - return $this->extensionMap[$extension]; |
|
88 | - } |
|
89 | - } |
|
74 | + /** |
|
75 | + * Simple method to return the contenttype. |
|
76 | + * |
|
77 | + * @param string $fileName |
|
78 | + * |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + protected function getContentType($fileName) |
|
82 | + { |
|
83 | + if (null !== $fileName) { |
|
84 | + // Just grabbing the extension |
|
85 | + $extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1)); |
|
86 | + if (isset($this->extensionMap[$extension])) { |
|
87 | + return $this->extensionMap[$extension]; |
|
88 | + } |
|
89 | + } |
|
90 | 90 | |
91 | - return 'application/octet-stream'; |
|
92 | - } |
|
91 | + return 'application/octet-stream'; |
|
92 | + } |
|
93 | 93 | } |
@@ -15,65 +15,65 @@ |
||
15 | 15 | */ |
16 | 16 | interface ICollection extends INode |
17 | 17 | { |
18 | - /** |
|
19 | - * Creates a new file in the directory. |
|
20 | - * |
|
21 | - * Data will either be supplied as a stream resource, or in certain cases |
|
22 | - * as a string. Keep in mind that you may have to support either. |
|
23 | - * |
|
24 | - * After successful creation of the file, you may choose to return the ETag |
|
25 | - * of the new file here. |
|
26 | - * |
|
27 | - * The returned ETag must be surrounded by double-quotes (The quotes should |
|
28 | - * be part of the actual string). |
|
29 | - * |
|
30 | - * If you cannot accurately determine the ETag, you should not return it. |
|
31 | - * If you don't store the file exactly as-is (you're transforming it |
|
32 | - * somehow) you should also not return an ETag. |
|
33 | - * |
|
34 | - * This means that if a subsequent GET to this new file does not exactly |
|
35 | - * return the same contents of what was submitted here, you are strongly |
|
36 | - * recommended to omit the ETag. |
|
37 | - * |
|
38 | - * @param string $name Name of the file |
|
39 | - * @param resource|string $data Initial payload |
|
40 | - * |
|
41 | - * @return string|null |
|
42 | - */ |
|
43 | - public function createFile($name, $data = null); |
|
18 | + /** |
|
19 | + * Creates a new file in the directory. |
|
20 | + * |
|
21 | + * Data will either be supplied as a stream resource, or in certain cases |
|
22 | + * as a string. Keep in mind that you may have to support either. |
|
23 | + * |
|
24 | + * After successful creation of the file, you may choose to return the ETag |
|
25 | + * of the new file here. |
|
26 | + * |
|
27 | + * The returned ETag must be surrounded by double-quotes (The quotes should |
|
28 | + * be part of the actual string). |
|
29 | + * |
|
30 | + * If you cannot accurately determine the ETag, you should not return it. |
|
31 | + * If you don't store the file exactly as-is (you're transforming it |
|
32 | + * somehow) you should also not return an ETag. |
|
33 | + * |
|
34 | + * This means that if a subsequent GET to this new file does not exactly |
|
35 | + * return the same contents of what was submitted here, you are strongly |
|
36 | + * recommended to omit the ETag. |
|
37 | + * |
|
38 | + * @param string $name Name of the file |
|
39 | + * @param resource|string $data Initial payload |
|
40 | + * |
|
41 | + * @return string|null |
|
42 | + */ |
|
43 | + public function createFile($name, $data = null); |
|
44 | 44 | |
45 | - /** |
|
46 | - * Creates a new subdirectory. |
|
47 | - * |
|
48 | - * @param string $name |
|
49 | - */ |
|
50 | - public function createDirectory($name); |
|
45 | + /** |
|
46 | + * Creates a new subdirectory. |
|
47 | + * |
|
48 | + * @param string $name |
|
49 | + */ |
|
50 | + public function createDirectory($name); |
|
51 | 51 | |
52 | - /** |
|
53 | - * Returns a specific child node, referenced by its name. |
|
54 | - * |
|
55 | - * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
56 | - * exist. |
|
57 | - * |
|
58 | - * @param string $name |
|
59 | - * |
|
60 | - * @return INode |
|
61 | - */ |
|
62 | - public function getChild($name); |
|
52 | + /** |
|
53 | + * Returns a specific child node, referenced by its name. |
|
54 | + * |
|
55 | + * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
56 | + * exist. |
|
57 | + * |
|
58 | + * @param string $name |
|
59 | + * |
|
60 | + * @return INode |
|
61 | + */ |
|
62 | + public function getChild($name); |
|
63 | 63 | |
64 | - /** |
|
65 | - * Returns an array with all the child nodes. |
|
66 | - * |
|
67 | - * @return INode[] |
|
68 | - */ |
|
69 | - public function getChildren(); |
|
64 | + /** |
|
65 | + * Returns an array with all the child nodes. |
|
66 | + * |
|
67 | + * @return INode[] |
|
68 | + */ |
|
69 | + public function getChildren(); |
|
70 | 70 | |
71 | - /** |
|
72 | - * Checks if a child-node with the specified name exists. |
|
73 | - * |
|
74 | - * @param string $name |
|
75 | - * |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function childExists($name); |
|
71 | + /** |
|
72 | + * Checks if a child-node with the specified name exists. |
|
73 | + * |
|
74 | + * @param string $name |
|
75 | + * |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function childExists($name); |
|
79 | 79 | } |
@@ -17,50 +17,50 @@ |
||
17 | 17 | */ |
18 | 18 | class UUIDUtil |
19 | 19 | { |
20 | - /** |
|
21 | - * Returns a pseudo-random v4 UUID. |
|
22 | - * |
|
23 | - * This function is based on a comment by Andrew Moore on php.net |
|
24 | - * |
|
25 | - * @see http://www.php.net/manual/en/function.uniqid.php#94959 |
|
26 | - * |
|
27 | - * @return string |
|
28 | - */ |
|
29 | - public static function getUUID() |
|
30 | - { |
|
31 | - return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
32 | - // 32 bits for "time_low" |
|
33 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
20 | + /** |
|
21 | + * Returns a pseudo-random v4 UUID. |
|
22 | + * |
|
23 | + * This function is based on a comment by Andrew Moore on php.net |
|
24 | + * |
|
25 | + * @see http://www.php.net/manual/en/function.uniqid.php#94959 |
|
26 | + * |
|
27 | + * @return string |
|
28 | + */ |
|
29 | + public static function getUUID() |
|
30 | + { |
|
31 | + return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
32 | + // 32 bits for "time_low" |
|
33 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
34 | 34 | |
35 | - // 16 bits for "time_mid" |
|
36 | - mt_rand(0, 0xffff), |
|
35 | + // 16 bits for "time_mid" |
|
36 | + mt_rand(0, 0xffff), |
|
37 | 37 | |
38 | - // 16 bits for "time_hi_and_version", |
|
39 | - // four most significant bits holds version number 4 |
|
40 | - mt_rand(0, 0x0fff) | 0x4000, |
|
38 | + // 16 bits for "time_hi_and_version", |
|
39 | + // four most significant bits holds version number 4 |
|
40 | + mt_rand(0, 0x0fff) | 0x4000, |
|
41 | 41 | |
42 | - // 16 bits, 8 bits for "clk_seq_hi_res", |
|
43 | - // 8 bits for "clk_seq_low", |
|
44 | - // two most significant bits holds zero and one for variant DCE1.1 |
|
45 | - mt_rand(0, 0x3fff) | 0x8000, |
|
42 | + // 16 bits, 8 bits for "clk_seq_hi_res", |
|
43 | + // 8 bits for "clk_seq_low", |
|
44 | + // two most significant bits holds zero and one for variant DCE1.1 |
|
45 | + mt_rand(0, 0x3fff) | 0x8000, |
|
46 | 46 | |
47 | - // 48 bits for "node" |
|
48 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
|
49 | - ); |
|
50 | - } |
|
47 | + // 48 bits for "node" |
|
48 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
|
49 | + ); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Checks if a string is a valid UUID. |
|
54 | - * |
|
55 | - * @param string $uuid |
|
56 | - * |
|
57 | - * @return bool |
|
58 | - */ |
|
59 | - public static function validateUUID($uuid) |
|
60 | - { |
|
61 | - return 0 !== preg_match( |
|
62 | - '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', |
|
63 | - $uuid |
|
64 | - ); |
|
65 | - } |
|
52 | + /** |
|
53 | + * Checks if a string is a valid UUID. |
|
54 | + * |
|
55 | + * @param string $uuid |
|
56 | + * |
|
57 | + * @return bool |
|
58 | + */ |
|
59 | + public static function validateUUID($uuid) |
|
60 | + { |
|
61 | + return 0 !== preg_match( |
|
62 | + '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', |
|
63 | + $uuid |
|
64 | + ); |
|
65 | + } |
|
66 | 66 | } |
@@ -28,231 +28,231 @@ discard block |
||
28 | 28 | */ |
29 | 29 | class Plugin extends ServerPlugin |
30 | 30 | { |
31 | - const ACCESS_NOTSHARED = 0; |
|
32 | - const ACCESS_SHAREDOWNER = 1; |
|
33 | - const ACCESS_READ = 2; |
|
34 | - const ACCESS_READWRITE = 3; |
|
35 | - const ACCESS_NOACCESS = 4; |
|
36 | - |
|
37 | - const INVITE_NORESPONSE = 1; |
|
38 | - const INVITE_ACCEPTED = 2; |
|
39 | - const INVITE_DECLINED = 3; |
|
40 | - const INVITE_INVALID = 4; |
|
41 | - |
|
42 | - /** |
|
43 | - * Reference to SabreDAV server object. |
|
44 | - * |
|
45 | - * @var Server |
|
46 | - */ |
|
47 | - protected $server; |
|
48 | - |
|
49 | - /** |
|
50 | - * This method should return a list of server-features. |
|
51 | - * |
|
52 | - * This is for example 'versioning' and is added to the DAV: header |
|
53 | - * in an OPTIONS response. |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function getFeatures() |
|
58 | - { |
|
59 | - return ['resource-sharing']; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Returns a plugin name. |
|
64 | - * |
|
65 | - * Using this name other plugins will be able to access other plugins |
|
66 | - * using \Sabre\DAV\Server::getPlugin |
|
67 | - * |
|
68 | - * @return string |
|
69 | - */ |
|
70 | - public function getPluginName() |
|
71 | - { |
|
72 | - return 'sharing'; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * This initializes the plugin. |
|
77 | - * |
|
78 | - * This function is called by Sabre\DAV\Server, after |
|
79 | - * addPlugin is called. |
|
80 | - * |
|
81 | - * This method should set up the required event subscriptions. |
|
82 | - */ |
|
83 | - public function initialize(Server $server) |
|
84 | - { |
|
85 | - $this->server = $server; |
|
86 | - |
|
87 | - $server->xml->elementMap['{DAV:}share-resource'] = 'Sabre\\DAV\\Xml\\Request\\ShareResource'; |
|
88 | - |
|
89 | - array_push( |
|
90 | - $server->protectedProperties, |
|
91 | - '{DAV:}share-mode' |
|
92 | - ); |
|
93 | - |
|
94 | - $server->on('method:POST', [$this, 'httpPost']); |
|
95 | - $server->on('propFind', [$this, 'propFind']); |
|
96 | - $server->on('getSupportedPrivilegeSet', [$this, 'getSupportedPrivilegeSet']); |
|
97 | - $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
98 | - $server->on('onBrowserPostAction', [$this, 'browserPostAction']); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Updates the list of sharees on a shared resource. |
|
103 | - * |
|
104 | - * The sharees array is a list of people that are to be added modified |
|
105 | - * or removed in the list of shares. |
|
106 | - * |
|
107 | - * @param string $path |
|
108 | - * @param Sharee[] $sharees |
|
109 | - */ |
|
110 | - public function shareResource($path, array $sharees) |
|
111 | - { |
|
112 | - $node = $this->server->tree->getNodeForPath($path); |
|
113 | - |
|
114 | - if (!$node instanceof ISharedNode) { |
|
115 | - throw new Forbidden('Sharing is not allowed on this node'); |
|
116 | - } |
|
117 | - |
|
118 | - // Getting ACL info |
|
119 | - $acl = $this->server->getPlugin('acl'); |
|
120 | - |
|
121 | - // If there's no ACL support, we allow everything |
|
122 | - if ($acl) { |
|
123 | - $acl->checkPrivileges($path, '{DAV:}share'); |
|
124 | - } |
|
125 | - |
|
126 | - foreach ($sharees as $sharee) { |
|
127 | - // We're going to attempt to get a local principal uri for a share |
|
128 | - // href by emitting the getPrincipalByUri event. |
|
129 | - $principal = null; |
|
130 | - $this->server->emit('getPrincipalByUri', [$sharee->href, &$principal]); |
|
131 | - $sharee->principal = $principal; |
|
132 | - } |
|
133 | - $node->updateInvites($sharees); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * This event is triggered when properties are requested for nodes. |
|
138 | - * |
|
139 | - * This allows us to inject any sharings-specific properties. |
|
140 | - */ |
|
141 | - public function propFind(PropFind $propFind, INode $node) |
|
142 | - { |
|
143 | - if ($node instanceof ISharedNode) { |
|
144 | - $propFind->handle('{DAV:}share-access', function () use ($node) { |
|
145 | - return new Property\ShareAccess($node->getShareAccess()); |
|
146 | - }); |
|
147 | - $propFind->handle('{DAV:}invite', function () use ($node) { |
|
148 | - return new Property\Invite($node->getInvites()); |
|
149 | - }); |
|
150 | - $propFind->handle('{DAV:}share-resource-uri', function () use ($node) { |
|
151 | - return new Property\Href($node->getShareResourceUri()); |
|
152 | - }); |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * We intercept this to handle POST requests on shared resources. |
|
158 | - * |
|
159 | - * @return bool|null |
|
160 | - */ |
|
161 | - public function httpPost(RequestInterface $request, ResponseInterface $response) |
|
162 | - { |
|
163 | - $path = $request->getPath(); |
|
164 | - $contentType = $request->getHeader('Content-Type'); |
|
165 | - if (null === $contentType) { |
|
166 | - return; |
|
167 | - } |
|
168 | - |
|
169 | - // We're only interested in the davsharing content type. |
|
170 | - if (false === strpos($contentType, 'application/davsharing+xml')) { |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - $message = $this->server->xml->parse( |
|
175 | - $request->getBody(), |
|
176 | - $request->getUrl(), |
|
177 | - $documentType |
|
178 | - ); |
|
179 | - |
|
180 | - switch ($documentType) { |
|
181 | - case '{DAV:}share-resource': |
|
182 | - $this->shareResource($path, $message->sharees); |
|
183 | - $response->setStatus(200); |
|
184 | - // Adding this because sending a response body may cause issues, |
|
185 | - // and I wanted some type of indicator the response was handled. |
|
186 | - $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
187 | - |
|
188 | - // Breaking the event chain |
|
189 | - return false; |
|
190 | - |
|
191 | - default: |
|
192 | - throw new BadRequest('Unexpected document type: '.$documentType.' for this Content-Type'); |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * This method is triggered whenever a subsystem reqeuests the privileges |
|
198 | - * hat are supported on a particular node. |
|
199 | - * |
|
200 | - * We need to add a number of privileges for scheduling purposes. |
|
201 | - */ |
|
202 | - public function getSupportedPrivilegeSet(INode $node, array &$supportedPrivilegeSet) |
|
203 | - { |
|
204 | - if ($node instanceof ISharedNode) { |
|
205 | - $supportedPrivilegeSet['{DAV:}share'] = [ |
|
206 | - 'abstract' => false, |
|
207 | - 'aggregates' => [], |
|
208 | - ]; |
|
209 | - } |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Returns a bunch of meta-data about the plugin. |
|
214 | - * |
|
215 | - * Providing this information is optional, and is mainly displayed by the |
|
216 | - * Browser plugin. |
|
217 | - * |
|
218 | - * The description key in the returned array may contain html and will not |
|
219 | - * be sanitized. |
|
220 | - * |
|
221 | - * @return array |
|
222 | - */ |
|
223 | - public function getPluginInfo() |
|
224 | - { |
|
225 | - return [ |
|
226 | - 'name' => $this->getPluginName(), |
|
227 | - 'description' => 'This plugin implements WebDAV resource sharing', |
|
228 | - 'link' => 'https://github.com/evert/webdav-sharing', |
|
229 | - ]; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * This method is used to generate HTML output for the |
|
234 | - * DAV\Browser\Plugin. |
|
235 | - * |
|
236 | - * @param string $output |
|
237 | - * @param string $path |
|
238 | - * |
|
239 | - * @return bool|null |
|
240 | - */ |
|
241 | - public function htmlActionsPanel(INode $node, &$output, $path) |
|
242 | - { |
|
243 | - if (!$node instanceof ISharedNode) { |
|
244 | - return; |
|
245 | - } |
|
246 | - |
|
247 | - $aclPlugin = $this->server->getPlugin('acl'); |
|
248 | - if ($aclPlugin) { |
|
249 | - if (!$aclPlugin->checkPrivileges($path, '{DAV:}share', \Sabre\DAVACL\Plugin::R_PARENT, false)) { |
|
250 | - // Sharing is not permitted, we will not draw this interface. |
|
251 | - return; |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - $output .= '<tr><td colspan="2"><form method="post" action=""> |
|
31 | + const ACCESS_NOTSHARED = 0; |
|
32 | + const ACCESS_SHAREDOWNER = 1; |
|
33 | + const ACCESS_READ = 2; |
|
34 | + const ACCESS_READWRITE = 3; |
|
35 | + const ACCESS_NOACCESS = 4; |
|
36 | + |
|
37 | + const INVITE_NORESPONSE = 1; |
|
38 | + const INVITE_ACCEPTED = 2; |
|
39 | + const INVITE_DECLINED = 3; |
|
40 | + const INVITE_INVALID = 4; |
|
41 | + |
|
42 | + /** |
|
43 | + * Reference to SabreDAV server object. |
|
44 | + * |
|
45 | + * @var Server |
|
46 | + */ |
|
47 | + protected $server; |
|
48 | + |
|
49 | + /** |
|
50 | + * This method should return a list of server-features. |
|
51 | + * |
|
52 | + * This is for example 'versioning' and is added to the DAV: header |
|
53 | + * in an OPTIONS response. |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function getFeatures() |
|
58 | + { |
|
59 | + return ['resource-sharing']; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Returns a plugin name. |
|
64 | + * |
|
65 | + * Using this name other plugins will be able to access other plugins |
|
66 | + * using \Sabre\DAV\Server::getPlugin |
|
67 | + * |
|
68 | + * @return string |
|
69 | + */ |
|
70 | + public function getPluginName() |
|
71 | + { |
|
72 | + return 'sharing'; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * This initializes the plugin. |
|
77 | + * |
|
78 | + * This function is called by Sabre\DAV\Server, after |
|
79 | + * addPlugin is called. |
|
80 | + * |
|
81 | + * This method should set up the required event subscriptions. |
|
82 | + */ |
|
83 | + public function initialize(Server $server) |
|
84 | + { |
|
85 | + $this->server = $server; |
|
86 | + |
|
87 | + $server->xml->elementMap['{DAV:}share-resource'] = 'Sabre\\DAV\\Xml\\Request\\ShareResource'; |
|
88 | + |
|
89 | + array_push( |
|
90 | + $server->protectedProperties, |
|
91 | + '{DAV:}share-mode' |
|
92 | + ); |
|
93 | + |
|
94 | + $server->on('method:POST', [$this, 'httpPost']); |
|
95 | + $server->on('propFind', [$this, 'propFind']); |
|
96 | + $server->on('getSupportedPrivilegeSet', [$this, 'getSupportedPrivilegeSet']); |
|
97 | + $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
98 | + $server->on('onBrowserPostAction', [$this, 'browserPostAction']); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Updates the list of sharees on a shared resource. |
|
103 | + * |
|
104 | + * The sharees array is a list of people that are to be added modified |
|
105 | + * or removed in the list of shares. |
|
106 | + * |
|
107 | + * @param string $path |
|
108 | + * @param Sharee[] $sharees |
|
109 | + */ |
|
110 | + public function shareResource($path, array $sharees) |
|
111 | + { |
|
112 | + $node = $this->server->tree->getNodeForPath($path); |
|
113 | + |
|
114 | + if (!$node instanceof ISharedNode) { |
|
115 | + throw new Forbidden('Sharing is not allowed on this node'); |
|
116 | + } |
|
117 | + |
|
118 | + // Getting ACL info |
|
119 | + $acl = $this->server->getPlugin('acl'); |
|
120 | + |
|
121 | + // If there's no ACL support, we allow everything |
|
122 | + if ($acl) { |
|
123 | + $acl->checkPrivileges($path, '{DAV:}share'); |
|
124 | + } |
|
125 | + |
|
126 | + foreach ($sharees as $sharee) { |
|
127 | + // We're going to attempt to get a local principal uri for a share |
|
128 | + // href by emitting the getPrincipalByUri event. |
|
129 | + $principal = null; |
|
130 | + $this->server->emit('getPrincipalByUri', [$sharee->href, &$principal]); |
|
131 | + $sharee->principal = $principal; |
|
132 | + } |
|
133 | + $node->updateInvites($sharees); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * This event is triggered when properties are requested for nodes. |
|
138 | + * |
|
139 | + * This allows us to inject any sharings-specific properties. |
|
140 | + */ |
|
141 | + public function propFind(PropFind $propFind, INode $node) |
|
142 | + { |
|
143 | + if ($node instanceof ISharedNode) { |
|
144 | + $propFind->handle('{DAV:}share-access', function () use ($node) { |
|
145 | + return new Property\ShareAccess($node->getShareAccess()); |
|
146 | + }); |
|
147 | + $propFind->handle('{DAV:}invite', function () use ($node) { |
|
148 | + return new Property\Invite($node->getInvites()); |
|
149 | + }); |
|
150 | + $propFind->handle('{DAV:}share-resource-uri', function () use ($node) { |
|
151 | + return new Property\Href($node->getShareResourceUri()); |
|
152 | + }); |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * We intercept this to handle POST requests on shared resources. |
|
158 | + * |
|
159 | + * @return bool|null |
|
160 | + */ |
|
161 | + public function httpPost(RequestInterface $request, ResponseInterface $response) |
|
162 | + { |
|
163 | + $path = $request->getPath(); |
|
164 | + $contentType = $request->getHeader('Content-Type'); |
|
165 | + if (null === $contentType) { |
|
166 | + return; |
|
167 | + } |
|
168 | + |
|
169 | + // We're only interested in the davsharing content type. |
|
170 | + if (false === strpos($contentType, 'application/davsharing+xml')) { |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + $message = $this->server->xml->parse( |
|
175 | + $request->getBody(), |
|
176 | + $request->getUrl(), |
|
177 | + $documentType |
|
178 | + ); |
|
179 | + |
|
180 | + switch ($documentType) { |
|
181 | + case '{DAV:}share-resource': |
|
182 | + $this->shareResource($path, $message->sharees); |
|
183 | + $response->setStatus(200); |
|
184 | + // Adding this because sending a response body may cause issues, |
|
185 | + // and I wanted some type of indicator the response was handled. |
|
186 | + $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
187 | + |
|
188 | + // Breaking the event chain |
|
189 | + return false; |
|
190 | + |
|
191 | + default: |
|
192 | + throw new BadRequest('Unexpected document type: '.$documentType.' for this Content-Type'); |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * This method is triggered whenever a subsystem reqeuests the privileges |
|
198 | + * hat are supported on a particular node. |
|
199 | + * |
|
200 | + * We need to add a number of privileges for scheduling purposes. |
|
201 | + */ |
|
202 | + public function getSupportedPrivilegeSet(INode $node, array &$supportedPrivilegeSet) |
|
203 | + { |
|
204 | + if ($node instanceof ISharedNode) { |
|
205 | + $supportedPrivilegeSet['{DAV:}share'] = [ |
|
206 | + 'abstract' => false, |
|
207 | + 'aggregates' => [], |
|
208 | + ]; |
|
209 | + } |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Returns a bunch of meta-data about the plugin. |
|
214 | + * |
|
215 | + * Providing this information is optional, and is mainly displayed by the |
|
216 | + * Browser plugin. |
|
217 | + * |
|
218 | + * The description key in the returned array may contain html and will not |
|
219 | + * be sanitized. |
|
220 | + * |
|
221 | + * @return array |
|
222 | + */ |
|
223 | + public function getPluginInfo() |
|
224 | + { |
|
225 | + return [ |
|
226 | + 'name' => $this->getPluginName(), |
|
227 | + 'description' => 'This plugin implements WebDAV resource sharing', |
|
228 | + 'link' => 'https://github.com/evert/webdav-sharing', |
|
229 | + ]; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * This method is used to generate HTML output for the |
|
234 | + * DAV\Browser\Plugin. |
|
235 | + * |
|
236 | + * @param string $output |
|
237 | + * @param string $path |
|
238 | + * |
|
239 | + * @return bool|null |
|
240 | + */ |
|
241 | + public function htmlActionsPanel(INode $node, &$output, $path) |
|
242 | + { |
|
243 | + if (!$node instanceof ISharedNode) { |
|
244 | + return; |
|
245 | + } |
|
246 | + |
|
247 | + $aclPlugin = $this->server->getPlugin('acl'); |
|
248 | + if ($aclPlugin) { |
|
249 | + if (!$aclPlugin->checkPrivileges($path, '{DAV:}share', \Sabre\DAVACL\Plugin::R_PARENT, false)) { |
|
250 | + // Sharing is not permitted, we will not draw this interface. |
|
251 | + return; |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + $output .= '<tr><td colspan="2"><form method="post" action=""> |
|
256 | 256 | <h3>Share this resource</h3> |
257 | 257 | <input type="hidden" name="sabreAction" value="share" /> |
258 | 258 | <label>Share with (uri):</label> <input type="text" name="href" placeholder="mailto:[email protected]"/><br /> |
@@ -265,48 +265,48 @@ discard block |
||
265 | 265 | <input type="submit" value="share" /> |
266 | 266 | </form> |
267 | 267 | </td></tr>'; |
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * This method is triggered for POST actions generated by the browser |
|
272 | - * plugin. |
|
273 | - * |
|
274 | - * @param string $path |
|
275 | - * @param string $action |
|
276 | - * @param array $postVars |
|
277 | - */ |
|
278 | - public function browserPostAction($path, $action, $postVars) |
|
279 | - { |
|
280 | - if ('share' !== $action) { |
|
281 | - return; |
|
282 | - } |
|
283 | - |
|
284 | - if (empty($postVars['href'])) { |
|
285 | - throw new BadRequest('The "href" POST parameter is required'); |
|
286 | - } |
|
287 | - if (empty($postVars['access'])) { |
|
288 | - throw new BadRequest('The "access" POST parameter is required'); |
|
289 | - } |
|
290 | - |
|
291 | - $accessMap = [ |
|
292 | - 'readwrite' => self::ACCESS_READWRITE, |
|
293 | - 'read' => self::ACCESS_READ, |
|
294 | - 'no-access' => self::ACCESS_NOACCESS, |
|
295 | - ]; |
|
296 | - |
|
297 | - if (!isset($accessMap[$postVars['access']])) { |
|
298 | - throw new BadRequest('The "access" POST must be readwrite, read or no-access'); |
|
299 | - } |
|
300 | - $sharee = new Sharee([ |
|
301 | - 'href' => $postVars['href'], |
|
302 | - 'access' => $accessMap[$postVars['access']], |
|
303 | - ]); |
|
304 | - |
|
305 | - $this->shareResource( |
|
306 | - $path, |
|
307 | - [$sharee] |
|
308 | - ); |
|
309 | - |
|
310 | - return false; |
|
311 | - } |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * This method is triggered for POST actions generated by the browser |
|
272 | + * plugin. |
|
273 | + * |
|
274 | + * @param string $path |
|
275 | + * @param string $action |
|
276 | + * @param array $postVars |
|
277 | + */ |
|
278 | + public function browserPostAction($path, $action, $postVars) |
|
279 | + { |
|
280 | + if ('share' !== $action) { |
|
281 | + return; |
|
282 | + } |
|
283 | + |
|
284 | + if (empty($postVars['href'])) { |
|
285 | + throw new BadRequest('The "href" POST parameter is required'); |
|
286 | + } |
|
287 | + if (empty($postVars['access'])) { |
|
288 | + throw new BadRequest('The "access" POST parameter is required'); |
|
289 | + } |
|
290 | + |
|
291 | + $accessMap = [ |
|
292 | + 'readwrite' => self::ACCESS_READWRITE, |
|
293 | + 'read' => self::ACCESS_READ, |
|
294 | + 'no-access' => self::ACCESS_NOACCESS, |
|
295 | + ]; |
|
296 | + |
|
297 | + if (!isset($accessMap[$postVars['access']])) { |
|
298 | + throw new BadRequest('The "access" POST must be readwrite, read or no-access'); |
|
299 | + } |
|
300 | + $sharee = new Sharee([ |
|
301 | + 'href' => $postVars['href'], |
|
302 | + 'access' => $accessMap[$postVars['access']], |
|
303 | + ]); |
|
304 | + |
|
305 | + $this->shareResource( |
|
306 | + $path, |
|
307 | + [$sharee] |
|
308 | + ); |
|
309 | + |
|
310 | + return false; |
|
311 | + } |
|
312 | 312 | } |