r1109: Trying to get notification permissions check to work
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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 // ###################################################################
23 /**
24 * Constructs HTML code <select>s from an array. You use they keys when
25 * you need to access a multi-dimensional array of data.
26 *
27 * @access public
28 *
29 * @param string HTML name of the select
30 * @param array Array of <option>s
31 * @param integer ID of the selected item, 0 for none
32 * @param string Name of the index where values are stored in the $array
33 * @param string Name of the iddex where the labels are stored in $array
34 * @param bool Value of the blank option, FALSE turns it off
35 * @param bool Construct a multiple-selection <select> menu and append "[]" to the end of the name
36 *
37 * @return string Constructed HTML output
38 */
39 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false, $multiple = false)
40 {
41 global $bugsys;
42
43 if ($multiple)
44 {
45 $selected = explode(',', $selected);
46 }
47
48 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
49 if ($includenil !== false)
50 {
51 $opts[] = '<option value="' . $includenil . '"' . ((!$selected OR (is_array($selected) AND in_array($includenil, $selected))) ? ' selected="selected"' : '') . '> ---------</option>';
52 }
53 foreach ($array AS $value => $label)
54 {
55 $newval = ($valuekey ? $label["$valuekey"] : $value);
56 $newlab = ($labelkey ? $label["$labelkey"] : $label);
57 $opts[] = '<option value="' . $newval . '"' . (($selected == $newval OR (is_array($selected) AND in_array($newval, $selected))) ? ' selected="selected"' : '') . '>' . $newlab . '</option>';
58 }
59 return '<select class="input" name="' . $name . ($multiple ? '[]' : '') . '"' . ($multiple ? ' multiple="multiple" size="' . (sizeof($array) < 8 ? sizeof($array) + 1 : 8) . '"' : '') . '>' . implode("\n\t", $opts) . "\r</select>";
60 }
61
62 // ################### Start construct_user_display ##################
63 // $userinfo needs userid, email, displayname, and showemail
64 function construct_user_display($userinfo, $html = true)
65 {
66 global $bugsys;
67
68 if (!$userinfo['userid'])
69 {
70 $userinfo['displayname'] = $bugsys->lang->string('Guest');
71 $userinfo['showemail'] = false;
72 }
73
74 if ($html)
75 {
76 eval('$username = "' . $bugsys->template->fetch('username_display') . '";');
77 }
78 else
79 {
80 if ($userinfo['showemail'])
81 {
82 $username = sprintf($bugsys->lang->string('%1$s &lt;%2$s&gt;'), $userinfo['displayname'], $userinfo['email']);
83 }
84 else
85 {
86 $username = $userinfo['displayname'];
87 }
88 }
89
90 return $username;
91 }
92
93 // ######################## Start can_perform ########################
94 // short-hand for bitwise &
95 function can_perform($bitmask, $productid = 0, $userinfo = null)
96 {
97 global $bugsys;
98
99 if ($userinfo == null)
100 {
101 $userinfo =& $bugsys->userinfo;
102 }
103
104 if (!isset($bugsys->permissions["$bitmask"]))
105 {
106 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
107 }
108
109 if ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
110 {
111 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
112
113 if (!in_array($bitmask, $inspecific))
114 {
115 $bugsys->debug("verdict on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]));
116 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]);
117 }
118 }
119
120 $bugsys->debug("verdict on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]));
121 return ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]);
122 }
123
124 // ###################################################################
125 /**
126 * Runs through a given datastore item and creates a series of <select>
127 * options.
128 *
129 * @access public
130 *
131 * @param string Datastore name
132 * @param string Array index for the label
133 * @param string Array index for the value
134 * @param mixed The selected value(s)
135 * @param bool Include a blank option? TRUE will set a null value, FALSE turns it off, anything else is used as the value for the blank option
136 * @param bool Generate it using admin printers?
137 *
138 * @return string Unelss in admin mode, returns the constructed options
139 */
140 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
141 {
142 global $bugsys;
143
144 if ($adminmode)
145 {
146 global $admin;
147 }
148
149 $select = '';
150
151 if ($includeblank === true OR $includeblank !== false)
152 {
153 $newval = ($inclueblank === true ? '' : $includeblank);
154 if ($adminmode)
155 {
156 $admin->list_item('', '', ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false));
157 }
158 else
159 {
160 $label = '';
161 $value = $newval;
162 $selected = ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false);
163 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
164 }
165 }
166
167 foreach ($bugsys->datastore["$datastore"] AS $item)
168 {
169 $label = $item["$labelname"];
170 $value = $item["$valuename"];
171 $selected = (($value == $selectedvalue OR (is_array($selectedvalue) AND in_array($value, $selectedvalue))) ? true : false);
172
173 if ($adminmode)
174 {
175 $admin->list_item($label, $value, $selected);
176 }
177 else
178 {
179 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
180 }
181 }
182
183 if (!$adminmode)
184 {
185 return $select;
186 }
187 }
188
189 // ################## Start construct_custom_fields ##################
190 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false)
191 {
192 global $bugsys;
193 static $fields;
194
195 if (!is_array($fields))
196 {
197 $fields = array();
198 $fields_fetch = $bugsys->db->query("
199 SELECT bugfield.*, permission.mask
200 FROM " . TABLE_PREFIX . "bugfield AS bugfield
201 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
202 ON (bugfield.fieldid = permission.fieldid)
203 WHERE (permission.mask = 2 OR permission.mask = 1)
204 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
205 );
206 while ($field = $bugsys->db->fetch_array($fields_fetch))
207 {
208 $fields["$field[fieldid]"] = $field;
209 }
210 }
211
212 $fieldvalues = $bugsys->db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = " . $bugsys->clean($bug['bugid'], TYPE_UINT));
213
214 $fieldbits = array();
215
216 foreach ($fields AS $field)
217 {
218 if ($nodefault)
219 {
220 $field['defaultvalue'] = '';
221 }
222
223 if (!is_null($bug["field$field[fieldid]"]))
224 {
225 $bugsys->debug("not null: $field[fieldid]");
226 $value = $bug["field$field[fieldid]"];
227 }
228 else
229 {
230 $value = $field['defaultvalue'];
231 }
232
233 if ($ignore21mask AND $field['mask'] != 0)
234 {
235 $field['mask'] = 2;
236 }
237
238 if ($field['mask'] == 2)
239 {
240 switch ($field['type'])
241 {
242 case 'input_text':
243 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
244 break;
245
246 case 'input_checkbox':
247 $selected = (($value) ? ' checked="checked"' : '');
248 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
249 break;
250
251 case 'select_single':
252 $selects = unserialize($field['selects']);
253 $value = trim($value);
254
255 $options = '';
256
257 $id = -1;
258 $select = '';
259 if (!$field['usedefault'] AND !trim($value))
260 {
261 $selected = ' selected="selected"';
262 }
263 else
264 {
265 $selected = '';
266 }
267 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
268
269 foreach ($selects AS $id => $select)
270 {
271 $selected = '';
272 $select = stripslashes(trim($select));
273 if ($select == $value)
274 {
275 $selected = ' selected="selected"';
276 }
277 else if ($field['usedefault'] AND $id == 0)
278 {
279 $selected = ' selected="selected"';
280 }
281 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
282 }
283 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
284 break;
285 }
286 }
287 else
288 {
289 $bugsys->debug('mask 1 processing');
290 if (is_null($fieldvalues["field$field[fieldid]"]))
291 {
292 $bugsys->debug("is null: $field[fieldid]");
293 if ($field['type'] == 'select_single')
294 {
295 if ($field['usedefault'])
296 {
297 $temp = unserialize($field['selects']);
298 $value = trim($temp[0]);
299 }
300 else
301 {
302 $value = $fieldvalues["field$field[fieldid]"];
303 }
304 }
305 else
306 {
307 $value = $field['defaultvalue'];
308 }
309 }
310 else
311 {
312 $value = $fieldvalues["field$field[fieldid]"];
313 }
314
315 if ($field['type'] == 'input_checkbox')
316 {
317 $value = (($value) ? 'True' : 'False');
318 }
319 $field['value'] = $value;
320 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
321 }
322 $fieldbits[] = $tempfield;
323 }
324
325 return $fieldbits;
326 }
327
328 // ################### Start process_custom_fields ###################
329 function process_custom_fields($bugid, $inputdata = array())
330 {
331 global $bugsys;
332
333 if (!$inputdata)
334 {
335 $inputdata =& $bugsys->in;
336 }
337
338 $fields = $bugsys->db->query("
339 SELECT bugfield.*
340 FROM " . TABLE_PREFIX . "bugfield AS bugfield
341 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
342 ON (bugfield.fieldid = permission.fieldid)
343 WHERE permission.mask = 2
344 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
345 );
346 while ($field = $bugsys->db->fetch_array($fields))
347 {
348 if ($field['type'] == 'input_checkbox')
349 {
350 $fieldbuild[] = 'field' . $field['fieldid'];
351 if (isset($inputdata["field$field[fieldid]"]))
352 {
353 $fieldvalue[] = 1;
354 }
355 else
356 {
357 $fieldvalue[] = 0;
358 }
359 continue;
360 }
361
362 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
363 {
364 $errorlist[] = sprintf($bugsys->lang->string('The "%1$s" field is a required field.'), $field['name']);
365 continue;
366 }
367
368 if (isset($inputdata["field$field[fieldid]"]))
369 {
370 $fieldbuild[] = 'field' . $field['fieldid'];
371
372 if ($field['type'] == 'input_text')
373 {
374 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
375 }
376 else
377 {
378 if ($inputdata["field$field[fieldid]"] == -1)
379 {
380 $fieldvalue[] = "''";
381 continue;
382 }
383
384 $temp = unserialize($field['selects']);
385 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
386 }
387 }
388 }
389
390 if ($errorlist)
391 {
392 return $errorlist;
393 }
394
395 if (sizeof($fieldbuild) < 1)
396 {
397 return;
398 }
399
400 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
401 }
402
403 // ####################### Start fetch_on_bits #######################
404 function fetch_on_bits($mask, $userinfo = null)
405 {
406 global $bugsys;
407
408 if ($userinfo == null)
409 {
410 $userinfo =& $bugsys->userinfo;
411 }
412
413 $onbits = array();
414
415 $usergroupid = $userinfo['usergroupid'];
416
417 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $bugsys->permissions["$mask"] AND is_array($bugsys->datastore['product']))
418 {
419 foreach ($bugsys->datastore['product'] AS $id => $product)
420 {
421 if (!$product['componentmother'])
422 {
423 $onbits["$id"] = $id;
424 }
425 }
426 }
427
428 if (is_array($bugsys->datastore['permission']["$usergroupid"]))
429 {
430 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
431 {
432 if ($bit & $bugsys->permissions["$mask"])
433 {
434 $onbits["$productid"] = $productid;
435 }
436 else
437 {
438 if ($onbits["$productid"])
439 {
440 unset($onbits["$productid"]);
441 }
442 }
443 }
444 }
445
446 if (sizeof($onbits) < 1)
447 {
448 $onbits = array(0);
449 }
450
451 return implode(',', $onbits);
452 }
453
454 // #################### Start isso_pre_parse_hook ####################
455 // the pre-parse hook for ISSO's template engine
456 function isso_pre_parse_hook($template)
457 {
458 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
459 return $template;
460 }
461
462 // ###################### Start fetch_help_link ######################
463 // returns a prepared link to insert into templates that opens up a
464 // help popup in the user-end
465 function fetch_help_link($topic)
466 {
467 global $bugsys;
468
469 if (isset($bugsys->datastore['help']["$topic"]))
470 {
471 eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
472 return $temp;
473 }
474 else
475 {
476 if ($bugsys->debug)
477 {
478 return "[[INVALID TOPIC: $topic]]";
479 }
480 // do we want this?
481 else if (null == 1)
482 {
483 return eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
484 }
485 }
486 }
487
488 // ###################################################################
489 /**
490 * Returns a user array of information that is specific to all visiting
491 * users (guests). This can then be passed to any function that requires
492 * user information.
493 *
494 * @access public
495 *
496 * @return array User information array
497 */
498 function fetch_guest_user()
499 {
500 global $bugsys;
501
502 return array(
503 'usergroupid' => 1,
504 'userid' => 0,
505 'email' => '',
506 'displayname' => '',
507 'showcolors' => 1,
508 'permissions' => $bugsys->datastore['usergroup'][1]['permissions'],
509 'displaytitle' => $bugsys->datastore['usergroup'][1]['displaytitle'],
510 );
511 }
512
513 // ###################################################################
514 /**
515 * Does an exhaustive permissions check on the bug. It checks for hidden
516 * bug status and ability to view hidden bugs. This normally was done
517 * at the top of each page, but it got so big, it was moved to a function.
518 *
519 * @access public
520 *
521 * @param array Bug array
522 * @param array Alternate user array
523 *
524 * @return bool Does the user have permission
525 */
526 function check_bug_permissions($bug, $userinfo = null)
527 {
528 global $bugsys;
529 if ($userinfo == null)
530 {
531 $userinfo = $bugsys->userinfo;
532 }
533
534 $bugsys->debug("checking permissions for $userinfo[userid] on bug $bug[bugid]");
535
536 if
537 (
538 !can_perform('canviewbugs', $bug['product'], $userinfo)
539 OR
540 !(
541 (
542 $bug['hidden']
543 AND
544 (
545 ($userinfo['userid'] == $bug['userid'] AND can_perform('canviewownhidden', $bug['productid'], $userinfo))
546 OR
547 can_perform('canviewhidden', $bug['productid'], $userinfo)
548 )
549 )
550 OR
551 !$bug['hidden']
552 )
553 )
554 {
555 return false;
556 }
557
558 return true;
559 }
560
561 /*=====================================================================*\
562 || ###################################################################
563 || # $HeadURL$
564 || # $Id$
565 || ###################################################################
566 \*=====================================================================*/
567 ?>