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