Fixed many minor bugs, new ones as well as debtor_trans related.
[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"));
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         }
61         function uploadBackup() {
62                 if (document.forms[0].uploadfile.value=='') {
63                         alert('" . _("Please select a file to upload.") . "')
64                         return
65                 }
66                 document.forms[0].action='backups.php?c=u&fn=' + document.forms[0].uploadfile.value
67                 document.forms[0].submit()
68         }
69         </script>
70         <center>
71         <table cellpadding=2 cellspacing=2 border=0>
72         <tr><td colspan=2 style='color:darkred'><b>$msg</b>&nbsp;</td></tr>
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         global $path_to_root;
112         //Generate Only
113         if (isset($_GET['c']))
114         {
115                 if ($_GET['c']=='g')
116                 {
117                         $filename = generate_backup($conn, $_GET['comp'], $_GET['comm']);
118                         header("Location: backups.php?c=gs&fn=" . urlencode($filename));
119                         return "";
120                 }
121                 //Generate and download
122                 if ($_GET['c']=='gd')
123                 {
124                         $filename = generate_backup($conn);
125                         header("Location: backups.php?c=ds&fn=" . urlencode($filename));
126                         return "";
127                 }
128                 //Download the file
129                 if ($_GET['c']=='d')
130                 {
131                         download_file(BACKUP_PATH . $_GET['fn']);
132                         exit;
133                 }
134                 //Delete the file
135                 if ($_GET['c']=='df')
136                 {
137                         $filename = $_GET['fn'];
138                         @unlink(BACKUP_PATH . $filename);
139                         header("Location: backups.php?c=dff&fn=" . urlencode($filename));
140                         return "";
141                 }
142                 if ($_GET['c']=='dff')
143                 {
144                         $msg = _("File successfully deleted.")."&nbsp;&nbsp;&nbsp;";
145                         $msg .= _("Filename") . " = " . $_GET['fn'];
146                         return $msg;
147                 }
148                 //Write JS script to open download window
149                 if ($_GET['c']=='ds')
150                 {
151                         $filename = urlencode($_GET['fn']);
152                         $msg = _("Backup is being downloaded...");
153                         $msg .= "<script language='javascript'>";
154                         $msg .= "function download_file() {location.href ='backups.php?c=d&fn=$filename'}; window.onload=download_file";
155                         $msg .= "</script>";
156                         return $msg;
157                 }
158                 //Print backup success message
159                 if ($_GET['c']=='gs')
160                 {
161                         $msg = _("Backup successfully generated.")."&nbsp;&nbsp;&nbsp;";
162                         $msg .= _("Filename") . " = " . $_GET['fn'];
163                         return $msg;
164                 }
165                 //Restore backup
166                 if ($_GET['c']=='r')
167                 {
168                         $filename=$_GET['fn'];
169                         restore_backup(BACKUP_PATH . $filename, $conn);
170                         header("Location: backups.php?c=rs&fn=" . urlencode($filename));
171                         return "";
172                 }
173                 //Print restore success message
174                 if ($_GET['c']=='rs')
175                 {
176                         $msg = _("Restore backup completed.")."&nbsp;&nbsp;&nbsp;";
177                         return $msg;
178                 }
179
180                 if ($_GET['c']=='u')
181                 {
182                         $filename = $_FILES['uploadfile']['tmp_name'];
183                         if (is_uploaded_file ($filename))
184                         {
185                                 restore_backup($filename, $conn);
186                                 $msg = _("Uploaded file has been restored.");
187                         }
188                         else
189                         {
190                                 $msg = _("Backup was not uploaded into the system.");
191                         }
192                         return $msg;
193                 }
194         }
195         return "";
196 }
197
198 function generate_backup($conn, $ext='no', $comm='')
199 {
200         if ($conn['tbpref'] != "")
201                 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
202         else
203                 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
204
205         $filename = db_export($conn, $filename, $ext, $comm);
206
207         return $filename;
208 }
209
210 function restore_backup($filename, $conn)
211 {
212         return db_import($filename, $conn);
213 }
214
215 function get_backup_file_combo()
216 {
217         global $path_to_root;
218         $ar_files = array();
219     default_focus('cmb_backups');
220     $dh = opendir(BACKUP_PATH);
221         while (($file = readdir($dh)) !== false)
222                 $ar_files[] = $file;
223         closedir($dh);
224
225     rsort($ar_files);
226         $opt_files = "";
227     foreach ($ar_files as $file)
228         if (strpos($file, ".sql") || strpos($file, ".sql"))
229                 $opt_files .= "<option value='$file'>$file</option>";
230
231         return "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
232 }
233
234 function get_compr_combo()
235 {
236         $ar_comps = array();
237
238         $ar_comps[] = _("No");
239     if (function_exists("gzcompress"))
240         $ar_comps[] = "zip";
241     if (function_exists("gzopen"))
242         $ar_comps[] = "gzip";
243         $opt_comps = "";
244     foreach ($ar_comps as $file)
245         $opt_comps .= "<option value='$file'>$file</option>";
246
247         return "<select name='cmb_comp'>$opt_comps</select>";
248 }
249
250 function download_file($filename)
251 {
252     if (empty($filename) || !file_exists($filename))
253     {
254         return FALSE;
255     }
256     $saveasname = basename($filename);
257     header('Content-type: application/octet-stream');
258     header('Content-Length: '.filesize($filename));
259     header('Content-Disposition: attachment; filename="'.$saveasname.'"');
260     readfile($filename);
261     return true;
262 }
263
264 function valid_paths()
265 {
266         global $path_to_root;
267
268         $st = "";
269         if (!file_exists(BACKUP_PATH))
270                 $st .= "&nbsp;&nbsp;&nbsp;-&nbsp;" . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>";
271         return $st;
272 }
273
274 ?>