diff --git a/lib/private/filechunking.php b/lib/private/filechunking.php index 990499e40b49..af4b0a459309 100644 --- a/lib/private/filechunking.php +++ b/lib/private/filechunking.php @@ -168,8 +168,7 @@ public function file_assemble($path) { $absolutePath = \OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getView()->getAbsolutePath($path)); $data = ''; // use file_put_contents as method because that best matches what this function does - if (OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data) - && \OC\Files\Filesystem::isValidPath($path)) { + if (\OC\Files\Filesystem::isValidPath($path)) { $path = \OC\Files\Filesystem::getView()->getRelativePath($absolutePath); $exists = \OC\Files\Filesystem::file_exists($path); $run = true; @@ -210,7 +209,6 @@ public function file_assemble($path) { \OC\Files\Filesystem::signal_post_write, array( \OC\Files\Filesystem::signal_param_path => $path) ); - OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count); return $count > 0; }else{ return false; diff --git a/lib/private/fileproxy.php b/lib/private/fileproxy.php index 2835e9746427..7d21402c6ff1 100644 --- a/lib/private/fileproxy.php +++ b/lib/private/fileproxy.php @@ -42,6 +42,7 @@ class OC_FileProxy{ private static $proxies=array(); + private static $wrapperSetup = false; public static $enabled=true; /** @@ -64,6 +65,19 @@ public function __call($function, $arguments) { * @param OC_FileProxy $proxy */ public static function register($proxy) { + if (!self::$wrapperSetup){ + self::$wrapperSetup = true; + \OC\Files\Filesystem::addStorageWrapper('proxy', function($mountPoint, \OC\Files\Storage\Storage $storage) { + if($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Proxy')) { + return $storage; + } else { + return new \OC\Files\Storage\Wrapper\Proxy(array( + 'storage' => $storage, + 'mountpoint' => $mountPoint + )); + } + }); + } self::$proxies[]=$proxy; } diff --git a/lib/private/files/storage/wrapper/proxy.php b/lib/private/files/storage/wrapper/proxy.php new file mode 100644 index 000000000000..15eaabb2a692 --- /dev/null +++ b/lib/private/files/storage/wrapper/proxy.php @@ -0,0 +1,395 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Storage\Wrapper; + +/** + * Emulate filesystem proxies as storage wrapper + */ +class Proxy extends Wrapper { + private $mountPoint; + + public function __construct($arguments) { + parent::__construct($arguments); + $this->mountPoint = $arguments['mountpoint']; + } + + private function runPreProxies($operation, $path, &$data = null) { + $absPath = $this->mountPoint . $path; + \OC_FileProxy::runPreProxies($operation, $absPath, $data); + } + + private function runPostProxies($operation, $path, $result) { + $absPath = $this->mountPoint . $path; + return \OC_FileProxy::runPostProxies($operation, $absPath, $result); + } + + /** + * see http://php.net/manual/en/function.mkdir.php + * + * @param string $path + * @return bool + */ + public function mkdir($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->mkdir($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.rmdir.php + * + * @param string $path + * @return bool + */ + public function rmdir($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->rmdir($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.opendir.php + * + * @param string $path + * @return resource + */ + public function opendir($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->opendir($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.is_dir.php + * + * @param string $path + * @return bool + */ + public function is_dir($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->is_dir($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.is_file.php + * + * @param string $path + * @return bool + */ + public function is_file($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->is_file($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.stat.php + * only the following keys are required in the result: size and mtime + * + * @param string $path + * @return array + */ + public function stat($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->stat($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.filetype.php + * + * @param string $path + * @return bool + */ + public function filetype($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->filetype($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.filesize.php + * The result for filesize when called on a folder is required to be 0 + * + * @param string $path + * @return int + */ + public function filesize($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->filesize($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * check if a file can be created in $path + * + * @param string $path + * @return bool + */ + public function isCreatable($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->isCreatable($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * check if a file can be read + * + * @param string $path + * @return bool + */ + public function isReadable($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->isReadable($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * check if a file can be written to + * + * @param string $path + * @return bool + */ + public function isUpdatable($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->isUpdatable($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * check if a file can be deleted + * + * @param string $path + * @return bool + */ + public function isDeletable($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->isDeletable($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * check if a file can be shared + * + * @param string $path + * @return bool + */ + public function isSharable($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->isSharable($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * get the full permissions of a path. + * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php + * + * @param string $path + * @return int + */ + public function getPermissions($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->getPermissions($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.file_exists.php + * + * @param string $path + * @return bool + */ + public function file_exists($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->file_exists($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.filemtime.php + * + * @param string $path + * @return int + */ + public function filemtime($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->filemtime($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.file_get_contents.php + * + * @param string $path + * @return string + */ + public function file_get_contents($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->file_get_contents($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.file_put_contents.php + * + * @param string $path + * @param string $data + * @return bool + */ + public function file_put_contents($path, $data) { + $this->runPreProxies(__FUNCTION__, $path, $data); + $result = $this->storage->file_put_contents($path, $data); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.unlink.php + * + * @param string $path + * @return bool + */ + public function unlink($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->unlink($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.rename.php + * + * @param string $path1 + * @param string $path2 + * @return bool + */ + public function rename($path1, $path2) { + $this->runPreProxies(__FUNCTION__, $path1, $path2); + $result = $this->storage->rename($path1, $path2); + return $this->runPostProxies(__FUNCTION__, $path1, $result); + } + + /** + * see http://php.net/manual/en/function.copy.php + * + * @param string $path1 + * @param string $path2 + * @return bool + */ + public function copy($path1, $path2) { + $this->runPreProxies(__FUNCTION__, $path1, $path2); + $result = $this->storage->copy($path1, $path2); + return $this->runPostProxies(__FUNCTION__, $path1, $result); + } + + /** + * see http://php.net/manual/en/function.fopen.php + * + * @param string $path + * @param string $mode + * @return resource + */ + public function fopen($path, $mode) { + $this->runPreProxies(__FUNCTION__, $path, $mode); + $result = $this->storage->fopen($path, $mode); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * get the mimetype for a file or folder + * The mimetype for a folder is required to be "httpd/unix-directory" + * + * @param string $path + * @return string + */ + public function getMimeType($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->getMimeType($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.free_space.php + * + * @param string $path + * @return int + */ + public function free_space($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->free_space($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * see http://php.net/manual/en/function.touch.php + * If the backend does not support the operation, false should be returned + * + * @param string $path + * @param int $mtime + * @return bool + */ + public function touch($path, $mtime = null) { + $this->runPreProxies(__FUNCTION__, $path, $mtime); + $result = $this->storage->touch($path, $mtime); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * get the path to a local version of the file. + * The local version of the file can be temporary and doesn't have to be persistent across requests + * + * @param string $path + * @return string + */ + public function getLocalFile($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->getLocalFile($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * get the path to a local version of the folder. + * The local version of the folder can be temporary and doesn't have to be persistent across requests + * + * @param string $path + * @return string + */ + public function getLocalFolder($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->getLocalFolder($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * check if a file or folder has been updated since $time + * + * @param string $path + * @param int $time + * @return bool + * + * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed. + * returning true for other changes in the folder is optional + */ + public function hasUpdated($path, $time) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->hasUpdated($path, $time); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } + + /** + * get the ETag for a file or folder + * + * @param string $path + * @return string + */ + public function getETag($path) { + $this->runPreProxies(__FUNCTION__, $path); + $result = $this->storage->getETag($path); + return $this->runPostProxies(__FUNCTION__, $path, $result); + } +} diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 4b3d167f8e9d..57392a81e0e2 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -364,12 +364,9 @@ protected function emit_file_hooks_post($exists, $path) { public function file_put_contents($path, $data) { if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); - if (\OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data) - and Filesystem::isValidPath($path) + if (Filesystem::isValidPath($path) and !Filesystem::isFileBlacklisted($path) ) { - $path = $this->getRelativePath($absolutePath); $exists = $this->file_exists($path); $run = true; if ($this->shouldEmitHooks($path)) { @@ -387,7 +384,6 @@ public function file_put_contents($path, $data) { if ($this->shouldEmitHooks($path) && $result !== false) { $this->emit_file_hooks_post($exists, $path); } - \OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count); return $result; } else { return false; @@ -428,13 +424,10 @@ public function rename($path1, $path2) { $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); if ( - \OC_FileProxy::runPreProxies('rename', $absolutePath1, $absolutePath2) - and Filesystem::isValidPath($path2) + Filesystem::isValidPath($path2) and Filesystem::isValidPath($path1) and !Filesystem::isFileBlacklisted($path2) ) { - $path1 = $this->getRelativePath($absolutePath1); - $path2 = $this->getRelativePath($absolutePath2); $exists = $this->file_exists($path2); if ($path1 == null or $path2 == null) { @@ -470,14 +463,12 @@ public function rename($path1, $path2) { $sourceMountPoint = $mount->getMountPoint(); $result = $mount->moveMount($absolutePath2); $manager->moveMount($sourceMountPoint, $mount->getMountPoint()); - \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2); } else { $result = false; } } elseif ($mp1 == $mp2) { if ($storage1) { $result = $storage1->rename($internalPath1, $internalPath2); - \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2); } else { $result = false; } @@ -534,14 +525,10 @@ public function copy($path1, $path2) { $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); if ( - \OC_FileProxy::runPreProxies('copy', $absolutePath1, $absolutePath2) - and Filesystem::isValidPath($path2) + Filesystem::isValidPath($path2) and Filesystem::isValidPath($path1) and !Filesystem::isFileBlacklisted($path2) ) { - $path1 = $this->getRelativePath($absolutePath1); - $path2 = $this->getRelativePath($absolutePath2); - if ($path1 == null or $path2 == null) { return false; } @@ -703,8 +690,7 @@ public function getMimeType($path) { public function hash($type, $path, $raw = false) { $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); - if (\OC_FileProxy::runPreProxies('hash', $absolutePath) && Filesystem::isValidPath($path)) { - $path = $this->getRelativePath($absolutePath); + if (Filesystem::isValidPath($path)) { if ($path == null) { return false; } @@ -717,9 +703,7 @@ public function hash($type, $path, $raw = false) { } list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); if ($storage) { - $result = $storage->hash($type, $internalPath, $raw); - $result = \OC_FileProxy::runPostProxies('hash', $absolutePath, $result); - return $result; + return $storage->hash($type, $internalPath, $raw); } } return null; @@ -746,12 +730,10 @@ public function free_space($path = '/') { private function basicOperation($operation, $path, $hooks = array(), $extraParam = null) { $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); - if (\OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam) - and Filesystem::isValidPath($path) + if (Filesystem::isValidPath($path) and !Filesystem::isFileBlacklisted($path) ) { - $path = $this->getRelativePath($absolutePath); - if ($path == null) { + if ($path === null) { return false; } @@ -764,8 +746,6 @@ private function basicOperation($operation, $path, $hooks = array(), $extraParam $result = $storage->$operation($internalPath); } - $result = \OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result); - if (in_array('delete', $hooks)) { $this->updater->remove($path); } @@ -936,6 +916,8 @@ public function getFileInfo($path, $includeMountPoints = true) { $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; } + // TODO remove when this is no longer needed in the encryption app + // this cant be moved the the wrapper since it's not a storage level operation $data = \OC_FileProxy::runPostProxies('getFileInfo', $path, $data); return new FileInfo($path, $storage, $internalPath, $data);