]> src.bluestatic.org Git - bugdar.git/blob - attachment.php
r1377: - Added Logging::getCommonFields()
[bugdar.git] / attachment.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'newattach',
24 'editattach'
25 );
26
27 define('SVN', '$Id$');
28
29 $focus['showreport'] = 'focus';
30
31 require_once('./global.php');
32 require_once('./includes/class_api_error.php');
33 require_once('./includes/class_notification.php');
34 require_once('./includes/api_attachment.php');
35 require_once('./includes/api_comment.php');
36
37 APIError(array(new API_Error_Handler($message), 'user_cumulative'));
38
39 if (isset($bugsys->in['attachmentid']))
40 {
41 $attachment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . $bugsys->input_clean('attachmentid', TYPE_UINT));
42 if (!$attachment)
43 {
44 $message->error(L_INVALID_ID);
45 }
46 }
47
48 $bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : $bugsys->input_clean('bugid', TYPE_UINT)));
49 if (!$bug)
50 {
51 $message->error(L_INVALID_ID);
52 }
53
54 if (!check_bug_permissions($bug))
55 {
56 $message->error_permission();
57 }
58
59 require_once('./includes/class_logging.php');
60
61 $notif = new NotificationCenter;
62 $notif->set_bug_data($bug);
63
64 // ###################################################################
65
66 if ($_POST['do'] == 'insert')
67 {
68 $attachapi = new AttachmentAPI($bugsys);
69 $attachapi->set('bugid', $bugsys->in['bugid']);
70
71 if (!can_perform('canputattach', $bug['product']))
72 {
73 $message->error_permission();
74 }
75
76 // max packet size
77 $var = $db->query_first("SHOW VARIABLES LIKE 'max_allowed_packet'");
78 $bugsys->debug("max_allowed_packet = $var[Value]");
79
80 // create alias
81 $FILE =& $_FILES['attachment'];
82
83 // PHP errors
84 switch ($FILE['error'])
85 {
86 case 0: break;
87 case 1: $message->add_error(_('PHP said the file you uploaded was too big.')); break;
88 case 2: $message->add_error(_('The file exceeds the allowed upload size.')); break;
89 case 3: $message->add_error(_('The file was only partially uploaded.')); break;
90 case 4: $message->add_error(_('The file was not uploaded at all.')); break;
91 case 6: $message->add_error(_('PHP could not find the /tmp directory.')); break;
92 }
93
94 // did it upload?
95 if (!is_uploaded_file($FILE['tmp_name']))
96 {
97 $message->add_error(_('The file you specified did not upload.'));
98 }
99
100 // TODO - put some MIME-type validation here
101
102 if (filesize($FILE['tmp_name']) > $var['Value'])
103 {
104 $message->add_error(_('The file you specified exceeds MySQL\'s maximum allowed packet.'));
105 }
106
107 $attachapi->set('attachment', file_get_contents($FILE['tmp_name']));
108 $attachapi->set('filename', $FILE['name']);
109 $attachapi->set('mimetype', $FILE['type']);
110 $attachapi->set('filesize', $FILE['size']);
111 $attachapi->set('description', $bugsys->in['description']);
112 $attachapi->set('userid', $bugsys->userinfo['userid']);
113
114 // insert an attachment
115 if (!$message->items)
116 {
117 $attachapi->insert();
118
119 $obsoletes = $bugsys->input_clean('obsoletes', TYPE_UINT);
120
121 $notif->send_new_attachment_notice($attachapi->values, $obsoletes, $attachapi->insertid);
122
123 // mark obsoletes
124 if (is_array($obsoletes) AND sizeof($obsoletes) > 0)
125 {
126 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
127
128 foreach ($obsoletes AS $attachmentid)
129 {
130 $log = new Logging;
131 $log->set_bugid($bug['bugid']);
132 $log->set_attachmentid($attachmentid);
133 $log->add_data(true, array('obsolete' => 0), array('obsolete'), false, 'attachment');
134 $log->add_data(false, array('obsolete' => 1), array('obsolete'), false, 'attachment');
135 $log->update_history();
136 }
137 }
138
139 // handle comment stuff
140 if (can_perform('canpostcomments', $bug['product']) AND trim($bugsys->in['comment']))
141 {
142 $comment = new CommentAPI($bugsys);
143 $comment->set('bugid', $bugsys->in['bugid']);
144 $comment->set('userid', $bugsys->userinfo['userid']);
145 $comment->set('comment', $bugsys->in['comment']);
146 $comment->set('dateline', $attachapi->values['dateline']);
147 $comment->insert();
148
149 $notif->send_new_comment_notice($comment->values);
150 }
151
152 // update the last post data
153 $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = " . $attachapi->values['dateline'] . ", hiddenlastposttime = " . $attachapi->values['dateline'] . ", lastpostby = " . $bugsys->userinfo['userid'] . ", hiddenlastpostby = " . $bugsys->userinfo['userid'] . " WHERE bugid = $bug[bugid]");
154
155 $notif->finalize();
156
157 $message->redirect(_('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
158 }
159 else
160 {
161 $show['errors'] = true;
162 $_REQUEST['do'] = 'add';
163 $message->error_list_process();
164 }
165 }
166
167 // ###################################################################
168
169 if ($_REQUEST['do'] == 'add')
170 {
171 if (!can_perform('canputattach', $bug['product']))
172 {
173 $message->error_permission();
174 }
175
176 $MAXFILESIZE = $funct->fetch_max_attachment_size();
177
178 $show['addcomment'] = ((can_perform('canpostcomments', $bug['product'])) ? true : false);
179 $show['obsoletes'] = false;
180
181 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
182 $obsoletes = '';
183 while ($obsolete = $db->fetch_array($obsoletes_fetch))
184 {
185 $show['obsoletes'] = true;
186 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\"" . (is_array($bugsys->in['obsoletes']) AND in_array($obsolete['attachmentid'], $bugsys->in['obsoletes']) ? ' checked="checked"' : '') . " /> $obsolete[filename]" . ($obsolete['description'] ? " [$obsolete[description]]" : '') . "</div>\n";
187 }
188
189 eval('$template->flush("' . $template->fetch('newattach') . '");');
190 }
191
192 // ###################################################################
193
194 if ($_POST['do'] == 'update')
195 {
196 if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
197 {
198 $message->error_permission();
199 }
200
201 $attachapi = new AttachmentAPI($bugsys);
202 $attachapi->set('attachmentid', $bugsys->in['attachmentid']);
203 $attachapi->dorelations = array();
204 $attachapi->set_condition();
205
206 if ($bugsys->in['__delete__'] != '')
207 {
208 if (!(can_perform('caneditattach', $bug['product']) AND can_perform('candeletedata', $bug['productid'])))
209 {
210 $message->error_permission();
211 }
212
213 $attachapi->delete();
214
215 $message->redirect(_('The attachment was successfully deleted.'), "showreport.php?bugid=$bug[bugid]");
216 }
217 else
218 {
219 $log = new Logging;
220 $log->set_bugid($bug['bugid']);
221 $log->set_attachmentid($bugsys->in['attachmentid']);
222
223 $attachapi->fetch();
224
225 $log->add_data(true, $attachapi->objdata, array('attachment'), true, 'attachment');
226
227 $attachapi->set('description', $bugsys->in['description']);
228 $attachapi->set('obsolete', $bugsys->in['obsolete']);
229 $attachapi->update();
230
231 $log->add_data(false, $attachapi->values, array('attachment'), true, 'attachment');
232
233 $log->update_history();
234
235 $message->redirect(_('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
236 }
237 }
238
239 // ###################################################################
240
241 if ($_REQUEST['do'] == 'edit')
242 {
243 if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
244 {
245 $message->error_permission();
246 }
247
248 $show['delete'] = (can_perform('caneditattach', $bug['product']) AND can_perform('candeletedata', $bug['productid']));
249
250 eval('$template->flush("' . $template->fetch('editattach') . '");');
251 }
252
253 /*=====================================================================*\
254 || ###################################################################
255 || # $HeadURL$
256 || # $Id$
257 || ###################################################################
258 \*=====================================================================*/
259 ?>