565e4b1ed836e7b177c3a36974ce04f0930fd5c8
[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 include_once($path_to_root . "/admin/db/transactions_db.inc");
23
24 if (isset($_GET['vw']))
25         $view_id = $_GET['vw'];
26 else
27         $view_id = find_submit('view');
28 if ($view_id != -1)
29 {
30         $row = get_attachment($view_id);
31         if ($row['filename'] != "")
32         {
33                 if(in_ajax()) {
34                         $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
35                 } else {
36                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
37                 header("Content-type: ".$type);
38                 header('Content-Length: '.$row['filesize']);
39                         header("Content-Disposition: inline");
40                 echo file_get_contents(company_path(). "/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(company_path()."/attachments/".$row['unique_name']);
63                 exit();
64                 }
65         }       
66 }
67
68 $js = "";
69 if ($SysPrefs->use_popup_windows)
70         $js .= get_js_open_window(800, 500);
71 page(_($help_context = "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         
83         if($_POST['filterType'] == ST_CUSTOMER){
84                 $_POST['trans_no'] = $_POST['customer_id'];
85         }elseif($_POST['filterType'] == ST_SUPPLIER){
86                 $_POST['trans_no'] = $_POST['supplier_id'];
87         }
88
89         if (!transaction_exists($_POST['filterType'], $_POST['trans_no']))
90                 display_error(_("Selected transaction does not exists."));
91         elseif ($Mode == 'ADD_ITEM' && !isset($_FILES['filename']))
92                 display_error(_("Select attachment file."));
93         elseif ($Mode == 'ADD_ITEM' && ($_FILES['filename']['error'] > 0)) {
94         if ($_FILES['filename']['error'] == UPLOAD_ERR_INI_SIZE) 
95                         display_error(_("The file size is over the maximum allowed."));
96         else
97                         display_error(_("Select attachment file."));
98         }
99         else {
100                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
101                 $tmpname = $_FILES['filename']['tmp_name'];
102
103                 $dir =  company_path()."/attachments";
104                 if (!file_exists($dir))
105                 {
106                         mkdir ($dir,0777);
107                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n";
108                         $fp = fopen($dir."/index.php", "w");
109                         fwrite($fp, $index_file);
110                         fclose($fp);
111                 }
112
113                 $filename = basename($_FILES['filename']['name']);
114                 $filesize = $_FILES['filename']['size'];
115                 $filetype = $_FILES['filename']['type'];
116
117                 // file name compatible with POSIX
118                 // protect against directory traversal
119                 if ($Mode == 'UPDATE_ITEM')
120                 {
121                     $row = get_attachment($selected_id);
122                     if ($row['filename'] == "")
123                         exit();
124                         $unique_name = $row['unique_name'];
125                         if ($filename && file_exists($dir."/".$unique_name))
126                                 unlink($dir."/".$unique_name);
127                 }
128                 else
129                         $unique_name = random_id();
130
131                 //save the file
132                 move_uploaded_file($tmpname, $dir."/".$unique_name);
133
134                 if ($Mode == 'ADD_ITEM')
135                 {
136                         add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
137                                 $filename, $unique_name, $filesize, $filetype);
138                         display_notification(_("Attachment has been inserted.")); 
139                 }
140                 else
141                 {
142                         update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
143                                 $filename, $unique_name, $filesize, $filetype); 
144                         display_notification(_("Attachment has been updated.")); 
145                 }
146         }
147         refresh_pager('trans_tbl');
148         $Ajax->activate('_page_body');
149         $Mode = 'RESET';
150 }
151
152 if ($Mode == 'Delete')
153 {
154         $row = get_attachment($selected_id);
155         $dir =  company_path()."/attachments";
156         if (file_exists($dir."/".$row['unique_name']))
157                 unlink($dir."/".$row['unique_name']);
158         delete_attachment($selected_id);        
159         display_notification(_("Attachment has been deleted.")); 
160         $Mode = 'RESET';
161 }
162
163 if ($Mode == 'RESET')
164 {
165         unset($_POST['trans_no']);
166         unset($_POST['description']);
167         $selected_id = -1;
168 }
169
170 function viewing_controls()
171 {
172         global $selected_id;
173         
174     start_table(TABLESTYLE_NOBORDER);
175
176         start_row();
177         systypes_list_cells(_("Type:"), 'filterType', null, true);
178         if (list_updated('filterType'))
179                 $selected_id = -1;
180
181         if(get_post('filterType') == ST_CUSTOMER ){
182                 customer_list_cells(_("Select a customer: "), 'customer_id', null, _('Select customer'), true, true);
183         } elseif(get_post('filterType') == ST_SUPPLIER){
184                 supplier_list_cells(_("Select a supplier: "), 'supplier_id', null,  _('Select supplier'), true,true);
185         }
186
187         end_row();
188     end_table(1);
189
190 }
191
192 function trans_view($trans)
193 {
194         return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
195 }
196
197 function edit_link($row)
198 {
199         return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
200 }
201
202 function view_link($row)
203 {
204         return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
205 }
206
207 function download_link($row)
208 {
209         return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
210 }
211
212 function delete_link($row)
213 {
214         return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
215 }
216
217 function display_rows($type, $id_no)
218 {
219         $sql = get_sql_for_attached_documents($type, $id_no);
220         $cols = array(
221                 _("#") => array('fun'=>'trans_view', 'ord'=>''),
222             _("Description") => array('name'=>'description'),
223             _("Filename") => array('name'=>'filename'),
224             _("Size") => array('name'=>'filesize'),
225             _("Filetype") => array('name'=>'filetype'),
226             _("Date Uploaded") => array('name'=>'tran_date', 'type'=>'date'),
227                 array('insert'=>true, 'fun'=>'edit_link'),
228                 array('insert'=>true, 'fun'=>'view_link'),
229                 array('insert'=>true, 'fun'=>'download_link'),
230                 array('insert'=>true, 'fun'=>'delete_link')
231             );  
232                 $table =& new_db_pager('trans_tbl', $sql, $cols);
233
234                 $table->width = "60%";
235
236                 display_db_pager($table);
237 }
238
239 //----------------------------------------------------------------------------------------
240
241 start_form(true);
242
243 viewing_controls();
244 $id_no = ($_POST['filterType'] == ST_CUSTOMER) ? get_post('customer_id') : get_post('supplier_id');
245 display_rows($_POST['filterType'], $id_no);
246
247 br(2);
248
249 start_table(TABLESTYLE2);
250
251 if ($selected_id != -1)
252 {
253         if ($Mode == 'Edit')
254         {
255                 $row = get_attachment($selected_id);
256                 $_POST['trans_no']  = $row["trans_no"];
257                 $_POST['description']  = $row["description"];
258                 hidden('trans_no', $row['trans_no']);
259                 hidden('unique_name', $row['unique_name']);
260                 label_row(_("Transaction #"), $row['trans_no']);
261         }       
262         hidden('selected_id', $selected_id);
263 }
264 else {
265         if( $id_no == 0 )
266                 text_row_ex(_("Transaction #").':', 'trans_no', 10);
267         else
268                 hidden('trans_no', $id_no);
269 }
270 text_row_ex(_("Description").':', 'description', 40);
271 file_row(_("Attached File") . ":", 'filename', 'filename');
272
273 end_table(1);
274
275 submit_add_or_update_center($selected_id == -1, '', 'process');
276
277 end_form();
278
279 end_page();
280