2013-10-23 14:03:57 +00:00
|
|
|
<?php
|
2015-02-26 10:37:37 +00:00
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-02-26 10:37:37 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @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/>
|
2015-02-26 10:37:37 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
namespace OC\Connector\Sabre;
|
|
|
|
|
2015-02-25 10:45:44 +00:00
|
|
|
use Sabre\DAV\IFile;
|
2015-02-12 11:29:01 +00:00
|
|
|
use \Sabre\DAV\PropFind;
|
|
|
|
use \Sabre\DAV\PropPatch;
|
|
|
|
use \Sabre\HTTP\RequestInterface;
|
|
|
|
use \Sabre\HTTP\ResponseInterface;
|
|
|
|
|
|
|
|
class FilesPlugin extends \Sabre\DAV\ServerPlugin {
|
2013-10-23 14:03:57 +00:00
|
|
|
|
|
|
|
// namespace
|
2013-10-24 12:40:43 +00:00
|
|
|
const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
2015-02-12 11:29:01 +00:00
|
|
|
const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
|
|
|
|
const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
|
|
|
|
const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
|
|
|
|
const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
|
|
|
|
const GETETAG_PROPERTYNAME = '{DAV:}getetag';
|
|
|
|
const GETLASTMODIFIED_PROPERTYNAME = '{DAV:}getlastmodified';
|
2013-10-23 14:03:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to main server object
|
|
|
|
*
|
2014-01-09 13:25:48 +00:00
|
|
|
* @var \Sabre\DAV\Server
|
2013-10-23 14:03:57 +00:00
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Tree
|
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
2015-02-25 10:45:44 +00:00
|
|
|
/**
|
|
|
|
* @param \Sabre\DAV\Tree $tree
|
|
|
|
*/
|
2015-02-12 11:29:01 +00:00
|
|
|
public function __construct(\Sabre\DAV\Tree $tree) {
|
|
|
|
$this->tree = $tree;
|
|
|
|
}
|
|
|
|
|
2013-10-23 14:03:57 +00:00
|
|
|
/**
|
|
|
|
* This initializes the plugin.
|
|
|
|
*
|
2014-01-09 13:25:48 +00:00
|
|
|
* This function is called by \Sabre\DAV\Server, after
|
2013-10-23 14:03:57 +00:00
|
|
|
* addPlugin is called.
|
|
|
|
*
|
|
|
|
* This method should set up the required event subscriptions.
|
|
|
|
*
|
2014-01-09 13:25:48 +00:00
|
|
|
* @param \Sabre\DAV\Server $server
|
2013-10-23 14:03:57 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2014-01-09 13:25:48 +00:00
|
|
|
public function initialize(\Sabre\DAV\Server $server) {
|
2013-10-23 14:03:57 +00:00
|
|
|
|
|
|
|
$server->xmlNamespaces[self::NS_OWNCLOUD] = 'oc';
|
2015-02-12 11:29:01 +00:00
|
|
|
$server->protectedProperties[] = self::FILEID_PROPERTYNAME;
|
|
|
|
$server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
|
|
|
|
$server->protectedProperties[] = self::SIZE_PROPERTYNAME;
|
|
|
|
$server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
|
|
|
|
|
|
|
|
// normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
|
|
|
|
$allowedProperties = ['{DAV:}getetag', '{DAV:}getlastmodified'];
|
|
|
|
$server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
|
2013-10-23 14:03:57 +00:00
|
|
|
|
|
|
|
$this->server = $server;
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->server->on('propFind', array($this, 'handleGetProperties'));
|
|
|
|
$this->server->on('propPatch', array($this, 'handleUpdateProperties'));
|
|
|
|
$this->server->on('afterBind', array($this, 'sendFileIdHeader'));
|
|
|
|
$this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
|
2015-02-25 10:45:44 +00:00
|
|
|
$this->server->on('afterMethod:GET', [$this,'httpGet']);
|
2015-05-21 15:52:13 +00:00
|
|
|
$this->server->on('afterResponse', function($request, ResponseInterface $response) {
|
|
|
|
$body = $response->getBody();
|
|
|
|
if (is_resource($body)) {
|
|
|
|
fclose($body);
|
|
|
|
}
|
|
|
|
});
|
2015-02-25 10:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin that adds a 'Content-Disposition: attachment' header to all files
|
|
|
|
* delivered by SabreDAV.
|
|
|
|
* @param RequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
*/
|
|
|
|
function httpGet(RequestInterface $request, ResponseInterface $response) {
|
|
|
|
// Only handle valid files
|
2015-03-26 19:45:39 +00:00
|
|
|
$node = $this->tree->getNodeForPath($request->getPath());
|
2015-02-25 10:45:44 +00:00
|
|
|
if (!($node instanceof IFile)) return;
|
|
|
|
|
|
|
|
$response->addHeader('Content-Disposition', 'attachment');
|
2013-10-23 14:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds all ownCloud-specific properties
|
|
|
|
*
|
2015-02-12 11:29:01 +00:00
|
|
|
* @param PropFind $propFind
|
2014-01-09 13:25:48 +00:00
|
|
|
* @param \Sabre\DAV\INode $node
|
2013-10-23 14:03:57 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2015-02-12 11:29:01 +00:00
|
|
|
public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
|
2013-10-23 14:03:57 +00:00
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
if ($node instanceof \OC\Connector\Sabre\Node) {
|
2013-10-23 14:03:57 +00:00
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
$propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
|
|
|
|
return $node->getFileId();
|
|
|
|
});
|
2013-10-23 14:03:57 +00:00
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
$propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
|
|
|
|
return $node->getDavPermissions();
|
|
|
|
});
|
2014-05-02 15:37:16 +00:00
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
$propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
|
|
|
|
return $node->getEtag();
|
|
|
|
});
|
2013-10-23 14:03:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
if ($node instanceof \OC\Connector\Sabre\File) {
|
|
|
|
$propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) {
|
|
|
|
/** @var $node \OC\Connector\Sabre\File */
|
|
|
|
$directDownloadUrl = $node->getDirectDownload();
|
|
|
|
if (isset($directDownloadUrl['url'])) {
|
|
|
|
return $directDownloadUrl['url'];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2014-12-15 14:20:24 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
if ($node instanceof \OC\Connector\Sabre\Directory) {
|
|
|
|
$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
|
|
|
|
return $node->getSize();
|
|
|
|
});
|
2014-08-29 09:39:02 +00:00
|
|
|
}
|
2013-10-23 14:03:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
/**
|
|
|
|
* Update ownCloud-specific properties
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param PropPatch $propPatch
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleUpdateProperties($path, PropPatch $propPatch) {
|
|
|
|
$propPatch->handle(self::GETLASTMODIFIED_PROPERTYNAME, function($time) use ($path) {
|
|
|
|
if (empty($time)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$node = $this->tree->getNodeForPath($path);
|
|
|
|
if (is_null($node)) {
|
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
$node->touch($time);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
$propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($path) {
|
|
|
|
if (empty($etag)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$node = $this->tree->getNodeForPath($path);
|
|
|
|
if (is_null($node)) {
|
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
if ($node->setEtag($etag) !== -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 11:20:59 +00:00
|
|
|
/**
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $filePath
|
2014-01-09 13:25:48 +00:00
|
|
|
* @param \Sabre\DAV\INode $node
|
|
|
|
* @throws \Sabre\DAV\Exception\BadRequest
|
2013-10-25 11:20:59 +00:00
|
|
|
*/
|
2014-01-09 13:25:48 +00:00
|
|
|
public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
|
2013-11-25 14:35:26 +00:00
|
|
|
// chunked upload handling
|
|
|
|
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
|
2015-02-12 11:29:01 +00:00
|
|
|
list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($filePath);
|
|
|
|
$info = \OC_FileChunking::decodeName($name);
|
2013-11-25 14:35:26 +00:00
|
|
|
if (!empty($info)) {
|
|
|
|
$filePath = $path . '/' . $info['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 15:14:08 +00:00
|
|
|
// we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
|
2013-11-25 14:35:26 +00:00
|
|
|
if (!$this->server->tree->nodeExists($filePath)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-20 15:14:08 +00:00
|
|
|
$node = $this->server->tree->getNodeForPath($filePath);
|
2015-02-12 11:29:01 +00:00
|
|
|
if ($node instanceof \OC\Connector\Sabre\Node) {
|
2013-10-25 11:20:59 +00:00
|
|
|
$fileId = $node->getFileId();
|
|
|
|
if (!is_null($fileId)) {
|
|
|
|
$this->server->httpResponse->setHeader('OC-FileId', $fileId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 14:03:57 +00:00
|
|
|
}
|