diff --git a/ajax/sessionController.php b/ajax/sessionController.php index b3db8bb8..285ac262 100644 --- a/ajax/sessionController.php +++ b/ajax/sessionController.php @@ -15,12 +15,15 @@ namespace OCA\Documents; class SessionController extends Controller{ public static function joinAsGuest($args){ - $uid = self::preDispatchGuest(); - $uid = substr(@$_POST['name'], 0, 16) .' '. $uid; - $token = @$args['token']; + $postfix = self::preDispatchGuest(); + + $uid = Helper::getArrayValueByKey($_POST, 'name'); + $guestUid = substr($uid, 0, 16) .' '. $postfix; + try { - $file = File::getByShareToken($token); - self::join($uid, $file); + $token = Helper::getArrayValueByKey($args, 'token'); + $fileId = File::getIdByShareToken($token); + self::join($guestUid, $fileId); } catch (\Exception $e){ Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage()); \OCP\JSON::error(); @@ -33,13 +36,15 @@ class SessionController extends Controller{ $fileId = intval(@$args['file_id']); try { - $file = new File($fileId); - - if ($file->getPermissions() & \OCP\PERMISSION_UPDATE) { - self::join($uid, $file); + $view = \OC\Files\Filesystem::getView(); + $path = $view->getPath($fileId); + + if ($view->isUpdatable($path)) { + self::join($uid, $fileId); } else { + $info = $view->getFileInfo(); \OCP\JSON::success(array( - 'permissions' => $file->getPermissions(), + 'permissions' => $info['permissions'], 'id' => $fileId )); } @@ -51,8 +56,8 @@ class SessionController extends Controller{ } } - protected static function join($uid, $file){ - $session = Db_Session::start($uid, $file); + protected static function join($uid, $fileId){ + $session = Db_Session::start($uid, $fileId); \OCP\JSON::success($session); exit(); } diff --git a/lib/db/session.php b/lib/db/session.php index f892859f..40c99f43 100644 --- a/lib/db/session.php +++ b/lib/db/session.php @@ -35,7 +35,8 @@ class Db_Session extends \OCA\Documents\Db { * @return array * @throws \Exception */ - public static function start($uid, File $file){ + public static function start($uid, $fileId){ + $file = new File($fileId); list($ownerView, $path) = $file->getOwnerViewAndPath(); // Create a directory to store genesis diff --git a/lib/file.php b/lib/file.php index 349b5223..0f132beb 100644 --- a/lib/file.php +++ b/lib/file.php @@ -47,6 +47,23 @@ class File { } } + public static function getIdByShareToken($token){ + $linkItem = \OCP\Share::getShareByToken($token, false); + if (is_array($linkItem) && isset($linkItem['uid_owner'])) { + // seems to be a valid share + $rootLinkItem = \OCP\Share::resolveReShare($linkItem); + $fileOwner = $rootLinkItem['uid_owner']; + } else { + throw new \Exception('This file was probably unshared'); + } + + if (!isset($rootLinkItem['path']) && isset($rootLinkItem['file_target'])){ + $rootLinkItem['path'] = $rootLinkItem['file_target']; + } + + return $rootLinkItem['file_source']; + } + public static function getByShareToken($token){ $linkItem = \OCP\Share::getShareByToken($token, false); if (is_array($linkItem) && isset($linkItem['uid_owner'])) {