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