bd9e1b4ba4f6a610b639e52944ed548177a06e91
[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 //      Checks $field existence in $table with given field $properties
25 //      $table - table name without prefix
26 //  $field -  optional field name
27 //  $properties - optional properties of field defined by MySQL:
28 //              'Type', 'Null', 'Key', 'Default', 'Extra'
29 //
30 function check_table($pref, $table, $field=null, $properties=null)
31 {
32         $tables = @db_query("SHOW TABLES LIKE '".$pref.$table."'");
33         if (!db_num_rows($tables))
34                 return 1;               // no such table or error
35
36         $fields = @db_query("SHOW COLUMNS FROM ".$pref.$table);
37         if (!isset($field)) 
38                 return 0;               // table exists
39
40         while( $row = db_fetch_assoc($fields)) 
41         {
42                 if ($row['Field'] == $field) 
43                 {
44                         if (!isset($properties)) 
45                                 return 0;
46                         foreach($properties as $property => $value) 
47                         {
48                                 if ($row[$property] != $value) 
49                                         return 3;       // failed type/length check
50                         }
51                         return 0; // property check ok.
52                 }
53         }
54         return 2; // field not found
55 }
56 //
57 //      Creates table of installer objects sorted by version.
58 //
59 function get_installers()
60 {
61         global $path_to_root;
62
63         $patchdir = $path_to_root."/sql/";
64         $upgrades = array();    
65         $datadir = @opendir($patchdir);
66
67         if ($datadir)
68         {
69                 while(false !== ($fname = readdir($datadir)))
70                 { // check all php files but index.php
71                         if (!is_dir($patchdir . $fname) && ($fname != 'index.php')
72                                 && stristr($fname, '.php') != false && $fname[0] != '.')
73                         {
74                                 unset($install);
75                                 include_once($patchdir . $fname);
76                                 if (isset($install)) // add installer if found
77                                         $upgrades[$install->version] =  $install;
78                         }
79                 }
80                 ksort($upgrades); // sort by file name
81                 $upgrades = array_values($upgrades);
82         }
83         return $upgrades;
84 }
85 //
86 //      Apply one differential data set.
87 //
88 function upgrade_step($index, $conn) 
89 {
90         global $path_to_root, $installers;
91
92         $inst = $installers[$index];
93         $pref = $conn['tbpref'];
94         $ret = true;
95
96         $force = get_post('force_'.$index);
97         if ($force || get_post('install_'.$index)) 
98         {
99                 $state = $inst->installed($pref);
100                 if (!$state || $force) 
101                 {
102                         if (!$inst->pre_check($pref, $force)) return false;
103                         $sql = $inst->sql;
104
105                         error_log(sprintf(_("Database upgrade for company '%s' (%s:%s*) started..."),
106                                 $conn['name'], $conn['dbname'], $conn['tbpref']));
107                                 
108                         if ($sql != '')
109                                 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
110
111                         $ret &= $inst->install($pref, $force);
112
113                         error_log(_("Database upgrade finished."));
114
115                 } else
116                         if ($state!==true) {
117                                 display_error(_("Upgrade cannot be done because database has been already partially upgraded. Please downgrade database to clean previous version or try forced upgrade."));
118                                 $ret = false;
119                         }
120         }
121         return $ret;
122 }
123
124 $installers = get_installers();
125
126 if (get_post('Upgrade')) 
127 {
128
129         $ret = true;
130         foreach ($db_connections as $comp => $conn) 
131         {
132         // connect to database
133                 if (!(set_global_connection($comp))) 
134                 {
135                         display_error(_("Cannot connect to database for company")
136                                 ." '".$conn['name']."'");
137                         continue;
138                 }
139         // create security backup       
140                 db_backup($conn, 'no', 'Security backup before upgrade', $conn['tbpref']);
141         // apply all upgrade data
142                 foreach ($installers as $i => $inst) 
143                 {
144                         $ret = upgrade_step($i, $conn);
145                         if (!$ret)
146                                 display_error(
147                                 sprintf(_("Database upgrade to version %s failed for company '%s'."),
148                                         $inst->version, $conn['name'])
149                                         .'<br>'
150                                         ._('You should restore company database from latest backup file'));
151                 }
152 //              db_close($conn); ?
153                 if (!$ret) break;
154         }
155         set_global_connection();
156         if($ret)
157         {       // re-read the prefs
158                 global $path_to_root;
159                 include_once($path_to_root . "/admin/db/users_db.inc");
160                 $user = get_user_by_login($_SESSION["wa_current_user"]->username);
161                 $_SESSION["wa_current_user"]->prefs = new user_prefs($user);
162                 display_notification(_('All companies data has been successfully updated'));
163         }       
164         unset($_SESSION['SysPrefs']); // re-read system setup
165         $_SESSION['SysPrefs'] = new sys_prefs();
166         $Ajax->activate('_page_body');
167 }
168
169 start_form();
170 start_table(TABLESTYLE);
171 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
172         _("Force upgrade"));
173 table_header($th);
174
175 $k = 0; //row colour counter
176 $partial = 0;
177 foreach($installers as $i => $inst)
178 {
179         alt_table_row_color($k);
180         start_row();
181         label_cell($inst->version);
182         label_cell($inst->description);
183         label_cell($inst->sql ? $inst->sql : '<i>'._('None').'</i>', 'align=center');
184 // this is checked only for first (site admin) company, 
185 // but in fact we should always upgrade all data sets after
186 // source upgrade.
187         $check = $inst->installed(TB_PREF);
188         if ($check === true)
189                 label_cell(_("Installed"));
190         else 
191                 if (!$check)
192                         check_cells(null,'install_'.$i, 0);
193                 else {
194                         label_cell("<span class=redfg>"
195                                 . sprintf(_("Partially installed (%s)"), $check) . "</span>");
196                         $partial++;
197                 }
198
199         check_cells(null,'force_'.$i, 0);
200         end_row();
201 }
202 end_table(1);
203 if ($partial!=0)        {
204         display_note(_("Database upgrades marked as partially installed cannot be installed automatically.
205 You have to clean database manually to enable them, or try to perform forced upgrade."));
206         br();
207 }
208 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');
209 end_form();
210
211 end_page();
212
213 ?>