diff options
Diffstat (limited to 'apps/files_sharing/lib/external')
-rw-r--r-- | apps/files_sharing/lib/external/cache.php | 47 | ||||
-rw-r--r-- | apps/files_sharing/lib/external/manager.php | 140 | ||||
-rw-r--r-- | apps/files_sharing/lib/external/mount.php | 53 | ||||
-rw-r--r-- | apps/files_sharing/lib/external/scanner.php | 54 | ||||
-rw-r--r-- | apps/files_sharing/lib/external/storage.php | 103 |
5 files changed, 397 insertions, 0 deletions
diff --git a/apps/files_sharing/lib/external/cache.php b/apps/files_sharing/lib/external/cache.php new file mode 100644 index 00000000000..cd06bfb1272 --- /dev/null +++ b/apps/files_sharing/lib/external/cache.php @@ -0,0 +1,47 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_Sharing\External; + +class Cache extends \OC\Files\Cache\Cache { + private $remote; + private $remoteUser; + private $storage; + + /** + * @param \OCA\Files_Sharing\External\Storage $storage + * @param string $remote + * @param string $remoteUser + */ + public function __construct($storage, $remote, $remoteUser) { + $this->storage = $storage; + list(, $remote) = explode('://', $remote, 2); + $this->remote = $remote; + $this->remoteUser = $remoteUser; + parent::__construct($storage); + } + + public function get($file) { + $result = parent::get($file); + $result['displayname_owner'] = $this->remoteUser . '@' . $this->remote; + if (!$file || $file === '') { + $result['is_share_mount_point'] = true; + $mountPoint = rtrim($this->storage->getMountPoint()); + $result['name'] = basename($mountPoint); + } + return $result; + } + + public function getFolderContentsById($id) { + $results = parent::getFolderContentsById($id); + foreach ($results as &$file) { + $file['displayname_owner'] = $this->remoteUser . '@' . $this->remote; + } + return $results; + } +} diff --git a/apps/files_sharing/lib/external/manager.php b/apps/files_sharing/lib/external/manager.php new file mode 100644 index 00000000000..70a0e98ebd5 --- /dev/null +++ b/apps/files_sharing/lib/external/manager.php @@ -0,0 +1,140 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_Sharing\External; + +use OC\Files\Filesystem; + +class Manager { + const STORAGE = '\OCA\Files_Sharing\External\Storage'; + + /** + * @var \OCP\IDBConnection + */ + private $connection; + + /** + * @var \OC\Files\Mount\Manager + */ + private $mountManager; + + /** + * @var \OC\Files\Storage\Loader + */ + private $storageLoader; + + /** + * @var \OC\User\Session + */ + private $userSession; + + /** + * @param \OCP\IDBConnection $connection + * @param \OC\Files\Mount\Manager $mountManager + * @param \OC\User\Session $userSession + * @param \OC\Files\Storage\Loader $storageLoader + */ + public function __construct(\OCP\IDBConnection $connection, \OC\Files\Mount\Manager $mountManager, + \OC\Files\Storage\Loader $storageLoader, \OC\User\Session $userSession) { + $this->connection = $connection; + $this->mountManager = $mountManager; + $this->userSession = $userSession; + $this->storageLoader = $storageLoader; + } + + public function addShare($remote, $token, $password, $name, $owner) { + $user = $this->userSession->getUser(); + if ($user) { + $query = $this->connection->prepare('INSERT INTO *PREFIX*share_external(`remote`, `share_token`, `password`, + `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`) VALUES(?, ?, ?, ?, ?, ?, ?, ?)'); + $mountPoint = Filesystem::normalizePath('/' . $name); + $hash = md5($mountPoint); + $query->execute(array($remote, $token, $password, $name, $owner, $user->getUID(), $mountPoint, $hash)); + + $options = array( + 'remote' => $remote, + 'token' => $token, + 'password' => $password, + 'mountpoint' => $mountPoint, + 'owner' => $owner + ); + return $this->mountShare($options); + } + } + + public function setup() { + // don't setup server-to-server shares if the file_external app is disabled + // FIXME no longer needed if we use the webdav implementation from core + if (\OC_App::isEnabled('files_external') === false) { + return false; + } + + $user = $this->userSession->getUser(); + if ($user) { + $query = $this->connection->prepare('SELECT `remote`, `share_token`, `password`, `mountpoint`, `owner` + FROM *PREFIX*share_external WHERE `user` = ?'); + $query->execute(array($user->getUID())); + + while ($row = $query->fetch()) { + $row['manager'] = $this; + $row['token'] = $row['share_token']; + $this->mountShare($row); + } + } + } + + protected function stripPath($path) { + $prefix = '/' . $this->userSession->getUser()->getUID() . '/files'; + return rtrim(substr($path, strlen($prefix)), '/'); + } + + /** + * @param array $data + * @return Mount + */ + protected function mountShare($data) { + $mountPoint = '/' . $this->userSession->getUser()->getUID() . '/files' . $data['mountpoint']; + $mount = new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); + $this->mountManager->addMount($mount); + return $mount; + } + + /** + * @return \OC\Files\Mount\Manager + */ + public function getMountManager() { + return $this->mountManager; + } + + /** + * @param string $source + * @param string $target + * @return bool + */ + public function setMountPoint($source, $target) { + $user = $this->userSession->getUser(); + $source = $this->stripPath($source); + $target = $this->stripPath($target); + $sourceHash = md5($source); + $targetHash = md5($target); + + $query = $this->connection->prepare('UPDATE *PREFIX*share_external SET + `mountpoint` = ?, `mountpoint_hash` = ? WHERE `mountpoint_hash` = ? AND `user` = ?'); + $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $user->getUID())); + + return $result; + } + + public function removeShare($mountPoint) { + $user = $this->userSession->getUser(); + $mountPoint = $this->stripPath($mountPoint); + $hash = md5($mountPoint); + $query = $this->connection->prepare('DELETE FROM *PREFIX*share_external WHERE `mountpoint_hash` = ? AND `user` = ?'); + return (bool)$query->execute(array($hash, $user->getUID())); + } +} diff --git a/apps/files_sharing/lib/external/mount.php b/apps/files_sharing/lib/external/mount.php new file mode 100644 index 00000000000..a42a12f9b9a --- /dev/null +++ b/apps/files_sharing/lib/external/mount.php @@ -0,0 +1,53 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_Sharing\External; + +use OC\Files\Mount\MoveableMount; + +class Mount extends \OC\Files\Mount\Mount implements MoveableMount { + + /** + * @var \OCA\Files_Sharing\External\Manager + */ + protected $manager; + + /** + * @param string|\OC\Files\Storage\Storage $storage + * @param string $mountpoint + * @param array $options + * @param \OCA\Files_Sharing\External\Manager $manager + * @param \OC\Files\Storage\Loader $loader + */ + public function __construct($storage, $mountpoint, $options, $manager, $loader = null) { + parent::__construct($storage, $mountpoint, $options, $loader); + $this->manager = $manager; + } + + /** + * Move the mount point to $target + * + * @param string $target the target mount point + * @return bool + */ + public function moveMount($target) { + $result = $this->manager->setMountPoint($this->mountPoint, $target); + $this->setMountPoint($target); + return $result; + } + + /** + * Remove the mount points + * + * @return mixed + * @return bool + */ + public function removeMount() { + return $this->manager->removeShare($this->mountPoint); + } +} diff --git a/apps/files_sharing/lib/external/scanner.php b/apps/files_sharing/lib/external/scanner.php new file mode 100644 index 00000000000..8921dd1a4c0 --- /dev/null +++ b/apps/files_sharing/lib/external/scanner.php @@ -0,0 +1,54 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_Sharing\External; + +class Scanner extends \OC\Files\Cache\Scanner { + /** + * @var \OCA\Files_Sharing\External\Storage + */ + protected $storage; + + public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1) { + $this->scanAll(); + } + + public function scanAll() { + $remote = $this->storage->getRemote(); + $token = $this->storage->getToken(); + $password = $this->storage->getPassword(); + $url = $remote . '/index.php/apps/files_sharing/shareinfo?t=' . $token; + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, + http_build_query(array('password' => $password))); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + curl_close($ch); + + $data = json_decode($result, true); + if ($data['status'] === 'success') { + $this->addResult($data['data'], ''); + } else { + throw new \Exception('Error while scanning remote share'); + } + } + + private function addResult($data, $path) { + $this->cache->put($path, $data); + if (isset($data['children'])) { + foreach ($data['children'] as $child) { + $this->addResult($child, ltrim($path . '/' . $child['name'], '/')); + } + } + } +} diff --git a/apps/files_sharing/lib/external/storage.php b/apps/files_sharing/lib/external/storage.php new file mode 100644 index 00000000000..cd04841bb09 --- /dev/null +++ b/apps/files_sharing/lib/external/storage.php @@ -0,0 +1,103 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_Sharing\External; + +use OC\Files\Filesystem; +use OC\Files\Storage\DAV; +use OCA\Files_Sharing\ISharedStorage; + +class Storage extends DAV implements ISharedStorage { + /** + * @var string + */ + private $remoteUser; + + /** + * @var string + */ + private $remote; + + /** + * @var string + */ + private $mountPoint; + + /** + * @var string + */ + private $token; + + public function __construct($options) { + $this->remote = $options['remote']; + $this->remoteUser = $options['owner']; + list($protocol, $remote) = explode('://', $this->remote); + list($host, $root) = explode('/', $remote); + $secure = $protocol === 'https'; + $root .= '/public.php/webdav'; + $this->mountPoint = $options['mountpoint']; + $this->token = $options['token']; + parent::__construct(array( + 'secure' => $secure, + 'host' => $host, + 'root' => $root, + 'user' => $options['token'], + 'password' => $options['password'] + )); + } + + public function getRemoteUser() { + return $this->remoteUser; + } + + public function getRemote() { + return $this->remote; + } + + public function getMountPoint() { + return $this->mountPoint; + } + + public function getToken() { + return $this->token; + } + + public function getPassword() { + return $this->password; + } + + /** + * @brief get id of the mount point + * @return string + */ + public function getId() { + return 'shared::' . md5($this->token . '@' . $this->remote); + } + + public function getCache($path = '', $storage = null) { + if (!$storage) { + $this->cache = new Cache($this, $this->remote, $this->remoteUser); + } + return $this->cache; + } + + /** + * @param string $path + * @param \OC\Files\Storage\Storage $storage + * @return \OCA\Files_Sharing\External\Scanner + */ + public function getScanner($path = '', $storage = null) { + if (!$storage) { + $storage = $this; + } + if (!isset($this->scanner)) { + $this->scanner = new Scanner($storage); + } + return $this->scanner; + } +} |