Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
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 Phil Davis <[email protected]>
13
 * @author Robin Appelman <[email protected]>
14
 * @author Steffen Lindner <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 * @author Vincent Petry <[email protected]>
17
 *
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
34
try {
35
36
	require_once __DIR__ . '/lib/base.php';
37
38
	if (\OCP\Util::needUpgrade()) {
39
		\OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
40
		exit;
41
	}
42
	if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
43
		\OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
44
		exit;
45
	}
46
47
	if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
48
		\OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
49
		exit;
50
	}
51
52
	// load all apps to get all api routes properly setup
53
	OC_App::loadApps();
54
55
	\OC::$server->getSession()->close();
56
57
	// initialize a dummy memory session
58
	$session = new \OC\Session\Memory('');
59
	$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
60
	$session = $cryptoWrapper->wrapSession($session);
61
	\OC::$server->setSession($session);
62
63
	$logger = \OC::$server->getLogger();
64
	$config = \OC::$server->getConfig();
65
66
	// Don't do anything if ownCloud has not been installed
67
	if (!$config->getSystemValue('installed', false)) {
68
		exit(0);
69
	}
70
71
	\OC::$server->getTempManager()->cleanOld();
72
73
	// Exit if background jobs are disabled!
74
	$appMode = \OCP\BackgroundJob::getExecutionType();
75
	if ($appMode == 'none') {
76
		if (OC::$CLI) {
77
			echo 'Background Jobs are disabled!' . PHP_EOL;
78
		} else {
79
			OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
80
		}
81
		exit(1);
82
	}
83
84
	if (OC::$CLI) {
85
		// set to run indefinitely if needed
86
		set_time_limit(0);
87
88
		// the cron job must be executed with the right user
89
		if (!OC_Util::runningOnWindows())  {
90
			if (!function_exists('posix_getuid')) {
91
				echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
92
				exit(0);
93
			}
94
			$user = posix_getpwuid(posix_getuid());
95
			$configUser = posix_getpwuid(fileowner(OC::$SERVERROOT . '/config/config.php'));
96
			if ($user['name'] !== $configUser['name']) {
97
				echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
98
				echo "Current user: " . $user['name'] . PHP_EOL;
99
				echo "Web server user: " . $configUser['name'] . PHP_EOL;
100
				exit(0);
101
			}
102
		}
103
104
		$instanceId = $config->getSystemValue('instanceid');
105
		$lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
106
		$lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
107
		$lockDirectory = rtrim($lockDirectory, '\\/');
108
		$lockFile = $lockDirectory . '/' . $lockFileName;
109
110
		if (!file_exists($lockFile)) {
111
			touch($lockFile);
112
		}
113
114
		// We call ownCloud from the CLI (aka cron)
115
		if ($appMode != 'cron') {
116
			\OCP\BackgroundJob::setExecutionType('cron');
117
		}
118
119
		// open the file and try to lock it. If it is not locked, the background
120
		// job can be executed, otherwise another instance is already running
121
		$fp = fopen($lockFile, 'w');
122
		$isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
123
124
		// check if backgroundjobs is still running. The wouldBlock check is
125
		// needed on systems with advisory locking, see
126
		// http://php.net/manual/en/function.flock.php#45464
127
		if (!$isLocked || $wouldBlock) {
128
			echo "Another instance of cron.php is still running!" . PHP_EOL;
129
			exit(1);
130
		}
131
132
		// Work
133
		$jobList = \OC::$server->getJobList();
134
135
		// We only ask for jobs for 14 minutes, because after 15 minutes the next
136
		// system cron task should spawn.
137
		$endTime = time() + 14 * 60;
138
139
		$executedJobs = [];
140
		while ($job = $jobList->getNext()) {
141
			if (isset($executedJobs[$job->getId()])) {
142
				break;
143
			}
144
145
			$logger->debug('Run ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
146
			$job->execute($jobList, $logger);
147
			// clean up after unclean jobs
148
			\OC_Util::tearDownFS();
149
			$logger->debug('Finished ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
150
151
			$jobList->setLastJob($job);
152
			$executedJobs[$job->getId()] = true;
153
			unset($job);
154
155
			if (time() > $endTime) {
156
				break;
157
			}
158
		}
159
160
		// unlock the file
161
		flock($fp, LOCK_UN);
162
		fclose($fp);
163
164
	} else {
165
		// We call cron.php from some website
166
		if ($appMode == 'cron') {
167
			// Cron is cron :-P
168
			OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
169
		} else {
170
			// Work and success :-)
171
			$jobList = \OC::$server->getJobList();
172
			$job = $jobList->getNext();
173
			if ($job != null) {
174
				$job->execute($jobList, $logger);
175
				$jobList->setLastJob($job);
176
			}
177
			OC_JSON::success();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::success() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
178
		}
179
	}
180
181
	// Log the successful cron execution
182
	if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
183
		\OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
184
	}
185
	exit();
186
187
} catch (Exception $ex) {
188
	\OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
189
}
190