Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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";
MC 32 require SCRIPT_PATH."/lib/app.inc.php";
87a4a6 33
132081 34 $app->setCaller('server');
MC 35
87a4a6 36 set_time_limit(0);
eed6b3 37 ini_set('error_reporting', E_ALL & ~E_NOTICE);
87a4a6 38
T 39 // make sure server_id is always an int
be76b0 40 $conf['server_id'] = intval($conf['server_id']);
87a4a6 41
32b40d 42 /*
eed6b3 43  * Try to Load the server configuration from the master-db
V 44  */
1d8f7f 45 if ($app->dbmaster->connect_error == NULL) {
cc7a82 46     $server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']);
7fe908 47
5be510 48     if(!is_array($server_db_record)) die('Unable to load the server configuration from database.');
7fe908 49
MC 50     //* Get the number of the last processed datalog_id, if the id of the local server
72695f 51     //* is > then the one of the remote system, then use the local ID as we might not have
T 52     //* reached the remote server during the last run then.
cc7a82 53     $local_server_db_record = $app->db->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']);
7fe908 54     $conf['last_datalog_id'] = (int) max($server_db_record['updated'], $local_server_db_record['updated']);
72695f 55     unset($local_server_db_record);
7fe908 56
eed6b3 57     $conf['mirror_server_id'] = (int) $server_db_record['mirror_server_id'];
039b46 58
db0a6f 59     // Load the ini_parser
T 60     $app->uses('ini_parser');
039b46 61
db0a6f 62     // Get server configuration
2a4cc6 63     $conf['serverconfig'] = $app->ini_parser->parse_ini_string(stripslashes($server_db_record['config']));
039b46 64
db0a6f 65     // Set the loglevel
2a4cc6 66     $conf['log_priority'] = intval($conf['serverconfig']['server']['loglevel']);
7fe908 67
615a0a 68     // Set level from which admin should be notified by email
T 69     if(!isset($conf['serverconfig']['server']['admin_notify_events']) || $conf['serverconfig']['server']['admin_notify_events'] == '') $conf['serverconfig']['server']['admin_notify_events'] = 3;
70     $conf['admin_notify_priority'] = intval($conf['serverconfig']['server']['admin_notify_events']);
eed6b3 71
039b46 72     // we do not need this variable anymore
9adcf5 73     unset($server_db_record);
7fe908 74
615a0a 75     // retrieve admin email address for notifications
7b47c0 76     $sys_ini = $app->db->queryOneRecord("SELECT * FROM sys_ini WHERE sysini_id = 1");
615a0a 77     $conf['sys_ini'] = $app->ini_parser->parse_ini_string(stripslashes($sys_ini['config']));
T 78     $conf['admin_mail'] = $conf['sys_ini']['mail']['admin_mail'];
79     unset($sys_ini);
7fe908 80
039b46 81     /*
V 82      * Save the rescue-config, maybe we need it (because the database is down)
83      */
84     $tmp['serverconfig']['server']['loglevel'] = $conf['log_priority'];
85     $tmp['serverconfig']['rescue'] = $conf['serverconfig']['rescue'];
86     file_put_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", serialize($tmp));
87     unset($tmp);
88
89     // protect the file
90     chmod(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", 0600);
7fe908 91
eed6b3 92 } else {
V 93     /*
94      * The master-db is not available.
95      * Problem: because we need to start the rescue-module (to rescue the DB if this IS the
96      * server, the master-db is running at) we have to initialize some config...
97      */
7fe908 98
039b46 99     /*
V 100      * If there is a temp-file with the data we could get from the database, then we use it
101      */
102     $tmp = array();
103     if (file_exists(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt")){
104         $tmp = unserialize(file_get_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt"));
105     }
7fe908 106
039b46 107     // maxint at 32 and 64 bit systems
7fe908 108     $conf['last_datalog_id'] = intval('9223372036854775807');
039b46 109
V 110     // no mirror
7fe908 111     $conf['mirror_server_id'] = 0;
039b46 112
7fe908 113     // Set the loglevel
039b46 114     $conf['log_priority'] = (isset($tmp['serverconfig']['server']['loglevel']))? $tmp['serverconfig']['server']['loglevel'] : LOGLEVEL_ERROR;
eed6b3 115     /*
V 116      * Set the configuration to rescue the database
117      */
039b46 118     if (isset($tmp['serverconfig']['rescue'])){
V 119         $conf['serverconfig']['rescue'] = $tmp['serverconfig']['rescue'];
120     }
121     else{
122         $conf['serverconfig']['rescue']['try_rescue'] = 'n';
123     }
124     // we do not need this variable anymore
125     unset($tmp);
db0a6f 126 }
T 127
3d4630 128
9b82d2 129 // Check whether another instance of this script is already running
eed6b3 130 if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock')) {
V 131     clearstatcache();
b18806 132     $pid = trim(file_get_contents($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock'));
MC 133     if(preg_match('/^[0-9]+$/', $pid)) {
134         if(file_exists('/proc/' . $pid)) {
135             $app->log('There is already an instance of server.php running with pid ' . $pid . '.', LOGLEVEL_DEBUG);
136             exit;
eed6b3 137         }
V 138     }
b18806 139     $app->log('There is already a lockfile set, but no process running with this pid (' . $pid . '). Continuing.', LOGLEVEL_WARN);
87a4a6 140 }
T 141
142 // Set Lockfile
b18806 143 @file_put_contents($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', getmypid());
eed6b3 144 $app->log('Set Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 145
eed6b3 146 /** Do we need to start the core-modules */
7fe908 147
MC 148
eed6b3 149 $needStartCore = true;
87a4a6 150
eed6b3 151 /*
V 152  * Next we try to process the datalog
153  */
1d8f7f 154 if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) {
9eff6c 155
d2d3b9 156     // Check if there is anything to update
eed6b3 157     if ($conf['mirror_server_id'] > 0) {
cc7a82 158         $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 159     } else {
cc7a82 160         $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 161     }
eed6b3 162
2a4cc6 163     $tmp_num_records = $tmp_rec['number'];
d2d3b9 164     unset($tmp_rec);
7fe908 165
5a43e7 166     //** Load required base-classes
ff6a68 167     $app->uses('modules,plugins,file,services,system');
5a43e7 168     //** Load the modules that are in the mods-enabled folder
T 169     $app->modules->loadModules('all');
170     //** Load the plugins that are in the plugins-enabled folder
171     $app->plugins->loadPlugins('all');
ab6e69 172     
MC 173     $app->plugins->raiseAction('server_plugins_loaded', '');
174     
eed6b3 175     if ($tmp_num_records > 0) {
d2d3b9 176         $app->log("Found $tmp_num_records changes, starting update process.", LOGLEVEL_DEBUG);
5a43e7 177         //** Go through the sys_datalog table and call the processing functions
T 178         //** from the modules that are hooked on to the table actions
d2d3b9 179         $app->modules->processDatalog();
T 180     }
5a43e7 181     //** Process actions from sys_remoteaction table
T 182     $app->modules->processActions();
183     //** Restart services that need to after configuration
184     $app->services->processDelayedActions();
185     //** All modules are already loaded and processed, so there is NO NEED to load the core once again...
186     $needStartCore = false;
7fe908 187
32b40d 188 } else {
1d8f7f 189     if ($app->db->connect->connect_error == NULL) {
eed6b3 190         $app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN);
0e381b 191     } else {
eed6b3 192         $app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN);
0e381b 193     }
782b02 194 }
3d4630 195
eed6b3 196 /*
V 197  * Under normal circumstances the system was loaded and all updates are done.
198  * but if we do not have to update anything or if the database is not accessible, then we
199  * have to start the core-system (if the database is accessible, we need the core because of the
200  * monitoring. If the databse is NOT accessible, we need the core because of rescue the db...
201  */
202 if ($needStartCore) {
203     // Write the log
204     $app->log('No Updated records found, starting only the core.', LOGLEVEL_DEBUG);
205     // Load required base-classes
206     $app->uses('modules,plugins,file,services');
207     // Load the modules that are im the mods-core folder
208     $app->modules->loadModules('core');
cc6568 209     // Load the plugins that are in the f folder
H 210     //$app->plugins->loadPlugins('core');
eed6b3 211 }
V 212
213
87a4a6 214 // Remove lock
eed6b3 215 @unlink($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
V 216 $app->log('Remove Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 217
32b40d 218
ba747c 219 die("finished.\n");
c30aaf 220 ?>