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