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(_($help_context = "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 // Creates table of installer objects sorted by version.
26 function get_installers()
30 $patchdir = $path_to_root."/sql/";
32 $datadir = @opendir($patchdir);
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] != '.')
42 include_once($patchdir . $fname);
43 if (isset($install)) // add installer if found
44 $upgrades[$install->version] = $install;
47 ksort($upgrades); // sort by file name
48 $upgrades = array_values($upgrades);
53 // Apply one differential data set.
55 function upgrade_step($index, $conn)
57 global $path_to_root, $installers;
59 $inst = $installers[$index];
60 $pref = $conn['tbpref'];
63 $force = get_post('force_'.$index);
64 if ($force || get_post('install_'.$index))
66 $state = $inst->installed($pref);
67 if (!$state || $force)
69 if (!$inst->pre_check($pref, $force)) return false;
72 error_log(sprintf(_("Database upgrade for company '%s' (%s:%s*) started..."),
73 $conn['name'], $conn['dbname'], $conn['tbpref']));
76 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
78 $ret &= $inst->install($pref, $force);
80 error_log(_("Database upgrade finished."));
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."));
91 $installers = get_installers();
93 if (get_post('Upgrade'))
97 foreach ($db_connections as $comp => $conn)
99 // connect to database
100 if (!(set_global_connection($comp)))
102 display_error(_("Cannot connect to database for company")
103 ." '".$conn['name']."'");
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)
111 $ret = upgrade_step($i, $conn);
114 sprintf(_("Database upgrade to version %s failed for company '%s'."),
115 $inst->version, $conn['name'])
117 ._('You should restore company database from latest backup file'));
119 // db_close($conn); ?
122 set_global_connection();
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'));
131 refresh_sys_prefs(); // re-read system setup
132 $Ajax->activate('_page_body');
136 start_table(TABLESTYLE);
137 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
141 $k = 0; //row colour counter
143 foreach($installers as $i => $inst)
145 alt_table_row_color($k);
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
153 $check = $inst->installed(TB_PREF);
155 label_cell(_("Installed"));
158 check_cells(null,'install_'.$i, 0);
160 label_cell("<span class=redfg>"
161 . sprintf(_("Partially installed (%s)"), $check) . "</span>");
165 check_cells(null,'force_'.$i, 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."));
174 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');