2011-06-12 00:14:24 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Michael Gapczynski
|
|
|
|
* @copyright 2011 Michael Gapczynski GapczynskiM@gmail.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class manages shared items within the database.
|
|
|
|
*/
|
|
|
|
class OC_SHARE {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-07-06 20:12:29 +04:00
|
|
|
public function __construct($source, $uid_shared_with, $permissions, $public = false) {
|
|
|
|
if ($source && OC_FILESYSTEM::file_exists($source) && OC_FILESYSTEM::is_readable($source)) {
|
2011-06-12 00:14:24 +04:00
|
|
|
$uid_owner = $_SESSION['user_id'];
|
|
|
|
if ($public) {
|
|
|
|
// TODO create token for public file
|
|
|
|
$token = sha1("$uid_owner-$item");
|
|
|
|
} else {
|
2011-07-06 20:12:29 +04:00
|
|
|
$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
|
|
|
|
$sourceLocalPath = substr($source, strlen("/".$uid_owner."/files/"));;
|
2011-06-12 00:14:24 +04:00
|
|
|
foreach ($uid_shared_with as $uid) {
|
2011-07-06 20:12:29 +04:00
|
|
|
// TODO check to see if target already exists in database
|
|
|
|
$target = "/".$uid."/files/Share/".$sourceLocalPath;
|
|
|
|
$query->execute(array($uid_owner, $uid, $source, $target, $permissions));
|
2011-06-12 00:14:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-06 20:12:29 +04:00
|
|
|
* Change is writeable for the specified item and user
|
|
|
|
* @param $source
|
|
|
|
* @param $uid_shared_with
|
2011-07-06 23:17:03 +04:00
|
|
|
* @param $is_writeable
|
2011-06-12 00:14:24 +04:00
|
|
|
*/
|
2011-07-06 20:12:29 +04:00
|
|
|
public static function setIsWriteable($source, $uid_shared_with, $is_writeable) {
|
2011-07-08 02:25:34 +04:00
|
|
|
$query = OC_DB::prepare("SELECT is_writeable FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with = ? LIMIT 1");
|
|
|
|
$result = $query->execute(array($source, $uid_shared_with))->fetchAll();
|
|
|
|
if (count($result) > 0) {
|
|
|
|
$query = OC_DB::prepare("UPDATE *PREFIX*sharing SET is_writeable = ? WHERE source COLLATE latin1_bin LIKE ? AND uid_shared_with = ? AND uid_owner = ?");
|
|
|
|
$query->execute(array($is_writeable, $source."%", $uid_shared_with, $_SESSION['user_id']));
|
|
|
|
} else {
|
2011-07-06 20:12:29 +04:00
|
|
|
// A new entry is added to the database when a file within a shared folder is set new a value for is_writeable, but not the entire folder
|
|
|
|
$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
|
2011-07-08 02:25:34 +04:00
|
|
|
$folders = OC_SHARE::getParentFolders($source);
|
|
|
|
$target = $folders['target'].substr($source, strlen($folders['source']));
|
2011-07-06 20:12:29 +04:00
|
|
|
$query->execute(array($_SESSION['user_id'], $uid_shared_with, $source, $target, $is_writeable));
|
|
|
|
}
|
2011-06-12 00:14:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-06 20:12:29 +04:00
|
|
|
* Check if the specified item is writeable for the user
|
2011-07-06 23:17:03 +04:00
|
|
|
* @param $target
|
2011-07-06 20:12:29 +04:00
|
|
|
* @return true or false
|
2011-06-12 00:14:24 +04:00
|
|
|
*/
|
2011-07-06 20:12:29 +04:00
|
|
|
public static function isWriteable($target) {
|
2011-07-08 02:25:34 +04:00
|
|
|
$query = OC_DB::prepare("SELECT is_writeable FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ? LIMIT 1");
|
2011-07-06 20:12:29 +04:00
|
|
|
$result = $query->execute(array($target, $_SESSION['user_id']))->fetchAll();
|
|
|
|
if (count($result) > 0) {
|
|
|
|
return $result[0]['is_writeable'];
|
|
|
|
} else {
|
2011-07-08 02:25:34 +04:00
|
|
|
// Check if the folder is writeable
|
2011-07-06 20:12:29 +04:00
|
|
|
$folders = OC_SHARE::getParentFolders($target, false);
|
|
|
|
$result = $query->execute(array($folders['target'], $_SESSION['user_id']))->fetchAll();
|
|
|
|
if (count($result) > 0) {
|
|
|
|
return $result[0]['is_writeable'];
|
|
|
|
} else {
|
2011-07-06 23:17:03 +04:00
|
|
|
return false;
|
2011-07-06 20:12:29 +04:00
|
|
|
}
|
|
|
|
}
|
2011-06-12 00:14:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unshare the item, removes it from all users specified
|
|
|
|
* @param array $uid_shared_with
|
|
|
|
*/
|
2011-07-06 23:17:03 +04:00
|
|
|
public static function unshare($source, $uid_shared_with) {
|
2011-07-08 22:58:35 +04:00
|
|
|
$query = OC_DB::prepare("DELETE FROM *PREFIX*sharing WHERE source COLLATE latin1_bin LIKE ? AND uid_shared_with = ? AND uid_owner = ?");
|
2011-06-12 00:14:24 +04:00
|
|
|
foreach ($uid_shared_with as $uid) {
|
2011-07-08 23:03:05 +04:00
|
|
|
$query->execute(array($source."%", $uid, $_SESSION['user_id']));
|
2011-06-12 00:14:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 23:17:03 +04:00
|
|
|
/**
|
|
|
|
* Unshare the item from the current user - used when the user deletes the item
|
|
|
|
* @param $target
|
|
|
|
*/
|
|
|
|
public static function unshareFromSelf($target) {
|
|
|
|
$query = OC_DB::prepare("DELETE FROM *PREFIX*sharing WHERE target COLLATE latin1_bin LIKE ? AND uid_shared_with = ?");
|
2011-07-07 04:16:29 +04:00
|
|
|
$query->execute(array($target."%", $_SESSION['user_id']));
|
2011-07-06 23:17:03 +04:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:12:29 +04:00
|
|
|
/**
|
|
|
|
* Set the source location to a new value
|
|
|
|
* @param $oldSource The current source location
|
2011-07-06 23:17:03 +04:00
|
|
|
* @param $newTarget The new source location
|
2011-07-06 20:12:29 +04:00
|
|
|
*/
|
|
|
|
public static function setSource($oldSource, $newSource) {
|
|
|
|
$query = OC_DB::prepare("UPDATE *PREFIX*sharing SET source = REPLACE(source, ?, ?) WHERE uid_owner = ?");
|
|
|
|
$query->execute(array($oldSource, $newSource, $_SESSION['user_id']));
|
|
|
|
}
|
|
|
|
|
2011-06-16 22:40:21 +04:00
|
|
|
/**
|
|
|
|
* Get the source location of the target item
|
2011-06-26 06:00:52 +04:00
|
|
|
* @param $target
|
2011-06-16 22:40:21 +04:00
|
|
|
* @return source path
|
|
|
|
*/
|
|
|
|
public static function getSource($target) {
|
2011-07-10 22:41:27 +04:00
|
|
|
// Remove any duplicate or trailing '/'
|
2011-07-05 19:56:02 +04:00
|
|
|
$target = rtrim($target, "/");
|
2011-07-10 22:41:27 +04:00
|
|
|
$target = preg_replace('{(/)\1+}', "/", $target);
|
2011-07-03 19:43:49 +04:00
|
|
|
$query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ? LIMIT 1");
|
2011-06-26 06:00:52 +04:00
|
|
|
$result = $query->execute(array($target, $_SESSION['user_id']))->fetchAll();
|
2011-06-25 03:38:39 +04:00
|
|
|
if (count($result) > 0) {
|
2011-06-26 06:00:52 +04:00
|
|
|
return $result[0]['source'];
|
2011-06-25 03:38:39 +04:00
|
|
|
} else {
|
2011-07-06 20:12:29 +04:00
|
|
|
$folders = OC_SHARE::getParentFolders($target, false);
|
2011-07-13 04:18:08 +04:00
|
|
|
if ($folders == false) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $folders['source'].substr($target, strlen($folders['target']));
|
|
|
|
}
|
2011-07-06 20:12:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getParentFolders($path, $isSource = true) {
|
2011-07-10 22:41:27 +04:00
|
|
|
// Remove any duplicate or trailing '/'
|
2011-07-06 20:12:29 +04:00
|
|
|
$path = rtrim($path, "/");
|
2011-07-10 22:41:27 +04:00
|
|
|
$path = preg_replace('{(/)\1+}', "/", $path);
|
2011-07-06 20:12:29 +04:00
|
|
|
if ($isSource) {
|
|
|
|
$query = OC_DB::prepare("SELECT target FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with = ? LIMIT 1");
|
|
|
|
} else {
|
|
|
|
$query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ? LIMIT 1");
|
|
|
|
}
|
2011-07-08 23:23:41 +04:00
|
|
|
// Prevent searching for user directory e.g. '/MTGap/files'
|
|
|
|
$userDirectory = substr($path, 0, strpos($path, "files") + 5);
|
|
|
|
while ($path != "" && $path != "/" && $path != "." && $path != $userDirectory) {
|
2011-07-06 20:12:29 +04:00
|
|
|
$result = $query->execute(array($path, $_SESSION['user_id']))->fetchAll();
|
|
|
|
if (count($result) > 0) {
|
|
|
|
break;
|
2011-06-26 06:00:52 +04:00
|
|
|
} else {
|
2011-07-06 20:12:29 +04:00
|
|
|
// Check if the parent directory of this target is shared
|
|
|
|
$path = dirname($path);
|
2011-06-26 06:00:52 +04:00
|
|
|
}
|
2011-06-25 03:20:08 +04:00
|
|
|
}
|
2011-07-06 20:12:29 +04:00
|
|
|
if (count($result) > 0) {
|
|
|
|
if ($isSource) {
|
|
|
|
$sourceFolder = $path;
|
|
|
|
$targetFolder = $result[0]['target'];
|
|
|
|
} else {
|
|
|
|
$sourceFolder = $result[0]['source'];
|
|
|
|
$targetFolder = $path;
|
|
|
|
}
|
|
|
|
// Return both the source folder and the target folder
|
|
|
|
return array("source" => $sourceFolder, "target" => $targetFolder);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-16 22:40:21 +04:00
|
|
|
}
|
2011-07-09 02:21:20 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the files within a shared folder that have their own entry for the purpose of name, location, or permissions that differ from the folder itself
|
|
|
|
* @param $sourceFolder The source folder of the files to look for
|
|
|
|
* @return array An array of the files if any
|
|
|
|
*/
|
|
|
|
public static function getSharedFilesIn($sourceFolder) {
|
|
|
|
// Append '/' in order to filter out the folder itself
|
|
|
|
$sourceFolder = $sourceFolder."/";
|
|
|
|
$query = OC_DB::prepare("SELECT source, target FROM *PREFIX*sharing WHERE source COLLATE latin1_bin LIKE ? AND uid_shared_with = ?");
|
|
|
|
return $query->execute(array($sourceFolder."%", $_SESSION['user_id']))->fetchAll();
|
|
|
|
}
|
2011-06-16 22:40:21 +04:00
|
|
|
|
2011-06-26 06:00:52 +04:00
|
|
|
/**
|
|
|
|
* Set the target location to a new value
|
|
|
|
* @param $oldTarget The current target location
|
|
|
|
* @param $newTarget The new target location
|
|
|
|
*/
|
|
|
|
public static function setTarget($oldTarget, $newTarget) {
|
2011-07-08 02:25:34 +04:00
|
|
|
$query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ? LIMIT 1");
|
|
|
|
$result = $query->execute(array($oldTarget, $_SESSION['user_id']))->fetchAll();
|
|
|
|
if (count($result) > 0) {
|
|
|
|
$query = OC_DB::prepare("UPDATE *PREFIX*sharing SET target = REPLACE(target, ?, ?) WHERE uid_shared_with = ?");
|
|
|
|
$query->execute(array($oldTarget, $newTarget, $_SESSION['user_id']));
|
|
|
|
} else {
|
2011-07-06 20:12:29 +04:00
|
|
|
// A new entry is added to the database when a file within a shared folder is renamed or is moved outside the original target folder
|
|
|
|
$query = OC_DB::prepare("SELECT uid_owner, is_writeable FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with = ? LIMIT 1");
|
|
|
|
$folders = OC_SHARE::getParentFolders($oldTarget, false);
|
2011-07-14 20:40:12 +04:00
|
|
|
$result = $query->execute(array($folders['source'], $_SESSION['user_id']))->fetchAll();
|
2011-07-06 20:12:29 +04:00
|
|
|
$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
|
2011-07-08 02:25:34 +04:00
|
|
|
$source = $folders['source'].substr($oldTarget, strlen($folders['target']));
|
|
|
|
$query->execute(array($result[0]['uid_owner'], $_SESSION['user_id'], $source, $newTarget, $result[0]['is_writeable']));
|
2011-07-06 20:12:29 +04:00
|
|
|
}
|
2011-06-26 06:00:52 +04:00
|
|
|
}
|
|
|
|
|
2011-06-12 00:14:24 +04:00
|
|
|
/**
|
|
|
|
* Get all items the user is sharing
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getSharedItems() {
|
|
|
|
$query = OC_DB::prepare("SELECT * FROM *PREFIX*sharing WHERE uid_owner = ?");
|
|
|
|
return $query->execute(array($_SESSION['user_id']))->fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all items shared with the user
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getItemsSharedWith() {
|
|
|
|
$query = OC_DB::prepare("SELECT * FROM *PREFIX*sharing WHERE uid_shared_with = ?");
|
|
|
|
return $query->execute(array($_SESSION['user_id']))->fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|