Buggy file view/download in ajax mode.
[fa-stable.git] / admin / attachments.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 $path_to_root="..";
13 $page_security = 8;
14
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 if (isset($_GET['vw']))
22         $view_id = $_GET['vw'];
23 else
24 $view_id = find_submit('view');
25 if ($view_id != -1)
26 {
27         $row = get_attachment($view_id);
28         if ($row['filename'] != "")
29         {
30                 if(in_ajax()) {
31                         $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
32                 } else {
33                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
34                 header("Content-type: ".$type);
35                 header('Content-Length: '.$row['filesize']);
36                 if ($type == 'application/octet-stream')
37                         header('Content-Disposition: attachment; filename='.$row['filename']);
38                 else
39                                 header("Content-Disposition: inline");
40                 echo file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
41                 exit();
42                 }
43         }       
44 }
45 if (isset($_GET['dl']))
46         $download_id = $_GET['dl'];
47 else
48         $download_id = find_submit('download');
49
50 if ($download_id != -1)
51 {
52         $row = get_attachment($download_id);
53         if ($row['filename'] != "")
54         {
55                 if(in_ajax()) {
56                         $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
57                 } else {
58                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
59                 header("Content-type: ".$type);
60                 header('Content-Length: '.$row['filesize']);
61                 header('Content-Disposition: attachment; filename='.$row['filename']);
62                 echo file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
63                 exit();
64                 }
65         }       
66 }
67
68 $js = "";
69 if ($use_popup_windows)
70         $js .= get_js_open_window(800, 500);
71 page(_("Attach Documents"), false, false, "", $js);
72
73 simple_page_mode(true);
74 //----------------------------------------------------------------------------------------
75 if (isset($_GET['filterType'])) // catch up external links
76         $_POST['filterType'] = $_GET['filterType'];
77 if (isset($_GET['trans_no']))
78         $_POST['trans_no'] = $_GET['trans_no'];
79         
80 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
81 {
82         if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
83         {
84                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
85                 $tmpname = $_FILES['filename']['tmp_name'];
86
87                 $dir =  $comp_path."/".user_company(). "/attachments";
88                 if (!file_exists($dir))
89                 {
90                         mkdir ($dir,0777);
91                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
92                         $fp = fopen($dir."/index.php", "w");
93                         fwrite($fp, $index_file);
94                         fclose($fp);
95                 }
96                 if ($Mode == 'UPDATE_ITEM' && file_exists($dir."/".$_POST['unique_name']))
97                         unlink($dir."/".$_POST['unique_name']);
98
99                 $unique_name = uniqid('');
100                 move_uploaded_file($tmpname, $dir."/".$unique_name);
101                 //save the file
102                 $filename = $_FILES['filename']['name'];
103                 $filesize = $_FILES['filename']['size'];
104                 $filetype = $_FILES['filename']['type'];
105         }
106         else
107         {
108                 $unique_name = $filename = $filetype = "";
109                 $filesize = 0;
110         }
111         $date = date2sql(Today());
112         if ($Mode == 'ADD_ITEM')
113         {
114                 $sql = "INSERT INTO ".TB_PREF."attachments (type_no, trans_no, description, filename, unique_name,
115                         filesize, filetype, tran_date) VALUES (".$_POST['filterType'].",".$_POST['trans_no'].",".
116                         db_escape($_POST['description']).", '$filename', '$unique_name', '$filesize', '$filetype', '$date')";
117                 db_query($sql, "Attachment could not be inserted");             
118                 display_notification(_("Attachment has been inserted.")); 
119         }
120         else
121         {
122                 $sql = "UPDATE ".TB_PREF."attachments SET
123                         type_no=".$_POST['filterType'].",
124                         trans_no=".$_POST['trans_no'].",
125                         description=".db_escape($_POST['description']).", ";
126                 if ($filename != "")
127                 {
128                         $sql .= "filename='$filename',
129                         unique_name='$unique_name',
130                         filesize='$filesize',
131                         filetype='$filetype', ";
132                 }       
133                 $sql .= "tran_date='$date' WHERE id=$selected_id";
134                 db_query($sql, "Attachment could not be updated");              
135                 display_notification(_("Attachment has been updated.")); 
136         }
137         $Mode = 'RESET';
138 }               
139
140 if ($Mode == 'Delete')
141 {
142         $row = get_attachment($selected_id);
143         $dir =  $comp_path."/".user_company(). "/attachments";
144         if (file_exists($dir."/".$row['unique_name']))
145                 unlink($dir."/".$row['unique_name']);
146         $sql = "DELETE FROM ".TB_PREF."attachments WHERE id = $selected_id";
147         db_query($sql, "Could not delete attachment");
148         display_notification(_("Attachment has been deleted.")); 
149         $Mode = 'RESET';
150 }
151
152 if ($Mode == 'RESET')
153 {
154         unset($_POST['trans_no']);
155         unset($_POST['description']);
156         $selected_id = -1;
157 }
158
159 function viewing_controls()
160 {
161     start_form(false, true);
162
163     start_table("class='tablestyle_noborder'");
164
165         systypes_list_row(_("Type:"), 'filterType', null, true);
166
167     end_table(1);
168
169         end_form();
170 }
171
172 //----------------------------------------------------------------------------------------
173
174 function get_attached_documents($type)
175 {
176         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
177         return db_query($sql, "Could not retrieve attachments");
178 }
179
180 function get_attachment($id)
181 {
182         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
183         $result = db_query($sql, "Could not retrieve attachments");
184         return db_fetch($result);
185 }
186
187 function display_rows($type)
188 {
189         global $table_style;
190
191         $rows = get_attached_documents($type);
192         $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
193         
194         div_start('transactions');
195         start_form();
196         start_table($table_style);
197         table_header($th);
198         $k = 0;
199         while ($row = db_fetch($rows))
200         {
201                 alt_table_row_color($k);
202                 
203                 label_cell(get_trans_view_str($type, $row['trans_no']));
204                 label_cell($row['description']);
205                 label_cell($row['filename']);
206                 label_cell($row['filesize']);
207                 label_cell($row['filetype']);
208                 label_cell(sql2date($row['tran_date']));
209                 edit_button_cell("Edit".$row['id'], _("Edit"));
210                 button_cell("view".$row['id'], _("View"), false, ICON_VIEW);
211                 button_cell("download".$row['id'], _("Download"), false, ICON_DOWN);
212                 delete_button_cell("Delete".$row['id'], _("Delete"));
213         end_row();
214         }       
215         end_table(1);
216         hidden('filterType', $type);
217         end_form();
218         div_end();
219 }
220
221 //----------------------------------------------------------------------------------------
222
223 viewing_controls();
224
225 if (isset($_POST['filterType']))
226         display_rows($_POST['filterType']);
227
228 start_form(true);
229
230 start_table($table_style2);
231
232 if ($selected_id != -1)
233 {
234         if ($Mode == 'Edit')
235         {
236                 $row = get_attachment($selected_id);
237                 $_POST['trans_no']  = $row["trans_no"];
238                 $_POST['description']  = $row["description"];
239                 hidden('trans_no', $row['trans_no']);
240                 hidden('unique_name', $row['unique_name']);
241                 label_row(_("Transaction #"), $row['trans_no']);
242         }       
243         hidden('selected_id', $selected_id);
244 }
245 else
246         text_row_ex(_("Transaction #").':', 'trans_no', 10);
247 text_row_ex(_("Description").':', 'description', 40);
248 start_row();
249 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
250 end_row();
251
252 end_table(1);
253 if (isset($_POST['filterType']))
254         hidden('filterType', $_POST['filterType']);
255
256 submit_add_or_update_center($selected_id == -1, '', true);
257
258 end_form();
259
260 end_page();
261
262 ?>