Fixed error in attachments edition resulting in unexpected attachment file deletion...
[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 = 'SA_ATTACHDOCUMENT';
14
15 include_once($path_to_root . "/includes/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21 include_once($path_to_root . "/admin/db/attachments_db.inc");
22
23 if (isset($_GET['vw']))
24         $view_id = $_GET['vw'];
25 else
26         $view_id = find_submit('view');
27 if ($view_id != -1)
28 {
29         $row = get_attachment($view_id);
30         if ($row['filename'] != "")
31         {
32                 if(in_ajax()) {
33                         $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
34                 } else {
35                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
36                 header("Content-type: ".$type);
37                 header('Content-Length: '.$row['filesize']);
38                 //if ($type == 'application/octet-stream')
39                 //      header('Content-Disposition: attachment; filename='.$row['filename']);
40                 //else
41                                 header("Content-Disposition: inline");
42                 echo file_get_contents(company_path(). "/attachments/".$row['unique_name']);
43                 exit();
44                 }
45         }       
46 }
47 if (isset($_GET['dl']))
48         $download_id = $_GET['dl'];
49 else
50         $download_id = find_submit('download');
51
52 if ($download_id != -1)
53 {
54         $row = get_attachment($download_id);
55         if ($row['filename'] != "")
56         {
57                 if(in_ajax()) {
58                         $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
59                 } else {
60                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
61                 header("Content-type: ".$type);
62                 header('Content-Length: '.$row['filesize']);
63                 header('Content-Disposition: attachment; filename='.$row['filename']);
64                 echo file_get_contents(company_path()."/attachments/".$row['unique_name']);
65                 exit();
66                 }
67         }       
68 }
69
70 $js = "";
71 if ($use_popup_windows)
72         $js .= get_js_open_window(800, 500);
73 page(_($help_context = "Attach Documents"), false, false, "", $js);
74
75 simple_page_mode(true);
76 //----------------------------------------------------------------------------------------
77 if (isset($_GET['filterType'])) // catch up external links
78         $_POST['filterType'] = $_GET['filterType'];
79 if (isset($_GET['trans_no']))
80         $_POST['trans_no'] = $_GET['trans_no'];
81
82 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
83 {
84         if (!$_POST['trans_no'])
85                 display_error(_("No transaction has been selected."));
86         elseif ($Mode == 'ADD_ITEM' && (!isset($_FILES['filename']) || $_FILES['filename']['size'] == 0))
87                 display_error(_("Select attachment file."));
88         else {
89                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
90                 $tmpname = $_FILES['filename']['tmp_name'];
91
92                 $dir =  company_path()."/attachments";
93                 if (!file_exists($dir))
94                 {
95                         mkdir ($dir,0777);
96                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
97                         $fp = fopen($dir."/index.php", "w");
98                         fwrite($fp, $index_file);
99                         fclose($fp);
100                 }
101
102                 $filename = basename($_FILES['filename']['name']);
103                 $filesize = $_FILES['filename']['size'];
104                 $filetype = $_FILES['filename']['type'];
105
106                 // file name compatible with POSIX
107                 // protect against directory traversal
108                 if ($Mode == 'UPDATE_ITEM')
109                 {
110                         $unique_name = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $_POST['unique_name']);
111                         if ($filename && file_exists($dir."/".$unique_name))
112                                 unlink($dir."/".$unique_name);
113                 }
114                 else
115                         $unique_name = uniqid('');
116
117                 //save the file
118                 move_uploaded_file($tmpname, $dir."/".$unique_name);
119
120                 if ($Mode == 'ADD_ITEM')
121                 {
122                         add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
123                                 $filename, $unique_name, $filesize, $filetype);
124                         display_notification(_("Attachment has been inserted.")); 
125                 }
126                 else
127                 {
128                         update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
129                                 $filename, $unique_name, $filesize, $filetype); 
130                         display_notification(_("Attachment has been updated.")); 
131                 }
132         }
133         refresh_pager('trans_table');
134         $Ajax->activate('_page_body');
135         $Mode = 'RESET';
136 }
137
138 if ($Mode == 'Delete')
139 {
140         $row = get_attachment($selected_id);
141         $dir =  company_path()."/attachments";
142         if (file_exists($dir."/".$row['unique_name']))
143                 unlink($dir."/".$row['unique_name']);
144         delete_attachment($selected_id);        
145         display_notification(_("Attachment has been deleted.")); 
146         $Mode = 'RESET';
147 }
148
149 if ($Mode == 'RESET')
150 {
151         unset($_POST['trans_no']);
152         unset($_POST['description']);
153         $selected_id = -1;
154 }
155
156 function viewing_controls()
157 {
158         global $selected_id;
159         
160     start_table(TABLESTYLE_NOBORDER);
161
162         start_row();
163         systypes_list_cells(_("Type:"), 'filterType', null, true);
164         if (list_updated('filterType'))
165                 $selected_id = -1;;
166
167         end_row();
168     end_table(1);
169
170 }
171
172 function trans_view($trans)
173 {
174         return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
175 }
176
177 function edit_link($row)
178 {
179         return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
180 }
181
182 function view_link($row)
183 {
184         return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
185 }
186
187 function download_link($row)
188 {
189         return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
190 }
191
192 function delete_link($row)
193 {
194         return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
195 }
196
197 function display_rows($type)
198 {
199         $sql = get_sql_for_attached_documents($type);
200         $cols = array(
201                 _("#") => array('fun'=>'trans_view', 'ord'=>''),
202             _("Description") => array('name'=>'description'),
203             _("Filename") => array('name'=>'filename'),
204             _("Size") => array('name'=>'filesize'),
205             _("Filetype") => array('name'=>'filetype'),
206             _("Date Uploaded") => array('name'=>'tran_date', 'type'=>'date'),
207                 array('insert'=>true, 'fun'=>'edit_link'),
208                 array('insert'=>true, 'fun'=>'view_link'),
209                 array('insert'=>true, 'fun'=>'download_link'),
210                 array('insert'=>true, 'fun'=>'delete_link')
211             );  
212                 $table =& new_db_pager('trans_tbl', $sql, $cols);
213
214                 $table->width = "60%";
215
216                 display_db_pager($table);
217 }
218
219 //----------------------------------------------------------------------------------------
220
221 start_form(true);
222
223 viewing_controls();
224
225 display_rows($_POST['filterType']);
226
227 br(2);
228
229 start_table(TABLESTYLE2);
230
231 if ($selected_id != -1)
232 {
233         if ($Mode == 'Edit')
234         {
235                 $row = get_attachment($selected_id);
236                 $_POST['trans_no']  = $row["trans_no"];
237                 $_POST['description']  = $row["description"];
238                 hidden('trans_no', $row['trans_no']);
239                 hidden('unique_name', $row['unique_name']);
240                 label_row(_("Transaction #"), $row['trans_no']);
241         }       
242         hidden('selected_id', $selected_id);
243 }
244 else
245         text_row_ex(_("Transaction #").':', 'trans_no', 10);
246 text_row_ex(_("Description").':', 'description', 40);
247 file_row(_("Attached File") . ":", 'filename', 'filename');
248
249 end_table(1);
250
251 submit_add_or_update_center($selected_id == -1, '', 'process');
252
253 end_form();
254
255 end_page();
256
257 ?>