r172: Removing from our echo; exit; error reporting system to Error::throw()
[bugdar.git] / attachment.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $fetchtemplates = array(
14 'newattach',
15 'editattach'
16 );
17
18 require_once('./global.php');
19
20 if (isset($bugsys->in['attachmentid']))
21 {
22 $attachment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . intval($bugsys->in['attachmentid']));
23 if (!$attachment)
24 {
25 $error->throw('alert: bad attachment');
26 }
27 }
28
29 $bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : intval($bugsys->in['bugid'])));
30 if (!$bug)
31 {
32 $error->throw('alert: bad bug');
33 }
34
35 // setup logging
36 require_once('./includes/class_history.php');
37 $log = new History();
38 $log->bugid = $bug['bugid'];
39
40 // ###################################################################
41
42 if ($_REQUEST['do'] == 'kill')
43 {
44 if (!can_perform('caneditattach'))
45 {
46 $error->throw_permission();
47 }
48
49 $db->query("DELETE FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
50
51 $log->language = 'log_kill_attachment';
52 $log->arguments = array($attachment['attachmentid']);
53 $log->allowempty = true;
54 $log->log();
55
56 echo "<a href=\"showreport.php?bugid=$bug[bugid]\">attachment removed</a>";
57 }
58
59 // ###################################################################
60
61 if ($_REQUEST['do'] == 'delete')
62 {
63 if (!can_perform('caneditattach'))
64 {
65 $error->throw_permission();
66 }
67
68 echo "are you sure you want to delete this attachment? <a href=\"attachment.php?do=kill&amp;attachmentid=$attachment[attachmentid]\">yes</a>";
69 }
70
71 // ###################################################################
72
73 if ($_POST['do'] == 'insert')
74 {
75 if (!can_perform('canputattach'))
76 {
77 $error->throw_permission();
78 }
79
80 // create alias
81 $FILE =& $_FILES['attachment'];
82
83 // PHP errors
84 switch ($FILE['error'])
85 {
86 case 0: break;
87 case 1: $error->throw('PHP said the file you uploaded was too big.'); break;
88 case 2: $error->throw('The file exceeds the allowed upload size.'); break;
89 case 3: $error->throw('The file was only partially uploaded.'); break;
90 case 4: $error->throw('The file was not uploaded at all.'); break;
91 case 6: $error->throw('PHP could not find the /tmp directory.'); break;
92 }
93
94 // did it upload?
95 if (!is_uploaded_file($FILE['tmp_name']))
96 {
97 $error->throw('The file you specified did not upload.');
98 }
99
100 // #*# put some MIME-type validation here
101
102 if (!$bugsys->in['description'])
103 {
104 $error->throw('you need a file description!');
105 }
106
107 $filedata = $bugsys->escape(file_get_contents($FILE['tmp_name']), true, true);
108 $time = time();
109
110 // insert an attachment
111 $db->query("
112 INSERT INTO attachment
113 (bugid, filename, mimetype, filesize,
114 attachment, description, dateline, userid)
115 VALUES
116 ($bug[bugid], '" . $bugsys->escape($FILE['name']) . "',
117 '" . $bugsys->escape($FILE['type']) . "', " . intval($FILE['size']) . ",
118 '$filedata', '" . $bugsys->in['description'] . "', $time,
119 " . $bugsys->userinfo['userid'] . "
120 )"
121 );
122
123 $attachmentid = $db->insert_id();
124 $log->language = 'log_new_attachment';
125 $log->arguments = array($FILE['name'], $attachmentid);
126 $log->allowempty = true;
127 $log->log();
128
129 // mark obsoletes
130 $obsoletes = $_POST['obsoletes'];
131 if (count($obsoletes) > 0)
132 {
133 array_walk($obsoletes, 'intval');
134 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
135
136 $log->language = 'log_mark_obsoletes';
137 $log->arguments = array($attachmentid, $FILE['name'], implode(', ', $obsoletes));
138 $log->log($log->diff('obsoleted attachments', '', implode(', ', $obsoletes)));
139 }
140
141 // handle comment stuff
142 if (can_perform('canpostcomments') AND trim($bugsys->in['comment']))
143 {
144 $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
145
146 if (!$bugsys->options['allowhtml'])
147 {
148 $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
149 }
150
151 $db->query("
152 INSERT INTO " . TABLE_PREFIX . "comment
153 (bugid, userid, dateline, comment, comment_parsed)
154 VALUES
155 ($bug[bugid], " . $bugsys->userinfo['userid'] . ",
156 $time, '" . $bugsys->in['comment'] . "',
157 '" . nl2br($bugsys->in['comment_parsed']) . "'
158 )"
159 );
160
161 $commentid = $db->insert_id();
162
163 $log->language = 'log_new_attachment_comment';
164 $log->arguments = array($attachmentid, $commentid);
165 $log->allowempty = true;
166 $log->log();
167 }
168
169 // update the last post data
170 $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = $time, lastpostby = " . $bugsys->userinfo['userid'] . " WHERE bugid = $bug[bugid]");
171
172 echo "<a href=\"showreport.php?bugid=$bug[bugid]\">attachment added</a>";
173 }
174
175 // ###################################################################
176
177 if ($_REQUEST['do'] == 'add')
178 {
179 if (!can_perform('canputattach'))
180 {
181 $error->throw_permission();
182 }
183
184 $MAXFILESIZE = $funct->fetch_max_attachment_size();
185
186 $show['addcomment'] = ((can_perform('canpostcomments')) ? true : false);
187 $show['obsoletes'] = false;
188
189 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
190 $obsoletes = '';
191 while ($obsolete = $db->fetch_array($obsoletes_fetch))
192 {
193 $show['obsoletes'] = true;
194 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\" /> $obsolete[filename] [$obsolete[description]]</div>\n";
195 }
196
197 eval('$template->flush("' . $template->fetch('newattach') . '");');
198 }
199
200 // ###################################################################
201
202 if ($_POST['do'] == 'update')
203 {
204 if (!(can_perform('caneditattach') OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach'))))
205 {
206 $error->throw_permission();
207 }
208
209 $db->query("
210 UPDATE " . TABLE_PREFIX . "attachment
211 SET description = '" . $bugsys->in['description'] . "',
212 obsolete = " . intval($bugsys->in['obsolete']) . "
213 WHERE attachmentid = " . intval($bugsys->in['attachmentid'])
214 );
215
216 $hist[1] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
217
218 $diff[0] = array_diff_assoc($attachment, $hist[1]);
219 $diff[1] = array_diff_assoc($hist[1], $attachment);
220
221 $log->language = 'log_update_attachment';
222 $log->arguments = array($attachment['attachmentid']);
223 $log->log($log->diff('description', $diff[0]['description'], $diff[1]['description']));
224 $log->log($log->diff('obsolete', $diff[0]['obsolete'], $diff[1]['obsolete']));
225
226 echo "<a href=\"showreport.php?bugid=$bug[bugid]\">attachment updated</a>";
227 }
228
229 // ###################################################################
230
231 if ($_REQUEST['do'] == 'edit')
232 {
233 if (!(can_perform('caneditattach') OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach'))))
234 {
235 $error->throw_permission();
236 }
237
238 $show['delete'] = ((can_perform('caneditattach')) ? true : false);
239
240 eval('$template->flush("' . $template->fetch('editattach') . '");');
241 }
242
243 /*=====================================================================*\
244 || ###################################################################
245 || # $HeadURL$
246 || # $Id$
247 || ###################################################################
248 \*=====================================================================*/
249 ?>