r972: Rebranding from Iris Studios to Blue Static
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 // ###################################################################
23 /**
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.
26 *
27 * @access public
28 *
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
36 *
37 * @return string Constructed HTML output
38 */
39 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false, $multiple = false)
40 {
41 global $bugsys;
42
43 if ($multiple)
44 {
45 $selected = explode(',', $selected);
46 }
47
48 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
49 if ($includenil !== false)
50 {
51 $opts[] = '<option value="' . $includenil . '"' . ((!$selected OR (is_array($selected) AND in_array($includenil, $selected))) ? ' selected="selected"' : '') . '> ---------</option>';
52 }
53 foreach ($array AS $value => $label)
54 {
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>';
58 }
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>";
60 }
61
62 // ################### Start construct_user_display ##################
63 // $userinfo needs userid, email, displayname, and showemail
64 function construct_user_display($userinfo, $html = true)
65 {
66 global $bugsys;
67
68 if (!$userinfo['userid'])
69 {
70 $userinfo['displayname'] = $bugsys->lang->string('Guest');
71 $userinfo['showemail'] = false;
72 }
73
74 if ($html)
75 {
76 eval('$username = "' . $bugsys->template->fetch('username_display') . '";');
77 }
78 else
79 {
80 if ($userinfo['showemail'])
81 {
82 $username = sprintf($bugsys->lang->string('%1$s &lt;%2$s&gt;'), $userinfo['displayname'], $userinfo['email']);
83 }
84 else
85 {
86 $username = $userinfo['displayname'];
87 }
88 }
89
90 return $username;
91 }
92
93 // ######################## Start can_perform ########################
94 // short-hand for bitwise &
95 function can_perform($bitmask, $productid = 0, $userinfo = null)
96 {
97 global $bugsys;
98
99 if ($userinfo == null)
100 {
101 $userinfo =& $bugsys->userinfo;
102 }
103
104 if (!isset($bugsys->permissions["$bitmask"]))
105 {
106 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
107 }
108
109 if ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
110 {
111 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
112
113 if (!in_array($bitmask, $inspecific))
114 {
115 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]);
116 }
117 }
118
119 return ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]);
120 }
121
122 // ###################################################################
123 /**
124 * Runs through a given datastore item and creates a series of <select>
125 * options.
126 *
127 * @access public
128 *
129 * @param string Datastore name
130 * @param string Array index for the label
131 * @param string Array index for the value
132 * @param mixed The selected value(s)
133 * @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
134 * @param bool Generate it using admin printers?
135 *
136 * @return string Unelss in admin mode, returns the constructed options
137 */
138 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
139 {
140 global $bugsys;
141
142 if ($adminmode)
143 {
144 global $admin;
145 }
146
147 $select = '';
148
149 if ($includeblank === true OR $includeblank !== false)
150 {
151 $newval = ($inclueblank === true ? '' : $includeblank);
152 if ($adminmode)
153 {
154 $admin->list_item('', '', ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false));
155 }
156 else
157 {
158 $label = '';
159 $value = $newval;
160 $selected = ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false);
161 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
162 }
163 }
164
165 foreach ($bugsys->datastore["$datastore"] AS $item)
166 {
167 $label = $item["$labelname"];
168 $value = $item["$valuename"];
169 $selected = (($value == $selectedvalue OR (is_array($selectedvalue) AND in_array($value, $selectedvalue))) ? true : false);
170
171 if ($adminmode)
172 {
173 $admin->list_item($label, $value, $selected);
174 }
175 else
176 {
177 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
178 }
179 }
180
181 if (!$adminmode)
182 {
183 return $select;
184 }
185 }
186
187 // ################## Start construct_custom_fields ##################
188 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false)
189 {
190 global $bugsys;
191 static $fields;
192
193 if (!is_array($fields))
194 {
195 $fields = array();
196 $fields_fetch = $bugsys->db->query("
197 SELECT bugfield.*, permission.mask
198 FROM " . TABLE_PREFIX . "bugfield AS bugfield
199 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
200 ON (bugfield.fieldid = permission.fieldid)
201 WHERE (permission.mask = 2 OR permission.mask = 1)
202 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
203 );
204 while ($field = $bugsys->db->fetch_array($fields_fetch))
205 {
206 $fields["$field[fieldid]"] = $field;
207 }
208 }
209
210 $fieldvalues = $bugsys->db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = " . $bugsys->clean($bug['bugid'], TYPE_UINT));
211
212 $fieldbits = array();
213
214 foreach ($fields AS $field)
215 {
216 if ($nodefault)
217 {
218 $field['defaultvalue'] = '';
219 }
220
221 if (!is_null($bug["field$field[fieldid]"]))
222 {
223 $bugsys->debug("not null: $field[fieldid]");
224 $value = $bug["field$field[fieldid]"];
225 }
226 else
227 {
228 $value = $field['defaultvalue'];
229 }
230
231 if ($ignore21mask AND $field['mask'] != 0)
232 {
233 $field['mask'] = 2;
234 }
235
236 if ($field['mask'] == 2)
237 {
238 switch ($field['type'])
239 {
240 case 'input_text':
241 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
242 break;
243
244 case 'input_checkbox':
245 $selected = (($value) ? ' checked="checked"' : '');
246 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
247 break;
248
249 case 'select_single':
250 $selects = unserialize($field['selects']);
251 $value = trim($value);
252
253 $options = '';
254
255 $id = -1;
256 $select = '';
257 if (!$field['usedefault'] AND !trim($value))
258 {
259 $selected = ' selected="selected"';
260 }
261 else
262 {
263 $selected = '';
264 }
265 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
266
267 foreach ($selects AS $id => $select)
268 {
269 $selected = '';
270 $select = stripslashes(trim($select));
271 if ($select == $value)
272 {
273 $selected = ' selected="selected"';
274 }
275 else if ($field['usedefault'] AND $id == 0)
276 {
277 $selected = ' selected="selected"';
278 }
279 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
280 }
281 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
282 break;
283 }
284 }
285 else
286 {
287 $bugsys->debug('mask 1 processing');
288 if (is_null($fieldvalues["field$field[fieldid]"]))
289 {
290 $bugsys->debug("is null: $field[fieldid]");
291 if ($field['type'] == 'select_single')
292 {
293 if ($field['usedefault'])
294 {
295 $temp = unserialize($field['selects']);
296 $value = trim($temp[0]);
297 }
298 else
299 {
300 $value = $fieldvalues["field$field[fieldid]"];
301 }
302 }
303 else
304 {
305 $value = $field['defaultvalue'];
306 }
307 }
308 else
309 {
310 $value = $fieldvalues["field$field[fieldid]"];
311 }
312
313 if ($field['type'] == 'input_checkbox')
314 {
315 $value = (($value) ? 'True' : 'False');
316 }
317 $field['value'] = $value;
318 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
319 }
320 $fieldbits[] = $tempfield;
321 }
322
323 return $fieldbits;
324 }
325
326 // ################### Start process_custom_fields ###################
327 function process_custom_fields($bugid, $inputdata = array())
328 {
329 global $bugsys;
330
331 if (!$inputdata)
332 {
333 $inputdata =& $bugsys->in;
334 }
335
336 $fields = $bugsys->db->query("
337 SELECT bugfield.*
338 FROM " . TABLE_PREFIX . "bugfield AS bugfield
339 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
340 ON (bugfield.fieldid = permission.fieldid)
341 WHERE permission.mask = 2
342 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
343 );
344 while ($field = $bugsys->db->fetch_array($fields))
345 {
346 if ($field['type'] == 'input_checkbox')
347 {
348 $fieldbuild[] = 'field' . $field['fieldid'];
349 if (isset($inputdata["field$field[fieldid]"]))
350 {
351 $fieldvalue[] = 1;
352 }
353 else
354 {
355 $fieldvalue[] = 0;
356 }
357 continue;
358 }
359
360 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
361 {
362 $errorlist[] = sprintf($bugsys->lang->string('The "%1$s" field is a required field.'), $field['name']);
363 continue;
364 }
365
366 if (isset($inputdata["field$field[fieldid]"]))
367 {
368 $fieldbuild[] = 'field' . $field['fieldid'];
369
370 if ($field['type'] == 'input_text')
371 {
372 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
373 }
374 else
375 {
376 if ($inputdata["field$field[fieldid]"] == -1)
377 {
378 $fieldvalue[] = "''";
379 continue;
380 }
381
382 $temp = unserialize($field['selects']);
383 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
384 }
385 }
386 }
387
388 if ($errorlist)
389 {
390 return $errorlist;
391 }
392
393 if (sizeof($fieldbuild) < 1)
394 {
395 return;
396 }
397
398 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
399 }
400
401 // ####################### Start fetch_on_bits #######################
402 function fetch_on_bits($mask, $userinfo = null)
403 {
404 global $bugsys;
405
406 if ($userinfo == null)
407 {
408 $userinfo =& $bugsys->userinfo;
409 }
410
411 $onbits = array();
412
413 $usergroupid = $userinfo['usergroupid'];
414
415 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $bugsys->permissions["$mask"])
416 {
417 foreach ($bugsys->datastore['product'] AS $id => $product)
418 {
419 if (!$product['componentmother'])
420 {
421 $onbits["$id"] = $id;
422 }
423 }
424 }
425
426 if (is_array($bugsys->datastore['permission']["$usergroupid"]))
427 {
428 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
429 {
430 if ($bit & $bugsys->permissions["$mask"])
431 {
432 $onbits["$productid"] = $productid;
433 }
434 else
435 {
436 if ($onbits["$productid"])
437 {
438 unset($onbits["$productid"]);
439 }
440 }
441 }
442 }
443
444 if (sizeof($onbits) < 1)
445 {
446 $onbits = array(0);
447 }
448
449 return implode(',', $onbits);
450 }
451
452 // #################### Start isso_pre_parse_hook ####################
453 // the pre-parse hook for ISSO's template engine
454 function isso_pre_parse_hook($template)
455 {
456 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
457 return $template;
458 }
459
460 // ###################### Start fetch_help_link ######################
461 // returns a prepared link to insert into templates that opens up a
462 // help popup in the user-end
463 function fetch_help_link($topic)
464 {
465 global $bugsys;
466
467 if (isset($bugsys->datastore['help']["$topic"]))
468 {
469 eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
470 return $temp;
471 }
472 else
473 {
474 if ($bugsys->debug)
475 {
476 return "[[INVALID TOPIC: $topic]]";
477 }
478 // do we want this?
479 else if (null == 1)
480 {
481 return eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
482 }
483 }
484 }
485
486 // ###################################################################
487 /**
488 * Returns a user array of information that is specific to all visiting
489 * users (guests). This can then be passed to any function that requires
490 * user information.
491 *
492 * @access public
493 *
494 * @return array User information array
495 */
496 function fetch_guest_user()
497 {
498 global $bugsys;
499
500 return array(
501 'usergroupid' => 1,
502 'userid' => 0,
503 'email' => '',
504 'displayname' => '',
505 'showcolours' => 1,
506 'permissions' => $bugsys->datastore['usergroup'][1]['permissions'],
507 'displaytitle' => $bugsys->datastore['usergroup'][1]['displaytitle'],
508 );
509 }
510
511 /*=====================================================================*\
512 || ###################################################################
513 || # $HeadURL$
514 || # $Id$
515 || ###################################################################
516 \*=====================================================================*/
517 ?>