tbrehm
2012-04-05 b2866973a4e79c99f7bc5e9a77c12cee9229babf
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'] == '') {
b28669 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 {
b28669 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
b28669 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
b28669 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
1c6b26 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"]) ) {
b28669 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 {
b28669 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 .. ";
b28669 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
c2a1c7 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"]) ) {
b28669 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 {
b28669 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']);
bed0eb 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"]) ) {
b28669 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 {
b28669 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']);
bed0eb 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"]) ) {
b28669 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 {
b28669 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
5cff6b 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'];
250     $tpl_ini_array['server']['hostname'] = $conf['hostname'];
251     $tpl_ini_array['server']['ip_address'] = @gethostbyname($conf['hostname']);
252     $tpl_ini_array['web']['website_basedir'] = $conf['web']['website_basedir'];
253     $tpl_ini_array['web']['website_path'] = $conf['web']['website_path'];
254     $tpl_ini_array['web']['website_symlinks'] = $conf['web']['website_symlinks'];
255     $tpl_ini_array['cron']['crontab_dir'] = $conf['cron']['crontab_dir'];
256     $tpl_ini_array['web']['security_level'] = 20;
257     $tpl_ini_array['web']['user'] = $conf['apache']['user'];
258     $tpl_ini_array['web']['group'] = $conf['apache']['group'];
259     $tpl_ini_array['web']['php_ini_path_apache'] = $conf['apache']['php_ini_path_apache'];
260     $tpl_ini_array['web']['php_ini_path_cgi'] = $conf['apache']['php_ini_path_cgi'];
261     $tpl_ini_array['mail']['pop3_imap_daemon'] = ($conf['dovecot']['installed'] == true)?'dovecot':'courier';
262     $tpl_ini_array['mail']['mail_filter_syntax'] = ($conf['dovecot']['installed'] == true)?'sieve':'maildrop';
263     $tpl_ini_array['dns']['bind_user'] = $conf['bind']['bind_user'];
264     $tpl_ini_array['dns']['bind_group'] = $conf['bind']['bind_group'];
265     $tpl_ini_array['dns']['bind_zonefiles_dir'] = $conf['bind']['bind_zonefiles_dir'];
266     $tpl_ini_array['dns']['named_conf_path'] = $conf['bind']['named_conf_path'];
267     $tpl_ini_array['dns']['named_conf_local_path'] = $conf['bind']['named_conf_local_path'];
4ffb51 268     
dba68f 269     $tpl_ini_array['web']['nginx_vhost_conf_dir'] = $conf['nginx']['vhost_conf_dir'];
T 270     $tpl_ini_array['web']['nginx_vhost_conf_enabled_dir'] = $conf['nginx']['vhost_conf_enabled_dir'];
271     $tpl_ini_array['web']['nginx_user'] = $conf['nginx']['user'];
272     $tpl_ini_array['web']['nginx_group'] = $conf['nginx']['group'];
273     $tpl_ini_array['web']['nginx_cgi_socket'] = $conf['nginx']['cgi_socket'];
274     $tpl_ini_array['web']['php_fpm_init_script'] = $conf['nginx']['php_fpm_init_script'];
275     $tpl_ini_array['web']['php_fpm_ini_path'] = $conf['nginx']['php_fpm_ini_path'];
276     $tpl_ini_array['web']['php_fpm_pool_dir'] = $conf['nginx']['php_fpm_pool_dir'];
277     $tpl_ini_array['web']['php_fpm_start_port'] = $conf['nginx']['php_fpm_start_port'];
278     $tpl_ini_array['web']['php_fpm_socket_dir'] = $conf['nginx']['php_fpm_socket_dir'];
279         
4ffb51 280     if ($conf['nginx']['installed'] == true) {
F 281         $tpl_ini_array['web']['server_type'] = 'nginx';
282         $tpl_ini_array['global']['webserver'] = 'nginx';
283     }
9a3a78 284
bed0eb 285     //* update the new template with the old values
9a3a78 286     if(is_array($old_ini_array)) {
L 287         foreach($old_ini_array as $tmp_section_name => $tmp_section_content) {
288             foreach($tmp_section_content as $tmp_var_name => $tmp_var_content) {
289                 $tpl_ini_array[$tmp_section_name][$tmp_var_name] = $tmp_var_content;
290             }
291         }
292     }
293
294     $new_ini = array_to_ini($tpl_ini_array);
5cff6b 295     $sql = "UPDATE ".$conf["mysql"]["database"].".server SET config = '".mysql_real_escape_string($new_ini)."' WHERE server_id = ".$conf['server_id'];
5f221a 296     $inst->db->query($sql);
bed0eb 297     
T 298     if($inst->db->dbHost != $inst->dbmaster->dbHost) {
299         $sql = "UPDATE ".$conf["mysql"]["master_database"].".server SET config = '".mysql_real_escape_string($new_ini)."' WHERE server_id = ".$conf['server_id'];
300         $inst->dbmaster->query($sql);
301     }
9a3a78 302     unset($old_ini_array);
L 303     unset($tpl_ini_array);
304     unset($new_ini);
305
306
307     //** Update system ini
5cff6b 308     $tmp_server_rec = $inst->db->queryOneRecord("SELECT config FROM ".$conf["mysql"]["database"].".sys_ini WHERE sysini_id = 1");
9a3a78 309     $old_ini_array = ini_to_array(stripslashes($tmp_server_rec['config']));
L 310     unset($tmp_server_rec);
311     $tpl_ini_array = ini_to_array(rf('tpl/system.ini.master'));
312
313     // update the new template with the old values
314     if(is_array($old_ini_array)) {
315         foreach($old_ini_array as $tmp_section_name => $tmp_section_content) {
316             foreach($tmp_section_content as $tmp_var_name => $tmp_var_content) {
317                 $tpl_ini_array[$tmp_section_name][$tmp_var_name] = $tmp_var_content;
318             }
319         }
320     }
321
322     $new_ini = array_to_ini($tpl_ini_array);
5cff6b 323     $tmp = $inst->db->queryOneRecord('SELECT count(sysini_id) as number FROM '.$conf["mysql"]["database"].'.sys_ini WHERE 1');
9a3a78 324     if($tmp['number'] == 0) {
5cff6b 325         $inst->db->query("INSERT INTO ".$conf["mysql"]["database"].".sys_ini (sysini_id, config) VALUES (1,'".mysql_real_escape_string($new_ini)."')");
9a3a78 326     } else {
5cff6b 327         $inst->db->query("UPDATE ".$conf["mysql"]["database"].".sys_ini SET config = '".mysql_real_escape_string($new_ini)."' WHERE sysini_id = 1");
9a3a78 328     }
L 329     unset($old_ini_array);
330     unset($tpl_ini_array);
331     unset($new_ini);
332 }
333
334 ?>