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/session.inc");
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20 include_once($path_to_root . "/admin/db/attachments_db.inc");
22 if (isset($_GET['vw']))
23 $view_id = $_GET['vw'];
25 $view_id = find_submit('view');
28 $row = get_attachment($view_id);
29 if ($row['filename'] != "")
32 $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
34 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
35 header("Content-type: ".$type);
36 header('Content-Length: '.$row['filesize']);
37 //if ($type == 'application/octet-stream')
38 // header('Content-Disposition: attachment; filename='.$row['filename']);
40 header("Content-Disposition: inline");
41 echo file_get_contents(company_path(). "/attachments/".$row['unique_name']);
46 if (isset($_GET['dl']))
47 $download_id = $_GET['dl'];
49 $download_id = find_submit('download');
51 if ($download_id != -1)
53 $row = get_attachment($download_id);
54 if ($row['filename'] != "")
57 $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
59 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
60 header("Content-type: ".$type);
61 header('Content-Length: '.$row['filesize']);
62 header('Content-Disposition: attachment; filename='.$row['filename']);
63 echo file_get_contents(company_path()."/attachments/".$row['unique_name']);
70 if ($use_popup_windows)
71 $js .= get_js_open_window(800, 500);
72 page(_($help_context = "Attach Documents"), false, false, "", $js);
74 simple_page_mode(true);
75 //----------------------------------------------------------------------------------------
76 if (isset($_GET['filterType'])) // catch up external links
77 $_POST['filterType'] = $_GET['filterType'];
78 if (isset($_GET['trans_no']))
79 $_POST['trans_no'] = $_GET['trans_no'];
81 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
83 if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
85 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
86 $tmpname = $_FILES['filename']['tmp_name'];
88 $dir = company_path()."/attachments";
89 if (!file_exists($dir))
92 $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
93 $fp = fopen($dir."/index.php", "w");
94 fwrite($fp, $index_file);
97 // file name compatible with POSIX
98 // protect against directory traversal
99 if ($Mode == 'UPDATE_ITEM')
101 $unique_name = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $_POST['unique_name']);
102 if ($Mode == 'UPDATE_ITEM' && file_exists($dir."/".$unique_name))
103 unlink($dir."/".$unique_name);
106 $unique_name = uniqid('');
107 move_uploaded_file($tmpname, $dir."/".$unique_name);
110 $filename = basename($_FILES['filename']['name']);
111 $filesize = $_FILES['filename']['size'];
112 $filetype = $_FILES['filename']['type'];
116 $unique_name = $filename = $filetype = "";
119 if ($Mode == 'ADD_ITEM')
121 add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
122 $filename, $unique_name, $filesize, $filetype);
123 display_notification(_("Attachment has been inserted."));
127 update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
128 $filename, $unique_name, $filesize, $filetype);
129 display_notification(_("Attachment has been updated."));
134 if ($Mode == 'Delete')
136 $row = get_attachment($selected_id);
137 $dir = company_path()."/attachments";
138 if (file_exists($dir."/".$row['unique_name']))
139 unlink($dir."/".$row['unique_name']);
140 delete_attachment($selected_id);
141 display_notification(_("Attachment has been deleted."));
145 if ($Mode == 'RESET')
147 unset($_POST['trans_no']);
148 unset($_POST['description']);
152 function viewing_controls()
156 start_table(TABLESTYLE_NOBORDER);
159 systypes_list_cells(_("Type:"), 'filterType', null, true);
160 if (list_updated('filterType'))
168 function display_rows($type)
170 $rows = get_attached_documents($type);
171 $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
173 start_table(TABLESTYLE);
176 while ($row = db_fetch($rows))
178 alt_table_row_color($k);
180 label_cell(get_trans_view_str($type, $row['trans_no']));
181 label_cell($row['description']);
182 label_cell($row['filename']);
183 label_cell($row['filesize']);
184 label_cell($row['filetype']);
185 label_cell(sql2date($row['tran_date']));
186 edit_button_cell("Edit".$row['id'], _("Edit"));
187 button_cell("view".$row['id'], _("View"), false, ICON_VIEW);
188 button_cell("download".$row['id'], _("Download"), false, ICON_DOWN);
189 delete_button_cell("Delete".$row['id'], _("Delete"));
195 //----------------------------------------------------------------------------------------
201 display_rows($_POST['filterType']);
204 start_table(TABLESTYLE2);
206 if ($selected_id != -1)
210 $row = get_attachment($selected_id);
211 $_POST['trans_no'] = $row["trans_no"];
212 $_POST['description'] = $row["description"];
213 hidden('trans_no', $row['trans_no']);
214 hidden('unique_name', $row['unique_name']);
215 label_row(_("Transaction #"), $row['trans_no']);
217 hidden('selected_id', $selected_id);
220 text_row_ex(_("Transaction #").':', 'trans_no', 10);
221 text_row_ex(_("Description").':', 'description', 40);
222 file_row(_("Attached File") . ":", 'filename', 'filename');
226 submit_add_or_update_center($selected_id == -1, '', 'both');