Blame view

sources/apps/files_external/lib/webdav.php 9.27 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  <?php
  /**
   * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OC\Files\Storage;
  
  class DAV extends \OC\Files\Storage\Common{
  	private $password;
  	private $user;
  	private $host;
  	private $secure;
  	private $root;
a293d369c   Kload   Update sources to...
17
  	private $certPath;
03e52840d   Kload   Init
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  	private $ready;
  	/**
  	 * @var \Sabre_DAV_Client
  	 */
  	private $client;
  
  	private static $tempFiles=array();
  
  	public function __construct($params) {
  		if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
  			$host = $params['host'];
  			//remove leading http[s], will be generated in createBaseUri()
  			if (substr($host, 0, 8) == "https://") $host = substr($host, 8);
  			else if (substr($host, 0, 7) == "http://") $host = substr($host, 7);
  			$this->host=$host;
  			$this->user=$params['user'];
  			$this->password=$params['password'];
  			if (isset($params['secure'])) {
  				if (is_string($params['secure'])) {
  					$this->secure = ($params['secure'] === 'true');
  				} else {
  					$this->secure = (bool)$params['secure'];
  				}
  			} else {
  				$this->secure = false;
  			}
a293d369c   Kload   Update sources to...
44
45
46
47
48
49
  			if ($this->secure === true) {
  				$certPath=\OC_User::getHome(\OC_User::getUser()) . '/files_external/rootcerts.crt';
  				if (file_exists($certPath)) {
  					$this->certPath=$certPath;
  				}
  			}
03e52840d   Kload   Init
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  			$this->root=isset($params['root'])?$params['root']:'/';
  			if ( ! $this->root || $this->root[0]!='/') {
  				$this->root='/'.$this->root;
  			}
  			if (substr($this->root, -1, 1)!='/') {
  				$this->root.='/';
  			}
  		} else {
  			throw new \Exception();
  		}
  	}
  
  	private function init(){
  		if($this->ready) {
  			return;
  		}
  		$this->ready = true;
a293d369c   Kload   Update sources to...
67
68
69
70
71
  		$settings = array(
  			'baseUri' => $this->createBaseUri(),
  			'userName' => $this->user,
  			'password' => $this->password,
  		);
03e52840d   Kload   Init
72
73
  
  		$this->client = new \Sabre_DAV_Client($settings);
a293d369c   Kload   Update sources to...
74
75
  		if ($this->secure === true && $this->certPath) {
  			$this->client->addTrustedCertificates($this->certPath);
03e52840d   Kload   Init
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  		}
  	}
  
  	public function getId(){
  		return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
  	}
  
  	private function createBaseUri() {
  		$baseUri='http';
  		if ($this->secure) {
  			$baseUri.='s';
  		}
  		$baseUri.='://'.$this->host.$this->root;
  		return $baseUri;
  	}
  
  	public function mkdir($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		return $this->simpleResponse('MKCOL', $path, null, 201);
  	}
  
  	public function rmdir($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		return $this->simpleResponse('DELETE', $path, null, 204);
  	}
  
  	public function opendir($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		try {
  			$response=$this->client->propfind($path, array(), 1);
  			$id=md5('webdav'.$this->root.$path);
  			$content = array();
  			$files=array_keys($response);
  			array_shift($files);//the first entry is the current directory
  			foreach ($files as $file) {
  				$file = urldecode(basename($file));
  				$content[]=$file;
  			}
  			\OC\Files\Stream\Dir::register($id, $content);
  			return opendir('fakedir://'.$id);
  		} catch(\Exception $e) {
  			return false;
  		}
  	}
  
  	public function filetype($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		try {
  			$response=$this->client->propfind($path, array('{DAV:}resourcetype'));
  			$responseType=$response["{DAV:}resourcetype"]->resourceType;
  			return (count($responseType)>0 and $responseType[0]=="{DAV:}collection")?'dir':'file';
  		} catch(\Exception $e) {
  			error_log($e->getMessage());
  			\OCP\Util::writeLog("webdav client", \OCP\Util::sanitizeHTML($e->getMessage()), \OCP\Util::ERROR);
  			return false;
  		}
  	}
03e52840d   Kload   Init
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  	public function file_exists($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		try {
  			$this->client->propfind($path, array('{DAV:}resourcetype'));
  			return true;//no 404 exception
  		} catch(\Exception $e) {
  			return false;
  		}
  	}
  
  	public function unlink($path) {
  		$this->init();
  		return $this->simpleResponse('DELETE', $path, null, 204);
  	}
  
  	public function fopen($path, $mode) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		switch($mode) {
  			case 'r':
  			case 'rb':
  				if ( ! $this->file_exists($path)) {
  					return false;
  				}
  				//straight up curl instead of sabredav here, sabredav put's the entire get result in memory
  				$curl = curl_init();
  				$fp = fopen('php://temp', 'r+');
  				curl_setopt($curl, CURLOPT_USERPWD, $this->user.':'.$this->password);
31b7f2792   Kload   Upgrade to ownclo...
166
  				curl_setopt($curl, CURLOPT_URL, $this->createBaseUri().str_replace(' ', '%20', $path));
03e52840d   Kload   Init
167
  				curl_setopt($curl, CURLOPT_FILE, $fp);
31b7f2792   Kload   Upgrade to ownclo...
168
  				curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
a293d369c   Kload   Update sources to...
169
170
171
172
173
174
175
176
  				if ($this->secure === true) {
  					curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  					curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  					if($this->certPath){
  						curl_setopt($curl, CURLOPT_CAINFO, $this->certPath);
  					}
  				}
  				
03e52840d   Kload   Init
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  				curl_exec ($curl);
  				curl_close ($curl);
  				rewind($fp);
  				return $fp;
  			case 'w':
  			case 'wb':
  			case 'a':
  			case 'ab':
  			case 'r+':
  			case 'w+':
  			case 'wb+':
  			case 'a+':
  			case 'x':
  			case 'x+':
  			case 'c':
  			case 'c+':
  				//emulate these
  				if (strrpos($path, '.')!==false) {
  					$ext=substr($path, strrpos($path, '.'));
  				} else {
  					$ext='';
  				}
  				$tmpFile = \OCP\Files::tmpFile($ext);
  				\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  				if($this->file_exists($path)) {
  					$this->getFile($path, $tmpFile);
  				}
  				self::$tempFiles[$tmpFile]=$path;
  				return fopen('close://'.$tmpFile, $mode);
  		}
  	}
  
  	public function writeBack($tmpFile) {
  		if (isset(self::$tempFiles[$tmpFile])) {
  			$this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
  			unlink($tmpFile);
  		}
  	}
  
  	public function free_space($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		try {
  			$response=$this->client->propfind($path, array('{DAV:}quota-available-bytes'));
  			if (isset($response['{DAV:}quota-available-bytes'])) {
  				return (int)$response['{DAV:}quota-available-bytes'];
  			} else {
a293d369c   Kload   Update sources to...
224
  				return \OC\Files\SPACE_UNKNOWN;
03e52840d   Kload   Init
225
226
  			}
  		} catch(\Exception $e) {
31b7f2792   Kload   Upgrade to ownclo...
227
  			return \OC\Files\SPACE_UNKNOWN;
03e52840d   Kload   Init
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  		}
  	}
  
  	public function touch($path, $mtime=null) {
  		$this->init();
  		if (is_null($mtime)) {
  			$mtime=time();
  		}
  		$path=$this->cleanPath($path);
  
  		// if file exists, update the mtime, else create a new empty file
  		if ($this->file_exists($path)) {
  			$this->client->proppatch($path, array('{DAV:}lastmodified' => $mtime));
  		} else {
  			$this->file_put_contents($path, '');
  		}
31b7f2792   Kload   Upgrade to ownclo...
244
  		return true;
03e52840d   Kload   Init
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  	}
  
  	public function getFile($path, $target) {
  		$this->init();
  		$source=$this->fopen($path, 'r');
  		file_put_contents($target, $source);
  	}
  
  	public function uploadFile($path, $target) {
  		$this->init();
  		$source=fopen($path, 'r');
  
  		$curl = curl_init();
  		curl_setopt($curl, CURLOPT_USERPWD, $this->user.':'.$this->password);
31b7f2792   Kload   Upgrade to ownclo...
259
  		curl_setopt($curl, CURLOPT_URL, $this->createBaseUri().str_replace(' ', '%20', $target));
03e52840d   Kload   Init
260
261
262
263
  		curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
  		curl_setopt($curl, CURLOPT_INFILE, $source); // file pointer
  		curl_setopt($curl, CURLOPT_INFILESIZE, filesize($path));
  		curl_setopt($curl, CURLOPT_PUT, true);
a293d369c   Kload   Update sources to...
264
265
266
267
268
269
270
  		if ($this->secure === true) {
  			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  			if($this->certPath){
  				curl_setopt($curl, CURLOPT_CAINFO, $this->certPath);
  			}
  		}
03e52840d   Kload   Init
271
272
273
274
275
276
277
  		curl_exec ($curl);
  		curl_close ($curl);
  	}
  
  	public function rename($path1, $path2) {
  		$this->init();
  		$path1=$this->cleanPath($path1);
31b7f2792   Kload   Upgrade to ownclo...
278
  		$path2=$this->createBaseUri().$this->cleanPath($path2);
03e52840d   Kload   Init
279
280
281
282
283
284
285
286
287
288
289
  		try {
  			$this->client->request('MOVE', $path1, null, array('Destination'=>$path2));
  			return true;
  		} catch(\Exception $e) {
  			return false;
  		}
  	}
  
  	public function copy($path1, $path2) {
  		$this->init();
  		$path1=$this->cleanPath($path1);
31b7f2792   Kload   Upgrade to ownclo...
290
  		$path2=$this->createBaseUri().$this->cleanPath($path2);
03e52840d   Kload   Init
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  		try {
  			$this->client->request('COPY', $path1, null, array('Destination'=>$path2));
  			return true;
  		} catch(\Exception $e) {
  			return false;
  		}
  	}
  
  	public function stat($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		try {
  			$response=$this->client->propfind($path, array('{DAV:}getlastmodified', '{DAV:}getcontentlength'));
  			return array(
  				'mtime'=>strtotime($response['{DAV:}getlastmodified']),
  				'size'=>(int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0,
  			);
  		} catch(\Exception $e) {
  			return array();
  		}
  	}
  
  	public function getMimeType($path) {
  		$this->init();
  		$path=$this->cleanPath($path);
  		try {
  			$response=$this->client->propfind($path, array('{DAV:}getcontenttype', '{DAV:}resourcetype'));
  			$responseType=$response["{DAV:}resourcetype"]->resourceType;
  			$type=(count($responseType)>0 and $responseType[0]=="{DAV:}collection")?'dir':'file';
  			if ($type=='dir') {
  				return 'httpd/unix-directory';
  			} elseif (isset($response['{DAV:}getcontenttype'])) {
  				return $response['{DAV:}getcontenttype'];
  			} else {
  				return false;
  			}
  		} catch(\Exception $e) {
  			return false;
  		}
  	}
  
  	public function cleanPath($path) {
31b7f2792   Kload   Upgrade to ownclo...
333
334
335
  		$path = \OC\Files\Filesystem::normalizePath($path);
  		// remove leading slash
  		return substr($path, 1);
03e52840d   Kload   Init
336
337
338
339
340
341
342
343
344
345
346
347
  	}
  
  	private function simpleResponse($method, $path, $body, $expected) {
  		$path=$this->cleanPath($path);
  		try {
  			$response=$this->client->request($method, $path, $body);
  			return $response['statusCode']==$expected;
  		} catch(\Exception $e) {
  			return false;
  		}
  	}
  }
a293d369c   Kload   Update sources to...
348