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