r721: Unwanted and unwonted conversions
[bugdar.git] / attachment.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Bugdar [#]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', $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->items[] = $lang->string('PHP said the file you uploaded was too big.'); break;
61 case 2: $message->items[] = $lang->string('The file exceeds the allowed upload size.'); break;
62 case 3: $message->items[] = $lang->string('The file was only partially uploaded.'); break;
63 case 4: $message->items[] = $lang->string('The file was not uploaded at all.'); break;
64 case 6: $message->items[] = $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->items[] = $lang->string('The file you specified did not upload.');
71 }
72
73 // #*# put some MIME-type validation here
74
75 $filedata = $bugsys->escape(file_get_contents($FILE['tmp_name']), true, true);
76 $time = TIMENOW;
77
78 // insert an attachment
79 if (!$message->items)
80 {
81 $db->query("
82 INSERT INTO " . TABLE_PREFIX . "attachment
83 (bugid, filename, mimetype, filesize,
84 attachment, description, dateline, userid)
85 VALUES
86 ($bug[bugid], '" . $bugsys->escape($FILE['name']) . "',
87 '" . $bugsys->escape($FILE['type']) . "', " . intval($FILE['size']) . ",
88 '$filedata', '" . $bugsys->in['description'] . "', $time,
89 " . $bugsys->userinfo['userid'] . "
90 )"
91 );
92
93 // mark obsoletes
94 $obsoletes = $_POST['obsoletes'];
95 if (count($obsoletes) > 0)
96 {
97 array_walk($obsoletes, 'intval');
98 $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
99
100 foreach ($obsoletes AS $attachmentid)
101 {
102 $log->attachmentid = $attachmentid;
103 $log->log($log->diff('obsolete', 0, 1));
104 }
105 }
106
107 // handle comment stuff
108 if (can_perform('canpostcomments', $bug['productid']) AND trim($bugsys->in['comment']))
109 {
110 $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
111
112 if (!$bugsys->options['allowhtml'])
113 {
114 $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
115 }
116
117 $db->query("
118 INSERT INTO " . TABLE_PREFIX . "comment
119 (bugid, userid, dateline, comment, comment_parsed)
120 VALUES
121 ($bug[bugid], " . $bugsys->userinfo['userid'] . ",
122 $time, '" . $bugsys->in['comment'] . "',
123 '" . nl2br($bugsys->in['comment_parsed']) . "'
124 )"
125 );
126 }
127
128 // update the last post data
129 $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = $time, lastpostby = " . $bugsys->userinfo['userid'] . " WHERE bugid = $bug[bugid]");
130
131 $message->redirect($lang->string('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
132 }
133 else
134 {
135 $show['errors'] = true;
136 $_REQUEST['do'] = 'add';
137 $message->error_list_process();
138 }
139 }
140
141 // ###################################################################
142
143 if ($_REQUEST['do'] == 'add')
144 {
145 if (!can_perform('canputattach', $bug['productid']))
146 {
147 $message->error_permission();
148 }
149
150 $MAXFILESIZE = $funct->fetch_max_attachment_size();
151
152 $show['addcomment'] = ((can_perform('canpostcomments', $bug['productid'])) ? true : false);
153 $show['obsoletes'] = false;
154
155 $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
156 $obsoletes = '';
157 while ($obsolete = $db->fetch_array($obsoletes_fetch))
158 {
159 $show['obsoletes'] = true;
160 $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\"" . (in_array($obsolete['attachmentid'], $bugsys->in['obsoletes']) ? ' checked="checked"' : '') . " /> $obsolete[filename] [$obsolete[description]]</div>\n";
161 }
162
163 eval('$template->flush("' . $template->fetch('newattach') . '");');
164 }
165
166 // ###################################################################
167
168 if ($_POST['do'] == 'update')
169 {
170 if (!(can_perform('caneditattach', $bug['productid']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['productid']))))
171 {
172 $message->error_permission();
173 }
174
175 $db->query("
176 UPDATE " . TABLE_PREFIX . "attachment
177 SET description = '" . $bugsys->in['description'] . "',
178 obsolete = " . intval($bugsys->in['obsolete']) . "
179 WHERE attachmentid = " . intval($bugsys->in['attachmentid'])
180 );
181
182 $hist[1] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
183
184 $diff[0] = array_diff_assoc($attachment, $hist[1]);
185 $diff[1] = array_diff_assoc($hist[1], $attachment);
186
187 $log->attachmentid = $attachment['attachmentid'];
188 $log->log($log->diff('description', $diff[0]['description'], $diff[1]['description']));
189 $log->log($log->diff('obsolete', $diff[0]['obsolete'], $diff[1]['obsolete']));
190
191 $message->redirect($lang->string('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
192 }
193
194 // ###################################################################
195
196 if ($_REQUEST['do'] == 'edit')
197 {
198 if (!(can_perform('caneditattach', $bug['productid']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['productid']))))
199 {
200 $message->error_permission();
201 }
202
203 $show['delete'] = ((can_perform('caneditattach', $bug['productid'])) ? true : false);
204
205 eval('$template->flush("' . $template->fetch('editattach') . '");');
206 }
207
208 /*=====================================================================*\
209 || ###################################################################
210 || # $HeadURL$
211 || # $Id$
212 || ###################################################################
213 \*=====================================================================*/
214 ?>