Fixed regresion in inst_upgrade.php
[fa-stable.git] / admin / inst_upgrade.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_SOFTWAREUPGRADE';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Software Upgrade"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/company_db.inc");
20 include_once($path_to_root . "/admin/db/maintenance_db.inc");
21 include_once($path_to_root . "/includes/ui.inc");
22
23 //
24 //      Creates table of installer objects sorted by version.
25 //
26 function get_installers()
27 {
28         global $path_to_root;
29
30         $patchdir = $path_to_root."/sql/";
31         $upgrades = array();    
32         $datadir = @opendir($patchdir);
33
34         if ($datadir)
35         {
36                 while(false !== ($fname = readdir($datadir)))
37                 { // check all php files but index.php
38                         if (!is_dir($patchdir . $fname) && ($fname != 'index.php')
39                                 && stristr($fname, '.php') != false && $fname[0] != '.')
40                         {
41                                 unset($install);
42                                 include_once($patchdir . $fname);
43                                 if (isset($install)) // add installer if found
44                                         $upgrades[$install->version] =  $install;
45                         }
46                 }
47                 ksort($upgrades); // sort by file name
48                 $upgrades = array_values($upgrades);
49         }
50         return $upgrades;
51 }
52 //
53 //      Apply one differential data set.
54 //
55 function upgrade_step($inst, $company, $conn, $force) 
56 {
57         global $path_to_root;
58
59         $pref = $conn['tbpref'];
60         $ret = true;
61
62                 $state = $inst->installed($pref);
63                 if (!$state || $force) 
64                 {
65                         if (!$inst->pre_check($pref, $force)) return false;
66                         $sql = $inst->sql;
67
68                         error_log(sprintf(_("Database upgrade for company '%s' (%s:%s*) started..."),
69                                 $conn['name'], $conn['dbname'], $conn['tbpref']));
70
71                         if ($sql != '')
72                                 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force, true);
73
74                         $ret &= $inst->install($company, $force);
75
76                         if (!$ret && is_callable(array($inst, 'post_fail')))
77                                 $inst->post_fail($pref);
78
79                         error_log(_("Database upgrade finished."));
80                 } else
81                         if ($state!==true) {
82                                 display_error(_("Upgrade cannot be done because database has been already partially upgraded. Please downgrade database to clean previous version or try forced upgrade."));
83                                 $ret = false;
84                         }
85         return $ret;
86 }
87
88 $installers = get_installers();
89
90 if (get_post('Upgrade')) 
91 {
92
93         $ret = true;
94         foreach ($db_connections as $comp => $conn) 
95         {
96         // connect to database
97                 if (!(set_global_connection($comp))) 
98                 {
99                         display_error(_("Cannot connect to database for company")
100                                 ." '".$conn['name']."'");
101                         continue;
102                 }
103         // create security backup       
104                 db_backup($conn, 'no', 'Security backup before upgrade');
105         // apply all upgrade data
106                 foreach ($installers as $i => $inst) 
107                 {
108
109                         $force = get_post('force_'.$i);
110                         if ($force || get_post('install_'.$i)) 
111                                 $ret = upgrade_step($installers[$i], $comp, $conn, $force);
112
113                         if (!$ret)
114                         {
115                                 display_error(
116                                 sprintf(_("Database upgrade to version %s failed for company '%s'."),
117                                         $inst->version, $conn['name'])
118                                         .'<br>'
119                                         ._('You should restore company database from latest backup file'));
120                         }
121                 }
122 //              db_close($conn); ?
123                 if (!$ret) break;
124         }
125         set_global_connection();
126         if($ret)
127         {       // re-read the prefs
128                 global $path_to_root;
129                 include_once($path_to_root . "/admin/db/users_db.inc");
130                 $user = get_user_by_login($_SESSION["wa_current_user"]->username);
131                 $_SESSION["wa_current_user"]->prefs = new user_prefs($user);
132                 display_notification(_('All companies data has been successfully updated'));
133         }       
134         refresh_sys_prefs(); // re-read system setup
135         $Ajax->activate('_page_body');
136 }
137
138 start_form();
139 start_table(TABLESTYLE);
140 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
141         _("Force upgrade"));
142 table_header($th);
143
144 $k = 0; //row colour counter
145 $partial = 0;
146 foreach($installers as $i => $inst)
147 {
148         alt_table_row_color($k);
149         start_row();
150         label_cell($inst->version);
151         label_cell($inst->description);
152         label_cell($inst->sql ? $inst->sql : '<i>'._('None').'</i>', 'align=center');
153 // this is checked only for first (site admin) company, 
154 // but in fact we should always upgrade all data sets after
155 // source upgrade.
156         $check = $inst->installed(TB_PREF);
157         if ($check === true)
158                 label_cell(_("Installed"));
159         else 
160                 if (!$check)
161                         check_cells(null,'install_'.$i, 0);
162                 else {
163                         label_cell("<span class=redfg>"
164                                 . sprintf(_("Partially installed (%s)"), $check) . "</span>");
165                         $partial++;
166                 }
167
168         check_cells(null,'force_'.$i, 0);
169         end_row();
170 }
171 end_table(1);
172 if ($partial!=0)        {
173         display_note(_("Database upgrades marked as partially installed cannot be installed automatically.
174 You have to clean database manually to enable them, or try to perform forced upgrade."));
175         br();
176 }
177 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');
178 end_form();
179
180 end_page();
181