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