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
['permission'];
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.*, MAX(permission.mask) AS 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 IN ({$bugsys->userinfo['usergroupid']}" . (sizeof($bugsys->userinfo
['groupids']) != 0 ?
',' . implode(',', $bugsys->userinfo
['groupids']) : '') . ")
229 GROUP BY (bugfield.fieldid)
231 while ($field = $bugsys->db
->fetch_array($fields_fetch))
233 $fields["$field[fieldid]"] = $field;
237 $fieldbits = array();
239 foreach ($fields AS $field)
243 $field['defaultvalue'] = '';
246 if (!is_null($bug["custom$field[fieldid]"]))
248 $bugsys->debug("not null: $field[fieldid]");
249 $value = $bug["custom$field[fieldid]"];
253 $value = $field['defaultvalue'];
256 if ($ignore21mask AND $field['mask'] != 0)
261 if ($field['mask'] == 2)
263 switch ($field['type'])
266 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_input_text') . '";');
269 case 'input_checkbox':
270 $selected = ($value ?
' checked="checked"' : '');
271 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_input_checkbox') . '";');
274 case 'select_single':
275 $selects = unserialize($field['selects']);
276 $value = trim($value);
282 if (!$field['usedefault'] AND !trim($value))
284 $selected = ' selected="selected"';
290 eval('$options .= "' . $bugsys->template
->fetch('bugfield_select_single_option') . '";');
292 foreach ($selects AS $id => $select)
295 $select = stripslashes(trim($select));
296 if ($select == $value)
298 $selected = ' selected="selected"';
300 else if ($field['usedefault'] AND $id == 0)
302 $selected = ' selected="selected"';
304 eval('$options .= "' . $bugsys->template
->fetch('bugfield_select_single_option') . '";');
306 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_select_single') . '";');
312 $bugsys->debug('mask 1 processing');
313 if (is_null($bug["custom$field[fieldid]"]))
315 $bugsys->debug("is null: $field[fieldid]");
316 if ($field['type'] == 'select_single')
318 if ($field['usedefault'])
320 $temp = unserialize($field['selects']);
321 $value = trim($temp[0]);
325 $value = $bug["custom$field[fieldid]"];
330 $value = $field['defaultvalue'];
335 $value = $bug["custom$field[fieldid]"];
338 if ($field['type'] == 'input_checkbox')
340 $value = ($value ?
'True' : 'False');
342 $field['value'] = $value;
343 eval('$tempfield = "' . $bugsys->template
->fetch('bugfield_static_text') . '";');
345 $fieldbits[] = $tempfield;
351 // ###################################################################
353 * This takes the bug API object and input data and then sanitizes, verifies,
354 * and processes the data for custom fields. If there are any errors,
355 * they are passed to the message reporter.
357 * @param object A BugAPI object
358 * @param object MessageReporter object
359 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
360 * @param bool Search mode: don't change certain fields when they're 0 or empty
362 * @return mixed NULL if an ID is passed, string if bugid is NULL
364 function process_custom_fields(&$bugapi, &$msg, $errorbox = false
, $searchMode = false
)
370 $inputdata =& $bugsys->in
;
373 $fields = $bugsys->db
->query("
374 SELECT bugfield.*, MAX(permission.mask) AS mask
375 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
376 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
377 ON (bugfield.fieldid = permission.fieldid)
378 WHERE permission.mask = 2
379 AND permission.usergroupid IN ({$bugsys->userinfo['usergroupid']}" . (sizeof($bugsys->userinfo
['groupids']) != 0 ?
',' . implode(',', $bugsys->userinfo
['groupids']) : '') . ")
380 GROUP BY (bugfield.fieldid)
382 while ($field = $bugsys->db
->fetch_array($fields))
384 $fieldname = "custom$field[fieldid]";
386 if ($field['type'] == 'input_checkbox')
388 if ($searchMode AND intval($inputdata["$fieldname"]) == 0)
392 $bugapi->set($fieldname, intval($inputdata["$fieldname"]));
396 // field data wasn't passed, so skip it
397 if (!isset($inputdata["$fieldname"]))
402 if ($field['required'] AND empty($inputdata["$fieldname"]) AND !$searchMode)
404 $errorlist[] = sprintf(_('The field "%1$s" is a required.'), $field['name']);
408 if (!empty($field['regexmatch']))
410 if (!preg_match('#' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["$fieldname"]))
412 $errorlist[] = sprintf(_('%1$s does not match the specified format'), $field['name']);
417 if (isset($inputdata["$fieldname"]))
419 if ($field['type'] == 'input_text')
421 if (empty($inputdata["$fieldname"]) AND $searchMode)
425 $bugapi->set($fieldname, $inputdata["$fieldname"]);
429 if ($inputdata["$fieldname"] == -1)
433 $bugapi->set($fieldname, '');
438 $temp = unserialize($field['selects']);
439 $bugapi->set($fieldname, trim($temp[ intval($inputdata["$fieldname"]) ]));;
448 foreach ($errorlist AS $err)
450 $msg->addError($err);
455 $msg->error($errorlist[0]);
460 // ####################### Start fetch_on_bits #######################
461 function fetch_on_bits($mask, $userinfo = null
)
465 if ($userinfo == null
)
467 $userinfo =& $bugsys->userinfo
;
472 $usergroupid = $userinfo['usergroupid'];
473 FetchUserPermissions($userinfo); // get the groups
474 $groups = $userinfo['groupids'];
475 $groups[] = $usergroupid;
477 // product-inspecific work
478 if (is_array($bugsys->datastore
['product']))
480 foreach ($groups AS $groupid)
482 // we only need to do this so long as there's no onbits array because this isn't product specific
483 if (sizeof($onbits) == 0 AND $bugsys->datastore
['usergroup']["$groupid"]['permissions'] & $bugsys->permissions
["$mask"])
485 foreach ($bugsys->datastore
['product'] AS $id => $product)
487 $onbits["$id"] = $id;
493 // bits set explicitly by products
496 // product specific work
497 foreach ($groups AS $groupid)
499 if (is_array($bugsys->datastore
['permission']["$groupid"]))
501 foreach ($bugsys->datastore
['permission']["$groupid"] AS $productid => $bit)
503 if ($bit & $bugsys->permissions
["$mask"])
505 $explicit["$productid"] = $productid;
506 $onbits["$productid"] = $productid;
510 // only unset if the bit was set in the first place by blanket and not product-specific permissions
511 // if it was set by product permissions then the highest level takes precedence
512 if ($onbits["$productid"] AND !isset($explicit["$productid"]))
514 unset($onbits["$productid"]);
521 // SQL queries would become very unhappy if we didn't do this
522 if (sizeof($onbits) < 1)
527 return implode(',', $onbits);
530 // #################### Start isso_pre_parse_hook ####################
531 // the pre-parse hook for ISSO's template engine
532 function isso_pre_parse_hook($template)
534 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
538 // ###################### Start fetch_help_link ######################
539 // returns a prepared link to insert into templates that opens up a
540 // help popup in the user-end
541 function fetch_help_link($topic)
545 if (isset($bugsys->datastore
['help']["$topic"]))
547 eval('$temp = "' . $bugsys->template
->fetch('help_link') . '";');
554 return "[[INVALID TOPIC: $topic]]";
559 return eval('$temp = "' . $bugsys->template
->fetch('help_link') . '";');
564 // ###################################################################
566 * Returns a user array of information that is specific to all visiting
567 * users (guests). This can then be passed to any function that requires
572 * @return array User information array
574 function fetch_guest_user()
580 'groupids' => array(),
585 'permissions' => $bugsys->datastore
['usergroup'][1]['permissions'],
586 'displaytitle' => $bugsys->datastore
['usergroup'][1]['displaytitle'],
587 'timezone' => $bugsys->options
['defaulttimezone']
591 // ###################################################################
593 * Does an exhaustive permissions check on the bug. It checks for hidden
594 * bug status and ability to view hidden bugs. This normally was done
595 * at the top of each page, but it got so big, it was moved to a function.
599 * @param array Bug array
600 * @param array Alternate user array
602 * @return bool Does the user have permission
604 function check_bug_permissions($bug, $userinfo = null
)
607 if ($userinfo == null
)
609 $userinfo = $bugsys->userinfo
;
612 $bugsys->debug("checking permissions for $userinfo[userid] on bug $bug[bugid]");
614 $bugsys->debug('*** START VERBOSE CHECK ***');
616 $bugsys->debug('* !can_perform(canviewbugs, $bug[product], $userinfo) = ' . (int)(!can_perform('canviewbugs', $bug['product'], $userinfo)));
617 $bugsys->debug('* $bug[hidden] = ' . (int)$bug['hidden']);
618 $bugsys->debug('* $userinfo[userid] (' . $userinfo['userid'] . ') == $bug[userid] (' . $bug['userid'] . ') = ' . (int)($userinfo['userid'] == $bug['userid']));
619 $bugsys->debug('* can_perform(canviewownhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewownhidden', $bug['product'], $userinfo)));
620 $bugsys->debug('* can_perform(canviewhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewhidden', $bug['product'], $userinfo)));
621 $bugsys->debug('* !$bug[hidden] = ' . (int)(!$bug['hidden']));
623 $bugsys->debug('*** END PERMISSIONS CHECK ***');
627 !can_perform('canviewbugs', $bug['product'], $userinfo)
634 ($userinfo['userid'] == $bug['userid'] AND can_perform('canviewownhidden', $bug['product'], $userinfo))
636 can_perform('canviewhidden', $bug['product'], $userinfo)
644 $bugsys->debug('*** DONE WITH REAL CALLS ***');
648 $bugsys->debug('*** DONE WITH REAL CALLS ***');
653 // ###################################################################
655 * Takes an array of bug information and returns another array with
656 * information that is suitable for display as all the IDs have been
657 * replaced by their string equivalents
659 * @param array Unprocessed bug data
660 * @param string Color to display if the user has opted to not show status colours
662 * @param array Bug array with data fit for display
664 function ProcessBugDataForDisplay($bug, $color = '')
668 $bug['hiddendisplay'] = ($bug['hidden'] AND (can_perform('canviewhidden', $bug['product']) OR (can_perform('canviewownhidden') AND $bug['userid'] == $bugsys->userinfo
['userid'])));
670 $bug['bgcolor'] = ($bugsys->userinfo
['showcolors'] ?
$bugsys->datastore
['status']["$bug[status]"]['color'] : $color);
671 $bug['product'] = $bugsys->datastore
['product']["$bug[product]"]['title'];
672 $bug['version'] = $bugsys->datastore
['version']["$bug[version]"]['version'];
673 $bug['component'] = $bugsys->datastore
['component']["$bug[component]"]['title'];
674 $bug['status'] = $bugsys->datastore
['status']["$bug[status]"]['status'];
675 $bug['resolution'] = $bugsys->datastore
['resolution']["$bug[resolution]"]['resolution'];
676 $bug['priority'] = $bugsys->datastore
['priority']["$bug[priority]"]['priority'];
677 $bug['severity'] = $bugsys->datastore
['severity']["$bug[severity]"]['severity'];
678 $bug['assignedto'] = ((empty($bug['assignedto']) OR !isset($bugsys->datastore
['assignto']["$bug[assignedto]"])) ?
'' : construct_user_display($bugsys->datastore
['assignto']["$bug[assignedto]"]));
680 $bug['lastposttime'] = ($bug['hiddendisplay'] ?
$bug['hiddenlastposttime'] : $bug['lastposttime']);
681 $bug['lastpost'] = ($bug['hiddendisplay'] ?
$bug['hiddenlastpostbyname'] : $bug['lastpostbyname']);
683 $bug['lastposttime'] = $bugsys->datef
->format($bugsys->options
['dateformat'], $bug['lastposttime']);
688 // ###################################################################
690 * Loads the pagination module and sets all of the appropriate options
695 function LoadPaginationFramework()
699 $bugsys->load('pagination', 'pagination', true
);
700 $bugsys->pagination
->setDefaultPerPage($bugsys->options
['defaultpp']);
701 $bugsys->pagination
->setMaxPerPage($bugsys->options
['maxpp']);
702 $bugsys->pagination
->setPageLinks($bugsys->options
['pagelinks']);
703 $bugsys->pagination
->setPageVar('p');
704 $bugsys->pagination
->setPerPageVar('pp');
705 $bugsys->pagination
->setBitProcessor('PageNavigatorBitCallback');
706 $bugsys->pagination
->setNavigatorProcessor('PageNavigatorCallback');
707 $bugsys->pagination
->processIncomingData();
710 // ###################################################################
712 * Callback function for the Pagination->BitProcessor()
714 * @param string Base link
715 * @param bool Do not show this as a link
716 * @param integer Page number
717 * @param object Page navigator framework
719 * @return string Processed HTML
721 function PageNavigatorBitCallback($baselink, $nolink, $number, $paginator)
724 eval('$return = "' . $bugsys->template
->fetch('pagenav_bit') . '";');
728 // ###################################################################
730 * Callback function for the Pagination->NavigatorProcessor()
732 * @param string Base URL
733 * @param integer Next page number
734 * @param integer Previous page number
735 * @param array Show information
736 * @param string Individual page bits
737 * @param object Page navigator framework
739 * @return string Processed HTML
741 function PageNavigatorCallback($baselink, $nextpage, $prevpage, $show, $pagebits, $paginator)
744 eval('$return = "' . $bugsys->template
->fetch('pagenav') . '";');
748 // ###################################################################
750 * Determines the correct permissions of the user. This is especially
751 * important for working with multiple-usergroup permission schemes.
752 * If a user is assigned to more than one usergroup, the highest level
753 * will always override (so a YES will always override a NO); this is
754 * because permissions are calculated with bitwise OR.
756 * @param array The user array with usergroups already exploded
758 * @return integer Permissions value
760 function FetchUserPermissions(&$user)
764 $perms = (int)$bugsys->datastore
['usergroup']["$user[usergroupid]"]['permissions'];
765 if (!is_array($user['groupids']))
767 $user['groupids'] = explode(',', $bugsys->userinfo
['groupids']);
769 $user['groupids'] = $bugsys->funct
->array_strip_empty($user['groupids']);
771 foreach ($user['groupids'] AS $group)
773 $perms |
= (int)$bugsys->datastore
['usergroup']["$group"]['permissions'];
779 /*=====================================================================*\
780 || ###################################################################
783 || ###################################################################
784 \*=====================================================================*/