1<?php
2/**
3 * @author Roeland Jago Douma <rullzer@owncloud.com>
4 * @author Semih Serhat Karakaya <karakayasemi@itu.edu.tr>
5 *
6 * @copyright Copyright (c) 2018, ownCloud GmbH
7 * @license AGPL-3.0
8 *
9 * This code is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License, version 3,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License, version 3,
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20 *
21 */
22
23namespace OCA\Files_Sharing;
24
25use OC\BackgroundJob\TimedJob;
26use OC\Share20\DefaultShareProvider;
27use OCP\Activity\IEvent;
28use OCP\IDBConnection;
29use OCP\Share\Exceptions\ShareNotFound;
30use OCP\Share\IManager;
31use OCP\Activity\IManager as ActivityIManager;
32
33/**
34 * Delete all shares that are expired
35 */
36class ExpireSharesJob extends TimedJob {
37
38	/**
39	 * @var IManager $shareManager
40	 */
41	private $shareManager;
42
43	/**
44	 * @var IDBConnection $connection
45	 */
46	private $connection;
47
48	/**
49	 * @var DefaultShareProvider $defaultShareProvider
50	 */
51	private $defaultShareProvider;
52
53	/**
54	 * @var ActivityIManager $activityManager
55	 */
56	private $activityManager;
57
58	/**
59	 * sets the correct interval for this timed job
60	 *
61	 * @param IManager $shareManager
62	 * @param IDBConnection $connection
63	 * @param DefaultShareProvider $defaultShareProvider
64	 * @param ActivityIManager $activityManager
65	 */
66	public function __construct(
67		IManager $shareManager,
68		IDBConnection $connection,
69		DefaultShareProvider $defaultShareProvider,
70		ActivityIManager $activityManager
71	) {
72		// Run once a day
73		$this->setInterval(24 * 60 * 60);
74		$this->shareManager = $shareManager;
75		$this->connection = $connection;
76		$this->defaultShareProvider = $defaultShareProvider;
77		$this->activityManager = $activityManager;
78	}
79
80	/**
81	 * Makes the background job do its work
82	 *
83	 * @param array $argument unused argument
84	 */
85	public function run($argument) {
86		//Current time
87		$today = new \DateTime("today");
88		$today = $today->format('Y-m-d H:i:s');
89
90		/*
91		 * Expire file shares only (for now)
92		 */
93		$qb = $this->connection->getQueryBuilder();
94		$qb->select('id')
95			->from('share')
96			->where(
97				$qb->expr()->andX(
98					$qb->expr()->lt('expiration', $qb->expr()->literal($today)),
99					$qb->expr()->orX(
100						$qb->expr()->eq('item_type', $qb->expr()->literal('file')),
101						$qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
102					)
103				)
104			);
105
106		$shares = $qb->execute();
107		while ($share = $shares->fetch()) {
108			$this->activityManager->setAgentAuthor(IEvent::AUTOMATION_AUTHOR);
109			try {
110				/*
111				 * The type of $share['id'] changes depends on the db type. (int for pgsql, string for others)
112				 * This situation led to problem when stubbing method in tests.
113				 * $share['id'] has been casted to string to ensure consistency.
114				 */
115				$shareObject = $this->defaultShareProvider->getShareById((string)$share['id']);
116				$this->shareManager->deleteShare($shareObject);
117			} catch (ShareNotFound $ex) {
118				//already deleted
119			} finally {
120				$this->activityManager->restoreAgentAuthor();
121			}
122		}
123		$shares->closeCursor();
124	}
125}
126