r136: Completed attachment uploading stuff:
[bugdar.git] / attachment.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $fetchtemplates = array(
14 'newattach',
15 'editattach'
16 );
17
18 require_once('./global.php');
19
20 if (isset($bugsys->in['attachmentid']))
21 {
22 $attachment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . intval($bugsys->in['attachmentid']));
23 if (!$attachment)
24 {
25 echo 'alert: bad attachment';
26 exit;
27 }
28 }
29
30 $bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : intval($bugsys->in['bugid'])));
31 if (!$bug)
32 {
33 echo 'alert: bad bug';
34 exit;
35 }
36
37 // ###################################################################
38
39 if ($_REQUEST['do'] == 'kill')
40 {
41 if (!can_perform('caneditattach'))
42 {
43 echo 'alert: no permission';
44 exit;
45 }
46
47 $db->query("DELETE FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
48
49 echo "<a href=\"showreport.php?bugid=$bug[bugid]\">attachment removed</a>";
50 }
51
52 // ###################################################################
53
54 if ($_REQUEST['do'] == 'delete')
55 {
56 if (!can_perform('caneditattach'))
57 {
58 echo 'alert: no permission';
59 exit;
60 }
61
62 echo "are you sure you want to delete this attachment? <a href=\"attachment.php?do=kill&amp;attachmentid=$attachment[attachmentid]\">yes</a>";
63 }
64
65 // ###################################################################
66
67 if ($_POST['do'] == 'insert')
68 {
69 if (!can_perform('canputattach'))
70 {
71 echo 'alert: no permission';
72 exit;
73 }
74
75 // create alias
76 $FILE =& $_FILES['attachment'];
77
78 // PHP errors
79 switch ($FILE['error'])
80 {
81 case 0: break;
82 case 1: echo 'PHP said the file you uploaded was too big.'; exit; break;
83 case 2: echo 'The file exceeds the allowed upload size.'; exit; break;
84 case 3: echo 'The file was only partially uploaded.'; exit; break;
85 case 4: echo 'The file was not uploaded at all.'; exit; break;
86 case 6: echo 'PHP could not find the /tmp directory.'; exit; break;
87 }
88
89 // did it upload?
90 if (!is_uploaded_file($FILE['tmp_name']))
91 {
92 echo 'The file you specified did not upload.';
93 exit;
94 }
95
96 // #*# put some MIME-type validation here
97
98 if (!$bugsys->in['description'])
99 {
100 echo 'you need a file description!';
101 exit;
102 }
103
104 $filedata = $bugsys->escape(file_get_contents($FILE['tmp_name']), true, true);
105 $time = time();
106
107 // insert an attachment
108 $db->query("
109 INSERT INTO attachment
110 (bugid, filename, mimetype, filesize,
111 attachment, description, dateline, userid)
112 VALUES
113 ($bug[bugid], '" . $bugsys->escape($FILE['name']) . "',
114 '" . $bugsys->escape($FILE['type']) . "', " . intval($FILE['size']) . ",
115 '$filedata', '" . $bugsys->in['description'] . "', $time,
116 " . $bugsys->userinfo['userid'] . "
117 )"
118 );
119
120 // mark obsoletes
121 $obsoletes = $_POST['obsoletes'];
122 array_walk($obsoletes, 'intval');
123 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
124
125 // handle comment stuff
126 if (can_perform('canpostcomments') AND trim($bugsys->in['comment']))
127 {
128 $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
129
130 if (!$bugsys->options['allowhtml'])
131 {
132 $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
133 }
134
135 $db->query("
136 INSERT INTO " . TABLE_PREFIX . "comment
137 (bugid, userid, dateline, comment, comment_parsed)
138 VALUES
139 ($bug[bugid], " . $bugsys->userinfo['userid'] . ",
140 $time, '" . $bugsys->in['comment'] . "',
141 '" . nl2br($bugsys->in['comment_parsed']) . "'
142 )"
143 );
144 }
145
146 // update the last post data
147 $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = $time, lastpostby = " . $bugsys->userinfo['userid'] . " WHERE bugid = $bug[bugid]");
148
149 echo "<a href=\"showreport.php?bugid=$bug[bugid]\">attachment added</a>";
150
151 }
152
153 // ###################################################################
154
155 if ($_REQUEST['do'] == 'add')
156 {
157 if (!can_perform('canputattach'))
158 {
159 echo 'alert: no permission';
160 exit;
161 }
162
163 $MAXFILESIZE = $funct->fetch_max_attachment_size();
164
165 $show['addcomment'] = ((can_perform('canpostcomments')) ? true : false);
166 $show['obsoletes'] = false;
167
168 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
169 $obsoletes = '';
170 while ($obsolete = $db->fetch_array($obsoletes_fetch))
171 {
172 $show['obsoletes'] = true;
173 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\" /> $obsolete[filename] [$obsolete[description]]</div>\n";
174 }
175
176 eval('$template->flush("' . $template->fetch('newattach') . '");');
177 }
178
179 // ###################################################################
180
181 if ($_POST['do'] == 'update')
182 {
183 if (!(can_perform('caneditattach') OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach'))))
184 {
185 echo 'alert: no permssion';
186 exit;
187 }
188
189 $db->query("
190 UPDATE " . TABLE_PREFIX . "attachment
191 SET description = '" . $bugsys->in['description'] . "',
192 obsolete = " . intval($bugsys->in['obsolete']) . "
193 WHERE attachmentid = " . intval($bugsys->in['attachmentid'])
194 );
195
196 echo "<a href=\"showreport.php?bugid=$bug[bugid]\">attachment updated</a>";
197 }
198
199 // ###################################################################
200
201 if ($_REQUEST['do'] == 'edit')
202 {
203 if (!(can_perform('caneditattach') OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach'))))
204 {
205 echo 'alert: no permssion';
206 exit;
207 }
208
209 $show['delete'] = ((can_perform('caneditattach')) ? true : false);
210
211 eval('$template->flush("' . $template->fetch('editattach') . '");');
212 }
213
214 /*=====================================================================*\
215 || ###################################################################
216 || # $HeadURL$
217 || # $Id$
218 || ###################################################################
219 \*=====================================================================*/
220 ?>