2f41d80c14cc15cef973f5dedb2e4b3b0b1232e9
[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                 //if ($type == 'application/octet-stream')
40                 //      header('Content-Disposition: attachment; filename='.$row['filename']);
41                 //else
42                                 header("Content-Disposition: inline");
43                 echo file_get_contents(company_path(). "/attachments/".$row['unique_name']);
44                 exit();
45                 }
46         }       
47 }
48 if (isset($_GET['dl']))
49         $download_id = $_GET['dl'];
50 else
51         $download_id = find_submit('download');
52
53 if ($download_id != -1)
54 {
55         $row = get_attachment($download_id);
56         if ($row['filename'] != "")
57         {
58                 if(in_ajax()) {
59                         $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
60                 } else {
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']);
66                 exit();
67                 }
68         }       
69 }
70
71 $js = "";
72 if ($use_popup_windows)
73         $js .= get_js_open_window(800, 500);
74 page(_($help_context = "Attach Documents"), false, false, "", $js);
75
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'];
82
83 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
84 {
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']) || $_FILES['filename']['size'] == 0))
88                 display_error(_("Select attachment file."));
89         else {
90                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
91                 $tmpname = $_FILES['filename']['tmp_name'];
92
93                 $dir =  company_path()."/attachments";
94                 if (!file_exists($dir))
95                 {
96                         mkdir ($dir,0777);
97                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
98                         $fp = fopen($dir."/index.php", "w");
99                         fwrite($fp, $index_file);
100                         fclose($fp);
101                 }
102
103                 $filename = basename($_FILES['filename']['name']);
104                 $filesize = $_FILES['filename']['size'];
105                 $filetype = $_FILES['filename']['type'];
106
107                 // file name compatible with POSIX
108                 // protect against directory traversal
109                 if ($Mode == 'UPDATE_ITEM')
110                 {
111                     $row = get_attachment($selected_id);
112                     if ($row['filename'] == "")
113                         exit();
114                         $unique_name = $row['unique_name'];
115                         if ($filename && file_exists($dir."/".$unique_name))
116                                 unlink($dir."/".$unique_name);
117                 }
118                 else
119                         $unique_name = uniqid('');
120
121                 //save the file
122                 move_uploaded_file($tmpname, $dir."/".$unique_name);
123
124                 if ($Mode == 'ADD_ITEM')
125                 {
126                         add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
127                                 $filename, $unique_name, $filesize, $filetype);
128                         display_notification(_("Attachment has been inserted.")); 
129                 }
130                 else
131                 {
132                         update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
133                                 $filename, $unique_name, $filesize, $filetype); 
134                         display_notification(_("Attachment has been updated.")); 
135                 }
136         }
137         refresh_pager('trans_tbl');
138         $Ajax->activate('_page_body');
139         $Mode = 'RESET';
140 }
141
142 if ($Mode == 'Delete')
143 {
144         $row = get_attachment($selected_id);
145         $dir =  company_path()."/attachments";
146         if (file_exists($dir."/".$row['unique_name']))
147                 unlink($dir."/".$row['unique_name']);
148         delete_attachment($selected_id);        
149         display_notification(_("Attachment has been deleted.")); 
150         $Mode = 'RESET';
151 }
152
153 if ($Mode == 'RESET')
154 {
155         unset($_POST['trans_no']);
156         unset($_POST['description']);
157         $selected_id = -1;
158 }
159
160 function viewing_controls()
161 {
162         global $selected_id;
163         
164     start_table(TABLESTYLE_NOBORDER);
165
166         start_row();
167         systypes_list_cells(_("Type:"), 'filterType', null, true);
168         if (list_updated('filterType'))
169                 $selected_id = -1;;
170
171         end_row();
172     end_table(1);
173
174 }
175
176 function trans_view($trans)
177 {
178         return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
179 }
180
181 function edit_link($row)
182 {
183         return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
184 }
185
186 function view_link($row)
187 {
188         return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
189 }
190
191 function download_link($row)
192 {
193         return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
194 }
195
196 function delete_link($row)
197 {
198         return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
199 }
200
201 function display_rows($type)
202 {
203         $sql = get_sql_for_attached_documents($type);
204         $cols = array(
205                 _("#") => array('fun'=>'trans_view', 'ord'=>''),
206             _("Description") => array('name'=>'description'),
207             _("Filename") => array('name'=>'filename'),
208             _("Size") => array('name'=>'filesize'),
209             _("Filetype") => array('name'=>'filetype'),
210             _("Date Uploaded") => array('name'=>'tran_date', 'type'=>'date'),
211                 array('insert'=>true, 'fun'=>'edit_link'),
212                 array('insert'=>true, 'fun'=>'view_link'),
213                 array('insert'=>true, 'fun'=>'download_link'),
214                 array('insert'=>true, 'fun'=>'delete_link')
215             );  
216                 $table =& new_db_pager('trans_tbl', $sql, $cols);
217
218                 $table->width = "60%";
219
220                 display_db_pager($table);
221 }
222
223 //----------------------------------------------------------------------------------------
224
225 start_form(true);
226
227 viewing_controls();
228
229 display_rows($_POST['filterType']);
230
231 br(2);
232
233 start_table(TABLESTYLE2);
234
235 if ($selected_id != -1)
236 {
237         if ($Mode == 'Edit')
238         {
239                 $row = get_attachment($selected_id);
240                 $_POST['trans_no']  = $row["trans_no"];
241                 $_POST['description']  = $row["description"];
242                 hidden('trans_no', $row['trans_no']);
243                 hidden('unique_name', $row['unique_name']);
244                 label_row(_("Transaction #"), $row['trans_no']);
245         }       
246         hidden('selected_id', $selected_id);
247 }
248 else
249         text_row_ex(_("Transaction #").':', 'trans_no', 10);
250 text_row_ex(_("Description").':', 'description', 40);
251 file_row(_("Attached File") . ":", 'filename', 'filename');
252
253 end_table(1);
254
255 submit_add_or_update_center($selected_id == -1, '', 'process');
256
257 end_form();
258
259 end_page();
260
261 ?>