<commit_before><?php
/**
 * ownCloud
 *
 * @author <NAME>
 * @copyright 2011 <NAME> <EMAIL>
 *
 * 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 {

	const WRITE = 1;
	const DELETE = 2;
	const UNSHARED = -1;
	const PUBLICLINK = "public";

	private $token;
      
	/**
	 * Share an item, adds an entry into the database
	 * @param $source The source location of the item
	 * @param $uid_shared_with The user or group to share the item with
	 * @param $permissions The permissions, use the constants WRITE and DELETE
	 */
	public function __construct($source, $uid_shared_with, $permissions) {
		$uid_owner = OC_User::getUser();
		$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 {
			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");
			}
			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);
				}
				// 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;
				}
				if (isset($gid)) {
					$uid = $uid."@".$gid;
				}
				$query->execute(array($uid_owner, $uid, $source, $target, $permissions));<commit_msg>Clear the folder size cache for the 'Shared' folder when a new item is shared<commit_after><?php
/**
 * ownCloud
 *
 * @author <NAME>
 * @copyright 2011 <NAME> <EMAIL>
 *
 * 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 {

	const WRITE = 1;
	const DELETE = 2;
	const UNSHARED = -1;
	const PUBLICLINK = "public";

	private $token;
      
	/**
	 * Share an item, adds an entry into the database
	 * @param $source The source location of the item
	 * @param $uid_shared_with The user or group to share the item with
	 * @param $permissions The permissions, use the constants WRITE and DELETE
	 */
	public function __construct($source, $uid_shared_with, $permissions) {
		$uid_owner = OC_User::getUser();
		$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 {
			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");
			}
			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);
				}
				// Check if the target already exists for the user, if it does append a number to the name
				$sharedFolder = "/".$uid."/files/Shared";
				$target = $sharedFolder."/".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;
				}
				if (isset($gid)) {
					$uid = $uid."@".$gid;
				}
				$query->execute(array($uid_owner, $uid, $source, $target, $permissions));
				// Clear the folder size cache for the 'Shared' folder
				$clearFolderSize = OC_DB::prepare("DELETE FROM *PREFIX*foldersize WHERE path = ?");
				$clearFolderSize->execute(array($sharedFolder));
{'fraction_non_alphanumeric': 0.1339803012746234, 'fraction_numerical': 0.0034762456546929316, 'mean_word_length': 3.5728476821192054, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3606139', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2083, 'n_words': 703}