share.php 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Bernhard Reiter <ockham@raz.or.at>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Christopher Schäpers <kondou@ts.unde.re>
  10. * @author Daniel Hansson <enoch85@gmail.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Michael Kuhn <suraia@ikkoku.de>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Robin McCorkell <robin@mccorkell.me.uk>
  18. * @author Roeland Jago Douma <roeland@famdouma.nl>
  19. * @author Sebastian Döll <sebastian.doell@libasys.de>
  20. * @author Thomas Müller <thomas.mueller@tmit.eu>
  21. * @author Vincent Petry <pvince81@owncloud.com>
  22. * @author Volkan Gezer <volkangezer@gmail.com>
  23. *
  24. * @license AGPL-3.0
  25. *
  26. * This code is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Affero General Public License, version 3,
  28. * as published by the Free Software Foundation.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU Affero General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Affero General Public License, version 3,
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>
  37. *
  38. */
  39. namespace OC\Share;
  40. use OC\Files\Filesystem;
  41. use OCA\FederatedFileSharing\DiscoveryManager;
  42. use OCP\DB\QueryBuilder\IQueryBuilder;
  43. use OCP\IUserSession;
  44. use OCP\IDBConnection;
  45. use OCP\IConfig;
  46. /**
  47. * This class provides the ability for apps to share their content between users.
  48. * Apps must create a backend class that implements OCP\Share_Backend and register it with this class.
  49. *
  50. * It provides the following hooks:
  51. * - post_shared
  52. */
  53. class Share extends Constants {
  54. /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask
  55. * Construct permissions for share() and setPermissions with Or (|) e.g.
  56. * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE
  57. *
  58. * Check if permission is granted with And (&) e.g. Check if delete is
  59. * granted: if ($permissions & PERMISSION_DELETE)
  60. *
  61. * Remove permissions with And (&) and Not (~) e.g. Remove the update
  62. * permission: $permissions &= ~PERMISSION_UPDATE
  63. *
  64. * Apps are required to handle permissions on their own, this class only
  65. * stores and manages the permissions of shares
  66. * @see lib/public/constants.php
  67. */
  68. /**
  69. * Register a sharing backend class that implements OCP\Share_Backend for an item type
  70. * @param string $itemType Item type
  71. * @param string $class Backend class
  72. * @param string $collectionOf (optional) Depends on item type
  73. * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files
  74. * @return boolean true if backend is registered or false if error
  75. */
  76. public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
  77. if (self::isEnabled()) {
  78. if (!isset(self::$backendTypes[$itemType])) {
  79. self::$backendTypes[$itemType] = array(
  80. 'class' => $class,
  81. 'collectionOf' => $collectionOf,
  82. 'supportedFileExtensions' => $supportedFileExtensions
  83. );
  84. if(count(self::$backendTypes) === 1) {
  85. \OC_Util::addScript('core', 'shareconfigmodel');
  86. \OC_Util::addScript('core', 'shareitemmodel');
  87. \OC_Util::addScript('core', 'sharedialogresharerinfoview');
  88. \OC_Util::addScript('core', 'sharedialoglinkshareview');
  89. \OC_Util::addScript('core', 'sharedialogexpirationview');
  90. \OC_Util::addScript('core', 'sharedialogshareelistview');
  91. \OC_Util::addScript('core', 'sharedialogview');
  92. \OC_Util::addScript('core', 'share');
  93. \OC_Util::addStyle('core', 'share');
  94. }
  95. return true;
  96. }
  97. \OCP\Util::writeLog('OCP\Share',
  98. 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class']
  99. .' is already registered for '.$itemType,
  100. \OCP\Util::WARN);
  101. }
  102. return false;
  103. }
  104. /**
  105. * Check if the Share API is enabled
  106. * @return boolean true if enabled or false
  107. *
  108. * The Share API is enabled by default if not configured
  109. */
  110. public static function isEnabled() {
  111. if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_enabled', 'yes') == 'yes') {
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * Find which users can access a shared item
  118. * @param string $path to the file
  119. * @param string $ownerUser owner of the file
  120. * @param boolean $includeOwner include owner to the list of users with access to the file
  121. * @param boolean $returnUserPaths Return an array with the user => path map
  122. * @param boolean $recursive take all parent folders into account (default true)
  123. * @return array
  124. * @note $path needs to be relative to user data dir, e.g. 'file.txt'
  125. * not '/admin/data/file.txt'
  126. */
  127. public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) {
  128. Filesystem::initMountPoints($ownerUser);
  129. $shares = $sharePaths = $fileTargets = array();
  130. $publicShare = false;
  131. $remoteShare = false;
  132. $source = -1;
  133. $cache = false;
  134. $view = new \OC\Files\View('/' . $ownerUser . '/files');
  135. $meta = $view->getFileInfo($path);
  136. if ($meta) {
  137. $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files'));
  138. } else {
  139. // if the file doesn't exists yet we start with the parent folder
  140. $meta = $view->getFileInfo(dirname($path));
  141. }
  142. if($meta !== false) {
  143. $source = $meta['fileid'];
  144. $cache = new \OC\Files\Cache\Cache($meta['storage']);
  145. }
  146. while ($source !== -1) {
  147. // Fetch all shares with another user
  148. if (!$returnUserPaths) {
  149. $query = \OC_DB::prepare(
  150. 'SELECT `share_with`, `file_source`, `file_target`
  151. FROM
  152. `*PREFIX*share`
  153. WHERE
  154. `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'
  155. );
  156. $result = $query->execute(array($source, self::SHARE_TYPE_USER));
  157. } else {
  158. $query = \OC_DB::prepare(
  159. 'SELECT `share_with`, `file_source`, `file_target`
  160. FROM
  161. `*PREFIX*share`
  162. WHERE
  163. `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')'
  164. );
  165. $result = $query->execute(array($source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique));
  166. }
  167. if (\OCP\DB::isError($result)) {
  168. \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  169. } else {
  170. while ($row = $result->fetchRow()) {
  171. $shares[] = $row['share_with'];
  172. if ($returnUserPaths) {
  173. $fileTargets[(int) $row['file_source']][$row['share_with']] = $row;
  174. }
  175. }
  176. }
  177. // We also need to take group shares into account
  178. $query = \OC_DB::prepare(
  179. 'SELECT `share_with`, `file_source`, `file_target`
  180. FROM
  181. `*PREFIX*share`
  182. WHERE
  183. `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')'
  184. );
  185. $result = $query->execute(array($source, self::SHARE_TYPE_GROUP));
  186. if (\OCP\DB::isError($result)) {
  187. \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  188. } else {
  189. while ($row = $result->fetchRow()) {
  190. $usersInGroup = \OC_Group::usersInGroup($row['share_with']);
  191. $shares = array_merge($shares, $usersInGroup);
  192. if ($returnUserPaths) {
  193. foreach ($usersInGroup as $user) {
  194. if (!isset($fileTargets[(int) $row['file_source']][$user])) {
  195. // When the user already has an entry for this file source
  196. // the file is either shared directly with him as well, or
  197. // he has an exception entry (because of naming conflict).
  198. $fileTargets[(int) $row['file_source']][$user] = $row;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. //check for public link shares
  205. if (!$publicShare) {
  206. $query = \OC_DB::prepare('
  207. SELECT `share_with`
  208. FROM `*PREFIX*share`
  209. WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1
  210. );
  211. $result = $query->execute(array($source, self::SHARE_TYPE_LINK));
  212. if (\OCP\DB::isError($result)) {
  213. \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  214. } else {
  215. if ($result->fetchRow()) {
  216. $publicShare = true;
  217. }
  218. }
  219. }
  220. //check for remote share
  221. if (!$remoteShare) {
  222. $query = \OC_DB::prepare('
  223. SELECT `share_with`
  224. FROM `*PREFIX*share`
  225. WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1
  226. );
  227. $result = $query->execute(array($source, self::SHARE_TYPE_REMOTE));
  228. if (\OCP\DB::isError($result)) {
  229. \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  230. } else {
  231. if ($result->fetchRow()) {
  232. $remoteShare = true;
  233. }
  234. }
  235. }
  236. // let's get the parent for the next round
  237. $meta = $cache->get((int)$source);
  238. if ($recursive === true && $meta !== false) {
  239. $source = (int)$meta['parent'];
  240. } else {
  241. $source = -1;
  242. }
  243. }
  244. // Include owner in list of users, if requested
  245. if ($includeOwner) {
  246. $shares[] = $ownerUser;
  247. }
  248. if ($returnUserPaths) {
  249. $fileTargetIDs = array_keys($fileTargets);
  250. $fileTargetIDs = array_unique($fileTargetIDs);
  251. if (!empty($fileTargetIDs)) {
  252. $query = \OC_DB::prepare(
  253. 'SELECT `fileid`, `path`
  254. FROM `*PREFIX*filecache`
  255. WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')'
  256. );
  257. $result = $query->execute();
  258. if (\OCP\DB::isError($result)) {
  259. \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  260. } else {
  261. while ($row = $result->fetchRow()) {
  262. foreach ($fileTargets[$row['fileid']] as $uid => $shareData) {
  263. $sharedPath = $shareData['file_target'];
  264. $sharedPath .= substr($path, strlen($row['path']) -5);
  265. $sharePaths[$uid] = $sharedPath;
  266. }
  267. }
  268. }
  269. }
  270. if ($includeOwner) {
  271. $sharePaths[$ownerUser] = $path;
  272. } else {
  273. unset($sharePaths[$ownerUser]);
  274. }
  275. return $sharePaths;
  276. }
  277. return array('users' => array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare);
  278. }
  279. /**
  280. * Get the items of item type shared with the current user
  281. * @param string $itemType
  282. * @param int $format (optional) Format type must be defined by the backend
  283. * @param mixed $parameters (optional)
  284. * @param int $limit Number of items to return (optional) Returns all by default
  285. * @param boolean $includeCollections (optional)
  286. * @return mixed Return depends on format
  287. */
  288. public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
  289. $parameters = null, $limit = -1, $includeCollections = false) {
  290. return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format,
  291. $parameters, $limit, $includeCollections);
  292. }
  293. /**
  294. * Get the items of item type shared with a user
  295. * @param string $itemType
  296. * @param string $user id for which user we want the shares
  297. * @param int $format (optional) Format type must be defined by the backend
  298. * @param mixed $parameters (optional)
  299. * @param int $limit Number of items to return (optional) Returns all by default
  300. * @param boolean $includeCollections (optional)
  301. * @return mixed Return depends on format
  302. */
  303. public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE,
  304. $parameters = null, $limit = -1, $includeCollections = false) {
  305. return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format,
  306. $parameters, $limit, $includeCollections);
  307. }
  308. /**
  309. * Get the item of item type shared with the current user
  310. * @param string $itemType
  311. * @param string $itemTarget
  312. * @param int $format (optional) Format type must be defined by the backend
  313. * @param mixed $parameters (optional)
  314. * @param boolean $includeCollections (optional)
  315. * @return mixed Return depends on format
  316. */
  317. public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE,
  318. $parameters = null, $includeCollections = false) {
  319. return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format,
  320. $parameters, 1, $includeCollections);
  321. }
  322. /**
  323. * Get the item of item type shared with a given user by source
  324. * @param string $itemType
  325. * @param string $itemSource
  326. * @param string $user User to whom the item was shared
  327. * @param string $owner Owner of the share
  328. * @param int $shareType only look for a specific share type
  329. * @return array Return list of items with file_target, permissions and expiration
  330. */
  331. public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) {
  332. $shares = array();
  333. $fileDependent = false;
  334. $where = 'WHERE';
  335. $fileDependentWhere = '';
  336. if ($itemType === 'file' || $itemType === 'folder') {
  337. $fileDependent = true;
  338. $column = 'file_source';
  339. $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ';
  340. $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` ';
  341. } else {
  342. $column = 'item_source';
  343. }
  344. $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent);
  345. $where .= ' `' . $column . '` = ? AND `item_type` = ? ';
  346. $arguments = array($itemSource, $itemType);
  347. // for link shares $user === null
  348. if ($user !== null) {
  349. $where .= ' AND `share_with` = ? ';
  350. $arguments[] = $user;
  351. }
  352. if ($shareType !== null) {
  353. $where .= ' AND `share_type` = ? ';
  354. $arguments[] = $shareType;
  355. }
  356. if ($owner !== null) {
  357. $where .= ' AND `uid_owner` = ? ';
  358. $arguments[] = $owner;
  359. }
  360. $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where);
  361. $result = \OC_DB::executeAudited($query, $arguments);
  362. while ($row = $result->fetchRow()) {
  363. if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
  364. continue;
  365. }
  366. if ($fileDependent && (int)$row['file_parent'] === -1) {
  367. // if it is a mount point we need to get the path from the mount manager
  368. $mountManager = \OC\Files\Filesystem::getMountManager();
  369. $mountPoint = $mountManager->findByStorageId($row['storage_id']);
  370. if (!empty($mountPoint)) {
  371. $path = $mountPoint[0]->getMountPoint();
  372. $path = trim($path, '/');
  373. $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt`
  374. $row['path'] = $path;
  375. } else {
  376. \OC::$server->getLogger()->warning(
  377. 'Could not resolve mount point for ' . $row['storage_id'],
  378. ['app' => 'OCP\Share']
  379. );
  380. }
  381. }
  382. $shares[] = $row;
  383. }
  384. //if didn't found a result than let's look for a group share.
  385. if(empty($shares) && $user !== null) {
  386. $groups = \OC_Group::getUserGroups($user);
  387. if (!empty($groups)) {
  388. $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)';
  389. $arguments = array($itemSource, $itemType, $groups);
  390. $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY);
  391. if ($owner !== null) {
  392. $where .= ' AND `uid_owner` = ?';
  393. $arguments[] = $owner;
  394. $types[] = null;
  395. }
  396. // TODO: inject connection, hopefully one day in the future when this
  397. // class isn't static anymore...
  398. $conn = \OC::$server->getDatabaseConnection();
  399. $result = $conn->executeQuery(
  400. 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where,
  401. $arguments,
  402. $types
  403. );
  404. while ($row = $result->fetch()) {
  405. $shares[] = $row;
  406. }
  407. }
  408. }
  409. return $shares;
  410. }
  411. /**
  412. * Get the item of item type shared with the current user by source
  413. * @param string $itemType
  414. * @param string $itemSource
  415. * @param int $format (optional) Format type must be defined by the backend
  416. * @param mixed $parameters
  417. * @param boolean $includeCollections
  418. * @param string $shareWith (optional) define against which user should be checked, default: current user
  419. * @return array
  420. */
  421. public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
  422. $parameters = null, $includeCollections = false, $shareWith = null) {
  423. $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith;
  424. return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format,
  425. $parameters, 1, $includeCollections, true);
  426. }
  427. /**
  428. * Get the item of item type shared by a link
  429. * @param string $itemType
  430. * @param string $itemSource
  431. * @param string $uidOwner Owner of link
  432. * @return array
  433. */
  434. public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) {
  435. return self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE,
  436. null, 1);
  437. }
  438. /**
  439. * Based on the given token the share information will be returned - password protected shares will be verified
  440. * @param string $token
  441. * @param bool $checkPasswordProtection
  442. * @return array|boolean false will be returned in case the token is unknown or unauthorized
  443. */
  444. public static function getShareByToken($token, $checkPasswordProtection = true) {
  445. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1);
  446. $result = $query->execute(array($token));
  447. if ($result === false) {
  448. \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR);
  449. }
  450. $row = $result->fetchRow();
  451. if ($row === false) {
  452. return false;
  453. }
  454. if (is_array($row) and self::expireItem($row)) {
  455. return false;
  456. }
  457. // password protected shares need to be authenticated
  458. if ($checkPasswordProtection && !\OCP\Share::checkPasswordProtectedShare($row)) {
  459. return false;
  460. }
  461. return $row;
  462. }
  463. /**
  464. * resolves reshares down to the last real share
  465. * @param array $linkItem
  466. * @return array file owner
  467. */
  468. public static function resolveReShare($linkItem)
  469. {
  470. if (isset($linkItem['parent'])) {
  471. $parent = $linkItem['parent'];
  472. while (isset($parent)) {
  473. $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1);
  474. $item = $query->execute(array($parent))->fetchRow();
  475. if (isset($item['parent'])) {
  476. $parent = $item['parent'];
  477. } else {
  478. return $item;
  479. }
  480. }
  481. }
  482. return $linkItem;
  483. }
  484. /**
  485. * Get the shared items of item type owned by the current user
  486. * @param string $itemType
  487. * @param int $format (optional) Format type must be defined by the backend
  488. * @param mixed $parameters
  489. * @param int $limit Number of items to return (optional) Returns all by default
  490. * @param boolean $includeCollections
  491. * @return mixed Return depends on format
  492. */
  493. public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
  494. $limit = -1, $includeCollections = false) {
  495. return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format,
  496. $parameters, $limit, $includeCollections);
  497. }
  498. /**
  499. * Get the shared item of item type owned by the current user
  500. * @param string $itemType
  501. * @param string $itemSource
  502. * @param int $format (optional) Format type must be defined by the backend
  503. * @param mixed $parameters
  504. * @param boolean $includeCollections
  505. * @return mixed Return depends on format
  506. */
  507. public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
  508. $parameters = null, $includeCollections = false) {
  509. return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format,
  510. $parameters, -1, $includeCollections);
  511. }
  512. /**
  513. * Get all users an item is shared with
  514. * @param string $itemType
  515. * @param string $itemSource
  516. * @param string $uidOwner
  517. * @param boolean $includeCollections
  518. * @param boolean $checkExpireDate
  519. * @return array Return array of users
  520. */
  521. public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) {
  522. $users = array();
  523. $items = self::getItems($itemType, $itemSource, null, null, $uidOwner, self::FORMAT_NONE, null, -1, $includeCollections, false, $checkExpireDate);
  524. if ($items) {
  525. foreach ($items as $item) {
  526. if ((int)$item['share_type'] === self::SHARE_TYPE_USER) {
  527. $users[] = $item['share_with'];
  528. } else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) {
  529. $users = array_merge($users, \OC_Group::usersInGroup($item['share_with']));
  530. }
  531. }
  532. }
  533. return $users;
  534. }
  535. /**
  536. * Share an item with a user, group, or via private link
  537. * @param string $itemType
  538. * @param string $itemSource
  539. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  540. * @param string $shareWith User or group the item is being shared with
  541. * @param int $permissions CRUDS
  542. * @param string $itemSourceName
  543. * @param \DateTime $expirationDate
  544. * @param bool $passwordChanged
  545. * @return boolean|string Returns true on success or false on failure, Returns token on success for links
  546. * @throws \OC\HintException when the share type is remote and the shareWith is invalid
  547. * @throws \Exception
  548. */
  549. public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) {
  550. $backend = self::getBackend($itemType);
  551. $l = \OC::$server->getL10N('lib');
  552. if ($backend->isShareTypeAllowed($shareType) === false) {
  553. $message = 'Sharing %s failed, because the backend does not allow shares from type %i';
  554. $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType));
  555. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG);
  556. throw new \Exception($message_t);
  557. }
  558. $uidOwner = \OC_User::getUser();
  559. $shareWithinGroupOnly = self::shareWithGroupMembersOnly();
  560. if (is_null($itemSourceName)) {
  561. $itemSourceName = $itemSource;
  562. }
  563. $itemName = $itemSourceName;
  564. // check if file can be shared
  565. if ($itemType === 'file' or $itemType === 'folder') {
  566. $path = \OC\Files\Filesystem::getPath($itemSource);
  567. $itemName = $path;
  568. // verify that the file exists before we try to share it
  569. if (!$path) {
  570. $message = 'Sharing %s failed, because the file does not exist';
  571. $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName));
  572. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
  573. throw new \Exception($message_t);
  574. }
  575. // verify that the user has share permission
  576. if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) {
  577. $message = 'You are not allowed to share %s';
  578. $message_t = $l->t('You are not allowed to share %s', [$path]);
  579. \OCP\Util::writeLog('OCP\Share', sprintf($message, $path), \OCP\Util::DEBUG);
  580. throw new \Exception($message_t);
  581. }
  582. }
  583. //verify that we don't share a folder which already contains a share mount point
  584. if ($itemType === 'folder') {
  585. $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/';
  586. $mountManager = \OC\Files\Filesystem::getMountManager();
  587. $mounts = $mountManager->findIn($path);
  588. foreach ($mounts as $mount) {
  589. if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
  590. $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!';
  591. \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG);
  592. throw new \Exception($message);
  593. }
  594. }
  595. }
  596. // single file shares should never have delete permissions
  597. if ($itemType === 'file') {
  598. $permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE;
  599. }
  600. //Validate expirationDate
  601. if ($expirationDate !== null) {
  602. try {
  603. /*
  604. * Reuse the validateExpireDate.
  605. * We have to pass time() since the second arg is the time
  606. * the file was shared, since it is not shared yet we just use
  607. * the current time.
  608. */
  609. $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource);
  610. } catch (\Exception $e) {
  611. throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404);
  612. }
  613. }
  614. // Verify share type and sharing conditions are met
  615. if ($shareType === self::SHARE_TYPE_USER) {
  616. if ($shareWith == $uidOwner) {
  617. $message = 'Sharing %s failed, because you can not share with yourself';
  618. $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]);
  619. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
  620. throw new \Exception($message_t);
  621. }
  622. if (!\OC_User::userExists($shareWith)) {
  623. $message = 'Sharing %s failed, because the user %s does not exist';
  624. $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith));
  625. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
  626. throw new \Exception($message_t);
  627. }
  628. if ($shareWithinGroupOnly) {
  629. $inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith));
  630. if (empty($inGroup)) {
  631. $message = 'Sharing %s failed, because the user '
  632. .'%s is not a member of any groups that %s is a member of';
  633. $message_t = $l->t('Sharing %s failed, because the user %s is not a member of any groups that %s is a member of', array($itemName, $shareWith, $uidOwner));
  634. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG);
  635. throw new \Exception($message_t);
  636. }
  637. }
  638. // Check if the item source is already shared with the user, either from the same owner or a different user
  639. if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups,
  640. $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) {
  641. // Only allow the same share to occur again if it is the same
  642. // owner and is not a user share, this use case is for increasing
  643. // permissions for a specific user
  644. if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
  645. $message = 'Sharing %s failed, because this item is already shared with %s';
  646. $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
  647. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
  648. throw new \Exception($message_t);
  649. }
  650. }
  651. if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER,
  652. $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) {
  653. // Only allow the same share to occur again if it is the same
  654. // owner and is not a user share, this use case is for increasing
  655. // permissions for a specific user
  656. if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
  657. $message = 'Sharing %s failed, because this item is already shared with user %s';
  658. $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith));
  659. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR);
  660. throw new \Exception($message_t);
  661. }
  662. }
  663. } else if ($shareType === self::SHARE_TYPE_GROUP) {
  664. if (!\OC_Group::groupExists($shareWith)) {
  665. $message = 'Sharing %s failed, because the group %s does not exist';
  666. $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith));
  667. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
  668. throw new \Exception($message_t);
  669. }
  670. if ($shareWithinGroupOnly && !\OC_Group::inGroup($uidOwner, $shareWith)) {
  671. $message = 'Sharing %s failed, because '
  672. .'%s is not a member of the group %s';
  673. $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith));
  674. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG);
  675. throw new \Exception($message_t);
  676. }
  677. // Check if the item source is already shared with the group, either from the same owner or a different user
  678. // The check for each user in the group is done inside the put() function
  679. if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith,
  680. null, self::FORMAT_NONE, null, 1, true, true)) {
  681. if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
  682. $message = 'Sharing %s failed, because this item is already shared with %s';
  683. $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
  684. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
  685. throw new \Exception($message_t);
  686. }
  687. }
  688. // Convert share with into an array with the keys group and users
  689. $group = $shareWith;
  690. $shareWith = array();
  691. $shareWith['group'] = $group;
  692. $shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner));
  693. } else if ($shareType === self::SHARE_TYPE_LINK) {
  694. $updateExistingShare = false;
  695. if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') {
  696. // IF the password is changed via the old ajax endpoint verify it before deleting the old share
  697. if ($passwordChanged === true) {
  698. self::verifyPassword($shareWith);
  699. }
  700. // when updating a link share
  701. // FIXME Don't delete link if we update it
  702. if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null,
  703. $uidOwner, self::FORMAT_NONE, null, 1)) {
  704. // remember old token
  705. $oldToken = $checkExists['token'];
  706. $oldPermissions = $checkExists['permissions'];
  707. //delete the old share
  708. Helper::delete($checkExists['id']);
  709. $updateExistingShare = true;
  710. }
  711. if ($passwordChanged === null) {
  712. // Generate hash of password - same method as user passwords
  713. if (is_string($shareWith) && $shareWith !== '') {
  714. self::verifyPassword($shareWith);
  715. $shareWith = \OC::$server->getHasher()->hash($shareWith);
  716. } else {
  717. // reuse the already set password, but only if we change permissions
  718. // otherwise the user disabled the password protection
  719. if ($checkExists && (int)$permissions !== (int)$oldPermissions) {
  720. $shareWith = $checkExists['share_with'];
  721. }
  722. }
  723. } else {
  724. if ($passwordChanged === true) {
  725. if (is_string($shareWith) && $shareWith !== '') {
  726. self::verifyPassword($shareWith);
  727. $shareWith = \OC::$server->getHasher()->hash($shareWith);
  728. }
  729. } else if ($updateExistingShare) {
  730. $shareWith = $checkExists['share_with'];
  731. }
  732. }
  733. if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) {
  734. $message = 'You need to provide a password to create a public link, only protected links are allowed';
  735. $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed');
  736. \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG);
  737. throw new \Exception($message_t);
  738. }
  739. if ($updateExistingShare === false &&
  740. self::isDefaultExpireDateEnabled() &&
  741. empty($expirationDate)) {
  742. $expirationDate = Helper::calcExpireDate();
  743. }
  744. // Generate token
  745. if (isset($oldToken)) {
  746. $token = $oldToken;
  747. } else {
  748. $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH,
  749. \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_UPPER.
  750. \OCP\Security\ISecureRandom::CHAR_DIGITS
  751. );
  752. }
  753. $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions,
  754. null, $token, $itemSourceName, $expirationDate);
  755. if ($result) {
  756. return $token;
  757. } else {
  758. return false;
  759. }
  760. }
  761. $message = 'Sharing %s failed, because sharing with links is not allowed';
  762. $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName));
  763. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
  764. throw new \Exception($message_t);
  765. } else if ($shareType === self::SHARE_TYPE_REMOTE) {
  766. /*
  767. * Check if file is not already shared with the remote user
  768. */
  769. if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE,
  770. $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) {
  771. $message = 'Sharing %s failed, because this item is already shared with %s';
  772. $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
  773. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
  774. throw new \Exception($message_t);
  775. }
  776. // don't allow federated shares if source and target server are the same
  777. list($user, $remote) = Helper::splitUserRemote($shareWith);
  778. $currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/'));
  779. $currentUser = \OC::$server->getUserSession()->getUser()->getUID();
  780. if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) {
  781. $message = 'Not allowed to create a federated share with the same user.';
  782. $message_t = $l->t('Not allowed to create a federated share with the same user');
  783. \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG);
  784. throw new \Exception($message_t);
  785. }
  786. $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER .
  787. \OCP\Security\ISecureRandom::CHAR_DIGITS);
  788. $shareWith = $user . '@' . $remote;
  789. $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName);
  790. $send = false;
  791. if ($shareId) {
  792. $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner);
  793. }
  794. if ($send === false) {
  795. $currentUser = \OC::$server->getUserSession()->getUser()->getUID();
  796. self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser);
  797. $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith));
  798. throw new \Exception($message_t);
  799. }
  800. return $send;
  801. } else {
  802. // Future share types need to include their own conditions
  803. $message = 'Share type %s is not valid for %s';
  804. $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource));
  805. \OCP\Util::writeLog('OCP\Share', sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG);
  806. throw new \Exception($message_t);
  807. }
  808. // Put the item into the database
  809. $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate);
  810. return $result ? true : false;
  811. }
  812. /**
  813. * Unshare an item from a user, group, or delete a private link
  814. * @param string $itemType
  815. * @param string $itemSource
  816. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  817. * @param string $shareWith User or group the item is being shared with
  818. * @param string $owner owner of the share, if null the current user is used
  819. * @return boolean true on success or false on failure
  820. */
  821. public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) {
  822. // check if it is a valid itemType
  823. self::getBackend($itemType);
  824. $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType);
  825. $toDelete = array();
  826. $newParent = null;
  827. $currentUser = $owner ? $owner : \OC_User::getUser();
  828. foreach ($items as $item) {
  829. // delete the item with the expected share_type and owner
  830. if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) {
  831. $toDelete = $item;
  832. // if there is more then one result we don't have to delete the children
  833. // but update their parent. For group shares the new parent should always be
  834. // the original group share and not the db entry with the unique name
  835. } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) {
  836. $newParent = $item['parent'];
  837. } else {
  838. $newParent = $item['id'];
  839. }
  840. }
  841. if (!empty($toDelete)) {
  842. self::unshareItem($toDelete, $newParent);
  843. return true;
  844. }
  845. return false;
  846. }
  847. /**
  848. * Unshare an item from all users, groups, and remove all links
  849. * @param string $itemType
  850. * @param string $itemSource
  851. * @return boolean true on success or false on failure
  852. */
  853. public static function unshareAll($itemType, $itemSource) {
  854. // Get all of the owners of shares of this item.
  855. $query = \OC_DB::prepare( 'SELECT `uid_owner` from `*PREFIX*share` WHERE `item_type`=? AND `item_source`=?' );
  856. $result = $query->execute(array($itemType, $itemSource));
  857. $shares = array();
  858. // Add each owner's shares to the array of all shares for this item.
  859. while ($row = $result->fetchRow()) {
  860. $shares = array_merge($shares, self::getItems($itemType, $itemSource, null, null, $row['uid_owner']));
  861. }
  862. if (!empty($shares)) {
  863. // Pass all the vars we have for now, they may be useful
  864. $hookParams = array(
  865. 'itemType' => $itemType,
  866. 'itemSource' => $itemSource,
  867. 'shares' => $shares,
  868. );
  869. \OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams);
  870. foreach ($shares as $share) {
  871. self::unshareItem($share);
  872. }
  873. \OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams);
  874. return true;
  875. }
  876. return false;
  877. }
  878. /**
  879. * Unshare an item shared with the current user
  880. * @param string $itemType
  881. * @param string $itemOrigin Item target or source
  882. * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional)
  883. * @return boolean true on success or false on failure
  884. *
  885. * Unsharing from self is not allowed for items inside collections
  886. */
  887. public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) {
  888. $originType = ($originIsSource) ? 'source' : 'target';
  889. $uid = \OCP\User::getUser();
  890. if ($itemType === 'file' || $itemType === 'folder') {
  891. $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?';
  892. } else {
  893. $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?';
  894. }
  895. $query = \OCP\DB::prepare($statement);
  896. $result = $query->execute(array($itemType, $itemOrigin));
  897. $shares = $result->fetchAll();
  898. $listOfUnsharedItems = array();
  899. $itemUnshared = false;
  900. foreach ($shares as $share) {
  901. if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER &&
  902. $share['share_with'] === $uid) {
  903. $deletedShares = Helper::delete($share['id']);
  904. $shareTmp = array(
  905. 'id' => $share['id'],
  906. 'shareWith' => $share['share_with'],
  907. 'itemTarget' => $share['item_target'],
  908. 'itemType' => $share['item_type'],
  909. 'shareType' => (int)$share['share_type'],
  910. );
  911. if (isset($share['file_target'])) {
  912. $shareTmp['fileTarget'] = $share['file_target'];
  913. }
  914. $listOfUnsharedItems = array_merge($listOfUnsharedItems, $deletedShares, array($shareTmp));
  915. $itemUnshared = true;
  916. break;
  917. } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
  918. if (\OC_Group::inGroup($uid, $share['share_with'])) {
  919. $groupShare = $share;
  920. }
  921. } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique &&
  922. $share['share_with'] === $uid) {
  923. $uniqueGroupShare = $share;
  924. }
  925. }
  926. if (!$itemUnshared && isset($groupShare) && !isset($uniqueGroupShare)) {
  927. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`'
  928. .' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,'
  929. .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)'
  930. .' VALUES (?,?,?,?,?,?,?,?,?,?,?)');
  931. $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'],
  932. $groupShare['id'], self::$shareTypeGroupUserUnique,
  933. \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'],
  934. $groupShare['file_target']));
  935. $shareTmp = array(
  936. 'id' => $groupShare['id'],
  937. 'shareWith' => $groupShare['share_with'],
  938. 'itemTarget' => $groupShare['item_target'],
  939. 'itemType' => $groupShare['item_type'],
  940. 'shareType' => (int)$groupShare['share_type'],
  941. );
  942. if (isset($groupShare['file_target'])) {
  943. $shareTmp['fileTarget'] = $groupShare['file_target'];
  944. }
  945. $listOfUnsharedItems = array_merge($listOfUnsharedItems, array($shareTmp));
  946. $itemUnshared = true;
  947. } elseif (!$itemUnshared && isset($uniqueGroupShare)) {
  948. $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?');
  949. $query->execute(array(0, $uniqueGroupShare['id']));
  950. $shareTmp = array(
  951. 'id' => $uniqueGroupShare['id'],
  952. 'shareWith' => $uniqueGroupShare['share_with'],
  953. 'itemTarget' => $uniqueGroupShare['item_target'],
  954. 'itemType' => $uniqueGroupShare['item_type'],
  955. 'shareType' => (int)$uniqueGroupShare['share_type'],
  956. );
  957. if (isset($uniqueGroupShare['file_target'])) {
  958. $shareTmp['fileTarget'] = $uniqueGroupShare['file_target'];
  959. }
  960. $listOfUnsharedItems = array_merge($listOfUnsharedItems, array($shareTmp));
  961. $itemUnshared = true;
  962. }
  963. if ($itemUnshared) {
  964. \OC_Hook::emit('OCP\Share', 'post_unshareFromSelf',
  965. array('unsharedItems' => $listOfUnsharedItems, 'itemType' => $itemType));
  966. }
  967. return $itemUnshared;
  968. }
  969. /**
  970. * sent status if users got informed by mail about share
  971. * @param string $itemType
  972. * @param string $itemSource
  973. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  974. * @param string $recipient with whom was the file shared
  975. * @param boolean $status
  976. */
  977. public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) {
  978. $status = $status ? 1 : 0;
  979. $query = \OC_DB::prepare(
  980. 'UPDATE `*PREFIX*share`
  981. SET `mail_send` = ?
  982. WHERE `item_type` = ? AND `item_source` = ? AND `share_type` = ? AND `share_with` = ?');
  983. $result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient));
  984. if($result === false) {
  985. \OCP\Util::writeLog('OCP\Share', 'Couldn\'t set send mail status', \OCP\Util::ERROR);
  986. }
  987. }
  988. /**
  989. * Set the permissions of an item for a specific user or group
  990. * @param string $itemType
  991. * @param string $itemSource
  992. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  993. * @param string $shareWith User or group the item is being shared with
  994. * @param int $permissions CRUDS permissions
  995. * @return boolean true on success or false on failure
  996. * @throws \Exception when trying to grant more permissions then the user has himself
  997. */
  998. public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
  999. $l = \OC::$server->getL10N('lib');
  1000. $connection = \OC::$server->getDatabaseConnection();
  1001. $intArrayToLiteralArray = function($intArray, $eb) {
  1002. return array_map(function($int) use ($eb) {
  1003. return $eb->literal((int)$int, 'integer');
  1004. }, $intArray);
  1005. };
  1006. $sanitizeItem = function($item) {
  1007. $item['id'] = (int)$item['id'];
  1008. $item['premissions'] = (int)$item['permissions'];
  1009. return $item;
  1010. };
  1011. if ($rootItem = self::getItems($itemType, $itemSource, $shareType, $shareWith,
  1012. \OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) {
  1013. // Check if this item is a reshare and verify that the permissions
  1014. // granted don't exceed the parent shared item
  1015. if (isset($rootItem['parent'])) {
  1016. $qb = $connection->getQueryBuilder();
  1017. $qb->select('permissions')
  1018. ->from('share')
  1019. ->where($qb->expr()->eq('id', $qb->createParameter('id')))
  1020. ->setParameter(':id', $rootItem['parent']);
  1021. $dbresult = $qb->execute();
  1022. $result = $dbresult->fetch();
  1023. $dbresult->closeCursor();
  1024. if (~(int)$result['permissions'] & $permissions) {
  1025. $message = 'Setting permissions for %s failed,'
  1026. .' because the permissions exceed permissions granted to %s';
  1027. $message_t = $l->t('Setting permissions for %s failed, because the permissions exceed permissions granted to %s', array($itemSource, \OC_User::getUser()));
  1028. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, \OC_User::getUser()), \OCP\Util::DEBUG);
  1029. throw new \Exception($message_t);
  1030. }
  1031. }
  1032. $qb = $connection->getQueryBuilder();
  1033. $qb->update('share')
  1034. ->set('permissions', $qb->createParameter('permissions'))
  1035. ->where($qb->expr()->eq('id', $qb->createParameter('id')))
  1036. ->setParameter(':id', $rootItem['id'])
  1037. ->setParameter(':permissions', $permissions);
  1038. $qb->execute();
  1039. if ($itemType === 'file' || $itemType === 'folder') {
  1040. \OC_Hook::emit('OCP\Share', 'post_update_permissions', array(
  1041. 'itemType' => $itemType,
  1042. 'itemSource' => $itemSource,
  1043. 'shareType' => $shareType,
  1044. 'shareWith' => $shareWith,
  1045. 'uidOwner' => \OC_User::getUser(),
  1046. 'permissions' => $permissions,
  1047. 'path' => $rootItem['path'],
  1048. 'share' => $rootItem
  1049. ));
  1050. }
  1051. // Share id's to update with the new permissions
  1052. $ids = [];
  1053. $items = [];
  1054. // Check if permissions were removed
  1055. if ((int)$rootItem['permissions'] & ~$permissions) {
  1056. // If share permission is removed all reshares must be deleted
  1057. if (($rootItem['permissions'] & \OCP\Constants::PERMISSION_SHARE) && (~$permissions & \OCP\Constants::PERMISSION_SHARE)) {
  1058. // delete all shares, keep parent and group children
  1059. Helper::delete($rootItem['id'], true, null, null, true);
  1060. }
  1061. // Remove permission from all children
  1062. $parents = [$rootItem['id']];
  1063. while (!empty($parents)) {
  1064. $parents = $intArrayToLiteralArray($parents, $qb->expr());
  1065. $qb = $connection->getQueryBuilder();
  1066. $qb->select('id', 'permissions', 'item_type')
  1067. ->from('share')
  1068. ->where($qb->expr()->in('parent', $parents));
  1069. $result = $qb->execute();
  1070. // Reset parents array, only go through loop again if
  1071. // items are found that need permissions removed
  1072. $parents = [];
  1073. while ($item = $result->fetch()) {
  1074. $item = $sanitizeItem($item);
  1075. $items[] = $item;
  1076. // Check if permissions need to be removed
  1077. if ($item['permissions'] & ~$permissions) {
  1078. // Add to list of items that need permissions removed
  1079. $ids[] = $item['id'];
  1080. $parents[] = $item['id'];
  1081. }
  1082. }
  1083. $result->closeCursor();
  1084. }
  1085. // Remove the permissions for all reshares of this item
  1086. if (!empty($ids)) {
  1087. $ids = "'".implode("','", $ids)."'";
  1088. // TODO this should be done with Doctrine platform objects
  1089. if (\OC::$server->getConfig()->getSystemValue("dbtype") === 'oci') {
  1090. $andOp = 'BITAND(`permissions`, ?)';
  1091. } else {
  1092. $andOp = '`permissions` & ?';
  1093. }
  1094. $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = '.$andOp
  1095. .' WHERE `id` IN ('.$ids.')');
  1096. $query->execute(array($permissions));
  1097. }
  1098. }
  1099. /*
  1100. * Permissions were added
  1101. * Update all USERGROUP shares. (So group shares where the user moved their mountpoint).
  1102. */
  1103. if ($permissions & ~(int)$rootItem['permissions']) {
  1104. $qb = $connection->getQueryBuilder();
  1105. $qb->select('id', 'permissions', 'item_type')
  1106. ->from('share')
  1107. ->where($qb->expr()->eq('parent', $qb->createParameter('parent')))
  1108. ->andWhere($qb->expr()->eq('share_type', $qb->createParameter('share_type')))
  1109. ->andWhere($qb->expr()->neq('permissions', $qb->createParameter('shareDeleted')))
  1110. ->setParameter(':parent', (int)$rootItem['id'])
  1111. ->setParameter(':share_type', 2)
  1112. ->setParameter(':shareDeleted', 0);
  1113. $result = $qb->execute();
  1114. $ids = [];
  1115. while ($item = $result->fetch()) {
  1116. $item = $sanitizeItem($item);
  1117. $items[] = $item;
  1118. $ids[] = $item['id'];
  1119. }
  1120. $result->closeCursor();
  1121. // Add permssions for all USERGROUP shares of this item
  1122. if (!empty($ids)) {
  1123. $ids = $intArrayToLiteralArray($ids, $qb->expr());
  1124. $qb = $connection->getQueryBuilder();
  1125. $qb->update('share')
  1126. ->set('permissions', $qb->createParameter('permissions'))
  1127. ->where($qb->expr()->in('id', $ids))
  1128. ->setParameter(':permissions', $permissions);
  1129. $qb->execute();
  1130. }
  1131. }
  1132. foreach ($items as $item) {
  1133. \OC_Hook::emit('OCP\Share', 'post_update_permissions', ['share' => $item]);
  1134. }
  1135. return true;
  1136. }
  1137. $message = 'Setting permissions for %s failed, because the item was not found';
  1138. $message_t = $l->t('Setting permissions for %s failed, because the item was not found', array($itemSource));
  1139. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG);
  1140. throw new \Exception($message_t);
  1141. }
  1142. /**
  1143. * validate expiration date if it meets all constraints
  1144. *
  1145. * @param string $expireDate well formate date string, e.g. "DD-MM-YYYY"
  1146. * @param string $shareTime timestamp when the file was shared
  1147. * @param string $itemType
  1148. * @param string $itemSource
  1149. * @return \DateTime validated date
  1150. * @throws \Exception when the expire date is in the past or further in the future then the enforced date
  1151. */
  1152. private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) {
  1153. $l = \OC::$server->getL10N('lib');
  1154. $date = new \DateTime($expireDate);
  1155. $today = new \DateTime('now');
  1156. // if the user doesn't provide a share time we need to get it from the database
  1157. // fall-back mode to keep API stable, because the $shareTime parameter was added later
  1158. $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced();
  1159. if ($defaultExpireDateEnforced && $shareTime === null) {
  1160. $items = self::getItemShared($itemType, $itemSource);
  1161. $firstItem = reset($items);
  1162. $shareTime = (int)$firstItem['stime'];
  1163. }
  1164. if ($defaultExpireDateEnforced) {
  1165. // initialize max date with share time
  1166. $maxDate = new \DateTime();
  1167. $maxDate->setTimestamp($shareTime);
  1168. $maxDays = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
  1169. $maxDate->add(new \DateInterval('P' . $maxDays . 'D'));
  1170. if ($date > $maxDate) {
  1171. $warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared';
  1172. $warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays));
  1173. \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN);
  1174. throw new \Exception($warning_t);
  1175. }
  1176. }
  1177. if ($date < $today) {
  1178. $message = 'Cannot set expiration date. Expiration date is in the past';
  1179. $message_t = $l->t('Cannot set expiration date. Expiration date is in the past');
  1180. \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::WARN);
  1181. throw new \Exception($message_t);
  1182. }
  1183. return $date;
  1184. }
  1185. /**
  1186. * Set expiration date for a share
  1187. * @param string $itemType
  1188. * @param string $itemSource
  1189. * @param string $date expiration date
  1190. * @param int $shareTime timestamp from when the file was shared
  1191. * @return boolean
  1192. * @throws \Exception when the expire date is not set, in the past or further in the future then the enforced date
  1193. */
  1194. public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) {
  1195. $user = \OC_User::getUser();
  1196. $l = \OC::$server->getL10N('lib');
  1197. if ($date == '') {
  1198. if (\OCP\Util::isDefaultExpireDateEnforced()) {
  1199. $warning = 'Cannot clear expiration date. Shares are required to have an expiration date.';
  1200. $warning_t = $l->t('Cannot clear expiration date. Shares are required to have an expiration date.');
  1201. \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN);
  1202. throw new \Exception($warning_t);
  1203. } else {
  1204. $date = null;
  1205. }
  1206. } else {
  1207. $date = self::validateExpireDate($date, $shareTime, $itemType, $itemSource);
  1208. }
  1209. $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?');
  1210. $query->bindValue(1, $date, 'datetime');
  1211. $query->bindValue(2, $itemType);
  1212. $query->bindValue(3, $itemSource);
  1213. $query->bindValue(4, $user);
  1214. $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK);
  1215. $query->execute();
  1216. \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array(
  1217. 'itemType' => $itemType,
  1218. 'itemSource' => $itemSource,
  1219. 'date' => $date,
  1220. 'uidOwner' => $user
  1221. ));
  1222. return true;
  1223. }
  1224. /**
  1225. * Retrieve the owner of a connection
  1226. *
  1227. * @param IDBConnection $connection
  1228. * @param int $shareId
  1229. * @throws \Exception
  1230. * @return string uid of share owner
  1231. */
  1232. private static function getShareOwner(IDBConnection $connection, $shareId) {
  1233. $qb = $connection->getQueryBuilder();
  1234. $qb->select('uid_owner')
  1235. ->from('share')
  1236. ->where($qb->expr()->eq('id', $qb->createParameter('shareId')))
  1237. ->setParameter(':shareId', $shareId);
  1238. $result = $qb->execute();
  1239. $result = $result->fetch();
  1240. if (empty($result)) {
  1241. throw new \Exception('Share not found');
  1242. }
  1243. return $result['uid_owner'];
  1244. }
  1245. /**
  1246. * Set password for a public link share
  1247. *
  1248. * @param IUserSession $userSession
  1249. * @param IDBConnection $connection
  1250. * @param IConfig $config
  1251. * @param int $shareId
  1252. * @param string $password
  1253. * @throws \Exception
  1254. * @return boolean
  1255. */
  1256. public static function setPassword(IUserSession $userSession,
  1257. IDBConnection $connection,
  1258. IConfig $config,
  1259. $shareId, $password) {
  1260. $user = $userSession->getUser();
  1261. if (is_null($user)) {
  1262. throw new \Exception("User not logged in");
  1263. }
  1264. $uid = self::getShareOwner($connection, $shareId);
  1265. if ($uid !== $user->getUID()) {
  1266. throw new \Exception('Cannot update share of a different user');
  1267. }
  1268. if ($password === '') {
  1269. $password = null;
  1270. }
  1271. //If passwords are enforced the password can't be null
  1272. if (self::enforcePassword($config) && is_null($password)) {
  1273. throw new \Exception('Cannot remove password');
  1274. }
  1275. self::verifyPassword($password);
  1276. $qb = $connection->getQueryBuilder();
  1277. $qb->update('share')
  1278. ->set('share_with', $qb->createParameter('pass'))
  1279. ->where($qb->expr()->eq('id', $qb->createParameter('shareId')))
  1280. ->setParameter(':pass', is_null($password) ? null : \OC::$server->getHasher()->hash($password))
  1281. ->setParameter(':shareId', $shareId);
  1282. $qb->execute();
  1283. return true;
  1284. }
  1285. /**
  1286. * Checks whether a share has expired, calls unshareItem() if yes.
  1287. * @param array $item Share data (usually database row)
  1288. * @return boolean True if item was expired, false otherwise.
  1289. */
  1290. protected static function expireItem(array $item) {
  1291. $result = false;
  1292. // only use default expiration date for link shares
  1293. if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) {
  1294. // calculate expiration date
  1295. if (!empty($item['expiration'])) {
  1296. $userDefinedExpire = new \DateTime($item['expiration']);
  1297. $expires = $userDefinedExpire->getTimestamp();
  1298. } else {
  1299. $expires = null;
  1300. }
  1301. // get default expiration settings
  1302. $defaultSettings = Helper::getDefaultExpireSetting();
  1303. $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires);
  1304. if (is_int($expires)) {
  1305. $now = time();
  1306. if ($now > $expires) {
  1307. self::unshareItem($item);
  1308. $result = true;
  1309. }
  1310. }
  1311. }
  1312. return $result;
  1313. }
  1314. /**
  1315. * Unshares a share given a share data array
  1316. * @param array $item Share data (usually database row)
  1317. * @param int $newParent parent ID
  1318. * @return null
  1319. */
  1320. protected static function unshareItem(array $item, $newParent = null) {
  1321. $shareType = (int)$item['share_type'];
  1322. $shareWith = null;
  1323. if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) {
  1324. $shareWith = $item['share_with'];
  1325. }
  1326. // Pass all the vars we have for now, they may be useful
  1327. $hookParams = array(
  1328. 'id' => $item['id'],
  1329. 'itemType' => $item['item_type'],
  1330. 'itemSource' => $item['item_source'],
  1331. 'shareType' => $shareType,
  1332. 'shareWith' => $shareWith,
  1333. 'itemParent' => $item['parent'],
  1334. 'uidOwner' => $item['uid_owner'],
  1335. );
  1336. if($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
  1337. $hookParams['fileSource'] = $item['file_source'];
  1338. $hookParams['fileTarget'] = $item['file_target'];
  1339. }
  1340. \OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams);
  1341. $deletedShares = Helper::delete($item['id'], false, null, $newParent);
  1342. $deletedShares[] = $hookParams;
  1343. $hookParams['deletedShares'] = $deletedShares;
  1344. \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams);
  1345. if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
  1346. list(, $remote) = Helper::splitUserRemote($item['share_with']);
  1347. self::sendRemoteUnshare($remote, $item['id'], $item['token']);
  1348. }
  1349. }
  1350. /**
  1351. * Get the backend class for the specified item type
  1352. * @param string $itemType
  1353. * @throws \Exception
  1354. * @return \OCP\Share_Backend
  1355. */
  1356. public static function getBackend($itemType) {
  1357. $l = \OC::$server->getL10N('lib');
  1358. if (isset(self::$backends[$itemType])) {
  1359. return self::$backends[$itemType];
  1360. } else if (isset(self::$backendTypes[$itemType]['class'])) {
  1361. $class = self::$backendTypes[$itemType]['class'];
  1362. if (class_exists($class)) {
  1363. self::$backends[$itemType] = new $class;
  1364. if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) {
  1365. $message = 'Sharing backend %s must implement the interface OCP\Share_Backend';
  1366. $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class));
  1367. \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR);
  1368. throw new \Exception($message_t);
  1369. }
  1370. return self::$backends[$itemType];
  1371. } else {
  1372. $message = 'Sharing backend %s not found';
  1373. $message_t = $l->t('Sharing backend %s not found', array($class));
  1374. \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR);
  1375. throw new \Exception($message_t);
  1376. }
  1377. }
  1378. $message = 'Sharing backend for %s not found';
  1379. $message_t = $l->t('Sharing backend for %s not found', array($itemType));
  1380. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), \OCP\Util::ERROR);
  1381. throw new \Exception($message_t);
  1382. }
  1383. /**
  1384. * Check if resharing is allowed
  1385. * @return boolean true if allowed or false
  1386. *
  1387. * Resharing is allowed by default if not configured
  1388. */
  1389. public static function isResharingAllowed() {
  1390. if (!isset(self::$isResharingAllowed)) {
  1391. if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') {
  1392. self::$isResharingAllowed = true;
  1393. } else {
  1394. self::$isResharingAllowed = false;
  1395. }
  1396. }
  1397. return self::$isResharingAllowed;
  1398. }
  1399. /**
  1400. * Get a list of collection item types for the specified item type
  1401. * @param string $itemType
  1402. * @return array
  1403. */
  1404. private static function getCollectionItemTypes($itemType) {
  1405. $collectionTypes = array($itemType);
  1406. foreach (self::$backendTypes as $type => $backend) {
  1407. if (in_array($backend['collectionOf'], $collectionTypes)) {
  1408. $collectionTypes[] = $type;
  1409. }
  1410. }
  1411. // TODO Add option for collections to be collection of themselves, only 'folder' does it now...
  1412. if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) {
  1413. unset($collectionTypes[0]);
  1414. }
  1415. // Return array if collections were found or the item type is a
  1416. // collection itself - collections can be inside collections
  1417. if (count($collectionTypes) > 0) {
  1418. return $collectionTypes;
  1419. }
  1420. return false;
  1421. }
  1422. /**
  1423. * Get the owners of items shared with a user.
  1424. *
  1425. * @param string $user The user the items are shared with.
  1426. * @param string $type The type of the items shared with the user.
  1427. * @param boolean $includeCollections Include collection item types (optional)
  1428. * @param boolean $includeOwner include owner in the list of users the item is shared with (optional)
  1429. * @return array
  1430. */
  1431. public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) {
  1432. // First, we find out if $type is part of a collection (and if that collection is part of
  1433. // another one and so on).
  1434. $collectionTypes = array();
  1435. if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) {
  1436. $collectionTypes[] = $type;
  1437. }
  1438. // Of these collection types, along with our original $type, we make a
  1439. // list of the ones for which a sharing backend has been registered.
  1440. // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it
  1441. // with its $includeCollections parameter set to true. Unfortunately, this fails currently.
  1442. $allMaybeSharedItems = array();
  1443. foreach ($collectionTypes as $collectionType) {
  1444. if (isset(self::$backends[$collectionType])) {
  1445. $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser(
  1446. $collectionType,
  1447. $user,
  1448. self::FORMAT_NONE
  1449. );
  1450. }
  1451. }
  1452. $owners = array();
  1453. if ($includeOwner) {
  1454. $owners[] = $user;
  1455. }
  1456. // We take a look at all shared items of the given $type (or of the collections it is part of)
  1457. // and find out their owners. Then, we gather the tags for the original $type from all owners,
  1458. // and return them as elements of a list that look like "Tag (owner)".
  1459. foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) {
  1460. foreach ($maybeSharedItems as $sharedItem) {
  1461. if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814
  1462. $owners[] = $sharedItem['uid_owner'];
  1463. }
  1464. }
  1465. }
  1466. return $owners;
  1467. }
  1468. /**
  1469. * Get shared items from the database
  1470. * @param string $itemType
  1471. * @param string $item Item source or target (optional)
  1472. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique
  1473. * @param string $shareWith User or group the item is being shared with
  1474. * @param string $uidOwner User that is the owner of shared items (optional)
  1475. * @param int $format Format to convert items to with formatItems() (optional)
  1476. * @param mixed $parameters to pass to formatItems() (optional)
  1477. * @param int $limit Number of items to return, -1 to return all matches (optional)
  1478. * @param boolean $includeCollections Include collection item types (optional)
  1479. * @param boolean $itemShareWithBySource (optional)
  1480. * @param boolean $checkExpireDate
  1481. * @return array
  1482. *
  1483. * See public functions getItem(s)... for parameter usage
  1484. *
  1485. */
  1486. public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null,
  1487. $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1,
  1488. $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) {
  1489. if (!self::isEnabled()) {
  1490. return array();
  1491. }
  1492. $backend = self::getBackend($itemType);
  1493. $collectionTypes = false;
  1494. // Get filesystem root to add it to the file target and remove from the
  1495. // file source, match file_source with the file cache
  1496. if ($itemType == 'file' || $itemType == 'folder') {
  1497. if(!is_null($uidOwner)) {
  1498. $root = \OC\Files\Filesystem::getRoot();
  1499. } else {
  1500. $root = '';
  1501. }
  1502. $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ';
  1503. if (!isset($item)) {
  1504. $where .= ' AND `file_target` IS NOT NULL ';
  1505. }
  1506. $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` ';
  1507. $fileDependent = true;
  1508. $queryArgs = array();
  1509. } else {
  1510. $fileDependent = false;
  1511. $root = '';
  1512. $collectionTypes = self::getCollectionItemTypes($itemType);
  1513. if ($includeCollections && !isset($item) && $collectionTypes) {
  1514. // If includeCollections is true, find collections of this item type, e.g. a music album contains songs
  1515. if (!in_array($itemType, $collectionTypes)) {
  1516. $itemTypes = array_merge(array($itemType), $collectionTypes);
  1517. } else {
  1518. $itemTypes = $collectionTypes;
  1519. }
  1520. $placeholders = join(',', array_fill(0, count($itemTypes), '?'));
  1521. $where = ' WHERE `item_type` IN ('.$placeholders.'))';
  1522. $queryArgs = $itemTypes;
  1523. } else {
  1524. $where = ' WHERE `item_type` = ?';
  1525. $queryArgs = array($itemType);
  1526. }
  1527. }
  1528. if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
  1529. $where .= ' AND `share_type` != ?';
  1530. $queryArgs[] = self::SHARE_TYPE_LINK;
  1531. }
  1532. if (isset($shareType)) {
  1533. // Include all user and group items
  1534. if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) {
  1535. $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) ';
  1536. $queryArgs[] = self::SHARE_TYPE_USER;
  1537. $queryArgs[] = self::$shareTypeGroupUserUnique;
  1538. $queryArgs[] = $shareWith;
  1539. $groups = \OC_Group::getUserGroups($shareWith);
  1540. if (!empty($groups)) {
  1541. $placeholders = join(',', array_fill(0, count($groups), '?'));
  1542. $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) ';
  1543. $queryArgs[] = self::SHARE_TYPE_GROUP;
  1544. $queryArgs = array_merge($queryArgs, $groups);
  1545. }
  1546. $where .= ')';
  1547. // Don't include own group shares
  1548. $where .= ' AND `uid_owner` != ?';
  1549. $queryArgs[] = $shareWith;
  1550. } else {
  1551. $where .= ' AND `share_type` = ?';
  1552. $queryArgs[] = $shareType;
  1553. if (isset($shareWith)) {
  1554. $where .= ' AND `share_with` = ?';
  1555. $queryArgs[] = $shareWith;
  1556. }
  1557. }
  1558. }
  1559. if (isset($uidOwner)) {
  1560. $where .= ' AND `uid_owner` = ?';
  1561. $queryArgs[] = $uidOwner;
  1562. if (!isset($shareType)) {
  1563. // Prevent unique user targets for group shares from being selected
  1564. $where .= ' AND `share_type` != ?';
  1565. $queryArgs[] = self::$shareTypeGroupUserUnique;
  1566. }
  1567. if ($fileDependent) {
  1568. $column = 'file_source';
  1569. } else {
  1570. $column = 'item_source';
  1571. }
  1572. } else {
  1573. if ($fileDependent) {
  1574. $column = 'file_target';
  1575. } else {
  1576. $column = 'item_target';
  1577. }
  1578. }
  1579. if (isset($item)) {
  1580. $collectionTypes = self::getCollectionItemTypes($itemType);
  1581. if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) {
  1582. $where .= ' AND (';
  1583. } else {
  1584. $where .= ' AND';
  1585. }
  1586. // If looking for own shared items, check item_source else check item_target
  1587. if (isset($uidOwner) || $itemShareWithBySource) {
  1588. // If item type is a file, file source needs to be checked in case the item was converted
  1589. if ($fileDependent) {
  1590. $where .= ' `file_source` = ?';
  1591. $column = 'file_source';
  1592. } else {
  1593. $where .= ' `item_source` = ?';
  1594. $column = 'item_source';
  1595. }
  1596. } else {
  1597. if ($fileDependent) {
  1598. $where .= ' `file_target` = ?';
  1599. $item = \OC\Files\Filesystem::normalizePath($item);
  1600. } else {
  1601. $where .= ' `item_target` = ?';
  1602. }
  1603. }
  1604. $queryArgs[] = $item;
  1605. if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) {
  1606. $placeholders = join(',', array_fill(0, count($collectionTypes), '?'));
  1607. $where .= ' OR `item_type` IN ('.$placeholders.'))';
  1608. $queryArgs = array_merge($queryArgs, $collectionTypes);
  1609. }
  1610. }
  1611. if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) {
  1612. // Make sure the unique user target is returned if it exists,
  1613. // unique targets should follow the group share in the database
  1614. // If the limit is not 1, the filtering can be done later
  1615. $where .= ' ORDER BY `*PREFIX*share`.`id` DESC';
  1616. } else {
  1617. $where .= ' ORDER BY `*PREFIX*share`.`id` ASC';
  1618. }
  1619. if ($limit != -1 && !$includeCollections) {
  1620. // The limit must be at least 3, because filtering needs to be done
  1621. if ($limit < 3) {
  1622. $queryLimit = 3;
  1623. } else {
  1624. $queryLimit = $limit;
  1625. }
  1626. } else {
  1627. $queryLimit = null;
  1628. }
  1629. $select = self::createSelectStatement($format, $fileDependent, $uidOwner);
  1630. $root = strlen($root);
  1631. $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit);
  1632. $result = $query->execute($queryArgs);
  1633. if ($result === false) {
  1634. \OCP\Util::writeLog('OCP\Share',
  1635. \OC_DB::getErrorMessage() . ', select=' . $select . ' where=',
  1636. \OCP\Util::ERROR);
  1637. }
  1638. $items = array();
  1639. $targets = array();
  1640. $switchedItems = array();
  1641. $mounts = array();
  1642. while ($row = $result->fetchRow()) {
  1643. self::transformDBResults($row);
  1644. // Filter out duplicate group shares for users with unique targets
  1645. if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
  1646. continue;
  1647. }
  1648. if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) {
  1649. $row['share_type'] = self::SHARE_TYPE_GROUP;
  1650. $row['unique_name'] = true; // remember that we use a unique name for this user
  1651. $row['share_with'] = $items[$row['parent']]['share_with'];
  1652. // if the group share was unshared from the user we keep the permission, otherwise
  1653. // we take the permission from the parent because this is always the up-to-date
  1654. // permission for the group share
  1655. if ($row['permissions'] > 0) {
  1656. $row['permissions'] = $items[$row['parent']]['permissions'];
  1657. }
  1658. // Remove the parent group share
  1659. unset($items[$row['parent']]);
  1660. if ($row['permissions'] == 0) {
  1661. continue;
  1662. }
  1663. } else if (!isset($uidOwner)) {
  1664. // Check if the same target already exists
  1665. if (isset($targets[$row['id']])) {
  1666. // Check if the same owner shared with the user twice
  1667. // through a group and user share - this is allowed
  1668. $id = $targets[$row['id']];
  1669. if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) {
  1670. // Switch to group share type to ensure resharing conditions aren't bypassed
  1671. if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) {
  1672. $items[$id]['share_type'] = self::SHARE_TYPE_GROUP;
  1673. $items[$id]['share_with'] = $row['share_with'];
  1674. }
  1675. // Switch ids if sharing permission is granted on only
  1676. // one share to ensure correct parent is used if resharing
  1677. if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE
  1678. && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
  1679. $items[$row['id']] = $items[$id];
  1680. $switchedItems[$id] = $row['id'];
  1681. unset($items[$id]);
  1682. $id = $row['id'];
  1683. }
  1684. $items[$id]['permissions'] |= (int)$row['permissions'];
  1685. }
  1686. continue;
  1687. } elseif (!empty($row['parent'])) {
  1688. $targets[$row['parent']] = $row['id'];
  1689. }
  1690. }
  1691. // Remove root from file source paths if retrieving own shared items
  1692. if (isset($uidOwner) && isset($row['path'])) {
  1693. if (isset($row['parent'])) {
  1694. $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?');
  1695. $parentResult = $query->execute(array($row['parent']));
  1696. if ($result === false) {
  1697. \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' .
  1698. \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where,
  1699. \OCP\Util::ERROR);
  1700. } else {
  1701. $parentRow = $parentResult->fetchRow();
  1702. $tmpPath = $parentRow['file_target'];
  1703. // find the right position where the row path continues from the target path
  1704. $pos = strrpos($row['path'], $parentRow['file_target']);
  1705. $subPath = substr($row['path'], $pos);
  1706. $splitPath = explode('/', $subPath);
  1707. foreach (array_slice($splitPath, 2) as $pathPart) {
  1708. $tmpPath = $tmpPath . '/' . $pathPart;
  1709. }
  1710. $row['path'] = $tmpPath;
  1711. }
  1712. } else {
  1713. if (!isset($mounts[$row['storage']])) {
  1714. $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']);
  1715. if (is_array($mountPoints) && !empty($mountPoints)) {
  1716. $mounts[$row['storage']] = current($mountPoints);
  1717. }
  1718. }
  1719. if (!empty($mounts[$row['storage']])) {
  1720. $path = $mounts[$row['storage']]->getMountPoint().$row['path'];
  1721. $relPath = substr($path, $root); // path relative to data/user
  1722. $row['path'] = rtrim($relPath, '/');
  1723. }
  1724. }
  1725. }
  1726. if($checkExpireDate) {
  1727. if (self::expireItem($row)) {
  1728. continue;
  1729. }
  1730. }
  1731. // Check if resharing is allowed, if not remove share permission
  1732. if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) {
  1733. $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE;
  1734. }
  1735. // Add display names to result
  1736. $row['share_with_displayname'] = $row['share_with'];
  1737. if ( isset($row['share_with']) && $row['share_with'] != '' &&
  1738. $row['share_type'] === self::SHARE_TYPE_USER) {
  1739. $row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']);
  1740. } else if(isset($row['share_with']) && $row['share_with'] != '' &&
  1741. $row['share_type'] === self::SHARE_TYPE_REMOTE) {
  1742. $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']);
  1743. foreach ($addressBookEntries as $entry) {
  1744. foreach ($entry['CLOUD'] as $cloudID) {
  1745. if ($cloudID === $row['share_with']) {
  1746. $row['share_with_displayname'] = $entry['FN'];
  1747. }
  1748. }
  1749. }
  1750. }
  1751. if ( isset($row['uid_owner']) && $row['uid_owner'] != '') {
  1752. $row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']);
  1753. }
  1754. if ($row['permissions'] > 0) {
  1755. $items[$row['id']] = $row;
  1756. }
  1757. }
  1758. // group items if we are looking for items shared with the current user
  1759. if (isset($shareWith) && $shareWith === \OCP\User::getUser()) {
  1760. $items = self::groupItems($items, $itemType);
  1761. }
  1762. if (!empty($items)) {
  1763. $collectionItems = array();
  1764. foreach ($items as &$row) {
  1765. // Return only the item instead of a 2-dimensional array
  1766. if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) {
  1767. if ($format == self::FORMAT_NONE) {
  1768. return $row;
  1769. } else {
  1770. break;
  1771. }
  1772. }
  1773. // Check if this is a collection of the requested item type
  1774. if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) {
  1775. if (($collectionBackend = self::getBackend($row['item_type']))
  1776. && $collectionBackend instanceof \OCP\Share_Backend_Collection) {
  1777. // Collections can be inside collections, check if the item is a collection
  1778. if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) {
  1779. $collectionItems[] = $row;
  1780. } else {
  1781. $collection = array();
  1782. $collection['item_type'] = $row['item_type'];
  1783. if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
  1784. $collection['path'] = basename($row['path']);
  1785. }
  1786. $row['collection'] = $collection;
  1787. // Fetch all of the children sources
  1788. $children = $collectionBackend->getChildren($row[$column]);
  1789. foreach ($children as $child) {
  1790. $childItem = $row;
  1791. $childItem['item_type'] = $itemType;
  1792. if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') {
  1793. $childItem['item_source'] = $child['source'];
  1794. $childItem['item_target'] = $child['target'];
  1795. }
  1796. if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
  1797. if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
  1798. $childItem['file_source'] = $child['source'];
  1799. } else { // TODO is this really needed if we already know that we use the file backend?
  1800. $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']);
  1801. $childItem['file_source'] = $meta['fileid'];
  1802. }
  1803. $childItem['file_target'] =
  1804. \OC\Files\Filesystem::normalizePath($child['file_path']);
  1805. }
  1806. if (isset($item)) {
  1807. if ($childItem[$column] == $item) {
  1808. // Return only the item instead of a 2-dimensional array
  1809. if ($limit == 1) {
  1810. if ($format == self::FORMAT_NONE) {
  1811. return $childItem;
  1812. } else {
  1813. // Unset the items array and break out of both loops
  1814. $items = array();
  1815. $items[] = $childItem;
  1816. break 2;
  1817. }
  1818. } else {
  1819. $collectionItems[] = $childItem;
  1820. }
  1821. }
  1822. } else {
  1823. $collectionItems[] = $childItem;
  1824. }
  1825. }
  1826. }
  1827. }
  1828. // Remove collection item
  1829. $toRemove = $row['id'];
  1830. if (array_key_exists($toRemove, $switchedItems)) {
  1831. $toRemove = $switchedItems[$toRemove];
  1832. }
  1833. unset($items[$toRemove]);
  1834. } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) {
  1835. // FIXME: Thats a dirty hack to improve file sharing performance,
  1836. // see github issue #10588 for more details
  1837. // Need to find a solution which works for all back-ends
  1838. $collectionBackend = self::getBackend($row['item_type']);
  1839. $sharedParents = $collectionBackend->getParents($row['item_source']);
  1840. foreach ($sharedParents as $parent) {
  1841. $collectionItems[] = $parent;
  1842. }
  1843. }
  1844. }
  1845. if (!empty($collectionItems)) {
  1846. $collectionItems = array_unique($collectionItems, SORT_REGULAR);
  1847. $items = array_merge($items, $collectionItems);
  1848. }
  1849. // filter out invalid items, these can appear when subshare entries exist
  1850. // for a group in which the requested user isn't a member any more
  1851. $items = array_filter($items, function($item) {
  1852. return $item['share_type'] !== self::$shareTypeGroupUserUnique;
  1853. });
  1854. return self::formatResult($items, $column, $backend, $format, $parameters);
  1855. } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) {
  1856. // FIXME: Thats a dirty hack to improve file sharing performance,
  1857. // see github issue #10588 for more details
  1858. // Need to find a solution which works for all back-ends
  1859. $collectionItems = array();
  1860. $collectionBackend = self::getBackend('folder');
  1861. $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner);
  1862. foreach ($sharedParents as $parent) {
  1863. $collectionItems[] = $parent;
  1864. }
  1865. if ($limit === 1) {
  1866. return reset($collectionItems);
  1867. }
  1868. return self::formatResult($collectionItems, $column, $backend, $format, $parameters);
  1869. }
  1870. return array();
  1871. }
  1872. /**
  1873. * group items with link to the same source
  1874. *
  1875. * @param array $items
  1876. * @param string $itemType
  1877. * @return array of grouped items
  1878. */
  1879. protected static function groupItems($items, $itemType) {
  1880. $fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false;
  1881. $result = array();
  1882. foreach ($items as $item) {
  1883. $grouped = false;
  1884. foreach ($result as $key => $r) {
  1885. // for file/folder shares we need to compare file_source, otherwise we compare item_source
  1886. // only group shares if they already point to the same target, otherwise the file where shared
  1887. // before grouping of shares was added. In this case we don't group them toi avoid confusions
  1888. if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
  1889. (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) {
  1890. // add the first item to the list of grouped shares
  1891. if (!isset($result[$key]['grouped'])) {
  1892. $result[$key]['grouped'][] = $result[$key];
  1893. }
  1894. $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions'];
  1895. $result[$key]['grouped'][] = $item;
  1896. $grouped = true;
  1897. break;
  1898. }
  1899. }
  1900. if (!$grouped) {
  1901. $result[] = $item;
  1902. }
  1903. }
  1904. return $result;
  1905. }
  1906. /**
  1907. * Put shared item into the database
  1908. * @param string $itemType Item type
  1909. * @param string $itemSource Item source
  1910. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  1911. * @param string $shareWith User or group the item is being shared with
  1912. * @param string $uidOwner User that is the owner of shared item
  1913. * @param int $permissions CRUDS permissions
  1914. * @param boolean|array $parentFolder Parent folder target (optional)
  1915. * @param string $token (optional)
  1916. * @param string $itemSourceName name of the source item (optional)
  1917. * @param \DateTime $expirationDate (optional)
  1918. * @throws \Exception
  1919. * @return mixed id of the new share or false
  1920. */
  1921. private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
  1922. $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) {
  1923. $queriesToExecute = array();
  1924. $suggestedItemTarget = null;
  1925. $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = '';
  1926. $groupItemTarget = $itemTarget = $fileSource = $parent = 0;
  1927. $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate);
  1928. if(!empty($result)) {
  1929. $parent = $result['parent'];
  1930. $itemSource = $result['itemSource'];
  1931. $fileSource = $result['fileSource'];
  1932. $suggestedItemTarget = $result['suggestedItemTarget'];
  1933. $suggestedFileTarget = $result['suggestedFileTarget'];
  1934. $filePath = $result['filePath'];
  1935. }
  1936. $isGroupShare = false;
  1937. if ($shareType == self::SHARE_TYPE_GROUP) {
  1938. $isGroupShare = true;
  1939. if (isset($shareWith['users'])) {
  1940. $users = $shareWith['users'];
  1941. } else {
  1942. $users = \OC_Group::usersInGroup($shareWith['group']);
  1943. }
  1944. // remove current user from list
  1945. if (in_array(\OCP\User::getUser(), $users)) {
  1946. unset($users[array_search(\OCP\User::getUser(), $users)]);
  1947. }
  1948. $groupItemTarget = Helper::generateTarget($itemType, $itemSource,
  1949. $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget);
  1950. $groupFileTarget = Helper::generateTarget($itemType, $itemSource,
  1951. $shareType, $shareWith['group'], $uidOwner, $filePath);
  1952. // add group share to table and remember the id as parent
  1953. $queriesToExecute['groupShare'] = array(
  1954. 'itemType' => $itemType,
  1955. 'itemSource' => $itemSource,
  1956. 'itemTarget' => $groupItemTarget,
  1957. 'shareType' => $shareType,
  1958. 'shareWith' => $shareWith['group'],
  1959. 'uidOwner' => $uidOwner,
  1960. 'permissions' => $permissions,
  1961. 'shareTime' => time(),
  1962. 'fileSource' => $fileSource,
  1963. 'fileTarget' => $groupFileTarget,
  1964. 'token' => $token,
  1965. 'parent' => $parent,
  1966. 'expiration' => $expirationDate,
  1967. );
  1968. } else {
  1969. $users = array($shareWith);
  1970. $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
  1971. $suggestedItemTarget);
  1972. }
  1973. $run = true;
  1974. $error = '';
  1975. $preHookData = array(
  1976. 'itemType' => $itemType,
  1977. 'itemSource' => $itemSource,
  1978. 'shareType' => $shareType,
  1979. 'uidOwner' => $uidOwner,
  1980. 'permissions' => $permissions,
  1981. 'fileSource' => $fileSource,
  1982. 'expiration' => $expirationDate,
  1983. 'token' => $token,
  1984. 'run' => &$run,
  1985. 'error' => &$error
  1986. );
  1987. $preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
  1988. $preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
  1989. \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData);
  1990. if ($run === false) {
  1991. throw new \Exception($error);
  1992. }
  1993. foreach ($users as $user) {
  1994. $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource;
  1995. $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user);
  1996. $userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType;
  1997. if ($sourceExists && $sourceExists['item_source'] === $itemSource) {
  1998. $fileTarget = $sourceExists['file_target'];
  1999. $itemTarget = $sourceExists['item_target'];
  2000. // for group shares we don't need a additional entry if the target is the same
  2001. if($isGroupShare && $groupItemTarget === $itemTarget) {
  2002. continue;
  2003. }
  2004. } elseif(!$sourceExists && !$isGroupShare) {
  2005. $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user,
  2006. $uidOwner, $suggestedItemTarget, $parent);
  2007. if (isset($fileSource)) {
  2008. if ($parentFolder) {
  2009. if ($parentFolder === true) {
  2010. $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user,
  2011. $uidOwner, $suggestedFileTarget, $parent);
  2012. if ($fileTarget != $groupFileTarget) {
  2013. $parentFolders[$user]['folder'] = $fileTarget;
  2014. }
  2015. } else if (isset($parentFolder[$user])) {
  2016. $fileTarget = $parentFolder[$user]['folder'].$itemSource;
  2017. $parent = $parentFolder[$user]['id'];
  2018. }
  2019. } else {
  2020. $fileTarget = Helper::generateTarget('file', $filePath, $userShareType,
  2021. $user, $uidOwner, $suggestedFileTarget, $parent);
  2022. }
  2023. } else {
  2024. $fileTarget = null;
  2025. }
  2026. } else {
  2027. // group share which doesn't exists until now, check if we need a unique target for this user
  2028. $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user,
  2029. $uidOwner, $suggestedItemTarget, $parent);
  2030. // do we also need a file target
  2031. if (isset($fileSource)) {
  2032. $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user,
  2033. $uidOwner, $suggestedFileTarget, $parent);
  2034. } else {
  2035. $fileTarget = null;
  2036. }
  2037. if (($itemTarget === $groupItemTarget) &&
  2038. (!isset($fileSource) || $fileTarget === $groupFileTarget)) {
  2039. continue;
  2040. }
  2041. }
  2042. $queriesToExecute[] = array(
  2043. 'itemType' => $itemType,
  2044. 'itemSource' => $itemSource,
  2045. 'itemTarget' => $itemTarget,
  2046. 'shareType' => $userShareType,
  2047. 'shareWith' => $user,
  2048. 'uidOwner' => $uidOwner,
  2049. 'permissions' => $permissions,
  2050. 'shareTime' => time(),
  2051. 'fileSource' => $fileSource,
  2052. 'fileTarget' => $fileTarget,
  2053. 'token' => $token,
  2054. 'parent' => $parent,
  2055. 'expiration' => $expirationDate,
  2056. );
  2057. }
  2058. $id = false;
  2059. if ($isGroupShare) {
  2060. $id = self::insertShare($queriesToExecute['groupShare']);
  2061. // Save this id, any extra rows for this group share will need to reference it
  2062. $parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
  2063. unset($queriesToExecute['groupShare']);
  2064. }
  2065. foreach ($queriesToExecute as $shareQuery) {
  2066. $shareQuery['parent'] = $parent;
  2067. $id = self::insertShare($shareQuery);
  2068. }
  2069. $postHookData = array(
  2070. 'itemType' => $itemType,
  2071. 'itemSource' => $itemSource,
  2072. 'parent' => $parent,
  2073. 'shareType' => $shareType,
  2074. 'uidOwner' => $uidOwner,
  2075. 'permissions' => $permissions,
  2076. 'fileSource' => $fileSource,
  2077. 'id' => $parent,
  2078. 'token' => $token,
  2079. 'expirationDate' => $expirationDate,
  2080. );
  2081. $postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
  2082. $postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
  2083. $postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget;
  2084. \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData);
  2085. return $id ? $id : false;
  2086. }
  2087. /**
  2088. * @param string $itemType
  2089. * @param string $itemSource
  2090. * @param int $shareType
  2091. * @param string $shareWith
  2092. * @param string $uidOwner
  2093. * @param int $permissions
  2094. * @param string|null $itemSourceName
  2095. * @param null|\DateTime $expirationDate
  2096. */
  2097. private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) {
  2098. $backend = self::getBackend($itemType);
  2099. $l = \OC::$server->getL10N('lib');
  2100. $result = array();
  2101. $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source';
  2102. $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true);
  2103. if ($checkReshare) {
  2104. // Check if attempting to share back to owner
  2105. if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) {
  2106. $message = 'Sharing %s failed, because the user %s is the original sharer';
  2107. $message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]);
  2108. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
  2109. throw new \Exception($message_t);
  2110. }
  2111. }
  2112. if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) {
  2113. // Check if share permissions is granted
  2114. if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
  2115. if (~(int)$checkReshare['permissions'] & $permissions) {
  2116. $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s';
  2117. $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner));
  2118. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG);
  2119. throw new \Exception($message_t);
  2120. } else {
  2121. // TODO Don't check if inside folder
  2122. $result['parent'] = $checkReshare['id'];
  2123. $result['expirationDate'] = $expirationDate;
  2124. // $checkReshare['expiration'] could be null and then is always less than any value
  2125. if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) {
  2126. $result['expirationDate'] = $checkReshare['expiration'];
  2127. }
  2128. // only suggest the same name as new target if it is a reshare of the
  2129. // same file/folder and not the reshare of a child
  2130. if ($checkReshare[$column] === $itemSource) {
  2131. $result['filePath'] = $checkReshare['file_target'];
  2132. $result['itemSource'] = $checkReshare['item_source'];
  2133. $result['fileSource'] = $checkReshare['file_source'];
  2134. $result['suggestedItemTarget'] = $checkReshare['item_target'];
  2135. $result['suggestedFileTarget'] = $checkReshare['file_target'];
  2136. } else {
  2137. $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null;
  2138. $result['suggestedItemTarget'] = null;
  2139. $result['suggestedFileTarget'] = null;
  2140. $result['itemSource'] = $itemSource;
  2141. $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null;
  2142. }
  2143. }
  2144. } else {
  2145. $message = 'Sharing %s failed, because resharing is not allowed';
  2146. $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName));
  2147. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
  2148. throw new \Exception($message_t);
  2149. }
  2150. } else {
  2151. $result['parent'] = null;
  2152. $result['suggestedItemTarget'] = null;
  2153. $result['suggestedFileTarget'] = null;
  2154. $result['itemSource'] = $itemSource;
  2155. $result['expirationDate'] = $expirationDate;
  2156. if (!$backend->isValidSource($itemSource, $uidOwner)) {
  2157. $message = 'Sharing %s failed, because the sharing backend for '
  2158. .'%s could not find its source';
  2159. $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType));
  2160. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG);
  2161. throw new \Exception($message_t);
  2162. }
  2163. if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
  2164. $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner);
  2165. if ($itemType == 'file' || $itemType == 'folder') {
  2166. $result['fileSource'] = $itemSource;
  2167. } else {
  2168. $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']);
  2169. $result['fileSource'] = $meta['fileid'];
  2170. }
  2171. if ($result['fileSource'] == -1) {
  2172. $message = 'Sharing %s failed, because the file could not be found in the file cache';
  2173. $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource));
  2174. \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG);
  2175. throw new \Exception($message_t);
  2176. }
  2177. } else {
  2178. $result['filePath'] = null;
  2179. $result['fileSource'] = null;
  2180. }
  2181. }
  2182. return $result;
  2183. }
  2184. /**
  2185. *
  2186. * @param array $shareData
  2187. * @return mixed false in case of a failure or the id of the new share
  2188. */
  2189. private static function insertShare(array $shareData) {
  2190. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  2191. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  2192. .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
  2193. .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)');
  2194. $query->bindValue(1, $shareData['itemType']);
  2195. $query->bindValue(2, $shareData['itemSource']);
  2196. $query->bindValue(3, $shareData['itemTarget']);
  2197. $query->bindValue(4, $shareData['shareType']);
  2198. $query->bindValue(5, $shareData['shareWith']);
  2199. $query->bindValue(6, $shareData['uidOwner']);
  2200. $query->bindValue(7, $shareData['permissions']);
  2201. $query->bindValue(8, $shareData['shareTime']);
  2202. $query->bindValue(9, $shareData['fileSource']);
  2203. $query->bindValue(10, $shareData['fileTarget']);
  2204. $query->bindValue(11, $shareData['token']);
  2205. $query->bindValue(12, $shareData['parent']);
  2206. $query->bindValue(13, $shareData['expiration'], 'datetime');
  2207. $result = $query->execute();
  2208. $id = false;
  2209. if ($result) {
  2210. $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
  2211. }
  2212. return $id;
  2213. }
  2214. /**
  2215. * Delete all shares with type SHARE_TYPE_LINK
  2216. */
  2217. public static function removeAllLinkShares() {
  2218. // Delete any link shares
  2219. $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ?');
  2220. $result = $query->execute(array(self::SHARE_TYPE_LINK));
  2221. while ($item = $result->fetchRow()) {
  2222. Helper::delete($item['id']);
  2223. }
  2224. }
  2225. /**
  2226. * In case a password protected link is not yet authenticated this function will return false
  2227. *
  2228. * @param array $linkItem
  2229. * @return boolean
  2230. */
  2231. public static function checkPasswordProtectedShare(array $linkItem) {
  2232. if (!isset($linkItem['share_with'])) {
  2233. return true;
  2234. }
  2235. if (!isset($linkItem['share_type'])) {
  2236. return true;
  2237. }
  2238. if (!isset($linkItem['id'])) {
  2239. return true;
  2240. }
  2241. if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) {
  2242. return true;
  2243. }
  2244. if ( \OC::$server->getSession()->exists('public_link_authenticated')
  2245. && \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) {
  2246. return true;
  2247. }
  2248. return false;
  2249. }
  2250. /**
  2251. * construct select statement
  2252. * @param int $format
  2253. * @param boolean $fileDependent ist it a file/folder share or a generla share
  2254. * @param string $uidOwner
  2255. * @return string select statement
  2256. */
  2257. private static function createSelectStatement($format, $fileDependent, $uidOwner = null) {
  2258. $select = '*';
  2259. if ($format == self::FORMAT_STATUSES) {
  2260. if ($fileDependent) {
  2261. $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, '
  2262. . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, '
  2263. . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, '
  2264. . '`uid_initiator`';
  2265. } else {
  2266. $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`';
  2267. }
  2268. } else {
  2269. if (isset($uidOwner)) {
  2270. if ($fileDependent) {
  2271. $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,'
  2272. . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,'
  2273. . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, '
  2274. . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`';
  2275. } else {
  2276. $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,'
  2277. . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`';
  2278. }
  2279. } else {
  2280. if ($fileDependent) {
  2281. if ($format == \OC_Share_Backend_File::FORMAT_GET_FOLDER_CONTENTS || $format == \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT) {
  2282. $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, '
  2283. . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, '
  2284. . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, '
  2285. . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`';
  2286. } else {
  2287. $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,'
  2288. . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,'
  2289. . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,'
  2290. . '`stime`, `expiration`, `token`, `storage`, `mail_send`,'
  2291. . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`';
  2292. }
  2293. }
  2294. }
  2295. }
  2296. return $select;
  2297. }
  2298. /**
  2299. * transform db results
  2300. * @param array $row result
  2301. */
  2302. private static function transformDBResults(&$row) {
  2303. if (isset($row['id'])) {
  2304. $row['id'] = (int) $row['id'];
  2305. }
  2306. if (isset($row['share_type'])) {
  2307. $row['share_type'] = (int) $row['share_type'];
  2308. }
  2309. if (isset($row['parent'])) {
  2310. $row['parent'] = (int) $row['parent'];
  2311. }
  2312. if (isset($row['file_parent'])) {
  2313. $row['file_parent'] = (int) $row['file_parent'];
  2314. }
  2315. if (isset($row['file_source'])) {
  2316. $row['file_source'] = (int) $row['file_source'];
  2317. }
  2318. if (isset($row['permissions'])) {
  2319. $row['permissions'] = (int) $row['permissions'];
  2320. }
  2321. if (isset($row['storage'])) {
  2322. $row['storage'] = (int) $row['storage'];
  2323. }
  2324. if (isset($row['stime'])) {
  2325. $row['stime'] = (int) $row['stime'];
  2326. }
  2327. if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) {
  2328. // discard expiration date for non-link shares, which might have been
  2329. // set by ancient bugs
  2330. $row['expiration'] = null;
  2331. }
  2332. }
  2333. /**
  2334. * format result
  2335. * @param array $items result
  2336. * @param string $column is it a file share or a general share ('file_target' or 'item_target')
  2337. * @param \OCP\Share_Backend $backend sharing backend
  2338. * @param int $format
  2339. * @param array $parameters additional format parameters
  2340. * @return array format result
  2341. */
  2342. private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) {
  2343. if ($format === self::FORMAT_NONE) {
  2344. return $items;
  2345. } else if ($format === self::FORMAT_STATUSES) {
  2346. $statuses = array();
  2347. foreach ($items as $item) {
  2348. if ($item['share_type'] === self::SHARE_TYPE_LINK) {
  2349. if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) {
  2350. continue;
  2351. }
  2352. $statuses[$item[$column]]['link'] = true;
  2353. } else if (!isset($statuses[$item[$column]])) {
  2354. $statuses[$item[$column]]['link'] = false;
  2355. }
  2356. if (!empty($item['file_target'])) {
  2357. $statuses[$item[$column]]['path'] = $item['path'];
  2358. }
  2359. }
  2360. return $statuses;
  2361. } else {
  2362. return $backend->formatItems($items, $format, $parameters);
  2363. }
  2364. }
  2365. /**
  2366. * remove protocol from URL
  2367. *
  2368. * @param string $url
  2369. * @return string
  2370. */
  2371. public static function removeProtocolFromUrl($url) {
  2372. if (strpos($url, 'https://') === 0) {
  2373. return substr($url, strlen('https://'));
  2374. } else if (strpos($url, 'http://') === 0) {
  2375. return substr($url, strlen('http://'));
  2376. }
  2377. return $url;
  2378. }
  2379. /**
  2380. * try http post first with https and then with http as a fallback
  2381. *
  2382. * @param string $remoteDomain
  2383. * @param string $urlSuffix
  2384. * @param array $fields post parameters
  2385. * @return array
  2386. */
  2387. private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) {
  2388. $protocol = 'https://';
  2389. $result = [
  2390. 'success' => false,
  2391. 'result' => '',
  2392. ];
  2393. $try = 0;
  2394. $discoveryManager = new DiscoveryManager(
  2395. \OC::$server->getMemCacheFactory(),
  2396. \OC::$server->getHTTPClientService()
  2397. );
  2398. while ($result['success'] === false && $try < 2) {
  2399. $endpoint = $discoveryManager->getShareEndpoint($protocol . $remoteDomain);
  2400. $result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields);
  2401. $try++;
  2402. $protocol = 'http://';
  2403. }
  2404. return $result;
  2405. }
  2406. /**
  2407. * send server-to-server share to remote server
  2408. *
  2409. * @param string $token
  2410. * @param string $shareWith
  2411. * @param string $name
  2412. * @param int $remote_id
  2413. * @param string $owner
  2414. * @return bool
  2415. */
  2416. private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) {
  2417. list($user, $remote) = Helper::splitUserRemote($shareWith);
  2418. if ($user && $remote) {
  2419. $url = $remote;
  2420. $local = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
  2421. $fields = array(
  2422. 'shareWith' => $user,
  2423. 'token' => $token,
  2424. 'name' => $name,
  2425. 'remoteId' => $remote_id,
  2426. 'owner' => $owner,
  2427. 'remote' => $local,
  2428. );
  2429. $url = self::removeProtocolFromUrl($url);
  2430. $result = self::tryHttpPostToShareEndpoint($url, '', $fields);
  2431. $status = json_decode($result['result'], true);
  2432. if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) {
  2433. \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]);
  2434. return true;
  2435. }
  2436. }
  2437. return false;
  2438. }
  2439. /**
  2440. * send server-to-server unshare to remote server
  2441. *
  2442. * @param string $remote url
  2443. * @param int $id share id
  2444. * @param string $token
  2445. * @return bool
  2446. */
  2447. private static function sendRemoteUnshare($remote, $id, $token) {
  2448. $url = rtrim($remote, '/');
  2449. $fields = array('token' => $token, 'format' => 'json');
  2450. $url = self::removeProtocolFromUrl($url);
  2451. $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields);
  2452. $status = json_decode($result['result'], true);
  2453. return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200));
  2454. }
  2455. /**
  2456. * check if user can only share with group members
  2457. * @return bool
  2458. */
  2459. public static function shareWithGroupMembersOnly() {
  2460. $value = \OC::$server->getAppConfig()->getValue('core', 'shareapi_only_share_with_group_members', 'no');
  2461. return ($value === 'yes') ? true : false;
  2462. }
  2463. /**
  2464. * @return bool
  2465. */
  2466. public static function isDefaultExpireDateEnabled() {
  2467. $defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no');
  2468. return ($defaultExpireDateEnabled === "yes") ? true : false;
  2469. }
  2470. /**
  2471. * @return bool
  2472. */
  2473. public static function enforceDefaultExpireDate() {
  2474. $enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
  2475. return ($enforceDefaultExpireDate === "yes") ? true : false;
  2476. }
  2477. /**
  2478. * @return int
  2479. */
  2480. public static function getExpireInterval() {
  2481. return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
  2482. }
  2483. /**
  2484. * Checks whether the given path is reachable for the given owner
  2485. *
  2486. * @param string $path path relative to files
  2487. * @param string $ownerStorageId storage id of the owner
  2488. *
  2489. * @return boolean true if file is reachable, false otherwise
  2490. */
  2491. private static function isFileReachable($path, $ownerStorageId) {
  2492. // if outside the home storage, file is always considered reachable
  2493. if (!(substr($ownerStorageId, 0, 6) === 'home::' ||
  2494. substr($ownerStorageId, 0, 13) === 'object::user:'
  2495. )) {
  2496. return true;
  2497. }
  2498. // if inside the home storage, the file has to be under "/files/"
  2499. $path = ltrim($path, '/');
  2500. if (substr($path, 0, 6) === 'files/') {
  2501. return true;
  2502. }
  2503. return false;
  2504. }
  2505. /**
  2506. * @param IConfig $config
  2507. * @return bool
  2508. */
  2509. public static function enforcePassword(IConfig $config) {
  2510. $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no');
  2511. return ($enforcePassword === "yes") ? true : false;
  2512. }
  2513. /**
  2514. * Get all share entries, including non-unique group items
  2515. *
  2516. * @param string $owner
  2517. * @return array
  2518. */
  2519. public static function getAllSharesForOwner($owner) {
  2520. $query = 'SELECT * FROM `*PREFIX*share` WHERE `uid_owner` = ?';
  2521. $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$owner]);
  2522. return $result->fetchAll();
  2523. }
  2524. /**
  2525. * Get all share entries, including non-unique group items for a file
  2526. *
  2527. * @param int $id
  2528. * @return array
  2529. */
  2530. public static function getAllSharesForFileId($id) {
  2531. $query = 'SELECT * FROM `*PREFIX*share` WHERE `file_source` = ?';
  2532. $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$id]);
  2533. return $result->fetchAll();
  2534. }
  2535. /**
  2536. * @param string $password
  2537. * @throws \Exception
  2538. */
  2539. private static function verifyPassword($password) {
  2540. $accepted = true;
  2541. $message = '';
  2542. \OCP\Util::emitHook('\OC\Share', 'verifyPassword', [
  2543. 'password' => $password,
  2544. 'accepted' => &$accepted,
  2545. 'message' => &$message
  2546. ]);
  2547. if (!$accepted) {
  2548. throw new \Exception($message);
  2549. }
  2550. }
  2551. }