Completed
Push — master ( a53aa4...e37cf2 )
by Lukas
01:05
created

cron.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Jakob Sack <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Oliver Kohl D.Sc. <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Steffen Lindner <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Vincent Petry <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
// Show warning if a PHP version below 5.6.0 is used
34
if (version_compare(PHP_VERSION, '5.6.0') === -1) {
35
	echo 'This version of Nextcloud requires at least PHP 5.6.0<br/>';
36
	echo 'You are currently running ' . PHP_VERSION . '. Please update your PHP version.';
37
	return;
38
}
39
40
try {
41
42
	require_once __DIR__ . '/lib/base.php';
43
44
	if (\OCP\Util::needUpgrade()) {
45
		\OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
46
		exit;
47
	}
48
	if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
49
		\OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
50
		exit;
51
	}
52
53
	// load all apps to get all api routes properly setup
54
	OC_App::loadApps();
55
56
	\OC::$server->getSession()->close();
57
58
	// initialize a dummy memory session
59
	$session = new \OC\Session\Memory('');
60
	$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
61
	$session = $cryptoWrapper->wrapSession($session);
62
	\OC::$server->setSession($session);
63
64
	$logger = \OC::$server->getLogger();
65
	$config = \OC::$server->getConfig();
66
67
	// Don't do anything if ownCloud has not been installed
68
	if (!$config->getSystemValue('installed', false)) {
69
		exit(0);
70
	}
71
72
	\OC::$server->getTempManager()->cleanOld();
73
74
	// Exit if background jobs are disabled!
75
	$appMode = \OCP\BackgroundJob::getExecutionType();
76
	if ($appMode == 'none') {
77
		if (OC::$CLI) {
78
			echo 'Background Jobs are disabled!' . PHP_EOL;
79
		} else {
80
			OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
81
		}
82
		exit(1);
83
	}
84
85
	if (OC::$CLI) {
86
		// set to run indefinitely if needed
87
		if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
88
			@set_time_limit(0);
89
		}
90
91
		// the cron job must be executed with the right user
92
		if (!function_exists('posix_getuid')) {
93
			echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
94
			exit(1);
95
		}
96
		$user = posix_getpwuid(posix_getuid());
97
		$configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php'));
98
		if ($user['name'] !== $configUser['name']) {
99
			echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
100
			echo "Current user: " . $user['name'] . PHP_EOL;
101
			echo "Web server user: " . $configUser['name'] . PHP_EOL;
102
			exit(1);
103
		}
104
105
		// We call ownCloud from the CLI (aka cron)
106
		if ($appMode != 'cron') {
107
			\OCP\BackgroundJob::setExecutionType('cron');
108
		}
109
110
		// Work
111
		$jobList = \OC::$server->getJobList();
112
113
		// We only ask for jobs for 14 minutes, because after 15 minutes the next
114
		// system cron task should spawn.
115
		$endTime = time() + 14 * 60;
116
117
		$executedJobs = [];
118
		while ($job = $jobList->getNext()) {
119
			if (isset($executedJobs[$job->getId()])) {
120
				$jobList->unlockJob($job);
121
				break;
122
			}
123
124
			$job->execute($jobList, $logger);
125
			// clean up after unclean jobs
126
			\OC_Util::tearDownFS();
127
128
			$jobList->setLastJob($job);
129
			$executedJobs[$job->getId()] = true;
130
			unset($job);
131
132
			if (time() > $endTime) {
133
				break;
134
			}
135
		}
136
137
	} else {
138
		// We call cron.php from some website
139
		if ($appMode == 'cron') {
140
			// Cron is cron :-P
141
			OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
142
		} else {
143
			// Work and success :-)
144
			$jobList = \OC::$server->getJobList();
145
			$job = $jobList->getNext();
146
			if ($job != null) {
147
				$job->execute($jobList, $logger);
148
				$jobList->setLastJob($job);
149
			}
150
			OC_JSON::success();
151
		}
152
	}
153
154
	// Log the successful cron execution
155
	if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
156
		\OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
157
	}
158
	exit();
159
160
} catch (Exception $ex) {
161
	\OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
162
} catch (Error $ex) {
0 ignored issues
show
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
163
	\OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
164
}
165