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