2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright (c)2004-2009 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 2 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
)
43 $selected = explode(',', $selected);
46 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
47 if ($includenil !== false
)
49 $opts[] = '<option value="' . $includenil . '"' . ((!$selected OR (is_array($selected) AND in_array($includenil, $selected))) ?
' selected="selected"' : '') . '> ---------</option>';
51 foreach ($array AS $value => $label)
53 $newval = ($valuekey ?
$label["$valuekey"] : $value);
54 $newlab = ($labelkey ?
$label["$labelkey"] : $label);
55 $opts[] = '<option value="' . $newval . '"' . (($selected == $newval OR (is_array($selected) AND in_array($newval, $selected))) ?
' selected="selected"' : '') . '>' . $newlab . '</option>';
57 return '<select class="input" id="' . $name . '" name="' . $name . ($multiple ?
'[]' : '') . '"' . ($multiple ?
' multiple="multiple" size="' . (sizeof($array) < 8 ?
sizeof($array) +
1 : 8) . '"' : '') . '>' . implode("\n\t", $opts) . "\r</select>";
61 * Constructs the user information link
63 * @param array Userinfo array - requires userid, email, displayname, and showemail values
64 * @param bool Return HTML or just a string?
67 function construct_user_display($userinfo, $html = true
)
69 if (!$userinfo['userid'])
71 $userinfo['displayname'] = T('Guest');
72 $userinfo['showemail'] = false
;
77 $tpl = new BSTemplate('username_display');
78 $tpl->vars
= array('userinfo' => $userinfo);
79 $username = $tpl->evaluate()->getTemplate();
83 if ($userinfo['showemail'])
85 $username = sprintf(T('%1$s <%2$s>'), $userinfo['displayname'], $userinfo['email']);
89 $username = $userinfo['displayname'];
96 // ######################## Start can_perform ########################
97 // short-hand for bitwise &
98 function can_perform($bitmask, $productid = 0, $userinfo = null
)
100 // masks that aren't product-specific
101 static $inspecific = array(
113 if ($userinfo == null
)
115 $userinfo =& bugdar
::$userinfo;
118 $permissions =& bugdar
::$datastore['permission'];
120 if (!isset(bugdar
::$permissions["$bitmask"]))
122 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING
);
125 if (!$userinfo['permissions'])
127 $userinfo['permissions'] = FetchUserPermissions($userinfo);
130 if ($productid AND !in_array($bitmask, $inspecific))
132 $verdict = (isset($permissions["$userinfo[usergroupid]"]["$productid"]) ?
($permissions["$userinfo[usergroupid]"]["$productid"] & bugdar
::$permissions["$bitmask"]) : ($userinfo['permissions'] & bugdar
::$permissions["$bitmask"]));
134 foreach ($userinfo['groupids'] AS $group)
136 if (isset($permissions["$group"]["$productid"]))
138 $verdict |
= ($permissions["$group"]["$productid"] & bugdar
::$permissions["$bitmask"]);
141 BSApp
::debug("verdict* on can_perform($bitmask, $productid, $userinfo[userid]) = $verdict");
145 BSApp
::debug("verdict on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($userinfo['permissions'] & bugdar
::$permissions["$bitmask"]));
146 return ($userinfo['permissions'] & bugdar
::$permissions["$bitmask"]);
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
)
173 if ($includeblank === true
OR $includeblank !== false
)
175 $newval = ($inclueblank === true ?
'' : $includeblank);
178 $admin->list_item('', '', ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true
: false
));
182 $tpl = new BSTemplate('selectoption');
186 'selected' => (!$selectedvalue ||
(is_array($selectedvalue) && in_array($newval, $selectedvalue)))
188 $select .= $tpl->evaluate()->getTemplate();
192 foreach (bugdar
::$datastore["$datastore"] AS $item)
194 $label = $item["$labelname"];
195 $value = $item["$valuename"];
196 $selected = (($value == $selectedvalue OR (is_array($selectedvalue) AND in_array($value, $selectedvalue))) ? true
: false
);
200 $admin->list_item($label, $value, $selected);
204 $tpl = new BSTemplate('selectoption');
208 'selected' => $selected
210 $select .= $tpl->evaluate()->getTemplate();
220 // ################## Start construct_custom_fields ##################
221 function construct_custom_fields($bug = array(), $ignore21mask = false
, $nodefault = false
, $searchMode = false
)
225 if (!is_array($fields))
228 $fields_fetch = BSApp
::$db->query("
229 SELECT bugfield.*, MAX(permission.mask) AS mask
230 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
231 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
232 ON (bugfield.fieldid = permission.fieldid)
233 WHERE (permission.mask = 2 OR permission.mask = 1)
234 AND permission.usergroupid IN (" . bugdar
::$userinfo['usergroupid'] . (sizeof(bugdar
::$userinfo['groupids']) != 0 ?
',' . implode(',', bugdar
::$userinfo['groupids']) : '') . ")
235 GROUP BY (bugfield.fieldid)
237 foreach ($fields_fetch as $field)
239 $fields["$field[fieldid]"] = $field;
243 $fieldbits = array();
245 foreach ($fields AS $field)
249 $field['defaultvalue'] = '';
252 if (!is_null($bug["custom$field[fieldid]"]))
254 BSApp
::debug("not null: $field[fieldid]");
255 $value = $bug["custom$field[fieldid]"];
259 $value = $field['defaultvalue'];
262 if ($ignore21mask AND $field['mask'] != 0)
267 if ($field['mask'] == 2)
269 switch ($field['type'])
272 $tpl = new BSTemplate('bugfield_input_text');
277 $tempfield = $tpl->evaluate()->getTemplate();
280 case 'input_checkbox':
281 $tpl = new BSTemplate('bugfield_input_checkbox');
284 'searchMode' => $searchMode,
285 'selected' => ($value ?
' checked="checked"' : '')
287 $tempfield = $tpl->evaluate()->getTemplate();
290 case 'select_single':
291 $selects = unserialize($field['selects']);
292 $value = trim($value);
295 // this overrides $field['usedefault'] because required fields will no longer have
296 // blank values as options
297 // TODO document above comment in the ACP
298 if (!$field['required'])
300 $tpl = new BSTemplate('bugfield_select_single_option');
304 'selected' => ((!$field['usedefault'] && !trim($value)) ?
' selected="selected"' : '')
306 $options = $tpl->evaluate()->getTemplate();
309 foreach ($selects as $id => $select)
311 $tpl = new BSTemplate('bugfield_select_single_option');
314 'select' => stripslashes(trim($select)),
315 'selected' => (($select == $value ||
($field['usedefault'] && $id == 0)) ?
' selected="selected"' : '')
317 $options .= $tpl->evaluate()->getTemplate();
320 $tpl = new BSTemplate('bugfield_select_single');
323 'options' => $options
325 $tempfield = $tpl->evaluate()->getTemplate();
331 BSApp
::debug('mask 1 processing');
332 if (is_null($bug["custom$field[fieldid]"]))
334 BSApp
::debug("is null: $field[fieldid]");
335 if ($field['type'] == 'select_single')
337 if ($field['usedefault'])
339 $temp = unserialize($field['selects']);
340 $value = trim($temp[0]);
344 $value = $bug["custom$field[fieldid]"];
349 $value = $field['defaultvalue'];
354 $value = $bug["custom$field[fieldid]"];
357 if ($field['type'] == 'input_checkbox')
359 $value = ($value ?
'True' : 'False');
361 $field['value'] = $value;
363 $tpl = new BSTemplate('bugfield_static_text');
364 $tpl->vars
= array('field' => $field);
365 $tempfield = $tpl->evaluate()->getTemplate();
367 $fieldbits[] = $tempfield;
373 // ###################################################################
375 * This takes the bug API object and input data and then sanitizes, verifies,
376 * and processes the data for custom fields. If there are any errors,
377 * they are passed to the message reporter.
379 * @param object A BugAPI object
380 * @param object MessageReporter object
381 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
382 * @param bool Search mode: don't change certain fields when they're 0 or empty
384 * @return mixed NULL if an ID is passed, string if bugid is NULL
386 function process_custom_fields(&$bugapi, &$msg, $errorbox = false
, $searchMode = false
)
390 $inputdata = &BSApp
::$input->in
;
393 $fields = BSApp
::$db->query("
394 SELECT bugfield.*, MAX(permission.mask) AS mask
395 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
396 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
397 ON (bugfield.fieldid = permission.fieldid)
398 WHERE permission.mask = 2
399 AND permission.usergroupid IN (" . bugdar
::$userinfo['usergroupid'] . (sizeof(bugdar
::$userinfo['groupids']) != 0 ?
',' . implode(',', bugdar
::$userinfo['groupids']) : '') . ")
400 GROUP BY (bugfield.fieldid)
402 foreach ($fields as $field)
404 $fieldname = "custom$field[fieldid]";
406 if ($field['type'] == 'input_checkbox')
408 if ($searchMode AND intval($inputdata["$fieldname"]) == 0)
412 $bugapi->set($fieldname, intval($inputdata["$fieldname"]));
415 else if ($field['type'] == 'select_single')
417 $temp = unserialize($field['selects']);
418 $inputdata[$fieldname] = $temp[intval($inputdata["$fieldname"])] . ''; // make it a string so isset() doesn't catch
421 // field data wasn't passed, so skip it
422 if (!isset($inputdata["$fieldname"]))
427 if ($field['required'] AND empty($inputdata["$fieldname"]) AND !$searchMode)
429 $errorlist[] = sprintf(T('The field "%1$s" is a required.'), $field['name']);
433 if (!empty($field['regexmatch']))
435 if (!preg_match('#' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["$fieldname"]))
437 $errorlist[] = sprintf(T('%1$s does not match the specified format'), $field['name']);
442 if (isset($inputdata["$fieldname"]))
444 if ($field['type'] == 'input_text')
446 if (empty($inputdata["$fieldname"]) AND $searchMode)
450 $bugapi->set($fieldname, $inputdata["$fieldname"]);
454 if (empty($inputdata["$fieldname"]))
458 $bugapi->set($fieldname, '');
463 $bugapi->set($fieldname, trim($inputdata["$fieldname"]));
472 foreach ($errorlist AS $err)
474 $msg->addError($err);
479 $msg->error($errorlist[0]);
484 // ####################### Start fetch_on_bits #######################
485 function fetch_on_bits($mask, $userinfo = null
)
487 if ($userinfo == null
)
489 $userinfo =& bugdar
::$userinfo;
494 $usergroupid = $userinfo['usergroupid'];
495 FetchUserPermissions($userinfo); // get the groups
496 $groups = $userinfo['groupids'];
497 $groups[] = $usergroupid;
499 // product-inspecific work
500 if (is_array(bugdar
::$datastore['product']))
502 foreach ($groups AS $groupid)
504 // we only need to do this so long as there's no onbits array because this isn't product specific
505 if (sizeof($onbits) == 0 AND bugdar
::$datastore['usergroup']["$groupid"]['permissions'] & bugdar
::$permissions["$mask"])
507 foreach (bugdar
::$datastore['product'] AS $id => $product)
509 $onbits["$id"] = $id;
515 // bits set explicitly by products
518 // product specific work
519 foreach ($groups AS $groupid)
521 if (is_array(bugdar
::$datastore['permission']["$groupid"]))
523 foreach (bugdar
::$datastore['permission']["$groupid"] AS $productid => $bit)
525 if ($bit & bugdar
::$permissions["$mask"])
527 $explicit["$productid"] = $productid;
528 $onbits["$productid"] = $productid;
532 // only unset if the bit was set in the first place by blanket and not product-specific permissions
533 // if it was set by product permissions then the highest level takes precedence
534 if ($onbits["$productid"] AND !isset($explicit["$productid"]))
536 unset($onbits["$productid"]);
543 // SQL queries would become very unhappy if we didn't do this
544 if (sizeof($onbits) < 1)
549 return implode(',', $onbits);
553 * Pre-parse hook for BSTemplate class. This merely substitutes help links
555 * @param string Template
558 function isso_pre_parse_hook($template)
560 $template = preg_replace('#\$help\[(.*)\]#', '<?php echo fetch_help_link("\1") ?>', $template);
565 * Returns the HTML used to generate a help link for a given topic
567 * @param string Topic name
570 function fetch_help_link($topic)
572 $tpl = new BSTemplate('help_link');
573 $tpl->vars
= array('topic' => $topic);
575 if (isset(bugdar
::$datastore['help']["$topic"]))
577 return $tpl->evaluate()->getTemplate();
581 if (BSApp
::get_debug())
583 return "[[INVALID TOPIC: $topic]]";
588 return $tpl->evaluate()->getTemplate();
593 // ###################################################################
595 * Returns a user array of information that is specific to all visiting
596 * users (guests). This can then be passed to any function that requires
601 * @return array User information array
603 function fetch_guest_user()
607 'groupids' => array(),
612 'permissions' => bugdar
::$datastore['usergroup'][1]['permissions'],
613 'displaytitle' => bugdar
::$datastore['usergroup'][1]['displaytitle'],
614 'timezone' => bugdar
::$options['defaulttimezone']
618 // ###################################################################
620 * Does an exhaustive permissions check on the bug. It checks for hidden
621 * bug status and ability to view hidden bugs. This normally was done
622 * at the top of each page, but it got so big, it was moved to a function.
626 * @param array Bug array
627 * @param array Alternate user array
629 * @return bool Does the user have permission
631 function check_bug_permissions($bug, $userinfo = null
)
633 if ($userinfo == null
)
635 $userinfo = bugdar
::$userinfo;
638 BSApp
::debug("checking permissions for $userinfo[userid] on bug $bug[bugid]");
640 BSApp
::debug('*** START VERBOSE CHECK ***');
642 BSApp
::debug('* !can_perform(canviewbugs, $bug[product], $userinfo) = ' . (int)(!can_perform('canviewbugs', $bug['product'], $userinfo)));
643 BSApp
::debug('* $bug[hidden] = ' . (int)$bug['hidden']);
644 BSApp
::debug('* $userinfo[userid] (' . $userinfo['userid'] . ') == $bug[userid] (' . $bug['userid'] . ') = ' . (int)($userinfo['userid'] == $bug['userid']));
645 BSApp
::debug('* can_perform(canviewownhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewownhidden', $bug['product'], $userinfo)));
646 BSApp
::debug('* can_perform(canviewhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewhidden', $bug['product'], $userinfo)));
647 BSApp
::debug('* !$bug[hidden] = ' . (int)(!$bug['hidden']));
649 BSApp
::debug('*** END PERMISSIONS CHECK ***');
653 !can_perform('canviewbugs', $bug['product'], $userinfo)
660 ($userinfo['userid'] == $bug['userid'] AND can_perform('canviewownhidden', $bug['product'], $userinfo))
662 can_perform('canviewhidden', $bug['product'], $userinfo)
670 BSApp
::debug('*** DONE WITH REAL CALLS ***');
674 BSApp
::debug('*** DONE WITH REAL CALLS ***');
679 // ###################################################################
681 * Takes an array of bug information and returns another array with
682 * information that is suitable for display as all the IDs have been
683 * replaced by their string equivalents
685 * @param array Unprocessed bug data
686 * @param string Color to display if the user has opted to not show status colours
688 * @param array Bug array with data fit for display
690 function ProcessBugDataForDisplay($bug, $color = '')
692 $bug['hiddendisplay'] = ($bug['hidden'] AND (can_perform('canviewhidden', $bug['product']) OR (can_perform('canviewownhidden') AND $bug['userid'] == bugdar
::$userinfo['userid'])));
694 $bug['bgcolor'] = (bugdar
::$userinfo['showcolors'] ? bugdar
::$datastore['status']["$bug[status]"]['color'] : $color);
695 $bug['product'] = bugdar
::$datastore['product']["$bug[product]"]['title'];
696 $bug['version'] = bugdar
::$datastore['version']["$bug[version]"]['version'];
697 $bug['component'] = bugdar
::$datastore['component']["$bug[component]"]['title'];
698 $bug['status'] = bugdar
::$datastore['status']["$bug[status]"]['status'];
699 $bug['resolution'] = bugdar
::$datastore['resolution']["$bug[resolution]"]['resolution'];
700 $bug['priority'] = bugdar
::$datastore['priority']["$bug[priority]"]['priority'];
701 $bug['severity'] = bugdar
::$datastore['severity']["$bug[severity]"]['severity'];
702 $bug['assignedto'] = ((empty($bug['assignedto']) OR !isset(bugdar
::$datastore['assignto']["$bug[assignedto]"])) ?
'' : construct_user_display(bugdar
::$datastore['assignto']["$bug[assignedto]"]));
704 $bug['lastposttime'] = ($bug['hiddendisplay'] ?
$bug['hiddenlastposttime'] : $bug['lastposttime']);
705 $bug['lastpost'] = ($bug['hiddendisplay'] ?
$bug['hiddenlastpostbyname'] : $bug['lastpostbyname']);
707 $bug['lastposttime'] = BSApp
::$date->format(bugdar
::$options['dateformat'], $bug['lastposttime']);
712 // ###################################################################
714 * Determines the correct permissions of the user. This is especially
715 * important for working with multiple-usergroup permission schemes.
716 * If a user is assigned to more than one usergroup, the highest level
717 * will always override (so a YES will always override a NO); this is
718 * because permissions are calculated with bitwise OR.
720 * @param array The user array with usergroups already exploded
722 * @return integer Permissions value
724 function FetchUserPermissions(&$user)
726 $perms = (int)bugdar
::$datastore['usergroup']["$user[usergroupid]"]['permissions'];
727 if (!is_array($user['groupids']))
729 $user['groupids'] = explode(',', bugdar
::$userinfo['groupids']);
731 $user['groupids'] = BSFunctions
::array_strip_empty($user['groupids']);
733 foreach ($user['groupids'] AS $group)
735 $perms |
= (int)bugdar
::$datastore['usergroup']["$group"]['permissions'];
741 // ###################################################################
743 * Fetches the path for an email template, given the name of the
744 * template and the locale to use
746 * @param string The template name
747 * @param string Language locale code
749 * @return string Template path
751 function FetchEmailPath($name, $locale)
753 return '../locale/' . $locale . '/emails/' . $name;