From 497789cdd89b4a44844fa8409115dda745ff8c5b Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Fri, 12 Aug 2011 17:22:32 -0400 Subject: [PATCH] Clean up constructor for OC_Share, add some error handling for sharing files --- apps/files_sharing/ajax/share.php | 9 ++++- apps/files_sharing/js/share.js | 6 ++- apps/files_sharing/lib_share.php | 67 +++++++++++++++---------------- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/apps/files_sharing/ajax/share.php b/apps/files_sharing/ajax/share.php index 10a235e432..f0166f391d 100644 --- a/apps/files_sharing/ajax/share.php +++ b/apps/files_sharing/ajax/share.php @@ -8,7 +8,14 @@ $sources = explode(";", $_POST['sources']); $uid_shared_with = $_POST['uid_shared_with']; $permissions = $_POST['permissions']; foreach ($sources as $source) { - new OC_Share($source, $uid_shared_with, $permissions); + if ($source && OC_FILESYSTEM::file_exists($source) && OC_FILESYSTEM::is_readable($source)) { + $source = "/".OC_User::getUser()."/files".$source; + try { + new OC_Share($source, $uid_shared_with, $permissions); + } catch (Exception $exception) { + echo "false"; + } + } } ?> \ No newline at end of file diff --git a/apps/files_sharing/js/share.js b/apps/files_sharing/js/share.js index 2e284b868e..4ba45901cb 100644 --- a/apps/files_sharing/js/share.js +++ b/apps/files_sharing/js/share.js @@ -67,8 +67,10 @@ $(document).ready(function() { url: OC.linkTo('files_sharing','ajax/share.php'), cache: false, data: data, - success: function() { - addUser(uid_shared_with, permissions, false); + success: function(result) { + if (result !== 'false') { + addUser(uid_shared_with, permissions, false); + } } }); }); diff --git a/apps/files_sharing/lib_share.php b/apps/files_sharing/lib_share.php index 159c45e11e..336a087ddf 100644 --- a/apps/files_sharing/lib_share.php +++ b/apps/files_sharing/lib_share.php @@ -32,43 +32,42 @@ class OC_Share { const DELETE = 2; /** - * TODO notify user a file is being shared with them? * Share an item, adds an entry into the database - * @param string $item - * @param user item shared with $uid_shared_with + * @param $source The source location of the item + * @param $uid_shared_with The user to share the item with + * @param $permissions The permissions, use the constants WRITE and DELETE */ - public function __construct($source, $uid_shared_with, $permissions, $public = false) { - if ($source && OC_FILESYSTEM::file_exists($source) && OC_FILESYSTEM::is_readable($source)) { - $source = "/".OC_User::getUser()."/files".$source; - $uid_owner = OC_User::getUser(); - if ($public) { - // TODO create token for public file - $token = sha1("$uid_owner-$item"); - } else { - $query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)"); - $target = "/".$uid_shared_with."/files/Share/".basename($source); - $check = OC_DB::prepare("SELECT target FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ?"); - $result = $check->execute(array($target, $uid_shared_with))->fetchAll(); - // Check if target already exists for the user, if it does append a number to the name - if (count($result) > 0) { - if ($pos = strrpos($target, ".")) { - $name = substr($target, 0, $pos); - $ext = substr($target, $pos); - } else { - $name = $target; - $ext = ""; - } - $counter = 1; - while (count($result) > 0) { - $newTarget = $name."_".$counter.$ext; - $result = $check->execute(array($newTarget, $uid_shared_with))->fetchAll(); - $counter++; - } - $target = $newTarget; - } - $query->execute(array($uid_owner, $uid_shared_with, $source, $target, $permissions)); - } + public function __construct($source, $uid_shared_with, $permissions) { + $uid_owner = OC_User::getUser(); + $target = "/".$uid_shared_with."/files/Share/".basename($source); + // Check if this item is already shared with the user + $checkSource = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with = ?"); + $resultCheckSource = $checkSource->execute(array($source, $uid_shared_with))->fetchAll(); + // TODO Check if the source is inside a folder + if (count($resultCheckSource) > 0) { + throw new Exception("This item is already shared with the specified user"); } + // Check if target already exists for the user, if it does append a number to the name + $checkTarget = OC_DB::prepare("SELECT target FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ?"); + $resultCheckTarget = $checkTarget->execute(array($target, $uid_shared_with))->fetchAll(); + if (count($resultCheckTarget) > 0) { + if ($pos = strrpos($target, ".")) { + $name = substr($target, 0, $pos); + $ext = substr($target, $pos); + } else { + $name = $target; + $ext = ""; + } + $counter = 1; + while (count($result) > 0) { + $newTarget = $name."_".$counter.$ext; + $resultCheckTarget = $checkTarget->execute(array($newTarget, $uid_shared_with))->fetchAll(); + $counter++; + } + $target = $newTarget; + } + $query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)"); + $query->execute(array($uid_owner, $uid_shared_with, $source, $target, $permissions)); } /**