From 1d7564dc2f4ec9e06f9047846cd6bf023a1c26ed Mon Sep 17 00:00:00 2001
From: Bart Visscher <bartv@thisnet.nl>
Date: Fri, 21 Feb 2014 22:58:29 +0100
Subject: [PATCH] Only check for existence of shared files when doing shared
 storage setup

The getItemsSharedWith function also retrieves related information,
resulting in work that isn't used here.
---
 apps/files_sharing/lib/sharedstorage.php |  2 +-
 lib/public/share.php                     | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php
index b922654e5e..18c8a4f42a 100644
--- a/apps/files_sharing/lib/sharedstorage.php
+++ b/apps/files_sharing/lib/sharedstorage.php
@@ -394,7 +394,7 @@ class Shared extends \OC\Files\Storage\Common {
 
 	public static function setup($options) {
 		if (!\OCP\User::isLoggedIn() || \OCP\User::getUser() != $options['user']
-			|| \OCP\Share::getItemsSharedWith('file')
+			|| \OCP\Share::hasFilesSharedWith()
 		) {
 			$user_dir = $options['user_dir'];
 			\OC\Files\Filesystem::mount('\OC\Files\Storage\Shared',
diff --git a/lib/public/share.php b/lib/public/share.php
index ebc555dba5..8cfe7417be 100644
--- a/lib/public/share.php
+++ b/lib/public/share.php
@@ -243,6 +243,29 @@ class Share {
 		return array("users" => array_unique($shares), "public" => $publicShare);
 	}
 
+	public static function hasFilesSharedWith() {
+		if (!self::isEnabled()) {
+			return false;
+		}
+		$shareWith = \OC_User::getUser();
+		$where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid`';
+		$where .= ' WHERE `file_target` IS NOT NULL';
+		$queryArgs = array();
+		$where .= ' AND `share_type` IN (?,?,?)';
+		$queryArgs[] = self::SHARE_TYPE_USER;
+		$queryArgs[] = self::SHARE_TYPE_GROUP;
+		$queryArgs[] = self::$shareTypeGroupUserUnique;
+		$userAndGroups = array_merge(array($shareWith), \OC_Group::getUserGroups($shareWith));
+		$placeholders = join(',', array_fill(0, count($userAndGroups), '?'));
+		$where .= ' AND `share_with` IN ('.$placeholders.')';
+		$queryArgs = array_merge($queryArgs, $userAndGroups);
+		// Don't include own group shares
+		$where .= ' AND `uid_owner` != ?';
+		$queryArgs[] = $shareWith;
+		$result = \OC_DB::executeAudited('SELECT COUNT(*) FROM `*PREFIX*share` '.$where, $queryArgs);
+		return $result->fetchOne() > 0;
+	}
+
 	/**
 	 * Get the items of item type shared with the current user
 	 * @param string Item type
-- 
GitLab