From 71350bc61879ab2e532d5f55c058b4fba9341656 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Fri, 22 Jun 2012 17:43:04 -0400 Subject: I'm having trouble making up my mind... translateItem -> getItems() -> formatItems(). It may change again. Preparing shared storage for new api. --- lib/files.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/files.php') diff --git a/lib/files.php b/lib/files.php index 469c3a15b8e..2feae20afe6 100644 --- a/lib/files.php +++ b/lib/files.php @@ -34,6 +34,11 @@ class OC_Files { */ public static function getDirectoryContent($directory, $mimetype_filter = ''){ $files=OC_FileCache::getFolderContent($directory, false, $mimetype_filter); + if ($directory == '') { + $files = array_merge($files, array()); + } else if (substr($directory, 7) == '/Shared') { + $files = array_merge($files, OCP\Share::getItemsSharedWith('file', $directory, OC_Share_Backend_File::FORMAT_FILE_APP)); + } foreach($files as &$file){ $file['directory']=$directory; $file['type']=($file['mimetype']=='httpd/unix-directory')?'dir':'file'; -- cgit v1.2.3 From 7c908a0016db30c70271304c4d080383911645e3 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Tue, 10 Jul 2012 18:56:22 -0400 Subject: Sharing files working using share API --- apps/files_sharing/appinfo/app.php | 4 +- apps/files_sharing/lib/share/file.php | 94 ++++++++++++++++ apps/files_sharing/lib/share/folder.php | 64 +++++++++++ apps/files_sharing/sharedstorage.php | 193 +++++++------------------------- core/js/share.js | 18 +-- lib/filecache.php | 8 ++ lib/files.php | 34 ++++-- lib/public/share.php | 102 ++++++++--------- 8 files changed, 288 insertions(+), 229 deletions(-) create mode 100644 apps/files_sharing/lib/share/file.php create mode 100644 apps/files_sharing/lib/share/folder.php (limited to 'lib/files.php') diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php index a08544909ba..7495a5bbe6c 100644 --- a/apps/files_sharing/appinfo/app.php +++ b/apps/files_sharing/appinfo/app.php @@ -1,7 +1,8 @@ . +*/ + +class OC_Share_Backend_File extends OCP\Share_Backend { + + const FORMAT_SOURCE_PATH = 0; + const FORMAT_FILE_APP = 1; + const FORMAT_FILE_APP_ROOT = 2; + const FORMAT_OPENDIR = 3; + + public function getSource($item, $uid) { + if (OC_Filesystem::file_exists($item)) { + return array('item' => null, 'file' => $item); + } + return false; + } + + public function generateTarget($item, $uid, $exclude = null) { + // TODO Make sure target path doesn't exist already + return '/Shared'.$item; + } + + public function formatItems($items, $format, $parameters = null) { + if ($format == self::FORMAT_OPENDIR) { + $files = array(); + foreach ($items as $file) { + $files[] = basename($file['file_target']); + } + return $files; + } else { + $shares = array(); + $ids = array(); + foreach ($items as $item) { + $shares[$item['file_source']] = $item; + $ids[] = $item['file_source']; + } + $ids = "'".implode("','", $ids)."'"; + if ($format == self::FORMAT_SOURCE_PATH) { + $query = OCP\DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + $result = $query->execute()->fetchAll(); + if (isset($result[0]['path'])) { + return $result[0]['path']; + } + return false; + } else if ($format == self::FORMAT_FILE_APP) { + $query = OCP\DB::prepare('SELECT id, path, name, ctime, mtime, mimetype, size, encrypted, versioned, writable FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + $result = $query->execute(); + $files = array(); + while ($file = $result->fetchRow()) { + // Set target path + $file['path'] = $shares[$file['id']]['file_target']; + $file['name'] = basename($file['path']); + // TODO Set permissions: $file['writable'] + $files[] = $file; + } + return $files; + } else if ($format == self::FORMAT_FILE_APP_ROOT) { + $query = OCP\DB::prepare('SELECT id, path, name, ctime, mtime, mimetype, size, encrypted, versioned, writable FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + $result = $query->execute(); + $mtime = 0; + $size = 0; + while ($file = $result->fetchRow()) { + if ($file['mtime'] > $mtime) { + $mtime = $file['mtime']; + } + $size += $file['size']; + } + return array(0 => array('name' => 'Shared', 'mtime' => $mtime, 'mimetype' => 'httpd/unix-directory', 'size' => $size, 'writable' => false)); + } + } + return array(); + } + +} + +?> \ No newline at end of file diff --git a/apps/files_sharing/lib/share/folder.php b/apps/files_sharing/lib/share/folder.php new file mode 100644 index 00000000000..033e2ba9667 --- /dev/null +++ b/apps/files_sharing/lib/share/folder.php @@ -0,0 +1,64 @@ +. +*/ + +class OC_Share_Backend_Folder extends OC_Share_Backend_File { + + public function inCollection($collections, $item) { + // TODO + } + + public function getChildrenSources($item) { + return OC_FileCache::getFolderContent($item); + } + + public function formatItems($items, $format, $parameters = null) { + if ($format == self::FORMAT_FILE_APP && isset($parameters['folder'])) { + $folder = $items[key($items)]; + $query = OCP\DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id = ?'); + $result = $query->execute(array($folder['file_source']))->fetchRow(); + if (isset($result['path'])) { + if (isset($parameters['mimetype_filter'])) { + $mimetype_filter = $parameters['mimetype_filter']; + } else { + $mimetype_filter = ''; + } + $pos = strpos($result['path'], $folder['item']); + $path = substr($result['path'], $pos).substr($parameters['folder'], strlen($folder['file_target'])); + $root = substr($result['path'], 0, $pos); + return OC_FileCache::getFolderContent($path, $root, $mimetype_filter); + } + }/* else if ($format == self::FORMAT_OPENDIR_ROOT) { + $query = OCP\DB::prepare('SELECT name FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + $result = $query->execute(); + $files = array(); + while ($file = $result->fetchRow()) { + // Set target path + $files[] = basename($shares[$file['id']]['item_target']); + } + return $files; + }*/ + return array(); + } + +} + + +?> \ No newline at end of file diff --git a/apps/files_sharing/sharedstorage.php b/apps/files_sharing/sharedstorage.php index 11a5d39f011..1dfdf133791 100644 --- a/apps/files_sharing/sharedstorage.php +++ b/apps/files_sharing/sharedstorage.php @@ -32,30 +32,25 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { $this->sharedFolder = $arguments['sharedFolder']; } - public function getSourcePath($target) { - $target = $this->sharedFolder.$target; + private function getSourcePath($target) { + $target = $this->sharedFolder.'/'.$target; + $target = rtrim($target, '/'); if (isset($this->sourcePaths[$target])) { return $this->sourcePaths[$target]; } else { - if (dirname($target) != $this->sharedFolder) { - $len = strlen($this->sharedFolder); - $pos = strpos($target, '/', $len); - // Get shared folder name - $itemTarget = substr($target, $len, $pos); - $insideFolder = true; + $pos = strpos($target, '/', 8); + // Get shared folder name + if ($pos !== false) { + $itemTarget = substr($target, 0, $pos); } else { $itemTarget = $target; - $insideFolder = false; } $sourcePath = OCP\Share::getItemSharedWith('file', $itemTarget, OC_Share_Backend_File::FORMAT_SOURCE_PATH); if ($sourcePath) { - if ($insideFolder) { - $this->sourcePaths[$target] = $sourcePath.substr($target, $pos); - } else { - $this->sourcePaths[$target] = $sourcePath; - } + $this->sourcePaths[$target] = $sourcePath.substr($target, strlen($itemTarget)); return $this->sourcePaths[$target]; } + OCP\Util::writeLog('files_sharing', 'File source path not found for: '.$target, OCP\Util::ERROR); return false; } } @@ -70,8 +65,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { if ($path == "" || $path == "/" || !$this->is_writable($path)) { return false; } else { - $source = $this->getSourcePath($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->mkdir($this->getInternalPath($source)); } @@ -80,116 +74,54 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { public function rmdir($path) { // The folder will be removed from the database, but won't be deleted from the owner's filesystem - OC_Share::unshareFromMySelf($this->datadir.$path); - $this->clearFolderSizeCache($path); + // TODO } public function opendir($path) { - if ($path == "" || $path == "/") { - $path = $this->datadir.$path; - // TODO - $sharedItems = OC_Share::getItemsInFolder($path); - $files = array(); - foreach ($sharedItems as $item) { - // If item is in the root of the shared storage provider and the item exists add it to the fakedirs - if (dirname($item['target'])."/" == $path && $this->file_exists(basename($item['target']))) { - $files[] = basename($item['target']); - } - } - OC_FakeDirStream::$dirs['shared']=$files; + if ($path == '' || $path == '/') { + $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_Folder::FORMAT_OPENDIR); + OC_FakeDirStream::$dirs['shared'] = $files; return opendir('fakedir://shared'); } else { - $source = $this->getSourcePath($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); - $dh = $storage->opendir($this->getInternalPath($source)); - $modifiedItems = OC_Share::getItemsInFolder($source); - if ($modifiedItems && $dh) { - $sources = array(); - $targets = array(); - // Remove any duplicate or trailing '/' - $path = preg_replace('{(/)\1+}', "/", $path); - $targetFolder = rtrim($this->datadir.$path, "/"); - foreach ($modifiedItems as $item) { - // If the item is in the current directory and the item exists add it to the arrays - if (dirname($item['target']) == $targetFolder && $this->file_exists($path."/".basename($item['target']))) { - // If the item was unshared from self, add it it to the arrays - if ($item['permissions'] == OC_Share::UNSHARED) { - $sources[] = basename($item['source']); - $targets[] = ""; - } else { - $sources[] = basename($item['source']); - $targets[] = basename($item['target']); - } - } - } - // Don't waste time if there aren't any modified items in the current directory - if (empty($sources)) { - return $dh; - } else { - global $FAKEDIRS; - $files = array(); - while (($filename = readdir($dh)) !== false) { - if ($filename != "." && $filename != "..") { - // If the file isn't in the sources array it isn't modified and can be added as is - if (!in_array($filename, $sources)) { - $files[] = $filename; - // The file has a different name than the source and is added to the fakedirs - } else { - $target = $targets[array_search($filename, $sources)]; - // Don't add the file if it was unshared from self by the user - if ($target != "") { - $files[] = $target; - } - } - } - } - $FAKEDIRS['shared'] = $files; - return opendir('fakedir://shared'); - } - } else { - return $dh; - } + return $storage->opendir($this->getInternalPath($source)); } } } - + public function is_dir($path) { - if ($path == "" || $path == "/") { + if ($path == '' || $path == '/') { return true; } else { - $source = $this->getSourcePath($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->is_dir($this->getInternalPath($source)); } } } - + public function is_file($path) { - $source = $this->getSourcePath($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->is_file($this->getInternalPath($source)); } } - - // TODO fill in other components of array + public function stat($path) { - if ($path == "" || $path == "/") { - $stat["size"] = $this->filesize($path); - $stat["mtime"] = $this->filemtime($path); - $stat["ctime"] = $this->filectime($path); + if ($path == '' || $path == '/') { + $stat['size'] = $this->filesize($path); + $stat['mtime'] = $this->filemtime($path); + $stat['ctime'] = $this->filectime($path); return $stat; } else { - $source = $this->getSourcePath($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->stat($this->getInternalPath($source)); } } } - + public function filetype($path) { if ($path == "" || $path == "/") { return "dir"; @@ -202,7 +134,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } } - + public function filesize($path) { if ($path == "" || $path == "/" || $this->is_dir($path)) { return $this->getFolderSize($path); @@ -215,55 +147,6 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } } - public function getFolderSize($path) { - return 0; //depricated - } - - private function calculateFolderSize($path) { - if ($this->is_file($path)) { - $path = dirname($path); - } - $size = 0; - if ($dh = $this->opendir($path)) { - while (($filename = readdir($dh)) !== false) { - if ($filename != "." && $filename != "..") { - $subFile = $path."/".$filename; - if ($this->is_file($subFile)) { - $size += $this->filesize($subFile); - } else { - $size += $this->getFolderSize($subFile); - } - } - } - if ($size > 0) { - $dbpath = rtrim($this->datadir.$path, "/"); -// $query = OCP\DB::prepare("INSERT INTO *PREFIX*foldersize VALUES(?,?)"); -// $result = $query->execute(array($dbpath, $size)); - } - } - return $size; - } - - private function clearFolderSizeCache($path) { - $path = rtrim($path, "/"); - $path = preg_replace('{(/)\1+}', "/", $path); - if ($this->is_file($path)) { - $path = dirname($path); - } - $dbpath = rtrim($this->datadir.$path, "/"); -// $query = OCP\DB::prepare("DELETE FROM *PREFIX*/*foldersize*/ WHERE path = ?"); -// $result = $query->execute(array($dbpath)); - if ($path != "/" && $path != "") { - $parts = explode("/", $path); - $part = array_pop($parts); - if (empty($part)) { - array_pop($parts); - } - $parent = implode("/", $parts); - $this->clearFolderSizeCache($parent); - } - } - public function is_readable($path) { return true; } @@ -271,7 +154,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { public function is_writable($path) { if($path == "" || $path == "/"){ return false; - }elseif (OC_Share::getPermissions($this->datadir.$path) & OC_Share::WRITE) { + }elseif (OC_Share::getPermissions($this->sharedFolder.$path) & OC_Share::WRITE) { return true; } else { return false; @@ -336,7 +219,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { $source = $this->getSourcePath($path); if ($source) { $info = array( - 'target' => $this->datadir.$path, + 'target' => $this->sharedFolder.$path, 'source' => $source, ); OCP\Util::emitHook('OC_Filestorage_Shared', 'file_get_contents', $info); @@ -350,7 +233,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { $source = $this->getSourcePath($path); if ($source) { $info = array( - 'target' => $this->datadir.$path, + 'target' => $this->sharedFolder.$path, 'source' => $source, ); OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info); @@ -366,7 +249,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { public function unlink($path) { // The item will be removed from the database, but won't be touched on the owner's filesystem - $target = $this->datadir.$path; + $target = $this->sharedFolder.$path; // Check if the item is inside a shared folder if (OC_Share::getParentFolders($target)) { // If entry for item already exists @@ -385,8 +268,8 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } public function rename($path1, $path2) { - $oldTarget = $this->datadir.$path1; - $newTarget = $this->datadir.$path2; + $oldTarget = $this->sharedFolder.$path1; + $newTarget = $this->sharedFolder.$path2; // Check if the item is inside a shared folder if ($folders = OC_Share::getParentFolders($oldTarget)) { $root1 = substr($path1, 0, strpos($path1, "/")); @@ -442,7 +325,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { $source = $this->getSourcePath($path); if ($source) { $info = array( - 'target' => $this->datadir.$path, + 'target' => $this->sharedFolder.$path, 'source' => $source, 'mode' => $mode, ); @@ -541,7 +424,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { public static function setup($options) { $user_dir = $options['user_dir']; - OC_Filesystem::mount('OC_Filestorage_Shared', array('sharedFolder' => $user_dir.'/Shared'), $user_dir.'/Shared/'); + OC_Filesystem::mount('OC_Filestorage_Shared', array('sharedFolder' => '/Shared'), $user_dir.'/Shared/'); } /** @@ -551,6 +434,6 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { */ public function hasUpdated($path,$time){ //TODO - return $this->filemtime($path)>$time; + return false; } } diff --git a/core/js/share.js b/core/js/share.js index 755e71e996e..3c7d2619eb2 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -84,10 +84,8 @@ OC.Share={ $(html).appendTo(appendTo); var data = OC.Share.loadItem(itemType, item); if (data) { - $.each(data, function(index, shares) { - $.each(shares, function(id, share) { - OC.Share.addShareWith(share.share_with, share.permissions); - }); + $.each(data, function(index, share) { + OC.Share.addShareWith(share.share_with, share.permissions); }); } $('#dropdown').show('blind'); @@ -160,6 +158,7 @@ $(document).ready(function() { }); if (typeof FileActions !== 'undefined') { + OC.Share.loadIcons('file'); FileActions.register('all', 'Share', function(filename) { // Return the correct sharing icon @@ -187,19 +186,24 @@ $(document).ready(function() { } }, function(filename) { var item = $('#dir').val() + '/' + filename; - var appendTo = $('tr').filterAttr('data-file',filename).find('td.filename'); + if ($('tr').filterAttr('data-file', filename).data('type') == 'dir') { + var itemType = 'folder'; + } else { + var itemType = 'file'; + } + var appendTo = $('tr').filterAttr('data-file', filename).find('td.filename'); // Check if drop down is already visible for a different file if (($('#dropdown').length > 0)) { if (item != $('#dropdown').data('item')) { OC.Share.hideDropDown(function () { $('tr').removeClass('mouseOver'); $('tr').filterAttr('data-file', filename).addClass('mouseOver'); - OC.Share.showDropDown('file', item, appendTo, true); + OC.Share.showDropDown(itemType, item, appendTo, true); }); } } else { $('tr').filterAttr('data-file',filename).addClass('mouseOver'); - OC.Share.showDropDown('file', item, appendTo, true); + OC.Share.showDropDown(itemType, item, appendTo, true); } }); } diff --git a/lib/filecache.php b/lib/filecache.php index d956f34dc48..b6f4d23986b 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -324,6 +324,10 @@ class OC_FileCache{ $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); } $lastSend=$count; + // NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache) + if (substr($path, 0, 7) == '/Shared') { + return; + } if($root===false){ $view=OC_Filesystem::getView(); }else{ @@ -361,6 +365,10 @@ class OC_FileCache{ * @return int size of the scanned file */ public static function scanFile($path,$root=false){ + // NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache) + if (substr($path, 0, 7) == '/Shared') { + return; + } if($root===false){ $view=OC_Filesystem::getView(); }else{ diff --git a/lib/files.php b/lib/files.php index 2feae20afe6..4f58ff6782d 100644 --- a/lib/files.php +++ b/lib/files.php @@ -33,15 +33,30 @@ class OC_Files { * @param dir $directory path under datadirectory */ public static function getDirectoryContent($directory, $mimetype_filter = ''){ - $files=OC_FileCache::getFolderContent($directory, false, $mimetype_filter); - if ($directory == '') { - $files = array_merge($files, array()); - } else if (substr($directory, 7) == '/Shared') { - $files = array_merge($files, OCP\Share::getItemsSharedWith('file', $directory, OC_Share_Backend_File::FORMAT_FILE_APP)); + $files = array(); + if (substr($directory, 0, 7) == '/Shared') { + if ($directory == '/Shared') { + $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP, array('mimetype_filter' => $mimetype_filter)); + } else { + $pos = strpos($directory, '/', 8); + // Get shared folder name + if ($pos !== false) { + $itemTarget = substr($directory, 0, $pos); + } else { + $itemTarget = $directory; + } + $files = OCP\Share::getItemSharedWith('folder', $itemTarget, OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter)); + } + } else { + $files = OC_FileCache::getFolderContent($directory, false, $mimetype_filter); + if ($directory == '') { + // Add 'Shared' folder + $files = array_merge($files, OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP_ROOT)); + } } - foreach($files as &$file){ - $file['directory']=$directory; - $file['type']=($file['mimetype']=='httpd/unix-directory')?'dir':'file'; + foreach ($files as &$file) { + $file['directory'] = $directory; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; } usort($files, "fileCmp");//TODO: remove this once ajax is merged return $files; @@ -109,8 +124,7 @@ class OC_Files { header('Content-Type: application/zip'); header('Content-Length: ' . filesize($filename)); }else{ - $fileData=OC_FileCache::get($filename); - header('Content-Type: ' . $fileData['mimetype']); + header('Content-Type: '.OC_Filesystem::getMimeType($filename)); } }elseif($zip or !OC_Filesystem::file_exists($filename)){ header("HTTP/1.0 404 Not Found"); diff --git a/lib/public/share.php b/lib/public/share.php index 641d1640019..8c7f5a7ad84 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -53,9 +53,9 @@ class Share { * @param array (optional) List of supported file extensions if this item type depends on files * @return Returns true if backend is registered or false if error */ - public static function registerBackend($itemType, $class, $dependsOn = null, $supportedFileExtensions = null) { + public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { if (!isset(self::$backendTypes[$itemType])) { - self::$backendTypes[$itemType] = array('class' => $class, 'dependsOn' => $dependsOn, 'supportedFileExtensions' => $supportedFileExtensions); + self::$backendTypes[$itemType] = array('class' => $class, 'collectionOf' => $collectionOf, 'supportedFileExtensions' => $supportedFileExtensions); return true; } \OC_Log::write('OCP\Share', 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'].' is already registered for '.$itemType, \OC_Log::WARN); @@ -69,8 +69,8 @@ class Share { * @param int Number of items to return (optional) Returns all by default * @return Return depends on format */ - public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, $limit = -1) { - return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $limit); + public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, $parameters = null, $limit = -1) { + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $parameters, $limit); } /** @@ -80,8 +80,8 @@ class Share { * @param int Format (optional) Format type must be defined by the backend * @return Return depends on format */ - public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE) { - return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, 1); + public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE, $parameters = null) { + return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $parameters, 1); } /** @@ -91,8 +91,8 @@ class Share { * @param int Format (optional) Format type must be defined by the backend * @return Return depends on format */ - public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE) { - return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, 1, true); + public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, $parameters = null) { + return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $parameters, 1, true); } /** @@ -102,8 +102,8 @@ class Share { * @param int Number of items to return (optional) Returns all by default * @return Return depends on format */ - public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $limit = -1) { - return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, $limit); + public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null, $limit = -1) { + return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, $parameters, $limit); } /** @@ -113,8 +113,8 @@ class Share { * @param int Format (optional) Format type must be defined by the backend * @return Return depends on format */ - public static function getItemShared($itemType, $item, $format = self::FORMAT_NONE) { - return self::getItems($itemType, $item, null, null, \OC_User::getUser(), $format); + public static function getItemShared($itemType, $item, $format = self::FORMAT_NONE, $parameters = null) { + return self::getItems($itemType, $item, null, null, \OC_User::getUser(), $format, $parameters); } /** @@ -124,8 +124,8 @@ class Share { * @param int Format (optional) Format type must be defined by the backend * @return Return depends on format */ - public static function getItemSharedBySource($itemType, $item, $format = self::FORMAT_NONE) { - return self::getItems($itemType, $item, null, null, \OC_User::getUser(), $format, -1, true); + public static function getItemSharedBySource($itemType, $item, $format = self::FORMAT_NONE, $parameters = null) { + return self::getItems($itemType, $item, null, null, \OC_User::getUser(), $format, $parameters, -1, true); } /** @@ -195,7 +195,7 @@ class Share { if ($parentFolder && $files = \OC_Files::getDirectoryContent($item)) { for ($i = 0; $i < count($files); $i++) { $name = substr($files[$i]['name'], strpos($files[$i]['name'], $item) - strlen($item)); - if ($files[$i]['mimetype'] == 'httpd/unix-directory' && $children = OC_Files::getDirectoryContent($name, '/')) { + if ($files[$i]['mimetype'] == 'httpd/unix-directory' && $children = \OC_Files::getDirectoryContent($name, '/')) { // Continue scanning into child folders array_push($files, $children); } else { @@ -370,19 +370,19 @@ class Share { } /** - * @brief Get a list of parent item types for the specified item type + * @brief Get a list of collection item types for the specified item type * @param string Item type * @return array */ - private static function getParentItemTypes($itemType) { - $parents = array($itemType); - foreach (self::$backends as $type => $backend) { - if (in_array($backend->dependsOn, $parents)) { - $parents[] = $type; + private static function getCollectionItemTypes($itemType) { + $collections = array($itemType); + foreach (self::$backendTypes as $type => $backend) { + if (in_array($backend['collectionOf'], $collections)) { + $collections[] = $type; } } - if (!empty($parents)) { - return $parents; + if (count($collections) > 1) { + return $collections; } return false; } @@ -400,19 +400,13 @@ class Share { * See public functions getItem(s)... for parameter usage * */ - private static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, $uidOwner = null, $format = self::FORMAT_NONE, $limit = -1, $isSource = false) { + private static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, $isSource = false) { if ($backend = self::getBackend($itemType)) { // Check if there are any parent types that include this type of items, e.g. a music album contains songs - if (isset($itemType)) { - if ($parents = self::getParentItemTypes($itemType)) { - $where = "WHERE item_type IN ('".implode("','", $parents)."')"; - } else { - $where = "WHERE item_type = '".$itemType."'"; - } - // TODO exclude items that are inside of folders and got converted i.e. songs, pictures - if ($itemType == 'files') { - - } + if ($parents = self::getCollectionItemTypes($itemType)) { + $where = "WHERE item_type IN ('".implode("','", $parents)."')"; + } else { + $where = "WHERE item_type = '".$itemType."'"; } if (isset($shareType) && isset($shareWith)) { // Include all user and group items @@ -450,20 +444,21 @@ class Share { } } else { if ($isSource) { - if ($itemType == 'file') { + if ($itemType == 'file' || $itemType == 'folder') { $where .= " AND file_source = '".$item."'"; } else { $where .= " AND item_source = '".$item."'"; } } else { - if ($itemType == 'file' && substr($item, -1) == '/') { - // Special case to select only the shared files inside the folder - $where .= " AND file_target LIKE '".$item."%/'"; + if ($itemType == 'file' || $itemType == 'folder') { + $where .= " AND file_target = '".$item."'"; } else { $where .= " AND item_target = '".$item."'"; } } } + } else if ($itemType == 'file') { + // TODO Exclude converted items inside shared folders } if ($limit != -1) { if ($limit == 1 && $shareType == self::$shareTypeUserAndGroups) { @@ -486,16 +481,12 @@ class Share { $result = $query->execute(); $items = array(); while ($item = $result->fetchRow()) { - if ($limit == 1) { - // Return just the item instead of 3-dimensional array - return $item; - } // Filter out duplicate group shares for users with unique targets if ($item['share_type'] == self::$shareTypeGroupUserUnique) { // Remove the parent group share - unset($items[$item['item_source']][$item['parent']]); + unset($items[$item['parent']]); } - $items[$item['item_source']][$item['id']] = $item; + $items[$item['id']] = $item; // TODO Add in parent item types children? if ($parents && in_array($item['item_type'], $parents)) { $children[] = $item; @@ -506,20 +497,17 @@ class Share { return $items; } else if ($format == self::FORMAT_STATUSES) { $statuses = array(); - foreach ($items as $shares) { - foreach ($shares as $info) { - if ($info['share_type'] == self::SHARE_TYPE_PRIVATE_LINK) { - $statuses[$info['item']] = true; - break; - } else if (!isset($statuses[$info['item']])) { - $statuses[$info['item']] = false; - } + foreach ($items as $item) { + if ($item['share_type'] == self::SHARE_TYPE_PRIVATE_LINK) { + $statuses[$item['item']] = true; + break; + } else if (!isset($statuses[$item['item']])) { + $statuses[$item['item']] = false; } - } return $statuses; } else { - return $backend->formatItems($items, $format); + return $backend->formatItems($items, $format, $parameters); } } else if ($limit == 1 || (isset($uidOwner) && isset($item))) { return false; @@ -764,12 +752,14 @@ abstract class Share_Backend { * This function allows the backend to control the output of shared items with custom formats. * It is only called through calls to the public getItem(s)Shared(With) functions. */ - public abstract function formatItems($items, $format); + public abstract function formatItems($items, $format, $parameters = null); } -abstract class Share_Backend_Parent extends Share_Backend { +abstract class Share_Backend_Collection extends Share_Backend { + + public abstract function inCollection($collections, $item); public abstract function getChildren($item); -- cgit v1.2.3 From 4d17ed2f71c8cbb0d34c039aa7953b2427ce5c78 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Wed, 25 Jul 2012 16:33:08 -0400 Subject: Make file actions permissions aware --- apps/calendar/js/loader.js | 2 +- apps/contacts/js/loader.js | 4 +-- apps/files/js/fileactions.js | 54 ++++++++++++++++++++------------- apps/files/js/files.js | 5 +-- apps/files/templates/part.list.php | 3 +- apps/files_archive/js/archive.js | 4 +-- apps/files_imageviewer/js/lightbox.js | 2 +- apps/files_pdfviewer/js/viewer.js | 2 +- apps/files_sharing/lib/share/file.php | 11 +++++-- apps/files_sharing/lib/share/folder.php | 13 +++++++- apps/files_texteditor/js/editor.js | 16 +++++++--- apps/files_versions/js/versions.js | 2 +- apps/media/js/loader.js | 4 +-- core/js/share.js | 2 +- lib/files.php | 18 ++++++++--- 15 files changed, 94 insertions(+), 48 deletions(-) (limited to 'lib/files.php') diff --git a/apps/calendar/js/loader.js b/apps/calendar/js/loader.js index cef95afc3aa..57cf5adff0e 100644 --- a/apps/calendar/js/loader.js +++ b/apps/calendar/js/loader.js @@ -75,7 +75,7 @@ Calendar_Import={ } $(document).ready(function(){ if(typeof FileActions !== 'undefined'){ - FileActions.register('text/calendar','importcal', '', Calendar_Import.importdialog); + FileActions.register('text/calendar','importcal', FileActions.PERMISSION_READ, '', Calendar_Import.importdialog); FileActions.setDefault('text/calendar','importcal'); }; }); diff --git a/apps/contacts/js/loader.js b/apps/contacts/js/loader.js index 577ad103064..3b1f4070485 100644 --- a/apps/contacts/js/loader.js +++ b/apps/contacts/js/loader.js @@ -78,9 +78,9 @@ Contacts_Import={ } $(document).ready(function(){ if(typeof FileActions !== 'undefined'){ - FileActions.register('text/vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.register('text/vcard','importaddressbook', FileActions.PERMISSION_READ, '', Contacts_Import.importdialog); FileActions.setDefault('text/vcard','importaddressbook'); - FileActions.register('text/x-vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.register('text/x-vcard','importaddressbook', FileActions.PERMISSION_READ, '', Contacts_Import.importdialog); FileActions.setDefault('text/x-vcard','importaddressbook'); }; }); \ No newline at end of file diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js index 4dc05088eed..d54dd466469 100644 --- a/apps/files/js/fileactions.js +++ b/apps/files/js/fileactions.js @@ -1,19 +1,28 @@ FileActions={ + PERMISSION_CREATE:4, + PERMISSION_READ:1, + PERMISSION_UPDATE:2, + PERMISSION_DELETE:8, + PERMISSION_SHARE:16, actions:{}, defaults:{}, icons:{}, currentFile:null, - register:function(mime,name,icon,action){ + register:function(mime,name,permissions,icon,action){ if(!FileActions.actions[mime]){ FileActions.actions[mime]={}; } - FileActions.actions[mime][name]=action; + if (!FileActions.actions[mime][name]) { + FileActions.actions[mime][name] = {}; + } + FileActions.actions[mime][name]['action'] = action; + FileActions.actions[mime][name]['permissions'] = permissions; FileActions.icons[name]=icon; }, setDefault:function(mime,name){ FileActions.defaults[mime]=name; }, - get:function(mime,type){ + get:function(mime,type,permissions){ var actions={}; if(FileActions.actions.all){ actions=$.extend( actions, FileActions.actions.all ) @@ -32,9 +41,15 @@ FileActions={ actions=$.extend( actions, FileActions.actions[type] ) } } - return actions; + var filteredActions = {}; + $.each(actions, function(name, action) { + if (action.permissions & permissions) { + filteredActions[name] = action.action; + } + }); + return filteredActions; }, - getDefault:function(mime,type){ + getDefault:function(mime,type,permissions){ if(mime){ var mimePart=mime.substr(0,mime.indexOf('/')); } @@ -48,22 +63,20 @@ FileActions={ }else{ name=FileActions.defaults.all; } - var actions=this.get(mime,type); + var actions=this.get(mime,type,permissions); return actions[name]; }, - display:function(parent, filename, type){ + display:function(parent){ FileActions.currentFile=parent; $('#fileList span.fileactions, #fileList td.date a.action').remove(); - var actions=FileActions.get(FileActions.getCurrentMimeType(),FileActions.getCurrentType()); + var actions=FileActions.get(FileActions.getCurrentMimeType(),FileActions.getCurrentType(), FileActions.getCurrentPermissions()); var file=FileActions.getCurrentFile(); if($('tr').filterAttr('data-file',file).data('renaming')){ return; } parent.children('a.name').append(''); - var defaultAction=FileActions.getDefault(FileActions.getCurrentMimeType(),FileActions.getCurrentType()); + var defaultAction=FileActions.getDefault(FileActions.getCurrentMimeType(),FileActions.getCurrentType(), FileActions.getCurrentPermissions()); for(name in actions){ - // no rename and share action for the 'Shared' dir - if((name=='Rename' || name =='Share') && type=='dir' && filename=='Shared') { continue; } if((name=='Download' || actions[name]!=defaultAction) && name!='Delete'){ var img=FileActions.icons[name]; if(img.call){ @@ -86,16 +99,12 @@ FileActions={ parent.find('a.name>span.fileactions').append(element); } } - if(actions['Delete'] && (type!='dir' || filename != 'Shared')){ // no delete action for the 'Shared' dir + if(actions['Delete']){ var img=FileActions.icons['Delete']; if(img.call){ img=img(file); } - if ($('#dir').val().indexOf('Shared') != -1) { - var html=' diff --git a/apps/files_archive/js/archive.js b/apps/files_archive/js/archive.js index 9fb9853e299..6bcbe092662 100644 --- a/apps/files_archive/js/archive.js +++ b/apps/files_archive/js/archive.js @@ -7,11 +7,11 @@ $(document).ready(function() { if(typeof FileActions!=='undefined'){ - FileActions.register('application/zip','Open','',function(filename){ + FileActions.register('application/zip','Open', FileActions.PERMISSION_READ, '',function(filename){ window.location=OC.linkTo('files', 'index.php')+'&dir='+encodeURIComponent($('#dir').val()).replace(/%2F/g, '/')+'/'+encodeURIComponent(filename); }); FileActions.setDefault('application/zip','Open'); - FileActions.register('application/x-gzip','Open','',function(filename){ + FileActions.register('application/x-gzip','Open', FileActions.PERMISSION_READ, '',function(filename){ window.location=OC.linkTo('files', 'index.php')+'&dir='+encodeURIComponent($('#dir').val()).replace(/%2F/g, '/')+'/'+encodeURIComponent(filename); }); FileActions.setDefault('application/x-gzip','Open'); diff --git a/apps/files_imageviewer/js/lightbox.js b/apps/files_imageviewer/js/lightbox.js index 31f08456d22..ff12d808bc8 100644 --- a/apps/files_imageviewer/js/lightbox.js +++ b/apps/files_imageviewer/js/lightbox.js @@ -1,6 +1,6 @@ $(document).ready(function() { if(typeof FileActions!=='undefined'){ - FileActions.register('image','View','',function(filename){ + FileActions.register('image','View', FileActions.PERMISSION_READ, '',function(filename){ viewImage($('#dir').val(),filename); }); FileActions.setDefault('image','View'); diff --git a/apps/files_pdfviewer/js/viewer.js b/apps/files_pdfviewer/js/viewer.js index 2c9cbb9b431..29db2ea7f24 100644 --- a/apps/files_pdfviewer/js/viewer.js +++ b/apps/files_pdfviewer/js/viewer.js @@ -40,7 +40,7 @@ $(document).ready(function(){ if(location.href.indexOf("files")!=-1) { PDFJS.workerSrc = OC.filePath('files_pdfviewer','js','pdfjs/build/pdf.js'); if(typeof FileActions!=='undefined'){ - FileActions.register('application/pdf','Edit','',function(filename){ + FileActions.register('application/pdf','Edit', FileActions.PERMISSION_READ, '',function(filename){ showPDFviewer($('#dir').val(),filename); }); FileActions.setDefault('application/pdf','Edit'); diff --git a/apps/files_sharing/lib/share/file.php b/apps/files_sharing/lib/share/file.php index 658c42ee327..dd63539cdfe 100644 --- a/apps/files_sharing/lib/share/file.php +++ b/apps/files_sharing/lib/share/file.php @@ -70,7 +70,14 @@ class OC_Share_Backend_File extends OCP\Share_Backend { // Set target path $file['path'] = $shares[$file['id']]['file_target']; $file['name'] = basename($file['path']); - // TODO Set permissions: $file['writable'] + $file['directory'] = $parameters['folder']; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $permissions = $shares[$file['id']]['permissions']; + if ($file['type'] == 'file') { + // Remove Create permission if type is file + $permissions &= ~OCP\Share::PERMISSION_CREATE; + } + $file['permissions'] = $permissions; $files[] = $file; } return $files; @@ -85,7 +92,7 @@ class OC_Share_Backend_File extends OCP\Share_Backend { } $size += $file['size']; } - return array(0 => array('name' => 'Shared', 'mtime' => $mtime, 'mimetype' => 'httpd/unix-directory', 'size' => $size, 'writable' => false)); + return array(0 => array('name' => 'Shared', 'mtime' => $mtime, 'mimetype' => 'httpd/unix-directory', 'size' => $size, 'writable' => false, 'type' => 'dir', 'directory' => '', 'permissions' => OCP\Share::PERMISSION_READ)); } } return array(); diff --git a/apps/files_sharing/lib/share/folder.php b/apps/files_sharing/lib/share/folder.php index 033e2ba9667..a43ce2b2caf 100644 --- a/apps/files_sharing/lib/share/folder.php +++ b/apps/files_sharing/lib/share/folder.php @@ -43,7 +43,18 @@ class OC_Share_Backend_Folder extends OC_Share_Backend_File { $pos = strpos($result['path'], $folder['item']); $path = substr($result['path'], $pos).substr($parameters['folder'], strlen($folder['file_target'])); $root = substr($result['path'], 0, $pos); - return OC_FileCache::getFolderContent($path, $root, $mimetype_filter); + $files = OC_FileCache::getFolderContent($path, $root, $mimetype_filter); + foreach ($files as &$file) { + $file['directory'] = $parameters['folder']; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $permissions = $folder['permissions']; + if ($file['type'] == 'file') { + // Remove Create permission if type is file + $permissions &= ~OCP\Share::PERMISSION_CREATE; + } + $file['permissions'] = $permissions; + } + return $files; } }/* else if ($format == self::FORMAT_OPENDIR_ROOT) { $query = OCP\DB::prepare('SELECT name FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); diff --git a/apps/files_texteditor/js/editor.js b/apps/files_texteditor/js/editor.js index 70bb74a9101..3784ea1032f 100644 --- a/apps/files_texteditor/js/editor.js +++ b/apps/files_texteditor/js/editor.js @@ -222,9 +222,17 @@ function showFileEditor(dir,filename){ } }); // Add the ctrl+s event - window.aceEditor.commands.addCommand({ name: "save", bindKey: { win: "Ctrl-S", mac: "Command-S", sender: "editor" }, exec: function(){ + window.aceEditor.commands.addCommand({ + name: "save", + bindKey: { + win: "Ctrl-S", + mac: "Command-S", + sender: "editor" + }, + exec: function(){ doFileSave(); - } }); + } + }); }); } else { // Failed to get the file. @@ -297,11 +305,11 @@ $(window).resize(function() { var is_editor_shown = false; $(document).ready(function(){ if(typeof FileActions!=='undefined'){ - FileActions.register('text','Edit','',function(filename){ + FileActions.register('text','Edit', FileActions.PERMISSION_READ, '',function(filename){ showFileEditor($('#dir').val(),filename); }); FileActions.setDefault('text','Edit'); - FileActions.register('application/xml','Edit','',function(filename){ + FileActions.register('application/xml','Edit', FileActions.PERMISSION_READ, '',function(filename){ showFileEditor($('#dir').val(),filename); }); FileActions.setDefault('application/xml','Edit'); diff --git a/apps/files_versions/js/versions.js b/apps/files_versions/js/versions.js index a090fde446e..c5c1553f1a8 100644 --- a/apps/files_versions/js/versions.js +++ b/apps/files_versions/js/versions.js @@ -11,7 +11,7 @@ $(document).ready(function() { $(document).ready(function(){ if (typeof FileActions !== 'undefined') { // Add history button to files/index.php - FileActions.register('file','History',function(){return OC.imagePath('core','actions/history')},function(filename){ + FileActions.register('file','History', FileActions.PERMISSION_UPDATE, function(){return OC.imagePath('core','actions/history')},function(filename){ if (scanFiles.scanning){return;}//workaround to prevent additional http request block scanning feedback diff --git a/apps/media/js/loader.js b/apps/media/js/loader.js index 393f8ba914e..ffe9c1cdd61 100644 --- a/apps/media/js/loader.js +++ b/apps/media/js/loader.js @@ -45,8 +45,8 @@ $(document).ready(function() { // FileActions.register('application/ogg','Add to playlist','',addAudio); if(typeof FileActions!=='undefined'){ - FileActions.register('audio','Play','',playAudio); - FileActions.register('application/ogg','','Play',playAudio); + FileActions.register('audio','Play', FileActions.PERMISSION_READ, '',playAudio); + FileActions.register('application/ogg', FileActions.PERMISSION_READ, '','Play',playAudio); FileActions.setDefault('audio','Play'); FileActions.setDefault('application/ogg','Play'); } diff --git a/core/js/share.js b/core/js/share.js index 42002ea7985..4cff9ec1a67 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -164,7 +164,7 @@ $(document).ready(function() { if (typeof FileActions !== 'undefined') { OC.Share.loadIcons('file'); - FileActions.register('all', 'Share', function(filename) { + FileActions.register('all', 'Share', FileActions.PERMISSION_SHARE, function(filename) { // Return the correct sharing icon if (scanFiles.scanning) { return; } // workaround to prevent additional http request block scanning feedback var item = $('#dir').val() + '/' + filename; diff --git a/lib/files.php b/lib/files.php index f7a7aecc167..fee71b777b3 100644 --- a/lib/files.php +++ b/lib/files.php @@ -36,7 +36,7 @@ class OC_Files { $files = array(); if (substr($directory, 0, 7) == '/Shared') { if ($directory == '/Shared') { - $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP, array('mimetype_filter' => $mimetype_filter)); + $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter)); } else { $pos = strpos($directory, '/', 8); // Get shared folder name @@ -49,15 +49,23 @@ class OC_Files { } } else { $files = OC_FileCache::getFolderContent($directory, false, $mimetype_filter); + foreach ($files as &$file) { + $file['directory'] = $directory; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $permissions = OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE; + if ($file['type'] == 'dir' && $file['writable']) { + $permissions |= OCP\Share::PERMISSION_CREATE; + } + if ($file['writable']) { + $permissions |= OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE; + } + $file['permissions'] = $permissions; + } if ($directory == '') { // Add 'Shared' folder $files = array_merge($files, OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP_ROOT)); } } - foreach ($files as &$file) { - $file['directory'] = $directory; - $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; - } usort($files, "fileCmp");//TODO: remove this once ajax is merged return $files; } -- cgit v1.2.3 From df8a2e5361714838637457373f9957c76fbf449f Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Thu, 16 Aug 2012 12:20:14 -0400 Subject: File sharing cleanup, works perfectly I think :) --- apps/files_sharing/lib/share/file.php | 91 ++++++++++++++------------------- apps/files_sharing/lib/share/folder.php | 73 ++++++++++---------------- apps/files_sharing/sharedstorage.php | 26 +++++----- core/js/share.js | 1 - lib/files.php | 4 +- lib/public/share.php | 29 ++++++++--- tests/lib/share/share.php | 8 +++ 7 files changed, 111 insertions(+), 121 deletions(-) (limited to 'lib/files.php') diff --git a/apps/files_sharing/lib/share/file.php b/apps/files_sharing/lib/share/file.php index 66be5f2b155..ae6315600f8 100644 --- a/apps/files_sharing/lib/share/file.php +++ b/apps/files_sharing/lib/share/file.php @@ -27,10 +27,10 @@ class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent { const FORMAT_OPENDIR = 3; public function isValidSource($item, $uid) { -// if (OC_Filesystem::file_exists($item)) { + if (OC_Filesystem::file_exists($item)) { return true; -// } -// return false; + } + return false; } public function getFilePath($item, $uid) { @@ -43,61 +43,48 @@ class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent { } public function formatItems($items, $format, $parameters = null) { - if ($format == self::FORMAT_OPENDIR) { + if ($format == self::FORMAT_SHARED_STORAGE) { + // Only 1 item should come through for this format call + return array('path' => $items[key($items)]['file_source'], 'permissions' => $items[key($items)]['permissions']); + } else if ($format == self::FORMAT_FILE_APP) { $files = array(); - foreach ($items as $file) { - $files[] = basename($file['file_target']); + foreach ($items as $item) { + $file = array(); + $file['path'] = $item['file_target']; + $file['name'] = basename($item['file_target']); + $file['ctime'] = $item['ctime']; + $file['mtime'] = $item['mtime']; + $file['mimetype'] = $item['mimetype']; + $file['size'] = $item['size']; + $file['encrypted'] = $item['encrypted']; + $file['versioned'] = $item['versioned']; + $file['directory'] = $parameters['folder']; + $file['type'] = ($item['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $file['permissions'] = $item['permissions']; + if ($file['type'] == 'file') { + // Remove Create permission if type is file + $file['permissions'] &= ~OCP\Share::PERMISSION_CREATE; + } + $files[] = $file; } return $files; - } else if ($format == self::FORMAT_SHARED_STORAGE) { - $id = $items[key($items)]['file_source']; - $query = OCP\DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id = ?'); - $result = $query->execute(array($id))->fetchAll(); - if (isset($result[0]['path'])) { - return array('path' => $result[0]['path'], 'permissions' => $items[key($items)]['permissions']); - } - return false; - } else { - $shares = array(); - $ids = array(); + } else if ($format == self::FORMAT_FILE_APP_ROOT) { + $mtime = 0; + $size = 0; foreach ($items as $item) { - $shares[$item['file_source']] = $item; - $ids[] = $item['file_source']; - } - $ids = "'".implode("','", $ids)."'"; - if ($format == self::FORMAT_FILE_APP) { - $query = OCP\DB::prepare('SELECT id, path, name, ctime, mtime, mimetype, size, encrypted, versioned, writable FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); - $result = $query->execute(); - $files = array(); - while ($file = $result->fetchRow()) { - // Set target path - $file['path'] = $shares[$file['id']]['file_target']; - $file['name'] = basename($file['path']); - $file['directory'] = $parameters['folder']; - $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; - $permissions = $shares[$file['id']]['permissions']; - if ($file['type'] == 'file') { - // Remove Create permission if type is file - $permissions &= ~OCP\Share::PERMISSION_CREATE; - } - $file['permissions'] = $permissions; - $files[] = $file; - } - return $files; - } else if ($format == self::FORMAT_FILE_APP_ROOT) { - $query = OCP\DB::prepare('SELECT id, path, name, ctime, mtime, mimetype, size, encrypted, versioned, writable FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); - $result = $query->execute(); - $mtime = 0; - $size = 0; - while ($file = $result->fetchRow()) { - if ($file['mtime'] > $mtime) { - $mtime = $file['mtime']; - } - $size += $file['size']; + if ($item['mtime'] > $mtime) { + $mtime = $item['mtime']; } - return array(0 => array('name' => 'Shared', 'mtime' => $mtime, 'mimetype' => 'httpd/unix-directory', 'size' => $size, 'writable' => false, 'type' => 'dir', 'directory' => '', 'permissions' => OCP\Share::PERMISSION_READ)); + $size += $item['size']; } - } + return array(0 => array('name' => 'Shared', 'mtime' => $mtime, 'mimetype' => 'httpd/unix-directory', 'size' => $size, 'writable' => false, 'type' => 'dir', 'directory' => '', 'permissions' => OCP\Share::PERMISSION_READ)); + } else if ($format == self::FORMAT_OPENDIR) { + $files = array(); + foreach ($items as $item) { + $files[] = basename($item['file_target']); + } + return $files; + } return array(); } diff --git a/apps/files_sharing/lib/share/folder.php b/apps/files_sharing/lib/share/folder.php index 2f101d33c8a..b6db96614fd 100644 --- a/apps/files_sharing/lib/share/folder.php +++ b/apps/files_sharing/lib/share/folder.php @@ -21,8 +21,32 @@ class OC_Share_Backend_Folder extends OC_Share_Backend_File { - public function inCollection($collections, $item) { - // TODO + public function formatItems($items, $format, $parameters = null) { + if ($format == self::FORMAT_SHARED_STORAGE) { + // Only 1 item should come through for this format call + return array('path' => $items[key($items)]['file_source'], 'permissions' => $items[key($items)]['permissions']); + } else if ($format == self::FORMAT_FILE_APP && isset($parameters['folder'])) { + // Only 1 item should come through for this format call + $folder = $items[key($items)]; + if (isset($parameters['mimetype_filter'])) { + $mimetype_filter = $parameters['mimetype_filter']; + } else { + $mimetype_filter = ''; + } + $path = $folder['file_source'].substr($parameters['folder'], 7 + strlen($folder['file_target'])); + $files = OC_FileCache::getFolderContent($path, '', $mimetype_filter); + foreach ($files as &$file) { + $file['directory'] = $parameters['folder']; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $file['permissions'] = $folder['permissions']; + if ($file['type'] == 'file') { + // Remove Create permission if type is file + $file['permissions'] &= ~OCP\Share::PERMISSION_CREATE; + } + } + return $files; + } + return array(); } public function getChildren($itemSource) { @@ -34,47 +58,4 @@ class OC_Share_Backend_Folder extends OC_Share_Backend_File { return $sources; } - public function formatItems($items, $format, $parameters = null) { - if ($format == self::FORMAT_FILE_APP && isset($parameters['folder'])) { - $folder = $items[key($items)]; - $query = OCP\DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id = ?'); - $result = $query->execute(array($folder['file_source']))->fetchRow(); - if (isset($result['path'])) { - if (isset($parameters['mimetype_filter'])) { - $mimetype_filter = $parameters['mimetype_filter']; - } else { - $mimetype_filter = ''; - } - $pos = strpos($result['path'], $folder['item']); - $path = substr($result['path'], $pos).substr($parameters['folder'], strlen($folder['file_target'])); - $root = substr($result['path'], 0, $pos); - $files = OC_FileCache::getFolderContent($path, $root, $mimetype_filter); - foreach ($files as &$file) { - $file['directory'] = $parameters['folder']; - $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; - $permissions = $folder['permissions']; - if ($file['type'] == 'file') { - // Remove Create permission if type is file - $permissions &= ~OCP\Share::PERMISSION_CREATE; - } - $file['permissions'] = $permissions; - } - return $files; - } - }/* else if ($format == self::FORMAT_OPENDIR_ROOT) { - $query = OCP\DB::prepare('SELECT name FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); - $result = $query->execute(); - $files = array(); - while ($file = $result->fetchRow()) { - // Set target path - $files[] = basename($shares[$file['id']]['item_target']); - } - return $files; - }*/ - return array(); - } - -} - - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/apps/files_sharing/sharedstorage.php b/apps/files_sharing/sharedstorage.php index fc2e6e32c7c..582c9c66172 100644 --- a/apps/files_sharing/sharedstorage.php +++ b/apps/files_sharing/sharedstorage.php @@ -50,7 +50,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { if (isset($this->files[$folder])) { $file = $this->files[$folder]; } else { - $file = OCP\Share::getItemSharedWith('file', $folder, OC_Share_Backend_File::FORMAT_SHARED_STORAGE); + $file = OCP\Share::getItemSharedWith('folder', $folder, OC_Share_Backend_File::FORMAT_SHARED_STORAGE); } if ($file) { $this->files[$target]['path'] = $file['path'].substr($target, strlen($folder)); @@ -285,19 +285,21 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } public function file_put_contents($path, $data) { - if ($this->is_writable($path)) { - $source = $this->getSourcePath($path); - if ($source) { - $info = array( - 'target' => $this->sharedFolder.$path, - 'source' => $source, - ); - OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info); - $storage = OC_Filesystem::getStorage($source); - $result = $storage->file_put_contents($this->getInternalPath($source), $data); - return $result; + if ($source = $this->getSourcePath($path)) { + // Check if permission is granted + if (($this->file_exists($path) && !$this->isUpdatable($path)) || ($this->is_dir($path) && !$this->isCreatable($path))) { + return false; } + $info = array( + 'target' => $this->sharedFolder.$path, + 'source' => $source, + ); + OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info); + $storage = OC_Filesystem::getStorage($source); + $result = $storage->file_put_contents($this->getInternalPath($source), $data); + return $result; } + return false; } public function unlink($path) { diff --git a/core/js/share.js b/core/js/share.js index 70d822f8814..38c20a75709 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -267,7 +267,6 @@ $(document).ready(function() { if (typeof FileActions !== 'undefined') { OC.Share.loadIcons('file'); - OC.Share.loadIcons('folder'); FileActions.register('all', 'Share', FileActions.PERMISSION_SHARE, function(filename) { // Return the correct sharing icon if (scanFiles.scanning) { return; } // workaround to prevent additional http request block scanning feedback diff --git a/lib/files.php b/lib/files.php index fee71b777b3..07d432f8066 100644 --- a/lib/files.php +++ b/lib/files.php @@ -41,9 +41,9 @@ class OC_Files { $pos = strpos($directory, '/', 8); // Get shared folder name if ($pos !== false) { - $itemTarget = substr($directory, 0, $pos); + $itemTarget = substr($directory, 7, $pos - 7); } else { - $itemTarget = $directory; + $itemTarget = substr($directory, 7); } $files = OCP\Share::getItemSharedWith('folder', $itemTarget, OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter)); } diff --git a/lib/public/share.php b/lib/public/share.php index c420943da26..940bf2d48a0 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -399,23 +399,28 @@ class Share { */ private static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, $includeCollections = false, $itemShareWithBySource = false) { $backend = self::getBackend($itemType); - // Get filesystem root to add it to the file target and remove from the file source + // Get filesystem root to add it to the file target and remove from the file source, match file_source with the file cache if ($backend instanceof Share_Backend_File_Dependent) { + $fileDependent = true; $root = \OC_Filesystem::getRoot(); + // TODO No need to do this for FORMAT_STATUSES and loading the item in the dropdown, it's a performance waste + $where = 'INNER JOIN *PREFIX*fscache ON file_source = *PREFIX*fscache.id '; } else { + $fileDependent = false; + $where = ''; $root = ''; } if ($itemType == 'file' && !isset($item)) { - $where = 'WHERE file_target IS NOT NULL'; + $where .= 'WHERE file_target IS NOT NULL'; $query_args = array(); } else if ($includeCollections && !isset($item) && $collectionTypes = self::getCollectionItemTypes($itemType)) { // If includeCollections is true, find collections of this item type, e.g. a music album contains songs $item_types = array_merge(array($itemType), $collectionTypes); $placeholders = join(',', array_fill(0, count($item_types), '?')); - $where = "WHERE item_type IN ('".$placeholders."')"; + $where .= "WHERE item_type IN ('".$placeholders."')"; $query_args = $item_types; - } else { - $where = "WHERE item_type = ?"; + } else if ($itemType != 'file' && $itemType != 'folder') { + $where .= "WHERE item_type = ?"; $query_args = array($itemType); } if (isset($shareType) && isset($shareWith)) { @@ -445,7 +450,6 @@ class Share { $query_args[] = self::$shareTypeGroupUserUnique; } if ($itemType == 'file' || $itemType == 'folder') { - $where = "INNER JOIN *PREFIX*fscache ON file_source = *PREFIX*fscache.id ".$where; $column = 'file_source'; } else { $column = 'item_source'; @@ -491,6 +495,7 @@ class Share { } $where .= ' LIMIT '.$limit; } + // TODO Optimize selects if ($format == self::FORMAT_STATUSES) { if ($itemType == 'file' || $itemType == 'folder') { $select = '*PREFIX*share.id, item_type, *PREFIX*share.parent, share_type, *PREFIX*fscache.path as file_source'; @@ -505,7 +510,15 @@ class Share { $select = 'id, item_type, item_source, parent, share_type, share_with, permissions, stime, file_source'; } } else { - $select = '*'; + if ($fileDependent) { + if (($itemType == 'file' || $itemType == 'folder') && $format == \OC_Share_Backend_File::FORMAT_FILE_APP || $format == \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT) { + $select = '*PREFIX*share.id, item_type, *PREFIX*share.parent, share_type, share_with, permissions, file_target, *PREFIX*fscache.id, path as file_source, name, ctime, mtime, mimetype, size, encrypted, versioned, writable'; + } else { + $select = '*PREFIX*share.id, item_type, item_source, item_target, *PREFIX*share.parent, share_type, share_with, permissions, stime, path as file_source, file_target'; + } + } else { + $select = '*'; + } } } $root = strlen($root); @@ -534,7 +547,7 @@ class Share { // TODO Check this outside of the loop // Check if this is a collection of the requested item type if ($row['item_type'] != $itemType && $itemType != 'file' && !isset($item)) { - if ($collectionBackend = self::getBackend($row['item_type'])) { + if ($collectionBackend = self::getBackend($row['item_type'] && $collectionBackend instanceof Share_Backend_Collection)) { $row['collection'] = array('item_type' => $itemType, $column => $row[$column]); // Fetch all of the children sources $children = $collectionBackend->getChildren($row[$column]); diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index 4b73cc183a3..efff2c5522c 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -233,6 +233,8 @@ class Test_Share extends UnitTestCase { $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ)); OC_User::setUserId($this->user2); $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt', 'test1.txt')); + + // Remove user } public function testShareWithGroup() { @@ -280,6 +282,12 @@ class Test_Share extends UnitTestCase { $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt', 'test1.txt')); OC_User::setUserId($this->user3); $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt')); + + // Remove user from group + + // Add user to group + + // Remove group } } -- cgit v1.2.3