Remove includes/class_api_error.php and all of the places we require() it
[bugdar.git] / attachment.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 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 2 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_notification.php');
33 require_once('./includes/api_attachment.php');
34 require_once('./includes/api_comment.php');
35
36 APIError(array(new API_Error_Handler($message), 'user_cumulative'));
37
38 if (isset($input->in['attachmentid']))
39 {
40 $attachment = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . $input->inputClean('attachmentid', TYPE_UINT));
41 if (!$attachment)
42 {
43 $message->error(L_INVALID_ID);
44 }
45 }
46
47 $bug = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : $input->inputClean('bugid', TYPE_UINT)));
48 if (!$bug)
49 {
50 $message->error(L_INVALID_ID);
51 }
52
53 if (!check_bug_permissions($bug))
54 {
55 $message->errorPermission();
56 }
57
58 require_once('./includes/class_logging.php');
59
60 $notif = new NotificationCenter;
61 $notif->set_bug_data($bug);
62
63 // ###################################################################
64
65 if ($_POST['do'] == 'insert')
66 {
67 $attachapi = new AttachmentAPI();
68 $attachapi->set('bugid', $input->in['bugid']);
69
70 if (!can_perform('canputattach', $bug['product']))
71 {
72 $message->errorPermission();
73 }
74
75 // max packet size
76 $var = $db->queryFirst("SHOW VARIABLES LIKE 'max_allowed_packet'");
77 BSApp::debug("max_allowed_packet = $var[Value]");
78
79 // create alias
80 $FILE =& $_FILES['attachment'];
81
82 // PHP errors
83 switch ($FILE['error'])
84 {
85 case 0: break;
86 case 1: $message->addError(T('PHP said the file you uploaded was too big.')); break;
87 case 2: $message->addError(T('The file exceeds the allowed upload size.')); break;
88 case 3: $message->addError(T('The file was only partially uploaded.')); break;
89 case 4: $message->addError(T('The file was not uploaded at all.')); break;
90 case 6: $message->addError(T('PHP could not find the /tmp directory.')); break;
91 }
92
93 // did it upload?
94 if (!is_uploaded_file($FILE['tmp_name']))
95 {
96 $message->addError(T('The file you specified did not upload.'));
97 }
98
99 // TODO - put some MIME-type validation here
100
101 if (filesize($FILE['tmp_name']) > $var['Value'])
102 {
103 $message->addError(T('The file you specified exceeds MySQL\'s maximum allowed packet.'));
104 }
105
106 $attachapi->set('attachment', file_get_contents($FILE['tmp_name']));
107 $attachapi->set('filename', $FILE['name']);
108 $attachapi->set('mimetype', $FILE['type']);
109 $attachapi->set('filesize', $FILE['size']);
110 $attachapi->set('description', $input->in['description']);
111 $attachapi->set('userid', bugdar::$userinfo['userid']);
112
113 // insert an attachment
114 if (!$message->hasErrors())
115 {
116 $attachapi->insert();
117
118 $obsoletes = $input->inputClean('obsoletes', TYPE_UINT);
119
120 $notif->send_new_attachment_notice($attachapi->values, $obsoletes, $attachapi->insertid);
121
122 // mark obsoletes
123 if (is_array($obsoletes) AND sizeof($obsoletes) > 0)
124 {
125 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
126
127 foreach ($obsoletes AS $attachmentid)
128 {
129 $log = new Logging;
130 $log->set_bugid($bug['bugid']);
131 $log->set_attachmentid($attachmentid);
132 $log->add_data(true, array('obsolete' => 0), array('obsolete'), false, 'attachment');
133 $log->add_data(false, array('obsolete' => 1), array('obsolete'), false, 'attachment');
134 $log->update_history();
135 }
136 }
137
138 // handle comment stuff
139 if (can_perform('canpostcomments', $bug['product']) AND trim($input->in['comment']))
140 {
141 $comment = new CommentAPI();
142 $comment->set('bugid', $input->in['bugid']);
143 $comment->set('userid', bugdar::$userinfo['userid']);
144 $comment->set('comment', $input->in['comment']);
145 $comment->set('dateline', $attachapi->values['dateline']);
146 $comment->insert();
147
148 $notif->send_new_comment_notice($comment->values);
149 }
150
151 // update the last post data
152 $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = " . $attachapi->values['dateline'] . ", hiddenlastposttime = " . $attachapi->values['dateline'] . ", lastpostby = " . bugdar::$userinfo['userid'] . ", hiddenlastpostby = " . bugdar::$userinfo['userid'] . " WHERE bugid = $bug[bugid]");
153
154 $notif->finalize();
155
156 $message->redirect(T('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
157 }
158 else
159 {
160 $show['errors'] = true;
161 $_REQUEST['do'] = 'add';
162 }
163 }
164
165 // ###################################################################
166
167 if ($_REQUEST['do'] == 'add')
168 {
169 if (!can_perform('canputattach', $bug['product']))
170 {
171 $message->errorPermission();
172 }
173
174 $MAXFILESIZE = BSFunctions::fetch_max_php_file_size();
175
176 $show['addcomment'] = ((can_perform('canpostcomments', $bug['product'])) ? true : false);
177 $show['obsoletes'] = false;
178
179 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
180 $obsoletes = '';
181 foreach ($obsoletes_fetch as $obsolete)
182 {
183 $show['obsoletes'] = true;
184 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\"" . (is_array($input->in['obsoletes']) AND in_array($obsolete['attachmentid'], $input->in['obsoletes']) ? ' checked="checked"' : '') . " /> $obsolete[filename]" . ($obsolete['description'] ? " [$obsolete[description]]" : '') . "</div>\n";
185 }
186
187 eval('$template->flush("' . $template->fetch('newattach') . '");');
188 }
189
190 // ###################################################################
191
192 if ($_POST['do'] == 'update')
193 {
194 if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == bugdar::$userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
195 {
196 $message->errorPermission();
197 }
198
199 $attachapi = new AttachmentAPI();
200 $attachapi->set('attachmentid', $input->in['attachmentid']);
201 $attachapi->dorelations = array();
202 $attachapi->set_condition();
203
204 if ($input->in['__delete__'] != '')
205 {
206 if (!(can_perform('caneditattach', $bug['product']) AND can_perform('candeletedata', $bug['productid'])))
207 {
208 $message->errorPermission();
209 }
210
211 $attachapi->delete();
212
213 $message->redirect(T('The attachment was successfully deleted.'), "showreport.php?bugid=$bug[bugid]");
214 }
215 else
216 {
217 $log = new Logging;
218 $log->set_bugid($bug['bugid']);
219 $log->set_attachmentid($input->in['attachmentid']);
220
221 $attachapi->fetch();
222
223 $log->add_data(true, $attachapi->record, array('attachment'), true, 'attachment');
224
225 $attachapi->set('description', $input->in['description']);
226 $attachapi->set('obsolete', $input->in['obsolete']);
227 $attachapi->update();
228
229 $log->add_data(false, $attachapi->values, array('attachment'), true, 'attachment');
230
231 $log->update_history();
232
233 $message->redirect(T('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
234 }
235 }
236
237 // ###################################################################
238
239 if ($_REQUEST['do'] == 'edit')
240 {
241 if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == bugdar::$userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
242 {
243 $message->errorPermission();
244 }
245
246 $show['delete'] = (can_perform('caneditattach', $bug['product']) AND can_perform('candeletedata', $bug['productid']));
247
248 eval('$template->flush("' . $template->fetch('editattach') . '");');
249 }
250
251 /*=====================================================================*\
252 || ###################################################################
253 || # $HeadURL$
254 || # $Id$
255 || ###################################################################
256 \*=====================================================================*/
257 ?>