r941: Making lots of changes to cope with the renaming of the tree PCV fields in...
[bugdar.git] / attachment.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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($lang->getlex('error_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($lang->getlex('error_invalid_id'));
52 }
53
54 require_once('./includes/class_logging.php');
55
56 $notif = new NotificationCenter;
57 $notif->set_bug_data($bug);
58
59 // ###################################################################
60
61 if ($_POST['do'] == 'insert')
62 {
63 $attachapi = new AttachmentAPI($bugsys);
64 $attachapi->set('bugid', $bugsys->in['bugid']);
65
66 if (!can_perform('canputattach', $bug['product']))
67 {
68 $message->error_permission();
69 }
70
71 // max packet size
72 $var = $db->query_first("SHOW VARIABLES LIKE 'max_allowed_packet'");
73 $bugsys->debug("max_allowed_packet = $var[Value]");
74
75 // create alias
76 $FILE =& $_FILES['attachment'];
77
78 // PHP errors
79 switch ($FILE['error'])
80 {
81 case 0: break;
82 case 1: $message->add_error($lang->string('PHP said the file you uploaded was too big.')); break;
83 case 2: $message->add_error($lang->string('The file exceeds the allowed upload size.')); break;
84 case 3: $message->add_error($lang->string('The file was only partially uploaded.')); break;
85 case 4: $message->add_error($lang->string('The file was not uploaded at all.')); break;
86 case 6: $message->add_error($lang->string('PHP could not find the /tmp directory.')); break;
87 }
88
89 // did it upload?
90 if (!is_uploaded_file($FILE['tmp_name']))
91 {
92 $message->add_error($lang->string('The file you specified did not upload.'));
93 }
94
95 // #*# put some MIME-type validation here
96
97 if (filesize($FILE['tmp_name']) > $var['Value'])
98 {
99 $message->add_error($lang->string('The file you specified exceeds MySQL\'s maximum allowed packet.'));
100 }
101
102 $attachapi->set('attachment', file_get_contents($FILE['tmp_name']));
103 $attachapi->set('filename', $FILE['name']);
104 $attachapi->set('mimetype', $FILE['type']);
105 $attachapi->set('filesize', $FILE['size']);
106 $attachapi->set('description', $bugsys->in['description']);
107 $attachapi->set('userid', $bugsys->userinfo['userid']);
108
109 // insert an attachment
110 if (!$message->items)
111 {
112 $attachapi->insert();
113
114 $obsoletes = $bugsys->input_clean('obsoletes', TYPE_UINT);
115
116 $notif->send_new_attachment_notice($attachapi->values, $obsoletes, $attachapi->insertid);
117
118 // mark obsoletes
119 if (is_array($obsoletes) AND sizeof($obsoletes) > 0)
120 {
121 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
122
123 foreach ($obsoletes AS $attachmentid)
124 {
125 $log = new Logging;
126 $log->set_bugid($bug['bugid']);
127 $log->set_attachmentid($attachmentid);
128 $log->add_data(true, array('obsolete' => 0), array('obsolete'), false, 'attachment');
129 $log->add_data(false, array('obsolete' => 1), array('obsolete'), false, 'attachment');
130 $log->update_history();
131 }
132 }
133
134 // handle comment stuff
135 if (can_perform('canpostcomments', $bug['product']) AND trim($bugsys->in['comment']))
136 {
137 $comment = new CommentAPI($bugsys);
138 $comment->set('bugid', $bugsys->in['bugid']);
139 $comment->set('userid', $bugsys->userinfo['userid']);
140 $comment->set('comment', $bugsys->in['comment']);
141 $comment->set('dateline', $attachapi->values['dateline']);
142 $comment->insert();
143
144 $notif->send_new_comment_notice($comment->values);
145 }
146
147 // update the last post data
148 $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]");
149
150 $notif->finalize();
151
152 $message->redirect($lang->string('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
153 }
154 else
155 {
156 $show['errors'] = true;
157 $_REQUEST['do'] = 'add';
158 $message->error_list_process();
159 }
160 }
161
162 // ###################################################################
163
164 if ($_REQUEST['do'] == 'add')
165 {
166 if (!can_perform('canputattach', $bug['product']))
167 {
168 $message->error_permission();
169 }
170
171 $MAXFILESIZE = $funct->fetch_max_attachment_size();
172
173 $show['addcomment'] = ((can_perform('canpostcomments', $bug['product'])) ? true : false);
174 $show['obsoletes'] = false;
175
176 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
177 $obsoletes = '';
178 while ($obsolete = $db->fetch_array($obsoletes_fetch))
179 {
180 $show['obsoletes'] = true;
181 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\"" . (in_array($obsolete['attachmentid'], (array)$bugsys->in['obsoletes']) ? ' checked="checked"' : '') . " /> $obsolete[filename]" . ($obsolete['description'] ? " [$obsolete[description]]" : '') . "</div>\n";
182 }
183
184 eval('$template->flush("' . $template->fetch('newattach') . '");');
185 }
186
187 // ###################################################################
188
189 if ($_POST['do'] == 'update')
190 {
191 if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
192 {
193 $message->error_permission();
194 }
195
196 $log = new Logging;
197 $log->set_bugid($bug['bugid']);
198 $log->set_attachmentid($bugsys->in['attachmentid']);
199
200 $attachapi = new AttachmentAPI($bugsys);
201 $attachapi->set('attachmentid', $bugsys->in['attachmentid']);
202 $attachapi->set_condition();
203 $attachapi->dorelations = array();
204 $attachapi->fetch();
205
206 $log->add_data(true, $attachapi->objdata, array('attachment'), true, 'attachment');
207
208 $attachapi->set('description', $bugsys->in['description']);
209 $attachapi->set('obsolete', $bugsys->in['obsolete']);
210 $attachapi->update();
211
212 $log->add_data(false, $attachapi->values, array('attachment'), true, 'attachment');
213
214 $log->update_history();
215
216 $message->redirect($lang->string('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
217 }
218
219 // ###################################################################
220
221 if ($_REQUEST['do'] == 'edit')
222 {
223 if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
224 {
225 $message->error_permission();
226 }
227
228 $show['delete'] = ((can_perform('caneditattach', $bug['product'])) ? true : false);
229
230 eval('$template->flush("' . $template->fetch('editattach') . '");');
231 }
232
233 /*=====================================================================*\
234 || ###################################################################
235 || # $HeadURL$
236 || # $Id$
237 || ###################################################################
238 \*=====================================================================*/
239 ?>