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