]>
src.bluestatic.org Git - bugdar.git/blob - includes/functions.php
2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright ©2002-2007 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 )
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>" ;
63 * Constructs the user information link
65 * @param array Userinfo array - requires userid, email, displayname, and showemail values
66 * @param bool Return HTML or just a string?
69 function construct_user_display ( $userinfo , $html = true )
71 if (! $userinfo [ 'userid' ])
73 $userinfo [ 'displayname' ] = T ( 'Guest' );
74 $userinfo [ 'showemail' ] = false ;
79 $tpl = new BSTemplate ( 'username_display' );
80 $tpl- > vars
= array ( 'userinfo' => $userinfo );
81 $username = $tpl- > evaluate ()-> getTemplate ();
85 if ( $userinfo [ 'showemail' ])
87 $username = sprintf ( T ( '%1 $s <%2 $s >' ), $userinfo [ 'displayname' ], $userinfo [ 'email' ]);
91 $username = $userinfo [ 'displayname' ];
98 // ######################## Start can_perform ########################
99 // short-hand for bitwise &
100 function can_perform ( $bitmask , $productid = 0 , $userinfo = null )
104 // masks that aren't product-specific
105 static $inspecific = array (
117 if ( $userinfo == null )
119 $userinfo =& bugdar
:: $userinfo ;
122 $permissions =& bugdar
:: $datastore [ 'permission' ];
124 if (! isset ( $bugsys- > permissions
[ " $bitmask" ]))
126 trigger_error('Invalid bitmask " ' . $bitmask . ' " specified for can_perform() [includes/functions.php]', E_USER_WARNING);
129 if (! $userinfo ['permissions'])
131 $userinfo ['permissions'] = FetchUserPermissions( $userinfo );
134 if ( $productid AND !in_array( $bitmask , $inspecific ))
136 $verdict = (isset( $permissions [" $userinfo [ usergroupid
] "][" $productid" ]) ? ( $permissions [ " $userinfo [usergroupid]" ][ " $productid" ] & $bugsys- >permissions[" $bitmask" ]) : ( $userinfo [ 'permissions' ] & $bugsys- > permissions
[ " $bitmask" ]));
138 foreach ( $userinfo ['groupids'] AS $group )
140 if (isset( $permissions [" $group" ][ " $productid" ]))
142 $verdict |= ( $permissions [" $group" ][ " $productid" ] & $bugsys- >permissions[" $bitmask" ]);
145 BSApp
:: debug ( "verdict* on can_perform( $bitmask , $productid , $userinfo [userid]) = $verdict" );
149 BSApp::debug(" verdict on
can_perform ( $bitmask , $productid , $userinfo [ userid
]) = " . ( $userinfo ['permissions'] & $bugsys- >permissions[" $bitmask" ]));
150 return ( $userinfo [ 'permissions' ] & $bugsys- > permissions
[ " $bitmask" ]);
153 // ###################################################################
155 * Runs through a given datastore item and creates a series of <select>
160 * @param string Datastore name
161 * @param string Array index for the label
162 * @param string Array index for the value
163 * @param mixed The selected value(s)
164 * @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
165 * @param bool Generate it using admin printers?
167 * @return string Unelss in admin mode, returns the constructed options
169 function construct_datastore_select( $datastore , $labelname , $valuename , $selectedvalue = 0, $includeblank = false, $adminmode = false)
180 if ( $includeblank === true OR $includeblank !== false)
182 $newval = ( $inclueblank === true ? '' : $includeblank );
185 $admin- >list_item('', '', ((! $selectedvalue OR (is_array( $selectedvalue ) AND in_array( $newval , $selectedvalue ))) ? true : false));
191 $selected = ((! $selectedvalue OR (is_array( $selectedvalue ) AND in_array( $newval , $selectedvalue ))) ? true : false);
192 eval(' $select .= " ' . $bugsys- >template->fetch(' selectoption
') . ' ";');
196 foreach (bugdar:: $datastore [" $datastore" ] AS $item )
198 $label = $item [ " $labelname" ];
199 $value = $item [" $valuename" ];
200 $selected = (( $value == $selectedvalue OR ( is_array ( $selectedvalue ) AND in_array ( $value , $selectedvalue ))) ? true : false );
204 $admin- > list_item ( $label , $value , $selected );
208 eval ( ' $select .= "' . $bugsys- > template
-> fetch ( 'selectoption' ) . '";' );
218 // ################## Start construct_custom_fields ##################
219 function construct_custom_fields ( $bug = array (), $ignore21mask = false , $nodefault = false , $searchMode = false )
223 if (! is_array ( $fields ))
226 $fields_fetch = BSApp
:: $db- > query ( "
227 SELECT bugfield.*, MAX(permission.mask) AS mask
228 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
229 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
230 ON (bugfield.fieldid = permission.fieldid)
231 WHERE (permission.mask = 2 OR permission.mask = 1)
232 AND permission.usergroupid IN (" . bugdar
:: $userinfo [ 'usergroupid' ] . ( sizeof ( bugdar
:: $userinfo [ 'groupids' ]) != 0 ? ',' . implode ( ',' , bugdar
:: $userinfo [ 'groupids' ]) : '' ) . ")
233 GROUP BY (bugfield.fieldid)
235 foreach ( $fields_fetch as $field )
237 $fields [ " $field [fieldid]" ] = $field ;
241 $fieldbits = array ();
243 foreach ( $fields AS $field )
247 $field [ 'defaultvalue' ] = '' ;
250 if (! is_null ( $bug [ "custom $field [fieldid]" ]))
252 BSApp
:: debug ( "not null: $field [fieldid]" );
253 $value = $bug [ "custom $field [fieldid]" ];
257 $value = $field [ 'defaultvalue' ];
260 if ( $ignore21mask AND $field [ 'mask' ] != 0 )
265 if ( $field [ 'mask' ] == 2 )
267 switch ( $field [ 'type' ])
270 $tpl = new BSTemplate ( 'bugfield_input_text' );
275 $tempfield = $tpl- > evaluate ()-> getTemplate ();
278 case 'input_checkbox' :
279 $tpl = new BSTemplate ( 'bugfield_input_checkbox' );
282 'searchMode' => $searchMode ,
283 'selected' => ( $value ? ' checked="checked"' : '' )
285 $tempfield = $tpl- > evaluate ()-> getTemplate ();
288 case 'select_single' :
289 $selects = unserialize ( $field [ 'selects' ]);
290 $value = trim ( $value );
292 $tpl = new BSTemplate ( 'bugfield_select_single_option' );
296 'selected' => ((! $field [ 'usedefault' ] && ! trim ( $value )) ? ' selected="selected"' : '' )
298 $options = $tpl- > evaluate ()-> getTemplate ();
300 foreach ( $selects as $id => $select )
302 $tpl = new BSTemplate ( 'bugfield_select_single_option' );
305 'select' => stripslashes ( trim ( $select )),
306 'selected' => (( $select == $value || ( $field [ 'usedefault' ] && $id == 0 )) ? ' selected="selected"' : '' )
308 $options .= $tpl- > evaluate ()-> getTemplate ();
311 $tpl = new BSTemplate ( 'bugfield_select_single' );
314 'options' => $options
316 $tempfield = $tpl- > evaluate ()-> getTemplate ();
322 BSApp
:: debug ( 'mask 1 processing' );
323 if ( is_null ( $bug [ "custom $field [fieldid]" ]))
325 BSApp
:: debug ( "is null: $field [fieldid]" );
326 if ( $field [ 'type' ] == 'select_single' )
328 if ( $field [ 'usedefault' ])
330 $temp = unserialize ( $field [ 'selects' ]);
331 $value = trim ( $temp [ 0 ]);
335 $value = $bug [ "custom $field [fieldid]" ];
340 $value = $field [ 'defaultvalue' ];
345 $value = $bug [ "custom $field [fieldid]" ];
348 if ( $field [ 'type' ] == 'input_checkbox' )
350 $value = ( $value ? 'True' : 'False' );
352 $field [ 'value' ] = $value ;
354 $tpl = new BSTemplate ( 'bugfield_static_text' );
355 $tpl- > vars
= array ( 'field' => $field );
356 $tempfield = $tpl- > evaluate ()-> getTemplate ();
358 $fieldbits [] = $tempfield ;
364 // ###################################################################
366 * This takes the bug API object and input data and then sanitizes, verifies,
367 * and processes the data for custom fields. If there are any errors,
368 * they are passed to the message reporter.
370 * @param object A BugAPI object
371 * @param object MessageReporter object
372 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
373 * @param bool Search mode: don't change certain fields when they're 0 or empty
375 * @return mixed NULL if an ID is passed, string if bugid is NULL
377 function process_custom_fields (& $bugapi , & $msg , $errorbox = false , $searchMode = false )
383 $inputdata =& $bugsys- > in
;
386 $fields = $bugsys- > db
-> query ( "
387 SELECT bugfield.*, MAX(permission.mask) AS mask
388 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
389 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
390 ON (bugfield.fieldid = permission.fieldid)
391 WHERE permission.mask = 2
392 AND permission.usergroupid IN (" . bugdar
:: $userinfo [ 'usergroupid' ] . ( sizeof ( bugdar
:: $userinfo [ 'groupids' ]) != 0 ? ',' . implode ( ',' , bugdar
:: $userinfo [ 'groupids' ]) : '' ) . ")
393 GROUP BY (bugfield.fieldid)
395 foreach ( $fields as $field )
397 $fieldname = "custom $field [fieldid]" ;
399 if ( $field [ 'type' ] == 'input_checkbox' )
401 if ( $searchMode AND intval ( $inputdata [ " $fieldname" ]) == 0)
405 $bugapi- >set( $fieldname , intval( $inputdata [" $fieldname" ]));
408 else if ( $field [ 'type' ] == 'select_single' )
410 $temp = unserialize ( $field [ 'selects' ]);
411 $inputdata [ $fieldname ] = $temp [ intval ( $inputdata [ " $fieldname" ])] . ''; // make it a string so isset() doesn't catch
414 // field data wasn't passed, so skip it
415 if (!isset( $inputdata [" $fieldname" ]))
420 if ( $field [ 'required' ] AND empty ( $inputdata [ " $fieldname" ]) AND ! $searchMode )
422 $errorlist [] = sprintf(T('The field " %
1 $s" is a required
. '), $field [' name
']);
426 if (!empty( $field [' regexmatch
']))
428 if (!preg_match(' #' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["$fieldname"]))
430 $errorlist [] = sprintf ( T ( '%1 $s does not match the specified format' ), $field [ 'name' ]);
435 if ( isset ( $inputdata [ " $fieldname" ]))
437 if ( $field ['type'] == 'input_text')
439 if (empty( $inputdata [" $fieldname" ]) AND $searchMode )
443 $bugapi- > set ( $fieldname , $inputdata [ " $fieldname" ]);
447 if (empty( $inputdata [" $fieldname" ]))
451 $bugapi- > set ( $fieldname , '' );
456 $bugapi- > set ( $fieldname , trim ( $inputdata [ " $fieldname" ]));
465 foreach ( $errorlist AS $err )
467 $msg- >addError( $err );
472 $msg- >error( $errorlist [0]);
477 // ####################### Start fetch_on_bits #######################
478 function fetch_on_bits( $mask , $userinfo = null)
482 if ( $userinfo == null)
484 $userinfo =& bugdar:: $userinfo ;
489 $usergroupid = $userinfo ['usergroupid'];
490 FetchUserPermissions( $userinfo ); // get the groups
491 $groups = $userinfo ['groupids'];
492 $groups [] = $usergroupid ;
494 // product-inspecific work
495 if (is_array(bugdar:: $datastore ['product']))
497 foreach ( $groups AS $groupid )
499 // we only need to do this so long as there's no onbits array because this isn't product specific
500 if (sizeof( $onbits ) == 0 AND bugdar:: $datastore ['usergroup'][" $groupid" ][ 'permissions' ] & $bugsys- > permissions
[ " $mask" ])
502 foreach (bugdar:: $datastore ['product'] AS $id => $product )
504 $onbits [" $id" ] = $id ;
510 // bits set explicitly by products
513 // product specific work
514 foreach ( $groups AS $groupid )
516 if ( is_array ( bugdar
:: $datastore [ 'permission' ][ " $groupid" ]))
518 foreach (bugdar:: $datastore ['permission'][" $groupid" ] AS $productid => $bit )
520 if ( $bit & $bugsys- > permissions
[ " $mask" ])
522 $explicit [" $productid" ] = $productid ;
523 $onbits [ " $productid" ] = $productid ;
527 // only unset if the bit was set in the first place by blanket and not product-specific permissions
528 // if it was set by product permissions then the highest level takes precedence
529 if ( $onbits [" $productid" ] AND ! isset ( $explicit [ " $productid" ]))
531 unset( $onbits [" $productid" ]);
538 // SQL queries would become very unhappy if we didn't do this
539 if ( sizeof ( $onbits ) < 1 )
544 return implode ( ',' , $onbits );
548 * Pre-parse hook for BSTemplate class. This merely substitutes help links
550 * @param string Template
553 function isso_pre_parse_hook ( $template )
555 $template = preg_replace ( '# \$ help\[(.*)\]#' , '<?php echo fetch_help_link("\1") ?>' , $template );
560 * Returns the HTML used to generate a help link for a given topic
562 * @param string Topic name
565 function fetch_help_link ( $topic )
567 $tpl = new BSTemplate ( 'help_link' );
568 $tpl- > vars
= array ( 'topic' => $topic );
570 if ( isset ( bugdar
:: $datastore [ 'help' ][ " $topic" ]))
572 return $tpl- >evaluate()->getTemplate();
576 if (BSApp::get_debug())
578 return " [[ INVALID TOPIC
: $topic ]] ";
583 return $tpl- >evaluate()->getTemplate();
588 // ###################################################################
590 * Returns a user array of information that is specific to all visiting
591 * users (guests). This can then be passed to any function that requires
596 * @return array User information array
598 function fetch_guest_user()
604 'groupids' => array(),
609 'permissions' => bugdar:: $datastore ['usergroup'][1]['permissions'],
610 'displaytitle' => bugdar:: $datastore ['usergroup'][1]['displaytitle'],
611 'timezone' => bugdar:: $options ['defaulttimezone']
615 // ###################################################################
617 * Does an exhaustive permissions check on the bug. It checks for hidden
618 * bug status and ability to view hidden bugs. This normally was done
619 * at the top of each page, but it got so big, it was moved to a function.
623 * @param array Bug array
624 * @param array Alternate user array
626 * @return bool Does the user have permission
628 function check_bug_permissions( $bug , $userinfo = null)
631 if ( $userinfo == null)
633 $userinfo = bugdar:: $userinfo ;
636 BSApp::debug(" checking permissions
for $userinfo [ userid
] on bug
$bug [ bugid
] ");
638 BSApp::debug('*** START VERBOSE CHECK ***');
640 BSApp::debug('* !can_perform(canviewbugs, $bug [product], $userinfo ) = ' . (int)(!can_perform('canviewbugs', $bug ['product'], $userinfo )));
641 BSApp::debug('* $bug [hidden] = ' . (int) $bug ['hidden']);
642 BSApp::debug('* $userinfo [userid] (' . $userinfo ['userid'] . ') == $bug [userid] (' . $bug ['userid'] . ') = ' . (int)( $userinfo ['userid'] == $bug ['userid']));
643 BSApp::debug('* can_perform(canviewownhidden, $bug [product], $userinfo ) = ' . (int)(!!can_perform('canviewownhidden', $bug ['product'], $userinfo )));
644 BSApp::debug('* can_perform(canviewhidden, $bug [product], $userinfo ) = ' . (int)(!!can_perform('canviewhidden', $bug ['product'], $userinfo )));
645 BSApp::debug('* ! $bug [hidden] = ' . (int)(! $bug ['hidden']));
647 BSApp::debug('*** END PERMISSIONS CHECK ***');
651 !can_perform('canviewbugs', $bug ['product'], $userinfo )
658 ( $userinfo ['userid'] == $bug ['userid'] AND can_perform('canviewownhidden', $bug ['product'], $userinfo ))
660 can_perform('canviewhidden', $bug ['product'], $userinfo )
668 BSApp::debug('*** DONE WITH REAL CALLS ***');
672 BSApp::debug('*** DONE WITH REAL CALLS ***');
677 // ###################################################################
679 * Takes an array of bug information and returns another array with
680 * information that is suitable for display as all the IDs have been
681 * replaced by their string equivalents
683 * @param array Unprocessed bug data
684 * @param string Color to display if the user has opted to not show status colours
686 * @param array Bug array with data fit for display
688 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 )
728 $perms = (int)bugdar:: $datastore ['usergroup'][" $user [ usergroupid
] "]['permissions'];
729 if (!is_array( $user ['groupids']))
731 $user ['groupids'] = explode(',', bugdar:: $userinfo ['groupids']);
733 $user ['groupids'] = BSFunctions::array_strip_empty( $user ['groupids']);
735 foreach ( $user ['groupids'] AS $group )
737 $perms |= (int)bugdar:: $datastore ['usergroup'][" $group" ][ 'permissions' ];
743 // ###################################################################
745 * Fetches the path for an email template, given the name of the
746 * template and the locale to use
748 * @param string The template name
749 * @param string Language locale code
751 * @return string Template path
753 function FetchEmailPath ( $name , $locale )
755 return '../locale/' . $locale . '/emails/' . $name ;
758 /*=====================================================================*\
759 || ###################################################################
762 || ###################################################################
763 \*=====================================================================*/