ExpireSharesJob.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_Sharing;
  24. use OC\BackgroundJob\TimedJob;
  25. /**
  26. * Delete all shares that are expired
  27. */
  28. class ExpireSharesJob extends TimedJob {
  29. /**
  30. * sets the correct interval for this timed job
  31. */
  32. public function __construct() {
  33. // Run once a day
  34. $this->setInterval(24 * 60 * 60);
  35. }
  36. /**
  37. * Makes the background job do its work
  38. *
  39. * @param array $argument unused argument
  40. */
  41. public function run($argument) {
  42. $connection = \OC::$server->getDatabaseConnection();
  43. //Current time
  44. $now = new \DateTime();
  45. $now = $now->format('Y-m-d H:i:s');
  46. /*
  47. * Expire file link shares only (for now)
  48. */
  49. $qb = $connection->getQueryBuilder();
  50. $qb->select('id', 'file_source', 'uid_owner', 'item_type')
  51. ->from('share')
  52. ->where(
  53. $qb->expr()->andX(
  54. $qb->expr()->eq('share_type', $qb->expr()->literal(\OCP\Share::SHARE_TYPE_LINK)),
  55. $qb->expr()->lte('expiration', $qb->expr()->literal($now)),
  56. $qb->expr()->orX(
  57. $qb->expr()->eq('item_type', $qb->expr()->literal('file')),
  58. $qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
  59. )
  60. )
  61. );
  62. $shares = $qb->execute();
  63. while($share = $shares->fetch()) {
  64. \OC\Share\Share::unshare($share['item_type'], $share['file_source'], \OCP\Share::SHARE_TYPE_LINK, null, $share['uid_owner']);
  65. }
  66. $shares->closeCursor();
  67. }
  68. }