2012-03-23 21:54:38 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Carlos Cerrillo <ccerrillo@gmail.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Michael Gapczynski <GapczynskiM@gmail.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Philipp Kapfer <philipp.kapfer@gmx.at>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2017-11-07 15:47:42 +03:00
|
|
|
* @author vkuimov "vkuimov@nextcloud"
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @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 21:54:38 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2012-09-07 20:30:48 +04:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2015-04-13 10:26:30 +03:00
|
|
|
use Exception;
|
2015-12-10 19:13:02 +03:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-01-03 19:26:44 +03:00
|
|
|
use Icewind\Streams\CallbackWrapper;
|
2015-03-10 18:30:13 +03:00
|
|
|
use Icewind\Streams\IteratorDirectory;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Files\Filesystem;
|
2015-04-13 10:26:30 +03:00
|
|
|
use OC\MemCache\ArrayCache;
|
2015-08-29 15:53:59 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2015-04-13 10:26:30 +03:00
|
|
|
use OCP\Constants;
|
|
|
|
use OCP\Files\FileInfo;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Files\ForbiddenException;
|
2015-01-20 21:45:32 +03:00
|
|
|
use OCP\Files\StorageInvalidException;
|
2014-06-30 17:46:37 +04:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\ILogger;
|
2015-04-13 10:26:30 +03:00
|
|
|
use OCP\Util;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2015-04-13 10:26:30 +03:00
|
|
|
use Sabre\DAV\Client;
|
2015-11-20 15:35:23 +03:00
|
|
|
use Sabre\DAV\Xml\Property\ResourceType;
|
2015-04-10 12:55:58 +03:00
|
|
|
use Sabre\HTTP\ClientException;
|
2015-03-09 13:48:55 +03:00
|
|
|
use Sabre\HTTP\ClientHttpException;
|
2014-06-30 17:46:37 +04:00
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/**
|
|
|
|
* Class DAV
|
|
|
|
*
|
|
|
|
* @package OC\Files\Storage
|
|
|
|
*/
|
2015-04-13 10:26:30 +03:00
|
|
|
class DAV extends Common {
|
|
|
|
/** @var string */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $password;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var string */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $user;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var string */
|
2017-02-22 23:47:49 +03:00
|
|
|
protected $authType;
|
|
|
|
/** @var string */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $host;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var bool */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $secure;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var string */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $root;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var string */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $certPath;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var bool */
|
2014-04-29 17:14:48 +04:00
|
|
|
protected $ready;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var Client */
|
2017-02-15 16:00:00 +03:00
|
|
|
protected $client;
|
2015-04-13 10:26:30 +03:00
|
|
|
/** @var ArrayCache */
|
2017-02-15 16:00:00 +03:00
|
|
|
protected $statCache;
|
2015-09-01 19:01:27 +03:00
|
|
|
/** @var \OCP\Http\Client\IClientService */
|
2017-02-15 16:00:00 +03:00
|
|
|
protected $httpClientService;
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function __construct($params) {
|
2015-04-13 10:26:30 +03:00
|
|
|
$this->statCache = new ArrayCache();
|
2015-09-01 19:01:27 +03:00
|
|
|
$this->httpClientService = \OC::$server->getHTTPClientService();
|
2012-12-24 22:45:52 +04: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 18:44:58 +04:00
|
|
|
$this->host = $host;
|
|
|
|
$this->user = $params['user'];
|
|
|
|
$this->password = $params['password'];
|
2017-02-22 23:47:49 +03:00
|
|
|
if (isset($params['authType'])) {
|
|
|
|
$this->authType = $params['authType'];
|
|
|
|
}
|
2012-12-24 22:45:52 +04:00
|
|
|
if (isset($params['secure'])) {
|
|
|
|
if (is_string($params['secure'])) {
|
|
|
|
$this->secure = ($params['secure'] === 'true');
|
|
|
|
} else {
|
|
|
|
$this->secure = (bool)$params['secure'];
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2012-12-24 22:45:52 +04:00
|
|
|
$this->secure = false;
|
|
|
|
}
|
2013-12-15 20:22:52 +04:00
|
|
|
if ($this->secure === true) {
|
2015-11-25 18:58:54 +03:00
|
|
|
// inject mock for testing
|
2016-06-27 23:26:43 +03:00
|
|
|
$certManager = \OC::$server->getCertificateManager();
|
|
|
|
if (is_null($certManager)) { //no user
|
|
|
|
$certManager = \OC::$server->getCertificateManager(null);
|
|
|
|
}
|
|
|
|
$certPath = $certManager->getAbsoluteBundlePath();
|
2013-12-15 20:22:52 +04:00
|
|
|
if (file_exists($certPath)) {
|
2014-03-04 18:44:58 +04:00
|
|
|
$this->certPath = $certPath;
|
2013-12-15 20:22:52 +04:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 00:26:47 +03:00
|
|
|
$this->root = $params['root'] ?? '/';
|
|
|
|
$this->root = '/' . ltrim($this->root, '/');
|
|
|
|
$this->root = rtrim($this->root, '/') . '/';
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2014-10-13 19:15:58 +04:00
|
|
|
throw new \Exception('Invalid webdav storage configuration');
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2012-10-22 00:04:45 +04:00
|
|
|
}
|
|
|
|
|
2017-02-15 16:00:00 +03:00
|
|
|
protected function init() {
|
2014-03-04 18:44:58 +04:00
|
|
|
if ($this->ready) {
|
2012-10-22 00:04:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->ready = true;
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2017-02-22 23:47:49 +03:00
|
|
|
$settings = [
|
2013-12-17 12:38:43 +04:00
|
|
|
'baseUri' => $this->createBaseUri(),
|
|
|
|
'userName' => $this->user,
|
|
|
|
'password' => $this->password,
|
2017-02-22 23:47:49 +03:00
|
|
|
];
|
|
|
|
if (isset($this->authType)) {
|
|
|
|
$settings['authType'] = $this->authType;
|
|
|
|
}
|
2012-03-23 21:54:38 +04:00
|
|
|
|
2016-02-24 21:48:47 +03:00
|
|
|
$proxy = \OC::$server->getConfig()->getSystemValue('proxy', '');
|
2017-09-28 15:22:42 +03:00
|
|
|
if ($proxy !== '') {
|
2016-02-24 21:48:47 +03:00
|
|
|
$settings['proxy'] = $proxy;
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:26:30 +03:00
|
|
|
$this->client = new Client($settings);
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->client->setThrowExceptions(true);
|
2013-12-17 12:38:43 +04:00
|
|
|
if ($this->secure === true && $this->certPath) {
|
2015-11-20 15:35:23 +03:00
|
|
|
$this->client->addCurlSetting(CURLOPT_CAINFO, $this->certPath);
|
2012-07-09 12:19:19 +04:00
|
|
|
}
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 20:16:01 +03:00
|
|
|
/**
|
|
|
|
* Clear the stat cache
|
|
|
|
*/
|
|
|
|
public function clearStatCache() {
|
|
|
|
$this->statCache->clear();
|
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-03-04 18:44:58 +04:00
|
|
|
public function getId() {
|
2012-10-12 01:06:57 +04:00
|
|
|
return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-10-13 19:15:58 +04:00
|
|
|
public function createBaseUri() {
|
2014-03-04 18:44:58 +04:00
|
|
|
$baseUri = 'http';
|
2012-11-30 19:27:11 +04:00
|
|
|
if ($this->secure) {
|
2014-03-04 18:44:58 +04:00
|
|
|
$baseUri .= 's';
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
$baseUri .= '://' . $this->host . $this->root;
|
2012-03-23 21:54:38 +04:00
|
|
|
return $baseUri;
|
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function mkdir($path) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2014-03-04 18:44:58 +04:00
|
|
|
$path = $this->cleanPath($path);
|
2015-02-11 20:16:01 +03:00
|
|
|
$result = $this->simpleResponse('MKCOL', $path, null, 201);
|
|
|
|
if ($result) {
|
|
|
|
$this->statCache->set($path, true);
|
|
|
|
}
|
|
|
|
return $result;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function rmdir($path) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2015-02-11 20:16:01 +03:00
|
|
|
$path = $this->cleanPath($path);
|
2014-01-31 19:06:11 +04:00
|
|
|
// FIXME: some WebDAV impl return 403 when trying to DELETE
|
|
|
|
// a non-empty folder
|
2015-02-11 20:16:01 +03:00
|
|
|
$result = $this->simpleResponse('DELETE', $path . '/', null, 204);
|
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->remove($path);
|
|
|
|
return $result;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function opendir($path) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2014-03-04 18:44:58 +04:00
|
|
|
$path = $this->cleanPath($path);
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2016-10-10 13:29:06 +03:00
|
|
|
$response = $this->client->propFind(
|
2015-02-11 20:16:01 +03:00
|
|
|
$this->encodePath($path),
|
2018-06-21 20:01:27 +03:00
|
|
|
['{DAV:}getetag'],
|
2015-02-11 20:16:01 +03:00
|
|
|
1
|
|
|
|
);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$content = [];
|
2014-03-04 18:44:58 +04:00
|
|
|
$files = array_keys($response);
|
|
|
|
array_shift($files); //the first entry is the current directory
|
2015-02-11 20:16:01 +03:00
|
|
|
|
|
|
|
if (!$this->statCache->hasKey($path)) {
|
|
|
|
$this->statCache->set($path, true);
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
foreach ($files as $file) {
|
2015-02-11 20:16:01 +03: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 18:44:58 +04:00
|
|
|
$content[] = $file;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-03-10 18:30:13 +03:00
|
|
|
return IteratorDirectory::wrap($content);
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 20:16:01 +03: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
|
2015-09-01 19:01:27 +03:00
|
|
|
*
|
2016-10-10 13:29:06 +03:00
|
|
|
* @return array|boolean propfind response or false if the entry was not found
|
2015-02-11 20:16:01 +03:00
|
|
|
*
|
2016-10-10 13:29:06 +03:00
|
|
|
* @throws ClientHttpException
|
2015-02-11 20:16:01 +03:00
|
|
|
*/
|
2016-04-13 18:31:47 +03:00
|
|
|
protected function propfind($path) {
|
2015-02-11 20:16:01 +03:00
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$cachedResponse = $this->statCache->get($path);
|
|
|
|
// we either don't know it, or we know it exists but need more details
|
|
|
|
if (is_null($cachedResponse) || $cachedResponse === true) {
|
|
|
|
$this->init();
|
|
|
|
try {
|
2016-10-10 13:29:06 +03:00
|
|
|
$response = $this->client->propFind(
|
2015-02-11 20:16:01 +03:00
|
|
|
$this->encodePath($path),
|
|
|
|
array(
|
|
|
|
'{DAV:}getlastmodified',
|
|
|
|
'{DAV:}getcontentlength',
|
|
|
|
'{DAV:}getcontenttype',
|
|
|
|
'{http://owncloud.org/ns}permissions',
|
2016-04-13 18:31:47 +03:00
|
|
|
'{http://open-collaboration-services.org/ns}share-permissions',
|
2015-02-11 20:16:01 +03:00
|
|
|
'{DAV:}resourcetype',
|
|
|
|
'{DAV:}getetag',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->statCache->set($path, $response);
|
2016-10-10 13:29:06 +03:00
|
|
|
} catch (ClientHttpException $e) {
|
2019-08-20 13:54:58 +03:00
|
|
|
if ($e->getHttpStatus() === 404 || $e->getHttpStatus() === 405) {
|
2016-10-10 13:29:06 +03:00
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->set($path, false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->convertException($e, $path);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->convertException($e, $path);
|
2015-02-11 20:16:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$response = $cachedResponse;
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function filetype($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03:00
|
|
|
$response = $this->propfind($path);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$responseType = [];
|
2014-01-31 19:06:11 +04:00
|
|
|
if (isset($response["{DAV:}resourcetype"])) {
|
2015-11-20 15:35:23 +03:00
|
|
|
/** @var ResourceType[] $response */
|
|
|
|
$responseType = $response["{DAV:}resourcetype"]->getValue();
|
2014-01-31 19:06:11 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
|
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function file_exists($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03: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
|
2016-10-10 13:29:06 +03:00
|
|
|
return ($this->propfind($path) !== false);
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function unlink($path) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2015-02-11 20:16:01 +03: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 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2013-02-09 20:35:47 +04:00
|
|
|
public function fopen($path, $mode) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2014-03-04 18:44:58 +04:00
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
switch ($mode) {
|
2012-03-23 21:54:38 +04:00
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2015-12-10 19:13:02 +03:00
|
|
|
try {
|
|
|
|
$response = $this->httpClientService
|
2017-09-28 15:22:42 +03:00
|
|
|
->newClient()
|
|
|
|
->get($this->createBaseUri() . $this->encodePath($path), [
|
|
|
|
'auth' => [$this->user, $this->password],
|
|
|
|
'stream' => true
|
|
|
|
]);
|
2018-02-09 15:09:03 +03:00
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
2016-03-13 00:45:07 +03:00
|
|
|
if ($e->getResponse() instanceof ResponseInterface
|
|
|
|
&& $e->getResponse()->getStatusCode() === 404) {
|
2015-12-10 19:13:02 +03:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
2012-10-11 23:13:19 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
|
2015-08-29 15:53:59 +03:00
|
|
|
if ($response->getStatusCode() !== Http::STATUS_OK) {
|
|
|
|
if ($response->getStatusCode() === Http::STATUS_LOCKED) {
|
2015-07-01 14:17:29 +03:00
|
|
|
throw new \OCP\Lock\LockedException($path);
|
2015-08-29 15:53:59 +03:00
|
|
|
} else {
|
2018-04-25 16:22:28 +03:00
|
|
|
Util::writeLog("webdav client", 'Guzzle get returned status code ' . $response->getStatusCode(), ILogger::ERROR);
|
2015-07-01 14:17:29 +03:00
|
|
|
}
|
2014-01-31 19:06:11 +04:00
|
|
|
}
|
2015-08-29 15:53:59 +03:00
|
|
|
|
|
|
|
return $response->getBody();
|
2012-03-23 21:54:38 +04:00
|
|
|
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
|
2015-07-02 17:31:10 +03:00
|
|
|
$tempManager = \OC::$server->getTempManager();
|
2014-03-04 18:44:58 +04:00
|
|
|
if (strrpos($path, '.') !== false) {
|
|
|
|
$ext = substr($path, strrpos($path, '.'));
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2014-03-04 18:44:58 +04:00
|
|
|
$ext = '';
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
if ($this->file_exists($path)) {
|
2014-06-20 18:09:38 +04:00
|
|
|
if (!$this->isUpdatable($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-02 17:31:10 +03:00
|
|
|
if ($mode === 'w' or $mode === 'w+') {
|
|
|
|
$tmpFile = $tempManager->getTemporaryFile($ext);
|
|
|
|
} else {
|
|
|
|
$tmpFile = $this->getCachedFile($path);
|
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
} else {
|
2014-06-20 18:09:38 +04:00
|
|
|
if (!$this->isCreatable(dirname($path))) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-02 17:31:10 +03:00
|
|
|
$tmpFile = $tempManager->getTemporaryFile($ext);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2017-01-03 19:26:44 +03:00
|
|
|
$handle = fopen($tmpFile, $mode);
|
|
|
|
return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
|
|
|
|
$this->writeBack($tmpFile, $path);
|
|
|
|
});
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/**
|
|
|
|
* @param string $tmpFile
|
|
|
|
*/
|
2017-01-03 19:26:44 +03:00
|
|
|
public function writeBack($tmpFile, $path) {
|
|
|
|
$this->uploadFile($tmpFile, $path);
|
|
|
|
unlink($tmpFile);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function free_space($path) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2014-03-04 18:44:58 +04:00
|
|
|
$path = $this->cleanPath($path);
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03:00
|
|
|
// TODO: cacheable ?
|
2016-10-10 13:29:06 +03:00
|
|
|
$response = $this->client->propfind($this->encodePath($path), ['{DAV:}quota-available-bytes']);
|
|
|
|
if ($response === false) {
|
|
|
|
return FileInfo::SPACE_UNKNOWN;
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
if (isset($response['{DAV:}quota-available-bytes'])) {
|
2012-03-23 21:54:38 +04:00
|
|
|
return (int)$response['{DAV:}quota-available-bytes'];
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2015-04-13 10:26:30 +03:00
|
|
|
return FileInfo::SPACE_UNKNOWN;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return FileInfo::SPACE_UNKNOWN;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-03-04 18:44:58 +04:00
|
|
|
public function touch($path, $mtime = null) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2012-11-30 19:27:11 +04:00
|
|
|
if (is_null($mtime)) {
|
2014-03-04 18:44:58 +04:00
|
|
|
$mtime = time();
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
$path = $this->cleanPath($path);
|
2013-07-12 15:25:37 +04:00
|
|
|
|
|
|
|
// if file exists, update the mtime, else create a new empty file
|
|
|
|
if ($this->file_exists($path)) {
|
2014-01-31 19:06:11 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03:00
|
|
|
$this->statCache->remove($path);
|
2016-04-01 22:16:29 +03:00
|
|
|
$this->client->proppatch($this->encodePath($path), ['{DAV:}lastmodified' => $mtime]);
|
|
|
|
// non-owncloud clients might not have accepted the property, need to recheck it
|
|
|
|
$response = $this->client->propfind($this->encodePath($path), ['{DAV:}getlastmodified'], 0);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-01 22:16:29 +03:00
|
|
|
if (isset($response['{DAV:}getlastmodified'])) {
|
|
|
|
$remoteMtime = strtotime($response['{DAV:}getlastmodified']);
|
|
|
|
if ($remoteMtime !== $mtime) {
|
|
|
|
// server has not accepted the mtime
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 14:29:01 +03:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 501) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2015-01-30 14:02:23 +03:00
|
|
|
return false;
|
2014-12-04 00:20:00 +03:00
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2014-01-31 19:06:11 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-12 15:25:37 +04:00
|
|
|
} else {
|
|
|
|
$this->file_put_contents($path, '');
|
|
|
|
}
|
2013-11-25 15:44:27 +04:00
|
|
|
return true;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 20:16:01 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $data
|
2015-04-13 10:26:30 +03:00
|
|
|
* @return int
|
2015-02-11 20:16:01 +03: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 13:48:55 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $target
|
|
|
|
*/
|
2014-03-04 18:44:58 +04:00
|
|
|
protected function uploadFile($path, $target) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2015-09-01 19:01:27 +03:00
|
|
|
|
2015-02-11 20:16:01 +03:00
|
|
|
// invalidate
|
|
|
|
$target = $this->cleanPath($target);
|
|
|
|
$this->statCache->remove($target);
|
2014-03-04 18:44:58 +04:00
|
|
|
$source = fopen($path, 'r');
|
2012-03-23 21:54:38 +04:00
|
|
|
|
2015-09-01 19:01:27 +03:00
|
|
|
$this->httpClientService
|
|
|
|
->newClient()
|
|
|
|
->put($this->createBaseUri() . $this->encodePath($target), [
|
|
|
|
'body' => $source,
|
|
|
|
'auth' => [$this->user, $this->password]
|
|
|
|
]);
|
|
|
|
|
2014-03-04 18:44:58 +04:00
|
|
|
$this->removeCachedFile($target);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2013-02-09 20:35:47 +04:00
|
|
|
public function rename($path1, $path2) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2015-02-11 20:16:01 +03:00
|
|
|
$path1 = $this->cleanPath($path1);
|
|
|
|
$path2 = $this->cleanPath($path2);
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-07-03 20:49:25 +03:00
|
|
|
// overwrite directory ?
|
|
|
|
if ($this->is_dir($path2)) {
|
|
|
|
// needs trailing slash in destination
|
|
|
|
$path2 = rtrim($path2, '/') . '/';
|
|
|
|
}
|
2015-02-11 20:16:01 +03:00
|
|
|
$this->client->request(
|
|
|
|
'MOVE',
|
|
|
|
$this->encodePath($path1),
|
|
|
|
null,
|
2015-07-03 20:49:25 +03:00
|
|
|
[
|
|
|
|
'Destination' => $this->createBaseUri() . $this->encodePath($path2),
|
|
|
|
]
|
2015-02-11 20:16:01 +03:00
|
|
|
);
|
|
|
|
$this->statCache->clear($path1 . '/');
|
|
|
|
$this->statCache->clear($path2 . '/');
|
|
|
|
$this->statCache->set($path1, false);
|
|
|
|
$this->statCache->set($path2, true);
|
2014-03-04 18:44:58 +04:00
|
|
|
$this->removeCachedFile($path1);
|
|
|
|
$this->removeCachedFile($path2);
|
2012-03-23 21:54:38 +04:00
|
|
|
return true;
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 12:55:58 +03:00
|
|
|
$this->convertException($e);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2013-02-09 20:35:47 +04:00
|
|
|
public function copy($path1, $path2) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2015-07-03 20:49:25 +03:00
|
|
|
$path1 = $this->cleanPath($path1);
|
|
|
|
$path2 = $this->cleanPath($path2);
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-07-03 20:49:25 +03:00
|
|
|
// overwrite directory ?
|
|
|
|
if ($this->is_dir($path2)) {
|
|
|
|
// needs trailing slash in destination
|
|
|
|
$path2 = rtrim($path2, '/') . '/';
|
|
|
|
}
|
|
|
|
$this->client->request(
|
|
|
|
'COPY',
|
|
|
|
$this->encodePath($path1),
|
|
|
|
null,
|
|
|
|
[
|
|
|
|
'Destination' => $this->createBaseUri() . $this->encodePath($path2),
|
|
|
|
]
|
|
|
|
);
|
2015-02-11 20:16:01 +03:00
|
|
|
$this->statCache->clear($path2 . '/');
|
|
|
|
$this->statCache->set($path2, true);
|
2014-03-04 18:44:58 +04:00
|
|
|
$this->removeCachedFile($path2);
|
2012-03-23 21:54:38 +04:00
|
|
|
return true;
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 12:55:58 +03:00
|
|
|
$this->convertException($e);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function stat($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03:00
|
|
|
$response = $this->propfind($path);
|
2017-01-24 18:56:24 +03:00
|
|
|
if (!$response) {
|
|
|
|
return false;
|
2016-10-10 13:29:06 +03:00
|
|
|
}
|
|
|
|
return [
|
2014-03-04 18:44:58 +04:00
|
|
|
'mtime' => strtotime($response['{DAV:}getlastmodified']),
|
|
|
|
'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0,
|
2016-10-10 13:29:06 +03:00
|
|
|
];
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return array();
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2012-09-07 17:22:01 +04:00
|
|
|
public function getMimeType($path) {
|
2017-09-28 15:22:42 +03:00
|
|
|
$remoteMimetype = $this->getMimeTypeFromRemote($path);
|
|
|
|
if ($remoteMimetype === 'application/octet-stream') {
|
|
|
|
return \OC::$server->getMimeTypeDetector()->detectPath($path);
|
|
|
|
} else {
|
|
|
|
return $remoteMimetype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMimeTypeFromRemote($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03:00
|
|
|
$response = $this->propfind($path);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$responseType = [];
|
2014-01-31 19:06:11 +04:00
|
|
|
if (isset($response["{DAV:}resourcetype"])) {
|
2015-11-20 15:35:23 +03:00
|
|
|
/** @var ResourceType[] $response */
|
|
|
|
$responseType = $response["{DAV:}resourcetype"]->getValue();
|
2014-01-31 19:06:11 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
$type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
|
|
|
|
if ($type == 'dir') {
|
2012-03-23 21:54:38 +04:00
|
|
|
return 'httpd/unix-directory';
|
2012-11-30 19:27:11 +04:00
|
|
|
} elseif (isset($response['{DAV:}getcontenttype'])) {
|
2012-03-23 21:54:38 +04:00
|
|
|
return $response['{DAV:}getcontenttype'];
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2017-09-28 15:22:42 +03:00
|
|
|
return 'application/octet-stream';
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2017-09-28 15:22:42 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2015-03-09 13:48:55 +03:00
|
|
|
* @return string
|
2014-02-19 12:31:54 +04:00
|
|
|
*/
|
2013-02-12 14:05:12 +04:00
|
|
|
public function cleanPath($path) {
|
2015-02-11 20:16:01 +03:00
|
|
|
if ($path === '') {
|
2014-05-16 14:30:08 +04:00
|
|
|
return $path;
|
|
|
|
}
|
2015-04-13 10:26:30 +03:00
|
|
|
$path = Filesystem::normalizePath($path);
|
2013-11-20 21:14:42 +04:00
|
|
|
// remove leading slash
|
|
|
|
return substr($path, 1);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
|
|
|
|
2014-01-31 19:06:11 +04:00
|
|
|
/**
|
|
|
|
* URL encodes the given path but keeps the slashes
|
2014-03-04 18:44:58 +04:00
|
|
|
*
|
2014-01-31 19:06:11 +04:00
|
|
|
* @param string $path to encode
|
|
|
|
* @return string encoded path
|
|
|
|
*/
|
2017-02-15 16:00:00 +03:00
|
|
|
protected function encodePath($path) {
|
2014-01-31 19:06:11 +04:00
|
|
|
// slashes need to stay
|
|
|
|
return str_replace('%2F', '/', rawurlencode($path));
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param string $path
|
2015-03-09 13:48:55 +03:00
|
|
|
* @param string|resource|null $body
|
|
|
|
* @param int $expected
|
|
|
|
* @return bool
|
|
|
|
* @throws StorageInvalidException
|
|
|
|
* @throws StorageNotAvailableException
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2017-02-15 16:00:00 +03:00
|
|
|
protected function simpleResponse($method, $path, $body, $expected) {
|
2014-03-04 18:44:58 +04:00
|
|
|
$path = $this->cleanPath($path);
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2014-03-04 18:44:58 +04:00
|
|
|
$response = $this->client->request($method, $this->encodePath($path), $body);
|
|
|
|
return $response['statusCode'] == $expected;
|
2015-02-12 14:29:01 +03:00
|
|
|
} catch (ClientHttpException $e) {
|
|
|
|
if ($e->getHttpStatus() === 404 && $method === 'DELETE') {
|
2015-02-11 20:16:01 +03:00
|
|
|
$this->statCache->clear($path . '/');
|
|
|
|
$this->statCache->set($path, false);
|
2015-01-30 14:02:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2014-03-04 18:44:58 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|
2013-08-02 17:44:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check if curl is installed
|
|
|
|
*/
|
|
|
|
public static function checkDependencies() {
|
2015-03-12 23:43:41 +03:00
|
|
|
return true;
|
2013-08-02 17:44:56 +04:00
|
|
|
}
|
2014-06-04 20:19:52 +04:00
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 17:28:24 +04:00
|
|
|
public function isUpdatable($path) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE);
|
2014-06-13 17:28:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 17:28:24 +04:00
|
|
|
public function isCreatable($path) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE);
|
2014-06-13 17:28:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 17:28:24 +04:00
|
|
|
public function isSharable($path) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE);
|
2014-06-13 17:28:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-06-13 17:28:24 +04:00
|
|
|
public function isDeletable($path) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE);
|
2014-06-13 17:28:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-09 13:48:55 +03:00
|
|
|
/** {@inheritdoc} */
|
2014-06-04 20:19:52 +04:00
|
|
|
public function getPermissions($path) {
|
|
|
|
$this->init();
|
2014-11-11 17:20:36 +03:00
|
|
|
$path = $this->cleanPath($path);
|
2015-04-27 18:41:02 +03:00
|
|
|
$response = $this->propfind($path);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-04 20:19:52 +04:00
|
|
|
if (isset($response['{http://owncloud.org/ns}permissions'])) {
|
2014-06-27 19:27:47 +04:00
|
|
|
return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']);
|
2014-06-13 17:28:24 +04:00
|
|
|
} else if ($this->is_dir($path)) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return Constants::PERMISSION_ALL;
|
2014-06-13 17:28:24 +04:00
|
|
|
} else if ($this->file_exists($path)) {
|
2015-04-13 10:26:30 +03:00
|
|
|
return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
|
2014-06-04 20:19:52 +04:00
|
|
|
} else {
|
2014-06-13 17:28:24 +04:00
|
|
|
return 0;
|
2014-06-04 20:19:52 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 19:10:46 +04:00
|
|
|
|
2015-04-27 18:41:02 +03:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getETag($path) {
|
|
|
|
$this->init();
|
|
|
|
$path = $this->cleanPath($path);
|
|
|
|
$response = $this->propfind($path);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-27 18:41:02 +03:00
|
|
|
if (isset($response['{DAV:}getetag'])) {
|
2019-03-14 16:46:39 +03:00
|
|
|
$etag = trim($response['{DAV:}getetag'], '"');
|
|
|
|
if (strlen($etag) > 40) {
|
|
|
|
$etag = md5($etag);
|
|
|
|
}
|
|
|
|
return $etag;
|
2015-04-27 18:41:02 +03:00
|
|
|
}
|
|
|
|
return parent::getEtag($path);
|
|
|
|
}
|
|
|
|
|
2014-06-27 19:27:47 +04:00
|
|
|
/**
|
|
|
|
* @param string $permissionsString
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function parsePermissions($permissionsString) {
|
2015-04-13 10:26:30 +03:00
|
|
|
$permissions = Constants::PERMISSION_READ;
|
2014-06-27 19:27:47 +04:00
|
|
|
if (strpos($permissionsString, 'R') !== false) {
|
2015-04-13 10:26:30 +03:00
|
|
|
$permissions |= Constants::PERMISSION_SHARE;
|
2014-06-27 19:27:47 +04:00
|
|
|
}
|
|
|
|
if (strpos($permissionsString, 'D') !== false) {
|
2015-04-13 10:26:30 +03:00
|
|
|
$permissions |= Constants::PERMISSION_DELETE;
|
2014-06-27 19:27:47 +04:00
|
|
|
}
|
|
|
|
if (strpos($permissionsString, 'W') !== false) {
|
2015-04-13 10:26:30 +03:00
|
|
|
$permissions |= Constants::PERMISSION_UPDATE;
|
2014-06-27 19:27:47 +04:00
|
|
|
}
|
2014-06-27 19:28:56 +04:00
|
|
|
if (strpos($permissionsString, 'CK') !== false) {
|
2015-04-13 10:26:30 +03:00
|
|
|
$permissions |= Constants::PERMISSION_CREATE;
|
|
|
|
$permissions |= Constants::PERMISSION_UPDATE;
|
2014-06-27 19:27:47 +04:00
|
|
|
}
|
|
|
|
return $permissions;
|
|
|
|
}
|
|
|
|
|
2014-06-27 19:10:46 +04:00
|
|
|
/**
|
|
|
|
* check if a file or folder has been updated since $time
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param int $time
|
2014-06-30 17:46:37 +04:00
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2014-06-27 19:10:46 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasUpdated($path, $time) {
|
|
|
|
$this->init();
|
2014-11-11 17:20:36 +03:00
|
|
|
$path = $this->cleanPath($path);
|
2014-06-30 17:46:37 +04:00
|
|
|
try {
|
2015-02-11 20:16:01 +03:00
|
|
|
// force refresh for $path
|
|
|
|
$this->statCache->remove($path);
|
|
|
|
$response = $this->propfind($path);
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($response === false) {
|
|
|
|
if ($path === '') {
|
|
|
|
// if root is gone it means the storage is not available
|
2018-04-23 12:30:22 +03:00
|
|
|
throw new StorageNotAvailableException('root is gone');
|
2016-10-10 13:29:06 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-30 17:46:37 +04:00
|
|
|
if (isset($response['{DAV:}getetag'])) {
|
|
|
|
$cachedData = $this->getCache()->get($path);
|
2015-04-27 18:41:02 +03:00
|
|
|
$etag = null;
|
|
|
|
if (isset($response['{DAV:}getetag'])) {
|
|
|
|
$etag = trim($response['{DAV:}getetag'], '"');
|
|
|
|
}
|
|
|
|
if (!empty($etag) && $cachedData['etag'] !== $etag) {
|
2014-06-30 17:46:37 +04:00
|
|
|
return true;
|
2016-04-15 11:52:53 +03:00
|
|
|
} else if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) {
|
|
|
|
$sharePermissions = (int)$response['{http://open-collaboration-services.org/ns}share-permissions'];
|
|
|
|
return $sharePermissions !== $cachedData['permissions'];
|
2014-06-30 17:46:37 +04:00
|
|
|
} 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 19:27:47 +04:00
|
|
|
} else {
|
2014-06-30 17:46:37 +04:00
|
|
|
$remoteMtime = strtotime($response['{DAV:}getlastmodified']);
|
|
|
|
return $remoteMtime > $time;
|
2014-06-27 19:27:47 +04:00
|
|
|
}
|
2015-04-10 12:55:58 +03:00
|
|
|
} catch (ClientHttpException $e) {
|
2016-10-10 13:29:06 +03:00
|
|
|
if ($e->getHttpStatus() === 405) {
|
2015-07-13 19:28:53 +03:00
|
|
|
if ($path === '') {
|
|
|
|
// if root is gone it means the storage is not available
|
2015-09-01 19:01:27 +03:00
|
|
|
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
|
2015-07-13 19:28:53 +03:00
|
|
|
}
|
2015-02-12 14:29:01 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2015-04-10 12:55:58 +03:00
|
|
|
return false;
|
|
|
|
} catch (\Exception $e) {
|
2015-07-01 14:17:29 +03:00
|
|
|
$this->convertException($e, $path);
|
2015-01-30 14:02:23 +03:00
|
|
|
return false;
|
2014-06-27 19:10:46 +04:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 21:45:32 +03:00
|
|
|
|
|
|
|
/**
|
2015-04-10 12:55:58 +03: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-07-01 14:17:29 +03:00
|
|
|
* @param string $path optional path from the operation
|
2015-01-20 21:45:32 +03: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
|
2019-11-05 21:19:16 +03:00
|
|
|
* @throws ForbiddenException if the action is not allowed
|
2015-01-20 21:45:32 +03:00
|
|
|
*/
|
2017-02-15 16:00:00 +03:00
|
|
|
protected function convertException(Exception $e, $path = '') {
|
2018-08-06 17:28:42 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, ['app' => 'files_external', 'level' => ILogger::DEBUG]);
|
2015-04-10 12:55:58 +03:00
|
|
|
if ($e instanceof ClientHttpException) {
|
2016-06-27 22:34:28 +03:00
|
|
|
if ($e->getHttpStatus() === Http::STATUS_LOCKED) {
|
2015-07-01 14:17:29 +03:00
|
|
|
throw new \OCP\Lock\LockedException($path);
|
|
|
|
}
|
2016-06-27 22:34:28 +03:00
|
|
|
if ($e->getHttpStatus() === Http::STATUS_UNAUTHORIZED) {
|
2015-04-10 12:55:58 +03:00
|
|
|
// either password was changed or was invalid all along
|
2015-09-01 19:01:27 +03:00
|
|
|
throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage());
|
2016-06-27 22:34:28 +03:00
|
|
|
} else if ($e->getHttpStatus() === Http::STATUS_METHOD_NOT_ALLOWED) {
|
2015-04-10 12:55:58 +03:00
|
|
|
// ignore exception for MethodNotAllowed, false will be returned
|
|
|
|
return;
|
2019-11-05 21:19:16 +03:00
|
|
|
} else if ($e->getHttpStatus() === Http::STATUS_FORBIDDEN){
|
|
|
|
// The operation is forbidden. Fail somewhat gracefully
|
|
|
|
throw new ForbiddenException(get_class($e) . ':' . $e->getMessage());
|
2015-04-10 12:55:58 +03:00
|
|
|
}
|
2015-09-01 19:01:27 +03:00
|
|
|
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
|
2015-04-10 12:55:58 +03:00
|
|
|
} else if ($e instanceof ClientException) {
|
|
|
|
// connection timeout or refused, server could be temporarily down
|
2015-09-01 19:01:27 +03:00
|
|
|
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
|
2015-04-10 12:55:58 +03:00
|
|
|
} else if ($e instanceof \InvalidArgumentException) {
|
|
|
|
// parse error because the server returned HTML instead of XML,
|
|
|
|
// possibly temporarily down
|
2015-09-01 19:01:27 +03:00
|
|
|
throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
|
2015-04-10 12:55:58 +03:00
|
|
|
} else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) {
|
|
|
|
// rethrow
|
|
|
|
throw $e;
|
2015-01-20 21:45:32 +03:00
|
|
|
}
|
|
|
|
|
2015-04-10 12:55:58 +03:00
|
|
|
// TODO: only log for now, but in the future need to wrap/rethrow exception
|
2015-01-20 21:45:32 +03:00
|
|
|
}
|
2012-03-23 21:54:38 +04:00
|
|
|
}
|