|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +/** |
| 4 | + * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> |
| 5 | + * |
| 6 | + * @author Roeland Jago Douma <roeland@famdouma.nl> |
| 7 | + * |
| 8 | + * @license GNU AGPL version 3 or any later version |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as |
| 12 | + * published by the Free Software Foundation, either version 3 of the |
| 13 | + * License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +namespace OC\Repair; |
| 26 | + |
| 27 | +use Doctrine\DBAL\Driver\Statement; |
| 28 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 29 | +use OCP\IConfig; |
| 30 | +use OCP\IDBConnection; |
| 31 | +use OCP\IGroupManager; |
| 32 | +use OCP\Migration\IOutput; |
| 33 | +use OCP\Migration\IRepairStep; |
| 34 | +use OCP\Notification\IManager; |
| 35 | + |
| 36 | +class RemoveLinkShares implements IRepairStep { |
| 37 | + /** @var IDBConnection */ |
| 38 | + private $connection; |
| 39 | + /** @var IConfig */ |
| 40 | + private $config; |
| 41 | + /** @var string[] */ |
| 42 | + private $userToNotify = []; |
| 43 | + /** @var IGroupManager */ |
| 44 | + private $groupManager; |
| 45 | + /** @var IManager */ |
| 46 | + private $notificationManager; |
| 47 | + /** @var ITimeFactory */ |
| 48 | + private $timeFactory; |
| 49 | + |
| 50 | + public function __construct(IDBConnection $connection, |
| 51 | + IConfig $config, |
| 52 | + IGroupManager $groupManager, |
| 53 | + IManager $notificationManager, |
| 54 | + ITimeFactory $timeFactory) { |
| 55 | + $this->connection = $connection; |
| 56 | + $this->config = $config; |
| 57 | + $this->groupManager = $groupManager; |
| 58 | + $this->notificationManager = $notificationManager; |
| 59 | + $this->timeFactory = $timeFactory; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function getName(): string { |
| 64 | + return 'Remove potentially over exposing share links'; |
| 65 | + } |
| 66 | + |
| 67 | + private function shouldRun(): bool { |
| 68 | + $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
| 69 | + |
| 70 | + if (version_compare($versionFromBeforeUpdate, '14.0.11', '<')) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + if (version_compare($versionFromBeforeUpdate, '15.0.8', '<')) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + if (version_compare($versionFromBeforeUpdate, '16.0.0', '<=')) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Delete the share |
| 85 | + * |
| 86 | + * @param int $id |
| 87 | + */ |
| 88 | + private function deleteShare(int $id) { |
| 89 | + $qb = $this->connection->getQueryBuilder(); |
| 90 | + $qb->delete('share') |
| 91 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
| 92 | + $qb->execute(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the total of affected shares |
| 97 | + * |
| 98 | + * @return int |
| 99 | + */ |
| 100 | + private function getTotal(): int { |
| 101 | + $sql = 'SELECT COUNT(*) AS `total` |
| 102 | + FROM `*PREFIX*share` |
| 103 | + WHERE `id` IN ( |
| 104 | + SELECT `s1`.`id` |
| 105 | + FROM ( |
| 106 | + SELECT * |
| 107 | + FROM `*PREFIX*share` |
| 108 | + WHERE `parent` IS NOT NULL |
| 109 | + AND `share_type` = 3 |
| 110 | + ) AS s1 |
| 111 | + JOIN `*PREFIX*share` AS s2 |
| 112 | + ON `s1`.`parent` = `s2`.`id` |
| 113 | + WHERE (`s2`.`share_type` = 1 OR `s2`.`share_type` = 2) |
| 114 | + AND `s1`.`item_source` = `s2`.`item_source` |
| 115 | + )'; |
| 116 | + $cursor = $this->connection->executeQuery($sql); |
| 117 | + $data = $cursor->fetchAll(); |
| 118 | + $total = (int)$data[0]['total']; |
| 119 | + $cursor->closeCursor(); |
| 120 | + |
| 121 | + return $total; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Get the cursor to fetch all the shares |
| 126 | + * |
| 127 | + * @return \Doctrine\DBAL\Driver\Statement |
| 128 | + */ |
| 129 | + private function getShares(): Statement { |
| 130 | + $sql = 'SELECT `s1`.`id`, `s1`.`uid_owner`, `s1`.`uid_initiator` |
| 131 | + FROM ( |
| 132 | + SELECT * |
| 133 | + FROM `*PREFIX*share` |
| 134 | + WHERE `parent` IS NOT NULL |
| 135 | + AND `share_type` = 3 |
| 136 | + ) AS s1 |
| 137 | + JOIN `*PREFIX*share` AS s2 |
| 138 | + ON `s1`.`parent` = `s2`.`id` |
| 139 | + WHERE (`s2`.`share_type` = 1 OR `s2`.`share_type` = 2) |
| 140 | + AND `s1`.`item_source` = `s2`.`item_source`'; |
| 141 | + $cursor = $this->connection->executeQuery($sql); |
| 142 | + return $cursor; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Process a single share |
| 147 | + * |
| 148 | + * @param array $data |
| 149 | + */ |
| 150 | + private function processShare(array $data) { |
| 151 | + $id = $data['id']; |
| 152 | + |
| 153 | + $this->addToNotify($data['uid_owner']); |
| 154 | + $this->addToNotify($data['uid_initiator']); |
| 155 | + |
| 156 | + $this->deleteShare((int)$id); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Update list of users to notify |
| 161 | + * |
| 162 | + * @param string $uid |
| 163 | + */ |
| 164 | + private function addToNotify(string $uid) { |
| 165 | + if (!isset($this->userToNotify[$uid])) { |
| 166 | + $this->userToNotify[$uid] = true; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Send all notifications |
| 172 | + */ |
| 173 | + private function sendNotification() { |
| 174 | + $time = $this->timeFactory->getDateTime(); |
| 175 | + |
| 176 | + $notification = $this->notificationManager->createNotification(); |
| 177 | + $notification->setApp('core') |
| 178 | + ->setDateTime($time) |
| 179 | + ->setObject('repair', 'exposing_links') |
| 180 | + ->setSubject('repair_exposing_links', []); |
| 181 | + |
| 182 | + $users = array_keys($this->userToNotify); |
| 183 | + foreach ($users as $user) { |
| 184 | + $notification->setUser($user); |
| 185 | + $this->notificationManager->notify($notification); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private function repair(IOutput $output) { |
| 190 | + $total = $this->getTotal(); |
| 191 | + $output->startProgress($total); |
| 192 | + |
| 193 | + $shareCursor = $this->getShares(); |
| 194 | + while($data = $shareCursor->fetch()) { |
| 195 | + $this->processShare($data); |
| 196 | + $output->advance(); |
| 197 | + } |
| 198 | + $output->finishProgress(); |
| 199 | + $shareCursor->closeCursor(); |
| 200 | + |
| 201 | + // Notifiy all admins |
| 202 | + $adminGroup = $this->groupManager->get('admin'); |
| 203 | + $adminUsers = $adminGroup->getUsers(); |
| 204 | + foreach ($adminUsers as $user) { |
| 205 | + $this->addToNotify($user->getUID()); |
| 206 | + } |
| 207 | + |
| 208 | + $output->info('Sending notifications to admins and affected users'); |
| 209 | + $this->sendNotification(); |
| 210 | + } |
| 211 | + |
| 212 | + public function run(IOutput $output) { |
| 213 | + if ($this->shouldRun()) { |
| 214 | + $output->info('Removing potentially over exposing link shares'); |
| 215 | + $this->repair($output); |
| 216 | + $output->info('Removed potentially over exposing link shares'); |
| 217 | + } else { |
| 218 | + $output->info('No need to remove link shares.'); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments