Passed
Push — master ( 0599a8...024ed9 )
by Roeland
14:40 queued 12s
created
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Artem Sidorenko <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Daniel Kesselberg <[email protected]>
9
 * @author hoellen <[email protected]>
10
 * @author J0WI <[email protected]>
11
 * @author Jakob Sack <[email protected]>
12
 * @author Joas Schilling <[email protected]>
13
 * @author Jörn Friedrich Dreyer <[email protected]>
14
 * @author Ko- <[email protected]>
15
 * @author Michael Kuhn <[email protected]>
16
 * @author Morris Jobke <[email protected]>
17
 * @author Oliver Kohl D.Sc. <[email protected]>
18
 * @author Robin Appelman <[email protected]>
19
 * @author Roeland Jago Douma <[email protected]>
20
 * @author Steffen Lindner <[email protected]>
21
 * @author Thomas Müller <[email protected]>
22
 * @author Vincent Petry <[email protected]>
23
 *
24
 * @license AGPL-3.0
25
 *
26
 * This code is free software: you can redistribute it and/or modify
27
 * it under the terms of the GNU Affero General Public License, version 3,
28
 * as published by the Free Software Foundation.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33
 * GNU Affero General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU Affero General Public License, version 3,
36
 * along with this program. If not, see <http://www.gnu.org/licenses/>
37
 *
38
 */
39
40
require_once __DIR__ . '/lib/versioncheck.php';
41
42
try {
43
	require_once __DIR__ . '/lib/base.php';
44
45
	if (\OCP\Util::needUpgrade()) {
46
		\OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
47
		exit;
48
	}
49
	if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
50
		\OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
51
		exit;
52
	}
53
54
	// load all apps to get all api routes properly setup
55
	OC_App::loadApps();
56
57
	\OC::$server->getSession()->close();
58
59
	// initialize a dummy memory session
60
	$session = new \OC\Session\Memory('');
61
	$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
62
	$session = $cryptoWrapper->wrapSession($session);
63
	\OC::$server->setSession($session);
64
65
	$logger = \OC::$server->getLogger();
66
	$config = \OC::$server->getConfig();
67
68
	// Don't do anything if Nextcloud has not been installed
69
	if (!$config->getSystemValue('installed', false)) {
70
		exit(0);
71
	}
72
73
	\OC::$server->getTempManager()->cleanOld();
74
75
	// Exit if background jobs are disabled!
76
	$appMode = $config->getAppValue('core', 'backgroundjobs_mode', 'ajax');
77
	if ($appMode === 'none') {
78
		if (OC::$CLI) {
79
			echo 'Background Jobs are disabled!' . PHP_EOL;
80
		} else {
81
			OC_JSON::error(['data' => ['message' => 'Background jobs disabled!']]);
82
		}
83
		exit(1);
84
	}
85
86
	if (OC::$CLI) {
87
		// set to run indefinitely if needed
88
		if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
89
			@set_time_limit(0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

89
			/** @scrutinizer ignore-unhandled */ @set_time_limit(0);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
		}
91
92
		// the cron job must be executed with the right user
93
		if (!function_exists('posix_getuid')) {
94
			echo "The posix extensions are required - see https://www.php.net/manual/en/book.posix.php" . PHP_EOL;
95
			exit(1);
96
		}
97
98
		$user = posix_getuid();
99
		$configUser = fileowner(OC::$configDir . 'config.php');
100
		if ($user !== $configUser) {
101
			echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
102
			echo "Current user id: " . $user . PHP_EOL;
103
			echo "Owner id of config.php: " . $configUser . PHP_EOL;
104
			exit(1);
105
		}
106
107
108
		// We call Nextcloud from the CLI (aka cron)
109
		if ($appMode !== 'cron') {
110
			$config->setAppValue('core', 'backgroundjobs_mode', 'cron');
111
		}
112
113
		// Work
114
		$jobList = \OC::$server->getJobList();
115
116
		// We only ask for jobs for 14 minutes, because after 5 minutes the next
117
		// system cron task should spawn and we want to have at most three
118
		// cron jobs running in parallel.
119
		$endTime = time() + 14 * 60;
120
121
		$executedJobs = [];
122
		while ($job = $jobList->getNext()) {
123
			if (isset($executedJobs[$job->getId()])) {
124
				$jobList->unlockJob($job);
125
				break;
126
			}
127
128
			$job->execute($jobList, $logger);
129
			// clean up after unclean jobs
130
			\OC_Util::tearDownFS();
131
132
			$jobList->setLastJob($job);
133
			$executedJobs[$job->getId()] = true;
134
			unset($job);
135
136
			if (time() > $endTime) {
137
				break;
138
			}
139
		}
140
	} else {
141
		// We call cron.php from some website
142
		if ($appMode === 'cron') {
143
			// Cron is cron :-P
144
			OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]);
145
		} else {
146
			// Work and success :-)
147
			$jobList = \OC::$server->getJobList();
148
			$job = $jobList->getNext();
149
			if ($job != null) {
150
				$job->execute($jobList, $logger);
151
				$jobList->setLastJob($job);
152
			}
153
			OC_JSON::success();
154
		}
155
	}
156
157
	// Log the successful cron execution
158
	$config->setAppValue('core', 'lastcron', time());
159
	exit();
160
} catch (Exception $ex) {
161
	\OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
162
} catch (Error $ex) {
163
	\OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
164
}
165