2015-10-07 21:05:44 +02:00
|
|
|
<?php
|
2025-04-27 14:38:18 +02:00
|
|
|
|
2015-10-07 21:05:44 +02:00
|
|
|
/**
|
2024-06-06 19:48:28 +02:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-10-07 21:05:44 +02:00
|
|
|
*/
|
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
2020-11-09 21:45:20 +01:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\BackgroundJob\TimedJob;
|
2025-03-24 15:39:29 +01:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2020-11-09 21:45:20 +01:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager;
|
2020-06-24 16:49:16 +02:00
|
|
|
use OCP\Share\IShare;
|
2015-10-07 21:05:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all shares that are expired
|
|
|
|
*/
|
|
|
|
class ExpireSharesJob extends TimedJob {
|
|
|
|
|
2020-11-09 21:45:20 +01:00
|
|
|
/** @var IManager */
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
public function __construct(ITimeFactory $time, IManager $shareManager, IDBConnection $db) {
|
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
$this->db = $db;
|
|
|
|
|
|
|
|
parent::__construct($time);
|
|
|
|
|
2015-10-07 21:05:44 +02:00
|
|
|
// Run once a day
|
|
|
|
$this->setInterval(24 * 60 * 60);
|
2024-10-07 17:36:55 +02:00
|
|
|
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
|
2015-10-07 21:05:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 21:45:20 +01:00
|
|
|
|
2015-10-07 21:05:44 +02:00
|
|
|
/**
|
|
|
|
* Makes the background job do its work
|
|
|
|
*
|
|
|
|
* @param array $argument unused argument
|
|
|
|
*/
|
|
|
|
public function run($argument) {
|
|
|
|
//Current time
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expire file link shares only (for now)
|
|
|
|
*/
|
2020-11-09 21:45:20 +01:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->select('id', 'share_type')
|
2015-10-07 21:05:44 +02:00
|
|
|
->from('share')
|
|
|
|
->where(
|
|
|
|
$qb->expr()->andX(
|
2025-03-24 15:39:29 +01:00
|
|
|
$qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_LINK, IShare::TYPE_EMAIL], IQueryBuilder::PARAM_INT_ARRAY)),
|
2015-10-07 21:05:44 +02:00
|
|
|
$qb->expr()->lte('expiration', $qb->expr()->literal($now)),
|
2025-03-24 15:39:29 +01:00
|
|
|
$qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY))
|
2015-10-07 21:05:44 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-03-31 11:18:03 +02:00
|
|
|
$shares = $qb->executeQuery();
|
2020-04-10 14:19:56 +02:00
|
|
|
while ($share = $shares->fetch()) {
|
2020-11-09 21:45:20 +01:00
|
|
|
if ((int) $share['share_type'] === IShare::TYPE_LINK) {
|
|
|
|
$id = 'ocinternal';
|
|
|
|
} elseif ((int) $share['share_type'] === IShare::TYPE_EMAIL) {
|
|
|
|
$id = 'ocMailShare';
|
|
|
|
}
|
|
|
|
|
|
|
|
$id .= ':' . $share['id'];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$share = $this->shareManager->getShareById($id);
|
|
|
|
$this->shareManager->deleteShare($share);
|
|
|
|
} catch (ShareNotFound $e) {
|
|
|
|
// Normally the share gets automatically expired on fetching it
|
|
|
|
}
|
2015-10-07 21:05:44 +02:00
|
|
|
}
|
|
|
|
$shares->closeCursor();
|
|
|
|
}
|
|
|
|
}
|