From 582dca4d91186723a142c29a344be5c6d060061e Mon Sep 17 00:00:00 2001
From: Michael Gapczynski <GapczynskiM@gmail.com>
Date: Tue, 16 Aug 2011 20:21:18 -0400
Subject: [PATCH] Initial merging of files_sharing and files_publiclink

---
 apps/files_sharing/js/share.js   | 30 +++++++-----
 apps/files_sharing/lib_share.php | 83 ++++++++++++++++++--------------
 2 files changed, 64 insertions(+), 49 deletions(-)

diff --git a/apps/files_sharing/js/share.js b/apps/files_sharing/js/share.js
index d9d64d96a0..18d8da8540 100644
--- a/apps/files_sharing/js/share.js
+++ b/apps/files_sharing/js/share.js
@@ -136,23 +136,29 @@ $(document).ready(function() {
 	
 	$('#makelink').live('change', function() {
 		if (this.checked) {
-			var data = 'path='+$('#dropdown').data('file')+'&expire=0';
+			var source = $('#dropdown').data('file');
+			var uid_shared_with = "public";
+			var permissions = 0;
+			var data = 'sources='+encodeURIComponent(source)+'&uid_shared_with='+encodeURIComponent(uid_shared_with)+'&permissions='+encodeURIComponent(permissions);
 			$.ajax({
-				type: 'GET',
-				url: OC.linkTo('files_publiclink','ajax/makelink.php'),
+				type: 'POST',
+				url: OC.linkTo('files_sharing','ajax/share.php'),
 				cache: false,
 				data: data,
-				success: function(token) {
-					if (token) {
+				success: function(result) {
+					if (result !== 'false') {
+						var token = 1234;
 						showPublicLink(token);
 					}
 				}
 			});
 		} else {
-			var data = 'token='+$('#link').data('token');
+			var source = $('#dropdown').data('file');
+			var uid_shared_with = "public";
+			var data = 'source='+encodeURIComponent(source)+'&uid_shared_with='+encodeURIComponent(uid_shared_with);
 			$.ajax({
 				type: 'GET',
-				url: OC.linkTo('files_publiclink','ajax/deletelink.php'),
+				url: OC.linkTo('files_sharing','ajax/unshare.php'),
 				cache: false,
 				data: data,
 				success: function(){
@@ -207,11 +213,11 @@ function createDropdown(filename, files) {
 			});
 		}
 	});
-	$.getJSON(OC.linkTo('files_publiclink', 'ajax/getlink.php'), { path: files }, function(token) {
-		if (token) {
-			showPublicLink(token);
-		}
-	});
+// 	$.getJSON(OC.linkTo('files_publiclink', 'ajax/getlink.php'), { path: files }, function(token) {
+// 		if (token) {
+// 			showPublicLink(token);
+// 		}
+// 	});
 	$('#dropdown').show('blind');
 	$('#share_with').chosen();
 }
diff --git a/apps/files_sharing/lib_share.php b/apps/files_sharing/lib_share.php
index a42fed3e76..1bf60f3b1d 100644
--- a/apps/files_sharing/lib_share.php
+++ b/apps/files_sharing/lib_share.php
@@ -30,6 +30,9 @@ class OC_Share {
 
 	const WRITE = 1;
 	const DELETE = 2;
+	const PUBLICLINK = "public";
+
+	private $token;
       
 	/**
 	 * Share an item, adds an entry into the database
@@ -39,48 +42,54 @@ class OC_Share {
 	 */
 	public function __construct($source, $uid_shared_with, $permissions) {
 		$uid_owner = OC_User::getUser();
-		if (OC_Group::groupExists($uid_shared_with)) {
-			$gid = $uid_shared_with;
-			$uid_shared_with = OC_Group::usersInGroup($gid);
-			// Remove the owner from the list of users in the group
-			$uid_shared_with = array_diff($uid_shared_with, array($uid_owner));
-		} else if (OC_User::userExists($uid_shared_with)) {
-			$gid = null;
-			$uid_shared_with = array($uid_shared_with);
+		$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
+		if ($uid_shared_with == self::PUBLICLINK) {
+			$token = sha1("$uid_shared_with-$source");
+			$query->execute(array($uid_owner, self::PUBLICLINK, $source, $token, $permissions));
+			$this->token = $token;
 		} else {
-			throw new Exception($uid_shared_with." is not a user");
-		}
-		foreach ($uid_shared_with as $uid) {
-			// 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 ".self::getUsersAndGroups($uid));
-			$resultCheckSource = $checkSource->execute(array($source, $uid))->fetchAll();
-			// TODO Check if the source is inside a folder
-			if (count($resultCheckSource) > 0 && !isset($gid)) {
-				throw new Exception("This item is already shared with ".$uid);
+			if (OC_Group::groupExists($uid_shared_with)) {
+				$gid = $uid_shared_with;
+				$uid_shared_with = OC_Group::usersInGroup($gid);
+				// Remove the owner from the list of users in the group
+				$uid_shared_with = array_diff($uid_shared_with, array($uid_owner));
+			} else if (OC_User::userExists($uid_shared_with)) {
+				$gid = null;
+				$uid_shared_with = array($uid_shared_with);
+			} else {
+				throw new Exception($uid_shared_with." is not a user");
 			}
-			// Check if the target already exists for the user, if it does append a number to the name
-			$target = "/".$uid."/files/Shared/".basename($source);
-			if (self::getSource($target)) {
-				if ($pos = strrpos($target, ".")) {
-					$name = substr($target, 0, $pos);
-					$ext = substr($target, $pos);
-				} else {
-					$name = $target;
-					$ext = "";
+			foreach ($uid_shared_with as $uid) {
+				// 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 ".self::getUsersAndGroups($uid));
+				$resultCheckSource = $checkSource->execute(array($source, $uid))->fetchAll();
+				// TODO Check if the source is inside a folder
+				if (count($resultCheckSource) > 0 && !isset($gid)) {
+					throw new Exception("This item is already shared with ".$uid);
 				}
-				$counter = 1;
-				while ($checkTarget !== false) {
-					$newTarget = $name."_".$counter.$ext;
-					$checkTarget = self::getSource($newTarget);
-					$counter++;
+				// Check if the target already exists for the user, if it does append a number to the name
+				$target = "/".$uid."/files/Shared/".basename($source);
+				if (self::getSource($target)) {
+					if ($pos = strrpos($target, ".")) {
+						$name = substr($target, 0, $pos);
+						$ext = substr($target, $pos);
+					} else {
+						$name = $target;
+						$ext = "";
+					}
+					$counter = 1;
+					while ($checkTarget !== false) {
+						$newTarget = $name."_".$counter.$ext;
+						$checkTarget = self::getSource($newTarget);
+						$counter++;
+					}
+					$target = $newTarget;
 				}
-				$target = $newTarget;
-			}
-			if (isset($gid)) {
-				$uid = $uid."@".$gid;
+				if (isset($gid)) {
+					$uid = $uid."@".$gid;
+				}
+				$query->execute(array($uid_owner, $uid, $source, $target, $permissions));
 			}
-			$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
-			$query->execute(array($uid_owner, $uid, $source, $target, $permissions));
 		}
 	}
 
-- 
GitLab