2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
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
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 \*=====================================================================*/
22 // ###################################################################
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.
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
37 * @return string Constructed HTML output
39 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false, $multiple = false)
45 $selected = explode(',', $selected);
48 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
49 if ($includenil !== false)
51 $opts[] = '<option value="' . $includenil . '"' . ((!$selected OR (is_array($selected) AND in_array($includenil, $selected))) ? ' selected="selected"' : '') . '> ---------</option>';
53 foreach ($array AS $value => $label)
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>';
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>";
62 // ################### Start construct_user_display ##################
63 // $userinfo needs userid, email, displayname, and showemail
64 function construct_user_display($userinfo, $html = true)
68 if (!$userinfo['userid'])
70 $userinfo['displayname'] = _('Guest');
71 $userinfo['showemail'] = false;
76 eval('$username = "' . $bugsys->template
->fetch('username_display') . '";');
80 if ($userinfo['showemail'])
82 $username = sprintf(_('%1$s <%2$s>'), $userinfo['displayname'], $userinfo['email']);
86 $username = $userinfo['displayname'];
93 // ######################## Start can_perform ########################
94 // short-hand for bitwise &
95 function can_perform($bitmask, $productid = 0, $userinfo = null)
99 // masks that aren't product-specific
100 static $inspecific = array(
112 if ($userinfo == null)
114 $userinfo =& $bugsys->userinfo
;
117 $permissions =& $bugsys->datastore
['permissions'];
119 if (!isset($bugsys->permissions
["$bitmask"]))
121 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
124 if (!$userinfo['permissions'])
126 $userinfo['permissions'] = FetchUserPermissions($userinfo);
129 if ($productid AND !in_array($bitmask, $inspecific))
131 $verdict = (isset($permissions["$userinfo[usergroupid
]"]["$productid"]) ? ($permissions["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]) : ($userinfo['permissions'] & $bugsys->permissions
["$bitmask"]));
133 foreach ($userinfo['groupids'] AS $group)
135 if (isset($permissions["$group"]["$productid"]))
137 $verdict |= ($permissions["$group"]["$productid"] & $bugsys->permissions["$bitmask"]);
140 $bugsys->debug("verdict* on can_perform($bitmask, $productid, $userinfo[userid]) = $verdict");
144 $bugsys->debug("verdict on
can_perform($bitmask, $productid, $userinfo[userid
]) = " . ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]));
145 return ($userinfo['permissions'] & $bugsys->permissions
["$bitmask"]);
148 // ###################################################################
150 * Runs through a given datastore item and creates a series of <select>
155 * @param string Datastore name
156 * @param string Array index for the label
157 * @param string Array index for the value
158 * @param mixed The selected value(s)
159 * @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
160 * @param bool Generate it using admin printers?
162 * @return string Unelss in admin mode, returns the constructed options
164 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
175 if ($includeblank === true OR $includeblank !== false)
177 $newval = ($inclueblank === true ? '' : $includeblank);
180 $admin->list_item('', '', ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false));
186 $selected = ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false);
187 eval('$select .= "' . $bugsys->template->fetch('selectoption
') . '";');
191 foreach ($bugsys->datastore["$datastore"] AS $item)
193 $label = $item["$labelname"];
194 $value = $item["$valuename"];
195 $selected = (($value == $selectedvalue OR (is_array($selectedvalue) AND in_array($value, $selectedvalue))) ? true : false);
199 $admin->list_item($label, $value, $selected);
203 eval('$select .= "' . $bugsys->template
->fetch('selectoption') . '";');
213 // ################## Start construct_custom_fields ##################
214 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false, $searchMode = false)
219 if (!is_array($fields))
222 $fields_fetch = $bugsys->db
->query("
223 SELECT bugfield.*, permission.mask
224 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
225 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
226 ON (bugfield.fieldid = permission.fieldid)
227 WHERE (permission.mask = 2 OR permission.mask = 1)
228 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
230 while ($field = $bugsys->db
->fetch_array($fields_fetch))
232 $fields["$field[fieldid]"] = $field;
236 $fieldbits = array();
238 foreach ($fields AS $field)
242 $field['defaultvalue'] = '';
245 if (!is_null($bug["custom$field[fieldid]"]))
247 $bugsys->debug("not null: $field[fieldid]");
248 $value = $bug["custom$field[fieldid]"];
252 $value = $field['defaultvalue'];
255 if ($ignore21mask AND $field['mask'] != 0)
260 if ($field['mask'] == 2)
262 switch ($field['type'])
265 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_input_text') . '";');
268 case 'input_checkbox':
269 $selected = ($value ? ' checked="checked"' : '');
270 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_input_checkbox') . '";');
273 case 'select_single':
274 $selects = unserialize($field['selects']);
275 $value = trim($value);
281 if (!$field['usedefault'] AND !trim($value))
283 $selected = ' selected="selected"';
289 eval('$options .= "' . $bugsys->template
->fetch('bugfield_select_single_option') . '";');
291 foreach ($selects AS $id => $select)
294 $select = stripslashes(trim($select));
295 if ($select == $value)
297 $selected = ' selected="selected"';
299 else if ($field['usedefault'] AND $id == 0)
301 $selected = ' selected="selected"';
303 eval('$options .= "' . $bugsys->template
->fetch('bugfield_select_single_option') . '";');
305 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_select_single') . '";');
311 $bugsys->debug('mask 1 processing');
312 if (is_null($bug["custom$field[fieldid]"]))
314 $bugsys->debug("is null: $field[fieldid]");
315 if ($field['type'] == 'select_single')
317 if ($field['usedefault'])
319 $temp = unserialize($field['selects']);
320 $value = trim($temp[0]);
324 $value = $bug["custom$field[fieldid]"];
329 $value = $field['defaultvalue'];
334 $value = $bug["custom$field[fieldid]"];
337 if ($field['type'] == 'input_checkbox')
339 $value = ($value ? 'True' : 'False');
341 $field['value'] = $value;
342 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_static_text') . '";');
344 $fieldbits[] = $tempfield;
350 // ###################################################################
352 * This takes the bug API object and input data and then sanitizes, verifies,
353 * and processes the data for custom fields. If there are any errors,
354 * they are passed to the message reporter.
356 * @param object A BugAPI object
357 * @param object MessageReporter object
358 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
359 * @param bool Search mode: don't change certain fields when they're 0 or empty
361 * @return mixed NULL if an ID is passed, string if bugid is NULL
363 function process_custom_fields(&$bugapi, &$msg, $errorbox = false, $searchMode = false)
369 $inputdata =& $bugsys->in
;
372 $fields = $bugsys->db
->query("
374 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
375 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
376 ON (bugfield.fieldid = permission.fieldid)
377 WHERE permission.mask = 2
378 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
380 while ($field = $bugsys->db
->fetch_array($fields))
382 $fieldname = "custom$field[fieldid]";
383 if ($field['type'] == 'input_checkbox')
385 if ($searchMode AND intval($inputdata["$fieldname"]) == 0)
389 $bugapi->set($fieldname, intval($inputdata["$fieldname"]));
393 if ($field['required'] AND empty($inputdata["$fieldname"]) AND !$searchMode)
395 $errorlist[] = sprintf(_('The "%
1$s" field is a required field
.'), $field['name
']);
399 if (!empty($field['regexmatch
']))
401 if (!preg_match('#' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["$fieldname"]))
403 $errorlist[] = sprintf(_('%1$s does not match the specified format'), $field['name']);
408 if (isset($inputdata["$fieldname"]))
410 if ($field['type'] == 'input_text')
412 if (empty($inputdata["$fieldname"]) AND $searchMode)
416 $bugapi->set($fieldname, $inputdata["$fieldname"]);
420 if ($inputdata["$fieldname"] == -1)
424 $bugapi->set($fieldname, '');
429 $temp = unserialize($field['selects']);
430 $bugapi->set($fieldname, trim($temp[ intval($inputdata["$fieldname"]) ]));;
439 foreach ($errorlist AS $err)
441 $msg->addError($err);
446 $msg->error($errorlist[0]);
451 // ####################### Start fetch_on_bits #######################
452 function fetch_on_bits($mask, $userinfo = null)
456 if ($userinfo == null)
458 $userinfo =& $bugsys->userinfo;
463 $usergroupid = $userinfo['usergroupid'];
465 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $bugsys->permissions
["$mask"] AND is_array($bugsys->datastore['product']))
467 foreach ($bugsys->datastore['product'] AS $id => $product)
469 $onbits["$id"] = $id;
473 if (is_array($bugsys->datastore
['permission']["$usergroupid"]))
475 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
477 if ($bit & $bugsys->permissions
["$mask"])
479 $onbits["$productid"] = $productid;
483 if ($onbits["$productid"])
485 unset($onbits["$productid"]);
491 if (sizeof($onbits) < 1)
496 return implode(',', $onbits);
499 // #################### Start isso_pre_parse_hook ####################
500 // the pre-parse hook for ISSO's template engine
501 function isso_pre_parse_hook($template)
503 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
507 // ###################### Start fetch_help_link ######################
508 // returns a prepared link to insert into templates that opens up a
509 // help popup in the user-end
510 function fetch_help_link($topic)
514 if (isset($bugsys->datastore
['help']["$topic"]))
516 eval('$temp = "' . $bugsys->template->fetch('help_link
') . '";');
523 return "[[INVALID TOPIC
: $topic]]";
528 return eval('$temp = "' . $bugsys->template->fetch('help_link
') . '";');
533 // ###################################################################
535 * Returns a user array of information that is specific to all visiting
536 * users (guests). This can then be passed to any function that requires
541 * @return array User information array
543 function fetch_guest_user()
549 'groupids' => array(),
554 'permissions' => $bugsys->datastore['usergroup'][1]['permissions'],
555 'displaytitle' => $bugsys->datastore['usergroup'][1]['displaytitle'],
556 'timezone' => $bugsys->options['defaulttimezone']
560 // ###################################################################
562 * Does an exhaustive permissions check on the bug. It checks for hidden
563 * bug status and ability to view hidden bugs. This normally was done
564 * at the top of each page, but it got so big, it was moved to a function.
568 * @param array Bug array
569 * @param array Alternate user array
571 * @return bool Does the user have permission
573 function check_bug_permissions($bug, $userinfo = null)
576 if ($userinfo == null)
578 $userinfo = $bugsys->userinfo;
581 $bugsys->debug("checking permissions
for $userinfo[userid
] on bug
$bug[bugid
]");
583 $bugsys->debug('*** START VERBOSE CHECK ***');
585 $bugsys->debug('* !can_perform(canviewbugs, $bug[product], $userinfo) = ' . (int)(!can_perform('canviewbugs', $bug['product'], $userinfo)));
586 $bugsys->debug('* $bug[hidden] = ' . (int)$bug['hidden']);
587 $bugsys->debug('* $userinfo[userid] (' . $userinfo['userid'] . ') == $bug[userid] (' . $bug['userid'] . ') = ' . (int)($userinfo['userid'] == $bug['userid']));
588 $bugsys->debug('* can_perform(canviewownhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewownhidden', $bug['product'], $userinfo)));
589 $bugsys->debug('* can_perform(canviewhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewhidden', $bug['product'], $userinfo)));
590 $bugsys->debug('* !$bug[hidden] = ' . (int)(!$bug['hidden']));
592 $bugsys->debug('*** END PERMISSIONS CHECK ***');
596 !can_perform('canviewbugs', $bug['product'], $userinfo)
603 ($userinfo['userid'] == $bug['userid'] AND can_perform('canviewownhidden', $bug['product'], $userinfo))
605 can_perform('canviewhidden', $bug['product'], $userinfo)
613 $bugsys->debug('*** DONE WITH REAL CALLS ***');
617 $bugsys->debug('*** DONE WITH REAL CALLS ***');
622 // ###################################################################
624 * Takes an array of bug information and returns another array with
625 * information that is suitable for display as all the IDs have been
626 * replaced by their string equivalents
628 * @param array Unprocessed bug data
629 * @param string Color to display if the user has opted to not show status colours
631 * @param array Bug array with data fit for display
633 function ProcessBugDataForDisplay($bug, $color = '')
637 $bug['hiddendisplay'] = ($bug['hidden'] AND (can_perform('canviewhidden', $bug['product']) OR (can_perform('canviewownhidden') AND $bug['userid'] == $bugsys->userinfo['userid'])));
639 $bug['bgcolor'] = ($bugsys->userinfo['showcolors'] ? $bugsys->datastore['status']["$bug[status
]"]['color'] : $color);
640 $bug['product'] = $bugsys->datastore['product']["$bug[product
]"]['title'];
641 $bug['version'] = $bugsys->datastore['version']["$bug[version
]"]['version'];
642 $bug['component'] = $bugsys->datastore['component']["$bug[component
]"]['title'];
643 $bug['status'] = $bugsys->datastore['status']["$bug[status
]"]['status'];
644 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution
]"]['resolution'];
645 $bug['priority'] = $bugsys->datastore['priority']["$bug[priority
]"]['priority'];
646 $bug['severity'] = $bugsys->datastore['severity']["$bug[severity
]"]['severity'];
648 $bug['lastposttime'] = ($bug['hiddendisplay'] ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
649 $bug['lastpost'] = ($bug['hiddendisplay'] ? $bug['hiddenlastpostbyname'] : $bug['lastpostbyname']);
651 $bug['lastposttime'] = $bugsys->datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
656 // ###################################################################
658 * Loads the pagination module and sets all of the appropriate options
663 function LoadPaginationFramework()
667 $bugsys->load('pagination', 'pagination', true);
668 $bugsys->pagination->setDefaultPerPage($bugsys->options['defaultpp']);
669 $bugsys->pagination->setMaxPerPage($bugsys->options['maxpp']);
670 $bugsys->pagination->setPageLinks($bugsys->options['pagelinks']);
671 $bugsys->pagination->setPageVar('p');
672 $bugsys->pagination->setPerPageVar('pp');
673 $bugsys->pagination->setBitProcessor('PageNavigatorBitCallback');
674 $bugsys->pagination->setNavigatorProcessor('PageNavigatorCallback');
675 $bugsys->pagination->processIncomingData();
678 // ###################################################################
680 * Callback function for the Pagination->BitProcessor()
682 * @param string Base link
683 * @param bool Do not show this as a link
684 * @param integer Page number
685 * @param object Page navigator framework
687 * @return string Processed HTML
689 function PageNavigatorBitCallback($baselink, $nolink, $number, $paginator)
692 eval('$return = "' . $bugsys->template->fetch('pagenav_bit
') . '";');
696 // ###################################################################
698 * Callback function for the Pagination->NavigatorProcessor()
700 * @param string Base URL
701 * @param integer Next page number
702 * @param integer Previous page number
703 * @param array Show information
704 * @param string Individual page bits
705 * @param object Page navigator framework
707 * @return string Processed HTML
709 function PageNavigatorCallback($baselink, $nextpage, $prevpage, $show, $pagebits, $paginator)
712 eval('$return = "' . $bugsys->template->fetch('pagenav
') . '";');
716 // ###################################################################
718 * Determines the correct permissions of the user. This is especially
719 * important for working with multiple-usergroup permission schemes.
720 * If a user is assigned to more than one usergroup, the highest level
721 * will always override (so a YES will always override a NO); this is
722 * because permissions are calculated with bitwise OR.
724 * @param array The user array with usergroups already exploded
726 * @return integer Permissions value
728 function FetchUserPermissions(&$user)
732 $perms = (int)$bugsys->datastore['usergroup']["$user[usergroupid
]"]['permissions'];
733 if (!is_array($user['groupids']))
735 $user['groupids'] = explode(',', $bugsys->userinfo['groupids']);
738 foreach ($user['groupids'] AS $group)
740 $perms |= (int)$bugsys->datastore['usergroup']["$group"]['permissions'];
746 /*=====================================================================*\
747 || ###################################################################
750 || ###################################################################
751 \*=====================================================================*/