|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OrangeJuice\Flysystem\WebDAV; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
|
6
|
|
|
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
|
7
|
|
|
use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait; |
|
8
|
|
|
use League\Flysystem\Adapter\Polyfill\StreamedTrait; |
|
9
|
|
|
use League\Flysystem\Config; |
|
10
|
|
|
use League\Flysystem\Util; |
|
11
|
|
|
use LogicException; |
|
12
|
|
|
use Sabre\DAV\Client; |
|
13
|
|
|
use Sabre\DAV\Exception; |
|
14
|
|
|
use Sabre\DAV\Exception\NotFound; |
|
15
|
|
|
|
|
16
|
|
|
class WebDAVAdapter extends AbstractAdapter |
|
17
|
|
|
{ |
|
18
|
|
|
use StreamedTrait; |
|
19
|
|
|
use StreamedCopyTrait; |
|
20
|
|
|
use NotSupportingVisibilityTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $resultMap = [ |
|
26
|
|
|
'{DAV:}getcontentlength' => 'size', |
|
27
|
|
|
'{DAV:}getcontenttype' => 'mimetype', |
|
28
|
|
|
'content-length' => 'size', |
|
29
|
|
|
'content-type' => 'mimetype', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Client |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Client $client |
|
41
|
|
|
* @param string $prefix |
|
42
|
|
|
*/ |
|
43
|
66 |
|
public function __construct(Client $client, $prefix = null) |
|
44
|
|
|
{ |
|
45
|
66 |
|
$this->client = $client; |
|
46
|
66 |
|
$this->setPathPrefix($prefix); |
|
47
|
66 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
12 |
|
public function getMetadata($path) |
|
53
|
|
|
{ |
|
54
|
12 |
|
$location = $this->applyPathPrefix($path); |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
12 |
|
$result = $this->client->propFind($location, [ |
|
58
|
12 |
|
'{DAV:}displayname', |
|
59
|
12 |
|
'{DAV:}getcontentlength', |
|
60
|
12 |
|
'{DAV:}getcontenttype', |
|
61
|
12 |
|
'{DAV:}getlastmodified', |
|
62
|
12 |
|
]); |
|
63
|
|
|
|
|
64
|
12 |
|
return $this->normalizeObject($result, $path); |
|
65
|
|
|
} catch (Exception $e) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
6 |
|
public function has($path) |
|
74
|
|
|
{ |
|
75
|
6 |
|
$location = $this->applyPathPrefix($path); |
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
6 |
|
$response = $this->client->request('HEAD', $location); |
|
79
|
3 |
|
return $response['statusCode'] === 200; |
|
80
|
3 |
|
} catch (Exception $e) { |
|
81
|
3 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
15 |
|
public function read($path) |
|
89
|
|
|
{ |
|
90
|
15 |
|
$location = $this->applyPathPrefix($path); |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
15 |
|
$response = $this->client->request('GET', $location); |
|
94
|
|
|
|
|
95
|
12 |
|
if ($response['statusCode'] !== 200) { |
|
96
|
6 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
return array_merge([ |
|
100
|
6 |
|
'contents' => $response['body'], |
|
101
|
6 |
|
'timestamp' => strtotime(is_array($response['headers']['last-modified']) |
|
102
|
6 |
|
? current($response['headers']['last-modified']) |
|
103
|
6 |
|
: $response['headers']['last-modified']), |
|
104
|
6 |
|
'path' => $path, |
|
105
|
6 |
|
], Util::map($response['headers'], static::$resultMap)); |
|
106
|
3 |
|
} catch (Exception $e) { |
|
107
|
3 |
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
9 |
|
public function write($path, $contents, Config $config) |
|
115
|
|
|
{ |
|
116
|
9 |
|
$location = $this->applyPathPrefix($path); |
|
117
|
9 |
|
$res = $this->client->request('PUT', $location, $contents); |
|
|
|
|
|
|
118
|
9 |
|
$result = compact('path', 'contents'); |
|
119
|
|
|
|
|
120
|
9 |
|
if ($config->get('visibility')) { |
|
121
|
3 |
|
throw new LogicException(__CLASS__.' does not support visibility settings.'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
6 |
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public function update($path, $contents, Config $config) |
|
131
|
|
|
{ |
|
132
|
3 |
|
return $this->write($path, $contents, $config); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
9 |
|
public function rename($path, $newpath) |
|
139
|
|
|
{ |
|
140
|
9 |
|
$location = $this->applyPathPrefix($path); |
|
141
|
9 |
|
$newLocation = $this->applyPathPrefix($newpath); |
|
142
|
|
|
|
|
143
|
|
|
try { |
|
144
|
9 |
|
$response = $this->client->request('MOVE', '/'.ltrim($location, '/'), null, [ |
|
145
|
9 |
|
'Destination' => '/'.ltrim($newLocation, '/'), |
|
146
|
9 |
|
]); |
|
147
|
|
|
|
|
148
|
6 |
|
if ($response['statusCode'] >= 200 && $response['statusCode'] < 300) { |
|
149
|
3 |
|
return true; |
|
150
|
|
|
} |
|
151
|
6 |
|
} catch (NotFound $e) { |
|
152
|
|
|
// Would have returned false here, but would be redundant |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
6 |
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
6 |
|
public function delete($path) |
|
162
|
|
|
{ |
|
163
|
6 |
|
$location = $this->applyPathPrefix($path); |
|
164
|
|
|
|
|
165
|
|
|
try { |
|
166
|
6 |
|
$this->client->request('DELETE', $location); |
|
167
|
|
|
|
|
168
|
3 |
|
return true; |
|
169
|
3 |
|
} catch (NotFound $e) { |
|
170
|
3 |
|
return false; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
6 |
|
public function createDir($path, Config $config) |
|
178
|
|
|
{ |
|
179
|
6 |
|
$location = $this->applyPathPrefix($path); |
|
180
|
6 |
|
$response = $this->client->request('MKCOL', $location); |
|
181
|
|
|
|
|
182
|
6 |
|
if ($response['statusCode'] !== 201) { |
|
183
|
3 |
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
3 |
|
return compact('path') + ['type' => 'dir']; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
6 |
|
public function deleteDir($dirname) |
|
193
|
|
|
{ |
|
194
|
6 |
|
return $this->delete($dirname); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritdoc} |
|
199
|
|
|
*/ |
|
200
|
3 |
|
public function listContents($directory = '', $recursive = false) |
|
201
|
|
|
{ |
|
202
|
3 |
|
$location = $this->applyPathPrefix($directory); |
|
203
|
3 |
|
$response = $this->client->propFind($location, [ |
|
204
|
3 |
|
'{DAV:}displayname', |
|
205
|
3 |
|
'{DAV:}getcontentlength', |
|
206
|
3 |
|
'{DAV:}getcontenttype', |
|
207
|
3 |
|
'{DAV:}getlastmodified', |
|
208
|
3 |
|
], 1); |
|
209
|
|
|
|
|
210
|
3 |
|
array_shift($response); |
|
211
|
3 |
|
$result = []; |
|
212
|
|
|
|
|
213
|
3 |
|
foreach ($response as $path => $object) { |
|
214
|
3 |
|
$path = $this->removePathPrefix($path); |
|
215
|
3 |
|
$object = $this->normalizeObject($object, $path); |
|
216
|
3 |
|
$result[] = $object; |
|
217
|
|
|
|
|
218
|
3 |
|
if ($recursive && $object['type'] === 'dir') { |
|
219
|
3 |
|
$result = array_merge($result, $this->listContents($object['path'], true)); |
|
220
|
3 |
|
} |
|
221
|
3 |
|
} |
|
222
|
|
|
|
|
223
|
3 |
|
return $result; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* {@inheritdoc} |
|
228
|
|
|
*/ |
|
229
|
3 |
|
public function getSize($path) |
|
230
|
|
|
{ |
|
231
|
3 |
|
return $this->getMetadata($path); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* {@inheritdoc} |
|
236
|
|
|
*/ |
|
237
|
3 |
|
public function getTimestamp($path) |
|
238
|
|
|
{ |
|
239
|
3 |
|
return $this->getMetadata($path); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* {@inheritdoc} |
|
244
|
|
|
*/ |
|
245
|
3 |
|
public function getMimetype($path) |
|
246
|
|
|
{ |
|
247
|
3 |
|
return $this->getMetadata($path); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Normalise a WebDAV repsonse object. |
|
252
|
|
|
* |
|
253
|
|
|
* @param array $object |
|
254
|
|
|
* @param string $path |
|
255
|
|
|
* |
|
256
|
|
|
* @return array |
|
257
|
|
|
*/ |
|
258
|
15 |
|
protected function normalizeObject(array $object, $path) |
|
259
|
|
|
{ |
|
260
|
15 |
|
if (! isset($object['{DAV:}getcontentlength'])) { |
|
261
|
3 |
|
return ['type' => 'dir', 'path' => trim($path, '/')]; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
15 |
|
$result = Util::map($object, static::$resultMap); |
|
265
|
|
|
|
|
266
|
15 |
|
if (isset($object['{DAV:}getlastmodified'])) { |
|
267
|
12 |
|
$result['timestamp'] = strtotime($object['{DAV:}getlastmodified']); |
|
268
|
12 |
|
} |
|
269
|
|
|
|
|
270
|
15 |
|
$result['type'] = 'file'; |
|
271
|
15 |
|
$result['path'] = trim($path, '/'); |
|
272
|
|
|
|
|
273
|
15 |
|
return $result; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.