r20: Changed newreport.php, newreport.php, and showreport.php to use commment.comment...
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # [#]app[#] [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]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 = count($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 if (!($phrase = @call_user_func_array('sprintf', $args)))
35 {
36 for ($i = 1; $i < $numargs; $i++)
37 {
38 $phrase = str_replace("%{$i}\$s", $args["$i"], $phrase);
39 }
40 }
41 }
42 return preg_replace('#%([0-9].*?)\$s#', '<strong>[ARG \1: UNDEFINED]</strong>', $phrase);
43 }
44 else
45 {
46 return "<strong>[UNDEFINED PHRASE: $args[0]]</strong>";
47 }
48 }
49
50 // ########################## Start sanitize #########################
51 function sanitize($toclean)
52 {
53 global $vars;
54
55 foreach ($toclean AS $varname => $cleaner)
56 {
57 $varvalue = $_REQUEST["$varname"];
58 switch (strtoupper($cleaner))
59 {
60 case 'STR':
61 $cleaned = trim($varvalue);
62 break;
63
64 case 'STR_NOHTML':
65 $cleaned = htmlspecialcharslike(trim($varvalue));
66 break;
67
68 case 'INT':
69 $cleaned = intval($varvalue);
70 break;
71
72 case 'FLOAT':
73 $cleaned = floatval($varvalue);
74 break;
75
76 default: break;
77 }
78 $vars["$varname"] = $cleaned;
79 }
80 }
81
82 // ##################### Start fetch_random_chars ####################
83 function fetch_random_chars($length = 0)
84 {
85 // Gimme a length!
86 if (!$length)
87 {
88 $length = rand(20, 65);
89 }
90
91 // Number of ints in our salt
92 $intcount = rand(0, intval($length / 2));
93
94 // Number of chars
95 $charcount = $length - $intcount;
96
97 // Upper-case chars
98 $upperchars = rand(1, intval($charcount / 2));
99
100 // Lower-case chars
101 $lowerchars = $charcount - $upperchars;
102
103 // Generate ints
104 for ($i = 0; $i < $intcount; $i++)
105 {
106 $string[] = rand(0, 9);
107 }
108
109 // Generate upper chars
110 for ($i = 0; $i < $upperchars; $i++)
111 {
112 $string[] = chr(rand(65, 90));
113 }
114
115 // Generate lower chars
116 for ($i = 0; $i < $lowerchars; $i++)
117 {
118 $string[] = chr(rand(97, 122));
119 }
120
121 // Randomly key the chars
122 foreach ($string AS $char)
123 {
124 $rand = mt_rand();
125 $newstr["$rand"] = $char;
126 }
127
128 // Sort the chars by thier random assignment
129 ksort($newstr);
130
131 // Flatten the array
132 $string = '';
133 foreach ($newstr AS $char)
134 {
135 $string .= $char;
136 }
137
138 return $string;
139 }
140
141 // ######################## Start mysetcookie ########################
142 function mysetcookie($name, $value = false, $sticky = true)
143 {
144 // Expire the cookie if it doesn't have a value
145 if (!$value)
146 {
147 setcookie($name, $value, time() - (2 * 900), '/');
148 }
149 else
150 {
151 // Make the cookie one that stays?
152 if ($sticky)
153 {
154 $expire = time() + 60 * 60 * 24 * 365;
155 }
156 else
157 {
158 $expire = 0;
159 }
160 setcookie($name, $value, $expire, '/');
161 }
162 }
163
164 // ############################ Start iff ############################
165 function iff($condition, $true, $false = null)
166 {
167 return ($condition) ? ($true) : ($false);
168 }
169
170 // ######################## Start exec_swap_bg #######################
171 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
172 {
173 global $row_class;
174 static $row_count;
175
176 $row_class = iff($row_count % 2, $class1, $class2);
177 $row_count++;
178 }
179
180 // ####################### Start addslasheslike ######################
181 // leave this named as such for easy typing
182 function addslasheslike($str)
183 {
184 static $mqgpc;
185
186 if (!isset($mqgpc))
187 {
188 $mqgpc = get_magic_quotes_gpc();
189 }
190
191 if ($mqgpc)
192 {
193 return $str;
194 }
195 else
196 {
197 return addslashes($str);
198 }
199 }
200
201 // #################### Start htmlspecialcharslike ###################
202 function htmlspecialcharslike($str)
203 {
204 return str_replace(array('"', '<', '>'), array('&quot;', '&lt;', '&gt;'), $str);
205 }
206
207 // ##################### Start unhtmlspecialchars ####################
208 function unhtmlspecialchars($str)
209 {
210 return str_replace(array('&quot;', '&lt;', '&gt;'), array('"', '<', '>'), $str);
211 }
212
213 // ################## Start fetch_user_display_name ##################
214 // preps a dispaly name if one isn't set
215 // should be able to be removed by the final version as registration should set this
216 function fetch_user_display_name(&$userinfo)
217 {
218 if (!$userinfo['displayname'])
219 {
220 $userinfo['displayname'] = ucwords(trim(str_replace(array('@', '.com', '.net', '.edu', '.org', '.info', '.biz'), ' ', $userinfo['email'])));
221 }
222 }
223
224 // ################## Start construct_option_select ##################
225 // creates a <select> menu from an array
226 // key vars are used when you need to get data out of the $label array
227 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
228 {
229 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
230 if ($includenil !== false)
231 {
232 $opts[] = '<option value="' . $includenil . '"' . iff(!$selected, ' selected="selected"') . '>Not Selected</option>';
233 }
234 foreach ($array AS $value => $label)
235 {
236 $opts[] = '<option value="' . iff($valuekey, $label["$valuekey"], $value) . '"' . iff($selected == iff($valuekey, $label["$valuekey"], $value), ' selected="selected"') . '>' . iff($labelkey, $label["$labelkey"], $label) . '</option>';
237 }
238 return '<select name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
239 }
240
241 // ########################## Start datelike #########################
242 function datelike($format, $timestamp)
243 {
244 global $bugsys;
245
246 if (!$format OR $format == 'standard')
247 {
248 $format = $bugsys->options['dateformat'];
249 }
250
251 return date($format, ($timestamp + (60 * $bugsys->userinfo['timezone'])));
252 }
253
254 // ################### Start construct_user_display ##################
255 // $userinfo needs userid, email, displayname, and showemail
256 function construct_user_display($userinfo)
257 {
258 fetch_user_display_name($userinfo);
259 return "$userinfo[displayname]" . iff($userinfo['showemail'], " &lt;$userinfo[email]&gt;") . " (userid: $userinfo[userid])";
260 }
261
262 /*=====================================================================*\
263 || ###################################################################
264 || # $HeadURL$
265 || # $Id$
266 || ###################################################################
267 \*=====================================================================*/
268 ?>