r530: Only check the product permissions in can_perform() if they exist
[bugdar.git] / includes / functions.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 // ################## Start fetch_user_display_name ##################
14 // preps a dispaly name if one isn't set
15 // should be able to be removed by the final version as registration should set this
16 function fetch_user_display_name(&$userinfo)
17 {
18 global $bugsys;
19
20 if (!$userinfo['displayname'])
21 {
22 $userinfo['displayname'] = ucwords(trim(str_replace(array('@', '.com', '.net', '.edu', '.org', '.info', '.biz'), ' ', $userinfo['email'])));
23 }
24
25 if (!$userinfo['userid'])
26 {
27 $userinfo['displayname'] = $bugsys->lang->string('Guest');
28 }
29 }
30
31 // ################## Start construct_option_select ##################
32 // creates a <select> menu from an array
33 // key vars are used when you need to get data out of the $label array
34 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
35 {
36 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
37 if ($includenil !== false)
38 {
39 $opts[] = '<option value="' . $includenil . '"' . ((!$selected) ? ' selected="selected"' : '') . '>' . $lang->string('Not Selected') . '</option>';
40 }
41 foreach ($array AS $value => $label)
42 {
43 $opts[] = '<option value="' . (($valuekey) ? $label["$valuekey"] : $value) . '"' . (($selected == (($valuekey) ? $label["$valuekey"] : $value)) ? ' selected="selected"' : '') . '>' . (($labelkey) ? $label["$labelkey"] : $label) . '</option>';
44 }
45 return '<select name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
46 }
47
48 // ################### Start construct_user_display ##################
49 // $userinfo needs userid, email, displayname, and showemail
50 function construct_user_display($userinfo, $html = true)
51 {
52 global $bugsys;
53 fetch_user_display_name($userinfo);
54
55 if ($html)
56 {
57 eval('$username = "' . $bugsys->template->fetch('username_display') . '";');
58 }
59 else
60 {
61 if ($userinfo['showemail'])
62 {
63 $username = sprintf($bugsys->lang->string('%1$s &lt;%2$s&gt;'), $userinfo['displayname'], $userinfo['email']);
64 }
65 else
66 {
67 $username = $userinfo['displayname'];
68 }
69 }
70
71 return $username;
72 }
73
74 // ######################## Start can_perform ########################
75 // short-hand for bitwise &
76 function can_perform($bitmask, $productid = 0)
77 {
78 global $bugsys, $_PERMISSION;
79
80 $userinfo = $bugsys->userinfo;
81
82 if (!isset($_PERMISSION["$bitmask"]))
83 {
84 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
85 }
86
87 if ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
88 {
89 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
90
91 if (!in_array($bitmask, $inspecific))
92 {
93 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $_PREMISSION["$bitmask"]);
94 }
95 }
96
97 return ($userinfo['permissions'] & $_PERMISSION["$bitmask"]);
98 }
99
100 // ################# Start construct_datastore_select ################
101 // loops through the specified datastore to create <select>s
102 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
103 {
104 global $bugsys;
105
106 if ($adminmode)
107 {
108 global $admin;
109 }
110
111 $select = '';
112
113 if ($includeblank)
114 {
115 if ($adminmode)
116 {
117 $admin->list_item('', '', ((!$selectedvalue) ? true : false));
118 }
119 else
120 {
121 $label = '';
122 $value = '';
123 $selected = ((!$selectedvalue) ? true : false);
124 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
125 }
126 }
127
128 foreach ($bugsys->datastore["$datastore"] AS $item)
129 {
130 $label = $item["$labelname"];
131 $value = $item["$valuename"];
132 $selected = (($value == $selectedvalue) ? true : false);
133
134 if ($adminmode)
135 {
136 $admin->list_item($label, $value, $selected);
137 }
138 else
139 {
140 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
141 }
142 }
143
144 if (!$adminmode)
145 {
146 return $select;
147 }
148 }
149
150 // ################## Start construct_custom_fields ##################
151 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false)
152 {
153 global $bugsys;
154 static $fields;
155
156 if (!is_array($fields))
157 {
158 $fields = array();
159 $fields_fetch = $bugsys->db->query("
160 SELECT bugfield.*, permission.mask
161 FROM " . TABLE_PREFIX . "bugfield AS bugfield
162 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
163 ON (bugfield.fieldid = permission.fieldid)
164 WHERE (permission.mask = 2 OR permission.mask = 1)
165 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
166 );
167 while ($field = $bugsys->db->fetch_array($fields_fetch))
168 {
169 $fields["$field[fieldid]"] = $field;
170 }
171 }
172
173 $fieldvalues = $bugsys->db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = " . intval($bug['bugid']));
174
175 $fieldbits = '';
176
177 foreach ($fields AS $field)
178 {
179 if ($nodefault)
180 {
181 $field['defaultvalue'] = '';
182 }
183
184 if (!is_null($bug["field$field[fieldid]"]))
185 {
186 $bugsys->debug("not null: $field[fieldid]");
187 $value = $bug["field$field[fieldid]"];
188 }
189 else
190 {
191 $value = $field['defaultvalue'];
192 }
193
194 if ($ignore21mask)
195 {
196 $field['mask'] = 2;
197 }
198
199 if ($field['mask'] == 2)
200 {
201 switch ($field['type'])
202 {
203 case 'input_text':
204 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
205 break;
206
207 case 'input_checkbox':
208 $selected = (($value) ? ' checked="checked"' : '');
209 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
210 break;
211
212 case 'select_single':
213 $selects = unserialize($field['selects']);
214 $value = trim($value);
215
216 $options = '';
217
218 $id = -1;
219 $select = '';
220 if (!$field['usedefault'] AND !trim($value))
221 {
222 $selected = ' selected="selected"';
223 }
224 else
225 {
226 $selected = '';
227 }
228 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
229
230 foreach ($selects AS $id => $select)
231 {
232 $selected = '';
233 $select = stripslashes(trim($select));
234 if ($select == $value)
235 {
236 $selected = ' selected="selected"';
237 }
238 else if ($field['usedefault'] AND $id == 0)
239 {
240 $selected = ' selected="selected"';
241 }
242 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
243 }
244 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
245 break;
246 }
247 }
248 else
249 {
250 $bugsys->debug('mask 1 processing');
251 if (is_null($fieldvalues["field$field[fieldid]"]))
252 {
253 $bugsys->debug("is null: $field[fieldid]");
254 if ($field['type'] == 'select_single')
255 {
256 if ($field['usedefault'])
257 {
258 $temp = unserialize($field['selects']);
259 $value = trim($temp[0]);
260 }
261 else
262 {
263 $value = $fieldvalues["field$field[fieldid]"];
264 }
265 }
266 else
267 {
268 $value = $field['defaultvalue'];
269 }
270 }
271 else
272 {
273 $value = $fieldvalues["field$field[fieldid]"];
274 }
275
276 if ($field['type'] == 'input_checkbox')
277 {
278 $value = (($value) ? 'True' : 'False');
279 }
280 $field['value'] = $value;
281 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
282 }
283 $fieldbits[] = $tempfield;
284 }
285
286 return $fieldbits;
287 }
288
289 // ################### Start process_custom_fields ###################
290 function process_custom_fields($bugid, $inputdata = array())
291 {
292 global $bugsys;
293
294 if (!$inputdata)
295 {
296 $inputdata =& $bugsys->in;
297 }
298
299 $fields = $bugsys->db->query("
300 SELECT bugfield.*
301 FROM " . TABLE_PREFIX . "bugfield AS bugfield
302 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
303 ON (bugfield.fieldid = permission.fieldid)
304 WHERE permission.mask = 2
305 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
306 );
307 while ($field = $bugsys->db->fetch_array($fields))
308 {
309 if ($field['type'] == 'input_checkbox')
310 {
311 $fieldbuild[] = 'field' . $field['fieldid'];
312 if (isset($inputdata["field$field[fieldid]"]))
313 {
314 $fieldvalue[] = 1;
315 }
316 else
317 {
318 $fieldvalue[] = 0;
319 }
320 continue;
321 }
322
323 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
324 {
325 $errorlist[] = sprintf($bugsys->lang->string('The field titled "%1$s" is a required field.'), $field['name']);
326 continue;
327 }
328
329 if (isset($inputdata["field$field[fieldid]"]))
330 {
331 $fieldbuild[] = 'field' . $field['fieldid'];
332
333 if ($field['type'] == 'input_text')
334 {
335 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
336 }
337 else
338 {
339 if ($inputdata["field$field[fieldid]"] == -1)
340 {
341 $fieldvalue[] = "''";
342 continue;
343 }
344
345 $temp = unserialize($field['selects']);
346 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
347 }
348 }
349 }
350
351 if ($errorlist)
352 {
353 return $errorlist;
354 }
355
356 if (count($fieldbuild) < 1)
357 {
358 return;
359 }
360
361 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
362 }
363
364 // ####################### Start fetch_on_bits #######################
365 function fetch_on_bits($mask)
366 {
367 global $bugsys, $_PERMISSION;
368
369 $onbits = array();
370
371 $usergroupid = $bugsys->userinfo['usergroupid'];
372
373 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $_PERMISSION["$mask"])
374 {
375 foreach ($bugsys->datastore['product'] AS $id => $product)
376 {
377 if (!$product['componentmother'])
378 {
379 $onbits["$id"] = $id;
380 }
381 }
382 }
383
384 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
385 {
386 if ($bit & $_PERMISSION["$mask"])
387 {
388 $onbits["$productid"] = $productid;
389 }
390 else
391 {
392 if ($onbits["$productid"])
393 {
394 unset($onbits["$productid"]);
395 }
396 }
397 }
398
399 if (count($onbits) < 1)
400 {
401 $onbits = array(0);
402 }
403
404 return implode(',', $onbits);
405 }
406
407 /*=====================================================================*\
408 || ###################################################################
409 || # $HeadURL$
410 || # $Id$
411 || ###################################################################
412 \*=====================================================================*/
413 ?>