Fixed many issues in output HTML code according to HTML 4.01 Transitional format.
[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($index, $conn) 
56 {
57         global $path_to_root, $installers;
58
59         $inst = $installers[$index];
60         $pref = $conn['tbpref'];
61         $ret = true;
62
63         $force = get_post('force_'.$index);
64         if ($force || get_post('install_'.$index)) 
65         {
66                 $state = $inst->installed($pref);
67                 if (!$state || $force) 
68                 {
69                         if (!$inst->pre_check($pref, $force)) return false;
70                         $sql = $inst->sql;
71
72                         error_log(sprintf(_("Database upgrade for company '%s' (%s:%s*) started..."),
73                                 $conn['name'], $conn['dbname'], $conn['tbpref']));
74                                 
75                         if ($sql != '')
76                                 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
77
78                         $ret &= $inst->install($pref, $force);
79
80                         error_log(_("Database upgrade finished."));
81
82                 } else
83                         if ($state!==true) {
84                                 display_error(_("Upgrade cannot be done because database has been already partially upgraded. Please downgrade database to clean previous version or try forced upgrade."));
85                                 $ret = false;
86                         }
87         }
88         return $ret;
89 }
90
91 $installers = get_installers();
92
93 if (get_post('Upgrade')) 
94 {
95
96         $ret = true;
97         foreach ($db_connections as $comp => $conn) 
98         {
99         // connect to database
100                 if (!(set_global_connection($comp))) 
101                 {
102                         display_error(_("Cannot connect to database for company")
103                                 ." '".$conn['name']."'");
104                         continue;
105                 }
106         // create security backup       
107                 db_backup($conn, 'no', 'Security backup before upgrade', $conn['tbpref']);
108         // apply all upgrade data
109                 foreach ($installers as $i => $inst) 
110                 {
111                         $ret = upgrade_step($i, $conn);
112                         if (!$ret)
113                                 display_error(
114                                 sprintf(_("Database upgrade to version %s failed for company '%s'."),
115                                         $inst->version, $conn['name'])
116                                         .'<br>'
117                                         ._('You should restore company database from latest backup file'));
118                 }
119 //              db_close($conn); ?
120                 if (!$ret) break;
121         }
122         set_global_connection();
123         if($ret)
124         {       // re-read the prefs
125                 global $path_to_root;
126                 include_once($path_to_root . "/admin/db/users_db.inc");
127                 $user = get_user_by_login($_SESSION["wa_current_user"]->username);
128                 $_SESSION["wa_current_user"]->prefs = new user_prefs($user);
129                 display_notification(_('All companies data has been successfully updated'));
130         }       
131         refresh_sys_prefs(); // re-read system setup
132         $Ajax->activate('_page_body');
133 }
134
135 start_form();
136 start_table(TABLESTYLE);
137 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
138         _("Force upgrade"));
139 table_header($th);
140
141 $k = 0; //row colour counter
142 $partial = 0;
143 foreach($installers as $i => $inst)
144 {
145         alt_table_row_color($k);
146         start_row();
147         label_cell($inst->version);
148         label_cell($inst->description);
149         label_cell($inst->sql ? $inst->sql : '<i>'._('None').'</i>', 'align=center');
150 // this is checked only for first (site admin) company, 
151 // but in fact we should always upgrade all data sets after
152 // source upgrade.
153         $check = $inst->installed(TB_PREF);
154         if ($check === true)
155                 label_cell(_("Installed"));
156         else 
157                 if (!$check)
158                         check_cells(null,'install_'.$i, 0);
159                 else {
160                         label_cell("<span class='redfg'>"
161                                 . sprintf(_("Partially installed (%s)"), $check) . "</span>");
162                         $partial++;
163                 }
164
165         check_cells(null,'force_'.$i, 0);
166         end_row();
167 }
168 end_table(1);
169 if ($partial!=0)        {
170         display_note(_("Database upgrades marked as partially installed cannot be installed automatically.
171 You have to clean database manually to enable them, or try to perform forced upgrade."));
172         br();
173 }
174 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');
175 end_form();
176
177 end_page();
178
179 ?>