r145: - Added logging mechanism to files
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
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 fetch_user_display_name ##################
52 // preps a dispaly name if one isn't set
53 // should be able to be removed by the final version as registration should set this
54 function fetch_user_display_name(&$userinfo)
55 {
56 if (!$userinfo['displayname'])
57 {
58 $userinfo['displayname'] = ucwords(trim(str_replace(array('@', '.com', '.net', '.edu', '.org', '.info', '.biz'), ' ', $userinfo['email'])));
59 }
60 }
61
62 // ################## Start construct_option_select ##################
63 // creates a <select> menu from an array
64 // key vars are used when you need to get data out of the $label array
65 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
66 {
67 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
68 if ($includenil !== false)
69 {
70 $opts[] = '<option value="' . $includenil . '"' . ((!$selected) ? ' selected="selected"' : '') . '>Not Selected</option>';
71 }
72 foreach ($array AS $value => $label)
73 {
74 $opts[] = '<option value="' . (($valuekey) ? $label["$valuekey"] : $value) . '"' . (($selected == (($valuekey) ? $label["$valuekey"] : $value)) ? ' selected="selected"' : '') . '>' . (($labelkey) ? $label["$labelkey"] : $label) . '</option>';
75 }
76 return '<select name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
77 }
78
79 // ########################## Start datelike #########################
80 function datelike($format, $timestamp)
81 {
82 global $bugsys;
83
84 if (!$format OR $format == 'standard')
85 {
86 $format = $bugsys->options['dateformat'];
87 }
88
89 return date($format, ($timestamp + (60 * $bugsys->userinfo['timezone'])));
90 }
91
92 // ################### Start construct_user_display ##################
93 // $userinfo needs userid, email, displayname, and showemail
94 function construct_user_display($userinfo, $userid = true)
95 {
96 fetch_user_display_name($userinfo);
97 return "$userinfo[displayname]" . (($userinfo['showemail']) ? " &lt;$userinfo[email]&gt;" : '') . (($userid) ? " (userid: $userinfo[userid])" : '');
98 }
99
100 // ######################## Start can_perform ########################
101 // short-hand for bitwise &
102 function can_perform($bitmask, $userinfo = null)
103 {
104 global $_PERMISSION;
105
106 if (!isset($_PERMISSION["$bitmask"]))
107 {
108 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
109 }
110
111 if (!$userinfo)
112 {
113 global $bugsys;
114 return ($bugsys->userinfo['permissions'] & $_PERMISSION["$bitmask"]);
115 }
116 return ($userinfo['permissions'] & $_PERMISSION["bitmask"]);
117 }
118
119 // #################### Start construct_pcv_select ###################
120 // constructs a product/component/version select with one go :-)
121 // NB: need to make sure we have the option to turn off just p/c selection without v
122 function construct_pcv_select($select = '', $prefix = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
123 {
124 global $bugsys;
125 static $HTML;
126
127 if ($HTML)
128 {
129 return $HTML;
130 }
131
132
133 $selected = ' checked="checked"';
134
135 $products_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
136 while ($product = $bugsys->db->fetch_array($products_fetch))
137 {
138 if ($product['componentmother'])
139 {
140 $components["$product[componentmother]"]["$product[productid]"] = $product;
141 }
142 else
143 {
144 $products["$product[productid]"] = $product;
145 }
146 }
147
148 $versions_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder");
149 while ($version = $bugsys->db->fetch_array($versions_fetch))
150 {
151 $versions["$version[productid]"]["$version[versionid]"] = $version;
152 }
153
154 foreach ($products AS $product)
155 {
156 $row['prefix'] = '';
157 $valuepfx = "p$product[productid]";
158 $row['value'] = "{$valuepfx}c0v0";
159 $row['title'] = "<strong>$product[title]</strong>";
160 $row['description'] = '';
161 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
162 $HTML .= construct_pcv_select_global_version($product['productid'], 0, $versions, $prefix, $select);
163 if (is_array($versions["$product[productid]"]))
164 {
165 foreach ($versions["$product[productid]"] AS $version)
166 {
167 $row['prefix'] = $prefix . $prefix;
168 $row['value'] = "{$valuepfx}c0v$version[versionid]";
169 $row['title'] = $version['version'];
170 $row['selected'] = (($select == $row['value']) ? $selected : '');
171 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
172 }
173 }
174
175 if (is_array($components["$product[productid]"]))
176 {
177 foreach ($components["$product[productid]"] AS $component)
178 {
179 $row['prefix'] = $prefix;
180 $valuepfx .= "c$component[productid]";
181 $row['value'] = "{$valuepfx}v0";
182 $row['selected'] = (($select == $row['value']) ? $selected : '');
183 $row['title'] = $component['title'];
184 $row['description'] = '';
185 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
186 $HTML .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, $prefix, $select);
187 if (is_array($versions["$component[productid]"]))
188 {
189 foreach ($versions["$component[productid]"] AS $version)
190 {
191 $row['prefix'] = $prefix . $prefix;
192 $row['value'] = "{$valuepfx}v$version[versionid]";
193 $row['selected'] = (($select == $row['value']) ? $selected : '');
194 $row['title'] = $version['version'];
195 $row['description'] = '';
196 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
197 }
198 }
199 }
200 }
201 }
202
203 return $HTML;
204 }
205
206 // ############ Start construct_pcv_select_global_version ############
207 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $prefix = '', $select = '')
208 {
209 global $bugsys;
210 if (is_array($versions['0']))
211 {
212 foreach ($versions['0'] AS $version)
213 {
214 $row['prefix'] = $prefix . $prefix;
215 $row['typeselect'] = $type;
216 $row['value'] = "p{$product}c{$component}v$version[versionid]";
217 $row['selected'] = (($select == $row['value']) ? ' checked="checked"' : '');
218 $row['title'] = $version['version'];
219 $row['description'] = '';
220 eval('$global_versions_html .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
221 }
222 }
223 return $global_versions_html;
224 }
225
226 // ###################### Start parse_pcv_select #####################
227 function parse_pcv_select($input, $validate = false)
228 {
229 global $bugsys;
230
231 $input = trim($input);
232
233 /*
234 yummy regex tests....
235 var_dump(preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input));
236 var_dump(preg_match('#^p(\d.+?)c(\d.+?)v(\d.+?)$#', $input));
237 var_dump(preg_match('#^p([0-9]+?)c([0-9]+?)v([0-9]+?)$#', $input));
238 var_dump(preg_match('#^p([0-9].+?)c([0-9].+?)v([0-9].+?)$#', $input));
239 */
240
241 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
242 {
243 return false;
244 }
245
246 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
247 if (count($trio) != 3)
248 {
249 return false;
250 }
251
252 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
253
254 if (!$validate)
255 {
256 return $return;
257 }
258 else
259 {
260 // -------------------------------------------------------------------
261 // pcv validation
262 $product = $bugsys->datastore['product'][ $pcv['product'] ];
263 if (!$product)
264 {
265 return false;
266 }
267 $version = $bugsys->datastore['version'][ $pcv['version'] ];
268 if (!$version)
269 {
270 return false;
271 }
272 // no component
273 if ($pcv['component'] == 0)
274 {
275 // not global version and version.productid != product.productid
276 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
277 {
278 return false;
279 }
280 }
281 // using a component
282 else
283 {
284 $component = $bugsys->datastore['product'][ $pcv['component'] ];
285 // component has the right mother
286 if ($component['componentmother'] == $product['productid'])
287 {
288 // version.productid != {component.productid | product.productid}
289 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
290 {
291 return false;
292 }
293 }
294 else
295 {
296 return false;
297 }
298 }
299
300 return $pcv;
301 }
302 }
303
304 // ######################### Start log_action ########################
305 function log_action($bugid, $language, $arguments, $original = array(), $changed = array())
306 {
307 global $bugsys;
308
309 $bugsys->db->query("
310 INSERT INTO " . TABLE_PREFIX . "history
311 (bugid, dateline, userid, language, arguments, original, changed)
312 VALUES
313 (" . intval($bugid) . ", " . time() . ", " . $bugsys->userinfo['userid'] . ",
314 '" . $bugsys->escape($language) . "', '" . $bugsys->escape(serialize($arguments)) . "',
315 '" . $bugsys->escape(serialize($original)) . "', '" . $bugsys->escape(serialize($changed)) . "'
316 )"
317 );
318 }
319
320 /*=====================================================================*\
321 || ###################################################################
322 || # $HeadURL$
323 || # $Id$
324 || ###################################################################
325 \*=====================================================================*/
326 ?>