r489: Cleaning up the logging system to work a little better
[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 define('SVN', '$Id$');
19
20 $focus['showreport'] = 'focus';
21
22 require_once('./global.php');
23
24 if (isset($bugsys->in['attachmentid']))
25 {
26 $attachment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . intval($bugsys->in['attachmentid']));
27 if (!$attachment)
28 {
29 $message->error($lang->getlex('error_invalid_id'));
30 }
31 }
32
33 $bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : intval($bugsys->in['bugid'])));
34 if (!$bug)
35 {
36 $message->error($lang->getlex('error_invalid_id'));
37 }
38
39 // setup logging
40 require_once('./includes/class_history.php');
41 $log = new History();
42 $log->bugid = $bug['bugid'];
43
44 // ###################################################################
45
46 if ($_REQUEST['do'] == 'kill')
47 {
48 if (!can_perform('caneditattach'))
49 {
50 $message->error_permission();
51 }
52
53 $db->query("DELETE FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
54
55 $log->arguments = array($attachment['attachmentid']);
56 $log->allowempty = true;
57 $log->log();
58
59 $message->redirect($lang->string('The attachment has been removed.'), "showreport.php?bugid=$bug[bugid]");
60 }
61
62 // ###################################################################
63
64 if ($_REQUEST['do'] == 'delete')
65 {
66 if (!can_perform('caneditattach'))
67 {
68 $message->error_permission();
69 }
70
71 $message->message(sprintf($lang->string('Are you sure want to delete this attachment (id: %1$s)? <a href="attachment.php?do=kill&amp;attachmentid=%1$s">Yes</a>'), $attachment['attachmentid']));
72 }
73
74 // ###################################################################
75
76 if ($_POST['do'] == 'insert')
77 {
78 if (!can_perform('canputattach'))
79 {
80 $message->error_permission();
81 }
82
83 // create alias
84 $FILE =& $_FILES['attachment'];
85
86 // PHP errors
87 switch ($FILE['error'])
88 {
89 case 0: break;
90 case 1: $message->error($lang->string('PHP said the file you uploaded was too big.')); break;
91 case 2: $message->error($lang->string('The file exceeds the allowed upload size.')); break;
92 case 3: $message->error($lang->string('The file was only partially uploaded.')); break;
93 case 4: $message->error($lang->string('The file was not uploaded at all.')); break;
94 case 6: $message->error($lang->string('PHP could not find the /tmp directory.')); break;
95 }
96
97 // did it upload?
98 if (!is_uploaded_file($FILE['tmp_name']))
99 {
100 $message->error($lang->string('The file you specified did not upload.'));
101 }
102
103 // #*# put some MIME-type validation here
104
105 if (!$bugsys->in['description'])
106 {
107 $message->error($lang->string('You need to specify a file description.'));
108 }
109
110 $filedata = $bugsys->escape(file_get_contents($FILE['tmp_name']), true, true);
111 $time = TIMENOW;
112
113 // insert an attachment
114 $db->query("
115 INSERT INTO attachment
116 (bugid, filename, mimetype, filesize,
117 attachment, description, dateline, userid)
118 VALUES
119 ($bug[bugid], '" . $bugsys->escape($FILE['name']) . "',
120 '" . $bugsys->escape($FILE['type']) . "', " . intval($FILE['size']) . ",
121 '$filedata', '" . $bugsys->in['description'] . "', $time,
122 " . $bugsys->userinfo['userid'] . "
123 )"
124 );
125
126 $attachmentid = $db->insert_id();
127 $log->arguments = array($FILE['name'], $attachmentid);
128 $log->allowempty = true;
129 $log->log();
130
131 // mark obsoletes
132 $obsoletes = $_POST['obsoletes'];
133 if (count($obsoletes) > 0)
134 {
135 array_walk($obsoletes, 'intval');
136 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
137
138 $log->arguments = array($attachmentid, $FILE['name'], implode(', ', $obsoletes));
139 $log->log($log->diff($lang->string('Obsoleted attachments'), '', implode(', ', $obsoletes)));
140 }
141
142 // handle comment stuff
143 if (can_perform('canpostcomments') AND trim($bugsys->in['comment']))
144 {
145 $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
146
147 if (!$bugsys->options['allowhtml'])
148 {
149 $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
150 }
151
152 $db->query("
153 INSERT INTO " . TABLE_PREFIX . "comment
154 (bugid, userid, dateline, comment, comment_parsed)
155 VALUES
156 ($bug[bugid], " . $bugsys->userinfo['userid'] . ",
157 $time, '" . $bugsys->in['comment'] . "',
158 '" . nl2br($bugsys->in['comment_parsed']) . "'
159 )"
160 );
161
162 $commentid = $db->insert_id();
163
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 $message->redirect($lang->string('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
173 }
174
175 // ###################################################################
176
177 if ($_REQUEST['do'] == 'add')
178 {
179 if (!can_perform('canputattach'))
180 {
181 $message->error_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 $message->error_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->arguments = array($attachment['attachmentid']);
222 $log->log($log->diff('description', $diff[0]['description'], $diff[1]['description']));
223 $log->log($log->diff('obsolete', $diff[0]['obsolete'], $diff[1]['obsolete']));
224
225 $message->redirect($lang->string('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
226 }
227
228 // ###################################################################
229
230 if ($_REQUEST['do'] == 'edit')
231 {
232 if (!(can_perform('caneditattach') OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach'))))
233 {
234 $message->error_permission();
235 }
236
237 $show['delete'] = ((can_perform('caneditattach')) ? true : false);
238
239 eval('$template->flush("' . $template->fetch('editattach') . '");');
240 }
241
242 /*=====================================================================*\
243 || ###################################################################
244 || # $HeadURL$
245 || # $Id$
246 || ###################################################################
247 \*=====================================================================*/
248 ?>