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