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