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