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';
14 include_once($path_to_root . "/includes/session.inc");
16 page(_("Software Upgrade"));
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");
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'
30 function check_table($pref, $table, $field=null, $properties=null)
32 $fields = @db_query("SHOW COLUMNS FROM ".$pref.$table);
34 return 1; // no such table or error
37 return 0; // table exists
39 while( $row = db_fetch_assoc($fields))
41 if ($row['Field'] == $field)
43 if (!isset($properties))
45 foreach($properties as $property => $value)
47 if ($row[$property] != $value)
48 return 3; // failed type/length check
50 return 0; // property check ok.
53 return 2; // field not found
56 // Creates table of installer objects sorted by version.
58 function get_installers()
62 $patchdir = $path_to_root."/sql/";
64 $datadir = @opendir($patchdir);
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)
74 include_once($patchdir . $fname);
75 if (isset($install)) // add installer if found
76 $upgrades[$install->version] = $install;
79 ksort($upgrades); // sort by file name
80 $upgrades = array_values($upgrades);
85 // Apply one differential data set.
87 function upgrade_step($index, $conn)
89 global $path_to_root, $installers;
91 $inst = $installers[$index];
93 $pref = $conn['tbpref'];
96 $force = get_post('force_'.$index);
97 if ($force || get_post('install_'.$index))
99 $state = $inst->installed($pref);
100 if (!$state || $force)
102 if (!$inst->pre_check($pref)) return false;
105 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
107 $ret &= $inst->install($pref, $force);
110 display_error(_("Upgrade cannot be done because database has been already partially upgraded. Please downgrade database to clean previous version or try forced upgrade."));
117 function db_open($conn)
119 $db = mysql_connect($conn["host"] ,$conn["dbuser"], $conn["dbpassword"]);
122 if (!mysql_select_db($conn["dbname"], $db))
127 $installers = get_installers();
129 if (get_post('Upgrade'))
133 foreach ($db_connections as $conn)
135 // connect to database
136 if (!($db = db_open($conn)))
138 display_error(_("Cannot connect to database for company")
139 ." '".$conn['name']."'");
142 // create security backup
143 db_backup($conn, 'no', 'Security backup before upgrade', $conn['tbpref']);
144 // apply all upgrade data
145 foreach ($installers as $i => $inst)
147 $ret = upgrade_step($i, $conn);
150 sprintf(_("Database upgrade to version %s failed for company '%s'."),
151 $inst->version, $conn['name'])
153 ._('You should restore company database from latest backup file'));
155 // db_close($conn); ?
159 { // re-read the prefs
160 global $path_to_root;
161 include_once($path_to_root . "/admin/db/users_db.inc");
162 $user = get_user_by_login($_SESSION["wa_current_user"]->username);
163 $_SESSION["wa_current_user"]->prefs = new user_prefs($user);
164 display_notification(_('All companies data has been successfully updated'));
166 $Ajax->activate('_page_body');
170 start_table($table_style);
171 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
175 $k = 0; //row colour counter
177 foreach($installers as $i => $inst)
179 alt_table_row_color($k);
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
187 $check = $inst->installed(TB_PREF);
189 label_cell(_("Installed"));
192 check_cells(null,'install_'.$i, 0);
194 label_cell("<span class=redfg>"
195 . sprintf(_("Partially installed (%s)"), $check) . "</span>");
199 check_cells(null,'force_'.$i, 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."));
208 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');