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