2013-10-28 15:22:06 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-03-26 06:44:34 -04:00
|
|
|
/**
|
2024-06-06 13:48:28 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-26 06:44:34 -04:00
|
|
|
*/
|
2013-10-28 15:22:06 -04:00
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
|
2015-12-10 08:14:54 -05:00
|
|
|
use OC\Files\Filesystem;
|
2016-02-18 03:57:29 -05:00
|
|
|
use OC\Files\View;
|
2021-07-25 11:57:11 -04:00
|
|
|
use OCA\Files_Sharing\AppInfo\Application;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
use OCP\Server;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCP\Util;
|
2015-06-15 08:10:10 -04:00
|
|
|
|
2013-10-28 15:22:06 -04:00
|
|
|
class Helper {
|
2014-06-25 09:20:52 -04:00
|
|
|
public static function registerHooks() {
|
2024-10-10 06:40:31 -04:00
|
|
|
Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
|
|
|
|
|
Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
|
2014-06-25 09:20:52 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
|
2014-06-25 09:20:52 -04:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 10:56:09 -04:00
|
|
|
/**
|
|
|
|
|
* check if file name already exists and generate unique target
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
2016-02-18 03:57:29 -05:00
|
|
|
* @param View $view
|
2014-04-30 10:56:09 -04:00
|
|
|
* @return string $path
|
|
|
|
|
*/
|
2024-09-19 17:53:43 -04:00
|
|
|
public static function generateUniqueTarget($path, $view) {
|
2014-04-30 10:56:09 -04:00
|
|
|
$pathinfo = pathinfo($path);
|
2018-01-26 17:46:40 -05:00
|
|
|
$ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
|
2014-04-30 10:56:09 -04:00
|
|
|
$name = $pathinfo['filename'];
|
|
|
|
|
$dir = $pathinfo['dirname'];
|
|
|
|
|
$i = 2;
|
2024-09-19 17:53:43 -04:00
|
|
|
while ($view->file_exists($path)) {
|
2016-02-18 03:57:29 -05:00
|
|
|
$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
|
2014-04-30 10:56:09 -04:00
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2014-06-12 13:49:52 -04:00
|
|
|
|
2014-08-13 06:55:14 -04:00
|
|
|
/**
|
|
|
|
|
* get default share folder
|
|
|
|
|
*
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param View|null $view
|
2021-07-25 11:57:11 -04:00
|
|
|
* @param string|null $userId
|
2014-08-13 06:55:14 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
public static function getShareFolder(?View $view = null, ?string $userId = null): string {
|
2016-06-17 05:11:59 -04:00
|
|
|
if ($view === null) {
|
|
|
|
|
$view = Filesystem::getView();
|
|
|
|
|
}
|
2021-07-25 11:57:11 -04:00
|
|
|
|
2025-02-03 09:34:01 -05:00
|
|
|
$config = Server::get(IConfig::class);
|
2021-07-25 11:57:11 -04:00
|
|
|
$systemDefault = $config->getSystemValue('share_folder', '/');
|
|
|
|
|
$allowCustomShareFolder = $config->getSystemValueBool('sharing.allow_custom_share_folder', true);
|
|
|
|
|
|
|
|
|
|
// Init custom shareFolder
|
|
|
|
|
$shareFolder = $systemDefault;
|
|
|
|
|
if ($userId !== null && $allowCustomShareFolder) {
|
|
|
|
|
$shareFolder = $config->getUserValue($userId, Application::APP_ID, 'share_folder', $systemDefault);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify and sanitize path
|
2016-02-18 03:57:29 -05:00
|
|
|
$shareFolder = Filesystem::normalizePath($shareFolder);
|
2015-03-26 16:56:44 -04:00
|
|
|
|
2021-07-25 11:57:11 -04:00
|
|
|
// Init path if folder doesn't exists
|
2016-06-17 05:11:59 -04:00
|
|
|
if (!$view->file_exists($shareFolder)) {
|
2015-03-26 16:56:44 -04:00
|
|
|
$dir = '';
|
|
|
|
|
$subdirs = explode('/', $shareFolder);
|
|
|
|
|
foreach ($subdirs as $subdir) {
|
|
|
|
|
$dir = $dir . '/' . $subdir;
|
2016-06-17 05:11:59 -04:00
|
|
|
if (!$view->is_dir($dir)) {
|
|
|
|
|
$view->mkdir($dir);
|
2015-03-26 16:56:44 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $shareFolder;
|
2014-08-13 06:55:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set default share folder
|
|
|
|
|
*
|
|
|
|
|
* @param string $shareFolder
|
|
|
|
|
*/
|
|
|
|
|
public static function setShareFolder($shareFolder) {
|
2025-02-03 09:34:01 -05:00
|
|
|
Server::get(IConfig::class)->setSystemValue('share_folder', $shareFolder);
|
2014-08-13 06:55:14 -04:00
|
|
|
}
|
2013-10-28 15:22:06 -04:00
|
|
|
}
|