Helper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OC\Files\Filesystem;
  9. use OC\Files\View;
  10. use OCA\Files_Sharing\AppInfo\Application;
  11. class Helper {
  12. public static function registerHooks() {
  13. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
  14. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
  15. \OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
  16. }
  17. /**
  18. * check if file name already exists and generate unique target
  19. *
  20. * @param string $path
  21. * @param array $excludeList
  22. * @param View $view
  23. * @return string $path
  24. */
  25. public static function generateUniqueTarget($path, $excludeList, $view) {
  26. $pathinfo = pathinfo($path);
  27. $ext = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
  28. $name = $pathinfo['filename'];
  29. $dir = $pathinfo['dirname'];
  30. $i = 2;
  31. while ($view->file_exists($path) || in_array($path, $excludeList)) {
  32. $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
  33. $i++;
  34. }
  35. return $path;
  36. }
  37. /**
  38. * get default share folder
  39. *
  40. * @param \OC\Files\View|null $view
  41. * @param string|null $userId
  42. * @return string
  43. */
  44. public static function getShareFolder(?View $view = null, ?string $userId = null): string {
  45. if ($view === null) {
  46. $view = Filesystem::getView();
  47. }
  48. $config = \OC::$server->getConfig();
  49. $systemDefault = $config->getSystemValue('share_folder', '/');
  50. $allowCustomShareFolder = $config->getSystemValueBool('sharing.allow_custom_share_folder', true);
  51. // Init custom shareFolder
  52. $shareFolder = $systemDefault;
  53. if ($userId !== null && $allowCustomShareFolder) {
  54. $shareFolder = $config->getUserValue($userId, Application::APP_ID, 'share_folder', $systemDefault);
  55. }
  56. // Verify and sanitize path
  57. $shareFolder = Filesystem::normalizePath($shareFolder);
  58. // Init path if folder doesn't exists
  59. if (!$view->file_exists($shareFolder)) {
  60. $dir = '';
  61. $subdirs = explode('/', $shareFolder);
  62. foreach ($subdirs as $subdir) {
  63. $dir = $dir . '/' . $subdir;
  64. if (!$view->is_dir($dir)) {
  65. $view->mkdir($dir);
  66. }
  67. }
  68. }
  69. return $shareFolder;
  70. }
  71. /**
  72. * set default share folder
  73. *
  74. * @param string $shareFolder
  75. */
  76. public static function setShareFolder($shareFolder) {
  77. \OC::$server->getConfig()->setSystemValue('share_folder', $shareFolder);
  78. }
  79. }