2012-03-23 17:54:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Alexander Bogdanov <syn@li.ru>
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Carlos Cerrillo <ccerrillo@gmail.com>
|
|
|
|
* @author Felix Moeller <mail@felixmoeller.de>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Michael Gapczynski <GapczynskiM@gmail.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Philippe Kueck <pk@plusline.de>
|
|
|
|
* @author Philipp Kapfer <philipp.kapfer@gmx.at>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2012-03-23 17:54:38 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2012-09-07 16:30:48 +00:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2015-04-13 07:26:30 +00:00
|
|
|
use Exception;
|
|
|
|
use OC\Files\Filesystem;
|
|
|
|
use OC\Files\Stream\Close;
|
|
|
|
use OC\Files\Stream\Dir;
|
|
|
|
use OC\MemCache\ArrayCache;
|
|
|
|
use OCP\Constants;
|
|
|
|
use OCP\Files;
|
|
|
|
use OCP\Files\FileInfo;
|
2015-01-20 18:45:32 +00:00
|
|
|
use OCP\Files\StorageInvalidException;
|
2014-06-30 13:46:37 +00:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2015-04-13 07:26:30 +00:00
|
|
|
use OCP\Util;
|
|
|
|
use Sabre\DAV\Client;
|
|
|
|
use Sabre\DAV\Exception\NotFound;
|
2015-04-10 09:55:58 +00:00
|
|
|
use Sabre\HTTP\ClientException;
|
2015-03-09 10:48:55 +00:00
|
|
|
use Sabre\HTTP\ClientHttpException;
|
2014-06-30 13:46:37 +00:00
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/**
|
|
|
|
* Class DAV
|
|
|
|
*
|
|
|
|
* @package OC\Files\Storage
|
|
|
|
*/
|
2015-04-13 07:26:30 +00:00
|
|
|
class DAV extends Common {
|
|
|
|
/** @var string */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $password;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var string */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $user;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var string */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $host;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var bool */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $secure;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var string */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $root;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var string */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $certPath;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var bool */
|
2014-04-29 13:14:48 +00:00
|
|
|
protected $ready;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var Client */
|
2012-03-23 17:54:38 +00:00
|
|
|
private $client;
|
2015-04-13 07:26:30 +00:00
|
|
|
/** @var ArrayCache */
|
2015-02-11 17:16:01 +00:00
|
|
|
private $statCache;
|
2015-03-09 10:48:55 +00:00
|
|
|
/** @var array */
|
|
|
|
private static $tempFiles = [];
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public function __construct($params) {
|
2015-04-13 07:26:30 +00:00
|
|
|
$this->statCache = new ArrayCache();
|
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);
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->host = $host;
|
|
|
|
$this->user = $params['user'];
|
|
|
|
$this->password = $params['password'];
|
2012-12-24 18:45:52 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-12-15 16:22:52 +00:00
|
|
|
if ($this->secure === true) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$certPath = \OC_User::getHome(\OC_User::getUser()) . '/files_external/rootcerts.crt';
|
2013-12-15 16:22:52 +00:00
|
|
|
if (file_exists($certPath)) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->certPath = $certPath;
|
2013-12-15 16:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->root = isset($params['root']) ? $params['root'] : '/';
|
|
|
|
if (!$this->root || $this->root[0] != '/') {
|
|
|
|
$this->root = '/' . $this->root;
|
2012-12-24 18:45:52 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
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 {
|
2014-10-13 15:15:58 +00:00
|
|
|
throw new \Exception('Invalid webdav storage configuration');
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2012-10-21 20:04:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 14:44:58 +00:00
|
|
|
private function init() {
|
|
|
|
if ($this->ready) {
|
2012-10-21 20:04:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->ready = true;
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2013-12-17 08:38:43 +00:00
|
|
|
$settings = array(
|
|
|
|
'baseUri' => $this->createBaseUri(),
|
|
|
|
'userName' => $this->user,
|
|
|
|
'password' => $this->password,
|
|
|
|
);
|
2012-03-23 17:54:38 +00:00
|
|
|
|
2015-04-13 07:26:30 +00:00
|
|
|
$this->client = new Client($settings);
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->client->setThrowExceptions(true);
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2013-12-17 08:38:43 +00:00
|
|
|
if ($this->secure === true && $this->certPath) {
|
2013-12-15 16:22:52 +00:00
|
|
|
$this->client->addTrustedCertificates($this->certPath);
|
2012-07-09 08:19:19 +00:00
|
|
|
}
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 17:16:01 +00:00
|
|
|
/**
|
|
|
|
* Clear the stat cache
|
|
|
|
*/
|
|
|
|
public function clearStatCache() {
|
|
|
|
$this->statCache->clear();
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-03-04 14:44:58 +00:00
|
|
|
public function getId() {
|
2012-10-11 21:06:57 +00:00
|
|
|
return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-10-13 15:15:58 +00:00
|
|
|
public function createBaseUri() {
|
2014-03-04 14:44:58 +00:00
|
|
|
$baseUri = 'http';
|
2012-11-30 15:27:11 +00:00
|
|
|
if ($this->secure) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$baseUri .= 's';
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
$baseUri .= '://' . $this->host . $this->root;
|
2012-03-23 17:54:38 +00:00
|
|
|
return $baseUri;
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function mkdir($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2014-03-04 14:44:58 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2015-02-11 17:16:01 +00:00
|
|
|
$result = $this->simpleResponse('MKCOL', $path, null, 201);
|
|
|
|
if ($result) {
|
|
|
|
$this->statCache->set($path, true);
|
|
|
|
}
|
|
|
|
return $result;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function rmdir($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2015-02-11 17:16:01 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2014-01-31 15:06:11 +00:00
|
|
|
// FIXME: some WebDAV impl return 403 when trying to DELETE
|
|
|
|
// a non-empty folder
|
2015-02-11 17:16:01 +00:00
|
|
|
$result = $this->simpleResponse('DELETE', $path . '/', null, 204);
|
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->remove($path);
|
|
|
|
return $result;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function opendir($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2014-03-04 14:44:58 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$response = $this->client->propfind(
|
|
|
|
$this->encodePath($path),
|
|
|
|
array(),
|
|
|
|
1
|
|
|
|
);
|
2014-03-04 14:44:58 +00:00
|
|
|
$id = md5('webdav' . $this->root . $path);
|
2013-01-28 14:34:15 +00:00
|
|
|
$content = array();
|
2014-03-04 14:44:58 +00:00
|
|
|
$files = array_keys($response);
|
|
|
|
array_shift($files); //the first entry is the current directory
|
2015-02-11 17:16:01 +00:00
|
|
|
|
|
|
|
if (!$this->statCache->hasKey($path)) {
|
|
|
|
$this->statCache->set($path, true);
|
|
|
|
}
|
2012-11-30 15:27:11 +00:00
|
|
|
foreach ($files as $file) {
|
2015-02-11 17:16:01 +00:00
|
|
|
$file = urldecode($file);
|
|
|
|
// do not store the real entry, we might not have all properties
|
|
|
|
if (!$this->statCache->hasKey($path)) {
|
|
|
|
$this->statCache->set($file, true);
|
|
|
|
}
|
|
|
|
$file = basename($file);
|
2014-03-04 14:44:58 +00:00
|
|
|
$content[] = $file;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-13 07:26:30 +00:00
|
|
|
Dir::register($id, $content);
|
2014-03-04 14:44:58 +00:00
|
|
|
return opendir('fakedir://' . $id);
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404) {
|
2015-02-11 17:16:01 +00:00
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->set($path, false);
|
2015-02-12 11:29:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 17:16:01 +00:00
|
|
|
/**
|
|
|
|
* Propfind call with cache handling.
|
|
|
|
*
|
|
|
|
* First checks if information is cached.
|
|
|
|
* If not, request it from the server then store to cache.
|
|
|
|
*
|
|
|
|
* @param string $path path to propfind
|
|
|
|
*
|
|
|
|
* @return array propfind response
|
|
|
|
*
|
2015-04-13 07:26:30 +00:00
|
|
|
* @throws NotFound
|
2015-02-11 17:16:01 +00:00
|
|
|
*/
|
|
|
|
private function propfind($path) {
|
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$cachedResponse = $this->statCache->get($path);
|
|
|
|
if ($cachedResponse === false) {
|
|
|
|
// we know it didn't exist
|
2015-04-13 07:26:30 +00:00
|
|
|
throw new NotFound();
|
2015-02-11 17:16:01 +00:00
|
|
|
}
|
|
|
|
// we either don't know it, or we know it exists but need more details
|
|
|
|
if (is_null($cachedResponse) || $cachedResponse === true) {
|
|
|
|
$this->init();
|
|
|
|
try {
|
|
|
|
$response = $this->client->propfind(
|
|
|
|
$this->encodePath($path),
|
|
|
|
array(
|
|
|
|
'{DAV:}getlastmodified',
|
|
|
|
'{DAV:}getcontentlength',
|
|
|
|
'{DAV:}getcontenttype',
|
|
|
|
'{http://owncloud.org/ns}permissions',
|
|
|
|
'{DAV:}resourcetype',
|
|
|
|
'{DAV:}getetag',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->statCache->set($path, $response);
|
2015-04-13 07:26:30 +00:00
|
|
|
} catch (NotFound $e) {
|
2015-02-11 17:16:01 +00:00
|
|
|
// remember that this path did not exist
|
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->set($path, false);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$response = $cachedResponse;
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function filetype($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$response = $this->propfind($path);
|
2014-01-31 15:06:11 +00:00
|
|
|
$responseType = array();
|
|
|
|
if (isset($response["{DAV:}resourcetype"])) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$responseType = $response["{DAV:}resourcetype"]->resourceType;
|
2014-01-31 15:06:11 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function file_exists($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$cachedState = $this->statCache->get($path);
|
|
|
|
if ($cachedState === false) {
|
|
|
|
// we know the file doesn't exist
|
|
|
|
return false;
|
|
|
|
} else if (!is_null($cachedState)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// need to get from server
|
|
|
|
$this->propfind($path);
|
2014-03-04 14:44:58 +00:00
|
|
|
return true; //no 404 exception
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function unlink($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2015-02-11 17:16:01 +00:00
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$result = $this->simpleResponse('DELETE', $path, null, 204);
|
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->remove($path);
|
|
|
|
return $result;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2013-02-09 16:35:47 +00:00
|
|
|
public function fopen($path, $mode) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2014-03-04 14:44:58 +00:00
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
switch ($mode) {
|
2012-03-23 17:54:38 +00:00
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2014-03-04 14:44:58 +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+');
|
2014-03-04 14:44:58 +00:00
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $this->user . ':' . $this->password);
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->createBaseUri() . $this->encodePath($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);
|
2015-05-12 10:00:56 +00:00
|
|
|
if(defined('CURLOPT_PROTOCOLS')) {
|
|
|
|
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
|
|
|
}
|
|
|
|
if(defined('CURLOPT_REDIR_PROTOCOLS')) {
|
|
|
|
curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
|
|
|
}
|
2013-12-17 08:38:43 +00:00
|
|
|
if ($this->secure === true) {
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
|
2014-03-04 14:44:58 +00:00
|
|
|
if ($this->certPath) {
|
2013-12-17 08:38:43 +00:00
|
|
|
curl_setopt($curl, CURLOPT_CAINFO, $this->certPath);
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
|
|
|
|
curl_exec($curl);
|
2014-01-31 15:06:11 +00:00
|
|
|
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
|
|
if ($statusCode !== 200) {
|
2015-04-13 07:26:30 +00:00
|
|
|
Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, Util::ERROR);
|
2014-01-31 15:06:11 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
curl_close($curl);
|
2012-03-23 17:54:38 +00:00
|
|
|
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
|
2014-03-04 14:44:58 +00:00
|
|
|
if (strrpos($path, '.') !== false) {
|
|
|
|
$ext = substr($path, strrpos($path, '.'));
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2014-03-04 14:44:58 +00:00
|
|
|
$ext = '';
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
if ($this->file_exists($path)) {
|
2014-06-20 14:09:38 +00:00
|
|
|
if (!$this->isUpdatable($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
$tmpFile = $this->getCachedFile($path);
|
|
|
|
} else {
|
2014-06-20 14:09:38 +00:00
|
|
|
if (!$this->isCreatable(dirname($path))) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-13 07:26:30 +00:00
|
|
|
$tmpFile = Files::tmpFile($ext);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-13 07:26:30 +00:00
|
|
|
Close::registerCallback($tmpFile, array($this, 'writeBack'));
|
2014-03-04 14:44:58 +00:00
|
|
|
self::$tempFiles[$tmpFile] = $path;
|
|
|
|
return fopen('close://' . $tmpFile, $mode);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/**
|
|
|
|
* @param string $tmpFile
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function free_space($path) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2014-03-04 14:44:58 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
// TODO: cacheable ?
|
2014-03-04 14:44:58 +00:00
|
|
|
$response = $this->client->propfind($this->encodePath($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 {
|
2015-04-13 07:26:30 +00:00
|
|
|
return FileInfo::SPACE_UNKNOWN;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return FileInfo::SPACE_UNKNOWN;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-03-04 14:44:58 +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)) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$mtime = time();
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
$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)) {
|
2014-01-31 15:06:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$this->statCache->remove($path);
|
2014-01-31 15:06:11 +00:00
|
|
|
$this->client->proppatch($this->encodePath($path), array('{DAV:}lastmodified' => $mtime));
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 501) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2015-01-30 11:02:23 +00:00
|
|
|
return false;
|
2014-12-03 21:20:00 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-01-31 15:06:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-12 11:25:37 +00:00
|
|
|
} else {
|
|
|
|
$this->file_put_contents($path, '');
|
|
|
|
}
|
2013-11-25 11:44:27 +00:00
|
|
|
return true;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 17:16:01 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $data
|
2015-04-13 07:26:30 +00:00
|
|
|
* @return int
|
2015-02-11 17:16:01 +00:00
|
|
|
*/
|
|
|
|
public function file_put_contents($path, $data) {
|
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$result = parent::file_put_contents($path, $data);
|
|
|
|
$this->statCache->remove($path);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $target
|
|
|
|
*/
|
2014-03-04 14:44:58 +00:00
|
|
|
protected function uploadFile($path, $target) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2015-02-11 17:16:01 +00:00
|
|
|
// invalidate
|
|
|
|
$target = $this->cleanPath($target);
|
|
|
|
$this->statCache->remove($target);
|
2014-03-04 14:44:58 +00:00
|
|
|
$source = fopen($path, 'r');
|
2012-03-23 17:54:38 +00:00
|
|
|
|
|
|
|
$curl = curl_init();
|
2014-03-04 14:44:58 +00:00
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $this->user . ':' . $this->password);
|
2014-03-31 15:00:32 +00:00
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->createBaseUri() . $this->encodePath($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);
|
2014-07-03 10:04:38 +00:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
2014-09-11 17:21:56 +00:00
|
|
|
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
|
|
|
curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
2013-12-17 08:38:43 +00:00
|
|
|
if ($this->secure === true) {
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
|
2014-03-04 14:44:58 +00:00
|
|
|
if ($this->certPath) {
|
2013-12-17 08:38:43 +00:00
|
|
|
curl_setopt($curl, CURLOPT_CAINFO, $this->certPath);
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
curl_exec($curl);
|
2014-01-31 15:06:11 +00:00
|
|
|
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
|
|
if ($statusCode !== 200) {
|
2015-04-13 07:26:30 +00:00
|
|
|
Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, Util::ERROR);
|
2014-01-31 15:06:11 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
curl_close($curl);
|
2014-08-13 13:45:33 +00:00
|
|
|
fclose($source);
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->removeCachedFile($target);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2013-02-09 16:35:47 +00:00
|
|
|
public function rename($path1, $path2) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2015-02-11 17:16:01 +00:00
|
|
|
$path1 = $this->cleanPath($path1);
|
|
|
|
$path2 = $this->cleanPath($path2);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$this->client->request(
|
|
|
|
'MOVE',
|
|
|
|
$this->encodePath($path1),
|
|
|
|
null,
|
|
|
|
array(
|
|
|
|
'Destination' => $this->createBaseUri() . $this->encodePath($path2)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->statCache->clear($path1 . '/');
|
|
|
|
$this->statCache->clear($path2 . '/');
|
|
|
|
$this->statCache->set($path1, false);
|
|
|
|
$this->statCache->set($path2, true);
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->removeCachedFile($path1);
|
|
|
|
$this->removeCachedFile($path2);
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2013-02-09 16:35:47 +00:00
|
|
|
public function copy($path1, $path2) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2014-01-31 15:06:11 +00:00
|
|
|
$path1 = $this->encodePath($this->cleanPath($path1));
|
2014-03-04 14:44:58 +00:00
|
|
|
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->client->request('COPY', $path1, null, array('Destination' => $path2));
|
2015-02-11 17:16:01 +00:00
|
|
|
$this->statCache->clear($path2 . '/');
|
|
|
|
$this->statCache->set($path2, true);
|
2014-03-04 14:44:58 +00:00
|
|
|
$this->removeCachedFile($path2);
|
2012-03-23 17:54:38 +00:00
|
|
|
return true;
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function stat($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$response = $this->propfind($path);
|
2012-06-29 17:00:41 +00:00
|
|
|
return array(
|
2014-03-04 14:44:58 +00:00
|
|
|
'mtime' => strtotime($response['{DAV:}getlastmodified']),
|
|
|
|
'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0,
|
2012-06-29 17:00:41 +00:00
|
|
|
);
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404) {
|
|
|
|
return array();
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return array();
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 13:22:01 +00:00
|
|
|
public function getMimeType($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
$response = $this->propfind($path);
|
2014-01-31 15:06:11 +00:00
|
|
|
$responseType = array();
|
|
|
|
if (isset($response["{DAV:}resourcetype"])) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$responseType = $response["{DAV:}resourcetype"]->resourceType;
|
2014-01-31 15:06:11 +00:00
|
|
|
}
|
2014-03-04 14:44:58 +00:00
|
|
|
$type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
|
|
|
|
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;
|
|
|
|
}
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2015-03-09 10:48:55 +00:00
|
|
|
* @return string
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2013-02-12 10:05:12 +00:00
|
|
|
public function cleanPath($path) {
|
2015-02-11 17:16:01 +00:00
|
|
|
if ($path === '') {
|
2014-05-16 10:30:08 +00:00
|
|
|
return $path;
|
|
|
|
}
|
2015-04-13 07:26:30 +00:00
|
|
|
$path = Filesystem::normalizePath($path);
|
2013-11-20 17:14:42 +00:00
|
|
|
// remove leading slash
|
|
|
|
return substr($path, 1);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 15:06:11 +00:00
|
|
|
/**
|
|
|
|
* URL encodes the given path but keeps the slashes
|
2014-03-04 14:44:58 +00:00
|
|
|
*
|
2014-01-31 15:06:11 +00:00
|
|
|
* @param string $path to encode
|
|
|
|
* @return string encoded path
|
|
|
|
*/
|
|
|
|
private function encodePath($path) {
|
|
|
|
// slashes need to stay
|
|
|
|
return str_replace('%2F', '/', rawurlencode($path));
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param string $path
|
2015-03-09 10:48:55 +00:00
|
|
|
* @param string|resource|null $body
|
|
|
|
* @param int $expected
|
|
|
|
* @return bool
|
|
|
|
* @throws StorageInvalidException
|
|
|
|
* @throws StorageNotAvailableException
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2013-02-09 16:35:47 +00:00
|
|
|
private function simpleResponse($method, $path, $body, $expected) {
|
2014-03-04 14:44:58 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2012-11-30 15:27:11 +00:00
|
|
|
try {
|
2014-03-04 14:44:58 +00:00
|
|
|
$response = $this->client->request($method, $this->encodePath($path), $body);
|
|
|
|
return $response['statusCode'] == $expected;
|
2015-02-12 11:29:01 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404 && $method === 'DELETE') {
|
2015-02-11 17:16:01 +00:00
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->set($path, false);
|
2015-01-30 11:02:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2014-03-04 14:44:58 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
return false;
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2013-08-02 13:44:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check if curl is installed
|
|
|
|
*/
|
|
|
|
public static function checkDependencies() {
|
2015-03-12 20:43:41 +00:00
|
|
|
return true;
|
2013-08-02 13:44:56 +00:00
|
|
|
}
|
2014-06-04 16:19:52 +00:00
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 13:28:24 +00:00
|
|
|
public function isUpdatable($path) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE);
|
2014-06-13 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 13:28:24 +00:00
|
|
|
public function isCreatable($path) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE);
|
2014-06-13 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 13:28:24 +00:00
|
|
|
public function isSharable($path) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE);
|
2014-06-13 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 13:28:24 +00:00
|
|
|
public function isDeletable($path) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE);
|
2014-06-13 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 10:48:55 +00:00
|
|
|
/** {@inheritdoc} */
|
2014-06-04 16:19:52 +00:00
|
|
|
public function getPermissions($path) {
|
|
|
|
$this->init();
|
2014-11-11 14:20:36 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2015-04-27 15:41:02 +00:00
|
|
|
$response = $this->propfind($path);
|
2014-06-04 16:19:52 +00:00
|
|
|
if (isset($response['{http://owncloud.org/ns}permissions'])) {
|
2014-06-27 15:27:47 +00:00
|
|
|
return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']);
|
2014-06-13 13:28:24 +00:00
|
|
|
} else if ($this->is_dir($path)) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return Constants::PERMISSION_ALL;
|
2014-06-13 13:28:24 +00:00
|
|
|
} else if ($this->file_exists($path)) {
|
2015-04-13 07:26:30 +00:00
|
|
|
return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
|
2014-06-04 16:19:52 +00:00
|
|
|
} else {
|
2014-06-13 13:28:24 +00:00
|
|
|
return 0;
|
2014-06-04 16:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 15:10:46 +00:00
|
|
|
|
2015-04-27 15:41:02 +00:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getETag($path) {
|
|
|
|
$this->init();
|
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$response = $this->propfind($path);
|
|
|
|
if (isset($response['{DAV:}getetag'])) {
|
|
|
|
return trim($response['{DAV:}getetag'], '"');
|
|
|
|
}
|
|
|
|
return parent::getEtag($path);
|
|
|
|
}
|
|
|
|
|
2014-06-27 15:27:47 +00:00
|
|
|
/**
|
|
|
|
* @param string $permissionsString
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function parsePermissions($permissionsString) {
|
2015-04-13 07:26:30 +00:00
|
|
|
$permissions = Constants::PERMISSION_READ;
|
2014-06-27 15:27:47 +00:00
|
|
|
if (strpos($permissionsString, 'R') !== false) {
|
2015-04-13 07:26:30 +00:00
|
|
|
$permissions |= Constants::PERMISSION_SHARE;
|
2014-06-27 15:27:47 +00:00
|
|
|
}
|
|
|
|
if (strpos($permissionsString, 'D') !== false) {
|
2015-04-13 07:26:30 +00:00
|
|
|
$permissions |= Constants::PERMISSION_DELETE;
|
2014-06-27 15:27:47 +00:00
|
|
|
}
|
|
|
|
if (strpos($permissionsString, 'W') !== false) {
|
2015-04-13 07:26:30 +00:00
|
|
|
$permissions |= Constants::PERMISSION_UPDATE;
|
2014-06-27 15:27:47 +00:00
|
|
|
}
|
2014-06-27 15:28:56 +00:00
|
|
|
if (strpos($permissionsString, 'CK') !== false) {
|
2015-04-13 07:26:30 +00:00
|
|
|
$permissions |= Constants::PERMISSION_CREATE;
|
|
|
|
$permissions |= Constants::PERMISSION_UPDATE;
|
2014-06-27 15:27:47 +00:00
|
|
|
}
|
|
|
|
return $permissions;
|
|
|
|
}
|
|
|
|
|
2014-06-27 15:10:46 +00:00
|
|
|
/**
|
|
|
|
* check if a file or folder has been updated since $time
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param int $time
|
2014-06-30 13:46:37 +00:00
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2014-06-27 15:10:46 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasUpdated($path, $time) {
|
|
|
|
$this->init();
|
2014-11-11 14:20:36 +00:00
|
|
|
$path = $this->cleanPath($path);
|
2014-06-30 13:46:37 +00:00
|
|
|
try {
|
2015-02-11 17:16:01 +00:00
|
|
|
// force refresh for $path
|
|
|
|
$this->statCache->remove($path);
|
|
|
|
$response = $this->propfind($path);
|
2014-06-30 13:46:37 +00:00
|
|
|
if (isset($response['{DAV:}getetag'])) {
|
|
|
|
$cachedData = $this->getCache()->get($path);
|
2015-04-27 15:41:02 +00:00
|
|
|
$etag = null;
|
|
|
|
if (isset($response['{DAV:}getetag'])) {
|
|
|
|
$etag = trim($response['{DAV:}getetag'], '"');
|
|
|
|
}
|
|
|
|
if (!empty($etag) && $cachedData['etag'] !== $etag) {
|
2014-06-30 13:46:37 +00:00
|
|
|
return true;
|
|
|
|
} else if (isset($response['{http://owncloud.org/ns}permissions'])) {
|
|
|
|
$permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']);
|
|
|
|
return $permissions !== $cachedData['permissions'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-27 15:27:47 +00:00
|
|
|
} else {
|
2014-06-30 13:46:37 +00:00
|
|
|
$remoteMtime = strtotime($response['{DAV:}getlastmodified']);
|
|
|
|
return $remoteMtime > $time;
|
2014-06-27 15:27:47 +00:00
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
} catch (ClientHttpException $e) {
|
2015-02-12 11:29:01 +00:00
|
|
|
if ($e->getHttpStatus() === 404) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-10 09:55:58 +00:00
|
|
|
$this->convertException($e);
|
|
|
|
return false;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->convertException($e);
|
2015-01-30 11:02:23 +00:00
|
|
|
return false;
|
2014-06-27 15:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 18:45:32 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-10 09:55:58 +00:00
|
|
|
* Interpret the given exception and decide whether it is due to an
|
|
|
|
* unavailable storage, invalid storage or other.
|
|
|
|
* This will either throw StorageInvalidException, StorageNotAvailableException
|
|
|
|
* or do nothing.
|
|
|
|
*
|
|
|
|
* @param Exception $e sabre exception
|
2015-01-20 18:45:32 +00:00
|
|
|
*
|
|
|
|
* @throws StorageInvalidException if the storage is invalid, for example
|
|
|
|
* when the authentication expired or is invalid
|
|
|
|
* @throws StorageNotAvailableException if the storage is not available,
|
|
|
|
* which might be temporary
|
|
|
|
*/
|
2015-04-10 09:55:58 +00:00
|
|
|
private function convertException(Exception $e) {
|
2015-04-13 07:26:30 +00:00
|
|
|
Util::writeLog('files_external', $e->getMessage(), Util::ERROR);
|
2015-04-10 09:55:58 +00:00
|
|
|
if ($e instanceof ClientHttpException) {
|
|
|
|
if ($e->getHttpStatus() === 401) {
|
|
|
|
// either password was changed or was invalid all along
|
|
|
|
throw new StorageInvalidException(get_class($e).': '.$e->getMessage());
|
|
|
|
} else if ($e->getHttpStatus() === 405) {
|
|
|
|
// ignore exception for MethodNotAllowed, false will be returned
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage());
|
|
|
|
} else if ($e instanceof ClientException) {
|
|
|
|
// connection timeout or refused, server could be temporarily down
|
|
|
|
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage());
|
|
|
|
} else if ($e instanceof \InvalidArgumentException) {
|
|
|
|
// parse error because the server returned HTML instead of XML,
|
|
|
|
// possibly temporarily down
|
|
|
|
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage());
|
|
|
|
} else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) {
|
|
|
|
// rethrow
|
|
|
|
throw $e;
|
2015-01-20 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 09:55:58 +00:00
|
|
|
// TODO: only log for now, but in the future need to wrap/rethrow exception
|
2015-01-20 18:45:32 +00:00
|
|
|
}
|
2012-03-23 17:54:38 +00:00
|
|
|
}
|
2013-12-15 16:22:52 +00:00
|
|
|
|