2013-10-28 20:22:06 +01:00
|
|
|
<?php
|
2015-03-26 11:44:34 +01:00
|
|
|
/**
|
2024-06-06 19:48:28 +02: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 11:44:34 +01:00
|
|
|
*/
|
2013-10-28 20:22:06 +01:00
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
2015-12-10 14:14:54 +01:00
|
|
|
use OC\Files\Filesystem;
|
2016-02-18 09:57:29 +01:00
|
|
|
use OC\Files\View;
|
2021-07-25 17:57:11 +02:00
|
|
|
use OCA\Files_Sharing\AppInfo\Application;
|
2025-02-03 15:34:01 +01:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\Server;
|
2024-10-10 12:40:31 +02:00
|
|
|
use OCP\Util;
|
2015-06-15 14:10:10 +02:00
|
|
|
|
2013-10-28 20:22:06 +01:00
|
|
|
class Helper {
|
2014-06-25 15:20:52 +02:00
|
|
|
public static function registerHooks() {
|
2024-10-10 12:40:31 +02: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 15:20:52 +02:00
|
|
|
|
2024-10-10 12:40:31 +02:00
|
|
|
Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
|
2014-06-25 15:20:52 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 16:56:09 +02:00
|
|
|
/**
|
|
|
|
* check if file name already exists and generate unique target
|
|
|
|
*
|
|
|
|
* @param string $path
|
2016-02-18 09:57:29 +01:00
|
|
|
* @param View $view
|
2014-04-30 16:56:09 +02:00
|
|
|
* @return string $path
|
|
|
|
*/
|
2024-09-19 23:53:43 +02:00
|
|
|
public static function generateUniqueTarget($path, $view) {
|
2014-04-30 16:56:09 +02:00
|
|
|
$pathinfo = pathinfo($path);
|
2018-01-26 23:46:40 +01:00
|
|
|
$ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
|
2014-04-30 16:56:09 +02:00
|
|
|
$name = $pathinfo['filename'];
|
|
|
|
$dir = $pathinfo['dirname'];
|
|
|
|
$i = 2;
|
2024-09-19 23:53:43 +02:00
|
|
|
while ($view->file_exists($path)) {
|
2016-02-18 09:57:29 +01:00
|
|
|
$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
|
2014-04-30 16:56:09 +02:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
2014-06-12 19:49:52 +02:00
|
|
|
|
2014-08-13 12:55:14 +02:00
|
|
|
/**
|
|
|
|
* get default share folder
|
|
|
|
*
|
2024-10-18 12:04:22 +02:00
|
|
|
* @param View|null $view
|
2021-07-25 17:57:11 +02:00
|
|
|
* @param string|null $userId
|
2014-08-13 12:55:14 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-28 16:13:19 +01:00
|
|
|
public static function getShareFolder(?View $view = null, ?string $userId = null): string {
|
2016-06-17 11:11:59 +02:00
|
|
|
if ($view === null) {
|
|
|
|
$view = Filesystem::getView();
|
|
|
|
}
|
2021-07-25 17:57:11 +02:00
|
|
|
|
2025-02-03 15:34:01 +01:00
|
|
|
$config = Server::get(IConfig::class);
|
2021-07-25 17:57:11 +02: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 09:57:29 +01:00
|
|
|
$shareFolder = Filesystem::normalizePath($shareFolder);
|
2015-03-26 21:56:44 +01:00
|
|
|
|
2021-07-25 17:57:11 +02:00
|
|
|
// Init path if folder doesn't exists
|
2016-06-17 11:11:59 +02:00
|
|
|
if (!$view->file_exists($shareFolder)) {
|
2015-03-26 21:56:44 +01:00
|
|
|
$dir = '';
|
|
|
|
$subdirs = explode('/', $shareFolder);
|
|
|
|
foreach ($subdirs as $subdir) {
|
|
|
|
$dir = $dir . '/' . $subdir;
|
2016-06-17 11:11:59 +02:00
|
|
|
if (!$view->is_dir($dir)) {
|
|
|
|
$view->mkdir($dir);
|
2015-03-26 21:56:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shareFolder;
|
2014-08-13 12:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set default share folder
|
|
|
|
*
|
|
|
|
* @param string $shareFolder
|
|
|
|
*/
|
|
|
|
public static function setShareFolder($shareFolder) {
|
2025-02-03 15:34:01 +01:00
|
|
|
Server::get(IConfig::class)->setSystemValue('share_folder', $shareFolder);
|
2014-08-13 12:55:14 +02:00
|
|
|
}
|
2013-10-28 20:22:06 +01:00
|
|
|
}
|