public function testUpdateLegacyAndNewId() { // add storage ids $oldCache = new \OC\Files\Cache\Cache($this->oldId); new \OC\Files\Cache\Cache($this->newId); // add file to old cache $fileId = $oldCache->put('/', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory')); try { $this->instance = new \OC\Files\Storage\AmazonS3($this->params); } catch (\Exception $e) { //ignore } $storages = $this->getStorages(); $this->assertTrue(isset($storages[$this->newId])); $this->assertFalse(isset($storages[$this->oldId])); $this->assertNull(\OC\Files\Cache\Cache::getById($fileId), 'old filecache has not been cleared'); }
/** * Find which users can access a shared item * @param string $path to the file * @param string $ownerUser owner of the file * @param boolean $includeOwner include owner to the list of users with access to the file * @param boolean $returnUserPaths Return an array with the user => path map * @param boolean $recursive take all parent folders into account (default true) * @return array * @note $path needs to be relative to user data dir, e.g. 'file.txt' * not '/admin/data/file.txt' */ public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) { Filesystem::initMountPoints($ownerUser); $shares = $sharePaths = $fileTargets = array(); $publicShare = false; $remoteShare = false; $source = -1; $cache = false; $view = new \OC\Files\View('/' . $ownerUser . '/files'); $meta = $view->getFileInfo($path); if ($meta) { $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); } else { // if the file doesn't exists yet we start with the parent folder $meta = $view->getFileInfo(dirname($path)); } if ($meta !== false) { $source = $meta['fileid']; $cache = new \OC\Files\Cache\Cache($meta['storage']); } while ($source !== -1) { // Fetch all shares with another user if (!$returnUserPaths) { $query = \OC_DB::prepare('SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'); $result = $query->execute(array($source, self::SHARE_TYPE_USER)); } else { $query = \OC_DB::prepare('SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')'); $result = $query->execute(array($source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique)); } if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); } else { while ($row = $result->fetchRow()) { $shares[] = $row['share_with']; if ($returnUserPaths) { $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; } } } // We also need to take group shares into account $query = \OC_DB::prepare('SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'); $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); } else { while ($row = $result->fetchRow()) { $usersInGroup = \OC_Group::usersInGroup($row['share_with']); $shares = array_merge($shares, $usersInGroup); if ($returnUserPaths) { foreach ($usersInGroup as $user) { if (!isset($fileTargets[(int) $row['file_source']][$user])) { // When the user already has an entry for this file source // the file is either shared directly with him as well, or // he has an exception entry (because of naming conflict). $fileTargets[(int) $row['file_source']][$user] = $row; } } } } } //check for public link shares if (!$publicShare) { $query = \OC_DB::prepare(' SELECT `share_with` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1); $result = $query->execute(array($source, self::SHARE_TYPE_LINK)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); } else { if ($result->fetchRow()) { $publicShare = true; } } } //check for remote share if (!$remoteShare) { $query = \OC_DB::prepare(' SELECT `share_with` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1); $result = $query->execute(array($source, self::SHARE_TYPE_REMOTE)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); } else { if ($result->fetchRow()) { $remoteShare = true; } } } // let's get the parent for the next round $meta = $cache->get((int) $source); if ($recursive === true && $meta !== false) { $source = (int) $meta['parent']; } else { $source = -1; } } // Include owner in list of users, if requested if ($includeOwner) { $shares[] = $ownerUser; } if ($returnUserPaths) { $fileTargetIDs = array_keys($fileTargets); $fileTargetIDs = array_unique($fileTargetIDs); if (!empty($fileTargetIDs)) { $query = \OC_DB::prepare('SELECT `fileid`, `path` FROM `*PREFIX*filecache` WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')'); $result = $query->execute(); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); } else { while ($row = $result->fetchRow()) { foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { $sharedPath = $shareData['file_target']; $sharedPath .= substr($path, strlen($row['path']) - 5); $sharePaths[$uid] = $sharedPath; } } } } if ($includeOwner) { $sharePaths[$ownerUser] = $path; } else { unset($sharePaths[$ownerUser]); } return $sharePaths; } return array('users' => array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare); }
/** * Find which users can access a shared item * @param string $path to the file * @param string $ownerUser owner of the file * @param boolean $includeOwner include owner to the list of users with access to the file * @param boolean $returnUserPaths Return an array with the user => path map * @return array * @note $path needs to be relative to user data dir, e.g. 'file.txt' * not '/admin/data/file.txt' */ public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false) { $shares = $sharePaths = $fileTargets = array(); $publicShare = false; $source = -1; $cache = false; $view = new \OC\Files\View('/' . $ownerUser . '/files'); if ($view->file_exists($path)) { $meta = $view->getFileInfo($path); $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); } else { // if the file doesn't exists yet we start with the parent folder $meta = $view->getFileInfo(dirname($path)); } if ($meta !== false) { $source = $meta['fileid']; $cache = new \OC\Files\Cache\Cache($meta['storage']); } while ($source !== -1) { // Fetch all shares with another user $query = \OC_DB::prepare('SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'); $result = $query->execute(array($source, self::SHARE_TYPE_USER)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { while ($row = $result->fetchRow()) { $shares[] = $row['share_with']; if ($returnUserPaths) { $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; } } } // We also need to take group shares into account $query = \OC_DB::prepare('SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'); $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { while ($row = $result->fetchRow()) { $usersInGroup = \OC_Group::usersInGroup($row['share_with']); $shares = array_merge($shares, $usersInGroup); if ($returnUserPaths) { foreach ($usersInGroup as $user) { $fileTargets[(int) $row['file_source']][$user] = $row; } } } } //check for public link shares if (!$publicShare) { $query = \OC_DB::prepare('SELECT `share_with` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'); $result = $query->execute(array($source, self::SHARE_TYPE_LINK)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { if ($result->fetchRow()) { $publicShare = true; } } } // let's get the parent for the next round $meta = $cache->get((int) $source); if ($meta !== false) { $source = (int) $meta['parent']; } else { $source = -1; } } // Include owner in list of users, if requested if ($includeOwner) { $shares[] = $ownerUser; if ($returnUserPaths) { $sharePaths[$ownerUser] = $path; } } if ($returnUserPaths) { $fileTargetIDs = array_keys($fileTargets); $fileTargetIDs = array_unique($fileTargetIDs); if (!empty($fileTargetIDs)) { $query = \OC_DB::prepare('SELECT `fileid`, `path` FROM `*PREFIX*filecache` WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')'); $result = $query->execute(); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { while ($row = $result->fetchRow()) { foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { $sharedPath = $shareData['file_target']; $sharedPath .= substr($path, strlen($row['path']) - 5); $sharePaths[$uid] = $sharedPath; } } } } return $sharePaths; } return array("users" => array_unique($shares), "public" => $publicShare); }
/** * @brief Find which users can access a shared item * @param $path to the file * @param $user owner of the file * @param include owner to the list of users with access to the file * @return array * @note $path needs to be relative to user data dir, e.g. 'file.txt' * not '/admin/data/file.txt' */ public static function getUsersSharingFile($path, $user, $includeOwner = false) { $shares = array(); $publicShare = false; $source = -1; $cache = false; $view = new \OC\Files\View('/' . $user . '/files/'); $meta = $view->getFileInfo(\OC\Files\Filesystem::normalizePath($path)); if ($meta !== false) { $source = $meta['fileid']; $cache = new \OC\Files\Cache\Cache($meta['storage']); } while ($source !== -1) { // Fetch all shares of this file path from DB $query = \OC_DB::prepare('SELECT `share_with` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ?'); $result = $query->execute(array($source, self::SHARE_TYPE_USER)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { while ($row = $result->fetchRow()) { $shares[] = $row['share_with']; } } // We also need to take group shares into account $query = \OC_DB::prepare('SELECT `share_with` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ?'); $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { while ($row = $result->fetchRow()) { $usersInGroup = \OC_Group::usersInGroup($row['share_with']); $shares = array_merge($shares, $usersInGroup); } } //check for public link shares if (!$publicShare) { $query = \OC_DB::prepare('SELECT `share_with` FROM `*PREFIX*share` WHERE `item_source` = ? AND `share_type` = ?'); $result = $query->execute(array($source, self::SHARE_TYPE_LINK)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); } else { if ($result->fetchRow()) { $publicShare = true; } } } // let's get the parent for the next round $meta = $cache->get((int) $source); if ($meta !== false) { $source = (int) $meta['parent']; } else { $source = -1; } } // Include owner in list of users, if requested if ($includeOwner) { $shares[] = $user; } return array("users" => array_unique($shares), "public" => $publicShare); }
/** * Create dummy data in the filecache for the given storage numeric id * * @param string $storageId storage id */ private function createData($storageId) { $cache = new \OC\Files\Cache\Cache($storageId); $cache->put('dummyfile.txt', array('size' => 5, 'mtime' => 12, 'mimetype' => 'text/plain')); }