|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files_Sharing\Repair; |
| 10 | + |
| 11 | +use OC\Files\SetupManager; |
| 12 | +use OCA\Files_Sharing\ShareTargetValidator; |
| 13 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 14 | +use OCP\Files\Mount\IMountManager; |
| 15 | +use OCP\IDBConnection; |
| 16 | +use OCP\IUserManager; |
| 17 | +use OCP\Migration\IOutput; |
| 18 | +use OCP\Migration\IRepairStep; |
| 19 | +use OCP\Share\IManager; |
| 20 | +use OCP\Share\IProviderFactory; |
| 21 | +use OCP\Share\IShare; |
| 22 | + |
| 23 | +class CleanupShareTarget implements IRepairStep { |
| 24 | + /** we only care about shares with a user target, |
| 25 | + * since the underling group/deck/talk share doesn't get moved |
| 26 | + */ |
| 27 | + private const USER_SHARE_TYPES = [ |
| 28 | + IShare::TYPE_USER, |
| 29 | + IShare::TYPE_USERGROUP, |
| 30 | + IShare::TYPE_DECK_USER, |
| 31 | + 11 // TYPE_USERROOM |
| 32 | + ]; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly IDBConnection $connection, |
| 36 | + private readonly IManager $shareManager, |
| 37 | + private readonly IProviderFactory $shareProviderFactory, |
| 38 | + private readonly ShareTargetValidator $shareTargetValidator, |
| 39 | + private readonly IUserManager $userManager, |
| 40 | + private readonly SetupManager $setupManager, |
| 41 | + private readonly IMountManager $mountManager, |
| 42 | + ) { |
| 43 | + } |
| 44 | + |
| 45 | + #[\Override] |
| 46 | + public function getName() { |
| 47 | + return 'Cleanup share names with false conflicts'; |
| 48 | + } |
| 49 | + |
| 50 | + #[\Override] |
| 51 | + public function run(IOutput $output) { |
| 52 | + $count = $this->countProblemShares(); |
| 53 | + if ($count === 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + $output->startProgress($count); |
| 57 | + |
| 58 | + $lastUser = ''; |
| 59 | + $userMounts = []; |
| 60 | + |
| 61 | + foreach ($this->getProblemShares() as $shareInfo) { |
| 62 | + $recipient = $this->userManager->getExistingUser($shareInfo['share_with']); |
| 63 | + $share = $this->shareProviderFactory |
| 64 | + ->getProviderForType((int)$shareInfo['share_type']) |
| 65 | + ->getShareById($shareInfo['id'], $recipient->getUID()); |
| 66 | + |
| 67 | + // since we ordered the share by user, we can reuse the last data until we get to the next user |
| 68 | + if ($lastUser !== $recipient->getUID()) { |
| 69 | + $lastUser = $recipient->getUID(); |
| 70 | + |
| 71 | + $this->setupManager->tearDown(); |
| 72 | + $this->setupManager->setupForUser($recipient); |
| 73 | + $userMounts = $this->mountManager->getAll(); |
| 74 | + } |
| 75 | + |
| 76 | + $oldTarget = $share->getTarget(); |
| 77 | + $newTarget = $this->cleanTarget($oldTarget); |
| 78 | + $share->setTarget($newTarget); |
| 79 | + $this->shareManager->moveShare($share, $recipient->getUID()); |
| 80 | + |
| 81 | + $this->shareTargetValidator->verifyMountPoint( |
| 82 | + $recipient, |
| 83 | + $share, |
| 84 | + $userMounts, |
| 85 | + [$share], |
| 86 | + ); |
| 87 | + |
| 88 | + $oldMountPoint = "/{$recipient->getUID()}/files$oldTarget/"; |
| 89 | + $newMountPoint = "/{$recipient->getUID()}/files$newTarget/"; |
| 90 | + $userMounts[$newMountPoint] = $userMounts[$oldMountPoint]; |
| 91 | + unset($userMounts[$oldMountPoint]); |
| 92 | + |
| 93 | + $output->advance(); |
| 94 | + } |
| 95 | + $output->finishProgress(); |
| 96 | + $output->info("Fixed $count shares"); |
| 97 | + } |
| 98 | + |
| 99 | + private function countProblemShares(): int { |
| 100 | + $query = $this->connection->getQueryBuilder(); |
| 101 | + $query->select($query->func()->count('id')) |
| 102 | + ->from('share') |
| 103 | + ->where($query->expr()->like('file_target', $query->createNamedParameter('% (_) (_)%'))) |
| 104 | + ->andWhere($query->expr()->in('share_type', $query->createNamedParameter(self::USER_SHARE_TYPES, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)); |
| 105 | + return (int)$query->executeQuery()->fetchOne(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return \Traversable<array{id: string, share_type: string, share_with: string}> |
| 110 | + */ |
| 111 | + private function getProblemShares(): \Traversable { |
| 112 | + $query = $this->connection->getQueryBuilder(); |
| 113 | + $query->select('id', 'share_type', 'share_with') |
| 114 | + ->from('share') |
| 115 | + ->where($query->expr()->like('file_target', $query->createNamedParameter('% (_) (_)%'))) |
| 116 | + ->andWhere($query->expr()->in('share_type', $query->createNamedParameter(self::USER_SHARE_TYPES, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
| 117 | + ->orderBy('share_with') |
| 118 | + ->addOrderBy('id'); |
| 119 | + $result = $query->executeQuery(); |
| 120 | + /** @var \Traversable<array{id: string, share_type: string, share_with: string}> $rows */ |
| 121 | + $rows = $result->iterateAssociative(); |
| 122 | + return $rows; |
| 123 | + } |
| 124 | + |
| 125 | + private function cleanTarget(string $target): string { |
| 126 | + return preg_replace('/( \([2-9]\)){2,}/', '', $target); |
| 127 | + } |
| 128 | +} |
0 commit comments