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