diff --git a/apps/calendar/appinfo/app.php b/apps/calendar/appinfo/app.php index 5c05c57bca..9ae255853d 100644 --- a/apps/calendar/appinfo/app.php +++ b/apps/calendar/appinfo/app.php @@ -10,6 +10,8 @@ OC::$CLASSPATH['OC_Calendar_Share'] = 'apps/calendar/lib/share.php'; OC::$CLASSPATH['OC_Search_Provider_Calendar'] = 'apps/calendar/lib/search.php'; OC::$CLASSPATH['OC_Calendar_Export'] = 'apps/calendar/lib/export.php'; OC::$CLASSPATH['OC_Calendar_Import'] = 'apps/calendar/lib/import.php'; +OC::$CLASSPATH['OC_Share_Backend_Calendar'] = 'apps/calendar/lib/share/calendar.php'; +OC::$CLASSPATH['OC_Share_Backend_Event'] = 'apps/calendar/lib/share/event.php'; //General Hooks OCP\Util::connectHook('OC_User', 'post_createUser', 'OC_Calendar_Hooks', 'createUser'); OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OC_Calendar_Hooks', 'deleteUser'); @@ -34,3 +36,5 @@ OCP\App::addNavigationEntry( array( 'icon' => OCP\Util::imagePath( 'calendar', 'icon.svg' ), 'name' => $l->t('Calendar'))); OC_Search::registerProvider('OC_Search_Provider_Calendar'); +OCP\Share::registerBackend('calendar', 'OC_Share_Backend_Calendar'); +OCP\Share::registerBackend('event', 'OC_Share_Backend_Event'); diff --git a/apps/calendar/js/loader.js b/apps/calendar/js/loader.js index b28d19ab00..253abafc42 100644 --- a/apps/calendar/js/loader.js +++ b/apps/calendar/js/loader.js @@ -173,7 +173,7 @@ Calendar_Import={ } $(document).ready(function(){ if(typeof FileActions !== 'undefined'){ - FileActions.register('text/calendar','importCalendar', '', Calendar_Import.Dialog.open); + FileActions.register('text/calendar','importCalendar', FileActions.PERMISSION_READ, '', Calendar_Import.Dialog.open); FileActions.setDefault('text/calendar','importCalendar'); }; }); diff --git a/apps/calendar/lib/share.php b/apps/calendar/lib/share.php index 4fe8817140..e5ffc04143 100644 --- a/apps/calendar/lib/share.php +++ b/apps/calendar/lib/share.php @@ -18,19 +18,16 @@ class OC_Calendar_Share{ * @return: array $return - information about calendars */ public static function allSharedwithuser($userid, $type, $active=null, $permission=null){ - $group_where = self::group_sql(OC_Group::getUserGroups($userid)); - $permission_where = self::permission_sql($permission); - if($type == self::CALENDAR){ - $active_where = self::active_sql($active); - }else{ - $active_where = ''; - } - $stmt = OCP\DB::prepare("SELECT * FROM *PREFIX*calendar_share_" . $type . " WHERE ((share = ? AND sharetype = 'user') " . $group_where . ") AND owner <> ? " . $permission_where . " " . $active_where); - $result = $stmt->execute(array($userid, $userid)); - $return = array(); - while( $row = $result->fetchRow()){ - $return[] = $row; + $format = OC_Share_Backend_Calendar::FORMAT_CALENDAR; + if ($type == self::EVENT) { + $format = OC_Share_Backend_Event::FORMAT_EVENT; } + $return = OCP\Share::getItemsSharedWith($type, + $format, + array( + 'active' => $active, + 'permissions' => $permission, + )); return $return; } /** diff --git a/apps/calendar/lib/share/calendar.php b/apps/calendar/lib/share/calendar.php new file mode 100644 index 0000000000..7f49829241 --- /dev/null +++ b/apps/calendar/lib/share/calendar.php @@ -0,0 +1,111 @@ + +* +* 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 . +*/ + +class OC_Share_Backend_Calendar implements OCP\Share_Backend_Collection { + const FORMAT_CALENDAR = 1; + + /** + * @brief Get the source of the item to be stored in the database + * @param string Item + * @param string Owner of the item + * @return mixed|array|false Source + * + * Return an array if the item is file dependent, the array needs two keys: 'item' and 'file' + * Return false if the item does not exist for the user + * + * The formatItems() function will translate the source returned back into the item + */ + public function isValidSource($itemSource, $uidOwner) { + $calendar = OC_Calendar_App::getCalendar( $itemSource ); + if ($calendar || $calendar['userid'] != $uidOwner) { + return false; + } + return true; + } + + /** + * @brief Get a unique name of the item for the specified user + * @param string Item + * @param string|false User the item is being shared with + * @param array|null List of similar item names already existing as shared items + * @return string Target name + * + * This function needs to verify that the user does not already have an item with this name. + * If it does generate a new name e.g. name_# + */ + public function generateTarget($itemSource, $shareWith, $exclude = null) { + $calendar = OC_Calendar_App::getCalendar( $itemSource ); + $user_calendars = array(); + foreach(OC_Contacts_Addressbook::all($uid) as $user_calendar) { + $user_calendars[] = $user_calendar['displayname']; + } + $name = $calendar['userid']."'s ".$calendar['displayname']; + $suffix = ''; + while (in_array($name.$suffix, $user_calendars)) { + $suffix++; + } + + return $name.$suffix; + } + + /** + * @brief Converts the shared item sources back into the item in the specified format + * @param array Shared items + * @param int Format + * @return ? + * + * The items array is a 3-dimensional array with the item_source as the first key and the share id as the second key to an array with the share info. + * The key/value pairs included in the share info depend on the function originally called: + * If called by getItem(s)Shared: id, item_type, item, item_source, share_type, share_with, permissions, stime, file_source + * If called by getItem(s)SharedWith: id, item_type, item, item_source, item_target, share_type, share_with, permissions, stime, file_source, file_target + * This function allows the backend to control the output of shared items with custom formats. + * It is only called through calls to the public getItem(s)Shared(With) functions. + */ + public function formatItems($items, $format, $parameters = null) { + $calendars = array(); + if ($format == self::FORMAT_CALENDAR) { + foreach ($items as $item) { + $calendar = OC_Calendar_App::getCalendar($item['item_source'], false); + // TODO: really check $parameters['permissions'] == 'rw'/'r' + if ($parameters['permissions'] == 'rw') { + continue; // TODO + } + $calendar['displaynamename'] = $item['item_target']; + $calendar['calendarid'] = $calendar['id']; + $calendar['owner'] = $calendar['userid']; + $calendars[] = $calendar; + } + } + return $calendars; + } + + public function getChildren($itemSource) { + $query = OCP\DB::prepare('SELECT id FROM *PREFIX*calendar_objects WHERE calendarid = ?'); + $result = $query->execute(array($itemSource)); + $sources = array(); + while ($object = $result->fetchRow()) { + $sources[] = $object['id']; + } + return $sources; + } + +} \ No newline at end of file diff --git a/apps/calendar/lib/share/event.php b/apps/calendar/lib/share/event.php new file mode 100644 index 0000000000..5bb72ee6c9 --- /dev/null +++ b/apps/calendar/lib/share/event.php @@ -0,0 +1,40 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Share_Backend_Event implements OCP\Share_Backend { + + const FORMAT_EVENT = 0; + + private static $event; + + public function isValidSource($itemSource, $uidOwner) { + self::$event = OC_Calendar_Object::find($itemSource); + if (self::$event) { + return true; + } + return false; + } + + public function generateTarget($itemSource, $shareWith, $exclude = null) { + // TODO Get default calendar and check for conflicts + return self::$event['summary']; + } + + public function formatItems($items, $format, $parameters = null) { + $events = array(); + if ($format == self::FORMAT_EVENT) { + foreach ($items as $item) { + $event = OC_Calendar_Object::find($item['item_source']); + $event['summary'] = $item['item_target']; + $events[] = $event; + } + } + return $events; + } + +} diff --git a/apps/calendar/lib/share_backend.php b/apps/calendar/lib/share_backend.php new file mode 100644 index 0000000000..f937c0d1c3 --- /dev/null +++ b/apps/calendar/lib/share_backend.php @@ -0,0 +1,44 @@ +. +*/ + +class OC_Share_Backend_Calendar extends OCP\Share_Backend { + + public function getSource($item, $uid) { + $query = OCP\DB::prepare('SELECT id FROM *PREFIX*calendar_calendars WHERE userid = ? AND displayname = ? LIMIT 1'); + return $query->execute(array($uid, $item))->fetchAll(); + } + + public function generateTarget($item, $uid) { + + } + + public function getItems($sources) { + + } + +} + +class OC_Share_Backend_Event extends OCP\Share_Backend { + +} + + +?> \ No newline at end of file diff --git a/apps/calendar/templates/part.choosecalendar.rowfields.php b/apps/calendar/templates/part.choosecalendar.rowfields.php index d29113c9a6..64aaa79719 100644 --- a/apps/calendar/templates/part.choosecalendar.rowfields.php +++ b/apps/calendar/templates/part.choosecalendar.rowfields.php @@ -5,7 +5,7 @@ - + diff --git a/apps/contacts/appinfo/app.php b/apps/contacts/appinfo/app.php index 7e73f315dd..6816343018 100644 --- a/apps/contacts/appinfo/app.php +++ b/apps/contacts/appinfo/app.php @@ -3,6 +3,8 @@ OC::$CLASSPATH['OC_Contacts_App'] = 'apps/contacts/lib/app.php'; OC::$CLASSPATH['OC_Contacts_Addressbook'] = 'apps/contacts/lib/addressbook.php'; OC::$CLASSPATH['OC_Contacts_VCard'] = 'apps/contacts/lib/vcard.php'; OC::$CLASSPATH['OC_Contacts_Hooks'] = 'apps/contacts/lib/hooks.php'; +OC::$CLASSPATH['OC_Share_Backend_Contact'] = 'apps/contacts/lib/share/contact.php'; +OC::$CLASSPATH['OC_Share_Backend_Addressbook'] = 'apps/contacts/lib/share/addressbook.php'; OC::$CLASSPATH['OC_Connector_Sabre_CardDAV'] = 'apps/contacts/lib/connector_sabre.php'; OC::$CLASSPATH['Sabre_CardDAV_VCFExportPlugin'] = 'apps/contacts/lib/VCFExportPlugin.php'; OC::$CLASSPATH['OC_Search_Provider_Contacts'] = 'apps/contacts/lib/search.php'; @@ -20,3 +22,6 @@ OCP\App::addNavigationEntry( array( OCP\Util::addscript('contacts', 'loader'); OC_Search::registerProvider('OC_Search_Provider_Contacts'); +OCP\Share::registerBackend('contact', 'OC_Share_Backend_Contact'); +OCP\Share::registerBackend('addressbook', 'OC_Share_Backend_Addressbook', 'contact'); + diff --git a/apps/contacts/css/contacts.css b/apps/contacts/css/contacts.css index c5308c4d25..ad8762167b 100644 --- a/apps/contacts/css/contacts.css +++ b/apps/contacts/css/contacts.css @@ -50,11 +50,12 @@ label:hover, dt:hover { color: #333; } .float { float: left; } .svg { border: inherit; background: inherit; } .listactions { height: 1em; width:60px; float: left; clear: right; } -.add,.edit,.delete,.mail, .globe, .upload, .download, .cloud { cursor: pointer; width: 20px; height: 20px; margin: 0; float: left; position:relative; opacity: 0.1; } +.add,.edit,.delete,.mail, .globe, .upload, .download, .cloud, .share { cursor: pointer; width: 20px; height: 20px; margin: 0; float: left; position:relative; opacity: 0.1; } .add:hover,.edit:hover,.delete:hover,.mail:hover, .globe:hover, .upload:hover, .download:hover .cloud:hover { opacity: 1.0 } .add { background:url('%webroot%/core/img/actions/add.svg') no-repeat center; clear: both; } .delete { background:url('%webroot%/core/img/actions/delete.svg') no-repeat center; } .edit { background:url('%webroot%/core/img/actions/rename.svg') no-repeat center; } +.share { background:url('%webroot%/core/img/actions/share.svg') no-repeat center; } .mail { background:url('%webroot%/core/img/actions/mail.svg') no-repeat center; } .upload { background:url('%webroot%/core/img/actions/upload.svg') no-repeat center; } .download { background:url('%webroot%/core/img/actions/download.svg') no-repeat center; } diff --git a/apps/contacts/js/loader.js b/apps/contacts/js/loader.js index 5bca0ab723..3b1f407048 100644 --- a/apps/contacts/js/loader.js +++ b/apps/contacts/js/loader.js @@ -78,9 +78,9 @@ Contacts_Import={ } $(document).ready(function(){ if(typeof FileActions !== 'undefined'){ - FileActions.register('text/vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.register('text/vcard','importaddressbook', FileActions.PERMISSION_READ, '', Contacts_Import.importdialog); FileActions.setDefault('text/vcard','importaddressbook'); - FileActions.register('text/x-vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.register('text/x-vcard','importaddressbook', FileActions.PERMISSION_READ, '', Contacts_Import.importdialog); FileActions.setDefault('text/x-vcard','importaddressbook'); }; }); \ No newline at end of file diff --git a/apps/contacts/js/settings.js b/apps/contacts/js/settings.js index 67aaa5b5f8..69cf473e06 100644 --- a/apps/contacts/js/settings.js +++ b/apps/contacts/js/settings.js @@ -4,6 +4,7 @@ OC.Contacts.Settings = OC.Contacts.Settings || { this.Addressbook.adrsettings = $('.addressbooks-settings').first(); this.Addressbook.adractions = $('#contacts-settings').find('div.actions'); console.log('actions: ' + this.Addressbook.adractions.length); + OC.Share.loadIcons('addressbook'); }, Addressbook:{ showActions:function(act) { diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php index eb61b6dbce..92c5f4da3a 100644 --- a/apps/contacts/lib/addressbook.php +++ b/apps/contacts/lib/addressbook.php @@ -64,10 +64,10 @@ class OC_Contacts_Addressbook { while( $row = $result->fetchRow()) { $addressbooks[] = $row; } + $addressbooks = array_merge($addressbooks, OCP\Share::getItemsSharedWith('addressbook', OC_Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS)); if(!$active && !count($addressbooks)) { self::addDefault($uid); } - return $addressbooks; } @@ -208,7 +208,12 @@ class OC_Contacts_Addressbook { public static function edit($id,$name,$description) { // Need these ones for checking uri $addressbook = self::find($id); - + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $id); + if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & OCP\Share::PERMISSION_UPDATE)) { + return false; + } + } if(is_null($name)) { $name = $addressbook['name']; } @@ -270,6 +275,13 @@ class OC_Contacts_Addressbook { * @return boolean */ public static function delete($id) { + $addressbook = self::find($id); + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $id); + if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & OCP\Share::PERMISSION_DELETE)) { + return false; + } + } self::setActive($id, false); try { $stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' ); diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php index b59a8372b7..f6ce213c49 100644 --- a/apps/contacts/lib/app.php +++ b/apps/contacts/lib/app.php @@ -37,21 +37,24 @@ class OC_Contacts_App { ) ) ); - } - else { - OCP\Util::writeLog('contacts', - 'Addressbook('.$id.') is not from '.OCP\USER::getUser(), - OCP\Util::ERROR); - //throw new Exception('This is not your addressbook.'); - OCP\JSON::error( - array( - 'data' => array( - 'message' => self::$l10n->t('This is not your addressbook.') + } else { + $sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $id, OC_Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS); + if ($sharedAddressbook) { + return $sharedAddressbook; + } else { + OCP\Util::writeLog('contacts', + 'Addressbook('.$id.') is not from '.OCP\USER::getUser(), + OCP\Util::ERROR); + //throw new Exception('This is not your addressbook.'); + OCP\JSON::error( + array( + 'data' => array( + 'message' => self::$l10n->t('This is not your addressbook.') + ) ) - ) - ); + ); + } } - exit(); } return $addressbook; } diff --git a/apps/contacts/lib/share/addressbook.php b/apps/contacts/lib/share/addressbook.php new file mode 100644 index 0000000000..62799696d9 --- /dev/null +++ b/apps/contacts/lib/share/addressbook.php @@ -0,0 +1,93 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Share_Backend_Addressbook implements OCP\Share_Backend_Collection { + const FORMAT_ADDRESSBOOKS = 1; + + /** + * @brief Get the source of the item to be stored in the database + * @param string Item + * @param string Owner of the item + * @return mixed|array|false Source + * + * Return an array if the item is file dependent, the array needs two keys: 'item' and 'file' + * Return false if the item does not exist for the user + * + * The formatItems() function will translate the source returned back into the item + */ + public function isValidSource($itemSource, $uidOwner) { + $addressbook = OC_Contacts_Addressbook::find( $itemSource ); + if( $addressbook === false || $addressbook['userid'] != $uidOwner) { + return false; + } + return true; + } + + /** + * @brief Get a unique name of the item for the specified user + * @param string Item + * @param string|false User the item is being shared with + * @param array|null List of similar item names already existing as shared items + * @return string Target name + * + * This function needs to verify that the user does not already have an item with this name. + * If it does generate a new name e.g. name_# + */ + public function generateTarget($itemSource, $shareWith, $exclude = null) { + $addressbook = OC_Contacts_Addressbook::find( $itemSource ); + $user_addressbooks = array(); + foreach(OC_Contacts_Addressbook::all($uid) as $user_addressbook) { + $user_addressbooks[] = $user_addressbook['displayname']; + } + $name = $addressbook['userid']."'s ".$addressbook['displayname']; + $suffix = ''; + while (in_array($name.$suffix, $user_addressbooks)) { + $suffix++; + } + + return $name.$suffix; + } + + /** + * @brief Converts the shared item sources back into the item in the specified format + * @param array Shared items + * @param int Format + * @return ? + * + * The items array is a 3-dimensional array with the item_source as the first key and the share id as the second key to an array with the share info. + * The key/value pairs included in the share info depend on the function originally called: + * If called by getItem(s)Shared: id, item_type, item, item_source, share_type, share_with, permissions, stime, file_source + * If called by getItem(s)SharedWith: id, item_type, item, item_source, item_target, share_type, share_with, permissions, stime, file_source, file_target + * This function allows the backend to control the output of shared items with custom formats. + * It is only called through calls to the public getItem(s)Shared(With) functions. + */ + public function formatItems($items, $format, $parameters = null) { + $addressbooks = array(); + if ($format == self::FORMAT_ADDRESSBOOKS) { + foreach ($items as $item) { + $addressbook = OC_Contacts_Addressbook::find($item['item_source']); + if ($addressbook) { + $addressbook['displayname'] = $item['item_target']; + $addressbooks[] = $addressbook; + } + } + } + return $addressbooks; + } + + public function getChildren($itemSource) { + $query = OCP\DB::prepare('SELECT id FROM *PREFIX*contacts_cards WHERE addressbookid = ?'); + $result = $query->execute(array($itemSource)); + $sources = array(); + while ($contact = $result->fetchRow()) { + $sources[] = $contact['id']; + } + return $sources; + } + +} diff --git a/apps/contacts/lib/share/contact.php b/apps/contacts/lib/share/contact.php new file mode 100644 index 0000000000..718c8f9025 --- /dev/null +++ b/apps/contacts/lib/share/contact.php @@ -0,0 +1,53 @@ +. +*/ + +class OC_Share_Backend_Contact implements OCP\Share_Backend { + + const FORMAT_CONTACT = 0; + + private static $contact; + + public function isValidSource($itemSource, $uidOwner) { + self::$contact = OC_Contacts_VCard::find($itemSource); + if (self::$contact) { + return true; + } + return false; + } + + public function generateTarget($itemSource, $shareWith, $exclude = null) { + // TODO Get default addressbook and check for conflicts + return self::$contact['fullname']; + } + + public function formatItems($items, $format, $parameters = null) { + $contacts = array(); + if ($format == self::FORMAT_CONTACT) { + foreach ($items as $item) { + $contact = OC_Contacts_VCard::find($item['item_source']); + $contact['fullname'] = $item['item_target']; + $contacts[] = $contact; + } + } + return $contacts; + } + +} \ No newline at end of file diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php index 81ae689d9a..8c38f1ba15 100644 --- a/apps/contacts/lib/vcard.php +++ b/apps/contacts/lib/vcard.php @@ -293,12 +293,17 @@ class OC_Contacts_VCard{ OCP\Util::writeLog('contacts', 'OC_Contacts_VCard::add. No vCard supplied', OCP\Util::ERROR); return null; }; - + $addressbook = OC_Contacts_Addressbook::find($aid); + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $aid); + if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & OCP\Share::PERMISSION_CREATE)) { + return false; + } + } if(!$isChecked) { OC_Contacts_App::loadCategoriesFromVCard($card); self::updateValuesFromAdd($aid, $card); } - $card->setString('VERSION', '3.0'); // Add product ID is missing. $prodid = trim($card->getAsString('PRODID')); @@ -357,6 +362,17 @@ class OC_Contacts_VCard{ foreach($objects as $object) { $vcard = OC_VObject::parse($object[1]); if(!is_null($vcard)) { + $oldcard = self::find($object[0]); + if (!$oldcard) { + return false; + } + $addressbook = OC_Contacts_Addressbook::find($oldcard['addressbookid']); + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedContact = OCP\Share::getItemSharedWithBySource('contact', $object[0], OCP\Share::FORMAT_NONE, null, true); + if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_UPDATE)) { + return false; + } + } $vcard->setString('REV', $now->format(DateTime::W3C)); $data = $vcard->serialize(); try { @@ -378,11 +394,20 @@ class OC_Contacts_VCard{ */ public static function edit($id, OC_VObject $card){ $oldcard = self::find($id); - + if (!$oldcard) { + return false; + } if(is_null($card)) { return false; } - + // NOTE: Owner checks are being made in the ajax files, which should be done inside the lib files to prevent any redundancies with sharing checks + $addressbook = OC_Contacts_Addressbook::find($oldcard['addressbookid']); + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedContact = OCP\Share::getItemSharedWithBySource('contact', $id, OCP\Share::FORMAT_NONE, null, true); + if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_UPDATE)) { + return false; + } + } OC_Contacts_App::loadCategoriesFromVCard($card); $fn = $card->getAsString('FN'); @@ -431,6 +456,17 @@ class OC_Contacts_VCard{ * @return boolean */ public static function delete($id){ + $card = self::find($id); + if (!$card) { + return false; + } + $addressbook = OC_Contacts_Addressbook::find($card['addressbookid']); + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedContact = OCP\Share::getItemSharedWithBySource('contact', $id, OCP\Share::FORMAT_NONE, null, true); + if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_DELETE)) { + return false; + } + } OC_Hook::emit('OC_Contacts_VCard', 'pre_deleteVCard', array('aid' => null, 'id' => $id, 'uri' => null)); $stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE id = ?' ); try { @@ -451,6 +487,18 @@ class OC_Contacts_VCard{ * @return boolean */ public static function deleteFromDAVData($aid,$uri){ + $addressbook = OC_Contacts_Addressbook::find($aid); + if ($addressbook['userid'] != OCP\User::getUser()) { + $query = OCP\DB::prepare( 'SELECT id FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri = ?' ); + $id = $query->execute(array($aid, $uri))->fetchOne(); + if (!$id) { + return false; + } + $sharedContact = OCP\Share::getItemSharedWithBySource('contact', $id, OCP\Share::FORMAT_NONE, null, true); + if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_DELETE)) { + return false; + } + } OC_Hook::emit('OC_Contacts_VCard', 'pre_deleteVCard', array('aid' => $aid, 'id' => null, 'uri' => $uri)); $stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri=?' ); try { @@ -594,7 +642,27 @@ class OC_Contacts_VCard{ */ public static function moveToAddressBook($aid, $id, $isAddressbook = false) { OC_Contacts_App::getAddressbook($aid); // check for user ownership. + $addressbook = OC_Contacts_Addressbook::find($aid); + if ($addressbook['userid'] != OCP\User::getUser()) { + $sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $aid); + if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & OCP\Share::PERMISSION_CREATE)) { + return false; + } + } if(is_array($id)) { + foreach ($id as $index => $cardId) { + $card = self::find($cardId); + if (!$card) { + unset($id[$index]); + } + $oldAddressbook = OC_Contacts_Addressbook::find($card['addressbookid']); + if ($oldAddressbook['userid'] != OCP\User::getUser()) { + $sharedContact = OCP\Share::getItemSharedWithBySource('contact', $cardId, OCP\Share::FORMAT_NONE, null, true); + if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_DELETE)) { + unset($id[$index]); + } + } + } $id_sql = join(',', array_fill(0, count($id), '?')); $prep = 'UPDATE *PREFIX*contacts_cards SET addressbookid = ? WHERE id IN ('.$id_sql.')'; try { @@ -613,6 +681,17 @@ class OC_Contacts_VCard{ if($isAddressbook) { $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_cards SET addressbookid = ? WHERE addressbookid = ?' ); } else { + $card = self::find($id); + if (!$card) { + return false; + } + $oldAddressbook = OC_Contacts_Addressbook::find($card['addressbookid']); + if ($oldAddressbook['userid'] != OCP\User::getUser()) { + $sharedContact = OCP\Share::getItemSharedWithBySource('contact', $id, OCP\Share::FORMAT_NONE, null, true); + if (!$sharedContact || !($sharedContact['permissions'] & OCP\Share::PERMISSION_DELETE)) { + return false; + } + } $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_cards SET addressbookid = ? WHERE id = ?' ); } try { diff --git a/apps/contacts/templates/settings.php b/apps/contacts/templates/settings.php index 3fddc59588..e3536c7b46 100644 --- a/apps/contacts/templates/settings.php +++ b/apps/contacts/templates/settings.php @@ -22,6 +22,9 @@ + + "> + diff --git a/apps/files/index.php b/apps/files/index.php index d65aa6cabb..6d53527025 100644 --- a/apps/files/index.php +++ b/apps/files/index.php @@ -93,7 +93,7 @@ $tmpl = new OCP\Template( 'files', 'index', 'user' ); $tmpl->assign( 'fileList', $list->fetchPage(), false ); $tmpl->assign( 'breadcrumb', $breadcrumbNav->fetchPage(), false ); $tmpl->assign( 'dir', OC_Filesystem::normalizePath($dir)); -$tmpl->assign( 'readonly', !OC_Filesystem::is_writable($dir.'/')); +$tmpl->assign( 'isCreatable', OC_Filesystem::isCreatable($dir.'/')); $tmpl->assign( 'files', $files ); $tmpl->assign( 'uploadMaxFilesize', $maxUploadFilesize); $tmpl->assign( 'uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize)); diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js index b6f4d0b089..39e848cec8 100644 --- a/apps/files/js/fileactions.js +++ b/apps/files/js/fileactions.js @@ -1,19 +1,28 @@ FileActions={ + PERMISSION_CREATE:4, + PERMISSION_READ:1, + PERMISSION_UPDATE:2, + PERMISSION_DELETE:8, + PERMISSION_SHARE:16, actions:{}, defaults:{}, icons:{}, currentFile:null, - register:function(mime,name,icon,action){ + register:function(mime,name,permissions,icon,action){ if(!FileActions.actions[mime]){ FileActions.actions[mime]={}; } - FileActions.actions[mime][name]=action; + if (!FileActions.actions[mime][name]) { + FileActions.actions[mime][name] = {}; + } + FileActions.actions[mime][name]['action'] = action; + FileActions.actions[mime][name]['permissions'] = permissions; FileActions.icons[name]=icon; }, setDefault:function(mime,name){ FileActions.defaults[mime]=name; }, - get:function(mime,type){ + get:function(mime,type,permissions){ var actions={}; if(FileActions.actions.all){ actions=$.extend( actions, FileActions.actions.all ) @@ -32,9 +41,15 @@ FileActions={ actions=$.extend( actions, FileActions.actions[type] ) } } - return actions; + var filteredActions = {}; + $.each(actions, function(name, action) { + if (action.permissions & permissions) { + filteredActions[name] = action.action; + } + }); + return filteredActions; }, - getDefault:function(mime,type){ + getDefault:function(mime,type,permissions){ if(mime){ var mimePart=mime.substr(0,mime.indexOf('/')); } @@ -48,22 +63,20 @@ FileActions={ }else{ name=FileActions.defaults.all; } - var actions=this.get(mime,type); + var actions=this.get(mime,type,permissions); return actions[name]; }, - display:function(parent, filename, type){ + display:function(parent){ FileActions.currentFile=parent; $('#fileList span.fileactions, #fileList td.date a.action').remove(); - var actions=FileActions.get(FileActions.getCurrentMimeType(),FileActions.getCurrentType()); + var actions=FileActions.get(FileActions.getCurrentMimeType(),FileActions.getCurrentType(), FileActions.getCurrentPermissions()); var file=FileActions.getCurrentFile(); if($('tr').filterAttr('data-file',file).data('renaming')){ return; } parent.children('a.name').append(''); - var defaultAction=FileActions.getDefault(FileActions.getCurrentMimeType(),FileActions.getCurrentType()); + var defaultAction=FileActions.getDefault(FileActions.getCurrentMimeType(),FileActions.getCurrentType(), FileActions.getCurrentPermissions()); for(name in actions){ - // no rename and share action for the 'Shared' dir - if((name=='Rename' || name =='Share') && type=='dir' && filename=='Shared') { continue; } if((name=='Download' || actions[name]!=defaultAction) && name!='Delete'){ var img=FileActions.icons[name]; if(img.call){ @@ -86,16 +99,12 @@ FileActions={ parent.find('a.name>span.fileactions').append(element); } } - if(actions['Delete'] && (type!='dir' || filename != 'Shared')){ // no delete action for the 'Shared' dir + if(actions['Delete']){ var img=FileActions.icons['Delete']; if(img.call){ img=img(file); } - if ($('#dir').val().indexOf('Shared') != -1) { - var html='
t('New');?>
- +
t('Nothing in here. Upload something!')?>
@@ -43,7 +43,7 @@ - + t( 'Name' ); ?> @@ -56,7 +56,7 @@ t( 'Modified' ); ?>t('Delete')?> <?php echo $l->t('Delete')?>" /> - + diff --git a/apps/files/templates/part.list.php b/apps/files/templates/part.list.php index 4506630c16..6667b5488a 100644 --- a/apps/files/templates/part.list.php +++ b/apps/files/templates/part.list.php @@ -1,5 +1,4 @@ - ' data-write=''> + ' data-permissions=''> diff --git a/apps/files_archive/js/archive.js b/apps/files_archive/js/archive.js index 9fb9853e29..6bcbe09266 100644 --- a/apps/files_archive/js/archive.js +++ b/apps/files_archive/js/archive.js @@ -7,11 +7,11 @@ $(document).ready(function() { if(typeof FileActions!=='undefined'){ - FileActions.register('application/zip','Open','',function(filename){ + FileActions.register('application/zip','Open', FileActions.PERMISSION_READ, '',function(filename){ window.location=OC.linkTo('files', 'index.php')+'&dir='+encodeURIComponent($('#dir').val()).replace(/%2F/g, '/')+'/'+encodeURIComponent(filename); }); FileActions.setDefault('application/zip','Open'); - FileActions.register('application/x-gzip','Open','',function(filename){ + FileActions.register('application/x-gzip','Open', FileActions.PERMISSION_READ, '',function(filename){ window.location=OC.linkTo('files', 'index.php')+'&dir='+encodeURIComponent($('#dir').val()).replace(/%2F/g, '/')+'/'+encodeURIComponent(filename); }); FileActions.setDefault('application/x-gzip','Open'); diff --git a/apps/files_archive/lib/storage.php b/apps/files_archive/lib/storage.php index ca36e76b48..3c14c3e1fd 100644 --- a/apps/files_archive/lib/storage.php +++ b/apps/files_archive/lib/storage.php @@ -89,10 +89,10 @@ class OC_Filestorage_Archive extends OC_Filestorage_Common{ return $this->archive->fileExists($path.'/')?'dir':'file'; } } - public function is_readable($path){ + public function isReadable($path){ return is_readable($this->path); } - public function is_writable($path){ + public function isUpdatable($path){ return is_writable($this->path); } public function file_exists($path){ diff --git a/apps/files_external/lib/amazons3.php b/apps/files_external/lib/amazons3.php index 9feb490dac..3c2e333017 100644 --- a/apps/files_external/lib/amazons3.php +++ b/apps/files_external/lib/amazons3.php @@ -134,12 +134,12 @@ class OC_Filestorage_AmazonS3 extends OC_Filestorage_Common { return false; } - public function is_readable($path) { + public function isReadable($path) { // TODO Check acl and determine who grantee is return true; } - public function is_writable($path) { + public function isUpdatable($path) { // TODO Check acl and determine who grantee is return true; } diff --git a/apps/files_external/lib/dropbox.php b/apps/files_external/lib/dropbox.php index a27d9d7c36..b90563a506 100755 --- a/apps/files_external/lib/dropbox.php +++ b/apps/files_external/lib/dropbox.php @@ -125,11 +125,11 @@ class OC_Filestorage_Dropbox extends OC_Filestorage_Common { return false; } - public function is_readable($path) { + public function isReadable($path) { return $this->file_exists($path); } - public function is_writable($path) { + public function isUpdatable($path) { return $this->file_exists($path); } diff --git a/apps/files_external/lib/dropboxtest.php b/apps/files_external/lib/dropboxtest.php new file mode 100644 index 0000000000..686549e16b --- /dev/null +++ b/apps/files_external/lib/dropboxtest.php @@ -0,0 +1,71 @@ + '526ar3qlrtzmv65', 'app_secret' => '3bbn0wo5lzgpjty', 'token' => 'a3ben02jb1y538a', 'token_secret' => 'x60h3fsky21r1b0')); +$dropbox->rename('/652072main_2012-2897_full (1).jpg', '/test.jpg'); +// $dropbox->test(); +// print_r($dropbox->mkdir('Again')); + +// GET&https%3A%2F%2Fapi.dropbox.com%2F1%2Fmetadata%2Fdropbox%2FownCloud&list%3D0%26 + +// uzpi8oo2rbax1po +// th9uoso3xxny3ca +//Step 3: Acquiring access tokens Array ( [token] => 37my637p88ng967 [token_secret] => t49fmgp3omucnnr ) +//The user is authenticated You should really save the oauth tokens somewhere, so the first steps will no longer be needed Array ( [token] => 37my637p88ng967 [token_secret] => t49fmgp3omucnnr ) + +// For convenience, definitely not required +// header('Content-Type: text/plain'); + +// // We need to start a session +// session_start(); + +// There are multiple steps in this workflow, we keep a 'state number' here +// if (isset($_SESSION['state'])) { +// $state = 2; +// } else { +// $state = 1; +// } +// +// switch($state) { +// +// /* In this phase we grab the initial request tokens +// and redirect the user to the 'authorize' page hosted +// on dropbox */ +// case 1 : +// echo "Step 1: Acquire request tokens\n"; +// $tokens = $oauth->getRequestToken(); +// print_r($tokens); +// +// // Note that if you want the user to automatically redirect back, you can +// // add the 'callback' argument to getAuthorizeUrl. +// echo "Step 2: You must now redirect the user to:\n"; +// echo $oauth->getAuthorizeUrl() . "\n"; +// $_SESSION['state'] = 2; +// $_SESSION['oauth_tokens'] = $tokens; +// die(); +// +// /* In this phase, the user just came back from authorizing +// and we're going to fetch the real access tokens */ +// case 2 : +// echo "Step 3: Acquiring access tokens\n"; +// $oauth->setToken($_SESSION['oauth_tokens']); +// $tokens = $oauth->getAccessToken(); +// print_r($tokens); +// $_SESSION['state'] = 3; +// $_SESSION['oauth_tokens'] = $tokens; +// // There is no break here, intentional +// +// /* This part gets called if the authentication process +// already succeeded. We can use our stored tokens and the api +// should work. Store these tokens somewhere, like a database */ +// case 3 : +// echo "The user is authenticated\n"; +// echo "You should really save the oauth tokens somewhere, so the first steps will no longer be needed\n"; +// print_r($_SESSION['oauth_tokens']); +// $oauth->setToken($_SESSION['oauth_tokens']); +// break; +// +// } + +?> \ No newline at end of file diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php index 2b387f0c83..73317bbf71 100644 --- a/apps/files_external/lib/google.php +++ b/apps/files_external/lib/google.php @@ -284,11 +284,11 @@ class OC_Filestorage_Google extends OC_Filestorage_Common { return false; } - public function is_readable($path) { + public function isReadable($path) { return true; } - public function is_writable($path) { + public function isUpdatable($path) { if ($path == '' || $path == '/') { return true; } else if ($entry = $this->getResource($path)) { diff --git a/apps/files_external/lib/googletest.php b/apps/files_external/lib/googletest.php new file mode 100644 index 0000000000..a80af6a097 --- /dev/null +++ b/apps/files_external/lib/googletest.php @@ -0,0 +1,8 @@ + '4/7nZMlRLlAEeXdY0AeH-eHNCL0YaK', 'token_secret' => 'NqO5VMGUVkwFtOYqHsex4257')); +// $drive = new OC_Filestorage_Google(array('token' => '1/4Xo84YtTxL2MkQst-Ti3nqF1Isy70NUHDRk-BwsLMf4', 'token_secret' => 'jYnyJ_4-ITNxlX9f9RDNoRW-')); +// var_export($drive->getFeed('https://docs.google.com/feeds/metadata/default', 'GET')); + diff --git a/apps/files_external/lib/streamwrapper.php b/apps/files_external/lib/streamwrapper.php index 7d56445361..467c5a5b84 100644 --- a/apps/files_external/lib/streamwrapper.php +++ b/apps/files_external/lib/streamwrapper.php @@ -32,11 +32,11 @@ abstract class OC_FileStorage_StreamWrapper extends OC_Filestorage_Common{ return filetype($this->constructUrl($path)); } - public function is_readable($path){ + public function isReadable($path){ return true;//not properly supported } - public function is_writable($path){ + public function isUpdatable($path){ return true;//not properly supported } diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php index 58b95a6ae0..94ccde1ff8 100644 --- a/apps/files_external/lib/swift.php +++ b/apps/files_external/lib/swift.php @@ -353,11 +353,11 @@ class OC_FileStorage_SWIFT extends OC_Filestorage_Common{ } } - public function is_readable($path){ + public function isReadable($path){ return true; } - public function is_writable($path){ + public function isUpdatable($path){ return true; } diff --git a/apps/files_external/lib/webdav.php b/apps/files_external/lib/webdav.php index 84d64b6519..e3f73c5c0a 100644 --- a/apps/files_external/lib/webdav.php +++ b/apps/files_external/lib/webdav.php @@ -104,11 +104,11 @@ class OC_FileStorage_DAV extends OC_Filestorage_Common{ } } - public function is_readable($path){ + public function isReadable($path){ return true;//not properly supported } - public function is_writable($path){ + public function isUpdatable($path){ return true;//not properly supported } diff --git a/apps/files_external/tests/test.php b/apps/files_external/tests/test.php new file mode 100644 index 0000000000..bd24404f3b --- /dev/null +++ b/apps/files_external/tests/test.php @@ -0,0 +1,7 @@ +"; +print_r(OC_Mount_Config::getSystemMountPoints()); +echo ""; +// OC_Mount_Config::addMountPoint('Photos', 'OC_Filestorage_SWIFT', array('host' => 'gapinthecloud.com', 'user' => 'Gap', 'token' => '23423afdasFJEW22', 'secure' => 'true', 'root' => ''), OC_Mount_Config::MOUNT_TYPE_GROUP, 'admin', false); +?> diff --git a/apps/files_imageviewer/js/lightbox.js b/apps/files_imageviewer/js/lightbox.js index 31f08456d2..ff12d808bc 100644 --- a/apps/files_imageviewer/js/lightbox.js +++ b/apps/files_imageviewer/js/lightbox.js @@ -1,6 +1,6 @@ $(document).ready(function() { if(typeof FileActions!=='undefined'){ - FileActions.register('image','View','',function(filename){ + FileActions.register('image','View', FileActions.PERMISSION_READ, '',function(filename){ viewImage($('#dir').val(),filename); }); FileActions.setDefault('image','View'); diff --git a/apps/files_pdfviewer/js/viewer.js b/apps/files_pdfviewer/js/viewer.js index 2c9cbb9b43..29db2ea7f2 100644 --- a/apps/files_pdfviewer/js/viewer.js +++ b/apps/files_pdfviewer/js/viewer.js @@ -40,7 +40,7 @@ $(document).ready(function(){ if(location.href.indexOf("files")!=-1) { PDFJS.workerSrc = OC.filePath('files_pdfviewer','js','pdfjs/build/pdf.js'); if(typeof FileActions!=='undefined'){ - FileActions.register('application/pdf','Edit','',function(filename){ + FileActions.register('application/pdf','Edit', FileActions.PERMISSION_READ, '',function(filename){ showPDFviewer($('#dir').val(),filename); }); FileActions.setDefault('application/pdf','Edit'); diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php index bbb753d5e6..7495a5bbe6 100644 --- a/apps/files_sharing/appinfo/app.php +++ b/apps/files_sharing/appinfo/app.php @@ -1,6 +1,8 @@ . +*/ + +class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent { + + const FORMAT_SHARED_STORAGE = 0; + const FORMAT_FILE_APP = 1; + const FORMAT_FILE_APP_ROOT = 2; + const FORMAT_OPENDIR = 3; + + public function isValidSource($item, $uid) { + if (OC_Filesystem::file_exists($item)) { + return true; + } + return false; + } + + public function getFilePath($item, $uid) { + return $item; + } + + public function generateTarget($item, $uid, $exclude = null) { + // TODO Make sure target path doesn't exist already + return $item; + } + + public function formatItems($items, $format, $parameters = null) { + if ($format == self::FORMAT_SHARED_STORAGE) { + // Only 1 item should come through for this format call + return array('path' => $items[key($items)]['file_source'], 'permissions' => $items[key($items)]['permissions']); + } else if ($format == self::FORMAT_FILE_APP) { + $files = array(); + foreach ($items as $item) { + $file = array(); + $file['path'] = $item['file_target']; + $file['name'] = basename($item['file_target']); + $file['ctime'] = $item['ctime']; + $file['mtime'] = $item['mtime']; + $file['mimetype'] = $item['mimetype']; + $file['size'] = $item['size']; + $file['encrypted'] = $item['encrypted']; + $file['versioned'] = $item['versioned']; + $file['directory'] = $parameters['folder']; + $file['type'] = ($item['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $file['permissions'] = $item['permissions']; + if ($file['type'] == 'file') { + // Remove Create permission if type is file + $file['permissions'] &= ~OCP\Share::PERMISSION_CREATE; + } + $files[] = $file; + } + return $files; + } else if ($format == self::FORMAT_FILE_APP_ROOT) { + $mtime = 0; + $size = 0; + foreach ($items as $item) { + if ($item['mtime'] > $mtime) { + $mtime = $item['mtime']; + } + $size += $item['size']; + } + return array(0 => array('name' => 'Shared', 'mtime' => $mtime, 'mimetype' => 'httpd/unix-directory', 'size' => $size, 'writable' => false, 'type' => 'dir', 'directory' => '', 'permissions' => OCP\Share::PERMISSION_READ)); + } else if ($format == self::FORMAT_OPENDIR) { + $files = array(); + foreach ($items as $item) { + $files[] = basename($item['file_target']); + } + return $files; + } + return array(); + } + +} \ No newline at end of file diff --git a/apps/files_sharing/lib/share/folder.php b/apps/files_sharing/lib/share/folder.php new file mode 100644 index 0000000000..b6db96614f --- /dev/null +++ b/apps/files_sharing/lib/share/folder.php @@ -0,0 +1,61 @@ +. +*/ + +class OC_Share_Backend_Folder extends OC_Share_Backend_File { + + public function formatItems($items, $format, $parameters = null) { + if ($format == self::FORMAT_SHARED_STORAGE) { + // Only 1 item should come through for this format call + return array('path' => $items[key($items)]['file_source'], 'permissions' => $items[key($items)]['permissions']); + } else if ($format == self::FORMAT_FILE_APP && isset($parameters['folder'])) { + // Only 1 item should come through for this format call + $folder = $items[key($items)]; + if (isset($parameters['mimetype_filter'])) { + $mimetype_filter = $parameters['mimetype_filter']; + } else { + $mimetype_filter = ''; + } + $path = $folder['file_source'].substr($parameters['folder'], 7 + strlen($folder['file_target'])); + $files = OC_FileCache::getFolderContent($path, '', $mimetype_filter); + foreach ($files as &$file) { + $file['directory'] = $parameters['folder']; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $file['permissions'] = $folder['permissions']; + if ($file['type'] == 'file') { + // Remove Create permission if type is file + $file['permissions'] &= ~OCP\Share::PERMISSION_CREATE; + } + } + return $files; + } + return array(); + } + + public function getChildren($itemSource) { + $files = OC_FileCache::getFolderContent($itemSource); + $sources = array(); + foreach ($files as $file) { + $sources[] = $file['path']; + } + return $sources; + } + +} \ No newline at end of file diff --git a/apps/files_sharing/sharedstorage.php b/apps/files_sharing/sharedstorage.php index 05df275ca9..582c9c6617 100644 --- a/apps/files_sharing/sharedstorage.php +++ b/apps/files_sharing/sharedstorage.php @@ -3,7 +3,7 @@ * ownCloud * * @author Michael Gapczynski - * @copyright 2011 Michael Gapczynski GapczynskiM@gmail.com + * @copyright 2011 Michael Gapczynski mtgap@owncloud.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -20,213 +20,217 @@ * */ -require_once( 'lib_share.php' ); - /** * Convert target path to source path and pass the function call to the correct storage provider */ class OC_Filestorage_Shared extends OC_Filestorage_Common { - private $datadir; - private $sourcePaths = array(); + private $sharedFolder; + private $files = array(); public function __construct($arguments) { - $this->datadir = $arguments['datadir']; - $this->datadir .= "/"; + $this->sharedFolder = $arguments['sharedFolder']; } - - public function getInternalPath($path) { + + /** + * @brief Get the source file path and the permissions granted for a shared file + * @param string Shared target file path + * @return Returns array with the keys path and permissions or false if not found + */ + private function getFile($target) { + $target = '/'.$target; + $target = rtrim($target, '/'); + if (isset($this->files[$target])) { + return $this->files[$target]; + } else { + $pos = strpos($target, '/', 1); + // Get shared folder name + if ($pos !== false) { + $folder = substr($target, 0, $pos); + if (isset($this->files[$folder])) { + $file = $this->files[$folder]; + } else { + $file = OCP\Share::getItemSharedWith('folder', $folder, OC_Share_Backend_File::FORMAT_SHARED_STORAGE); + } + if ($file) { + $this->files[$target]['path'] = $file['path'].substr($target, strlen($folder)); + $this->files[$target]['permissions'] = $file['permissions']; + return $this->files[$target]; + } + } else { + $file = OCP\Share::getItemSharedWith('file', $target, OC_Share_Backend_File::FORMAT_SHARED_STORAGE); + if ($file) { + $this->files[$target] = $file; + return $this->files[$target]; + } + } + OCP\Util::writeLog('files_sharing', 'File source not found for: '.$target, OCP\Util::ERROR); + return false; + } + } + + /** + * @brief Get the source file path for a shared file + * @param string Shared target file path + * @return Returns source file path or false if not found + */ + private function getSourcePath($target) { + $file = $this->getFile($target); + if (isset($file['path'])) { + return $file['path']; + } + return false; + } + + /** + * @brief Get the permissions granted for a shared file + * @param string Shared target file path + * @return Returns CRUDS permissions granted or false if not found + */ + private function getPermissions($target) { + $file = $this->getFile($target); + if (isset($file['permissions'])) { + return $file['permissions']; + } + return false; + } + + /** + * @brief Get the internal path to pass to the storage filesystem call + * @param string Source file path + * @return Source file path with mount point stripped out + */ + private function getInternalPath($path) { $mountPoint = OC_Filesystem::getMountPoint($path); $internalPath = substr($path, strlen($mountPoint)); return $internalPath; } - public function getSource($target) { - $target = $this->datadir.$target; - if (array_key_exists($target, $this->sourcePaths)) { - return $this->sourcePaths[$target]; - } else { - $source = OC_Share::getSource($target); - $this->sourcePaths[$target] = $source; - return $source; - } - } - public function mkdir($path) { - if ($path == "" || $path == "/" || !$this->is_writable($path)) { + if ($path == '' || $path == '/' || !$this->isCreatable(dirname($path))) { return false; - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->mkdir($this->getInternalPath($source)); - } + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->mkdir($this->getInternalPath($source)); } + return false; } public function rmdir($path) { - // The folder will be removed from the database, but won't be deleted from the owner's filesystem - OC_Share::unshareFromMySelf($this->datadir.$path); + if (($source = $this->getSourcePath($path)) && $this->isDeletable($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->rmdir($this->getInternalPath($source)); + } + return false; } public function opendir($path) { - if ($path == "" || $path == "/") { - $path = $this->datadir.$path; - $sharedItems = OC_Share::getItemsInFolder($path); - $files = array(); - foreach ($sharedItems as $item) { - // If item is in the root of the shared storage provider and the item exists add it to the fakedirs - if (dirname($item['target'])."/" == $path && $this->file_exists(basename($item['target']))) { - $files[] = basename($item['target']); - } - } - OC_FakeDirStream::$dirs['shared'.$path] = $files; - return opendir('fakedir://shared'.$path); - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - $dh = $storage->opendir($this->getInternalPath($source)); - $modifiedItems = OC_Share::getItemsInFolder($source); - if ($modifiedItems && $dh) { - $sources = array(); - $targets = array(); - // Remove any duplicate or trailing '/' - $path = preg_replace('{(/)\1+}', "/", $path); - $targetFolder = rtrim($this->datadir.$path, "/"); - foreach ($modifiedItems as $item) { - // If the item is in the current directory and the item exists add it to the arrays - if (dirname($item['target']) == $targetFolder && $this->file_exists($path."/".basename($item['target']))) { - // If the item was unshared from self, add it it to the arrays - if ($item['permissions'] == OC_Share::UNSHARED) { - $sources[] = basename($item['source']); - $targets[] = ""; - } else { - $sources[] = basename($item['source']); - $targets[] = basename($item['target']); - } - } - } - // Don't waste time if there aren't any modified items in the current directory - if (empty($sources)) { - return $dh; - } else { - global $FAKEDIRS; - $files = array(); - while (($filename = readdir($dh)) !== false) { - if ($filename != "." && $filename != "..") { - // If the file isn't in the sources array it isn't modified and can be added as is - if (!in_array($filename, $sources)) { - $files[] = $filename; - // The file has a different name than the source and is added to the fakedirs - } else { - $target = $targets[array_search($filename, $sources)]; - // Don't add the file if it was unshared from self by the user - if ($target != "") { - $files[] = $target; - } - } - } - } - $FAKEDIRS['shared'] = $files; - return opendir('fakedir://shared'); - } - } else { - return $dh; - } - } + if ($path == '' || $path == '/') { + $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_Folder::FORMAT_OPENDIR); + OC_FakeDirStream::$dirs['shared'] = $files; + return opendir('fakedir://shared'); + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->opendir($this->getInternalPath($source)); } + return false; } - + public function is_dir($path) { - if ($path == "" || $path == "/") { + if ($path == '' || $path == '/') { return true; - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->is_dir($this->getInternalPath($source)); - } + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->is_dir($this->getInternalPath($source)); } + return false; } - + public function is_file($path) { - $source = $this->getSource($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->is_file($this->getInternalPath($source)); } + return false; } - - // TODO fill in other components of array + public function stat($path) { - if ($path == "" || $path == "/") { - $stat["size"] = $this->filesize($path); - $stat["mtime"] = $this->filemtime($path); - $stat["ctime"] = $this->filectime($path); + if ($path == '' || $path == '/') { + $stat['size'] = $this->filesize($path); + $stat['mtime'] = $this->filemtime($path); + $stat['ctime'] = $this->filectime($path); return $stat; - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->stat($this->getInternalPath($source)); - } + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->stat($this->getInternalPath($source)); } + return false; } - + public function filetype($path) { - if ($path == "" || $path == "/") { - return "dir"; - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->filetype($this->getInternalPath($source)); - } + if ($path == '' || $path == '/') { + return 'dir'; + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->filetype($this->getInternalPath($source)); } - + return false; } - + public function filesize($path) { - if ($path == "" || $path == "/" || $this->is_dir($path)) { + if ($path == '' || $path == '/' || $this->is_dir($path)) { return 0; - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->filesize($this->getInternalPath($source)); - } + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->filesize($this->getInternalPath($source)); } + return false; } - public function is_readable($path) { - return true; - } - - public function is_writable($path) { - if($path == "" || $path == "/"){ - return false; - }elseif (OC_Share::getPermissions($this->datadir.$path) & OC_Share::WRITE) { - return true; - } else { + public function isCreatable($path) { + if ($path == '') { return false; } + return ($this->getPermissions($path) & OCP\Share::PERMISSION_CREATE); } - + + public function isReadable($path) { + return $this->file_exists($path); + } + + public function isUpdatable($path) { + if ($path == '') { + return false; + } + return ($this->getPermissions($path) & OCP\Share::PERMISSION_UPDATE); + } + + public function isDeletable($path) { + if ($path == '') { + return false; + } + return ($this->getPermissions($path) & OCP\Share::PERMISSION_DELETE); + } + + public function isSharable($path) { + if ($path == '') { + return false; + } + return ($this->getPermissions($path) & OCP\Share::PERMISSION_SHARE); + } + public function file_exists($path) { - if ($path == "" || $path == "/") { + if ($path == '' || $path == '/') { return true; - } else { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->file_exists($this->getInternalPath($source)); - } + } else if ($source = $this->getSourcePath($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->file_exists($this->getInternalPath($source)); } + return false; } public function filectime($path) { - if ($path == "" || $path == "/") { + if ($path == '' || $path == '/') { $ctime = 0; if ($dh = $this->opendir($path)) { while (($filename = readdir($dh)) !== false) { @@ -238,7 +242,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } return $ctime; } else { - $source = $this->getSource($path); + $source = $this->getSourcePath($path); if ($source) { $storage = OC_Filesystem::getStorage($source); return $storage->filectime($this->getInternalPath($source)); @@ -247,7 +251,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } public function filemtime($path) { - if ($path == "" || $path == "/") { + if ($path == '' || $path == '/') { $mtime = 0; if ($dh = $this->opendir($path)) { while (($filename = readdir($dh)) !== false) { @@ -259,7 +263,7 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } return $mtime; } else { - $source = $this->getSource($path); + $source = $this->getSourcePath($path); if ($source) { $storage = OC_Filesystem::getStorage($source); return $storage->filemtime($this->getInternalPath($source)); @@ -268,10 +272,10 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } public function file_get_contents($path) { - $source = $this->getSource($path); + $source = $this->getSourcePath($path); if ($source) { $info = array( - 'target' => $this->datadir.$path, + 'target' => $this->sharedFolder.$path, 'source' => $source, ); OCP\Util::emitHook('OC_Filestorage_Shared', 'file_get_contents', $info); @@ -281,92 +285,66 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { } public function file_put_contents($path, $data) { - if ($this->is_writable($path)) { - $source = $this->getSource($path); - if ($source) { - $info = array( - 'target' => $this->datadir.$path, - 'source' => $source, - ); - OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info); - $storage = OC_Filesystem::getStorage($source); - $result = $storage->file_put_contents($this->getInternalPath($source), $data); - return $result; + if ($source = $this->getSourcePath($path)) { + // Check if permission is granted + if (($this->file_exists($path) && !$this->isUpdatable($path)) || ($this->is_dir($path) && !$this->isCreatable($path))) { + return false; } + $info = array( + 'target' => $this->sharedFolder.$path, + 'source' => $source, + ); + OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info); + $storage = OC_Filesystem::getStorage($source); + $result = $storage->file_put_contents($this->getInternalPath($source), $data); + return $result; } + return false; } public function unlink($path) { - // The item will be removed from the database, but won't be touched on the owner's filesystem - $target = $this->datadir.$path; - // Check if the item is inside a shared folder - if (OC_Share::getParentFolders($target)) { - // If entry for item already exists - if (OC_Share::getItem($target)) { - OC_Share::unshareFromMySelf($target, false); - } else { - OC_Share::pullOutOfFolder($target, $target); - OC_Share::unshareFromMySelf($target, false); - } - // Delete the database entry - } else { - OC_Share::unshareFromMySelf($target); + // Delete the file if DELETE permission is granted + if (($source = $this->getSourcePath($path)) && $this->isDeletable($path)) { + $storage = OC_Filesystem::getStorage($source); + return $storage->unlink($this->getInternalPath($source)); } - return true; + return false; } public function rename($path1, $path2) { - $oldTarget = $this->datadir.$path1; - $newTarget = $this->datadir.$path2; - // Check if the item is inside a shared folder - if ($folders = OC_Share::getParentFolders($oldTarget)) { - $root1 = substr($path1, 0, strpos($path1, "/")); - $root2 = substr($path1, 0, strpos($path2, "/")); - // Prevent items from being moved into different shared folders until versioning (cut and paste) and prevent items going into 'Shared' - if ($root1 !== $root2) { - return false; - // Check if both paths have write permission - } else if ($this->is_writable($path1) && $this->is_writable($path2)) { - $oldSource = $this->getSource($path1); - $newSource = $folders['source'].substr($newTarget, strlen($folders['target'])); - if ($oldSource) { - $storage = OC_Filesystem::getStorage($oldSource); + if ($oldSource = $this->getSourcePath($path1)) { + $root1 = substr($path1, 0, strpos($path1, '/')); + $root2 = substr($path2, 0, strpos($path2, '/')); + // Moving/renaming is only allowed within the same shared folder + if ($root1 == $root2) { + $storage = OC_Filesystem::getStorage($oldSource); + $newSource = substr($oldSource, 0, strpos($oldSource, $root1)).$path2; + if (dirname($path1) == dirname($path2)) { + // Rename the file if UPDATE permission is granted + if ($this->isUpdatable($path1)) { + return $storage->rename($this->getInternalPath($oldSource), $this->getInternalPath($newSource)); + } + // Move the file if DELETE and CREATE permissions are granted + } else if ($this->isDeletable($path1) && $this->isCreatable(dirname($path2))) { return $storage->rename($this->getInternalPath($oldSource), $this->getInternalPath($newSource)); } - // If the user doesn't have write permission, items can only be renamed and not moved - } else if (dirname($path1) !== dirname($path2)) { - return false; - // The item will be renamed in the database, but won't be touched on the owner's filesystem - } else { - OC_Share::pullOutOfFolder($oldTarget, $newTarget); - // If this is a folder being renamed, call setTarget in case there are any database entries inside the folder - if (self::is_dir($path1)) { - OC_Share::setTarget($oldTarget, $newTarget); - } } - } else { - OC_Share::setTarget($oldTarget, $newTarget); } - return true; + return false; } public function copy($path1, $path2) { - if ($path2 == "" || $path2 == "/") { - // TODO Construct new shared item or should this not be allowed? - } else { - if ($this->is_writable($path2)) { - $tmpFile = $this->toTmpFile($path1); - $result = $this->fromTmpFile($tmpFile, $path2); - return $result; - } else { - return false; - } + // Copy the file if CREATE permission is granted + if (($source = $this->getSourcePath($path1)) && $this->isCreatable(dirname($path2))) { + $source = $this->fopen($path1, 'r'); + $target = $this->fopen($path2, 'w'); + return OC_Helper::streamCopy($source, $target); } + return true; } - + public function fopen($path, $mode) { - $source = $this->getSource($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { switch ($mode) { case 'r+': case 'rb+': @@ -382,12 +360,12 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { case 'xb': case 'a': case 'ab': - if (!$this->is_writable($path)) { + if (!$this->isUpdatable($path)) { return false; } } $info = array( - 'target' => $this->datadir.$path, + 'target' => $this->sharedFolder.$path, 'source' => $source, 'mode' => $mode, ); @@ -395,95 +373,46 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { $storage = OC_Filesystem::getStorage($source); return $storage->fopen($this->getInternalPath($source), $mode); } + return false; } - - public function toTmpFile($path) { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->toTmpFile($this->getInternalPath($source)); - } - } - - public function fromTmpFile($tmpFile, $path) { - if ($this->is_writable($path)) { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - $result = $storage->fromTmpFile($tmpFile, $this->getInternalPath($source)); - return $result; - } - } else { - return false; - } - } - + public function getMimeType($path) { - if ($path == "" || $path == "/") { + if ($path == '' || $path == '/') { return 'httpd/unix-directory'; } - $source = $this->getSource($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->getMimeType($this->getInternalPath($source)); } + return false; } - - public function hash($type, $path, $raw = false) { - $source = $this->getSource($path); - if ($source) { - $storage = OC_Filesystem::getStorage($source); - return $storage->hash($type, $this->getInternalPath($source), $raw); - } - } - + public function free_space($path) { - $source = $this->getSource($path); + $source = $this->getSourcePath($path); if ($source) { $storage = OC_Filesystem::getStorage($source); return $storage->free_space($this->getInternalPath($source)); } } - - public function search($query) { - return $this->searchInDir($query); - } - - protected function searchInDir($query, $path = "") { - $files = array(); - if ($dh = $this->opendir($path)) { - while (($filename = readdir($dh)) !== false) { - if ($filename != "." && $filename != "..") { - if (strstr(strtolower($filename), strtolower($query))) { - $files[] = $path."/".$filename; - } - if ($this->is_dir($path."/".$filename)) { - $files = array_merge($files, $this->searchInDir($query, $path."/".$filename)); - } - } - } - } - return $files; - } public function getLocalFile($path) { - $source = $this->getSource($path); - if ($source) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); return $storage->getLocalFile($this->getInternalPath($source)); } + return false; } - public function touch($path, $mtime=null){ - $source = $this->getSource($path); - if ($source) { + public function touch($path, $mtime = null) { + if ($source = $this->getSourcePath($path)) { $storage = OC_Filesystem::getStorage($source); - return $storage->touch($this->getInternalPath($source),$mtime); + return $storage->touch($this->getInternalPath($source), $mtime); } + return false; } public static function setup($options) { $user_dir = $options['user_dir']; - OC_Filesystem::mount('OC_Filestorage_Shared', array('datadir' => $user_dir.'/Shared'), $user_dir.'/Shared/'); + OC_Filesystem::mount('OC_Filestorage_Shared', array('sharedFolder' => '/Shared'), $user_dir.'/Shared/'); } /** @@ -493,6 +422,6 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { */ public function hasUpdated($path,$time){ //TODO - return $this->filemtime($path)>$time; + return false; } } diff --git a/apps/files_sharing/templates/get.php b/apps/files_sharing/templates/get.php new file mode 100755 index 0000000000..57275f07a3 --- /dev/null +++ b/apps/files_sharing/templates/get.php @@ -0,0 +1,11 @@ + + + + + + + + + + +
t( 'Size' ); ?>t( 'Modified' ); ?>t('Delete all')?> <?php echo $l->t('Delete')?>" />
\ No newline at end of file diff --git a/apps/files_texteditor/js/editor.js b/apps/files_texteditor/js/editor.js index 70bb74a910..3784ea1032 100644 --- a/apps/files_texteditor/js/editor.js +++ b/apps/files_texteditor/js/editor.js @@ -222,9 +222,17 @@ function showFileEditor(dir,filename){ } }); // Add the ctrl+s event - window.aceEditor.commands.addCommand({ name: "save", bindKey: { win: "Ctrl-S", mac: "Command-S", sender: "editor" }, exec: function(){ + window.aceEditor.commands.addCommand({ + name: "save", + bindKey: { + win: "Ctrl-S", + mac: "Command-S", + sender: "editor" + }, + exec: function(){ doFileSave(); - } }); + } + }); }); } else { // Failed to get the file. @@ -297,11 +305,11 @@ $(window).resize(function() { var is_editor_shown = false; $(document).ready(function(){ if(typeof FileActions!=='undefined'){ - FileActions.register('text','Edit','',function(filename){ + FileActions.register('text','Edit', FileActions.PERMISSION_READ, '',function(filename){ showFileEditor($('#dir').val(),filename); }); FileActions.setDefault('text','Edit'); - FileActions.register('application/xml','Edit','',function(filename){ + FileActions.register('application/xml','Edit', FileActions.PERMISSION_READ, '',function(filename){ showFileEditor($('#dir').val(),filename); }); FileActions.setDefault('application/xml','Edit'); diff --git a/apps/files_versions/appinfo/api.php b/apps/files_versions/appinfo/api.php new file mode 100644 index 0000000000..a7386bc2c9 --- /dev/null +++ b/apps/files_versions/appinfo/api.php @@ -0,0 +1,36 @@ +. +*/ + +return array( + 'list' => array('method' => 'GET', 'class' => 'Storage', 'function' => 'getVersions', + 'parameters' => array( + 'file' => array('required' => true, 'type' => 'string') + ) + ), + 'revert' => array('method' => 'POST', 'class' => 'Storage', 'function' => 'rollback', + 'parameters' => array( + 'file' => array('required' => true, 'type' => 'string'), + 'time' => array('required' => true, 'type' => 'int') + ) + ) +); + +?> \ No newline at end of file diff --git a/apps/files_versions/js/versions.js b/apps/files_versions/js/versions.js index a090fde446..c5c1553f1a 100644 --- a/apps/files_versions/js/versions.js +++ b/apps/files_versions/js/versions.js @@ -11,7 +11,7 @@ $(document).ready(function() { $(document).ready(function(){ if (typeof FileActions !== 'undefined') { // Add history button to files/index.php - FileActions.register('file','History',function(){return OC.imagePath('core','actions/history')},function(filename){ + FileActions.register('file','History', FileActions.PERMISSION_UPDATE, function(){return OC.imagePath('core','actions/history')},function(filename){ if (scanFiles.scanning){return;}//workaround to prevent additional http request block scanning feedback diff --git a/apps/gallery/appinfo/app.php b/apps/gallery/appinfo/app.php index df3b68ef73..9103f66441 100644 --- a/apps/gallery/appinfo/app.php +++ b/apps/gallery/appinfo/app.php @@ -28,6 +28,9 @@ OC::$CLASSPATH['OC_Gallery_Sharing'] = 'gallery/lib/sharing.php'; OC::$CLASSPATH['OC_Gallery_Hooks_Handlers'] = 'gallery/lib/hooks_handlers.php'; OC::$CLASSPATH['Pictures_Managers'] = 'gallery/lib/managers.php'; OC::$CLASSPATH['Pictures_Tiles'] = 'gallery/lib/tiles.php'; +OC::$CLASSPATH['OC_Share_Backend_Photo'] = 'gallery/lib/share.php'; + +// OCP\Share::registerBackend('photo', new OC_Share_Backend_Photo()); $l = OC_L10N::get('gallery'); diff --git a/apps/gallery/lib/share.php b/apps/gallery/lib/share.php new file mode 100644 index 0000000000..d6c5f40d49 --- /dev/null +++ b/apps/gallery/lib/share.php @@ -0,0 +1,42 @@ +. +*/ + +abstract class OC_Share_Photo_Backend implements OCP\Share_Backend { + + public $dependsOn = 'file'; + public $supportedFileExtensions = array('jpg', 'png', 'gif'); + + public function getSource($item, $uid) { + return array('item' => 'blah.jpg', 'file' => $item); + } + + public function generateTarget($item, $uid, $exclude = null) { + // TODO Make sure target path doesn't exist already + return $item; + } + + public function formatItems($items, $format, $parameters = null) { + + } + +} + +?> \ No newline at end of file diff --git a/apps/media/js/loader.js b/apps/media/js/loader.js index 393f8ba914..ffe9c1cdd6 100644 --- a/apps/media/js/loader.js +++ b/apps/media/js/loader.js @@ -45,8 +45,8 @@ $(document).ready(function() { // FileActions.register('application/ogg','Add to playlist','',addAudio); if(typeof FileActions!=='undefined'){ - FileActions.register('audio','Play','',playAudio); - FileActions.register('application/ogg','','Play',playAudio); + FileActions.register('audio','Play', FileActions.PERMISSION_READ, '',playAudio); + FileActions.register('application/ogg', FileActions.PERMISSION_READ, '','Play',playAudio); FileActions.setDefault('audio','Play'); FileActions.setDefault('application/ogg','Play'); } diff --git a/apps/media/lib/share/album.php b/apps/media/lib/share/album.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/apps/media/lib/share/artist.php b/apps/media/lib/share/artist.php new file mode 100644 index 0000000000..7218fa1a27 --- /dev/null +++ b/apps/media/lib/share/artist.php @@ -0,0 +1,65 @@ +. +*/ + +class OC_Share_Backend_Artist extends OCP\Share_Backend { + + public function getSource($item, $uid) { + $query = OCP\DB::prepare('SELECT artist_id FROM *PREFIX*media_artists WHERE artist_id = ? AND song_user = ?'); + $result = $query->execute(array($item, $uid))->fetchRow(); + if (is_array($result)) { + return array('item' => $item, 'file' => $result['song_path']); + } + return false; + } + + public function generateTarget($item, $uid, $exclude = null) { + // TODO Make sure target path doesn't exist already + return '/Shared'.$item; + } + + public function formatItems($items, $format) { + $ids = array(); + foreach ($items as $id => $info) { + $ids[] = $id; + } + $ids = "'".implode("','", $ids)."'"; + switch ($format) { + case self::FORMAT_SOURCE_PATH: + $query = OCP\DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + return $query->execute()->fetchAll(); + case self::FORMAT_FILE_APP: + $query = OCP\DB::prepare('SELECT id, path, name, ctime, mtime, mimetype, size, encrypted, versioned, writable FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + $result = $query->execute(); + $files = array(); + while ($file = $result->fetchRow()) { + // Set target path + $file['path'] = $items[$file['id']]['item_target']; + $file['name'] = basename($file['path']); + // TODO Set permissions: $file['writable'] + $files[] = $file; + } + return $files; + } + } + +} + +?> \ No newline at end of file diff --git a/apps/media/lib/share/song.php b/apps/media/lib/share/song.php new file mode 100644 index 0000000000..fc69975f35 --- /dev/null +++ b/apps/media/lib/share/song.php @@ -0,0 +1,65 @@ +. +*/ + +class OC_Share_Backend_Song extends OCP\Share_Backend { + + public function getSource($item, $uid) { + $query = OCP\DB::prepare('SELECT song_path FROM *PREFIX*media_songs WHERE song_id = ? AND song_user = ?'); + $result = $query->execute(array($item, $uid))->fetchRow(); + if (is_array($result)) { + return array('item' => $item, 'file' => $result['song_path']); + } + return false; + } + + public function generateTarget($item, $uid, $exclude = null) { + // TODO Make sure target path doesn't exist already + return '/Shared'.$item; + } + + public function formatItems($items, $format) { + $ids = array(); + foreach ($items as $id => $info) { + $ids[] = $id; + } + $ids = "'".implode("','", $ids)."'"; + switch ($format) { + case self::FORMAT_SOURCE_PATH: + $query = OCP\DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + return $query->execute()->fetchAll(); + case self::FORMAT_FILE_APP: + $query = OCP\DB::prepare('SELECT id, path, name, ctime, mtime, mimetype, size, encrypted, versioned, writable FROM *PREFIX*fscache WHERE id IN ('.$ids.')'); + $result = $query->execute(); + $files = array(); + while ($file = $result->fetchRow()) { + // Set target path + $file['path'] = $items[$file['id']]['item_target']; + $file['name'] = basename($file['path']); + // TODO Set permissions: $file['writable'] + $files[] = $file; + } + return $files; + } + } + +} + +?> \ No newline at end of file diff --git a/core/ajax/share.php b/core/ajax/share.php new file mode 100644 index 0000000000..ee9700295e --- /dev/null +++ b/core/ajax/share.php @@ -0,0 +1,128 @@ +. +*/ +require_once '../../lib/base.php'; + +OC_JSON::checkLoggedIn(); +if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['item'])) { + switch ($_POST['action']) { + case 'share': + if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) { + try { + OCP\Share::shareItem($_POST['itemType'], $_POST['item'], $_POST['item'], (int)$_POST['shareType'], $_POST['shareWith'], $_POST['permissions']); + // TODO May need to return private link + OC_JSON::success(); + } catch (Exception $exception) { + OC_JSON::error(array('data' => array('message' => $exception->getMessage()))); + } + } + break; + case 'unshare': + if (isset($_POST['shareType']) && isset($_POST['shareWith'])) { + $return = OCP\Share::unshare($_POST['itemType'], $_POST['item'], $_POST['shareType'], $_POST['shareWith']); + ($return) ? OC_JSON::success() : OC_JSON::error(); + } + break; + case 'setTarget': + if (isset($_POST['newTarget'])) { + $return = OCP\Share::setTarget($_POST['itemType'], $_POST['item'], $_POST['newTarget']); + ($return) ? OC_JSON::success() : OC_JSON::error(); + } + break; + case 'setPermissions': + if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) { + $return = OCP\Share::setPermissions($_POST['itemType'], $_POST['item'], $_POST['shareType'], $_POST['shareWith'], $_POST['permissions']); + ($return) ? OC_JSON::success() : OC_JSON::error(); + } + break; + } +} else if (isset($_GET['fetch'])) { + switch ($_GET['fetch']) { + case 'getItemsSharedStatuses': + if (isset($_GET['itemType'])) { + $return = OCP\Share::getItemsShared($_GET['itemType'], OCP\Share::FORMAT_STATUSES); + is_array($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error(); + } + break; + case 'getItem': + // TODO Check if the item was shared to the current user + if (isset($_GET['itemType']) && isset($_GET['item'])) { + $return = OCP\Share::getItemShared($_GET['itemType'], $_GET['item']); + ($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error(); + } + break; + case 'getShareWith': + if (isset($_GET['search'])) { + $shareWith = array(); + if (OC_App::isEnabled('contacts')) { + // TODO Add function to contacts to only get the 'fullname' column to improve performance + $ids = OC_Contacts_Addressbook::activeIds(); + foreach ($ids as $id) { + $vcards = OC_Contacts_VCard::all($id); + foreach ($vcards as $vcard) { + $contact = $vcard['fullname']; + if (stripos($contact, $_GET['search']) !== false + && (!isset($_GET['itemShares']) + || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]) + || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]) + || !in_array($contact, $_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]))) { + $shareWith[] = array('label' => $contact, 'value' => array('shareType' => 5, 'shareWith' => $vcard['id'])); + } + } + } + } + $count = 0; + $users = array(); + $limit = 0; + $offset = 0; + while ($count < 4 && count($users) == $limit) { + $limit = 4 - $count; + $users = OC_User::getUsers($_GET['search'], $limit, $offset); + $offset += $limit; + foreach ($users as $user) { + if ((!isset($_GET['itemShares']) || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_USER]) || !in_array($user, $_GET['itemShares'][OCP\Share::SHARE_TYPE_USER])) && $user != OC_User::getUser()) { + $shareWith[] = array('label' => $user, 'value' => array('shareType' => OCP\Share::SHARE_TYPE_USER, 'shareWith' => $user)); + $count++; + } + } + } + $count = 0; + $groups = OC_Group::getUserGroups(OC_User::getUser()); + foreach ($groups as $group) { + if ($count < 4) { + if (stripos($group, $_GET['search']) !== false + && (!isset($_GET['itemShares']) + || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]) + || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]) + || !in_array($group, $_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]))) { + $shareWith[] = array('label' => $group.' (group)', 'value' => array('shareType' => OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => $group)); + $count++; + } + } else { + break; + } + } + OC_JSON::success(array('data' => $shareWith)); + } + break; + } +} + +?> \ No newline at end of file diff --git a/core/js/share.js b/core/js/share.js new file mode 100644 index 0000000000..bd145eab41 --- /dev/null +++ b/core/js/share.js @@ -0,0 +1,419 @@ +OC.Share={ + SHARE_TYPE_USER:0, + SHARE_TYPE_GROUP:1, + SHARE_TYPE_PRIVATE_LINK:3, + SHARE_TYPE_EMAIL:4, + PERMISSION_CREATE:4, + PERMISSION_READ:1, + PERMISSION_UPDATE:2, + PERMISSION_DELETE:8, + PERMISSION_SHARE:16, + itemShares:[], + statuses:[], + droppedDown:false, + loadIcons:function(itemType) { + // Load all share icons + $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getItemsSharedStatuses', itemType: itemType }, function(result) { + if (result && result.status === 'success') { + $.each(result.data, function(item, hasPrivateLink) { + // Private links override shared in terms of icon display + if (itemType != 'file' && itemType != 'folder') { + if (hasPrivateLink) { + var image = OC.imagePath('core', 'actions/public'); + } else { + var image = OC.imagePath('core', 'actions/shared'); + } + $('a.share[data-item="'+item+'"]').css('background', 'url('+image+') no-repeat center'); + } + OC.Share.statuses[item] = hasPrivateLink; + }); + } + }); + }, + loadItem:function(itemType, item) { + var data = ''; + if (typeof OC.Share.statuses[item] !== 'undefined') { + $.ajax({type: 'GET', url: OC.filePath('core', 'ajax', 'share.php'), data: { fetch: 'getItem', itemType: itemType, item: item }, async: false, success: function(result) { + if (result && result.status === 'success') { + data = result.data; + } else { + data = false; + } + }}); + } + return data; + }, + share:function(itemType, item, shareType, shareWith, permissions, callback) { + $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'share', itemType: itemType, item: item, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) { + if (result && result.status === 'success') { + if (callback) { + callback(result.data); + } + } else { + OC.dialogs.alert(result.data.message, 'Error while sharing'); + } + }); + }, + unshare:function(itemType, item, shareType, shareWith, callback) { + $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'unshare', itemType: itemType, item: item, shareType: shareType, shareWith: shareWith }, function(result) { + if (result && result.status === 'success') { + if (callback) { + callback(); + } + } else { + OC.dialogs.alert('Error', 'Error while unsharing'); + } + }); + }, + setPermissions:function(itemType, item, shareType, shareWith, permissions) { + $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setPermissions', itemType: itemType, item: item, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) { + if (!result || result.status !== 'success') { + OC.dialogs.alert('Error', 'Error while changing permissions'); + } + }); + }, + showDropDown:function(itemType, item, appendTo, privateLink, possiblePermissions) { + var html = ''; + $(html).appendTo(appendTo); + // Reset item shares + OC.Share.itemShares = []; + var data = OC.Share.loadItem(itemType, item); + if (data) { + $.each(data, function(index, share) { + if (share.share_type == OC.Share.SHARE_TYPE_PRIVATE_LINK) { + OC.Share.showPrivateLink(item, share.share_with); + } else { + OC.Share.addShareWith(share.share_type, share.share_with, share.permissions, possiblePermissions); + + } + }); + } + $('#shareWith').autocomplete({minLength: 2, source: function(search, response) { +// if (cache[search.term]) { +// response(cache[search.term]); +// } else { + $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWith', search: search.term, itemShares: OC.Share.itemShares }, function(result) { + if (result.status == 'success' && result.data.length > 0) { + response(result.data); + } else { + // Suggest sharing via email if valid email address + var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i); + if (pattern.test(search.term)) { + response([{label: 'Share via email: '+search.term, value: {shareType: OC.Share.SHARE_TYPE_EMAIL, shareWith: search.term}}]); + } else { + response(['No people found']); + } + } + }); +// } + }, + focus: function(event, focused) { + event.preventDefault(); + }, + select: function(event, selected) { + var shareType = selected.item.value.shareType; + var shareWith = selected.item.value.shareWith; + $(this).val(shareWith); + // Default permissions are Read and Share + var permissions = OC.Share.PERMISSION_READ | OC.Share.PERMISSION_SHARE; + OC.Share.share($('#dropdown').data('item-type'), $('#dropdown').data('item'), shareType, shareWith, permissions, function() { + OC.Share.addShareWith(shareType, shareWith, permissions, possiblePermissions); + $('#shareWith').val(''); + }); + return false; + } + }); + $('#dropdown').show('blind', function() { + OC.Share.droppedDown = true; + }); + $('#shareWith').focus(); + }, + hideDropDown:function(callback) { + $('#dropdown').hide('blind', function() { + OC.Share.droppedDown = false; + $('#dropdown').remove(); + if (typeof FileActions !== 'undefined') { + $('tr').removeClass('mouseOver'); + } + if (callback) { + callback.call(); + } + }); + }, + addShareWith:function(shareType, shareWith, permissions, possiblePermissions) { + if (!OC.Share.itemShares[shareType]) { + OC.Share.itemShares[shareType] = []; + } + OC.Share.itemShares[shareType].push(shareWith); + var editChecked = createChecked = updateChecked = deleteChecked = shareChecked = ''; + if (permissions & OC.Share.PERMISSION_CREATE) { + createChecked = 'checked="checked"'; + editChecked = 'checked="checked"'; + } + if (permissions & OC.Share.PERMISSION_UPDATE) { + updateChecked = 'checked="checked"'; + editChecked = 'checked="checked"'; + } + if (permissions & OC.Share.PERMISSION_DELETE) { + deleteChecked = 'checked="checked"'; + editChecked = 'checked="checked"'; + } + if (permissions & OC.Share.PERMISSION_SHARE) { + shareChecked = 'checked="checked"'; + } + var html = '
  • '; + html += shareWith; + if (possiblePermissions & OC.Share.PERMISSION_CREATE || possiblePermissions & OC.Share.PERMISSION_UPDATE || possiblePermissions & OC.Share.PERMISSION_DELETE) { + if (editChecked == '') { + html += ''; + html += ''; + html += ''; + html += '
  • '; + $(html).appendTo('#shareWithList'); + + }, + showPrivateLink:function(item, token) { + $('#privateLinkCheckbox').attr('checked', true); + var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service=files&token='+token; + if (token.indexOf('&path=') == -1) { + link += '&file=' + encodeURIComponent(item).replace(/%2F/g, '/'); + } else { + // Disable checkbox if inside a shared parent folder + $('#privateLinkCheckbox').attr('disabled', 'true'); + } + $('#privateLinkText').val(link); + $('#privateLinkText').show('blind', function() { + $('#privateLinkText').after('
    '); + $('#email').show(); + $('#emailButton').show(); + }); + }, + hidePrivateLink:function() { + $('#privateLinkText').hide('blind'); + $('#emailBreak').remove(); + $('#email').hide(); + $('#emailButton').hide(); + }, + emailPrivateLink:function() { + var link = $('#privateLinkText').val(); + var file = link.substr(link.lastIndexOf('/') + 1).replace(/%20/g, ' '); + $.post(OC.filePath('files_sharing', 'ajax', 'email.php'), { toaddress: $('#email').val(), link: link, file: file } ); + $('#email').css('font-weight', 'bold'); + $('#email').animate({ fontWeight: 'normal' }, 2000, function() { + $(this).val(''); + }).val('Email sent'); + }, + dirname:function(path) { + return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, ''); + } +} + +$(document).ready(function() { + + $('a.share').live('click', function(event) { + event.stopPropagation(); + if ($(this).data('item-type') !== undefined && $(this).data('item') !== undefined) { + var itemType = $(this).data('item-type'); + var item = $(this).data('item'); + var appendTo = $(this).parent().parent(); + var privateLink = false; + var possiblePermissions = $(this).data('possible-permissions'); + if ($(this).data('private-link') !== undefined && $(this).data('private-link') == true) { + privateLink = true; + } + if (OC.Share.droppedDown) { + if (item != $('#dropdown').data('item')) { + OC.Share.hideDropDown(function () { + OC.Share.showDropDown(itemType, item, appendTo, privateLink, possiblePermissions); + }); + } else { + OC.Share.hideDropDown(); + } + } else { + OC.Share.showDropDown(itemType, item, appendTo, privateLink, possiblePermissions); + } + } + }); + + if (typeof FileActions !== 'undefined') { + OC.Share.loadIcons('file'); + FileActions.register('all', 'Share', FileActions.PERMISSION_SHARE, function(filename) { + // Return the correct sharing icon + if (scanFiles.scanning) { return; } // workaround to prevent additional http request block scanning feedback + var item = $('#dir').val() + filename; + // Check if status is in cache + if (OC.Share.statuses[item] === true) { + return OC.imagePath('core', 'actions/public'); + } else if (OC.Share.statuses[item] === false) { + return OC.imagePath('core', 'actions/shared'); + } else { + var last = ''; + var path = OC.Share.dirname(item); + // Search for possible parent folders that are shared + while (path != last) { + if (OC.Share.statuses[path] === true) { + return OC.imagePath('core', 'actions/public'); + } else if (OC.Share.statuses[path] === false) { + return OC.imagePath('core', 'actions/shared'); + } + last = path; + path = OC.Share.dirname(path); + } + return OC.imagePath('core', 'actions/share'); + } + }, function(filename) { + var item = $('#dir').val() + filename; + if ($('tr').filterAttr('data-file', filename).data('type') == 'dir') { + var itemType = 'folder'; + var possiblePermissions = OC.Share.PERMISSION_CREATE | OC.Share.PERMISSION_UPDATE | OC.Share.PERMISSION_DELETE | OC.Share.PERMISSION_SHARE; + } else { + var itemType = 'file'; + var possiblePermissions = OC.Share.PERMISSION_UPDATE | OC.Share.PERMISSION_DELETE | OC.Share.PERMISSION_SHARE; + } + var appendTo = $('tr').filterAttr('data-file', filename).find('td.filename'); + // Check if drop down is already visible for a different file + if (OC.Share.droppedDown) { + if (item != $('#dropdown').data('item')) { + OC.Share.hideDropDown(function () { + $('tr').filterAttr('data-file', filename).addClass('mouseOver'); + OC.Share.showDropDown(itemType, item, appendTo, true, possiblePermissions); + }); + } else { + OC.Share.hideDropDown(); + } + } else { + $('tr').filterAttr('data-file',filename).addClass('mouseOver'); + OC.Share.showDropDown(itemType, item, appendTo, true, possiblePermissions); + } + }); + } + + $(this).click(function(event) { + if (OC.Share.droppedDown && !($(event.target).hasClass('drop')) && $('#dropdown').has(event.target).length === 0) { + OC.Share.hideDropDown(); + } + }); + + $('#shareWithList li').live('mouseenter', function(event) { + // Show permissions and unshare button + $(':hidden', this).filter(':not(.cruds)').show(); + }); + + $('#shareWithList li').live('mouseleave', function(event) { + // Hide permissions and unshare button + if (!$('.cruds', this).is(':visible')) { + $('a', this).hide(); + if (!$('input[name="edit"]', this).is(':checked')) { + $('input:[type=checkbox]', this).hide(); + $('label', this).hide(); + } + } else { + $('a.unshare', this).hide(); + } + }); + + $('.showCruds').live('click', function() { + $(this).parent().find('.cruds').toggle(); + }); + + $('.unshare').live('click', function() { + var li = $(this).parent(); + var shareType = $(li).data('share-type'); + var shareWith = $(li).data('share-with'); + OC.Share.unshare($('#dropdown').data('item-type'), $('#dropdown').data('item'), shareType, shareWith, function() { + $(li).remove(); + var index = OC.Share.itemShares[shareType].indexOf(shareWith); + OC.Share.itemShares[shareType].splice(index, 1); + }); + }); + + $('.permissions').live('change', function() { + if ($(this).attr('name') == 'edit') { + var li = $(this).parent().parent() + var checkboxes = $('.permissions', li); + var checked = $(this).is(':checked'); + // Check/uncheck Create, Update, and Delete checkboxes if Edit is checked/unck + $(checkboxes).filter('input[name="create"]').attr('checked', checked); + $(checkboxes).filter('input[name="update"]').attr('checked', checked); + $(checkboxes).filter('input[name="delete"]').attr('checked', checked); + } else { + var li = $(this).parent().parent().parent(); + var checkboxes = $('.permissions', li); + // Uncheck Edit if Create, Update, and Delete are not checked + if (!$(this).is(':checked') && !$(checkboxes).filter('input[name="create"]').is(':checked') && !$(checkboxes).filter('input[name="update"]').is(':checked') && !$(checkboxes).filter('input[name="delete"]').is(':checked')) { + $(checkboxes).filter('input[name="edit"]').attr('checked', false); + // Check Edit if Create, Update, or Delete is checked + } else if (($(this).attr('name') == 'create' || $(this).attr('name') == 'update' || $(this).attr('name') == 'delete')) { + $(checkboxes).filter('input[name="edit"]').attr('checked', true); + } + } + var permissions = OC.Share.PERMISSION_READ; + $(checkboxes).filter(':not(input[name="edit"])').filter(':checked').each(function(index, checkbox) { + permissions |= $(checkbox).data('permissions'); + }); + OC.Share.setPermissions($('#dropdown').data('item-type'), $('#dropdown').data('item'), $(li).data('share-type'), $(li).data('share-with'), permissions); + }); + + $('#privateLinkCheckbox').live('change', function() { + var itemType = $('#dropdown').data('item-type'); + var item = $('#dropdown').data('item'); + if (this.checked) { + // Create a private link + OC.Share.share(itemType, item, OC.Share.SHARE_TYPE_PRIVATE_LINK, 0, 0, function(token) { + OC.Share.showPrivateLink(item, 'foo'); + // Change icon + OC.Share.icons[item] = OC.imagePath('core', 'actions/public'); + }); + } else { + // Delete private link + OC.Share.unshare(item, 'public', function() { + OC.Share.hidePrivateLink(); + // Change icon + if (OC.Share.itemUsers || OC.Share.itemGroups) { + OC.Share.icons[item] = OC.imagePath('core', 'actions/shared'); + } else { + OC.Share.icons[item] = OC.imagePath('core', 'actions/share'); + } + }); + } + }); + + $('#privateLinkText').live('click', function() { + $(this).focus(); + $(this).select(); + }); + + $('#emailPrivateLink').live('submit', function() { + OC.Share.emailPrivateLink(); + }); +}); diff --git a/db_structure.xml b/db_structure.xml index 5a783d41a9..a5470a2d04 100644 --- a/db_structure.xml +++ b/db_structure.xml @@ -506,6 +506,121 @@ + + + *dbprefix*share + + + + + id + 1 + integer + 0 + true + 4 + + + + share_type + integer + + true + 1 + + + + share_with + text + + true + 255 + + + + uid_owner + text + + true + 255 + + + + parent + integer + + false + 4 + + + + item_type + text + + true + 64 + + + + item_source + text + + false + 255 + + + + item_target + text + + false + 255 + + + + file_source + integer + + false + 4 + + + + file_target + text + + false + 512 + + + + permissions + integer + + true + 1 + + + + stime + integer + + true + 8 + + + + accepted + integer + 0 + true + 1 + + + + +
    + *dbprefix*queuedtasks diff --git a/lib/filecache.php b/lib/filecache.php index e8b17e254e..352fc695f3 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -358,6 +358,10 @@ class OC_FileCache{ $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); } $lastSend=$count; + // NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache) + if (substr($path, 0, 7) == '/Shared') { + return; + } if($root===false){ $view=OC_Filesystem::getView(); }else{ @@ -395,6 +399,10 @@ class OC_FileCache{ * @return int size of the scanned file */ public static function scanFile($path,$root=false){ + // NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache) + if (substr($path, 0, 7) == '/Shared') { + return; + } if($root===false){ $view=OC_Filesystem::getView(); }else{ diff --git a/lib/files.php b/lib/files.php index b9c2ead944..ce7cf2c446 100644 --- a/lib/files.php +++ b/lib/files.php @@ -37,10 +37,38 @@ class OC_Files { if($directory=='/'){ $directory=''; } - $files=OC_FileCache::getFolderContent($directory, false, $mimetype_filter); - foreach($files as &$file){ - $file['directory']=$directory; - $file['type']=($file['mimetype']=='httpd/unix-directory')?'dir':'file'; + $files = array(); + if (substr($directory, 0, 7) == '/Shared') { + if ($directory == '/Shared') { + $files = OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter)); + } else { + $pos = strpos($directory, '/', 8); + // Get shared folder name + if ($pos !== false) { + $itemTarget = substr($directory, 7, $pos - 7); + } else { + $itemTarget = substr($directory, 7); + } + $files = OCP\Share::getItemSharedWith('folder', $itemTarget, OC_Share_Backend_File::FORMAT_FILE_APP, array('folder' => $directory, 'mimetype_filter' => $mimetype_filter)); + } + } else { + $files = OC_FileCache::getFolderContent($directory, false, $mimetype_filter); + foreach ($files as &$file) { + $file['directory'] = $directory; + $file['type'] = ($file['mimetype'] == 'httpd/unix-directory') ? 'dir' : 'file'; + $permissions = OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE; + if ($file['type'] == 'dir' && $file['writable']) { + $permissions |= OCP\Share::PERMISSION_CREATE; + } + if ($file['writable']) { + $permissions |= OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE; + } + $file['permissions'] = $permissions; + } + if ($directory == '') { + // Add 'Shared' folder + $files = array_merge($files, OCP\Share::getItemsSharedWith('file', OC_Share_Backend_File::FORMAT_FILE_APP_ROOT)); + } } usort($files, "fileCmp");//TODO: remove this once ajax is merged return $files; @@ -108,8 +136,7 @@ class OC_Files { header('Content-Type: application/zip'); header('Content-Length: ' . filesize($filename)); }else{ - $fileData=OC_FileCache::get($filename); - header('Content-Type: ' . $fileData['mimetype']); + header('Content-Type: '.OC_Filesystem::getMimeType($filename)); } }elseif($zip or !OC_Filesystem::file_exists($filename)){ header("HTTP/1.0 404 Not Found"); diff --git a/lib/filestorage.php b/lib/filestorage.php index 672b9cb0d7..5bfd09253d 100644 --- a/lib/filestorage.php +++ b/lib/filestorage.php @@ -33,8 +33,11 @@ abstract class OC_Filestorage{ abstract public function stat($path); abstract public function filetype($path); abstract public function filesize($path); - abstract public function is_readable($path); - abstract public function is_writable($path); + abstract public function isCreatable($path); + abstract public function isReadable($path); + abstract public function isUpdatable($path); + abstract public function isDeletable($path); + abstract public function isSharable($path); abstract public function file_exists($path); abstract public function filectime($path); abstract public function filemtime($path); diff --git a/lib/filestorage/common.php b/lib/filestorage/common.php index 4f506a3149..c829be62f7 100644 --- a/lib/filestorage/common.php +++ b/lib/filestorage/common.php @@ -54,8 +54,17 @@ abstract class OC_Filestorage_Common extends OC_Filestorage { return $stat['size']; } } -// abstract public function is_readable($path); -// abstract public function is_writable($path); + public function isCreatable($path) { + return $this->isUpdatable($path); + } +// abstract public function isReadable($path); +// abstract public function isUpdatable($path); + public function isDeletable($path) { + return $this->isUpdatable($path); + } + public function isSharable($path) { + return $this->isReadable($path); + } // abstract public function file_exists($path); public function filectime($path) { $stat = $this->stat($path); diff --git a/lib/filestorage/commontest.php b/lib/filestorage/commontest.php index 1b01ff856a..b5126a407b 100644 --- a/lib/filestorage/commontest.php +++ b/lib/filestorage/commontest.php @@ -51,11 +51,11 @@ class OC_Filestorage_CommonTest extends OC_Filestorage_Common{ public function filetype($path){ return $this->storage->filetype($path); } - public function is_readable($path){ - return $this->storage->is_readable($path); + public function isReadable($path){ + return $this->storage->isReadable($path); } - public function is_writable($path){ - return $this->storage->is_writable($path); + public function isUpdatable($path){ + return $this->storage->isUpdatable($path); } public function file_exists($path){ return $this->storage->file_exists($path); diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index 2087663809..22d17469df 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -45,10 +45,10 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ return filesize($this->datadir.$path); } } - public function is_readable($path){ + public function isReadable($path){ return is_readable($this->datadir.$path); } - public function is_writable($path){ + public function isUpdatable($path){ return is_writable($this->datadir.$path); } public function file_exists($path){ @@ -85,7 +85,7 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ return $this->delTree($path); } public function rename($path1,$path2){ - if (!$this->is_writable($path1)) { + if (!$this->isUpdatable($path1)) { OC_Log::write('core','unable to rename, file is not writable : '.$path1,OC_Log::ERROR); return false; } @@ -128,7 +128,7 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ } public function getMimeType($path){ - if($this->is_readable($path)){ + if($this->isReadable($path)){ return OC_Helper::getMimeType($this->datadir.$path); }else{ return false; diff --git a/lib/filesystem.php b/lib/filesystem.php index 6f11cda4fd..82fbf11afd 100644 --- a/lib/filesystem.php +++ b/lib/filesystem.php @@ -426,12 +426,33 @@ class OC_Filesystem{ static public function readfile($path){ return self::$defaultInstance->readfile($path); } + /** + * @deprecated Replaced by isReadable() as part of CRUDS + */ static public function is_readable($path){ return self::$defaultInstance->is_readable($path); } + /** + * @deprecated Replaced by isCreatable(), isUpdatable(), isDeletable() as part of CRUDS + */ static public function is_writable($path){ return self::$defaultInstance->is_writable($path); } + static public function isCreatable($path) { + return self::$defaultInstance->isCreatable($path); + } + static public function isReadable($path) { + return self::$defaultInstance->isReadable($path); + } + static public function isUpdatable($path) { + return self::$defaultInstance->isUpdatable($path); + } + static public function isDeletable($path) { + return self::$defaultInstance->isDeletable($path); + } + static public function isSharable($path) { + return self::$defaultInstance->isSharable($path); + } static public function file_exists($path){ return self::$defaultInstance->file_exists($path); } diff --git a/lib/filesystemview.php b/lib/filesystemview.php index 6e76e1b6da..a888e5340e 100644 --- a/lib/filesystemview.php +++ b/lib/filesystemview.php @@ -211,11 +211,32 @@ class OC_FilesystemView { } return false; } - public function is_readable($path) { - return $this->basicOperation('is_readable', $path); + /** + * @deprecated Replaced by isReadable() as part of CRUDS + */ + public function is_readable($path){ + return $this->basicOperation('isReadable',$path); } - public function is_writable($path) { - return $this->basicOperation('is_writable', $path); + /** + * @deprecated Replaced by isCreatable(), isUpdatable(), isDeletable() as part of CRUDS + */ + public function is_writable($path){ + return $this->basicOperation('isUpdatable',$path); + } + public function isCreatable($path) { + return $this->basicOperation('isCreatable', $path); + } + public function isReadable($path) { + return $this->basicOperation('isReadable', $path); + } + public function isUpdatable($path) { + return $this->basicOperation('isUpdatable', $path); + } + public function isDeletable($path) { + return $this->basicOperation('isDeletable', $path); + } + public function isSharable($path) { + return $this->basicOperation('isSharable', $path); } public function file_exists($path) { if($path=='/'){ diff --git a/lib/group/backend.php b/lib/group/backend.php index 4c7d09bcb1..5969986c65 100644 --- a/lib/group/backend.php +++ b/lib/group/backend.php @@ -105,6 +105,7 @@ abstract class OC_Group_Backend implements OC_Group_Interface { * * Returns a list with all groups */ + public function getGroups($search = '', $limit = -1, $offset = 0) { return array(); } @@ -115,7 +116,7 @@ abstract class OC_Group_Backend implements OC_Group_Interface { * @return bool */ public function groupExists($gid){ - return in_array($gid, $this->getGroups()); + return in_array($gid, $this->getGroups($gid, 1)); } /** diff --git a/lib/group/database.php b/lib/group/database.php index 1cb4171f49..0b4ae393cf 100644 --- a/lib/group/database.php +++ b/lib/group/database.php @@ -178,6 +178,20 @@ class OC_Group_Database extends OC_Group_Backend { return $groups; } + /** + * check if a group exists + * @param string $gid + * @return bool + */ + public function groupExists($gid) { + $query = OC_DB::prepare('SELECT gid FROM *PREFIX*groups WHERE gid = ?'); + $result = $query->execute(array($gid))->fetchOne(); + if ($result) { + return true; + } + return false; + } + /** * @brief get a list of all users in a group * @returns array with user ids diff --git a/lib/public/share.php b/lib/public/share.php new file mode 100644 index 0000000000..c57016fb98 --- /dev/null +++ b/lib/public/share.php @@ -0,0 +1,1054 @@ +. +*/ +namespace OCP; + +\OC_Hook::connect('OC_User', 'post_deleteUser', 'OCP\Share', 'post_deleteUser'); +\OC_Hook::connect('OC_User', 'post_addToGroup', 'OCP\Share', 'post_addToGroup'); +\OC_Hook::connect('OC_User', 'post_removeFromGroup', 'OCP\Share', 'post_removeFromGroup'); + +/** +* This class provides the ability for apps to share their content between users. +* Apps must create a backend class that implements OCP\Share_Backend and register it with this class. +*/ +class Share { + + const SHARE_TYPE_USER = 0; + const SHARE_TYPE_GROUP = 1; + const SHARE_TYPE_PRIVATE_LINK = 3; + const SHARE_TYPE_EMAIL = 4; + const SHARE_TYPE_CONTACT = 5; + const SHARE_TYPE_REMOTE = 6; + + /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask + * Construct permissions for share() and setPermissions with Or (|) e.g. Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE + * Check if permission is granted with And (&) e.g. Check if delete is granted: if ($permissions & PERMISSION_DELETE) + * Remove permissions with And (&) and Not (~) e.g. Remove the update permission: $permissions &= ~PERMISSION_UPDATE + * Apps are required to handle permissions on their own, this class only stores and manages the permissions of shares + */ + const PERMISSION_CREATE = 4; + const PERMISSION_READ = 1; + const PERMISSION_UPDATE = 2; + const PERMISSION_DELETE = 8; + const PERMISSION_SHARE = 16; + + const FORMAT_NONE = -1; + const FORMAT_STATUSES = -2; + const FORMAT_SOURCES = -3; + + private static $shareTypeUserAndGroups = -1; + private static $shareTypeGroupUserUnique = 2; + private static $backends = array(); + private static $backendTypes = array(); + + /** + * @brief Register a sharing backend class that implements OCP\Share_Backend for an item type + * @param string Item type + * @param string Backend class + * @param string (optional) Depends on item type + * @param array (optional) List of supported file extensions if this item type depends on files + * @return Returns true if backend is registered or false if error + */ + public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { + if (!isset(self::$backendTypes[$itemType])) { + self::$backendTypes[$itemType] = array('class' => $class, 'collectionOf' => $collectionOf, 'supportedFileExtensions' => $supportedFileExtensions); + return true; + } + \OC_Log::write('OCP\Share', 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'].' is already registered for '.$itemType, \OC_Log::WARN); + return false; + } + + /** + * @brief Get the items of item type shared with the current user + * @param string Item type + * @param int Format (optional) Format type must be defined by the backend + * @param int Number of items to return (optional) Returns all by default + * @return Return depends on format + */ + public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, $includeCollections = false) { + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $parameters, $limit, $includeCollections); + } + + /** + * @brief Get the item of item type shared with the current user + * @param string Item type + * @param string Item target + * @param int Format (optional) Format type must be defined by the backend + * @return Return depends on format + */ + public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE, $parameters = null, $includeCollections = false) { + return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $parameters, 1, $includeCollections); + } + + /** + * @brief Get the item of item type shared with the current user by source + * @param string Item type + * @param string Item source + * @param int Format (optional) Format type must be defined by the backend + * @return Return depends on format + */ + public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, $parameters = null, $includeCollections = false) { + return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, $parameters, 1, $includeCollections, true); + } + + /** + * @brief Get the shared items of item type owned by the current user + * @param string Item type + * @param int Format (optional) Format type must be defined by the backend + * @param int Number of items to return (optional) Returns all by default + * @return Return depends on format + */ + public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, $includeCollections = false) { + return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, $parameters, $limit, $includeCollections); + } + + /** + * @brief Get the shared item of item type owned by the current user + * @param string Item type + * @param string Item source + * @param int Format (optional) Format type must be defined by the backend + * @return Return depends on format + */ + public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, $parameters = null, $includeCollections = false) { + return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, $parameters, -1, $includeCollections); + } + + /** + * @brief Share an item with a user, group, or via private link + * @param string Item type + * @param string Item source + * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_PRIVATE_LINK + * @param string User or group the item is being shared with + * @param int CRUDS permissions + * @return bool Returns true on success or false on failure + */ + public static function shareItem($itemType, $itemName, $itemSource, $shareType, $shareWith, $permissions) { + $uidOwner = \OC_User::getUser(); + // Verify share type and sharing conditions are met + if ($shareType === self::SHARE_TYPE_USER) { + if ($shareWith == $uidOwner) { + $message = 'Sharing '.$itemSource.' failed, because the user '.$shareWith.' is the item owner'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + if (!\OC_User::userExists($shareWith)) { + $message = 'Sharing '.$itemSource.' failed, because the user '.$shareWith.' does not exist'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } else { + $inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith)); + if (empty($inGroup)) { + $message = 'Sharing '.$itemSource.' failed, because the user '.$shareWith.' is not a member of any groups that '.$uidOwner.' is a member of'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } + } else if ($shareType === self::SHARE_TYPE_GROUP) { + if (!\OC_Group::groupExists($shareWith)) { + $message = 'Sharing '.$itemSource.' failed, because the group '.$shareWith.' does not exist'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } else if (!\OC_Group::inGroup($uidOwner, $shareWith)) { + $message = 'Sharing '.$itemSource.' failed, because '.$uidOwner.' is not a member of the group '.$shareWith; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } else if ($shareType === self::SHARE_TYPE_PRIVATE_LINK) { + $shareWith = md5(uniqid($itemSource, true)); + return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions); + } else if ($shareType === self::SHARE_TYPE_CONTACT) { + if (!\OC_App::isEnabled('contacts')) { + $message = 'Sharing '.$itemSource.' failed, because the contacts app is not enabled'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + return false; + } + $vcard = \OC_Contacts_App::getContactVCard($shareWith); + if (!isset($vcard)) { + $message = 'Sharing '.$itemSource.' failed, because the contact does not exist'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + $details = \OC_Contacts_VCard::structureContact($vcard); + // TODO Add ownCloud user to contacts vcard + if (!isset($details['EMAIL'])) { + $message = 'Sharing '.$itemSource.' failed, because no email address is associated with the contact'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + return self::shareItem($itemType, $itemName, $itemSource, self::SHARE_TYPE_EMAIL, $details['EMAIL'], $permissions); + } else { + // Future share types need to include their own conditions + $message = 'Share type '.$shareType.' is not valid for '.$itemSource; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + // TODO This query has pretty bad performance if there are large collections, figure out a way to make the collection searching more efficient + if (self::getItems($itemType, $itemSource, $shareType, $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true)) { + $message = 'Sharing '.$itemSource.' failed, because this item is already shared with '.$shareWith; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + if ($shareType == self::SHARE_TYPE_GROUP) { + // Convert share with into an array with the keys group and users + $group = $shareWith; + $shareWith = array(); + $shareWith['group'] = $group; + $shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner)); + } + // If the item is a folder, scan through the folder looking for equivalent item types + if ($itemType == 'folder') { + $parentFolder = self::put('folder', $itemSource, $shareType, $shareWith, $uidOwner, $permissions, true); + if ($parentFolder && $files = \OC_Files::getDirectoryContent($itemSource)) { + for ($i = 0; $i < count($files); $i++) { + $name = substr($files[$i]['name'], strpos($files[$i]['name'], $itemSource) - strlen($itemSource)); + if ($files[$i]['mimetype'] == 'httpd/unix-directory' && $children = \OC_Files::getDirectoryContent($name, '/')) { + // Continue scanning into child folders + array_push($files, $children); + } else { + // Pass on to put() to check if this item should be converted, the item won't be inserted into the database unless it can be converted + self::put('file', $name, $name, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder); + } + } + return true; + } + return false; + } else { + // Put the item into the database + return self::put($itemType, $itemName, $itemSource, $shareType, $shareWith, $uidOwner, $permissions); + } + } + + /** + * @brief Unshare an item from a user, group, or delete a private link + * @param string Item type + * @param string Item source + * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_PRIVATE_LINK + * @param string User or group the item is being shared with + * @return Returns true on success or false on failure + */ + public static function unshare($itemType, $itemSource, $shareType, $shareWith) { + if ($item = self::getItems($itemType, $itemSource, $shareType, $shareWith, \OC_User::getUser(), self::FORMAT_NONE, null, 1)) { + self::delete($item['id']); + return true; + } + return false; + } + + /** + * @brief Unshare an item shared with the current user + * @param string Item type + * @param string Item target + * @return Returns true on success or false on failure + * + * Unsharing from self is not allowed for items inside collections + * + */ + public static function unshareFromSelf($itemType, $itemTarget) { + if ($item = self::getItemSharedWith($itemType, $itemTarget)) { + if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { + // TODO + } + // Delete + return self::delete($item['id'], true); + } + return false; + } + + /** + * @brief Set the permissions of an item for a specific user or group + * @param string Item type + * @param string Item source + * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_PRIVATE_LINK + * @param string User or group the item is being shared with + * @param int CRUDS permissions + * @return Returns true on success or false on failure + */ + public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) { + if ($item = self::getItems($itemType, $itemSource, $shareType, $shareWith, \OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) { + // Check if this item is a reshare and verify that the permissions granted don't exceed the parent shared item + if (isset($item['parent'])) { + $query = \OC_DB::prepare('SELECT permissions FROM *PREFIX*share WHERE id = ? LIMIT 1'); + $result = $query->execute(array($item['parent']))->fetchRow(); + if (~(int)$result['permissions'] & $permissions) { + $message = 'Setting permissions for '.$itemSource.' failed, because the permissions exceed permissions granted to '.\OC_User::getUser(); + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } + $query = \OC_DB::prepare('UPDATE *PREFIX*share SET permissions = ? WHERE id = ?'); + $query->execute(array($permissions, $item['id'])); + // Check if permissions were removed + if ($item['permissions'] & ~$permissions) { + // If share permission is removed all reshares must be deleted + if (($item['permissions'] & self::PERMISSION_SHARE) && (~$permissions & self::PERMISSION_SHARE)) { + self::delete($item['id'], true); + } else { + $ids = array(); + $parents = array($item['id']); + while (!empty($parents)) { + $parents = "'".implode("','", $parents)."'"; + $query = \OC_DB::prepare('SELECT id, permissions FROM *PREFIX*share WHERE parent IN ('.$parents.')'); + $result = $query->execute(); + // Reset parents array, only go through loop again if items are found that need permissions removed + $parents = array(); + while ($item = $result->fetchRow()) { + // Check if permissions need to be removed + if ($item['permissions'] & ~$permissions) { + // Add to list of items that need permissions removed + $ids[] = $item['id']; + $parents[] = $item['id']; + } + } + } + // Remove the permissions for all reshares of this item + if (!empty($ids)) { + $ids = "'".implode("','", $ids)."'"; + $query = \OC_DB::prepare('UPDATE *PREFIX*share SET permissions = permissions & ? WHERE id IN ('.$ids.')'); + $query->execute(array($permissions)); + } + } + } + return true; + } + $message = 'Setting permissions for '.$itemSource.' failed, because the item was not found'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + + /** + * @brief Get the backend class for the specified item type + * @param string Item type + * @return Sharing backend object + */ + private static function getBackend($itemType) { + if (isset(self::$backends[$itemType])) { + return self::$backends[$itemType]; + } else if (isset(self::$backendTypes[$itemType]['class'])) { + $class = self::$backendTypes[$itemType]['class']; + if (class_exists($class)) { + self::$backends[$itemType] = new $class; + if (!(self::$backends[$itemType] instanceof Share_Backend)) { + $message = 'Sharing backend '.$class.' must implement the interface OCP\Share_Backend'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + return self::$backends[$itemType]; + } else { + $message = 'Sharing backend '.$class.' not found'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } + $message = 'Sharing backend for '.$itemType.' not found'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + + /** + * @brief Get a list of collection item types for the specified item type + * @param string Item type + * @return array + */ + private static function getCollectionItemTypes($itemType) { + $collectionTypes = array($itemType); + foreach (self::$backendTypes as $type => $backend) { + if (in_array($backend['collectionOf'], $collectionTypes)) { + $collectionTypes[] = $type; + } + } + if (count($collectionTypes) > 1) { + unset($collectionTypes[0]); + return $collectionTypes; + } + return false; + } + + /** + * @brief Get shared items from the database + * @param string Item type + * @param string Item source or target (optional) + * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_PRIVATE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique + * @param string User or group the item is being shared with + * @param string User that is the owner of shared items (optional) + * @param int Format to convert items to with formatItems() + * @param mixed Parameters to pass to formatItems() + * @param int Number of items to return, -1 to return all matches (optional) + * @param bool Include collection item types (optional) + * @return mixed + * + * See public functions getItem(s)... for parameter usage + * + */ + private static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, $includeCollections = false, $itemShareWithBySource = false) { + $backend = self::getBackend($itemType); + // Get filesystem root to add it to the file target and remove from the file source, match file_source with the file cache + if ($itemType == 'file' || $itemType == 'folder') { + $root = \OC_Filesystem::getRoot(); + $where = 'INNER JOIN *PREFIX*fscache ON file_source = *PREFIX*fscache.id '; + if (!isset($item)) { + $where .= 'WHERE file_target IS NOT NULL'; + } + $fileDependent = true; + $queryArgs = array(); + } else { + $fileDependent = false; + $root = ''; + if ($includeCollections && !isset($item) && $collectionTypes = self::getCollectionItemTypes($itemType)) { + // If includeCollections is true, find collections of this item type, e.g. a music album contains songs + $itemTypes = array_merge(array($itemType), $collectionTypes); + $placeholders = join(',', array_fill(0, count($itemTypes), '?')); + $where = "WHERE item_type IN ('".$placeholders."')"; + $queryArgs = $itemTypes; + } else { + $where = 'WHERE item_type = ?'; + $queryArgs = array($itemType); + } + } + if (isset($shareType) && isset($shareWith)) { + // Include all user and group items + if ($shareType == self::$shareTypeUserAndGroups) { + $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; + } else { + $where .= ' AND share_type = ? AND share_with = ?'; + $queryArgs[] = $shareType; + $queryArgs[] = $shareWith; + } + } + if (isset($uidOwner)) { + $where .= " AND uid_owner = ?"; + $queryArgs[] = $uidOwner; + if (!isset($shareType)) { + // Prevent unique user targets for group shares from being selected + $where .= " AND share_type != ?"; + $queryArgs[] = self::$shareTypeGroupUserUnique; + } + if ($itemType == 'file' || $itemType == 'folder') { + $column = 'file_source'; + } else { + $column = 'item_source'; + } + } else { + if ($itemType == 'file' || $itemType == 'folder') { + $column = 'file_target'; + } else { + $column = 'item_target'; + } + } + if (isset($item)) { + // If looking for own shared items, check item_source else check item_target + if (isset($uidOwner) || $itemShareWithBySource) { + // If item type is a file, file source needs to be checked in case the item was converted + if ($itemType == 'file' || $itemType == 'folder') { + $where .= " AND path = ?"; + $queryArgs[] = $root.$item; + } else { + $where .= " AND item_source = ?"; + $column = 'item_source'; + $queryArgs[] = $item; + } + } else { + if ($itemType == 'file' || $itemType == 'folder') { + $where .= " AND file_target = ?"; + } else { + $where .= " AND item_target = ?"; + } + $queryArgs[] = $item; + } + if ($includeCollections && $collectionTypes = self::getCollectionItemTypes($itemType)) { + // TODO Bart - this doesn't work with only one argument +// $placeholders = join(',', array_fill(0, count($collectionTypes), '?')); +// $where .= " OR item_type IN ('".$placeholders."')"; +// $queryArgs = array_merge($queryArgs, $collectionTypes); + } + } + if ($limit != -1 && !$includeCollections) { + if ($shareType == self::$shareTypeUserAndGroups) { + // Make sure the unique user target is returned if it exists, unique targets should follow the group share in the database + // If the limit is not 1, the filtering can be done later + $where .= ' ORDER BY *PREFIX*share.id DESC'; + } + // The limit must be at least 3, because filtering needs to be done + if ($limit < 3) { + $where .= ' LIMIT 3'; + } else { + $where .= ' LIMIT '.$limit; + } + } + // TODO Optimize selects + if ($format == self::FORMAT_STATUSES) { + if ($itemType == 'file' || $itemType == 'folder') { + $select = '*PREFIX*share.id, item_type, *PREFIX*share.parent, share_type, *PREFIX*fscache.path as file_source'; + } else { + $select = 'id, item_type, item_source, parent, share_type'; + } + } else { + if (isset($uidOwner)) { + if ($itemType == 'file' || $itemType == 'folder') { + $select = '*PREFIX*share.id, item_type, *PREFIX*fscache.path as file_source, *PREFIX*share.parent, share_type, share_with, permissions, stime'; + } else { + $select = 'id, item_type, item_source, parent, share_type, share_with, permissions, stime, file_source'; + } + } else { + if ($fileDependent) { + if (($itemType == 'file' || $itemType == 'folder') && $format == \OC_Share_Backend_File::FORMAT_FILE_APP || $format == \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT) { + $select = '*PREFIX*share.id, item_type, *PREFIX*share.parent, share_type, share_with, permissions, file_target, *PREFIX*fscache.id, path as file_source, name, ctime, mtime, mimetype, size, encrypted, versioned, writable'; + } else { + $select = '*PREFIX*share.id, item_type, item_source, item_target, *PREFIX*share.parent, share_type, share_with, permissions, stime, path as file_source, file_target'; + } + } else { + $select = '*'; + } + } + } + $root = strlen($root); + $query = \OC_DB::prepare('SELECT '.$select.' FROM *PREFIX*share '.$where); + $result = $query->execute($queryArgs); + $items = array(); + $targets = array(); + while ($row = $result->fetchRow()) { + // Filter out duplicate group shares for users with unique targets + if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { + $row['share_type'] = self::SHARE_TYPE_GROUP; + $row['share_with'] = $items[$row['parent']]['share_with']; + // Remove the parent group share + unset($items[$row['parent']]); + } else if (!isset($uidOwner)) { + // Check if the same target already exists + if (isset($targets[$row[$column]])) { + // Check if the same owner shared with the user twice through a group and user share - this is allowed + $id = $targets[$row[$column]]; + if ($items[$id]['uid_owner'] == $row['uid_owner']) { + // Switch to group share type to ensure resharing conditions aren't bypassed + if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { + $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; + $items[$id]['share_with'] = $row['share_with']; + } + // Switch ids if sharing permission is granted on only one share to ensure correct parent is used if resharing + if (~(int)$items[$id]['permissions'] & self::PERMISSION_SHARE && (int)$row['permissions'] & self::PERMISSION_SHARE) { + $items[$row['id']] = $items[$id]; + unset($items[$id]); + $id = $row['id']; + } + // Combine the permissions for the item + $items[$id]['permissions'] |= (int)$row['permissions']; + continue; + } + } else { + $targets[$row[$column]] = $row['id']; + } + } + // Remove root from file source paths if retrieving own shared items + if (isset($uidOwner) && isset($row['file_source'])) { + $row['file_source'] = substr($row['file_source'], $root); + } + $items[$row['id']] = $row; + } + if (!empty($items)) { + $collectionItems = array(); + foreach ($items as &$row) { + // Return only the item instead of a 2-dimensional array + if ($limit == 1 && $row['item_type'] == $itemType && $row[$column] == $item) { + if ($format == self::FORMAT_NONE) { + return $row; + } else { + break; + } + } + // Check if this is a collection of the requested item type + if ($includeCollections && $row['item_type'] != $itemType && $collectionBackend = self::getBackend($row['item_type']) && $collectionBackend instanceof Share_Backend_Collection) { + $row['collection'] = array('item_type' => $itemType, $column => $row[$column]); + // Fetch all of the children sources + $children = $collectionBackend->getChildren($row[$column]); + foreach ($children as $child) { + $childItem = $row; + $childItem['item_source'] = $child; +// $childItem['item_target'] = $child['target']; TODO + if (isset($item)) { + if ($childItem[$column] == $item) { + // Return only the item instead of a 2-dimensional array + if ($limit == 1 && $format == self::FORMAT_NONE) { + return $childItem; + } else { + // Unset the items array and break out of both loops + $items = array(); + $items[] = $childItem; + break 2; + } + } + } else { + $collectionItems[] = $childItem; + } + } + // Remove collection item + unset($items[$row['id']]); + } + } + if (!empty($collectionItems)) { + $items = array_merge($items, $collectionItems); + } + if ($format == self::FORMAT_NONE) { + return $items; + } else if ($format == self::FORMAT_STATUSES) { + $statuses = array(); + foreach ($items as $item) { + if ($item['share_type'] == self::SHARE_TYPE_PRIVATE_LINK) { + $statuses[$item[$column]] = true; + } else if (!isset($statuses[$item[$column]])) { + $statuses[$item[$column]] = false; + } + } + return $statuses; + } else { + return $backend->formatItems($items, $format, $parameters); + } + } else if ($limit == 1 || (isset($uidOwner) && isset($item))) { + return false; + } + return array(); + } + + /** + * @brief Put shared item into the database + * @param string Item type + * @param string Item source + * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_PRIVATE_LINK + * @param string User or group the item is being shared with + * @param int CRUDS permissions + * @param bool|array Parent folder target (optional) + * @return bool Returns true on success or false on failure + */ + private static function put($itemType, $itemName, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder = null) { + // Check file extension for an equivalent item type to convert to + if ($itemType == 'file') { + $extension = strtolower(substr($itemSource, strrpos($itemSource, '.') + 1)); + foreach (self::$backends as $type => $backend) { + if (isset($backend->dependsOn) && $backend->dependsOn == 'file' && isset($backend->supportedFileExtensions) && in_array($extension, $backend->supportedFileExtensions)) { + $itemType = $type; + break; + } + } + // Exit if this is being called for a file inside a folder, and no equivalent item type is found + if (isset($parentFolder) && $itemType == 'file') { + return false; + } + } + $backend = self::getBackend($itemType); + // Check if this is a reshare + if ($checkReshare = self::getItemSharedWith($itemType, $itemName, self::FORMAT_NONE, null, true)) { + // Check if attempting to share back to owner + if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) { + $message = 'Sharing '.$itemSource.' failed, because the user '.$shareWith.' is the original sharer'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + // Check if attempting to share back to group TODO Check unique user target + } else if ($shareType == self::SHARE_TYPE_GROUP && $checkReshare['share_with'] == $shareWith['group']) { + $message = 'Sharing '.$itemSource.' failed, because the item was orignally shared with the group '.$shareWith['group']; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + // Check if attempting to share back a group share to a member of the same group + } else if (($checkReshare['share_type'] == self::SHARE_TYPE_GROUP || $checkReshare['share_type'] == self::$shareTypeGroupUserUnique) && $shareType == self::SHARE_TYPE_USER) { + if ($checkReshare['share_type'] == self::$shareTypeGroupUserUnique) { + $query = \OC_DB::prepare('SELECT share_with FROM *PREFIX*share WHERE id = ?'); + $group = $query->execute(array($checkReshare['parent']))->fetchOne(); + } else { + $group = $checkReshare['share_with']; + } + if (\OC_Group::inGroup($shareWith, $group)) { + $message = 'Sharing '.$itemSource.' failed, because the user '.$shareWith.' is a member of the original group share'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } + // Check if share permissions is granted + if ((int)$checkReshare['permissions'] & self::PERMISSION_SHARE) { + if (~(int)$checkReshare['permissions'] & $permissions) { + $message = 'Sharing '.$itemSource.' failed, because the permissions exceed permissions granted to '.$uidOwner; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } else { + // TODO Don't check if inside folder + $parent = $checkReshare['id']; + $itemSource = $checkReshare['item_source']; + // TODO Suggest item/file target + $suggestedItemTarget = $checkReshare['item_target']; + $fileSource = $checkReshare['file_source']; + $filePath = $checkReshare['file_target']; + } + } else { + $message = 'Sharing '.$itemSource.' failed, because resharing is not allowed'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } else { + $parent = null; + if (!$backend->isValidSource($itemSource, $uidOwner)) { + $message = 'Sharing '.$itemSource.' failed, because the sharing backend for '.$itemType.' could not find its source'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + $parent = null; + if ($backend instanceof Share_Backend_File_Dependent) { + // NOTE Apps should start using the file cache ids in their tables + $filePath = $backend->getFilePath($itemSource, $uidOwner); + $fileSource = \OC_FileCache::getId($filePath); + if ($fileSource == -1) { + $message = 'Sharing '.$itemSource.' failed, because the file could not be found in the file cache'; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + } else { + $filePath = null; + $fileSource = null; + } + } + $query = \OC_DB::prepare('INSERT INTO *PREFIX*share (item_type, item_source, item_target, parent, share_type, share_with, uid_owner, permissions, stime, file_source, file_target) VALUES (?,?,?,?,?,?,?,?,?,?,?)'); + // Share with a group + if ($shareType == self::SHARE_TYPE_GROUP) { + if (isset($fileSource)) { + if ($parentFolder) { + if ($parentFolder === true) { + $groupFileTarget = self::generateTarget('file', $filePath, $shareType, $shareWith['group'], $uidOwner); + // Set group default file target for future use + $parentFolders[0]['folder'] = $groupFileTarget; + } else { + // Get group default file target + $groupFileTarget = $parentFolder[0]['folder'].$itemSource; + $parent = $parentFolder[0]['id']; + unset($parentFolder[0]); + // Only loop through users we know have different file target paths + $uidSharedWith = array_keys($parentFolder); + } + } else { + $groupFileTarget = self::generateTarget('file', $filePath, $shareType, $shareWith['group'], $uidOwner); + } + } else { + $groupFileTarget = null; + } + $groupItemTarget = self::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'], $uidOwner); + $uniqueTargets = array(); + // Loop through all users of this group in case we need to add an extra row + foreach ($shareWith['users'] as $uid) { + $itemTarget = self::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $uid, $uidOwner); + if (isset($fileSource)) { + if ($parentFolder) { + if ($parentFolder === true) { + $fileTarget = self::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $uid, $uidOwner); + if ($fileTarget != $groupFileTarget) { + $parentFolders[$uid]['folder'] = $fileTarget; + } + } else if (isset($parentFolder[$uid])) { + $fileTarget = $parentFolder[$uid]['folder'].$itemSource; + $parent = $parentFolder[$uid]['id']; + } + } else { + $fileTarget = self::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $uid, $uidOwner); + } + } else { + $fileTarget = null; + } + // Insert an extra row for the group share if the item or file target is unique for this user + if ($itemTarget != $groupItemTarget || (isset($fileSource) && $fileTarget != $groupFileTarget)) { + $uniqueTargets[] = array('uid' => $uid, 'item_target' => $itemTarget, 'file_target' => $fileTarget); + } + } + $query->execute(array($itemType, $itemSource, $groupItemTarget, $parent, $shareType, $shareWith['group'], $uidOwner, $permissions, time(), $fileSource, $groupFileTarget)); + // Save this id, any extra rows for this group share will need to reference it + $parent = \OC_DB::insertid('*PREFIX*share'); + foreach ($uniqueTargets as $unique) { + $query->execute(array($itemType, $itemSource, $unique['item_target'], $parent, self::$shareTypeGroupUserUnique, $unique['uid'], $uidOwner, $permissions, time(), $fileSource, $unique['file_target'])); + $id = \OC_DB::insertid('*PREFIX*share'); + if ($parentFolder === true) { + $parentFolders['id'] = $id; + } + } + if ($parentFolder === true) { + // Return parent folders to preserve file target paths for potential children + return $parentFolders; + } + } else { + $itemTarget = self::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner); + if (isset($fileSource)) { + if ($parentFolder) { + if ($parentFolder === true) { + $fileTarget = self::generateTarget('file', $filePath, $shareType, $shareWith, $uidOwner); + $parentFolders['folder'] = $fileTarget; + } else { + $fileTarget = $parentFolder['folder'].$itemSource; + $parent = $parentFolder['id']; + } + } else { + $fileTarget = self::generateTarget('file', $filePath, $shareType, $shareWith, $uidOwner); + } + } else { + $fileTarget = null; + } + $query->execute(array($itemType, $itemSource, $itemTarget, $parent, $shareType, $shareWith, $uidOwner, $permissions, time(), $fileSource, $fileTarget)); + $id = \OC_DB::insertid('*PREFIX*share'); + if ($parentFolder === true) { + $parentFolders['id'] = $id; + // Return parent folder to preserve file target paths for potential children + return $parentFolders; + } + } + return true; + } + + /** + * @brief Generate a unique target for the item + * @param string Item type + * @param string Item source + * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_PRIVATE_LINK + * @param string User or group the item is being shared with + * @return string Item target + * + * TODO Use a suggested item target by default + * + */ + private static function generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner) { + $backend = self::getBackend($itemType); + if ($shareType == self::SHARE_TYPE_PRIVATE_LINK) { + return $backend->generateTarget($itemSource, false); + } else { + if ($itemType == 'file' || $itemType == 'folder') { + $column = 'file_target'; + } else { + $column = 'item_target'; + } + if ($shareType == self::SHARE_TYPE_USER) { + // Share with is a user, so set share type to user and groups + $shareType = self::$shareTypeUserAndGroups; + $userAndGroups = array_merge(array($shareWith), \OC_Group::getUserGroups($shareWith)); + } else { + $userAndGroups = false; + } + $exclude = null; + // Backend has 3 opportunities to generate a unique target + for ($i = 0; $i < 2; $i++) { + if ($shareType == self::SHARE_TYPE_GROUP) { + $target = $backend->generateTarget($itemSource, false, $exclude); + } else { + $target = $backend->generateTarget($itemSource, $shareWith, $exclude); + } + if (is_array($exclude) && in_array($target, $exclude)) { + break; + } + // Check if target already exists + if ($checkTarget = self::getItems($itemType, $target, $shareType, $shareWith, null, self::FORMAT_NONE, null, 1)) { + // If matching target is from the same owner, use the same target. The share type will be different so this isn't the same share. + if ($checkTarget['uid_owner'] == $uidOwner) { + return $target; + } + if (!isset($exclude)) { + $exclude = array(); + } + // Find similar targets to improve backend's chances to generate a unqiue target + if ($userAndGroups) { + $checkTargets = \OC_DB::prepare("SELECT ".$column." FROM *PREFIX*share WHERE item_type = ? AND share_type IN (?,?,?) AND share_with IN ('".implode("','", $userAndGroups)."') AND ".$column." LIKE ?"); + $result = $checkTargets->execute(array($itemType, self::SHARE_TYPE_USER, self::SHARE_TYPE_GROUP, self::$shareTypeGroupUserUnique, '%'.$target.'%')); + } else { + $checkTargets = \OC_DB::prepare("SELECT ".$column." FROM *PREFIX*share WHERE item_type = ? AND share_type = ? AND share_with = ? AND ".$column." LIKE ?"); + $result = $checkTargets->execute(array($itemType, self::SHARE_TYPE_GROUP, $shareWith, '%'.$target.'%')); + } + while ($row = $result->fetchRow()) { + $exclude[] = $row[$column]; + } + } else { + return $target; + } + } + } + $message = 'Sharing backend registered for '.$itemType.' did not generate a unique target for '.$itemSource; + \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); + throw new \Exception($message); + } + + /** + * @brief Delete all reshares of an item + * @param int Id of item to delete + * @param bool If true, exclude the parent from the delete (optional) + * @param string The user that the parent was shared with (optinal) + */ + private static function delete($parent, $excludeParent = false, $uidOwner = null) { + $ids = array($parent); + $parents = array($parent); + while (!empty($parents)) { + $parents = "'".implode("','", $parents)."'"; + // Check the owner on the first search of reshares, useful for finding and deleting the reshares by a single user of a group share + if (count($ids) == 1 && isset($uidOwner)) { + $query = \OC_DB::prepare('SELECT id FROM *PREFIX*share WHERE parent IN ('.$parents.') AND uid_owner = ?'); + $result = $query->execute(array($uidOwner)); + } else { + $query = \OC_DB::prepare('SELECT id, item_type, item_target, parent, uid_owner FROM *PREFIX*share WHERE parent IN ('.$parents.')'); + $result = $query->execute(); + } + // Reset parents array, only go through loop again if items are found + $parents = array(); + while ($item = $result->fetchRow()) { + // Search for a duplicate parent share, this occurs when an item is shared to the same user through a group and user or the same item is shared by different users + $userAndGroups = array_merge(array($item['uid_owner']), \OC_Group::getUserGroups($item['uid_owner'])); + $query = \OC_DB::prepare("SELECT id, permissions FROM *PREFIX*share WHERE item_type = ? AND item_target = ? AND share_type IN (?,?,?) AND share_with IN ('".implode("','", $userAndGroups)."') AND uid_owner != ? AND id != ?"); + $duplicateParent = $query->execute(array($item['item_type'], $item['item_target'], self::SHARE_TYPE_USER, self::SHARE_TYPE_GROUP, self::$shareTypeGroupUserUnique, $item['uid_owner'], $item['parent']))->fetchRow(); + if ($duplicateParent) { + // Change the parent to the other item id if share permission is granted + if ($duplicateParent['permissions'] & self::PERMISSION_SHARE) { + $query = \OC_DB::prepare('UPDATE *PREFIX*share SET parent = ? WHERE id = ?'); + $query->execute(array($duplicateParent['id'], $item['id'])); + continue; + } + } + $ids[] = $item['id']; + $parents[] = $item['id']; + } + } + if ($excludeParent) { + unset($ids[0]); + } + if (!empty($ids)) { + $ids = "'".implode("','", $ids)."'"; + $query = \OC_DB::prepare('DELETE FROM *PREFIX*share WHERE id IN ('.$ids.')'); + $query->execute(); + } + } + + /** + * Hook Listeners + */ + + public static function post_deleteUser($arguments) { + // Delete any items shared with the deleted user + $query = \OC_DB::prepare('DELETE FROM *PREFIX*share WHERE share_with = ? AND share_type = ? OR share_type = ?'); + $result = $query->execute(array($arguments['uid'], self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique)); + // Delete any items the deleted user shared + $query = \OC_DB::prepare('SELECT id FROM *PREFIX*share WHERE uid_owner = ?'); + $result = $query->execute(array($arguments['uid'])); + while ($item = $result->fetchRow()) { + self::delete($item['id']); + } + } + + public static function post_addToGroup($arguments) { + // TODO + } + + public static function post_removeFromGroup($arguments) { + // TODO Don't call if user deleted? + $query = \OC_DB::prepare('SELECT id, share_type FROM *PREFIX*share WHERE (share_type = ? AND share_with = ?) OR (share_type = ? AND share_with = ?)'); + $result = $query->execute(array(self::SHARE_TYPE_GROUP, $arguments['gid'], self::$shareTypeGroupUserUnique, $arguments['uid'])); + while ($item = $result->fetchRow()) { + if ($item['share_type'] == self::SHARE_TYPE_GROUP) { + // Delete all reshares by this user of the group share + self::delete($item['id'], true, $arguments['uid']); + } else { + self::delete($item['id']); + } + } + } + +} + +/** +* Interface that apps must implement to share content. +*/ +interface Share_Backend { + + /** + * @brief Get the source of the item to be stored in the database + * @param string Item source + * @param string Owner of the item + * @return mixed|array|false Source + * + * Return an array if the item is file dependent, the array needs two keys: 'item' and 'file' + * Return false if the item does not exist for the user + * + * The formatItems() function will translate the source returned back into the item + */ + public function isValidSource($itemSource, $uidOwner); + + /** + * @brief Get a unique name of the item for the specified user + * @param string Item source + * @param string|false User the item is being shared with + * @param array|null List of similar item names already existing as shared items + * @return string Target name + * + * This function needs to verify that the user does not already have an item with this name. + * If it does generate a new name e.g. name_# + */ + public function generateTarget($itemSource, $shareWith, $exclude = null); + + /** + * @brief Converts the shared item sources back into the item in the specified format + * @param array Shared items + * @param int Format + * @return ? + * + * The items array is a 3-dimensional array with the item_source as the first key and the share id as the second key to an array with the share info. + * The key/value pairs included in the share info depend on the function originally called: + * If called by getItem(s)Shared: id, item_type, item, item_source, share_type, share_with, permissions, stime, file_source + * If called by getItem(s)SharedWith: id, item_type, item, item_source, item_target, share_type, share_with, permissions, stime, file_source, file_target + * This function allows the backend to control the output of shared items with custom formats. + * It is only called through calls to the public getItem(s)Shared(With) functions. + */ + public function formatItems($items, $format, $parameters = null); + +} + +/** +* Interface for share backends that share content that is dependent on files. +* Extends the Share_Backend interface. +*/ +interface Share_Backend_File_Dependent extends Share_Backend { + + /** + * @brief Get the file path of the item + * @param + * @param + * @return + */ + public function getFilePath($itemSource, $uidOwner); + +} + +/** +* Interface for collections of of items implemented by another share backend. +* Extends the Share_Backend interface. +*/ +interface Share_Backend_Collection extends Share_Backend { + + /** + * @brief Get the sources of the children of the item + * @param string Item source + * @return array Returns an array of sources + */ + public function getChildren($itemSource); + +} + +?> diff --git a/tests/lib/filestorage.php b/tests/lib/filestorage.php index e554a75e44..0a8715600d 100644 --- a/tests/lib/filestorage.php +++ b/tests/lib/filestorage.php @@ -31,13 +31,13 @@ abstract class Test_FileStorage extends UnitTestCase { */ public function testRoot(){ $this->assertTrue($this->instance->file_exists('/'),'Root folder does not exist'); - $this->assertTrue($this->instance->is_readable('/'),'Root folder is not readable'); + $this->assertTrue($this->instance->isReadable('/'),'Root folder is not readable'); $this->assertTrue($this->instance->is_dir('/'),'Root folder is not a directory'); $this->assertFalse($this->instance->is_file('/'),'Root folder is a file'); $this->assertEqual('dir',$this->instance->filetype('/')); //without this, any further testing would be useless, not an acutal requirement for filestorage though - $this->assertTrue($this->instance->is_writable('/'),'Root folder is not writable'); + $this->assertTrue($this->instance->isUpdatable('/'),'Root folder is not writable'); } public function testDirectories(){ @@ -50,8 +50,8 @@ abstract class Test_FileStorage extends UnitTestCase { $this->assertFalse($this->instance->is_file('/folder')); $this->assertEqual('dir',$this->instance->filetype('/folder')); $this->assertEqual(0,$this->instance->filesize('/folder')); - $this->assertTrue($this->instance->is_readable('/folder')); - $this->assertTrue($this->instance->is_writable('/folder')); + $this->assertTrue($this->instance->isReadable('/folder')); + $this->assertTrue($this->instance->isUpdatable('/folder')); $dh=$this->instance->opendir('/'); $content=array(); @@ -154,7 +154,7 @@ abstract class Test_FileStorage extends UnitTestCase { $textFile=OC::$SERVERROOT.'/tests/data/lorem.txt'; $ctimeStart=time(); $this->instance->file_put_contents('/lorem.txt',file_get_contents($textFile)); - $this->assertTrue($this->instance->is_readable('/lorem.txt')); + $this->assertTrue($this->instance->isReadable('/lorem.txt')); $ctimeEnd=time(); $cTime=$this->instance->filectime('/lorem.txt'); $mTime=$this->instance->filemtime('/lorem.txt'); diff --git a/tests/lib/share/backend.php b/tests/lib/share/backend.php new file mode 100644 index 0000000000..9fe625a1fa --- /dev/null +++ b/tests/lib/share/backend.php @@ -0,0 +1,67 @@ +. +*/ + +class Test_Share_Backend implements OCP\Share_Backend { + + const FORMAT_SOURCE = 0; + const FORMAT_TARGET = 1; + const FORMAT_PERMISSIONS = 2; + + private $testItem = 'test.txt'; + + public function isValidSource($itemSource, $uidOwner) { + if ($itemSource == $this->testItem) { + return true; + } + } + + public function generateTarget($itemSource, $shareWith, $exclude = null) { + $target = $itemSource; + if (isset($exclude)) { + $pos = strrpos($target, '.'); + $name = substr($target, 0, $pos); + $ext = substr($target, $pos); + $append = ''; + $i = 1; + while (in_array($name.$append.$ext, $exclude)) { + $append = $i; + $i++; + } + $target = $name.$append.$ext; + } + return $target; + } + + public function formatItems($items, $format, $parameters = null) { + $testItems = array(); + foreach ($items as $item) { + if ($format == self::FORMAT_SOURCE) { + $testItems[] = $item['item_source']; + } else if ($format == self::FORMAT_TARGET) { + $testItems[] = $item['item_target']; + } else if ($format == self::FORMAT_PERMISSIONS) { + $testItems[] = $item['permissions']; + } + } + return $testItems; + } + +} diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php new file mode 100644 index 0000000000..89f0fbc976 --- /dev/null +++ b/tests/lib/share/share.php @@ -0,0 +1,380 @@ +. +*/ + +class Test_Share extends UnitTestCase { + + protected $itemType; + protected $userBackend; + protected $user1; + protected $user2; + protected $groupBackend; + protected $group1; + protected $group2; + + + public function setUp() { + OC_User::clearBackends(); + OC_User::useBackend('dummy'); + $this->user1 = uniqid('user_'); + $this->user2 = uniqid('user_'); + $this->user3 = uniqid('user_'); + $this->user4 = uniqid('user_'); + OC_User::createUser($this->user1, 'pass'); + OC_User::createUser($this->user2, 'pass'); + OC_User::createUser($this->user3, 'pass'); + OC_User::createUser($this->user4, 'pass'); + OC_User::setUserId($this->user1); + OC_Group::clearBackends(); + OC_Group::useBackend(new OC_Group_Dummy); + $this->group1 = uniqid('group_'); + $this->group2 = uniqid('group_'); + OC_Group::createGroup($this->group1); + OC_Group::createGroup($this->group2); + OC_Group::addToGroup($this->user1, $this->group1); + OC_Group::addToGroup($this->user2, $this->group1); + OC_Group::addToGroup($this->user3, $this->group1); + OC_Group::addToGroup($this->user2, $this->group2); + OC_Group::addToGroup($this->user4, $this->group2); + OCP\Share::registerBackend('test', 'Test_Share_Backend'); + } + + public function tearDown() { + $query = OC_DB::prepare('DELETE FROM *PREFIX*share WHERE item_type = ?'); + $query->execute(array('test')); + } + + public function testShareInvalidShareType() { + $this->expectException(new Exception('Share type foobar is not valid for test.txt')); + OCP\Share::shareItem('test', 'test.txt', 'test.txt', 'foobar', $this->user2, OCP\Share::PERMISSION_READ); + } + + public function testInvalidItemType() { + $message = 'Sharing backend for foobar not found'; + try { + OCP\Share::shareItem('foobar', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + try { + OCP\Share::getItemsSharedWith('foobar'); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + try { + OCP\Share::getItemSharedWith('foobar', 'test.txt'); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + try { + OCP\Share::getItemSharedWithBySource('foobar', 'test.txt'); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + try { + OCP\Share::getItemShared('foobar', 'test.txt'); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + try { + OCP\Share::unshare('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + try { + OCP\Share::setPermissions('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_UPDATE); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + } + + public function testShareWithUser() { + // Invalid shares + $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + $message = 'Sharing test.txt failed, because the user foobar does not exist'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + $message = 'Sharing foobar failed, because the sharing backend for test could not find its source'; + try { + OCP\Share::shareItem('test', 'foobar', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Valid share + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ)); + $this->assertEqual(OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + + // Attempt to share again + OC_User::setUserId($this->user1); + $message = 'Sharing test.txt failed, because this item is already shared with '.$this->user2; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Attempt to share back + OC_User::setUserId($this->user2); + $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Unshare + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2)); + + // Attempt reshare without share permission + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user2); + $message = 'Sharing test.txt failed, because resharing is not allowed'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Owner grants share and update permission + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_SHARE)); + + // Attempt reshare with escalated permissions + OC_User::setUserId($this->user2); + $message = 'Sharing test.txt failed, because the permissions exceed permissions granted to '.$this->user2; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Valid reshare + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE)); + $this->assertEqual(OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + OC_User::setUserId($this->user3); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE)); + + // Attempt to escalate permissions + OC_User::setUserId($this->user2); + $message = 'Setting permissions for test.txt failed, because the permissions exceed permissions granted to '.$this->user2; + try { + OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Remove update permission + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE)); + OC_User::setUserId($this->user3); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ)); + + // Remove share permission + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user3); + $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt')); + + // Reshare again, and then have owner unshare + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE)); + OC_User::setUserId($this->user2); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2)); + OC_User::setUserId($this->user2); + $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt')); + OC_User::setUserId($this->user3); + $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt')); + + // Attempt target conflict + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user3); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt', 'test1.txt')); + + // Remove user + OC_User::deleteUser($this->user1); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test1.txt')); + } + + public function testShareWithGroup() { + // Invalid shares + $message = 'Sharing test.txt failed, because the group foobar does not exist'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + $message = 'Sharing test.txt failed, because '.$this->user1.' is not a member of the group '.$this->group2; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Valid share + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ)); + $this->assertEqual(OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + OC_User::setUserId($this->user3); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt')); + + // Attempt to share again + OC_User::setUserId($this->user1); + $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Attempt to share back to owner of group share + OC_User::setUserId($this->user2); + $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Attempt to share back to group + $message = 'Sharing test.txt failed, because the item was orignally shared with the group '.$this->group1; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Attempt to share back to member of group + $message = 'Sharing test.txt failed, because the user '.$this->user3.' is a member of the original group share'; + try { + OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ); + $this->fail('Exception was expected: '.$message); + } catch (Exception $exception) { + $this->assertEqual($exception->getMessage(), $message); + } + + // Unshare + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1)); + + // Valid share with same person - user then group + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE | OCP\Share::PERMISSION_SHARE)); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt')); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE | OCP\Share::PERMISSION_SHARE)); + OC_User::setUserId($this->user3); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt')); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE)); + + // Valid reshare + OC_User::setUserId($this->user2); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\Share::PERMISSION_READ)); + OC_User::setUserId($this->user4); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt')); + + // Unshare from user only + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE)); + OC_User::setUserId($this->user4); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array()); + + // Valid share with same person - group then user + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt')); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE)); + + // Unshare from group only + OC_User::setUserId($this->user1); + $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE)); + + // Attempt user specific target conflict + OC_User::setUserId($this->user3); + $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE)); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt', 'test1.txt')); + + // Valid reshare TODO Broken + $this->assertTrue(OCP\Share::shareItem('test', 'test1.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE)); + OC_User::setUserId($this->user4); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test1.txt')); + + // Remove user from group + OC_Group::removeFromGroup($this->user2, $this->group1); + OC_User::setUserId($this->user2); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt')); + OC_User::setUserId($this->user4); + $this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array()); + + // Add user to group + + // Remove group + } + +}