fixup commit for tag 'unstable_2_0_5'
[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 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>
74                 <td style='padding-right:30px'>" . _("Backup scripts") . "</td>
75         </tr>
76         <tr>
77                 <td style='padding-right:30px'>$cmb</td>
78                 <script language='javascript'>
79                         if (document.forms[0].cmb_backups.options.length!=0) document.forms[0].cmb_backups.selectedIndex=0
80                 </script>
81                 <td>
82                         <table height=160 cellpadding=0 cellspacing=0 border=0>
83                         <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>
84                         <tr><td><input onclick='javascript:restoreBackup()' type='button' style='width:150px' value='". _("Restore Backup") . "'></td><td>&nbsp;</td></tr>
85                         <tr><td><input onclick='javascript:viewBackup()' type='button' style='width:150px' value='". _("View Backup") . "'></td><td>&nbsp;</td></tr>
86                         <tr><td><input onclick='javascript:deleteBackup()' type='button' style='width:150px' value='". _("Delete Backup") . "'></td><td>&nbsp;</td></tr>
87                         <tr><td><input onclick='javascript:downloadBackup()' type='button' style='width:150px' value='". _("Download Backup") . "'></td><td>&nbsp;</td></tr>
88                         <tr><td><input onclick='javascript:uploadBackup()' type='button' style='width:150px' value='". _("Upload Backup"). "'></td>
89                                 <td style='padding-left:20px'><input name='uploadfile' type='file'></td>
90                         </tr>
91                         </table>
92                 </td>
93         </tr>
94         <tr>
95                 <td style='padding-right:30px'>" . _("Comments") . " (" . _("Create Backup") . ")</td>
96         </tr>
97         <tr>
98                 <td style='padding-right:30px'><textarea rows=4 cols=30 name='comments'></textarea></td>
99         </tr>
100         </table></center>";
101
102 end_form();
103
104 //-------------------------------------------------------------------------------------------------
105
106 end_page();
107
108
109 function handle_form($conn)
110 {
111 if(isset($_GET['c']))
112         switch($_GET['c'])
113         {
114                 case 'g':       //Generate Only
115                         $filename = generate_backup($conn, $_GET['comp'], $_GET['comm']);
116                         header("Location: backups.php?c=gs&fn=" . urlencode($filename));
117                         break;
118
119                 case 'gd':      //Generate and download
120                         $filename = generate_backup($conn);
121                         header("Location: backups.php?c=ds&fn=" . urlencode($filename));
122                         break;
123
124                 case 'd':       //Download the file
125                         download_file(BACKUP_PATH . $_GET['fn']);
126                         exit;
127
128                 case 'df':      //Delete the file
129                         $filename = $_GET['fn'];
130                         @unlink(BACKUP_PATH . $filename);
131                         header("Location: backups.php?c=dff&fn=" . urlencode($filename));
132                         break;
133
134                 case 'dff':
135                         $msg = _("File successfully deleted.")." ";
136                         $msg .= _("Filename") . " = " . $_GET['fn'];
137                         display_notification($msg);
138                         break;
139
140                 case 'ds':      //Write JS script to open download window
141                         $filename = urlencode($_GET['fn']);
142                         display_notification(_("Backup is being downloaded..."));
143                         
144                         add_js_source("<script language='javascript'>
145                         function download_file() {location.href ='backups.php?c=d&fn=$filename'}; 
146                                 Behaviour.addLoadEvent(download_file);
147                         </script>");
148                         break;
149
150                 case 'gs':      //Print backup success message
151                         $msg = _("Backup successfully generated."). ' ';
152                         $msg .= _("Filename") . " = " . $_GET['fn'];
153                         display_notification($msg);
154                         break;
155
156                 case 'r':       //Restore backup
157                         $filename=$_GET['fn'];
158                         if( restore_backup(BACKUP_PATH . $filename, $conn) )
159                                 header("Location: backups.php?c=rs&fn=" . urlencode($filename));
160                         break;
161
162                 case 'rs':      //Print restore success message
163                         display_notification(_("Restore backup completed."));
164                         break;
165
166                 case 'u':
167                         $filename = $_FILES['uploadfile']['tmp_name'];
168                         if (is_uploaded_file ($filename))
169                         {
170                                 if( restore_backup($filename, $conn) )
171                                         display_notification(_("Uploaded file has been restored."));
172                                 else
173                                         display_error(_("Database restore failed."));
174                         }
175                         else
176                         {
177                                 display_error(_("Backup was not uploaded into the system."));
178                         }
179                 }
180 }
181
182 function generate_backup($conn, $ext='no', $comm='')
183 {
184         if ($conn['tbpref'] != "")
185                 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
186         else
187                 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
188
189         $filename = db_export($conn, $filename, $ext, $comm);
190
191         return $filename;
192 }
193
194 function restore_backup($filename, $conn)
195 {
196         return db_import($filename, $conn);
197 }
198
199 function get_backup_file_combo()
200 {
201         global $path_to_root;
202         $ar_files = array();
203     default_focus('cmb_backups');
204     $dh = opendir(BACKUP_PATH);
205         while (($file = readdir($dh)) !== false)
206                 $ar_files[] = $file;
207         closedir($dh);
208
209     rsort($ar_files);
210         $opt_files = "";
211     foreach ($ar_files as $file)
212         if (strpos($file, ".sql") || strpos($file, ".sql"))
213                 $opt_files .= "<option value='$file'>$file</option>";
214
215         return "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
216 }
217
218 function get_compr_combo()
219 {
220         $ar_comps = array();
221
222         $ar_comps[] = _("No");
223     if (function_exists("gzcompress"))
224         $ar_comps[] = "zip";
225     if (function_exists("gzopen"))
226         $ar_comps[] = "gzip";
227         $opt_comps = "";
228     foreach ($ar_comps as $file)
229         $opt_comps .= "<option value='$file'>$file</option>";
230
231         return "<select name='cmb_comp'>$opt_comps</select>";
232 }
233
234 function download_file($filename)
235 {
236     if (empty($filename) || !file_exists($filename))
237     {
238         return false;
239     }
240     $saveasname = basename($filename);
241     header('Content-type: application/octet-stream');
242     header('Content-Length: '.filesize($filename));
243     header('Content-Disposition: attachment; filename="'.$saveasname.'"');
244     readfile($filename);
245     return true;
246 }
247
248 function valid_paths()
249 {
250         global $path_to_root;
251
252         $st = "";
253         if (!file_exists(BACKUP_PATH))
254                 $st .= "&nbsp;&nbsp;&nbsp;-&nbsp;" . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>";
255         return $st;
256 }
257
258 ?>