Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RepairInvalidShares.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Repair;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. /**
  28. * Repairs shares with invalid data
  29. */
  30. class RepairInvalidShares implements IRepairStep {
  31. const CHUNK_SIZE = 200;
  32. /** @var \OCP\IConfig */
  33. protected $config;
  34. /** @var \OCP\IDBConnection */
  35. protected $connection;
  36. /**
  37. * @param \OCP\IConfig $config
  38. * @param \OCP\IDBConnection $connection
  39. */
  40. public function __construct($config, $connection) {
  41. $this->connection = $connection;
  42. $this->config = $config;
  43. }
  44. public function getName() {
  45. return 'Repair invalid shares';
  46. }
  47. /**
  48. * Past bugs would make it possible to set an expiration date on user shares even
  49. * though it is not supported. This functions removes the expiration date from such entries.
  50. */
  51. private function removeExpirationDateFromNonLinkShares(IOutput $out) {
  52. $builder = $this->connection->getQueryBuilder();
  53. $builder
  54. ->update('share')
  55. ->set('expiration', 'null')
  56. ->where($builder->expr()->isNotNull('expiration'))
  57. ->andWhere($builder->expr()->neq('share_type', $builder->expr()->literal(\OC\Share\Constants::SHARE_TYPE_LINK)));
  58. $updatedEntries = $builder->execute();
  59. if ($updatedEntries > 0) {
  60. $out->info('Removed invalid expiration date from ' . $updatedEntries . ' shares');
  61. }
  62. }
  63. /**
  64. * In the past link shares with public upload enabled were missing the delete permission.
  65. */
  66. private function addShareLinkDeletePermission(IOutput $out) {
  67. $oldPerms = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE;
  68. $newPerms = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  69. $builder = $this->connection->getQueryBuilder();
  70. $builder
  71. ->update('share')
  72. ->set('permissions', $builder->expr()->literal($newPerms))
  73. ->where($builder->expr()->eq('share_type', $builder->expr()->literal(\OC\Share\Constants::SHARE_TYPE_LINK)))
  74. ->andWhere($builder->expr()->eq('permissions', $builder->expr()->literal($oldPerms)));
  75. $updatedEntries = $builder->execute();
  76. if ($updatedEntries > 0) {
  77. $out->info('Fixed link share permissions for ' . $updatedEntries . ' shares');
  78. }
  79. }
  80. /**
  81. * Remove shares where the parent share does not exist anymore
  82. */
  83. private function removeSharesNonExistingParent(IOutput $out) {
  84. $deletedEntries = 0;
  85. $query = $this->connection->getQueryBuilder();
  86. $query->select('s1.parent')
  87. ->from('share', 's1')
  88. ->where($query->expr()->isNotNull('s1.parent'))
  89. ->andWhere($query->expr()->isNull('s2.id'))
  90. ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id'))
  91. ->groupBy('s1.parent')
  92. ->setMaxResults(self::CHUNK_SIZE);
  93. $deleteQuery = $this->connection->getQueryBuilder();
  94. $deleteQuery->delete('share')
  95. ->where($deleteQuery->expr()->eq('parent', $deleteQuery->createParameter('parent')));
  96. $deletedInLastChunk = self::CHUNK_SIZE;
  97. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  98. $deletedInLastChunk = 0;
  99. $result = $query->execute();
  100. while ($row = $result->fetch()) {
  101. $deletedInLastChunk++;
  102. $deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent'])
  103. ->execute();
  104. }
  105. $result->closeCursor();
  106. }
  107. if ($deletedEntries) {
  108. $out->info('Removed ' . $deletedEntries . ' shares where the parent did not exist');
  109. }
  110. }
  111. public function run(IOutput $out) {
  112. $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  113. if (version_compare($ocVersionFromBeforeUpdate, '8.2.0.7', '<')) {
  114. // this situation was only possible before 8.2
  115. $this->removeExpirationDateFromNonLinkShares($out);
  116. }
  117. if (version_compare($ocVersionFromBeforeUpdate, '9.1.0.9', '<')) {
  118. // this situation was only possible before 9.1
  119. $this->addShareLinkDeletePermission($out);
  120. }
  121. $this->removeSharesNonExistingParent($out);
  122. }
  123. }