1
0

Helper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Sharing;
  29. use OC\Files\Filesystem;
  30. use OC\Files\View;
  31. use OCA\Files_Sharing\AppInfo\Application;
  32. class Helper {
  33. public static function registerHooks() {
  34. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
  35. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
  36. \OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
  37. }
  38. /**
  39. * check if file name already exists and generate unique target
  40. *
  41. * @param string $path
  42. * @param array $excludeList
  43. * @param View $view
  44. * @return string $path
  45. */
  46. public static function generateUniqueTarget($path, $excludeList, $view) {
  47. $pathinfo = pathinfo($path);
  48. $ext = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
  49. $name = $pathinfo['filename'];
  50. $dir = $pathinfo['dirname'];
  51. $i = 2;
  52. while ($view->file_exists($path) || in_array($path, $excludeList)) {
  53. $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
  54. $i++;
  55. }
  56. return $path;
  57. }
  58. /**
  59. * get default share folder
  60. *
  61. * @param \OC\Files\View|null $view
  62. * @param string|null $userId
  63. * @return string
  64. */
  65. public static function getShareFolder(View $view = null, string $userId = null): string {
  66. if ($view === null) {
  67. $view = Filesystem::getView();
  68. }
  69. $config = \OC::$server->getConfig();
  70. $systemDefault = $config->getSystemValue('share_folder', '/');
  71. $allowCustomShareFolder = $config->getSystemValueBool('sharing.allow_custom_share_folder', true);
  72. // Init custom shareFolder
  73. $shareFolder = $systemDefault;
  74. if ($userId !== null && $allowCustomShareFolder) {
  75. $shareFolder = $config->getUserValue($userId, Application::APP_ID, 'share_folder', $systemDefault);
  76. }
  77. // Verify and sanitize path
  78. $shareFolder = Filesystem::normalizePath($shareFolder);
  79. // Init path if folder doesn't exists
  80. if (!$view->file_exists($shareFolder)) {
  81. $dir = '';
  82. $subdirs = explode('/', $shareFolder);
  83. foreach ($subdirs as $subdir) {
  84. $dir = $dir . '/' . $subdir;
  85. if (!$view->is_dir($dir)) {
  86. $view->mkdir($dir);
  87. }
  88. }
  89. }
  90. return $shareFolder;
  91. }
  92. /**
  93. * set default share folder
  94. *
  95. * @param string $shareFolder
  96. */
  97. public static function setShareFolder($shareFolder) {
  98. \OC::$server->getConfig()->setSystemValue('share_folder', $shareFolder);
  99. }
  100. }