r527: Base new permission checks
[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 define('SVN', '$Id$');
19
20 $focus['showreport'] = 'focus';
21
22 require_once('./global.php');
23
24 if (isset($bugsys->in['attachmentid']))
25 {
26 $attachment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . intval($bugsys->in['attachmentid']));
27 if (!$attachment)
28 {
29 $message->error($lang->getlex('error_invalid_id'));
30 }
31 }
32
33 $bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : intval($bugsys->in['bugid'])));
34 if (!$bug)
35 {
36 $message->error($lang->getlex('error_invalid_id'));
37 }
38
39 // setup logging
40 require_once('./includes/class_history.php');
41 $log = new History();
42 $log->bugid = $bug['bugid'];
43
44 // ###################################################################
45
46 if ($_POST['do'] == 'insert')
47 {
48 if (!can_perform('canputattach', null, $bug['productid']))
49 {
50 $message->error_permission();
51 }
52
53 // create alias
54 $FILE =& $_FILES['attachment'];
55
56 // PHP errors
57 switch ($FILE['error'])
58 {
59 case 0: break;
60 case 1: $message->error($lang->string('PHP said the file you uploaded was too big.')); break;
61 case 2: $message->error($lang->string('The file exceeds the allowed upload size.')); break;
62 case 3: $message->error($lang->string('The file was only partially uploaded.')); break;
63 case 4: $message->error($lang->string('The file was not uploaded at all.')); break;
64 case 6: $message->error($lang->string('PHP could not find the /tmp directory.')); break;
65 }
66
67 // did it upload?
68 if (!is_uploaded_file($FILE['tmp_name']))
69 {
70 $message->error($lang->string('The file you specified did not upload.'));
71 }
72
73 // #*# put some MIME-type validation here
74
75 if (!$bugsys->in['description'])
76 {
77 $message->error($lang->string('You need to specify a file description.'));
78 }
79
80 $filedata = $bugsys->escape(file_get_contents($FILE['tmp_name']), true, true);
81 $time = TIMENOW;
82
83 // insert an attachment
84 $db->query("
85 INSERT INTO attachment
86 (bugid, filename, mimetype, filesize,
87 attachment, description, dateline, userid)
88 VALUES
89 ($bug[bugid], '" . $bugsys->escape($FILE['name']) . "',
90 '" . $bugsys->escape($FILE['type']) . "', " . intval($FILE['size']) . ",
91 '$filedata', '" . $bugsys->in['description'] . "', $time,
92 " . $bugsys->userinfo['userid'] . "
93 )"
94 );
95
96 // mark obsoletes
97 $obsoletes = $_POST['obsoletes'];
98 if (count($obsoletes) > 0)
99 {
100 array_walk($obsoletes, 'intval');
101 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
102
103 foreach ($obsoletes AS $attachmentid)
104 {
105 $log->attachmentid = $attachmentid;
106 $log->log($log->diff('obsolete', 0, 1));
107 }
108 }
109
110 // handle comment stuff
111 if (can_perform('canpostcomments', null, $bug['productid']) AND trim($bugsys->in['comment']))
112 {
113 $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
114
115 if (!$bugsys->options['allowhtml'])
116 {
117 $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
118 }
119
120 $db->query("
121 INSERT INTO " . TABLE_PREFIX . "comment
122 (bugid, userid, dateline, comment, comment_parsed)
123 VALUES
124 ($bug[bugid], " . $bugsys->userinfo['userid'] . ",
125 $time, '" . $bugsys->in['comment'] . "',
126 '" . nl2br($bugsys->in['comment_parsed']) . "'
127 )"
128 );
129 }
130
131 // update the last post data
132 $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = $time, lastpostby = " . $bugsys->userinfo['userid'] . " WHERE bugid = $bug[bugid]");
133
134 $message->redirect($lang->string('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
135 }
136
137 // ###################################################################
138
139 if ($_REQUEST['do'] == 'add')
140 {
141 if (!can_perform('canputattach', null, $bug['productid']))
142 {
143 $message->error_permission();
144 }
145
146 $MAXFILESIZE = $funct->fetch_max_attachment_size();
147
148 $show['addcomment'] = ((can_perform('canpostcomments', null, $bug['productid'])) ? true : false);
149 $show['obsoletes'] = false;
150
151 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
152 $obsoletes = '';
153 while ($obsolete = $db->fetch_array($obsoletes_fetch))
154 {
155 $show['obsoletes'] = true;
156 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\" /> $obsolete[filename] [$obsolete[description]]</div>\n";
157 }
158
159 eval('$template->flush("' . $template->fetch('newattach') . '");');
160 }
161
162 // ###################################################################
163
164 if ($_POST['do'] == 'update')
165 {
166 if (!(can_perform('caneditattach', null, $bug['productid']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', null, $bug['productid']))))
167 {
168 $message->error_permission();
169 }
170
171 $db->query("
172 UPDATE " . TABLE_PREFIX . "attachment
173 SET description = '" . $bugsys->in['description'] . "',
174 obsolete = " . intval($bugsys->in['obsolete']) . "
175 WHERE attachmentid = " . intval($bugsys->in['attachmentid'])
176 );
177
178 $hist[1] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
179
180 $diff[0] = array_diff_assoc($attachment, $hist[1]);
181 $diff[1] = array_diff_assoc($hist[1], $attachment);
182
183 $log->attachmentid = $attachment['attachmentid'];
184 $log->log($log->diff('description', $diff[0]['description'], $diff[1]['description']));
185 $log->log($log->diff('obsolete', $diff[0]['obsolete'], $diff[1]['obsolete']));
186
187 $message->redirect($lang->string('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
188 }
189
190 // ###################################################################
191
192 if ($_REQUEST['do'] == 'edit')
193 {
194 if (!(can_perform('caneditattach', null, $bug['productid']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', null, $bug['productid']))))
195 {
196 $message->error_permission();
197 }
198
199 $show['delete'] = ((can_perform('caneditattach', null, $bug['productid'])) ? true : false);
200
201 eval('$template->flush("' . $template->fetch('editattach') . '");');
202 }
203
204 /*=====================================================================*\
205 || ###################################################################
206 || # $HeadURL$
207 || # $Id$
208 || ###################################################################
209 \*=====================================================================*/
210 ?>