tbrehm
2013-02-22 526b997c9891a796b152cdbab8e329b356b1f596
commit | author | age
9a3a78 1 <?php
L 2 /*
3 Copyright (c) 2007-2010, Till Brehm, projektfarm Gmbh
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
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.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
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 */
29
30 function prepareDBDump() {
31     global $conf;
32
33     //** load the pre update sql script do perform modifications on the database before the database is dumped
34     if(is_file(ISPC_INSTALL_ROOT."/install/sql/pre_update.sql")) {
35         if($conf['mysql']['admin_password'] == '') {
02bf99 36             caselog("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." ".escapeshellarg($conf['mysql']['database'])." < '".ISPC_INSTALL_ROOT."/install/sql/pre_update.sql' &> /dev/null", __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
9a3a78 37         } else {
02bf99 38             caselog("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." ".escapeshellarg($conf['mysql']['database'])." < '".ISPC_INSTALL_ROOT."/install/sql/pre_update.sql' &> /dev/null", __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
9a3a78 39         }
L 40     }
41
42     //** export the current database data
43     if( !empty($conf["mysql"]["admin_password"]) ) {
44
02bf99 45         system("mysqldump -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." -c -t --add-drop-table --create-options --quick --result-file=existing_db.sql ".$conf['mysql']['database']);
9a3a78 46     }
L 47     else {
48
02bf99 49         system("mysqldump -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -c -t --add-drop-table --create-options --quick --result-file=existing_db.sql ".$conf['mysql']['database']);
9a3a78 50     }
L 51
52     /*
53      * If we have a server with nothing in it except VE's then the database of thie server is empty.
54      * so the following line will no longer work!
55      */
56     //if(filesize('existing_db.sql') < 30000) die('Possible problem with dumping the database. We will stop here. Please check the file existing_db.sql');
57
58     // create a backup copy of the ispconfig database in the root folder
8cf78b 59     if(isset($conf['backup_path'])) {
T 60         $backup_db_name = $conf['backup_path'].'/ispconfig_db_backup.sql';
61     } else {
62         $backup_db_name = '/root/ispconfig_db_backup_'.@date('Y-m-d_H-i').'.sql';
63     }
9a3a78 64     copy('existing_db.sql',$backup_db_name);
L 65     chmod($backup_db_name, 0700);
66     chown($backup_db_name, 'root');
67     chgrp($backup_db_name, 'root');
68
69     if ($conf['powerdns']['installed']) {
70         //** export the current PowerDNS database data
71             if( !empty($conf["mysql"]["admin_password"]) ) {
02bf99 72                     system("mysqldump -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." -c -t --add-drop-table --create-options --quick --result-file=existing_powerdns_db.sql ".$conf['powerdns']['database']);
9a3a78 73             } else {
02bf99 74                     system("mysqldump -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -c -t --add-drop-table --create-options --quick --result-file=existing_powerdns_db.sql ".$conf['powerdns']['database']);
9a3a78 75             }
L 76
77         // create a backup copy of the PowerDNS database in the root folder
78         $backup_db_name = '/root/ispconfig_powerdns_db_backup_'.@date('Y-m-d_h-i').'.sql';
79             copy('existing_powerdns_db.sql',$backup_db_name);
80         chmod($backup_db_name, 0700);
81             chown($backup_db_name, 'root');
82             chgrp($backup_db_name, 'root');
83     }
84 }
85
86 function checkDbHealth() {
87     global $conf;
88
89     //* Array containing non OK tables (can be repaired, crashed, corrupt)
90     $notok = array();
91
92     echo "Checking ISPConfig database .. ";
02bf99 93     exec("mysqlcheck -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." -r ".escapeshellarg($conf["mysql"]["database"]), $result);
9a3a78 94     for( $i=0; $i<sizeof($result);$i++) {
L 95         if ( substr($result[$i], -2) != "OK" ) {
96             $notok[] = $result[$i];
97         }
98     }
99
100     if ( sizeof($notok) > 0 ) {
101         echo "\nSome tables where not 'OK'. Please check the list below.\n\n";
102         foreach ($notok as $key => $value) {
103             echo "$value\n";
104         }
105         echo "\nPress enter to continue or CTRL-C to cancel the installation ..";
106         sread();
107     }
108     else
109       echo "OK\n";
110 }
111
112 function updateDbAndIni() {
113     global $inst, $conf;
114
115     //* Update $conf array with values from the server.ini that shall be preserved
116     $tmp = $inst->db->queryOneRecord("SELECT * FROM ".$conf["mysql"]["database"].".server WHERE server_id = ".$conf['server_id']);
117     $ini_array = ini_to_array(stripslashes($tmp['config']));
118     $current_db_version = (isset($tmp['dbversion']))?intval($tmp['dbversion']):0;
119
edf806 120     if(!is_array($ini_array) or count($ini_array) == 0) die('Unable to read server configuration from database.');
9a3a78 121
L 122     $conf['services']['mail'] = ($tmp['mail_server'] == 1)?true:false;
123     $conf['services']['web'] = ($tmp['web_server'] == 1)?true:false;
124     $conf['services']['dns'] = ($tmp['dns_server'] == 1)?true:false;
125     $conf['services']['file'] = ($tmp['file_server'] == 1)?true:false;
126     $conf['services']['db'] = ($tmp['db_server'] == 1)?true:false;
127     $conf['services']['vserver'] = ($tmp['vserver_server'] == 1)?true:false;
128     $conf['services']['proxy'] = (isset($tmp['proxy_server']) && $tmp['proxy_server'] == 1)?true:false;
129     $conf['services']['firewall'] = (isset($tmp['firewall_server']) && $tmp['firewall_server'] == 1)?true:false;
130     
131     $conf['postfix']['vmail_mailbox_base'] = $ini_array['mail']['homedir_path'];
132     
e41fba 133     if(isset($ini_array['web']['server_type']) && $ini_array['web']['server_type'] != ''){
4ffb51 134         $conf['webserver']['server_type'] = $ini_array['web']['server_type'];
F 135         if($conf['webserver']['server_type'] == 'nginx'){
136             $conf['apache']['installed'] = false;
137         } else {
138             $conf['nginx']['installed'] = false;
139         }
140     } else {
141         $conf['webserver']['server_type'] = 'apache';
142         $conf['nginx']['installed'] = false;
143     }
144     
9a3a78 145     //* Do incremental DB updates only on installed ISPConfig versions > 3.0.3
L 146     if(compare_ispconfig_version('3.0.3',ISPC_APP_VERSION) >= 0) {
147         
148         swriteln($inst->lng('Starting incremental database update.'));
149         
150         //* get the version of the db schema from the server table 
151         $found = true;
152         while($found == true) {
153             $next_db_version = intval($current_db_version + 1);
154             $patch_filename = realpath(dirname(__FILE__).'/../').'/sql/incremental/upd_'.str_pad($next_db_version, 4, '0', STR_PAD_LEFT).'.sql';
155             if(is_file($patch_filename)) {
156                 //* Load patch file into database
157                 if( !empty($conf["mysql"]["admin_password"]) ) {
02bf99 158                     system("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." --force -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." ".escapeshellarg($conf['mysql']['database'])." < ".$patch_filename);
9a3a78 159                 } else {
02bf99 160                     system("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." --force -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." ".escapeshellarg($conf['mysql']['database'])." < ".$patch_filename);
9a3a78 161                 }
L 162                 swriteln($inst->lng('Loading SQL patch file').': '.$patch_filename);
163                 $current_db_version = $next_db_version;
164             } else {
165                 $found = false;
166             }
167         }
168         
169         //* update the database version in server table
170         $inst->db->query("UPDATE ".$conf["mysql"]["database"].".server SET dbversion = '".$current_db_version."' WHERE server_id = ".$conf['server_id']);
8cf78b 171         if($inst->db->dbHost != $inst->dbmaster->dbHost) $inst->dbmaster->query("UPDATE ".$conf["mysql"]["master_database"].".server SET dbversion = '".$current_db_version."' WHERE server_id = ".$conf['server_id']);
9a3a78 172         
L 173     
174     //* If ISPConfig Version < 3.0.3, we will do a full db update
175     } else {
176         
177         swriteln($inst->lng('Starting full database update.'));
178         
179         //** Delete the old database
180         if( !$inst->db->query('DROP DATABASE IF EXISTS '.$conf['mysql']['database']) ) {
181         $inst->error('Unable to drop MySQL database: '.$conf['mysql']['database'].'.');
182         }
183
184         //** Create the mysql database
185         $inst->configure_database();
186
187         //** empty all databases
188         $db_tables = $inst->db->getTables();
189
190         foreach($db_tables as $table) {
191             $inst->db->query("TRUNCATE $table");
192         }
193
194         //** load old data back into database
195         if( !empty($conf["mysql"]["admin_password"]) ) {
02bf99 196             system("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." --force -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." ".escapeshellarg($conf['mysql']['database'])." < existing_db.sql");
9a3a78 197         } else {
02bf99 198             system("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." --force -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." ".escapeshellarg($conf['mysql']['database'])." < existing_db.sql");
9a3a78 199         }
L 200         
201         //** Get the database version number based on the patchfile
202         $found = true;
203         while($found == true) {
204             $next_db_version = intval($current_db_version + 1);
205             $patch_filename = realpath(dirname(__FILE__).'/../').'/sql/incremental/upd_'.str_pad($next_db_version, 4, '0', STR_PAD_LEFT).'.sql';
206             if(is_file($patch_filename)) {
207                 $current_db_version = $next_db_version;
208             } else {
209                 $found = false;
210             }
211         }
212         
213         //* update the database version in server table
214         $inst->db->query("UPDATE ".$conf["mysql"]["database"].".server SET dbversion = '".$current_db_version."' WHERE server_id = ".$conf['server_id']);
8cf78b 215         if($inst->db->dbHost != $inst->dbmaster->dbHost) $inst->dbmaster->query("UPDATE ".$conf["mysql"]["master_database"].".server SET dbversion = '".$current_db_version."' WHERE server_id = ".$conf['server_id']);
9a3a78 216
L 217         if ($conf['powerdns']['installed']) {
218                                                  
219             swriteln($inst->lng('Starting full PowerDNS database update.'));
220
221             //** Delete the old PowerDNS database
222             if( !$inst->db->query('DROP DATABASE IF EXISTS '.$conf['powerdns']['database']) ) {
223                 $inst->error('Unable to drop MySQL database: '.$conf['powerdns']['database'].'.');
224             }
225
226             //** Create the mysql database
227             $inst->configure_powerdns();
228
229             //** load old data back into the PowerDNS database
230             if( !empty($conf["mysql"]["admin_password"]) ) {
02bf99 231                 system("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." --force -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." -p".escapeshellarg($conf['mysql']['admin_password'])." ".escapeshellarg($conf['powerdns']['database'])." < existing_powerdns_db.sql");
9a3a78 232             } else {
02bf99 233                 system("mysql --default-character-set=".escapeshellarg($conf['mysql']['charset'])." --force -h ".escapeshellarg($conf['mysql']['host'])." -u ".escapeshellarg($conf['mysql']['admin_user'])." ".escapeshellarg($conf['powerdns']['database'])." < existing_powerdns_db.sql");
9a3a78 234             }
L 235         }
236     }
237
238
239     //** Update server ini
edf806 240     $tmp_server_rec = $inst->db->queryOneRecord("SELECT config FROM ".$conf["mysql"]["database"].".server WHERE server_id = ".$conf['server_id']);
9a3a78 241     $old_ini_array = ini_to_array(stripslashes($tmp_server_rec['config']));
L 242     unset($tmp_server_rec);
243     $tpl_ini_array = ini_to_array(rf('tpl/server.ini.master'));
244     
245     //* Update further distribution specific parameters for server config here
246     //* HINT: Every line added here has to be added in installer_base.lib.php too!!
247     $tpl_ini_array['jailkit']['jailkit_chroot_app_programs'] = $conf['jailkit']['jailkit_chroot_app_programs'];
248     $tpl_ini_array['fastcgi']['fastcgi_phpini_path'] = $conf['fastcgi']['fastcgi_phpini_path'];
249     $tpl_ini_array['fastcgi']['fastcgi_starter_path'] = $conf['fastcgi']['fastcgi_starter_path'];
526b99 250     $tpl_ini_array['fastcgi']['fastcgi_bin'] = $conf['fastcgi']['fastcgi_bin'];
9a3a78 251     $tpl_ini_array['server']['hostname'] = $conf['hostname'];
L 252     $tpl_ini_array['server']['ip_address'] = @gethostbyname($conf['hostname']);
253     $tpl_ini_array['web']['website_basedir'] = $conf['web']['website_basedir'];
254     $tpl_ini_array['web']['website_path'] = $conf['web']['website_path'];
255     $tpl_ini_array['web']['website_symlinks'] = $conf['web']['website_symlinks'];
256     $tpl_ini_array['cron']['crontab_dir'] = $conf['cron']['crontab_dir'];
257     $tpl_ini_array['web']['security_level'] = 20;
258     $tpl_ini_array['web']['user'] = $conf['apache']['user'];
259     $tpl_ini_array['web']['group'] = $conf['apache']['group'];
260     $tpl_ini_array['web']['php_ini_path_apache'] = $conf['apache']['php_ini_path_apache'];
261     $tpl_ini_array['web']['php_ini_path_cgi'] = $conf['apache']['php_ini_path_cgi'];
262     $tpl_ini_array['mail']['pop3_imap_daemon'] = ($conf['dovecot']['installed'] == true)?'dovecot':'courier';
263     $tpl_ini_array['mail']['mail_filter_syntax'] = ($conf['dovecot']['installed'] == true)?'sieve':'maildrop';
264     $tpl_ini_array['dns']['bind_user'] = $conf['bind']['bind_user'];
265     $tpl_ini_array['dns']['bind_group'] = $conf['bind']['bind_group'];
266     $tpl_ini_array['dns']['bind_zonefiles_dir'] = $conf['bind']['bind_zonefiles_dir'];
267     $tpl_ini_array['dns']['named_conf_path'] = $conf['bind']['named_conf_path'];
268     $tpl_ini_array['dns']['named_conf_local_path'] = $conf['bind']['named_conf_local_path'];
4ffb51 269     
dba68f 270     $tpl_ini_array['web']['nginx_vhost_conf_dir'] = $conf['nginx']['vhost_conf_dir'];
T 271     $tpl_ini_array['web']['nginx_vhost_conf_enabled_dir'] = $conf['nginx']['vhost_conf_enabled_dir'];
272     $tpl_ini_array['web']['nginx_user'] = $conf['nginx']['user'];
273     $tpl_ini_array['web']['nginx_group'] = $conf['nginx']['group'];
274     $tpl_ini_array['web']['nginx_cgi_socket'] = $conf['nginx']['cgi_socket'];
275     $tpl_ini_array['web']['php_fpm_init_script'] = $conf['nginx']['php_fpm_init_script'];
276     $tpl_ini_array['web']['php_fpm_ini_path'] = $conf['nginx']['php_fpm_ini_path'];
277     $tpl_ini_array['web']['php_fpm_pool_dir'] = $conf['nginx']['php_fpm_pool_dir'];
278     $tpl_ini_array['web']['php_fpm_start_port'] = $conf['nginx']['php_fpm_start_port'];
279     $tpl_ini_array['web']['php_fpm_socket_dir'] = $conf['nginx']['php_fpm_socket_dir'];
280         
4ffb51 281     if ($conf['nginx']['installed'] == true) {
F 282         $tpl_ini_array['web']['server_type'] = 'nginx';
283         $tpl_ini_array['global']['webserver'] = 'nginx';
284     }
9a3a78 285
8cf78b 286     //* update the new template with the old values
9a3a78 287     if(is_array($old_ini_array)) {
L 288         foreach($old_ini_array as $tmp_section_name => $tmp_section_content) {
289             foreach($tmp_section_content as $tmp_var_name => $tmp_var_content) {
290                 $tpl_ini_array[$tmp_section_name][$tmp_var_name] = $tmp_var_content;
291             }
292         }
293     }
294
295     $new_ini = array_to_ini($tpl_ini_array);
edf806 296     $sql = "UPDATE ".$conf["mysql"]["database"].".server SET config = '".mysql_real_escape_string($new_ini)."' WHERE server_id = ".$conf['server_id'];
5f221a 297     $inst->db->query($sql);
8cf78b 298     
T 299     if($inst->db->dbHost != $inst->dbmaster->dbHost) {
300         $sql = "UPDATE ".$conf["mysql"]["master_database"].".server SET config = '".mysql_real_escape_string($new_ini)."' WHERE server_id = ".$conf['server_id'];
301         $inst->dbmaster->query($sql);
302     }
9a3a78 303     unset($old_ini_array);
L 304     unset($tpl_ini_array);
305     unset($new_ini);
306
307
308     //** Update system ini
edf806 309     $tmp_server_rec = $inst->db->queryOneRecord("SELECT config FROM ".$conf["mysql"]["database"].".sys_ini WHERE sysini_id = 1");
9a3a78 310     $old_ini_array = ini_to_array(stripslashes($tmp_server_rec['config']));
L 311     unset($tmp_server_rec);
312     $tpl_ini_array = ini_to_array(rf('tpl/system.ini.master'));
313
314     // update the new template with the old values
315     if(is_array($old_ini_array)) {
316         foreach($old_ini_array as $tmp_section_name => $tmp_section_content) {
317             foreach($tmp_section_content as $tmp_var_name => $tmp_var_content) {
318                 $tpl_ini_array[$tmp_section_name][$tmp_var_name] = $tmp_var_content;
319             }
320         }
321     }
322
323     $new_ini = array_to_ini($tpl_ini_array);
edf806 324     $tmp = $inst->db->queryOneRecord('SELECT count(sysini_id) as number FROM '.$conf["mysql"]["database"].'.sys_ini WHERE 1');
9a3a78 325     if($tmp['number'] == 0) {
edf806 326         $inst->db->query("INSERT INTO ".$conf["mysql"]["database"].".sys_ini (sysini_id, config) VALUES (1,'".mysql_real_escape_string($new_ini)."')");
9a3a78 327     } else {
edf806 328         $inst->db->query("UPDATE ".$conf["mysql"]["database"].".sys_ini SET config = '".mysql_real_escape_string($new_ini)."' WHERE sysini_id = 1");
9a3a78 329     }
L 330     unset($old_ini_array);
331     unset($tpl_ini_array);
332     unset($new_ini);
333 }
334
335 ?>