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 ***********************************************************************/
13 $page_security = 'SA_ATTACHDOCUMENT';
15 include_once($path_to_root . "/includes/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
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");
24 if (isset($_GET['vw']))
25 $view_id = $_GET['vw'];
27 $view_id = find_submit('view');
30 $row = get_attachment($view_id);
31 if ($row['filename'] != "")
34 $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
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']);
45 if (isset($_GET['dl']))
46 $download_id = $_GET['dl'];
48 $download_id = find_submit('download');
50 if ($download_id != -1)
52 $row = get_attachment($download_id);
53 if ($row['filename'] != "")
56 $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
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']);
69 if ($SysPrefs->use_popup_windows)
70 $js .= get_js_open_window(800, 500);
71 page(_($help_context = "Attach Documents"), false, false, "", $js);
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'];
80 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
83 if (!transaction_exists($_POST['filterType'], $_POST['trans_no']))
84 display_error(_("Selected transaction does not exists."));
85 elseif ($Mode == 'ADD_ITEM' && !isset($_FILES['filename']))
86 display_error(_("Select attachment file."));
87 elseif ($Mode == 'ADD_ITEM' && ($_FILES['filename']['error'] > 0)) {
88 if ($_FILES['filename']['error'] == UPLOAD_ERR_INI_SIZE)
89 display_error(_("The file size is over the maximum allowed."));
91 display_error(_("Select attachment file."));
94 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
95 $tmpname = $_FILES['filename']['tmp_name'];
97 $dir = company_path()."/attachments";
98 if (!file_exists($dir))
101 $index_file = "<?php\nheader(\"Location: ../index.php\");\n";
102 $fp = fopen($dir."/index.php", "w");
103 fwrite($fp, $index_file);
107 $filename = basename($_FILES['filename']['name']);
108 $filesize = $_FILES['filename']['size'];
109 $filetype = $_FILES['filename']['type'];
111 // file name compatible with POSIX
112 // protect against directory traversal
113 if ($Mode == 'UPDATE_ITEM')
115 $row = get_attachment($selected_id);
116 if ($row['filename'] == "")
118 $unique_name = $row['unique_name'];
119 if ($filename && file_exists($dir."/".$unique_name))
120 unlink($dir."/".$unique_name);
123 $unique_name = random_id();
126 move_uploaded_file($tmpname, $dir."/".$unique_name);
128 if ($Mode == 'ADD_ITEM')
130 add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
131 $filename, $unique_name, $filesize, $filetype);
132 display_notification(_("Attachment has been inserted."));
136 update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
137 $filename, $unique_name, $filesize, $filetype);
138 display_notification(_("Attachment has been updated."));
141 refresh_pager('trans_tbl');
142 $Ajax->activate('_page_body');
146 if ($Mode == 'Delete')
148 $row = get_attachment($selected_id);
149 $dir = company_path()."/attachments";
150 if (file_exists($dir."/".$row['unique_name']))
151 unlink($dir."/".$row['unique_name']);
152 delete_attachment($selected_id);
153 display_notification(_("Attachment has been deleted."));
157 if ($Mode == 'RESET')
159 unset($_POST['description']);
163 function viewing_controls()
167 start_table(TABLESTYLE_NOBORDER);
170 systypes_list_cells(_("Type:"), 'filterType', null, true);
171 if (list_updated('filterType'))
174 if(get_post('filterType') == ST_CUSTOMER ){
175 customer_list_cells(_("Select a customer: "), 'trans_no', null, false, true, true);
176 } elseif(get_post('filterType') == ST_SUPPLIER){
177 supplier_list_cells(_("Select a supplier: "), 'trans_no', null, false, true,true);
185 function trans_view($trans)
187 return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
190 function edit_link($row)
192 return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
195 function view_link($row)
197 return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
200 function download_link($row)
202 return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
205 function delete_link($row)
207 return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
210 function display_rows($type, $trans_no)
212 $sql = get_sql_for_attached_documents($type, $type==ST_SUPPLIER || $type==ST_CUSTOMER ? $trans_no : 0);
214 _("#") => $type == ST_SUPPLIER || $type == ST_CUSTOMER? 'skip' : array('fun'=>'trans_view', 'ord'=>''),
215 _("Description") => array('name'=>'description'),
216 _("Filename") => array('name'=>'filename'),
217 _("Size") => array('name'=>'filesize'),
218 _("Filetype") => array('name'=>'filetype'),
219 _("Date Uploaded") => array('name'=>'tran_date', 'type'=>'date'),
220 array('insert'=>true, 'fun'=>'edit_link'),
221 array('insert'=>true, 'fun'=>'view_link'),
222 array('insert'=>true, 'fun'=>'download_link'),
223 array('insert'=>true, 'fun'=>'delete_link')
226 $table =& new_db_pager('trans_tbl', $sql, $cols);
228 $table->width = "60%";
230 display_db_pager($table);
233 //----------------------------------------------------------------------------------------
234 if (list_updated('filterType') || list_updated('trans_no'))
235 $Ajax->activate('_page_body');
241 $type = get_post('filterType');
243 display_rows($type, get_post('trans_no'));
247 start_table(TABLESTYLE2);
249 if ($selected_id != -1)
253 $row = get_attachment($selected_id);
254 $_POST['trans_no'] = $row["trans_no"];
255 $_POST['description'] = $row["description"];
256 hidden('trans_no', $row['trans_no']);
257 hidden('unique_name', $row['unique_name']);
258 if ($type != ST_SUPPLIER && $type != ST_CUSTOMER)
259 label_row(_("Transaction #"), $row['trans_no']);
261 hidden('selected_id', $selected_id);
264 if ($type != ST_SUPPLIER && $type != ST_CUSTOMER)
265 text_row_ex(_("Transaction #").':', 'trans_no', 10);
267 text_row_ex(_("Description").':', 'description', 40);
268 file_row(_("Attached File") . ":", 'filename', 'filename');
272 submit_add_or_update_center($selected_id == -1, '', 'process');