Corrected backup maintenance page display during file download.
[fa-stable.git] / admin / backups.php
1 <?php
2
3 $page_security = 15;
4
5 $path_to_root="..";
6 include_once($path_to_root . "/includes/session.inc");
7 include_once($path_to_root . "/includes/ui.inc");
8 include_once($path_to_root . "/admin/db/maintenance_db.inc");
9
10 $valid_paths = valid_paths();
11 //$valid_paths = '';
12 if ($valid_paths != "")
13 {
14         page(_("Backup and Restore Database - Error"));
15         display_error (_("Backup paths have not been set correctly.") ."&nbsp;&nbsp;&nbsp;" . _("Please contact System Administrator.") . "<br>" .$valid_paths);
16         end_page();
17         exit;
18 }
19
20 $db_name = $_SESSION["wa_current_user"]->company;
21 $msg = handle_form($db_connections[$db_name]);
22
23 page(_("Backup and Restore Database"), false, false, '', '', true);
24
25 //-------------------------------------------------------------------------------
26 start_form(true, true);
27
28 $cmb = get_backup_file_combo();
29 $compr = get_compr_combo();
30
31 echo "
32         <script language='javascript'>
33         function createBackup() {
34                 ext = document.forms[0].cmb_comp.options[document.forms[0].cmb_comp.selectedIndex].value
35                 comm = document.forms[0].comments.value
36                 document.location.replace('backups.php?c=g&comp='+ext+'&comm='+comm)
37         }
38         function restoreBackup() {
39                 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
40                 document.location.replace('backups.php?c=r&fn='+pFilename)
41         }
42         function viewBackup() {
43                 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
44                 var ext = pFilename.substr(pFilename.lastIndexOf('.') + 1)
45                 if (ext != 'sql') {
46                         alert('" . _('This extension can not be be viewed: ') . "' + ext)
47                         return
48                 }
49                 window.open('" . BACKUP_PATH . "'+pFilename, '', 'toolbar=no,scrollbars=yes')
50         }
51         function deleteBackup() {
52                 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
53                 if (!confirm('" . _("Are you sure you want to delete the backup file - ") . "'+pFilename+'?'))
54                         return
55                 document.location.replace('backups.php?c=df&fn='+pFilename)
56         }
57         function downloadBackup() {
58                 pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
59                 document.location.replace('backups.php?c=ds&fn='+pFilename)
60                 Behaviour.apply();
61         }
62         function uploadBackup() {
63                 if (document.forms[0].uploadfile.value=='') {
64                         alert('" . _("Please select a file to upload.") . "')
65                         return
66                 }
67                 document.forms[0].action='backups.php?c=u&fn=' + document.forms[0].uploadfile.value
68                 document.forms[0].submit()
69         }
70         </script>
71         <center>
72         <table cellpadding=2 cellspacing=2 border=0>
73         <tr><td colspan=2 style='color:darkred'><b>$msg</b>&nbsp;</td></tr>
74         <tr>
75                 <td style='padding-right:30px'>" . _("Backup scripts") . "</td>
76         </tr>
77         <tr>
78                 <td style='padding-right:30px'>$cmb</td>
79                 <script language='javascript'>
80                         if (document.forms[0].cmb_backups.options.length!=0) document.forms[0].cmb_backups.selectedIndex=0
81                 </script>
82                 <td>
83                         <table height=160 cellpadding=0 cellspacing=0 border=0>
84                         <tr><td><input onclick='javascript:createBackup()' type='button' style='width:150px' value='". _("Create Backup") . "'></td><td style='padding-left:20px'>" . _("Compression") . "&nbsp;&nbsp;$compr</td></tr>
85                         <tr><td><input onclick='javascript:restoreBackup()' type='button' style='width:150px' value='". _("Restore Backup") . "'></td><td>&nbsp;</td></tr>
86                         <tr><td><input onclick='javascript:viewBackup()' type='button' style='width:150px' value='". _("View Backup") . "'></td><td>&nbsp;</td></tr>
87                         <tr><td><input onclick='javascript:deleteBackup()' type='button' style='width:150px' value='". _("Delete Backup") . "'></td><td>&nbsp;</td></tr>
88                         <tr><td><input onclick='javascript:downloadBackup()' type='button' style='width:150px' value='". _("Download Backup") . "'></td><td>&nbsp;</td></tr>
89                         <tr><td><input onclick='javascript:uploadBackup()' type='button' style='width:150px' value='". _("Upload Backup"). "'></td>
90                                 <td style='padding-left:20px'><input name='uploadfile' type='file'></td>
91                         </tr>
92                         </table>
93                 </td>
94         </tr>
95         <tr>
96                 <td style='padding-right:30px'>" . _("Comments") . " (" . _("Create Backup") . ")</td>
97         </tr>
98         <tr>
99                 <td style='padding-right:30px'><textarea rows=4 cols=30 name='comments'></textarea></td>
100         </tr>
101         </table></center>";
102
103 end_form();
104
105 //-------------------------------------------------------------------------------------------------
106
107 end_page();
108
109
110 function handle_form($conn)
111 {
112         global $path_to_root;
113         //Generate Only
114         if (isset($_GET['c']))
115         {
116                 if ($_GET['c']=='g')
117                 {
118                         $filename = generate_backup($conn, $_GET['comp'], $_GET['comm']);
119                         header("Location: backups.php?c=gs&fn=" . urlencode($filename));
120                         return "";
121                 }
122                 //Generate and download
123                 if ($_GET['c']=='gd')
124                 {
125                         $filename = generate_backup($conn);
126                         header("Location: backups.php?c=ds&fn=" . urlencode($filename));
127                         return "";
128                 }
129                 //Download the file
130                 if ($_GET['c']=='d')
131                 {
132                         download_file(BACKUP_PATH . $_GET['fn']);
133                         exit;
134                 }
135                 //Delete the file
136                 if ($_GET['c']=='df')
137                 {
138                         $filename = $_GET['fn'];
139                         @unlink(BACKUP_PATH . $filename);
140                         header("Location: backups.php?c=dff&fn=" . urlencode($filename));
141                         return "";
142                 }
143                 if ($_GET['c']=='dff')
144                 {
145                         $msg = _("File successfully deleted.")."&nbsp;&nbsp;&nbsp;";
146                         $msg .= _("Filename") . " = " . $_GET['fn'];
147                         return $msg;
148                 }
149                 //Write JS script to open download window
150                 if ($_GET['c']=='ds')
151                 {
152                         $filename = urlencode($_GET['fn']);
153                         $msg = _("Backup is being downloaded...");
154                         $msg .= "<script language='javascript'>";
155                         $msg .= "function download_file() {location.href ='backups.php?c=d&fn=$filename'}; 
156                                 Behaviour.addLoadEvent(download_file);";
157                         $msg .= "</script>";
158                         return $msg;
159                 }
160                 //Print backup success message
161                 if ($_GET['c']=='gs')
162                 {
163                         $msg = _("Backup successfully generated.")."&nbsp;&nbsp;&nbsp;";
164                         $msg .= _("Filename") . " = " . $_GET['fn'];
165                         return $msg;
166                 }
167                 //Restore backup
168                 if ($_GET['c']=='r')
169                 {
170                         $filename=$_GET['fn'];
171                         restore_backup(BACKUP_PATH . $filename, $conn);
172                         header("Location: backups.php?c=rs&fn=" . urlencode($filename));
173                         return "";
174                 }
175                 //Print restore success message
176                 if ($_GET['c']=='rs')
177                 {
178                         $msg = _("Restore backup completed.")."&nbsp;&nbsp;&nbsp;";
179                         return $msg;
180                 }
181
182                 if ($_GET['c']=='u')
183                 {
184                         $filename = $_FILES['uploadfile']['tmp_name'];
185                         if (is_uploaded_file ($filename))
186                         {
187                                 restore_backup($filename, $conn);
188                                 $msg = _("Uploaded file has been restored.");
189                         }
190                         else
191                         {
192                                 $msg = _("Backup was not uploaded into the system.");
193                         }
194                         return $msg;
195                 }
196         }
197         return "";
198 }
199
200 function generate_backup($conn, $ext='no', $comm='')
201 {
202         if ($conn['tbpref'] != "")
203                 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
204         else
205                 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
206
207         $filename = db_export($conn, $filename, $ext, $comm);
208
209         return $filename;
210 }
211
212 function restore_backup($filename, $conn)
213 {
214         return db_import($filename, $conn);
215 }
216
217 function get_backup_file_combo()
218 {
219         global $path_to_root;
220         $ar_files = array();
221     default_focus('cmb_backups');
222     $dh = opendir(BACKUP_PATH);
223         while (($file = readdir($dh)) !== false)
224                 $ar_files[] = $file;
225         closedir($dh);
226
227     rsort($ar_files);
228         $opt_files = "";
229     foreach ($ar_files as $file)
230         if (strpos($file, ".sql") || strpos($file, ".sql"))
231                 $opt_files .= "<option value='$file'>$file</option>";
232
233         return "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
234 }
235
236 function get_compr_combo()
237 {
238         $ar_comps = array();
239
240         $ar_comps[] = _("No");
241     if (function_exists("gzcompress"))
242         $ar_comps[] = "zip";
243     if (function_exists("gzopen"))
244         $ar_comps[] = "gzip";
245         $opt_comps = "";
246     foreach ($ar_comps as $file)
247         $opt_comps .= "<option value='$file'>$file</option>";
248
249         return "<select name='cmb_comp'>$opt_comps</select>";
250 }
251
252 function download_file($filename)
253 {
254     if (empty($filename) || !file_exists($filename))
255     {
256         return FALSE;
257     }
258     $saveasname = basename($filename);
259     header('Content-type: application/octet-stream');
260     header('Content-Length: '.filesize($filename));
261     header('Content-Disposition: attachment; filename="'.$saveasname.'"');
262     readfile($filename);
263     return true;
264 }
265
266 function valid_paths()
267 {
268         global $path_to_root;
269
270         $st = "";
271         if (!file_exists(BACKUP_PATH))
272                 $st .= "&nbsp;&nbsp;&nbsp;-&nbsp;" . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>";
273         return $st;
274 }
275
276 ?>