Fixed After successful Backup, loader.gif still present
[fa-stable.git] / admin / backups.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_BACKUP';
13
14 $path_to_root="..";
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/includes/ui.inc");
17 include_once($path_to_root . "/admin/db/maintenance_db.inc");
18
19 if (get_post('view')) {
20         if (!get_post('backups')) {
21                 display_error(_('Select backup file first.'));
22         } else {
23                 $filename = $SysPrefs->backup_dir() . clean_file_name(get_post('backups'));
24                 if (in_ajax()) 
25                         $Ajax->popup( $filename );
26                 else {
27                     header('Content-type: text/plain');
28                 header('Content-Length: '.filesize($filename));
29                         header("Content-Disposition: inline; filename=".basename($filename));
30                 readfile($filename);
31                         exit();
32                 }
33         }
34 };
35 if (get_post('download')) {
36         if (get_post('backups')) {
37                 download_file($SysPrefs->backup_dir().clean_file_name(get_post('backups')));
38                 exit;
39         } else
40                 display_error(_("Select backup file first."));
41 }
42
43 page(_($help_context = "Backup and Restore Database"), false, false, '', '');
44
45 check_paths();
46
47 function check_paths()
48 {
49   global $SysPrefs;
50
51         if (!file_exists($SysPrefs->backup_dir())) {
52                 display_error (_("Backup paths have not been set correctly.") 
53                         ._("Please contact System Administrator.")."<br>" 
54                         . _("cannot find backup directory") . " - " . $SysPrefs->backup_dir() . "<br>");
55                 end_page();
56                 exit;
57         }
58 }
59
60 function generate_backup($conn, $ext='no', $comm='')
61 {
62         global $SysPrefs;
63
64         $filename = db_backup($conn, $ext, $comm, $SysPrefs->backup_dir());
65         if ($filename)
66                 display_notification(_("Backup successfully generated."). ' '
67                         . _("Filename") . ": " . $filename);
68         else
69                 display_error(_("Database backup failed."));
70
71         return $filename;
72 }
73
74
75 function get_backup_file_combo()
76 {
77         global $path_to_root, $Ajax, $SysPrefs;
78         
79         $ar_files = array();
80     default_focus('backups');
81     $dh = opendir($SysPrefs->backup_dir());
82         while (($file = readdir($dh)) !== false)
83                 $ar_files[] = $file;
84         closedir($dh);
85
86     rsort($ar_files);
87         $opt_files = "";
88     foreach ($ar_files as $file)
89                 if (preg_match("/.sql(.zip|.gz)?$/", $file))
90                 $opt_files .= "<option value='$file'>$file</option>";
91
92         $selector = "<select name='backups' size=2 style='height:160px;min-width:230px'>$opt_files</select>";
93
94         $Ajax->addUpdate('backups', "_backups_sel", $selector);
95         $selector = "<span id='_backups_sel'>".$selector."</span>\n";
96
97         return $selector;
98 }
99
100 function compress_list_row($label, $name, $value=null)
101 {
102         $ar_comps = array('no'=>_("No"));
103
104     if (function_exists("gzcompress"))
105         $ar_comps['zip'] = "zip";
106     if (function_exists("gzopen"))
107         $ar_comps['gzip'] = "gzip";
108
109         echo "<tr><td class='label'>$label</td><td>";
110         echo array_selector('comp', $value, $ar_comps);
111         echo "</td></tr>";
112 }
113
114 function download_file($filename)
115 {
116     if (empty($filename) || !file_exists($filename))
117     {
118                 display_error(_('Select backup file first.'));
119         return false;
120     }
121     $saveasname = basename($filename);
122     header('Content-type: application/octet-stream');
123         header('Content-Length: '.filesize($filename));
124         header('Content-Disposition: attachment; filename="'.$saveasname.'"');
125     readfile($filename);
126
127     return true;
128 }
129
130 $conn = $db_connections[user_company()];
131 $backup_name = clean_file_name(get_post('backups'));
132 $backup_path = $SysPrefs->backup_dir() . $backup_name;
133
134 if (get_post('creat')) {
135         generate_backup($conn, get_post('comp'), get_post('comments'));
136         $Ajax->activate('backups');
137         $SysPrefs->refresh(); // re-read system setup
138 };
139
140 if (get_post('restore')) {
141         if ($backup_name) {
142                 if (db_import($backup_path, $conn, true, false, check_value('protected')))
143                         display_notification(_("Restore backup completed."));
144                 $SysPrefs->refresh(); // re-read system setup
145         } else
146                 display_error(_("Select backup file first."));
147 }
148
149 if (get_post('deldump')) {
150         if ($backup_name) {
151                 if (unlink($backup_path)) {
152                         display_notification(_("File successfully deleted.")." "
153                                         . _("Filename") . ": " . $backup_name);
154                         $Ajax->activate('backups');
155                 }
156                 else
157                         display_error(_("Can't delete backup file."));
158         } else
159                 display_error(_("Select backup file first."));
160 }
161
162 if (get_post('upload'))
163 {
164         $tmpname = $_FILES['uploadfile']['tmp_name'];
165         $fname = trim(basename($_FILES['uploadfile']['name']));
166
167         if ($fname) {
168                 if (!preg_match("/\.sql(\.zip|\.gz)?$/", $fname))
169                         display_error(_("You can only upload *.sql backup files"));
170                 elseif ($fname != clean_file_name($fname))
171                         display_error(_("Filename contains forbidden chars. Please rename file and try again."));
172                 elseif (is_uploaded_file($tmpname)) {
173                         rename($tmpname, $SysPrefs->backup_dir() . $fname);
174                         display_notification(_("File uploaded to backup directory"));
175                         $Ajax->activate('backups');
176                 } else
177                         display_error(_("File was not uploaded into the system."));
178         } else
179                 display_error(_("Select backup file first."));
180
181 }
182 //-------------------------------------------------------------------------------
183 start_form(true, true);
184 start_outer_table(TABLESTYLE2);
185 table_section(1);
186 table_section_title(_("Create backup"));
187         textarea_row(_("Comments:"), 'comments', null, 30, 8);
188         compress_list_row(_("Compression:"),'comp');
189         vertical_space("height='20px'");
190         submit_row('creat',_("Create Backup"), false, "colspan=2 align='center'", '', 'process');
191 table_section(2);
192 table_section_title(_("Backup scripts maintenance"));
193
194         start_row();
195         echo "<td style='padding-left:20px' align='left'>".get_backup_file_combo()."</td>";
196         echo "<td style='padding-left:20px' valign='top'>";
197         start_table();
198         submit_row('view',_("View Backup"), false, '', '', false);
199         submit_row('download',_("Download Backup"), false, '', '', false);
200         submit_row('restore',_("Restore Backup"), false, '','', 'process');
201         submit_js_confirm('restore',_("You are about to restore database from backup file.\nDo you want to continue?"));
202
203         submit_row('deldump', _("Delete Backup"), false, '','', true);
204         // don't use 'delete' name or IE js errors appear
205         submit_js_confirm('deldump', sprintf(_("You are about to remove selected backup file.\nDo you want to continue ?")));
206         end_table();
207         echo "</td>";
208         end_row();
209 start_row();
210         echo "<td style='padding-left:20px'  cspan=2>"
211         . radio(_('Update security settings'), 'protect', 0) . '<br>'
212         . radio(_('Protect security settings'), 'protect', 1, true) . "</td>";
213 end_row();
214 start_row();
215         echo "<td style='padding-left:20px' align='left'><input name='uploadfile' type='file'></td>";
216         submit_cells('upload',_("Upload file"),"style='padding-left:20px'", '', true);
217 end_row();
218 end_outer_table();
219
220 end_form();
221
222 end_page();