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