r63: Updated initial copyright year from 2003 to 2002
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Renapsus [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2002-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2002 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 // ########################### Start phrase ##########################
14 function phrase()
15 {
16 global $bugsys;
17
18 $args = func_get_args();
19 $numargs = sizeof($args);
20
21 if ($numargs < 1)
22 {
23 return false;
24 }
25
26 if ($phrasetext = $bugsys->language["$args[0]"])
27 {
28 if ($numargs < 2)
29 {
30 $phrase = $phrasetext;
31 }
32 else
33 {
34 $args[0] = $phrasetext;
35 if (($phrase = @call_user_func_array('sprintf', $args)) === false)
36 {
37 for ($i = 1; $i < $numargs; $i++)
38 {
39 $phrase = str_replace("%{$i}\$s", $args["$i"], $args[0]);
40 }
41 }
42 }
43 return preg_replace('#%([0-9].*?)\$s#', '<strong>[ARG \\1: UNDEFINED]</strong>', $phrase);
44 }
45 else
46 {
47 return "<strong>[UNDEFINED PHRASE: $args[0]]</strong>";
48 }
49 }
50
51 // ########################## Start sanitize #########################
52 function sanitize($toclean)
53 {
54 global $vars;
55
56 foreach ($toclean AS $varname => $cleaner)
57 {
58 $varvalue = $_REQUEST["$varname"];
59 switch (strtoupper($cleaner))
60 {
61 case 'STR':
62 $cleaned = trim($varvalue);
63 break;
64
65 case 'STR_NOHTML':
66 $cleaned = htmlspecialcharslike(trim($varvalue));
67 break;
68
69 case 'INT':
70 $cleaned = intval($varvalue);
71 break;
72
73 case 'FLOAT':
74 $cleaned = floatval($varvalue);
75 break;
76
77 default: break;
78 }
79 $vars["$varname"] = $cleaned;
80 }
81 }
82
83 // ##################### Start fetch_random_chars ####################
84 function fetch_random_chars($length = 0)
85 {
86 // Gimme a length!
87 if (!$length)
88 {
89 $length = rand(20, 65);
90 }
91
92 // Number of ints in our salt
93 $intcount = rand(0, intval($length / 2));
94
95 // Number of chars
96 $charcount = $length - $intcount;
97
98 // Upper-case chars
99 $upperchars = rand(1, intval($charcount / 2));
100
101 // Lower-case chars
102 $lowerchars = $charcount - $upperchars;
103
104 // Generate ints
105 for ($i = 0; $i < $intcount; $i++)
106 {
107 $string[] = rand(0, 9);
108 }
109
110 // Generate upper chars
111 for ($i = 0; $i < $upperchars; $i++)
112 {
113 $string[] = chr(rand(65, 90));
114 }
115
116 // Generate lower chars
117 for ($i = 0; $i < $lowerchars; $i++)
118 {
119 $string[] = chr(rand(97, 122));
120 }
121
122 // Randomly key the chars
123 foreach ($string AS $char)
124 {
125 $rand = mt_rand();
126 $newstr["$rand"] = $char;
127 }
128
129 // Sort the chars by thier random assignment
130 ksort($newstr);
131
132 // Flatten the array
133 $string = '';
134 foreach ($newstr AS $char)
135 {
136 $string .= $char;
137 }
138
139 return $string;
140 }
141
142 // ######################## Start mysetcookie ########################
143 function mysetcookie($name, $value = false, $sticky = true)
144 {
145 // Expire the cookie if it doesn't have a value
146 if (!$value)
147 {
148 setcookie($name, $value, time() - (2 * 900), '/');
149 }
150 else
151 {
152 // Make the cookie one that stays?
153 if ($sticky)
154 {
155 $expire = time() + 60 * 60 * 24 * 365;
156 }
157 else
158 {
159 $expire = 0;
160 }
161 setcookie($name, $value, $expire, '/');
162 }
163 }
164
165 // ############################ Start iff ############################
166 function iff($condition, $true, $false = null)
167 {
168 return ($condition) ? ($true) : ($false);
169 }
170
171 // ######################## Start exec_swap_bg #######################
172 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
173 {
174 global $row_class;
175 static $row_count;
176
177 $row_class = iff($row_count % 2, $class1, $class2);
178 $row_count++;
179 }
180
181 // ####################### Start addslasheslike ######################
182 // leave this named as such for easy typing
183 function addslasheslike($str)
184 {
185 static $mqgpc;
186
187 if (!isset($mqgpc))
188 {
189 $mqgpc = get_magic_quotes_gpc();
190 }
191
192 if ($mqgpc)
193 {
194 return $str;
195 }
196 else
197 {
198 return addslashes($str);
199 }
200 }
201
202 // #################### Start htmlspecialcharslike ###################
203 function htmlspecialcharslike($str)
204 {
205 return str_replace(array('"', '<', '>'), array('&quot;', '&lt;', '&gt;'), $str);
206 }
207
208 // ##################### Start unhtmlspecialchars ####################
209 function unhtmlspecialchars($str)
210 {
211 return str_replace(array('&quot;', '&lt;', '&gt;'), array('"', '<', '>'), $str);
212 }
213
214 // ################## Start fetch_user_display_name ##################
215 // preps a dispaly name if one isn't set
216 // should be able to be removed by the final version as registration should set this
217 function fetch_user_display_name(&$userinfo)
218 {
219 if (!$userinfo['displayname'])
220 {
221 $userinfo['displayname'] = ucwords(trim(str_replace(array('@', '.com', '.net', '.edu', '.org', '.info', '.biz'), ' ', $userinfo['email'])));
222 }
223 }
224
225 // ################## Start construct_option_select ##################
226 // creates a <select> menu from an array
227 // key vars are used when you need to get data out of the $label array
228 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
229 {
230 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
231 if ($includenil !== false)
232 {
233 $opts[] = '<option value="' . $includenil . '"' . iff(!$selected, ' selected="selected"') . '>Not Selected</option>';
234 }
235 foreach ($array AS $value => $label)
236 {
237 $opts[] = '<option value="' . iff($valuekey, $label["$valuekey"], $value) . '"' . iff($selected == iff($valuekey, $label["$valuekey"], $value), ' selected="selected"') . '>' . iff($labelkey, $label["$labelkey"], $label) . '</option>';
238 }
239 return '<select name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
240 }
241
242 // ########################## Start datelike #########################
243 function datelike($format, $timestamp)
244 {
245 global $bugsys;
246
247 if (!$format OR $format == 'standard')
248 {
249 $format = $bugsys->options['dateformat'];
250 }
251
252 return date($format, ($timestamp + (60 * $bugsys->userinfo['timezone'])));
253 }
254
255 // ################### Start construct_user_display ##################
256 // $userinfo needs userid, email, displayname, and showemail
257 function construct_user_display($userinfo, $userid = true)
258 {
259 fetch_user_display_name($userinfo);
260 return "$userinfo[displayname]" . iff($userinfo['showemail'], " &lt;$userinfo[email]&gt;") . iff($userid, " (userid: $userinfo[userid])");
261 }
262
263 // ######################## Start can_perform ########################
264 // short-hand for bitwise &
265 function can_perform($bitmask, $userinfo = null)
266 {
267 global $_PERMISSION;
268 if (!$userinfo)
269 {
270 global $bugsys;
271 return ($bugsys->userinfo['permissions'] & $_PERMISSION["$bitmask"]);
272 }
273 return ($userinfo['permissions'] & $_PERMISSION["bitmask"]);
274 }
275
276 // #################### Start construct_pcv_select ###################
277 // constructs a product/component/version select with one go :-)
278 // NB: need to make sure we have the option to turn off just p/c selection without v
279 function construct_pcv_select($select = '', $prefix = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
280 {
281 global $bugsys, $DB_sql, $tpl;
282 static $HTML;
283
284 if ($HTML)
285 {
286 return $HTML;
287 }
288
289
290 $selected = ' checked="checked"';
291
292 $products_fetch = $DB_sql->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
293 while ($product = $DB_sql->fetch_array($products_fetch))
294 {
295 if ($product['componentmother'])
296 {
297 $components["$product[componentmother]"]["$product[productid]"] = $product;
298 }
299 else
300 {
301 $products["$product[productid]"] = $product;
302 }
303 }
304
305 $versions_fetch = $DB_sql->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder");
306 while ($version = $DB_sql->fetch_array($versions_fetch))
307 {
308 $versions["$version[productid]"]["$version[versionid]"] = $version;
309 }
310
311 foreach ($products AS $product)
312 {
313 $row['prefix'] = '';
314 $valuepfx = "p$product[productid]";
315 $row['value'] = "{$valuepfx}c0v0";
316 $row['title'] = "<strong>$product[title]</strong>";
317 $row['description'] = '';
318 eval('$HTML .= "' . $tpl->fetch('pcv_select_row') . '";');
319 $HTML .= construct_pcv_select_global_version($product['productid'], 0, $versions, $prefix, $select);
320 if (is_array($versions["$product[productid]"]))
321 {
322 foreach ($versions["$product[productid]"] AS $version)
323 {
324 $row['prefix'] = $prefix . $prefix;
325 $row['value'] = "{$valuepfx}c0v$version[versionid]";
326 $row['title'] = $version['version'];
327 $row['selected'] = iff($select == $row['value'], $selected, '');
328 eval('$HTML .= "' . $tpl->fetch('pcv_select_row') . '";');
329 }
330 }
331
332 if (is_array($components["$product[productid]"]))
333 {
334 foreach ($components["$product[productid]"] AS $component)
335 {
336 $row['prefix'] = $prefix;
337 $valuepfx .= "c$component[productid]";
338 $row['value'] = "{$valuepfx}v0";
339 $row['selected'] = iff($select == $row['value'], $selected, '');
340 $row['title'] = $component['title'];
341 $row['description'] = '';
342 eval('$HTML .= "' . $tpl->fetch('pcv_select_row') . '";');
343 $HTML .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, $prefix, $select);
344 if (is_array($versions["$component[productid]"]))
345 {
346 foreach ($versions["$component[productid]"] AS $version)
347 {
348 $row['prefix'] = $prefix . $prefix;
349 $row['value'] = "{$valuepfx}v$version[versionid]";
350 $row['selected'] = iff($select == $row['value'], $selected, '');
351 $row['title'] = $version['version'];
352 $row['description'] = '';
353 eval('$HTML .= "' . $tpl->fetch('pcv_select_row') . '";');
354 }
355 }
356 }
357 }
358 }
359
360 return $HTML;
361 }
362
363 // ############ Start construct_pcv_select_global_version ############
364 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $prefix = '', $select = '')
365 {
366 global $tpl;
367 if (is_array($versions['0']))
368 {
369 foreach ($versions['0'] AS $version)
370 {
371 $row['prefix'] = $prefix . $prefix;
372 $row['typeselect'] = $type;
373 $row['value'] = "p{$product}c{$component}v$version[versionid]";
374 $row['selected'] = iff($select == $row['value'], ' checked="checked"', '');
375 $row['title'] = $version['version'];
376 $row['description'] = '';
377 eval('$global_versions_html .= "' . $tpl->fetch('pcv_select_row') . '";');
378 }
379 }
380 return $global_versions_html;
381 }
382
383 // ###################### Start parse_pcv_select #####################
384 function parse_pcv_select($input, $validate = false)
385 {
386 global $bugsys;
387
388 $input = trim($input);
389
390 /*
391 yummy regex tests....
392 var_dump(preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input));
393 var_dump(preg_match('#^p(\d.+?)c(\d.+?)v(\d.+?)$#', $input));
394 var_dump(preg_match('#^p([0-9]+?)c([0-9]+?)v([0-9]+?)$#', $input));
395 var_dump(preg_match('#^p([0-9].+?)c([0-9].+?)v([0-9].+?)$#', $input));
396 */
397
398 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
399 {
400 return false;
401 }
402
403 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
404 if (count($trio) != 3)
405 {
406 return false;
407 }
408
409 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
410
411 if (!$validate)
412 {
413 return $return;
414 }
415 else
416 {
417 // -------------------------------------------------------------------
418 // pcv validation
419 $product = $bugsys->datastore['product'][ $pcv['product'] ];
420 if (!$product)
421 {
422 return false;
423 }
424 $version = $bugsys->datastore['version'][ $pcv['version'] ];
425 if (!$version)
426 {
427 return false;
428 }
429 // no component
430 if ($pcv['component'] == 0)
431 {
432 // not global version and version.productid != product.productid
433 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
434 {
435 return false;
436 }
437 }
438 // using a component
439 else
440 {
441 $component = $bugsys->datastore['product'][ $pcv['component'] ];
442 // component has the right mother
443 if ($component['componentmother'] == $product['productid'])
444 {
445 // version.productid != {component.productid | product.productid}
446 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
447 {
448 return false;
449 }
450 }
451 else
452 {
453 return false;
454 }
455 }
456
457 return $pcv;
458 }
459 }
460
461 /*=====================================================================*\
462 || ###################################################################
463 || # $HeadURL$
464 || # $Id$
465 || ###################################################################
466 \*=====================================================================*/
467 ?>