2012-03-23 17:54:38 +00:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2012-09-07 16:30:48 +00:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
|
|
|
class DAV extends \OC\Files\Storage\Common{
|
2012-03-23 17:54:38 +00:00
|
|
|
private $password;
|
|
|
|
private $user;
|
|
|
|
private $host;
|
|
|
|
private $secure;
|
|
|
|
private $root;
|
2012-10-21 20:04:45 +00:00
|
|
|
private $ready;
|
2012-03-23 17:54:38 +00:00
|
|
|
/**
|
2012-10-11 21:17:59 +00:00
|
|
|
* @var \Sabre_DAV_Client
|
2012-03-23 17:54:38 +00:00
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
private static $tempFiles=array();
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function __construct($params) {
|
2012-12-24 18:45:52 +00:00
|
|
|
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'];
|
|
|
|
}
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-12-24 18:45:52 +00:00
|
|
|
$this->secure = false;
|
|
|
|
}
|
|
|
|
$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.='/';
|
2012-11-05 15:39:03 +00:00
|
|
|
}
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2013-02-12 01:27:05 +00:00
|
|
|
throw new \Exception();
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2012-10-21 20:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function init(){
|
2013-02-09 15:46:55 +00:00
|
|
|
if($this->ready) {
|
2012-10-21 20:04:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->ready = true;
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-12-24 18:45:52 +00:00
|
|
|
$settings = array(
|
|
|
|
'baseUri' => $this->createBaseUri(),
|
|
|
|
'userName' => $this->user,
|
|
|
|
'password' => $this->password,
|
|
|
|
);
|
2012-03-23 17:54:38 +00:00
|
|
|
|
2013-01-15 13:57:23 +00:00
|
|
|
$this->client = new \Sabre_DAV_Client($settings);
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-11-30 15:27:11 +00:00
|
|
|
$caview = \OCP\Files::getStorage('files_external');
|
|
|
|
if ($caview) {
|
2012-07-09 07:40:33 +00:00
|
|
|
$certPath=\OCP\Config::getSystemValue('datadirectory').$caview->getAbsolutePath("").'rootcerts.crt';
|
2012-11-04 17:18:03 +00:00
|
|
|
if (file_exists($certPath)) {
|
2012-07-09 07:40:33 +00:00
|
|
|
$this->client->addTrustedCertificates($certPath);
|
|
|
|
}
|
2012-07-09 08:19:19 +00:00
|
|
|
}
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 21:06:57 +00:00
|
|
|
public function getId(){
|
|
|
|
return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
private function createBaseUri() {
|
2012-03-23 17:54:38 +00:00
|
|
|
$baseUri='http';
|
2012-11-30 15:27:11 +00:00
|
|
|
if ($this->secure) {
|
2012-06-29 09:59:47 +00:00
|
|
|
$baseUri.='s';
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
$baseUri.='://'.$this->host.$this->root;
|
|
|
|
return $baseUri;
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function mkdir($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-10-28 22:58:08 +00:00
|
|
|
return $this->simpleResponse('MKCOL', $path, null, 201);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function rmdir($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-10-28 22:58:08 +00:00
|
|
|
return $this->simpleResponse('DELETE', $path, null, 204);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function opendir($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-10-28 22:58:08 +00:00
|
|
|
$response=$this->client->propfind($path, array(), 1);
|
2012-03-23 17:54:38 +00:00
|
|
|
$id=md5('webdav'.$this->root.$path);
|
2013-01-28 14:34:15 +00:00
|
|
|
$content = array();
|
2012-07-20 14:34:57 +00:00
|
|
|
$files=array_keys($response);
|
|
|
|
array_shift($files);//the first entry is the current directory
|
2012-11-30 15:27:11 +00:00
|
|
|
foreach ($files as $file) {
|
2012-06-29 17:00:41 +00:00
|
|
|
$file = urldecode(basename($file));
|
2013-01-28 14:34:15 +00:00
|
|
|
$content[]=$file;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2013-01-28 14:34:15 +00:00
|
|
|
\OC\Files\Stream\Dir::register($id, $content);
|
2012-03-23 17:54:38 +00:00
|
|
|
return opendir('fakedir://'.$id);
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function filetype($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-03-23 17:54:38 +00:00
|
|
|
$response=$this->client->propfind($path, array('{DAV:}resourcetype'));
|
|
|
|
$responseType=$response["{DAV:}resourcetype"]->resourceType;
|
|
|
|
return (count($responseType)>0 and $responseType[0]=="{DAV:}collection")?'dir':'file';
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-07-02 15:52:51 +00:00
|
|
|
error_log($e->getMessage());
|
|
|
|
\OCP\Util::writeLog("webdav client", \OCP\Util::sanitizeHTML($e->getMessage()), \OCP\Util::ERROR);
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function isReadable($path) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;//not properly supported
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function isUpdatable($path) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;//not properly supported
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function file_exists($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-07-20 14:34:57 +00:00
|
|
|
$this->client->propfind($path, array('{DAV:}resourcetype'));
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;//no 404 exception
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function unlink($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2013-02-09 16:35:47 +00:00
|
|
|
return $this->simpleResponse('DELETE', $path, null, 204);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 16:35:47 +00:00
|
|
|
public function fopen($path, $mode) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
switch($mode) {
|
2012-03-23 17:54:38 +00:00
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2012-11-30 15:27:11 +00:00
|
|
|
if ( ! $this->file_exists($path)) {
|
2012-10-11 19:13:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-23 17:54:38 +00:00
|
|
|
//straight up curl instead of sabredav here, sabredav put's the entire get result in memory
|
|
|
|
$curl = curl_init();
|
|
|
|
$fp = fopen('php://temp', 'r+');
|
2012-10-28 22:58:08 +00:00
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $this->user.':'.$this->password);
|
2013-08-19 09:09:55 +00:00
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->createBaseUri().str_replace(' ', '%20', $path));
|
2012-03-23 17:54:38 +00:00
|
|
|
curl_setopt($curl, CURLOPT_FILE, $fp);
|
2013-08-02 19:18:29 +00:00
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
2012-03-23 17:54:38 +00:00
|
|
|
|
|
|
|
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
|
2012-11-30 15:27:11 +00:00
|
|
|
if (strrpos($path, '.')!==false) {
|
2012-10-28 22:58:08 +00:00
|
|
|
$ext=substr($path, strrpos($path, '.'));
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-03-23 17:54:38 +00:00
|
|
|
$ext='';
|
|
|
|
}
|
2012-10-30 21:59:55 +00:00
|
|
|
$tmpFile = \OCP\Files::tmpFile($ext);
|
2013-01-28 14:34:15 +00:00
|
|
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
|
2012-09-07 13:22:01 +00:00
|
|
|
if($this->file_exists($path)) {
|
2012-10-28 22:58:08 +00:00
|
|
|
$this->getFile($path, $tmpFile);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
self::$tempFiles[$tmpFile]=$path;
|
2012-10-28 22:58:08 +00:00
|
|
|
return fopen('close://'.$tmpFile, $mode);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function writeBack($tmpFile) {
|
2012-11-30 15:27:11 +00:00
|
|
|
if (isset(self::$tempFiles[$tmpFile])) {
|
2012-10-23 20:53:54 +00:00
|
|
|
$this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
|
2012-03-23 17:54:38 +00:00
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function free_space($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-03-23 17:54:38 +00:00
|
|
|
$response=$this->client->propfind($path, array('{DAV:}quota-available-bytes'));
|
2012-11-30 15:27:11 +00:00
|
|
|
if (isset($response['{DAV:}quota-available-bytes'])) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return (int)$response['{DAV:}quota-available-bytes'];
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-03-23 17:54:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2013-07-25 14:14:46 +00:00
|
|
|
return \OC\Files\SPACE_UNKNOWN;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:53:02 +00:00
|
|
|
public function touch($path, $mtime=null) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-11-30 15:27:11 +00:00
|
|
|
if (is_null($mtime)) {
|
2012-03-23 17:54:38 +00:00
|
|
|
$mtime=time();
|
|
|
|
}
|
|
|
|
$path=$this->cleanPath($path);
|
2013-07-12 11:25:37 +00:00
|
|
|
|
|
|
|
// 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, '');
|
|
|
|
}
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 16:35:47 +00:00
|
|
|
public function getFile($path, $target) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2013-02-09 16:35:47 +00:00
|
|
|
$source=$this->fopen($path, 'r');
|
|
|
|
file_put_contents($target, $source);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 16:35:47 +00:00
|
|
|
public function uploadFile($path, $target) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-10-28 22:58:08 +00:00
|
|
|
$source=fopen($path, 'r');
|
2012-03-23 17:54:38 +00:00
|
|
|
|
|
|
|
$curl = curl_init();
|
2012-10-28 22:58:08 +00:00
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $this->user.':'.$this->password);
|
2013-08-19 09:09:55 +00:00
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->createBaseUri().str_replace(' ', '%20', $target));
|
2012-03-23 17:54:38 +00:00
|
|
|
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);
|
|
|
|
curl_exec ($curl);
|
|
|
|
curl_close ($curl);
|
|
|
|
}
|
|
|
|
|
2013-02-09 16:35:47 +00:00
|
|
|
public function rename($path1, $path2) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path1=$this->cleanPath($path1);
|
|
|
|
$path2=$this->root.$this->cleanPath($path2);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-12-03 17:02:22 +00:00
|
|
|
$this->client->request('MOVE', $path1, null, array('Destination'=>$path2));
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 16:35:47 +00:00
|
|
|
public function copy($path1, $path2) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path1=$this->cleanPath($path1);
|
|
|
|
$path2=$this->root.$this->cleanPath($path2);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-12-03 17:02:22 +00:00
|
|
|
$this->client->request('COPY', $path1, null, array('Destination'=>$path2));
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function stat($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-11-04 10:10:46 +00:00
|
|
|
$response=$this->client->propfind($path, array('{DAV:}getlastmodified', '{DAV:}getcontentlength'));
|
2012-06-29 17:00:41 +00:00
|
|
|
return array(
|
|
|
|
'mtime'=>strtotime($response['{DAV:}getlastmodified']),
|
|
|
|
'size'=>(int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0,
|
|
|
|
);
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function getMimeType($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-11-04 10:10:46 +00:00
|
|
|
$response=$this->client->propfind($path, array('{DAV:}getcontenttype', '{DAV:}resourcetype'));
|
2012-03-23 17:54:38 +00:00
|
|
|
$responseType=$response["{DAV:}resourcetype"]->resourceType;
|
|
|
|
$type=(count($responseType)>0 and $responseType[0]=="{DAV:}collection")?'dir':'file';
|
2012-11-30 15:27:11 +00:00
|
|
|
if ($type=='dir') {
|
2012-03-23 17:54:38 +00:00
|
|
|
return 'httpd/unix-directory';
|
2012-11-30 15:27:11 +00:00
|
|
|
} elseif (isset($response['{DAV:}getcontenttype'])) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return $response['{DAV:}getcontenttype'];
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 10:05:12 +00:00
|
|
|
public function cleanPath($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
if ( ! $path || $path[0]=='/') {
|
2012-10-28 22:58:08 +00:00
|
|
|
return substr($path, 1);
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-03-23 17:54:38 +00:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 16:35:47 +00:00
|
|
|
private function simpleResponse($method, $path, $body, $expected) {
|
2012-03-23 17:54:38 +00:00
|
|
|
$path=$this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2012-10-28 22:58:08 +00:00
|
|
|
$response=$this->client->request($method, $path, $body);
|
2012-03-23 17:54:38 +00:00
|
|
|
return $response['statusCode']==$expected;
|
2012-12-03 17:02:22 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-03-23 17:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|