Quick Search:

View

Revision:

Diff

Diff from 978 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/tags/oscommerce-3.0a3/admin/backup.php

Annotated File View

hpdl
1
1 <?php
2 /*
mattice
151
3   $Id: backup.php 814 2006-08-27 15:28:23Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
410
8   Copyright (c) 2006 osCommerce
hpdl
1
9
10   Released under the GNU General Public License
11 */
12
13   require('includes/application_top.php');
14
15   $action = (isset($_GET['action']) ? $_GET['action'] : '');
16
17   if (!empty($action)) {
18     switch ($action) {
19       case 'forget':
20         $Qcfg = $osC_Database->query('delete from :table_configuration where configuration_key = :configuration_key');
21         $Qcfg->bindTable(':table_configuration', TABLE_CONFIGURATION);
22         $Qcfg->bindValue(':configuration_key', 'DB_LAST_RESTORE');
23         $Qcfg->execute();
24
25         if ($Qcfg->affectedRows()) {
26           $osC_Cache->clear('configuration');
27         }
28
29         $osC_MessageStack->add_session('header', SUCCESS_LAST_RESTORE_CLEARED, 'success');
30
hpdl
758
31         osc_redirect(osc_href_link_admin(FILENAME_BACKUP));
hpdl
1
32         break;
33       case 'backupnow':
hpdl
758
34         osc_set_time_limit(0);
hpdl
1
35
36         $backup_file = 'db_' . DB_DATABASE . '-' . date('YmdHis') . '.sql';
37         $fp = fopen(DIR_FS_BACKUP . $backup_file, 'w');
38
39         $schema = '# osCommerce, Open Source E-Commerce Solutions' . "\n" .
40                   '# http://www.oscommerce.com' . "\n" .
41                   '#' . "\n" .
42                   '# Database Backup For ' . STORE_NAME . "\n" .
43                   '# Copyright (c) ' . date('Y') . ' ' . STORE_OWNER . "\n" .
44                   '#' . "\n" .
45                   '# Database: ' . DB_DATABASE . "\n" .
46                   '# Database Server: ' . DB_SERVER . "\n" .
47                   '#' . "\n" .
hpdl
758
48                   '# Backup Date: ' . osC_DateTime::getShort(null, true) . "\n\n";
hpdl
1
49         fputs($fp, $schema);
50
51         $Qtables = $osC_Database->query('show tables');
52         while ($Qtables->next()) {
hpdl
775
53           $table = $Qtables->value('Tables_in_' . DB_DATABASE);
hpdl
1
54
55           $schema = 'drop table if exists ' . $table . ';' . "\n" .
56                     'create table ' . $table . ' (' . "\n";
57
58           $table_list = array();
59
60           $Qfields = $osC_Database->query('show fields from :table');
61           $Qfields->bindTable(':table', $table);
62           $Qfields->execute();
63
64           while ($Qfields->next()) {
65             $table_list[] = $Qfields->value('Field');
66
67             $schema .= '  ' . $Qfields->value('Field') . ' ' . $Qfields->value('Type');
68
69             if (strlen($Qfields->value('Default')) > 0) $schema .= ' default \'' . $Qfields->value('Default') . '\'';
70
71             if ($Qfields->value('Null') != 'YES') $schema .= ' not null';
72
73             if (strlen($Qfields->value('Extra')) > 0) $schema .= ' ' . $Qfields->value('Extra');
74
75             $schema .= ',' . "\n";
76           }
77
78           $schema = ereg_replace(",\n$", '', $schema);
79
80 // add the keys
81           $index = array();
82
83           $Qkeys = $osC_Database->query('show keys from :table');
84           $Qkeys->bindTable(':table', $table);
85           $Qkeys->execute();
86
87           while ($Qkeys->next()) {
88             $kname = $Qkeys->value('Key_name');
89
90             if (!isset($index[$kname])) {
91               $index[$kname] = array('unique' => !$Qkeys->value('Non_unique'),
92                                      'columns' => array());
93             }
94
95             $index[$kname]['columns'][] = $Qkeys->value('Column_name');
96           }
97
98           while (list($kname, $info) = each($index)) {
99             $schema .= ',' . "\n";
100
101             $columns = implode($info['columns'], ', ');
102
103             if ($kname == 'PRIMARY') {
104               $schema .= '  PRIMARY KEY (' . $columns . ')';
105             } elseif ($info['unique']) {
106               $schema .= '  UNIQUE ' . $kname . ' (' . $columns . ')';
107             } else {
108               $schema .= '  KEY ' . $kname . ' (' . $columns . ')';
109             }
110           }
111
112           $schema .= "\n" . ');' . "\n\n";
113           fputs($fp, $schema);
114
115 // dump the data
116           $Qrows = $osC_Database->query('select :columns from :table');
117           $Qrows->bindRaw(':columns', implode(', ', $table_list));
118           $Qrows->bindTable(':table', $table);
119           $Qrows->execute();
120
121           while ($Qrows->next()) {
122             $rows = $Qrows->toArray();
123
124             $schema = 'insert into ' . $table . ' (' . implode(', ', $table_list) . ') values (';
125
126             reset($table_list);
127             while (list(,$i) = each($table_list)) {
128               if (!isset($rows[$i])) {
129                 $schema .= 'NULL, ';
130               } elseif (strlen($rows[$i]) > 0) {
131                 $row = addslashes($rows[$i]);
132                 $row = ereg_replace("\n#", "\n".'\#', $row);
133
134                 $schema .= '\'' . $row . '\', ';
135               } else {
136                 $schema .= '\'\', ';
137               }
138             }
139
140             $schema = ereg_replace(', $', '', $schema) . ');' . "\n";
141             fputs($fp, $schema);
142           }
143         }
144
145         fclose($fp);
146
147         if (isset($_POST['download']) && ($_POST['download'] == 'yes')) {
148           switch ($_POST['compress']) {
149             case 'gzip':
150               exec(LOCAL_EXE_GZIP . ' ' . DIR_FS_BACKUP . $backup_file);
151               $backup_file .= '.gz';
152               break;
153             case 'zip':
154               exec(LOCAL_EXE_ZIP . ' -j ' . DIR_FS_BACKUP . $backup_file . '.zip ' . DIR_FS_BACKUP . $backup_file);
155               unlink(DIR_FS_BACKUP . $backup_file);
156               $backup_file .= '.zip';
157           }
158           header('Content-type: application/x-octet-stream');
159           header('Content-disposition: attachment; filename=' . $backup_file);
160
161           readfile(DIR_FS_BACKUP . $backup_file);
162           unlink(DIR_FS_BACKUP . $backup_file);
163
164           exit;
165         } else {
166           switch ($_POST['compress']) {
167             case 'gzip':
168               exec(LOCAL_EXE_GZIP . ' ' . DIR_FS_BACKUP . $backup_file);
169               break;
170             case 'zip':
171               exec(LOCAL_EXE_ZIP . ' -j ' . DIR_FS_BACKUP . $backup_file . '.zip ' . DIR_FS_BACKUP . $backup_file);
172               unlink(DIR_FS_BACKUP . $backup_file);
173           }
174
175           $osC_MessageStack->add_session('header', SUCCESS_DATABASE_SAVED, 'success');
176         }
177
hpdl
758
178         osc_redirect(osc_href_link_admin(FILENAME_BACKUP));
hpdl
1
179         break;
180       case 'restorenow':
181       case 'restorelocalnow':
hpdl
758
182         osc_set_time_limit(0);
hpdl
1
183
184         if ($action == 'restorenow') {
185           $read_from = basename($_GET['file']);
186
187           if (file_exists(DIR_FS_BACKUP . $read_from)) {
188             $restore_file = DIR_FS_BACKUP . $read_from;
189             $extension = substr($read_from, -3);
190
191             if ( ($extension == 'sql') || ($extension == '.gz') || ($extension == 'zip') ) {
192               switch ($extension) {
193                 case 'sql':
194                   $restore_from = $restore_file;
195                   $remove_raw = false;
196                   break;
197                 case '.gz':
198                   $restore_from = substr($restore_file, 0, -3);
199                   exec(LOCAL_EXE_GUNZIP . ' ' . $restore_file . ' -c > ' . $restore_from);
200                   $remove_raw = true;
201                   break;
202                 case 'zip':
203                   $restore_from = substr($restore_file, 0, -4);
204                   exec(LOCAL_EXE_UNZIP . ' ' . $restore_file . ' -d ' . DIR_FS_BACKUP);
205                   $remove_raw = true;
206               }
207
208               if (isset($restore_from) && file_exists($restore_from) && (filesize($restore_from) > 15000)) {
209                 $fd = fopen($restore_from, 'rb');
210                 $restore_query = fread($fd, filesize($restore_from));
211                 fclose($fd);
212               }
213             }
214           }
215         } elseif ($action == 'restorelocalnow') {
216           $sql_file = new upload('sql_file');
217           $sql_file->set_output_messages('session');
218
219           if ($sql_file->parse() == true) {
220             $restore_query = fread(fopen($sql_file->tmp_filename, 'r'), filesize($sql_file->tmp_filename));
221             $read_from = $sql_file->filename;
222           }
223         }
224
225         if (isset($restore_query)) {
226           $sql_array = array();
227           $sql_length = strlen($restore_query);
228           $pos = strpos($restore_query, ';');
229           for ($i=$pos; $i<$sql_length; $i++) {
230             if ($restore_query[0] == '#') {
231               $restore_query = ltrim(substr($restore_query, strpos($restore_query, "\n")));
232               $sql_length = strlen($restore_query);
233               $i = strpos($restore_query, ';')-1;
234               continue;
235             }
236             if ($restore_query[($i+1)] == "\n") {
237               for ($j=($i+2); $j<$sql_length; $j++) {
238                 if (trim($restore_query[$j]) != '') {
239                   $next = substr($restore_query, $j, 6);
240                   if ($next[0] == '#') {
241 // find out where the break position is so we can remove this line (#comment line)
242                     for ($k=$j; $k<$sql_length; $k++) {
243                       if ($restore_query[$k] == "\n") break;
244                     }
245                     $query = substr($restore_query, 0, $i+1);
246                     $restore_query = substr($restore_query, $k);
247 // join the query before the comment appeared, with the rest of the dump
248                     $restore_query = $query . $restore_query;
249                     $sql_length = strlen($restore_query);
250                     $i = strpos($restore_query, ';')-1;
251                     continue 2;
252                   }
253                   break;
254                 }
255               }
256               if ($next == '') { // get the last insert query
257                 $next = 'insert';
258               }
259               if ( (eregi('create', $next)) || (eregi('insert', $next)) || (eregi('drop t', $next)) ) {
260                 $next = '';
261                 $sql_array[] = substr($restore_query, 0, $i);
262                 $restore_query = ltrim(substr($restore_query, $i+1));
263                 $sql_length = strlen($restore_query);
264                 $i = strpos($restore_query, ';')-1;
265               }
266             }
267           }
268
hpdl
814
269           $Qdrop = $osC_Database->query('drop table if exists :table_address_book, :table_banners, :table_banners_history, :table_categories, :table_categories_description, :table_configuration, :table_configuration_group, :table_countries, :table_currencies, :table_customers, :table_customers_basket, :table_languages, :table_manufacturers, :table_manufacturers_info, :table_newsletters, :table_orders, :table_orders_products, :table_orders_products_attributes, :table_orders_products_download, :table_orders_status, :table_orders_status_history, :table_orders_total, :table_products, :table_products_attributes, :table_products_attributes_download, :table_products_description, :table_products_notifications, :table_products_options, :table_products_options_values, :table_products_options_values_to_products_options, :table_products_to_categories, :table_reviews, :table_sessions, :table_specials, :table_tax_class, :table_tax_rates, :table_geo_zones, :table_zones_to_geo_zones, :table_whos_online, :table_zones');
hpdl
1
270           $Qdrop->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
271           $Qdrop->bindTable(':table_banners', TABLE_BANNERS);
272           $Qdrop->bindTable(':table_banners_history', TABLE_BANNERS_HISTORY);
273           $Qdrop->bindTable(':table_categories', TABLE_CATEGORIES);
274           $Qdrop->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
275           $Qdrop->bindTable(':table_configuration', TABLE_CONFIGURATION);
276           $Qdrop->bindTable(':table_configuration_group', TABLE_CONFIGURATION_GROUP);
277           $Qdrop->bindTable(':table_countries', TABLE_COUNTRIES);
278           $Qdrop->bindTable(':table_currencies', TABLE_CURRENCIES);
279           $Qdrop->bindTable(':table_customers', TABLE_CUSTOMERS);
280           $Qdrop->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
281           $Qdrop->bindTable(':table_languages', TABLE_LANGUAGES);
282           $Qdrop->bindTable(':table_manufacturers', TABLE_MANUFACTURERS);
283           $Qdrop->bindTable(':table_manufacturers_info', TABLE_MANUFACTURERS_INFO);
284           $Qdrop->bindTable(':table_newsletters', TABLE_NEWSLETTERS);
285           $Qdrop->bindTable(':table_orders', TABLE_ORDERS);
286           $Qdrop->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
287           $Qdrop->bindTable(':table_orders_products_attributes', TABLE_ORDERS_PRODUCTS_ATTRIBUTES);
288           $Qdrop->bindTable(':table_orders_products_download', TABLE_ORDERS_PRODUCTS_DOWNLOAD);
289           $Qdrop->bindTable(':table_orders_status', TABLE_ORDERS_STATUS);
290           $Qdrop->bindTable(':table_orders_status_history', TABLE_ORDERS_STATUS_HISTORY);
291           $Qdrop->bindTable(':table_orders_total', TABLE_ORDERS_TOTAL);
292           $Qdrop->bindTable(':table_products', TABLE_PRODUCTS);
293           $Qdrop->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
294           $Qdrop->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
295           $Qdrop->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
296           $Qdrop->bindTable(':table_products_notifications', TABLE_PRODUCTS_NOTIFICATIONS);
297           $Qdrop->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS);
298           $Qdrop->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES);
299           $Qdrop->bindTable(':table_products_options_values_to_products_options', TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS);
300           $Qdrop->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
301           $Qdrop->bindTable(':table_reviews', TABLE_REVIEWS);
302           $Qdrop->bindTable(':table_sessions', TABLE_SESSIONS);
303           $Qdrop->bindTable(':table_specials', TABLE_SPECIALS);
304           $Qdrop->bindTable(':table_tax_class', TABLE_TAX_CLASS);
305           $Qdrop->bindTable(':table_tax_rates', TABLE_TAX_RATES);
306           $Qdrop->bindTable(':table_geo_zones', TABLE_GEO_ZONES);
307           $Qdrop->bindTable(':table_zones_to_geo_zones', TABLE_ZONES_TO_GEO_ZONES);
308           $Qdrop->bindTable(':table_whos_online', TABLE_WHOS_ONLINE);
309           $Qdrop->bindTable(':table_zones', TABLE_ZONES);
310           $Qdrop->execute();
311
312           for ($i=0, $n=sizeof($sql_array); $i<$n; $i++) {
313             $osC_Database->simpleQuery($sql_array[$i]);
314           }
315
316           $Qcfg = $osC_Database->query('delete from :table_configuration where configuration_key = :configuration_key');
317           $Qcfg->bindTable(':table_configuration', TABLE_CONFIGURATION);
318           $Qcfg->bindValue(':configuration_key', 'DB_LAST_RESTORE');
319           $Qcfg->execute();
320
321           $Qcfg = $osC_Database->query('insert into :table_configuration values ("", "Last Database Restore", "DB_LAST_RESTORE", :read_from, "Last database restore file", "6", "", "", now(), "", "")');
322           $Qcfg->bindTable(':table_configuration', TABLE_CONFIGURATION);
323           $Qcfg->bindValue(':read_from', $read_from);
324           $Qcfg->execute();
325
326           $osC_Cache->clear('configuration');
327
328           if (isset($remove_raw) && ($remove_raw == true)) {
329             unlink($restore_from);
330           }
331
332           $osC_MessageStack->add_session('header', SUCCESS_DATABASE_RESTORED, 'success');
333         }
334
hpdl
758
335         osc_redirect(osc_href_link_admin(FILENAME_BACKUP));
hpdl
1
336         break;
337       case 'download':
338         if (isset($_GET['file'])) {
339           $extension = substr($_GET['file'], -3);
340
341           if ( ($extension == 'zip') || ($extension == '.gz') || ($extension == 'sql') ) {
342             if ($fp = fopen(DIR_FS_BACKUP . basename($_GET['file']), 'rb')) {
343               $buffer = fread($fp, filesize(DIR_FS_BACKUP . basename($_GET['file'])));
344               fclose($fp);
345
346               header('Content-type: application/x-octet-stream');
347               header('Content-disposition: attachment; filename=' . basename($_GET['file']));
348
349               echo $buffer;
350
351               exit;
352             }
353           } else {
354             $osC_MessageStack->add('header', ERROR_DOWNLOAD_LINK_NOT_ACCEPTABLE, 'error');
355           }
356         }
357         break;
358       case 'deleteconfirm':
359         if (isset($_GET['file'])) {
360           if (file_exists(DIR_FS_BACKUP . basename($_GET['file']))) {
hpdl
758
361             if (osc_remove(DIR_FS_BACKUP . basename($_GET['file']))) {
hpdl
1
362               $osC_MessageStack->add_session('header', SUCCESS_BACKUP_DELETED, 'success');
363
hpdl
758
364               osc_redirect(osc_href_link_admin(FILENAME_BACKUP));
hpdl
1
365             }
366           }
367         }
368         break;
369     }
370   }
371
372 // check if the backup directory exists
373   $dir_ok = false;
374   if (is_dir(DIR_FS_BACKUP)) {
375     if (is_writeable(DIR_FS_BACKUP)) {
376       $dir_ok = true;
377     } else {
378       $osC_MessageStack->add('header', ERROR_BACKUP_DIRECTORY_NOT_WRITEABLE, 'error');
379     }
380   } else {
381     $osC_MessageStack->add('header', ERROR_BACKUP_DIRECTORY_DOES_NOT_EXIST, 'error');
382   }
383
384   $page_contents = 'backup.php';
385
386   require('templates/default.php');
387
388   require('includes/application_bottom.php');
389 ?>