1<?php if (!defined('MASTER_CRONJOB')) die('You cannot access this file directly!');
2
3/**
4 * This file is part of the Froxlor project.
5 * Copyright (c) 2003-2009 the SysCP Team (see authors).
6 * Copyright (c) 2010 the Froxlor Team (see authors).
7 *
8 * For the full copyright and license information, please view the COPYING
9 * file that was distributed with this source code. You can also view the
10 * COPYING file online at http://files.froxlor.org/misc/COPYING.txt
11 *
12 * @copyright  (c) the authors
13 * @author     Florian Lippert <flo@syscp.org> (2003-2009)
14 * @author     Froxlor team <team@froxlor.org> (2010-)
15 * @license    GPLv2 http://files.froxlor.org/misc/COPYING.txt
16 * @package    Cron
17 *
18 */
19
20if (@php_sapi_name() != 'cli'
21	&& @php_sapi_name() != 'cgi'
22	&& @php_sapi_name() != 'cgi-fcgi'
23) {
24	die('This script will only work in the shell.');
25}
26
27// ensure that default timezone is set
28if (function_exists("date_default_timezone_set")
29	&& function_exists("date_default_timezone_get")
30) {
31	@date_default_timezone_set(@date_default_timezone_get());
32}
33
34$basename = basename($_SERVER['PHP_SELF'], '.php');
35$crontype = "";
36if (isset($argv) && is_array($argv) && count($argv) > 1) {
37	for($x=1;$x < count($argv);$x++) {
38		if (substr(strtolower($argv[$x]), 0, 2) == '--'
39			&& strlen($argv[$x]) > 3
40		) {
41			$crontype = substr(strtolower($argv[$x]), 2);
42			$basename .= "-".$crontype;
43			break;
44		}
45	}
46}
47$lockdir = '/var/run/';
48$lockFilename = 'froxlor_' . $basename . '.lock-';
49$lockfName = $lockFilename . getmypid();
50$lockfile = $lockdir . $lockfName;
51
52// guess the froxlor installation path
53// normally you should not need to modify this script anymore, if your
54// froxlor installation isn't in /var/www/froxlor
55define('FROXLOR_INSTALL_DIR', dirname(dirname(__FILE__)));
56
57
58// create and open the lockfile!
59$keepLockFile = false;
60$debugHandler = fopen($lockfile, 'w');
61fwrite($debugHandler, 'Setting Lockfile to ' . $lockfile . "\n");
62fwrite($debugHandler, 'Setting Froxlor installation path to ' . FROXLOR_INSTALL_DIR . "\n");
63
64if (!file_exists(FROXLOR_INSTALL_DIR . '/lib/userdata.inc.php')) {
65	die("Froxlor does not seem to be installed yet - skipping cronjob");
66}
67
68// Includes the Usersettings eg. MySQL-Username/Passwort etc.
69require FROXLOR_INSTALL_DIR . '/lib/userdata.inc.php';
70fwrite($debugHandler, 'Userdatas included' . "\n");
71
72// Legacy sql-root-information
73if (isset($sql['root_user'])
74	&& isset($sql['root_password'])
75	&& (!isset($sql_root) || !is_array($sql_root))
76) {
77	$sql_root = array(0 => array('caption' => 'Default', 'host' => $sql['host'], 'user' => $sql['root_user'], 'password' => $sql['root_password']));
78	unset($sql['root_user']);
79	unset($sql['root_password']);
80}
81
82// Includes the Functions
83require FROXLOR_INSTALL_DIR . '/lib/functions.php';
84
85//Includes the MySQL-Tabledefinitions etc.
86require FROXLOR_INSTALL_DIR . '/lib/tables.inc.php';
87fwrite($debugHandler, 'Table definitions included' . "\n");
88
89// try database connection, it will throw
90// and exception itself if failed
91try {
92	Database::query("SELECT 1");
93} catch (Exception $e) {
94	// Do not proceed further if no database connection could be established
95	fclose($debugHandler);
96	unlink($lockfile);
97	die($e->getMessage());
98}
99
100fwrite($debugHandler, 'Database-connection established' . "\n");
101
102// open the lockfile directory and scan for existing lockfiles
103$lockDirHandle = opendir($lockdir);
104
105while ($fName = readdir($lockDirHandle)) {
106
107	if ($lockFilename == substr($fName, 0, strlen($lockFilename))
108			&& $lockfName != $fName
109	) {
110		// Check if last run jailed out with an exception
111		$croncontent = file($lockdir . $fName);
112		$lastline = $croncontent[(count($croncontent) - 1)];
113
114		if ($lastline == '=== Keep lockfile because of exception ===') {
115			fclose($debugHandler);
116			unlink($lockfile);
117			dieWithMail('Last cron jailed out with an exception. Exiting...' . "\n" . 'Take a look into the contents of ' . $lockdir . $fName . '* for more information!' . "\n");
118		}
119
120		// Check if cron is running or has died.
121		$check_pid = substr(strrchr($fName, "-"), 1);
122		system("kill -CHLD " . (int)$check_pid . " 1> /dev/null 2> /dev/null", $check_pid_return);
123
124		if ($check_pid_return == 1) {
125			// Result:      Existing lockfile/pid isn't running
126			//              Most likely it has died
127			//
128			// Action:      Remove it and continue
129			//
130			fwrite($debugHandler, 'Previous cronjob didn\'t exit clean. PID: ' . $check_pid . "\n");
131			fwrite($debugHandler, 'Removing lockfile: ' . $lockdir . $fName . "\n");
132			@unlink($lockdir . $fName);
133
134		} else {
135			// Result:      A Cronscript with this pid
136			//              is still running
137			// Action:      remove my own Lock and die
138			//
139			// close the current lockfile
140			fclose($debugHandler);
141
142			// ... and delete it
143			unlink($lockfile);
144			dieWithMail('There is already a Cronjob for '.$crontype.' in progress. Exiting...' . "\n" . 'Take a look into the contents of ' . $lockdir . $lockFilename . '* for more information!' . "\n");
145		}
146	}
147}
148
149/**
150 * if using fcgid or fpm for froxlor-vhost itself, we have to check
151 * whether the permission of the files are still correct
152 */
153fwrite($debugHandler, 'Checking froxlor file permissions'."\n");
154$_mypath = makeCorrectDir(FROXLOR_INSTALL_DIR);
155
156if (((int)Settings::Get('system.mod_fcgid') == 1 && (int)Settings::Get('system.mod_fcgid_ownvhost') == 1)
157		|| ((int)Settings::Get('phpfpm.enabled') == 1 && (int)Settings::Get('phpfpm.enabled_ownvhost') == 1)
158) {
159	$user = Settings::Get('system.mod_fcgid_httpuser');
160	$group = Settings::Get('system.mod_fcgid_httpgroup');
161
162	if (Settings::Get('phpfpm.enabled') == 1) {
163		$user = Settings::Get('phpfpm.vhost_httpuser');
164		$group = Settings::Get('phpfpm.vhost_httpgroup');
165	}
166	// all the files and folders have to belong to the local user
167	// now because we also use fcgid for our own vhost
168	safe_exec('chown -R ' . $user . ':' . $group . ' ' . escapeshellarg($_mypath));
169} else {
170	// back to webserver permission
171	$user = Settings::Get('system.httpuser');
172	$group = Settings::Get('system.httpgroup');
173	safe_exec('chown -R ' . $user . ':' . $group . ' ' . escapeshellarg($_mypath));
174}
175
176// Initialize logging
177$cronlog = FroxlorLogger::getInstanceOf(array('loginname' => 'cronjob'));
178fwrite($debugHandler, 'Logger has been included' . "\n");
179
180if (hasUpdates($version) || hasDbUpdates($dbversion)
181) {
182	if (Settings::Get('system.cron_allowautoupdate') == null
183		|| Settings::Get('system.cron_allowautoupdate') == 0
184	) {
185		/**
186		 * Do not proceed further if the Database version is not the same as the script version
187		 */
188		fclose($debugHandler);
189		unlink($lockfile);
190		$errormessage = "Version of file doesn't match version of database. Exiting...\n\n";
191		$errormessage.= "Possible reason: Froxlor update\n";
192		$errormessage.= "Information: Current version in database: ".Settings::Get('panel.version')." (DB: ".Settings::Get('panel.db_version').") - version of Froxlor files: ".$version." (DB: ".$dbversion.")\n";
193		$errormessage.= "Solution: Please visit your Foxlor admin interface for further information.\n";
194		dieWithMail($errormessage);
195	}
196
197	if (Settings::Get('system.cron_allowautoupdate') == 1) {
198		/**
199		 * let's walk the walk - do the dangerous shit
200		 */
201		$cronlog->logAction(CRON_ACTION, LOG_WARNING, 'Automatic update is activated and we are going to proceed without any notices');
202		$cronlog->logAction(CRON_ACTION, LOG_WARNING, 'all new settings etc. will be stored with the default value, that might not always be right for your system!');
203		$cronlog->logAction(CRON_ACTION, LOG_WARNING, "If you don't want this to happen in the future consider removing the --allow-autoupdate flag from the cronjob");
204		fwrite($debugHandler, '*** WARNING *** - Automatic update is activated and we are going to proceed without any notices' . "\n");
205		fwrite($debugHandler, '*** WARNING *** - all new settings etc. will be stored with the default value, that might not always be right for your system!' . "\n");
206		fwrite($debugHandler, "*** WARNING *** - If you don't want this to happen in the future consider removing the --allow-autoupdate flag from the cronjob\n");
207		// including update procedures
208		include_once FROXLOR_INSTALL_DIR.'/install/updatesql.php';
209		// pew - everything went better than expected
210		$cronlog->logAction(CRON_ACTION, LOG_WARNING, 'Automatic update done - you should check your settings to be sure everything is fine');
211		fwrite($debugHandler, '*** WARNING *** - Automatic update done - you should check your settings to be sure everything is fine' . "\n");
212	}
213}
214
215fwrite($debugHandler, 'Froxlor version and database version are correct' . "\n");
216
217$cronscriptDebug = (Settings::Get('system.debug_cron') == '1') ? true : false;
218
219// Create a new idna converter
220$idna_convert = new idna_convert_wrapper();
221