Moved all SQL statements from PHP files into relevant *_db.inc files.
[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/session.inc");
16
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");
21
22 if (isset($_GET['vw']))
23         $view_id = $_GET['vw'];
24 else
25 $view_id = find_submit('view');
26 if ($view_id != -1)
27 {
28         $row = get_attachment($view_id);
29         if ($row['filename'] != "")
30         {
31                 if(in_ajax()) {
32                         $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
33                 } else {
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']);
39                 else
40                                 header("Content-Disposition: inline");
41                 echo file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
42                 exit();
43                 }
44         }       
45 }
46 if (isset($_GET['dl']))
47         $download_id = $_GET['dl'];
48 else
49         $download_id = find_submit('download');
50
51 if ($download_id != -1)
52 {
53         $row = get_attachment($download_id);
54         if ($row['filename'] != "")
55         {
56                 if(in_ajax()) {
57                         $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
58                 } else {
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($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
64                 exit();
65                 }
66         }       
67 }
68
69 $js = "";
70 if ($use_popup_windows)
71         $js .= get_js_open_window(800, 500);
72 page(_($help_context = "Attach Documents"), false, false, "", $js);
73
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'];
80         
81 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
82 {
83         if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
84         {
85                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
86                 $tmpname = $_FILES['filename']['tmp_name'];
87
88                 $dir =  $comp_path."/".user_company(). "/attachments";
89                 if (!file_exists($dir))
90                 {
91                         mkdir ($dir,0777);
92                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
93                         $fp = fopen($dir."/index.php", "w");
94                         fwrite($fp, $index_file);
95                         fclose($fp);
96                 }
97                 if ($Mode == 'UPDATE_ITEM' && file_exists($dir."/".$_POST['unique_name']))
98                         unlink($dir."/".$_POST['unique_name']);
99
100                 $unique_name = uniqid('');
101                 move_uploaded_file($tmpname, $dir."/".$unique_name);
102                 //save the file
103                 $filename = $_FILES['filename']['name'];
104                 $filesize = $_FILES['filename']['size'];
105                 $filetype = $_FILES['filename']['type'];
106         }
107         else
108         {
109                 $unique_name = $filename = $filetype = "";
110                 $filesize = 0;
111         }
112         if ($Mode == 'ADD_ITEM')
113         {
114                 add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
115                         $filename, $unique_name, $filesize, $filetype);
116                 display_notification(_("Attachment has been inserted.")); 
117         }
118         else
119         {
120                 update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
121                         $filename, $unique_name, $filesize, $filetype); 
122                 display_notification(_("Attachment has been updated.")); 
123         }
124         $Mode = 'RESET';
125 }               
126
127 if ($Mode == 'Delete')
128 {
129         $row = get_attachment($selected_id);
130         $dir =  $comp_path."/".user_company(). "/attachments";
131         if (file_exists($dir."/".$row['unique_name']))
132                 unlink($dir."/".$row['unique_name']);
133         delete_attachment($selected_id);        
134         display_notification(_("Attachment has been deleted.")); 
135         $Mode = 'RESET';
136 }
137
138 if ($Mode == 'RESET')
139 {
140         unset($_POST['trans_no']);
141         unset($_POST['description']);
142         $selected_id = -1;
143 }
144
145 function viewing_controls()
146 {
147     start_form();
148
149     start_table("class='tablestyle_noborder'");
150
151         systypes_list_row(_("Type:"), 'filterType', null, true);
152
153     end_table(1);
154
155         end_form();
156 }
157
158 function display_rows($type)
159 {
160         global $table_style;
161
162         $rows = get_attached_documents($type);
163         $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
164         
165         div_start('transactions');
166         start_form();
167         start_table($table_style);
168         table_header($th);
169         $k = 0;
170         while ($row = db_fetch($rows))
171         {
172                 alt_table_row_color($k);
173                 
174                 label_cell(get_trans_view_str($type, $row['trans_no']));
175                 label_cell($row['description']);
176                 label_cell($row['filename']);
177                 label_cell($row['filesize']);
178                 label_cell($row['filetype']);
179                 label_cell(sql2date($row['tran_date']));
180                 edit_button_cell("Edit".$row['id'], _("Edit"));
181                 button_cell("view".$row['id'], _("View"), false, ICON_VIEW);
182                 button_cell("download".$row['id'], _("Download"), false, ICON_DOWN);
183                 delete_button_cell("Delete".$row['id'], _("Delete"));
184         end_row();
185         }       
186         end_table(1);
187         hidden('filterType', $type);
188         end_form();
189         div_end();
190 }
191
192 //----------------------------------------------------------------------------------------
193
194 viewing_controls();
195
196 if (isset($_POST['filterType']))
197         display_rows($_POST['filterType']);
198
199 start_form(true);
200
201 start_table($table_style2);
202
203 if ($selected_id != -1)
204 {
205         if ($Mode == 'Edit')
206         {
207                 $row = get_attachment($selected_id);
208                 $_POST['trans_no']  = $row["trans_no"];
209                 $_POST['description']  = $row["description"];
210                 hidden('trans_no', $row['trans_no']);
211                 hidden('unique_name', $row['unique_name']);
212                 label_row(_("Transaction #"), $row['trans_no']);
213         }       
214         hidden('selected_id', $selected_id);
215 }
216 else
217         text_row_ex(_("Transaction #").':', 'trans_no', 10);
218 text_row_ex(_("Description").':', 'description', 40);
219 start_row();
220 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
221 end_row();
222
223 end_table(1);
224 if (isset($_POST['filterType']))
225         hidden('filterType', $_POST['filterType']);
226
227 submit_add_or_update_center($selected_id == -1, '', 'both');
228
229 end_form();
230
231 end_page();
232
233 ?>