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