From 7e3b796de82f24f91930e96866c6c5f15ec04096 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Thu, 28 Jul 2011 20:05:18 -0400 Subject: [PATCH] More elegant solution for preparing IN ?s, thanks icewind --- apps/files_sharing/lib_share.php | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/apps/files_sharing/lib_share.php b/apps/files_sharing/lib_share.php index d0d7374794..df462c699e 100644 --- a/apps/files_sharing/lib_share.php +++ b/apps/files_sharing/lib_share.php @@ -79,18 +79,6 @@ class OC_SHARE { return $groups; } - /** - * Create a string of ?s based on the specified count - * @return A string to be placed inside the IN operator in a query for uid_shared_with - */ - private static function prepareIN($count) { - $questionMarks = "?"; - for($i = 1; $i < $count; $i++) { - $questionMarks .= ",?"; - } - return $questionMarks; - } - /** * Create a new entry in the database for a file inside a shared folder * @@ -143,7 +131,7 @@ class OC_SHARE { $folder = preg_replace('{(/)\1+}', "/", $folder); $length = strlen($folder); $userAndGroups = self::getUserAndGroups(); - $query = OC_DB::prepare("SELECT uid_owner, source, target FROM *PREFIX*sharing WHERE SUBSTR(source, 1, ?) = ? OR SUBSTR(target, 1, ?) = ? AND uid_shared_with IN(".self::prepareIN(count($userAndGroups)).")"); + $query = OC_DB::prepare("SELECT uid_owner, source, target FROM *PREFIX*sharing WHERE SUBSTR(source, 1, ?) = ? OR SUBSTR(target, 1, ?) = ? AND uid_shared_with IN(".substr(str_repeat(",?", count($userAndGroups)), 1).")"); return $query->execute(array_merge(array($length, $folder, $length, $folder), $userAndGroups))->fetchAll(); } @@ -157,7 +145,7 @@ class OC_SHARE { $target = rtrim($target, "/"); $target = preg_replace('{(/)\1+}', "/", $target); $userAndGroups = self::getUserAndGroups(); - $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with IN(".self::prepareIN(count($userAndGroups)).") LIMIT 1"); + $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with IN(".substr(str_repeat(",?", count($userAndGroups)), 1).") LIMIT 1"); // Prevent searching for user directory e.g. '/MTGap/files' $userDirectory = substr($target, 0, strpos($target, "files") + 5); while ($target != "" && $target != "/" && $target != "." && $target != $userDirectory) { @@ -186,7 +174,7 @@ class OC_SHARE { $target = rtrim($target, "/"); $target = preg_replace('{(/)\1+}', "/", $target); $userAndGroups = self::getUserAndGroups(); - $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with IN(".self::prepareIN(count($userAndGroups)).") LIMIT 1"); + $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with IN(".substr(str_repeat(",?", count($userAndGroups)), 1).") LIMIT 1"); $result = $query->execute(array_merge(array($target), $userAndGroups))->fetchAll(); if (count($result) > 0) { return $result[0]['source']; @@ -207,7 +195,7 @@ class OC_SHARE { */ public static function isWriteable($target) { $userAndGroups = self::getUserAndGroups(); - $query = OC_DB::prepare("SELECT is_writeable FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with IN(".self::prepareIN(count($userAndGroups)).") LIMIT 1"); + $query = OC_DB::prepare("SELECT is_writeable FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with IN(".substr(str_repeat(",?", count($userAndGroups)), 1).") LIMIT 1"); $result = $query->execute(array_merge(array($target), $userAndGroups))->fetchAll(); if (count($result) > 0) { return $result[0]['is_writeable'];