r1163: Actually do the regex match on process_custom_fields()
[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 (!$userinfo['permissions'])
110 {
111 $userinfo['permissions'] = (int)$bugsys->datastore['usergroup']["$userinfo[usergroupid]"]['permissions'];
112 }
113
114 if ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
115 {
116 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
117
118 if (!in_array($bitmask, $inspecific))
119 {
120 $bugsys->debug("verdict* on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]));
121 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]);
122 }
123 }
124
125 $bugsys->debug("verdict on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]));
126 return ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]);
127 }
128
129 // ###################################################################
130 /**
131 * Runs through a given datastore item and creates a series of <select>
132 * options.
133 *
134 * @access public
135 *
136 * @param string Datastore name
137 * @param string Array index for the label
138 * @param string Array index for the value
139 * @param mixed The selected value(s)
140 * @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
141 * @param bool Generate it using admin printers?
142 *
143 * @return string Unelss in admin mode, returns the constructed options
144 */
145 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
146 {
147 global $bugsys;
148
149 if ($adminmode)
150 {
151 global $admin;
152 }
153
154 $select = '';
155
156 if ($includeblank === true OR $includeblank !== false)
157 {
158 $newval = ($inclueblank === true ? '' : $includeblank);
159 if ($adminmode)
160 {
161 $admin->list_item('', '', ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false));
162 }
163 else
164 {
165 $label = '';
166 $value = $newval;
167 $selected = ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false);
168 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
169 }
170 }
171
172 foreach ($bugsys->datastore["$datastore"] AS $item)
173 {
174 $label = $item["$labelname"];
175 $value = $item["$valuename"];
176 $selected = (($value == $selectedvalue OR (is_array($selectedvalue) AND in_array($value, $selectedvalue))) ? true : false);
177
178 if ($adminmode)
179 {
180 $admin->list_item($label, $value, $selected);
181 }
182 else
183 {
184 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
185 }
186 }
187
188 if (!$adminmode)
189 {
190 return $select;
191 }
192 }
193
194 // ################## Start construct_custom_fields ##################
195 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false)
196 {
197 global $bugsys;
198 static $fields;
199
200 if (!is_array($fields))
201 {
202 $fields = array();
203 $fields_fetch = $bugsys->db->query("
204 SELECT bugfield.*, permission.mask
205 FROM " . TABLE_PREFIX . "bugfield AS bugfield
206 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
207 ON (bugfield.fieldid = permission.fieldid)
208 WHERE (permission.mask = 2 OR permission.mask = 1)
209 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
210 );
211 while ($field = $bugsys->db->fetch_array($fields_fetch))
212 {
213 $fields["$field[fieldid]"] = $field;
214 }
215 }
216
217 $fieldvalues = $bugsys->db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = " . $bugsys->clean($bug['bugid'], TYPE_UINT));
218
219 $fieldbits = array();
220
221 foreach ($fields AS $field)
222 {
223 if ($nodefault)
224 {
225 $field['defaultvalue'] = '';
226 }
227
228 if (!is_null($bug["field$field[fieldid]"]))
229 {
230 $bugsys->debug("not null: $field[fieldid]");
231 $value = $bug["field$field[fieldid]"];
232 }
233 else
234 {
235 $value = $field['defaultvalue'];
236 }
237
238 if ($ignore21mask AND $field['mask'] != 0)
239 {
240 $field['mask'] = 2;
241 }
242
243 if ($field['mask'] == 2)
244 {
245 switch ($field['type'])
246 {
247 case 'input_text':
248 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
249 break;
250
251 case 'input_checkbox':
252 $selected = (($value) ? ' checked="checked"' : '');
253 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
254 break;
255
256 case 'select_single':
257 $selects = unserialize($field['selects']);
258 $value = trim($value);
259
260 $options = '';
261
262 $id = -1;
263 $select = '';
264 if (!$field['usedefault'] AND !trim($value))
265 {
266 $selected = ' selected="selected"';
267 }
268 else
269 {
270 $selected = '';
271 }
272 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
273
274 foreach ($selects AS $id => $select)
275 {
276 $selected = '';
277 $select = stripslashes(trim($select));
278 if ($select == $value)
279 {
280 $selected = ' selected="selected"';
281 }
282 else if ($field['usedefault'] AND $id == 0)
283 {
284 $selected = ' selected="selected"';
285 }
286 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
287 }
288 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
289 break;
290 }
291 }
292 else
293 {
294 $bugsys->debug('mask 1 processing');
295 if (is_null($fieldvalues["field$field[fieldid]"]))
296 {
297 $bugsys->debug("is null: $field[fieldid]");
298 if ($field['type'] == 'select_single')
299 {
300 if ($field['usedefault'])
301 {
302 $temp = unserialize($field['selects']);
303 $value = trim($temp[0]);
304 }
305 else
306 {
307 $value = $fieldvalues["field$field[fieldid]"];
308 }
309 }
310 else
311 {
312 $value = $field['defaultvalue'];
313 }
314 }
315 else
316 {
317 $value = $fieldvalues["field$field[fieldid]"];
318 }
319
320 if ($field['type'] == 'input_checkbox')
321 {
322 $value = (($value) ? 'True' : 'False');
323 }
324 $field['value'] = $value;
325 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
326 }
327 $fieldbits[] = $tempfield;
328 }
329
330 return $fieldbits;
331 }
332
333 // ###################################################################
334 /**
335 * This takes the bug ID and input data and then sanitizes, verifies,
336 * and processes the data for custom fields. If there are any errors,
337 * they are passed to the message reporter.
338 *
339 * @param integer The bug ID; if NULL, then it returns a query that needs to have %1$s replaced with the bug ID, otherwise it executes the query itself
340 * @param object MessageReporter object
341 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
342 * @param array If you don't want to get the data from $bugsys->in[], then an optional input source
343 *
344 * @return mixed NULL if an ID is passed, string if bugid is NULL
345 */
346 function process_custom_fields($bugid, $msg, $errorbox = false, $inputdata = array())
347 {
348 global $bugsys;
349
350 if (!$inputdata)
351 {
352 $inputdata =& $bugsys->in;
353 }
354
355 $fields = $bugsys->db->query("
356 SELECT bugfield.*
357 FROM " . TABLE_PREFIX . "bugfield AS bugfield
358 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
359 ON (bugfield.fieldid = permission.fieldid)
360 WHERE permission.mask = 2
361 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
362 );
363 while ($field = $bugsys->db->fetch_array($fields))
364 {
365 if ($field['type'] == 'input_checkbox')
366 {
367 $fieldbuild[] = 'field' . $field['fieldid'];
368 if (isset($inputdata["field$field[fieldid]"]))
369 {
370 $fieldvalue[] = 1;
371 }
372 else
373 {
374 $fieldvalue[] = 0;
375 }
376 continue;
377 }
378
379 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
380 {
381 $errorlist[] = sprintf($bugsys->lang->string('The "%1$s" field is a required field.'), $field['name']);
382 continue;
383 }
384
385 if (!empty($field['regexmatch']))
386 {
387 if (!preg_match('#' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["field$field[fieldid]"]))
388 {
389 $errorlist[] = sprintf(_('%1$s does not match the specified format'), $field['name']);
390 continue;
391 }
392 }
393
394 if (isset($inputdata["field$field[fieldid]"]))
395 {
396 $fieldbuild[] = 'field' . $field['fieldid'];
397
398 if ($field['type'] == 'input_text')
399 {
400 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
401 }
402 else
403 {
404 if ($inputdata["field$field[fieldid]"] == -1)
405 {
406 $fieldvalue[] = "''";
407 continue;
408 }
409
410 $temp = unserialize($field['selects']);
411 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
412 }
413 }
414 }
415
416 if ($errorlist)
417 {
418 if ($errorbox)
419 {
420 foreach ($errorlist AS $err)
421 {
422 $msg->add_error($err);
423 }
424 }
425 else
426 {
427 $msg->error($errorlist[0]);
428 }
429 }
430
431 if (sizeof($fieldbuild) < 1)
432 {
433 return;
434 }
435
436 $query = "REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES (%1$s, " . implode(', ', $fieldvalue) . ")";
437 if ($bugid === null)
438 {
439 return $query;
440 }
441
442 $bugsys->db->query(sprintf($query, $bugid));
443 }
444
445 // ####################### Start fetch_on_bits #######################
446 function fetch_on_bits($mask, $userinfo = null)
447 {
448 global $bugsys;
449
450 if ($userinfo == null)
451 {
452 $userinfo =& $bugsys->userinfo;
453 }
454
455 $onbits = array();
456
457 $usergroupid = $userinfo['usergroupid'];
458
459 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $bugsys->permissions["$mask"] AND is_array($bugsys->datastore['product']))
460 {
461 foreach ($bugsys->datastore['product'] AS $id => $product)
462 {
463 if (!$product['componentmother'])
464 {
465 $onbits["$id"] = $id;
466 }
467 }
468 }
469
470 if (is_array($bugsys->datastore['permission']["$usergroupid"]))
471 {
472 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
473 {
474 if ($bit & $bugsys->permissions["$mask"])
475 {
476 $onbits["$productid"] = $productid;
477 }
478 else
479 {
480 if ($onbits["$productid"])
481 {
482 unset($onbits["$productid"]);
483 }
484 }
485 }
486 }
487
488 if (sizeof($onbits) < 1)
489 {
490 $onbits = array(0);
491 }
492
493 return implode(',', $onbits);
494 }
495
496 // #################### Start isso_pre_parse_hook ####################
497 // the pre-parse hook for ISSO's template engine
498 function isso_pre_parse_hook($template)
499 {
500 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
501 return $template;
502 }
503
504 // ###################### Start fetch_help_link ######################
505 // returns a prepared link to insert into templates that opens up a
506 // help popup in the user-end
507 function fetch_help_link($topic)
508 {
509 global $bugsys;
510
511 if (isset($bugsys->datastore['help']["$topic"]))
512 {
513 eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
514 return $temp;
515 }
516 else
517 {
518 if ($bugsys->debug)
519 {
520 return "[[INVALID TOPIC: $topic]]";
521 }
522 // do we want this?
523 else if (null == 1)
524 {
525 return eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
526 }
527 }
528 }
529
530 // ###################################################################
531 /**
532 * Returns a user array of information that is specific to all visiting
533 * users (guests). This can then be passed to any function that requires
534 * user information.
535 *
536 * @access public
537 *
538 * @return array User information array
539 */
540 function fetch_guest_user()
541 {
542 global $bugsys;
543
544 return array(
545 'usergroupid' => 1,
546 'userid' => 0,
547 'email' => '',
548 'displayname' => '',
549 'showcolors' => 1,
550 'permissions' => $bugsys->datastore['usergroup'][1]['permissions'],
551 'displaytitle' => $bugsys->datastore['usergroup'][1]['displaytitle'],
552 );
553 }
554
555 // ###################################################################
556 /**
557 * Does an exhaustive permissions check on the bug. It checks for hidden
558 * bug status and ability to view hidden bugs. This normally was done
559 * at the top of each page, but it got so big, it was moved to a function.
560 *
561 * @access public
562 *
563 * @param array Bug array
564 * @param array Alternate user array
565 *
566 * @return bool Does the user have permission
567 */
568 function check_bug_permissions($bug, $userinfo = null)
569 {
570 global $bugsys;
571 if ($userinfo == null)
572 {
573 $userinfo = $bugsys->userinfo;
574 }
575
576 $bugsys->debug("checking permissions for $userinfo[userid] on bug $bug[bugid]");
577
578 $bugsys->debug('*** START VERBOSE CHECK ***');
579
580 $bugsys->debug('* !can_perform(canviewbugs, $bug[product], $userinfo) = ' . (int)(!can_perform('canviewbugs', $bug['product'], $userinfo)));
581 $bugsys->debug('* $bug[hidden] = ' . (int)$bug['hidden']);
582 $bugsys->debug('* $userinfo[userid] (' . $userinfo['userid'] . ') == $bug[userid] (' . $bug['userid'] . ') = ' . (int)($userinfo['userid'] == $bug['userid']));
583 $bugsys->debug('* can_perform(canviewownhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewownhidden', $bug['product'], $userinfo)));
584 $bugsys->debug('* can_perform(canviewhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewhidden', $bug['product'], $userinfo)));
585 $bugsys->debug('* !$bug[hidden] = ' . (int)(!$bug['hidden']));
586
587 $bugsys->debug('*** END PERMISSIONS CHECK ***');
588
589 if
590 (
591 !can_perform('canviewbugs', $bug['product'], $userinfo)
592 OR
593 !(
594 (
595 $bug['hidden']
596 AND
597 (
598 ($userinfo['userid'] == $bug['userid'] AND can_perform('canviewownhidden', $bug['product'], $userinfo))
599 OR
600 can_perform('canviewhidden', $bug['product'], $userinfo)
601 )
602 )
603 OR
604 !$bug['hidden']
605 )
606 )
607 {
608 $bugsys->debug('*** DONE WITH REAL CALLS ***');
609 return false;
610 }
611
612 $bugsys->debug('*** DONE WITH REAL CALLS ***');
613
614 return true;
615 }
616
617 /*=====================================================================*\
618 || ###################################################################
619 || # $HeadURL$
620 || # $Id$
621 || ###################################################################
622 \*=====================================================================*/
623 ?>