Pascal Dreissen
2016-07-08 505fc4feb44bf9606f6ecc1f7bae897cf61446a3
commit | author | age
87a4a6 1 <?php
T 2 /*
039b46 3   Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh
eed6b3 4   All rights reserved.
87a4a6 5
eed6b3 6   Redistribution and use in source and binary forms, with or without modification,
V 7   are permitted provided that the following conditions are met:
87a4a6 8
eed6b3 9  * Redistributions of source code must retain the above copyright notice,
V 10   this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice,
12   this list of conditions and the following disclaimer in the documentation
13   and/or other materials provided with the distribution.
14  * Neither the name of ISPConfig nor the names of its contributors
15   may be used to endorse or promote products derived from this software without
16   specific prior written permission.
87a4a6 17
eed6b3 18   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
V 19   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
87a4a6 29
8e9a5f 30 define('SCRIPT_PATH', dirname($_SERVER["SCRIPT_FILENAME"]));
7fe908 31 require SCRIPT_PATH."/lib/config.inc.php";
611407 32
MB 33 // Check whether another instance of this script is already running
34 if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock')) {
35     clearstatcache();
36     $pid = trim(file_get_contents($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock'));
37     if(preg_match('/^[0-9]+$/', $pid)) {
38         if(file_exists('/proc/' . $pid)) {
39             print @date('d.m.Y-H:i').' - WARNING - There is already an instance of server.php running with pid ' . $pid . '.' . "\n";
40             exit;
41         }
42     }
43     print @date('d.m.Y-H:i').' - WARNING - There is already a lockfile set, but no process running with this pid (' . $pid . '). Continuing.' . "\n";
44 }
45
46 // Set Lockfile
47 @file_put_contents($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', getmypid());
48
49 if($conf['log_priority'] <= LOGLEVEL_DEBUG) print 'Set Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock' . "\n";
50
7fe908 51 require SCRIPT_PATH."/lib/app.inc.php";
87a4a6 52
132081 53 $app->setCaller('server');
MC 54
87a4a6 55 set_time_limit(0);
eed6b3 56 ini_set('error_reporting', E_ALL & ~E_NOTICE);
87a4a6 57
T 58 // make sure server_id is always an int
be76b0 59 $conf['server_id'] = intval($conf['server_id']);
87a4a6 60
32b40d 61 /*
eed6b3 62  * Try to Load the server configuration from the master-db
V 63  */
1d8f7f 64 if ($app->dbmaster->connect_error == NULL) {
cc7a82 65     $server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']);
7fe908 66
5be510 67     if(!is_array($server_db_record)) die('Unable to load the server configuration from database.');
7fe908 68
MC 69     //* Get the number of the last processed datalog_id, if the id of the local server
72695f 70     //* is > then the one of the remote system, then use the local ID as we might not have
T 71     //* reached the remote server during the last run then.
cc7a82 72     $local_server_db_record = $app->db->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']);
7fe908 73     $conf['last_datalog_id'] = (int) max($server_db_record['updated'], $local_server_db_record['updated']);
72695f 74     unset($local_server_db_record);
7fe908 75
eed6b3 76     $conf['mirror_server_id'] = (int) $server_db_record['mirror_server_id'];
039b46 77
db0a6f 78     // Load the ini_parser
T 79     $app->uses('ini_parser');
039b46 80
db0a6f 81     // Get server configuration
2a4cc6 82     $conf['serverconfig'] = $app->ini_parser->parse_ini_string(stripslashes($server_db_record['config']));
039b46 83
db0a6f 84     // Set the loglevel
2a4cc6 85     $conf['log_priority'] = intval($conf['serverconfig']['server']['loglevel']);
7fe908 86
615a0a 87     // Set level from which admin should be notified by email
T 88     if(!isset($conf['serverconfig']['server']['admin_notify_events']) || $conf['serverconfig']['server']['admin_notify_events'] == '') $conf['serverconfig']['server']['admin_notify_events'] = 3;
89     $conf['admin_notify_priority'] = intval($conf['serverconfig']['server']['admin_notify_events']);
eed6b3 90
039b46 91     // we do not need this variable anymore
9adcf5 92     unset($server_db_record);
7fe908 93
615a0a 94     // retrieve admin email address for notifications
7b47c0 95     $sys_ini = $app->db->queryOneRecord("SELECT * FROM sys_ini WHERE sysini_id = 1");
615a0a 96     $conf['sys_ini'] = $app->ini_parser->parse_ini_string(stripslashes($sys_ini['config']));
T 97     $conf['admin_mail'] = $conf['sys_ini']['mail']['admin_mail'];
98     unset($sys_ini);
7fe908 99
039b46 100     /*
V 101      * Save the rescue-config, maybe we need it (because the database is down)
102      */
103     $tmp['serverconfig']['server']['loglevel'] = $conf['log_priority'];
104     $tmp['serverconfig']['rescue'] = $conf['serverconfig']['rescue'];
105     file_put_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", serialize($tmp));
106     unset($tmp);
107
108     // protect the file
109     chmod(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", 0600);
7fe908 110
eed6b3 111 } else {
V 112     /*
113      * The master-db is not available.
114      * Problem: because we need to start the rescue-module (to rescue the DB if this IS the
115      * server, the master-db is running at) we have to initialize some config...
116      */
7fe908 117
039b46 118     /*
V 119      * If there is a temp-file with the data we could get from the database, then we use it
120      */
121     $tmp = array();
122     if (file_exists(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt")){
123         $tmp = unserialize(file_get_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt"));
124     }
7fe908 125
039b46 126     // maxint at 32 and 64 bit systems
7fe908 127     $conf['last_datalog_id'] = intval('9223372036854775807');
039b46 128
V 129     // no mirror
7fe908 130     $conf['mirror_server_id'] = 0;
039b46 131
7fe908 132     // Set the loglevel
039b46 133     $conf['log_priority'] = (isset($tmp['serverconfig']['server']['loglevel']))? $tmp['serverconfig']['server']['loglevel'] : LOGLEVEL_ERROR;
eed6b3 134     /*
V 135      * Set the configuration to rescue the database
136      */
039b46 137     if (isset($tmp['serverconfig']['rescue'])){
V 138         $conf['serverconfig']['rescue'] = $tmp['serverconfig']['rescue'];
139     }
140     else{
141         $conf['serverconfig']['rescue']['try_rescue'] = 'n';
142     }
143     // we do not need this variable anymore
144     unset($tmp);
db0a6f 145 }
3d4630 146
eed6b3 147 /** Do we need to start the core-modules */
7fe908 148
MC 149
eed6b3 150 $needStartCore = true;
87a4a6 151
eed6b3 152 /*
V 153  * Next we try to process the datalog
154  */
1d8f7f 155 if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) {
9eff6c 156
d2d3b9 157     // Check if there is anything to update
eed6b3 158     if ($conf['mirror_server_id'] > 0) {
cc7a82 159         $tmp_rec = $app->dbmaster->queryOneRecord("SELECT count(server_id) as number from sys_datalog WHERE datalog_id > ? AND (server_id = ? OR server_id = ? OR server_id = 0)", $conf['last_datalog_id'], $conf['server_id'], $conf['mirror_server_id']);
9adcf5 160     } else {
cc7a82 161         $tmp_rec = $app->dbmaster->queryOneRecord("SELECT count(server_id) as number from sys_datalog WHERE datalog_id > ? AND (server_id = ? OR server_id = 0)", $conf['last_datalog_id'], $conf['server_id']);
9adcf5 162     }
eed6b3 163
2a4cc6 164     $tmp_num_records = $tmp_rec['number'];
d2d3b9 165     unset($tmp_rec);
7fe908 166
5a43e7 167     //** Load required base-classes
ff6a68 168     $app->uses('modules,plugins,file,services,system');
5a43e7 169     //** Load the modules that are in the mods-enabled folder
T 170     $app->modules->loadModules('all');
171     //** Load the plugins that are in the plugins-enabled folder
172     $app->plugins->loadPlugins('all');
ab6e69 173     
MC 174     $app->plugins->raiseAction('server_plugins_loaded', '');
175     
eed6b3 176     if ($tmp_num_records > 0) {
d2d3b9 177         $app->log("Found $tmp_num_records changes, starting update process.", LOGLEVEL_DEBUG);
5a43e7 178         //** Go through the sys_datalog table and call the processing functions
T 179         //** from the modules that are hooked on to the table actions
d2d3b9 180         $app->modules->processDatalog();
T 181     }
5a43e7 182     //** Process actions from sys_remoteaction table
T 183     $app->modules->processActions();
184     //** Restart services that need to after configuration
185     $app->services->processDelayedActions();
186     //** All modules are already loaded and processed, so there is NO NEED to load the core once again...
187     $needStartCore = false;
7fe908 188
32b40d 189 } else {
1d8f7f 190     if ($app->db->connect->connect_error == NULL) {
eed6b3 191         $app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN);
0e381b 192     } else {
eed6b3 193         $app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN);
0e381b 194     }
782b02 195 }
3d4630 196
eed6b3 197 /*
V 198  * Under normal circumstances the system was loaded and all updates are done.
199  * but if we do not have to update anything or if the database is not accessible, then we
200  * have to start the core-system (if the database is accessible, we need the core because of the
201  * monitoring. If the databse is NOT accessible, we need the core because of rescue the db...
202  */
203 if ($needStartCore) {
204     // Write the log
205     $app->log('No Updated records found, starting only the core.', LOGLEVEL_DEBUG);
206     // Load required base-classes
207     $app->uses('modules,plugins,file,services');
208     // Load the modules that are im the mods-core folder
209     $app->modules->loadModules('core');
cc6568 210     // Load the plugins that are in the f folder
H 211     //$app->plugins->loadPlugins('core');
eed6b3 212 }
V 213
214
87a4a6 215 // Remove lock
eed6b3 216 @unlink($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
V 217 $app->log('Remove Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 218
32b40d 219
ba747c 220 die("finished.\n");
c30aaf 221 ?>