]>
src.bluestatic.org Git - bugdar.git/blob - functions.php
38f494289be961b311d4250c862bb081ad3186c4
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>" ;
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' ] = T ( 'Guest' );
71 $userinfo [ 'showemail' ] = false ;
76 $tpl = new BSTemplate ( 'username_display' );
77 $tpl- > vars
= array ( 'userinfo' => $userinfo );
78 $username = $tpl- > evaluate ()-> getTemplate ();
82 if ( $userinfo [ 'showemail' ])
84 $username = sprintf ( T ( '%1 $s <%2 $s >' ), $userinfo [ 'displayname' ], $userinfo [ 'email' ]);
88 $username = $userinfo [ 'displayname' ];
95 // ######################## Start can_perform ########################
96 // short-hand for bitwise &
97 function can_perform ( $bitmask , $productid = 0 , $userinfo = null )
101 // masks that aren't product-specific
102 static $inspecific = array (
114 if ( $userinfo == null )
116 $userinfo =& bugdar
:: $userinfo ;
119 $permissions =& bugdar
:: $datastore [ 'permission' ];
121 if (! isset ( $bugsys- > permissions
[ " $bitmask" ]))
123 trigger_error('Invalid bitmask " ' . $bitmask . ' " specified for can_perform() [includes/functions.php]', E_USER_WARNING);
126 if (! $userinfo ['permissions'])
128 $userinfo ['permissions'] = FetchUserPermissions( $userinfo );
131 if ( $productid AND !in_array( $bitmask , $inspecific ))
133 $verdict = (isset( $permissions [" $userinfo [ usergroupid
] "][" $productid" ]) ? ( $permissions [ " $userinfo [usergroupid]" ][ " $productid" ] & $bugsys- >permissions[" $bitmask" ]) : ( $userinfo [ 'permissions' ] & $bugsys- > permissions
[ " $bitmask" ]));
135 foreach ( $userinfo ['groupids'] AS $group )
137 if (isset( $permissions [" $group" ][ " $productid" ]))
139 $verdict |= ( $permissions [" $group" ][ " $productid" ] & $bugsys- >permissions[" $bitmask" ]);
142 BSApp
:: debug ( "verdict* on can_perform( $bitmask , $productid , $userinfo [userid]) = $verdict" );
146 BSApp::debug(" verdict on
can_perform ( $bitmask , $productid , $userinfo [ userid
]) = " . ( $userinfo ['permissions'] & $bugsys- >permissions[" $bitmask" ]));
147 return ( $userinfo [ 'permissions' ] & $bugsys- > permissions
[ " $bitmask" ]);
150 // ###################################################################
152 * Runs through a given datastore item and creates a series of <select>
157 * @param string Datastore name
158 * @param string Array index for the label
159 * @param string Array index for the value
160 * @param mixed The selected value(s)
161 * @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
162 * @param bool Generate it using admin printers?
164 * @return string Unelss in admin mode, returns the constructed options
166 function construct_datastore_select( $datastore , $labelname , $valuename , $selectedvalue = 0, $includeblank = false, $adminmode = false)
177 if ( $includeblank === true OR $includeblank !== false)
179 $newval = ( $inclueblank === true ? '' : $includeblank );
182 $admin- >list_item('', '', ((! $selectedvalue OR (is_array( $selectedvalue ) AND in_array( $newval , $selectedvalue ))) ? true : false));
188 $selected = ((! $selectedvalue OR (is_array( $selectedvalue ) AND in_array( $newval , $selectedvalue ))) ? true : false);
189 eval(' $select .= " ' . $bugsys- >template->fetch(' selectoption
') . ' ";');
193 foreach (bugdar:: $datastore [" $datastore" ] AS $item )
195 $label = $item [ " $labelname" ];
196 $value = $item [" $valuename" ];
197 $selected = (( $value == $selectedvalue OR ( is_array ( $selectedvalue ) AND in_array ( $value , $selectedvalue ))) ? true : false );
201 $admin- > list_item ( $label , $value , $selected );
205 eval ( ' $select .= "' . $bugsys- > template
-> fetch ( 'selectoption' ) . '";' );
215 // ################## Start construct_custom_fields ##################
216 function construct_custom_fields ( $bug = array (), $ignore21mask = false , $nodefault = false , $searchMode = false )
220 if (! is_array ( $fields ))
223 $fields_fetch = BSApp
:: $db- > query ( "
224 SELECT bugfield.*, MAX(permission.mask) AS mask
225 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
226 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
227 ON (bugfield.fieldid = permission.fieldid)
228 WHERE (permission.mask = 2 OR permission.mask = 1)
229 AND permission.usergroupid IN (" . bugdar
:: $userinfo [ 'usergroupid' ] . ( sizeof ( bugdar
:: $userinfo [ 'groupids' ]) != 0 ? ',' . implode ( ',' , bugdar
:: $userinfo [ 'groupids' ]) : '' ) . ")
230 GROUP BY (bugfield.fieldid)
232 foreach ( $fields_fetch as $field )
234 $fields [ " $field [fieldid]" ] = $field ;
238 $fieldbits = array ();
240 foreach ( $fields AS $field )
244 $field [ 'defaultvalue' ] = '' ;
247 if (! is_null ( $bug [ "custom $field [fieldid]" ]))
249 BSApp
:: debug ( "not null: $field [fieldid]" );
250 $value = $bug [ "custom $field [fieldid]" ];
254 $value = $field [ 'defaultvalue' ];
257 if ( $ignore21mask AND $field [ 'mask' ] != 0 )
262 if ( $field [ 'mask' ] == 2 )
264 switch ( $field [ 'type' ])
267 $tpl = new BSTemplate ( 'bugfield_input_text' );
272 $tempfield = $tpl- > evaluate ()-> getTemplate ();
275 case 'input_checkbox' :
276 $tpl = new BSTemplate ( 'bugfield_input_checkbox' );
279 'searchMode' => $searchMode ,
280 'selected' => ( $value ? ' checked="checked"' : '' )
282 $tempfield = $tpl- > evaluate ()-> getTemplate ();
285 case 'select_single' :
286 $selects = unserialize ( $field [ 'selects' ]);
287 $value = trim ( $value );
289 $tpl = new BSTemplate ( 'bugfield_select_single_option' );
293 'selected' => ((! $field [ 'usedefault' ] && ! trim ( $value )) ? ' selected="selected"' : '' )
295 $options = $tpl- > evaluate ()-> getTemplate ();
297 foreach ( $selects as $id => $select )
299 $tpl = new BSTemplate ( 'bugfield_select_single_option' );
302 'select' => stripslashes ( trim ( $select )),
303 'selected' => (( $select == $value || ( $field [ 'usedefault' ] && $id == 0 )) ? ' selected="selected"' : '' )
305 $options .= $tpl- > evaluate ()-> getTemplate ();
308 $tpl = new BSTemplate ( 'bugfield_select_single' );
311 'options' => $options
313 $tempfield = $tpl- > evaluate ()-> getTemplate ();
319 BSApp
:: debug ( 'mask 1 processing' );
320 if ( is_null ( $bug [ "custom $field [fieldid]" ]))
322 BSApp
:: debug ( "is null: $field [fieldid]" );
323 if ( $field [ 'type' ] == 'select_single' )
325 if ( $field [ 'usedefault' ])
327 $temp = unserialize ( $field [ 'selects' ]);
328 $value = trim ( $temp [ 0 ]);
332 $value = $bug [ "custom $field [fieldid]" ];
337 $value = $field [ 'defaultvalue' ];
342 $value = $bug [ "custom $field [fieldid]" ];
345 if ( $field [ 'type' ] == 'input_checkbox' )
347 $value = ( $value ? 'True' : 'False' );
349 $field [ 'value' ] = $value ;
351 $tpl = new BSTemplate ( 'bugfield_static_text' );
352 $tpl- > vars
= array ( 'field' => $field );
353 $tempfield = $tpl- > evaluate ()-> getTemplate ();
355 $fieldbits [] = $tempfield ;
361 // ###################################################################
363 * This takes the bug API object and input data and then sanitizes, verifies,
364 * and processes the data for custom fields. If there are any errors,
365 * they are passed to the message reporter.
367 * @param object A BugAPI object
368 * @param object MessageReporter object
369 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
370 * @param bool Search mode: don't change certain fields when they're 0 or empty
372 * @return mixed NULL if an ID is passed, string if bugid is NULL
374 function process_custom_fields (& $bugapi , & $msg , $errorbox = false , $searchMode = false )
380 $inputdata =& $bugsys- > in
;
383 $fields = $bugsys- > db
-> query ( "
384 SELECT bugfield.*, MAX(permission.mask) AS mask
385 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
386 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
387 ON (bugfield.fieldid = permission.fieldid)
388 WHERE permission.mask = 2
389 AND permission.usergroupid IN (" . bugdar
:: $userinfo [ 'usergroupid' ] . ( sizeof ( bugdar
:: $userinfo [ 'groupids' ]) != 0 ? ',' . implode ( ',' , bugdar
:: $userinfo [ 'groupids' ]) : '' ) . ")
390 GROUP BY (bugfield.fieldid)
392 foreach ( $fields as $field )
394 $fieldname = "custom $field [fieldid]" ;
396 if ( $field [ 'type' ] == 'input_checkbox' )
398 if ( $searchMode AND intval ( $inputdata [ " $fieldname" ]) == 0)
402 $bugapi- >set( $fieldname , intval( $inputdata [" $fieldname" ]));
405 else if ( $field [ 'type' ] == 'select_single' )
407 $temp = unserialize ( $field [ 'selects' ]);
408 $inputdata [ $fieldname ] = $temp [ intval ( $inputdata [ " $fieldname" ])] . ''; // make it a string so isset() doesn't catch
411 // field data wasn't passed, so skip it
412 if (!isset( $inputdata [" $fieldname" ]))
417 if ( $field [ 'required' ] AND empty ( $inputdata [ " $fieldname" ]) AND ! $searchMode )
419 $errorlist [] = sprintf(T('The field " %
1 $s" is a required
. '), $field [' name
']);
423 if (!empty( $field [' regexmatch
']))
425 if (!preg_match(' #' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["$fieldname"]))
427 $errorlist [] = sprintf ( T ( '%1 $s does not match the specified format' ), $field [ 'name' ]);
432 if ( isset ( $inputdata [ " $fieldname" ]))
434 if ( $field ['type'] == 'input_text')
436 if (empty( $inputdata [" $fieldname" ]) AND $searchMode )
440 $bugapi- > set ( $fieldname , $inputdata [ " $fieldname" ]);
444 if (empty( $inputdata [" $fieldname" ]))
448 $bugapi- > set ( $fieldname , '' );
453 $bugapi- > set ( $fieldname , trim ( $inputdata [ " $fieldname" ]));
462 foreach ( $errorlist AS $err )
464 $msg- >addError( $err );
469 $msg- >error( $errorlist [0]);
474 // ####################### Start fetch_on_bits #######################
475 function fetch_on_bits( $mask , $userinfo = null)
479 if ( $userinfo == null)
481 $userinfo =& bugdar:: $userinfo ;
486 $usergroupid = $userinfo ['usergroupid'];
487 FetchUserPermissions( $userinfo ); // get the groups
488 $groups = $userinfo ['groupids'];
489 $groups [] = $usergroupid ;
491 // product-inspecific work
492 if (is_array(bugdar:: $datastore ['product']))
494 foreach ( $groups AS $groupid )
496 // we only need to do this so long as there's no onbits array because this isn't product specific
497 if (sizeof( $onbits ) == 0 AND bugdar:: $datastore ['usergroup'][" $groupid" ][ 'permissions' ] & $bugsys- > permissions
[ " $mask" ])
499 foreach (bugdar:: $datastore ['product'] AS $id => $product )
501 $onbits [" $id" ] = $id ;
507 // bits set explicitly by products
510 // product specific work
511 foreach ( $groups AS $groupid )
513 if ( is_array ( bugdar
:: $datastore [ 'permission' ][ " $groupid" ]))
515 foreach (bugdar:: $datastore ['permission'][" $groupid" ] AS $productid => $bit )
517 if ( $bit & $bugsys- > permissions
[ " $mask" ])
519 $explicit [" $productid" ] = $productid ;
520 $onbits [ " $productid" ] = $productid ;
524 // only unset if the bit was set in the first place by blanket and not product-specific permissions
525 // if it was set by product permissions then the highest level takes precedence
526 if ( $onbits [" $productid" ] AND ! isset ( $explicit [ " $productid" ]))
528 unset( $onbits [" $productid" ]);
535 // SQL queries would become very unhappy if we didn't do this
536 if ( sizeof ( $onbits ) < 1 )
541 return implode ( ',' , $onbits );
545 * Pre-parse hook for BSTemplate class. This merely substitutes help links
547 * @param string Template
550 function isso_pre_parse_hook ( $template )
552 $template = preg_replace ( '# \$ help\[(.*)\]#' , '<?php echo fetch_help_link("\1") ?>' , $template );
557 * Returns the HTML used to generate a help link for a given topic
559 * @param string Topic name
562 function fetch_help_link ( $topic )
564 $tpl = new BSTemplate ( 'help_link' );
565 $tpl- > vars
= array ( 'topic' => $topic );
567 if ( isset ( bugdar
:: $datastore [ 'help' ][ " $topic" ]))
569 return $tpl- >evaluate()->getTemplate();
573 if (BSApp::get_debug())
575 return " [[ INVALID TOPIC
: $topic ]] ";
580 return $tpl- >evaluate()->getTemplate();
585 // ###################################################################
587 * Returns a user array of information that is specific to all visiting
588 * users (guests). This can then be passed to any function that requires
593 * @return array User information array
595 function fetch_guest_user()
601 'groupids' => array(),
606 'permissions' => bugdar:: $datastore ['usergroup'][1]['permissions'],
607 'displaytitle' => bugdar:: $datastore ['usergroup'][1]['displaytitle'],
608 'timezone' => bugdar:: $options ['defaulttimezone']
612 // ###################################################################
614 * Does an exhaustive permissions check on the bug. It checks for hidden
615 * bug status and ability to view hidden bugs. This normally was done
616 * at the top of each page, but it got so big, it was moved to a function.
620 * @param array Bug array
621 * @param array Alternate user array
623 * @return bool Does the user have permission
625 function check_bug_permissions( $bug , $userinfo = null)
628 if ( $userinfo == null)
630 $userinfo = bugdar:: $userinfo ;
633 BSApp::debug(" checking permissions
for $userinfo [ userid
] on bug
$bug [ bugid
] ");
635 BSApp::debug('*** START VERBOSE CHECK ***');
637 BSApp::debug('* !can_perform(canviewbugs, $bug [product], $userinfo ) = ' . (int)(!can_perform('canviewbugs', $bug ['product'], $userinfo )));
638 BSApp::debug('* $bug [hidden] = ' . (int) $bug ['hidden']);
639 BSApp::debug('* $userinfo [userid] (' . $userinfo ['userid'] . ') == $bug [userid] (' . $bug ['userid'] . ') = ' . (int)( $userinfo ['userid'] == $bug ['userid']));
640 BSApp::debug('* can_perform(canviewownhidden, $bug [product], $userinfo ) = ' . (int)(!!can_perform('canviewownhidden', $bug ['product'], $userinfo )));
641 BSApp::debug('* can_perform(canviewhidden, $bug [product], $userinfo ) = ' . (int)(!!can_perform('canviewhidden', $bug ['product'], $userinfo )));
642 BSApp::debug('* ! $bug [hidden] = ' . (int)(! $bug ['hidden']));
644 BSApp::debug('*** END PERMISSIONS CHECK ***');
648 !can_perform('canviewbugs', $bug ['product'], $userinfo )
655 ( $userinfo ['userid'] == $bug ['userid'] AND can_perform('canviewownhidden', $bug ['product'], $userinfo ))
657 can_perform('canviewhidden', $bug ['product'], $userinfo )
665 BSApp::debug('*** DONE WITH REAL CALLS ***');
669 BSApp::debug('*** DONE WITH REAL CALLS ***');
674 // ###################################################################
676 * Takes an array of bug information and returns another array with
677 * information that is suitable for display as all the IDs have been
678 * replaced by their string equivalents
680 * @param array Unprocessed bug data
681 * @param string Color to display if the user has opted to not show status colours
683 * @param array Bug array with data fit for display
685 function ProcessBugDataForDisplay( $bug , $color = '')
689 $bug ['hiddendisplay'] = ( $bug ['hidden'] AND (can_perform('canviewhidden', $bug ['product']) OR (can_perform('canviewownhidden') AND $bug ['userid'] == bugdar:: $userinfo ['userid'])));
691 $bug ['bgcolor'] = (bugdar:: $userinfo ['showcolors'] ? bugdar:: $datastore ['status'][" $bug [ status
] "]['color'] : $color );
692 $bug ['product'] = bugdar:: $datastore ['product'][" $bug [ product
] "]['title'];
693 $bug ['version'] = bugdar:: $datastore ['version'][" $bug [ version
] "]['version'];
694 $bug ['component'] = bugdar:: $datastore ['component'][" $bug [ component
] "]['title'];
695 $bug ['status'] = bugdar:: $datastore ['status'][" $bug [ status
] "]['status'];
696 $bug ['resolution'] = bugdar:: $datastore ['resolution'][" $bug [ resolution
] "]['resolution'];
697 $bug ['priority'] = bugdar:: $datastore ['priority'][" $bug [ priority
] "]['priority'];
698 $bug ['severity'] = bugdar:: $datastore ['severity'][" $bug [ severity
] "]['severity'];
699 $bug ['assignedto'] = ((empty( $bug ['assignedto']) OR !isset(bugdar:: $datastore ['assignto'][" $bug [ assignedto
] "])) ? '' : construct_user_display(bugdar:: $datastore ['assignto'][" $bug [ assignedto
] "]));
701 $bug ['lastposttime'] = ( $bug ['hiddendisplay'] ? $bug ['hiddenlastposttime'] : $bug ['lastposttime']);
702 $bug ['lastpost'] = ( $bug ['hiddendisplay'] ? $bug ['hiddenlastpostbyname'] : $bug ['lastpostbyname']);
704 $bug ['lastposttime'] = BSApp:: $date- >format(bugdar:: $options ['dateformat'], $bug ['lastposttime']);
709 // ###################################################################
711 * Determines the correct permissions of the user. This is especially
712 * important for working with multiple-usergroup permission schemes.
713 * If a user is assigned to more than one usergroup, the highest level
714 * will always override (so a YES will always override a NO); this is
715 * because permissions are calculated with bitwise OR.
717 * @param array The user array with usergroups already exploded
719 * @return integer Permissions value
721 function FetchUserPermissions(& $user )
725 $perms = (int)bugdar:: $datastore ['usergroup'][" $user [ usergroupid
] "]['permissions'];
726 if (!is_array( $user ['groupids']))
728 $user ['groupids'] = explode(',', bugdar:: $userinfo ['groupids']);
730 $user ['groupids'] = BSFunctions::array_strip_empty( $user ['groupids']);
732 foreach ( $user ['groupids'] AS $group )
734 $perms |= (int)bugdar:: $datastore ['usergroup'][" $group" ][ 'permissions' ];
740 // ###################################################################
742 * Fetches the path for an email template, given the name of the
743 * template and the locale to use
745 * @param string The template name
746 * @param string Language locale code
748 * @return string Template path
750 function FetchEmailPath ( $name , $locale )
752 return '../locale/' . $locale . '/emails/' . $name ;
755 /*=====================================================================*\
756 || ###################################################################
759 || ###################################################################
760 \*=====================================================================*/