Again fixed nested subtypes in account types correctly in COA, Monthly Breakdown...
[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 if (get_post('view')) {
20         $filename = BACKUP_PATH . get_post('cmb_backups');
21         if (in_ajax()) 
22                 $Ajax->popup( $filename );
23         else {
24             header('Content-type: application/octet-stream');
25         header('Content-Length: '.filesize($filename));
26                 header("Content-Disposition: inline; filename=$filename");
27         readfile($filename);
28                 exit();
29         }
30 };
31
32 if (get_post('download')) {
33         download_file(BACKUP_PATH . get_post('cmb_backups'));
34         exit;
35 }
36
37 page(_("Backup and Restore Database"), false, false, '', '');
38
39 check_paths();
40
41 function check_paths()
42 {
43         if (!file_exists(BACKUP_PATH)) {
44                 display_error (_("Backup paths have not been set correctly.") 
45                         ._("Please contact System Administrator.")."<br>" 
46                         . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>");
47                 end_page();
48                 exit;
49         }
50 }
51
52 function generate_backup($conn, $ext='no', $comm='')
53 {
54         if ($conn['tbpref'] != "")
55                 $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
56         else
57                 $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
58
59         $filename = db_export($conn, $filename, $ext, $comm);
60         if ($filename)
61                 display_notification(_("Backup successfully generated."). ' '
62                         . _("Filename") . ": " . $filename);
63         else
64                 display_error(_("Database backup failed."));
65         
66         return $filename;
67 }
68
69
70 function get_backup_file_combo()
71 {
72         global $path_to_root, $Ajax;
73         
74         $ar_files = array();
75     default_focus('cmb_backups');
76     $dh = opendir(BACKUP_PATH);
77         while (($file = readdir($dh)) !== false)
78                 $ar_files[] = $file;
79         closedir($dh);
80
81     rsort($ar_files);
82         $opt_files = "";
83     foreach ($ar_files as $file)
84                 if (preg_match("/.sql(.zip|.gz)?$/", $file))
85                 $opt_files .= "<option value='$file'>$file</option>";
86
87         $selector = "<select name='cmb_backups' size=2 style='height:160px;min-width:230px'>$opt_files</select>";
88
89         $Ajax->addUpdate('cmd_backups', "_cmd_backups_sel", $selector);
90         $selector = "<span id='_cmd_backups_sel'>".$selector."</span>\n";
91
92         return $selector;
93 }
94
95 function compress_list_row($label, $name, $value=null)
96 {
97         $ar_comps = array('no'=>_("No"));
98
99     if (function_exists("gzcompress"))
100         $ar_comps['zip'] = "zip";
101     if (function_exists("gzopen"))
102         $ar_comps['gzip'] = "gzip";
103
104         echo "<tr><td>$label</td><td>";
105         array_selector('comp', $value, $ar_comps);
106         echo "</td></tr>";
107 }
108
109 function download_file($filename)
110 {
111     if (empty($filename) || !file_exists($filename))
112     {
113         return false;
114     }
115     $saveasname = basename($filename);
116     header('Content-type: application/octet-stream');
117         header('Content-Length: '.filesize($filename));
118         header('Content-Disposition: attachment; filename="'.$saveasname.'"');
119     readfile($filename);
120
121     return true;
122 }
123
124 $db_name = $_SESSION["wa_current_user"]->company;
125 $conn = $db_connections[$db_name];
126
127 if (get_post('creat')) {
128         generate_backup($conn, get_post('comp'), get_post('comments'));
129         $Ajax->activate('cmd_backups');
130 };
131
132 if (get_post('restore')) {
133         if (db_import(BACKUP_PATH . get_post('cmb_backups'), $conn))
134                 display_notification(_("Restore backup completed."));
135 }
136
137 if (get_post('delete')) {
138         if (unlink(BACKUP_PATH . get_post('cmb_backups'))) {
139                 display_notification(_("File successfully deleted.")." "
140                                 . _("Filename") . ": " . get_post('cmb_backups'));
141                 $Ajax->activate('cmd_backups');
142         }
143         else
144                 display_error(_("Can't delete backup file."));
145 };
146
147 if (get_post('upload'))
148 {
149         $tmpname = $_FILES['uploadfile']['tmp_name'];
150         $fname = $_FILES['uploadfile']['name'];
151
152         if (!preg_match("/.sql(.zip|.gz)?$/", $fname))
153                 display_error(_("You can only upload *.sql backup files"));
154         elseif (is_uploaded_file($tmpname)) {
155                 rename($tmpname, BACKUP_PATH . $fname);
156                 display_notification( "File uploaded to backup directory");
157                 $Ajax->activate('cmd_backups');
158         } else
159                 display_error(_("File was not uploaded into the system."));
160 }
161 //-------------------------------------------------------------------------------
162 start_form(true, true);
163 start_outer_table($table_style2);
164 table_section(1);
165 table_section_title(_("Create backup"));
166         textarea_row(_("Comments:"), 'comments', null, 30, 8);
167         compress_list_row(_("Compression:"),'comp');
168         vertical_space("height='20px'");
169         submit_row('creat',_("Create Backup"), false, "colspan=2 align='center'", '', 'process');
170 table_section(2);
171 table_section_title(_("Backup scripts maintenance"));
172
173         start_row();
174         echo "<td style='padding-left:20px'align='left'>".get_backup_file_combo()."</td>";
175         echo "<td valign='top'>";
176         start_table();
177         submit_row('view',_("View Backup"), false, '', '', true);
178         submit_row('download',_("Download Backup"), false, '', '', false);
179         submit_row('restore',_("Restore Backup"), false, '','', 'process');
180
181         $js = "if(confirm(\""
182                 .sprintf(_('You are about to remove %s backup file.<br> Do you want to continue ?'),
183                         get_post('cmb_backups'))
184                 ."\")) { JsHttpRequest.request(\"delete\"); }";
185
186         submit_row('delete', _("Delete Backup"), false, '','', 'dialog');
187         end_table();
188         echo "</td>";
189         end_row();
190 start_row();
191 echo "<td style='padding-left:20px' align='left'><input name='uploadfile' type='file'></td>";
192         submit_cells('upload',_("Upload file"),'', '', true);
193 end_row();
194 end_outer_table();
195
196 end_form();
197
198 end_page();
199 ?>