Completed
Push — stable10 ( 374e1c...9b4fe2 )
by Lukas
15:32 queued 08:49
created

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.4.0 is used
34 View Code Duplication
if (version_compare(PHP_VERSION, '5.4.0') === -1) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
	echo 'This version of Nextcloud requires at least PHP 5.4.0<br/>';
36
	echo 'You are currently running ' . PHP_VERSION . '. Please update your PHP version.';
37
	return;
38
}
39
40
// Show warning if PHP 7.1 is used as Nextcloud is not compatible with PHP 7.1 for now
41
// @see https://github.com/nextcloud/docker-ci/issues/10
42 View Code Duplication
if (version_compare(PHP_VERSION, '7.1.0') !== -1) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
	echo 'This version of Nextcloud is not compatible with PHP 7.1.<br/>';
44
	echo 'You are currently running ' . PHP_VERSION . '.';
45
	return;
46
}
47
48
try {
49
50
	require_once __DIR__ . '/lib/base.php';
51
52
	if (\OCP\Util::needUpgrade()) {
53
		\OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
54
		exit;
55
	}
56
	if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
57
		\OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
58
		exit;
59
	}
60
61
	if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
62
		\OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
63
		exit;
64
	}
65
66
	// load all apps to get all api routes properly setup
67
	OC_App::loadApps();
68
69
	\OC::$server->getSession()->close();
70
71
	// initialize a dummy memory session
72
	$session = new \OC\Session\Memory('');
73
	$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
74
	$session = $cryptoWrapper->wrapSession($session);
75
	\OC::$server->setSession($session);
76
77
	$logger = \OC::$server->getLogger();
78
	$config = \OC::$server->getConfig();
79
80
	// Don't do anything if ownCloud has not been installed
81
	if (!$config->getSystemValue('installed', false)) {
82
		exit(0);
83
	}
84
85
	\OC::$server->getTempManager()->cleanOld();
86
87
	// Exit if background jobs are disabled!
88
	$appMode = \OCP\BackgroundJob::getExecutionType();
89
	if ($appMode == 'none') {
90
		if (OC::$CLI) {
91
			echo 'Background Jobs are disabled!' . PHP_EOL;
92
		} else {
93
			OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
94
		}
95
		exit(1);
96
	}
97
98
	if (OC::$CLI) {
99
		// set to run indefinitely if needed
100
		set_time_limit(0);
101
102
		// the cron job must be executed with the right user
103
		if (!function_exists('posix_getuid')) {
104
			echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
105
			exit(0);
106
		}
107
		$user = posix_getpwuid(posix_getuid());
108
		$configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php'));
109
		if ($user['name'] !== $configUser['name']) {
110
			echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
111
			echo "Current user: " . $user['name'] . PHP_EOL;
112
			echo "Web server user: " . $configUser['name'] . PHP_EOL;
113
			exit(0);
114
		}
115
116
		// We call ownCloud from the CLI (aka cron)
117
		if ($appMode != 'cron') {
118
			\OCP\BackgroundJob::setExecutionType('cron');
119
		}
120
121
		// Work
122
		$jobList = \OC::$server->getJobList();
123
124
		// We only ask for jobs for 14 minutes, because after 15 minutes the next
125
		// system cron task should spawn.
126
		$endTime = time() + 14 * 60;
127
128
		$executedJobs = [];
129
		while ($job = $jobList->getNext()) {
130
			if (isset($executedJobs[$job->getId()])) {
131
				$jobList->unlockJob($job);
132
				break;
133
			}
134
135
			$logger->debug('Run ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
136
			$job->execute($jobList, $logger);
137
			// clean up after unclean jobs
138
			\OC_Util::tearDownFS();
139
			$logger->debug('Finished ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
140
141
			$jobList->setLastJob($job);
142
			$executedJobs[$job->getId()] = true;
143
			unset($job);
144
145
			if (time() > $endTime) {
146
				break;
147
			}
148
		}
149
150
	} else {
151
		// We call cron.php from some website
152
		if ($appMode == 'cron') {
153
			// Cron is cron :-P
154
			OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
155
		} else {
156
			// Work and success :-)
157
			$jobList = \OC::$server->getJobList();
158
			$job = $jobList->getNext();
159
			if ($job != null) {
160
				$job->execute($jobList, $logger);
161
				$jobList->setLastJob($job);
162
			}
163
			OC_JSON::success();
164
		}
165
	}
166
167
	// Log the successful cron execution
168
	if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
169
		\OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
170
	}
171
	exit();
172
173
} catch (Exception $ex) {
174
	\OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
175
} catch (Error $ex) {
176
	\OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
177
}
178