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