Folder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Sharing\ShareBackend;
  30. class Folder extends File implements \OCP\Share_Backend_Collection {
  31. /**
  32. * get shared parents
  33. *
  34. * @param int $itemSource item source ID
  35. * @param string $shareWith with whom should the item be shared
  36. * @param string $owner owner of the item
  37. * @return array with shares
  38. */
  39. public function getParents($itemSource, $shareWith = null, $owner = null) {
  40. $result = [];
  41. $parent = $this->getParentId($itemSource);
  42. $userManager = \OC::$server->getUserManager();
  43. while ($parent) {
  44. $shares = \OCP\Share::getItemSharedWithUser('folder', $parent, $shareWith, $owner);
  45. if ($shares) {
  46. foreach ($shares as $share) {
  47. $name = basename($share['path']);
  48. $share['collection']['path'] = $name;
  49. $share['collection']['item_type'] = 'folder';
  50. $share['file_path'] = $name;
  51. $ownerUser = $userManager->get($share['uid_owner']);
  52. $displayNameOwner = $ownerUser === null ? $share['uid_owner'] : $ownerUser->getDisplayName();
  53. $shareWithUser = $userManager->get($share['share_with']);
  54. $displayNameShareWith = $shareWithUser === null ? $share['share_with'] : $shareWithUser->getDisplayName();
  55. $share['displayname_owner'] = $displayNameOwner ? $displayNameOwner : $share['uid_owner'];
  56. $share['share_with_displayname'] = $displayNameShareWith ? $displayNameShareWith : $share['uid_owner'];
  57. $result[] = $share;
  58. }
  59. }
  60. $parent = $this->getParentId($parent);
  61. }
  62. return $result;
  63. }
  64. /**
  65. * get file cache ID of parent
  66. *
  67. * @param int $child file cache ID of child
  68. * @return mixed parent ID or null
  69. */
  70. private function getParentId($child) {
  71. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  72. $qb->select('parent')
  73. ->from('filecache')
  74. ->where(
  75. $qb->expr()->eq('fileid', $qb->createNamedParameter($child))
  76. );
  77. $result = $qb->execute();
  78. $row = $result->fetch();
  79. $result->closeCursor();
  80. return $row ? $row['parent'] : null;
  81. }
  82. public function getChildren($itemSource) {
  83. $children = [];
  84. $parents = [$itemSource];
  85. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  86. $qb->select('id')
  87. ->from('mimetypes')
  88. ->where(
  89. $qb->expr()->eq('mimetype', $qb->createNamedParameter('httpd/unix-directory'))
  90. );
  91. $result = $qb->execute();
  92. $row = $result->fetch();
  93. $result->closeCursor();
  94. if ($row = $result->fetchRow()) {
  95. $mimetype = (int) $row['id'];
  96. } else {
  97. $mimetype = -1;
  98. }
  99. while (!empty($parents)) {
  100. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  101. $parents = array_map(function ($parent) use ($qb) {
  102. return $qb->createNamedParameter($parent);
  103. }, $parents);
  104. $qb->select('`fileid', 'name', '`mimetype')
  105. ->from('filecache')
  106. ->where(
  107. $qb->expr()->in('parent', $parents)
  108. );
  109. $result = $qb->execute();
  110. $parents = [];
  111. while ($file = $result->fetch()) {
  112. $children[] = ['source' => $file['fileid'], 'file_path' => $file['name']];
  113. // If a child folder is found look inside it
  114. if ((int) $file['mimetype'] === $mimetype) {
  115. $parents[] = $file['fileid'];
  116. }
  117. }
  118. $result->closeCursor();
  119. }
  120. return $children;
  121. }
  122. }