Helper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  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;
  30. use OC\Files\Filesystem;
  31. use OC\Files\View;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Share\Exceptions\ShareNotFound;
  34. use OCP\User;
  35. class Helper {
  36. public static function registerHooks() {
  37. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
  38. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
  39. \OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
  40. }
  41. /**
  42. * check if file name already exists and generate unique target
  43. *
  44. * @param string $path
  45. * @param array $excludeList
  46. * @param View $view
  47. * @return string $path
  48. */
  49. public static function generateUniqueTarget($path, $excludeList, $view) {
  50. $pathinfo = pathinfo($path);
  51. $ext = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
  52. $name = $pathinfo['filename'];
  53. $dir = $pathinfo['dirname'];
  54. $i = 2;
  55. while ($view->file_exists($path) || in_array($path, $excludeList)) {
  56. $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
  57. $i++;
  58. }
  59. return $path;
  60. }
  61. /**
  62. * get default share folder
  63. *
  64. * @param \OC\Files\View
  65. * @return string
  66. */
  67. public static function getShareFolder($view = null) {
  68. if ($view === null) {
  69. $view = Filesystem::getView();
  70. }
  71. $shareFolder = \OC::$server->getConfig()->getSystemValue('share_folder', '/');
  72. $shareFolder = Filesystem::normalizePath($shareFolder);
  73. if (!$view->file_exists($shareFolder)) {
  74. $dir = '';
  75. $subdirs = explode('/', $shareFolder);
  76. foreach ($subdirs as $subdir) {
  77. $dir = $dir . '/' . $subdir;
  78. if (!$view->is_dir($dir)) {
  79. $view->mkdir($dir);
  80. }
  81. }
  82. }
  83. return $shareFolder;
  84. }
  85. /**
  86. * set default share folder
  87. *
  88. * @param string $shareFolder
  89. */
  90. public static function setShareFolder($shareFolder) {
  91. \OC::$server->getConfig()->setSystemValue('share_folder', $shareFolder);
  92. }
  93. }