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 //if ($type == 'application/octet-stream')
40 // header('Content-Disposition: attachment; filename='.$row['filename']);
42 header("Content-Disposition: inline");
43 echo file_get_contents(company_path(). "/attachments/".$row['unique_name']);
48 if (isset($_GET['dl']))
49 $download_id = $_GET['dl'];
51 $download_id = find_submit('download');
53 if ($download_id != -1)
55 $row = get_attachment($download_id);
56 if ($row['filename'] != "")
59 $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
61 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
62 header("Content-type: ".$type);
63 header('Content-Length: '.$row['filesize']);
64 header('Content-Disposition: attachment; filename='.$row['filename']);
65 echo file_get_contents(company_path()."/attachments/".$row['unique_name']);
72 if ($SysPrefs->use_popup_windows)
73 $js .= get_js_open_window(800, 500);
74 page(_($help_context = "Attach Documents"), false, false, "", $js);
76 simple_page_mode(true);
77 //----------------------------------------------------------------------------------------
78 if (isset($_GET['filterType'])) // catch up external links
79 $_POST['filterType'] = $_GET['filterType'];
80 if (isset($_GET['trans_no']))
81 $_POST['trans_no'] = $_GET['trans_no'];
83 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
85 if (!transaction_exists($_POST['filterType'], $_POST['trans_no']))
86 display_error(_("Selected transaction does not exists."));
87 elseif ($Mode == 'ADD_ITEM' && !isset($_FILES['filename']))
88 display_error(_("Select attachment file."));
89 elseif ($Mode == 'ADD_ITEM' && ($_FILES['filename']['error'] > 0)) {
90 if ($_FILES['filename']['error'] == UPLOAD_ERR_INI_SIZE)
91 display_error(_("The file size is over the maximum allowed."));
93 display_error(_("Select attachment file."));
96 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
97 $tmpname = $_FILES['filename']['tmp_name'];
99 $dir = company_path()."/attachments";
100 if (!file_exists($dir))
103 $index_file = "<?php\nheader(\"Location: ../index.php\");\n";
104 $fp = fopen($dir."/index.php", "w");
105 fwrite($fp, $index_file);
109 $filename = basename($_FILES['filename']['name']);
110 $filesize = $_FILES['filename']['size'];
111 $filetype = $_FILES['filename']['type'];
113 // file name compatible with POSIX
114 // protect against directory traversal
115 if ($Mode == 'UPDATE_ITEM')
117 $row = get_attachment($selected_id);
118 if ($row['filename'] == "")
120 $unique_name = $row['unique_name'];
121 if ($filename && file_exists($dir."/".$unique_name))
122 unlink($dir."/".$unique_name);
125 $unique_name = uniqid('');
128 move_uploaded_file($tmpname, $dir."/".$unique_name);
130 if ($Mode == 'ADD_ITEM')
132 add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
133 $filename, $unique_name, $filesize, $filetype);
134 display_notification(_("Attachment has been inserted."));
138 update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
139 $filename, $unique_name, $filesize, $filetype);
140 display_notification(_("Attachment has been updated."));
143 refresh_pager('trans_tbl');
144 $Ajax->activate('_page_body');
148 if ($Mode == 'Delete')
150 $row = get_attachment($selected_id);
151 $dir = company_path()."/attachments";
152 if (file_exists($dir."/".$row['unique_name']))
153 unlink($dir."/".$row['unique_name']);
154 delete_attachment($selected_id);
155 display_notification(_("Attachment has been deleted."));
159 if ($Mode == 'RESET')
161 unset($_POST['trans_no']);
162 unset($_POST['description']);
166 function viewing_controls()
170 start_table(TABLESTYLE_NOBORDER);
173 systypes_list_cells(_("Type:"), 'filterType', null, true);
174 if (list_updated('filterType'))
182 function trans_view($trans)
184 return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
187 function edit_link($row)
189 return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
192 function view_link($row)
194 return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
197 function download_link($row)
199 return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
202 function delete_link($row)
204 return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
207 function display_rows($type)
209 $sql = get_sql_for_attached_documents($type);
211 _("#") => array('fun'=>'trans_view', 'ord'=>''),
212 _("Description") => array('name'=>'description'),
213 _("Filename") => array('name'=>'filename'),
214 _("Size") => array('name'=>'filesize'),
215 _("Filetype") => array('name'=>'filetype'),
216 _("Date Uploaded") => array('name'=>'tran_date', 'type'=>'date'),
217 array('insert'=>true, 'fun'=>'edit_link'),
218 array('insert'=>true, 'fun'=>'view_link'),
219 array('insert'=>true, 'fun'=>'download_link'),
220 array('insert'=>true, 'fun'=>'delete_link')
222 $table =& new_db_pager('trans_tbl', $sql, $cols);
224 $table->width = "60%";
226 display_db_pager($table);
229 //----------------------------------------------------------------------------------------
235 display_rows($_POST['filterType']);
239 start_table(TABLESTYLE2);
241 if ($selected_id != -1)
245 $row = get_attachment($selected_id);
246 $_POST['trans_no'] = $row["trans_no"];
247 $_POST['description'] = $row["description"];
248 hidden('trans_no', $row['trans_no']);
249 hidden('unique_name', $row['unique_name']);
250 label_row(_("Transaction #"), $row['trans_no']);
252 hidden('selected_id', $selected_id);
255 text_row_ex(_("Transaction #").':', 'trans_no', 10);
256 text_row_ex(_("Description").':', 'description', 40);
257 file_row(_("Attached File") . ":", 'filename', 'filename');
261 submit_add_or_update_center($selected_id == -1, '', 'process');