Bug 5352: missing global declaration in reset_form() in attachments.php. Fixed.
[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         $filename = basename($_FILES['filename']['name']);
84         if (!transaction_exists($_POST['filterType'], $_POST['trans_no']))
85                 display_error(_("Selected transaction does not exists."));
86         elseif ($Mode == 'ADD_ITEM' && !in_array(strtoupper(substr($filename, strlen($filename) - 3)), array('JPG','PNG','GIF', 'PDF', 'DOC', 'ODT')))
87         {
88                 display_error(_('Only graphics,pdf,doc and odt files are supported.'));
89         } elseif ($Mode == 'ADD_ITEM' && !isset($_FILES['filename']))
90                 display_error(_("Select attachment file."));
91         elseif ($Mode == 'ADD_ITEM' && ($_FILES['filename']['error'] > 0)) {
92         if ($_FILES['filename']['error'] == UPLOAD_ERR_INI_SIZE) 
93                         display_error(_("The file size is over the maximum allowed."));
94         else
95                         display_error(_("Select attachment file."));
96         } elseif ( strlen($filename) > 60) {
97                 display_error(_("File name exceeds maximum of 60 chars. Please change filename and try again."));
98         } else {
99                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
100                 $tmpname = $_FILES['filename']['tmp_name'];
101
102                 $dir =  company_path()."/attachments";
103                 if (!file_exists($dir))
104                 {
105                         mkdir ($dir,0777);
106                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n";
107                         $fp = fopen($dir."/index.php", "w");
108                         fwrite($fp, $index_file);
109                         fclose($fp);
110                 }
111
112                 $filesize = $_FILES['filename']['size'];
113                 $filetype = $_FILES['filename']['type'];
114
115                 // file name compatible with POSIX
116                 // protect against directory traversal
117                 if ($Mode == 'UPDATE_ITEM')
118                 {
119                     $row = get_attachment($selected_id);
120                     if ($row['filename'] == "")
121                         exit();
122                         $unique_name = $row['unique_name'];
123                         if ($filename && file_exists($dir."/".$unique_name))
124                                 unlink($dir."/".$unique_name);
125                 }
126                 else
127                         $unique_name = random_id();
128
129                 //save the file
130                 move_uploaded_file($tmpname, $dir."/".$unique_name);
131
132                 if ($Mode == 'ADD_ITEM')
133                 {
134                         add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
135                                 $filename, $unique_name, $filesize, $filetype);
136                         display_notification(_("Attachment has been inserted.")); 
137                 }
138                 else
139                 {
140                         update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
141                                 $filename, $unique_name, $filesize, $filetype); 
142                         display_notification(_("Attachment has been updated.")); 
143                 }
144                 reset_form();
145         }
146         refresh_pager('trans_tbl');
147         $Ajax->activate('_page_body');
148 }
149
150 if ($Mode == 'Delete')
151 {
152         $row = get_attachment($selected_id);
153         $dir =  company_path()."/attachments";
154         if (file_exists($dir."/".$row['unique_name']))
155                 unlink($dir."/".$row['unique_name']);
156         delete_attachment($selected_id);        
157         display_notification(_("Attachment has been deleted.")); 
158         reset_form();
159 }
160
161 if ($Mode == 'RESET')
162         reset_form();
163
164 function reset_form()
165 {
166         global $selected_id;
167         unset($_POST['trans_no']);
168         unset($_POST['description']);
169         $selected_id = -1;
170 }
171
172 function viewing_controls()
173 {
174         global $selected_id;
175         
176     start_table(TABLESTYLE_NOBORDER);
177
178         start_row();
179         systypes_list_cells(_("Type:"), 'filterType', null, true);
180         if (list_updated('filterType'))
181                 reset_form();
182
183         if(get_post('filterType') == ST_CUSTOMER ){
184                 customer_list_cells(_("Select a customer: "), 'trans_no', null, false, true, true);
185         } elseif(get_post('filterType') == ST_SUPPLIER){
186                 supplier_list_cells(_("Select a supplier: "), 'trans_no', null,  false, true,true);
187         }
188
189         end_row();
190     end_table(1);
191
192 }
193
194 function trans_view($trans)
195 {
196         return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
197 }
198
199 function edit_link($row)
200 {
201         return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
202 }
203
204 function view_link($row)
205 {
206         return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
207 }
208
209 function download_link($row)
210 {
211         return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
212 }
213
214 function delete_link($row)
215 {
216         return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
217 }
218
219 function display_rows($type, $trans_no)
220 {
221         $sql = get_sql_for_attached_documents($type, $type==ST_SUPPLIER || $type==ST_CUSTOMER ? $trans_no : 0);
222         $cols = array(
223                 _("#") => $type == ST_SUPPLIER || $type == ST_CUSTOMER? 'skip' : array('fun'=>'trans_view', 'ord'=>''),
224             _("Description") => array('name'=>'description'),
225             _("Filename") => array('name'=>'filename'),
226             _("Size") => array('name'=>'filesize'),
227             _("Filetype") => array('name'=>'filetype'),
228             _("Date Uploaded") => array('name'=>'tran_date', 'type'=>'date'),
229                 array('insert'=>true, 'fun'=>'edit_link'),
230                 array('insert'=>true, 'fun'=>'view_link'),
231                 array('insert'=>true, 'fun'=>'download_link'),
232                 array('insert'=>true, 'fun'=>'delete_link')
233             );  
234
235         $table =& new_db_pager('trans_tbl', $sql, $cols);
236
237         $table->width = "60%";
238
239         display_db_pager($table);
240 }
241
242 //----------------------------------------------------------------------------------------
243 if (list_updated('filterType') || list_updated('trans_no'))
244         $Ajax->activate('_page_body');
245
246 start_form(true);
247
248 viewing_controls();
249
250 $type = get_post('filterType');
251
252 display_rows($type, get_post('trans_no'));
253
254 br(2);
255
256 start_table(TABLESTYLE2);
257
258 if ($selected_id != -1)
259 {
260         if ($Mode == 'Edit')
261         {
262                 $row = get_attachment($selected_id);
263                 $_POST['trans_no']  = $row["trans_no"];
264                 $_POST['description']  = $row["description"];
265                 hidden('trans_no', $row['trans_no']);
266                 hidden('unique_name', $row['unique_name']);
267                 if ($type != ST_SUPPLIER && $type != ST_CUSTOMER)
268                         label_row(_("Transaction #"), $row['trans_no']);
269         }       
270         hidden('selected_id', $selected_id);
271 }
272 else {
273         if ($type != ST_SUPPLIER && $type != ST_CUSTOMER)
274                 text_row_ex(_("Transaction #").':', 'trans_no', 10);
275 }
276 text_row_ex(_("Description").':', 'description', 40);
277 file_row(_("Attached File") . ":", 'filename', 'filename');
278
279 end_table(1);
280
281 submit_add_or_update_center($selected_id == -1, '', 'process');
282
283 end_form();
284
285 end_page();
286