4 * utils.php - Misc utils common to all aspects of the site
6 * SourceForge: Breaking Down the Barriers to Open Source Development
7 * Copyright 1999-2001 (c) VA Linux Systems
8 * http://sourceforge.net
16 * util_check_fileupload() - determines if a filename is appropriate for upload
18 * @param string The name of the file being uploaded
20 function util_check_fileupload($filename) {
22 /* Empty file is a valid file.
23 This is because this function should be called
24 unconditionally at the top of submit action processing
25 and many forms have optional file upload. */
26 if ($filename == 'none' || $filename == '') {
30 /* This should be enough... */
31 if (!is_uploaded_file($filename)) {
34 /* ... but we'd rather be paranoic */
35 if (strstr($filename, '..')) {
38 if (!is_file($filename)) {
41 if (!file_exists($filename)) {
44 if (dirname($filename) != '/tmp') {
51 * util_send_message() - Send email
52 * This function should be used in place of the PHP mail() function
54 * @param string The email recipients address
55 * @param string The email subject
56 * @param string The body of the email message
57 * @param string The optional email sender address. Defaults to 'noreply@'
58 * @param string The addresses to blind-carbon-copy this message
61 function util_send_message($to,$subject,$body,$from='',$BCC='') {
63 $to='noreply@'.$GLOBALS['sys_default_domain'];
66 $from='noreply@'.$GLOBALS['sys_default_domain'];
69 $to='noreply@'.$GLOBALS['sys_default_domain'];
74 "\nSubject: $subject".
77 exec ("/bin/echo \"". util_prep_string_for_sendmail($body) .
78 "\" | /usr/sbin/sendmail -f'$from' -t -i >& /dev/null &");
81 function util_send_jabber($to,$subject,$body) {
82 if (!$GLOBALS['sys_use_jabber']) {
85 $JABBER = new Jabber();
86 if (!$JABBER->Connect()) {
87 echo '<BR>Unable to connect';
90 //$JABBER->SendAuth();
91 //$JABBER->AccountRegistration();
92 if (!$JABBER->SendAuth()) {
93 echo '<BR>Auth Failure';
94 $JABBER->Disconnect();
96 //or die("Couldn't authenticate!");
98 $JABBER->SendPresence(NULL, NULL, "online");
100 $body=htmlspecialchars($body);
101 $to_arr=explode(',',$to);
102 for ($i=0; $i<count($to_arr); $i++) {
104 //echo '<BR>Sending Jabbers To: '.$to_arr[$i];
105 if (!$JABBER->SendMessage($to_arr[$i], "normal", NULL, array("body" => $body,"subject"=>$subject))) {
106 echo '<BR>Error Sending to '.$to_arr[$i];
111 $JABBER->CruiseControl(2);
112 $JABBER->Disconnect();
116 * util_prep_string_for_sendmail() - Prepares a string to be sent by email
118 * @param string The text to be prepared
119 * @returns The prepared text
122 function util_prep_string_for_sendmail($body) {
123 //$body=str_replace("\\","\\\\",$body);
124 $body=str_replace("`","\\`",$body);
125 $body=str_replace("\"","\\\"",$body);
126 $body=str_replace("\$","\\\$",$body);
131 * util_handle_message() - a convenience wrapper which sends messages
132 * to either a jabber account or email account or both, depending on
135 * @param array array of user_id's from the user table
136 * @param string subject of the message
137 * @param string the message body
138 * @param string a comma-separated list of email address
139 * @param string a comma-separated list of jabber address
141 function util_handle_message($id_arr,$subject,$body,$extra_emails='',$extra_jabbers='') {
144 if (count($id_arr) < 1) {
147 $res=db_query("SELECT jabber_address,email,jabber_only
148 FROM users WHERE user_id IN (". implode($id_arr,',') .")");
149 $rows=db_numrows($res);
151 for ($i=0; $i<$rows; $i++) {
153 // Build arrays of the jabber address
155 if (db_result($res,$i,'jabber_address')) {
156 $address['jabber_address'][]=db_result($res,$i,'jabber_address');
157 if (db_result($res,$i,'jabber_only') != 1) {
158 $address['email'][]=db_result($res,$i,'email');
161 $address['email'][]=db_result($res,$i,'email');
164 if (count($address['email']) > 0) {
165 $extra_email1=implode($address['email'],',').',';
167 if (count($address['jabber_address']) > 0) {
168 $extra_jabber1=implode($address['jabber_address'],',').',';
171 if ($extra_email1 || $extra_emails) {
172 util_send_message('',$subject,$body,'',$extra_email1.$extra_emails);
174 if ($extra_jabber1 || $extra_jabbers) {
175 util_send_jabber($extra_jabber1.$extra_jabbers,$subject,$body);
180 * util_unconvert_htmlspecialchars() - Unconverts a string converted with htmlspecialchars()
181 * This function requires PHP 4.0.3 or greater
183 * @param string The string to unconvert
184 * @returns The unconverted string
187 function util_unconvert_htmlspecialchars($string) {
188 if (strlen($string) < 1) {
191 //$trans = get_html_translation_table(HTMLENTITIES, ENT_QUOTES);
192 $trans = get_html_translation_table(HTML_ENTITIES);
193 $trans = array_flip ($trans);
194 $str = strtr ($string, $trans);
200 * util_result_columns_to_assoc() - Takes a result set and turns the column pair into an associative array
202 * @param string The result set ID
203 * @param int The column key
204 * @param int The optional column value
205 * @returns An associative array
208 function util_result_columns_to_assoc($result, $col_key=0, $col_val=1) {
209 $rows=db_numrows($result);
213 for ($i=0; $i<$rows; $i++) {
214 $arr[db_result($result,$i,$col_key)]=db_result($result,$i,$col_val);
223 * util_result_column_to_array() - Takes a result set and turns the optional column into an array
225 * @param int The result set ID
226 * @param int The column
230 function &util_result_column_to_array($result, $col=0) {
232 Takes a result set and turns the optional column into
235 $rows=db_numrows($result);
239 for ($i=0; $i<$rows; $i++) {
240 $arr[$i]=db_result($result,$i,$col);
249 * result_column_to_array() - DEPRECATED; DO NOT USE!
251 * @param int The result set ID
252 * @param int The column
253 * @see util_result_column_to_array()
257 function &result_column_to_array($result, $col=0) {
259 backwards compatibility
261 return util_result_column_to_array($result, $col);
265 * util_wrap_find_space() - Find the first space in a string
267 * @param string The string in which to find the space
268 * @param int The number of characters to wrap - Default is 80
269 * @returns The position of the first space
272 function util_wrap_find_space($string,$wrap) {
279 //find the first space starting at $start
280 $pos=@strpos($string,' ',$start);
282 //if that space is too far over, go back and start more to the left
283 if (($pos > ($wrap+5)) || !$pos) {
285 $start=($wrap-($try*5));
286 //if we've gotten so far left , just truncate the line
300 * util_line_wrap() - Automatically linewrap text
302 * @param string The text to wrap
303 * @param int The number of characters to wrap - Default is 80
304 * @param string The line break to use - Default is '\n'
305 * @returns The wrapped text
308 function util_line_wrap ($text, $wrap = 80, $break = "\n") {
309 $paras = explode("\n", $text);
313 while ($i < count($paras)) {
314 if (strlen($paras[$i]) <= $wrap) {
315 $result[] = $paras[$i];
318 $pos=util_wrap_find_space($paras[$i],$wrap);
320 $result[] = substr($paras[$i], 0, $pos);
322 $new = trim(substr($paras[$i], $pos, strlen($paras[$i]) - $pos));
325 $pos=util_wrap_find_space($paras[$i],$wrap);
331 return implode($break, $result);
335 * util_make_links() - Turn URL's into HREF's.
337 * @param string The URL
338 * @returns The HREF'ed URL
341 function util_make_links ($data='') {
342 if(empty($data)) { return $data; }
344 $lines = split("\n",$data);
345 while ( list ($key,$line) = each ($lines)) {
346 // When we come here, we usually have form input
347 // encoded in entities. Our aim is to NOT include
348 // angle brackets in the URL
349 // (RFC2396; http://www.w3.org/Addressing/URL/5.1_Wrappers.html)
350 $line = str_replace('>', "\1", $line);
351 $line = eregi_replace("([ \t]|^)www\."," http://www.",$line);
352 $text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $line);
353 $text = eregi_replace("([[:space:]]|^)(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "\\1<a href=\"mailto:\\2\" target=\"_new\">\\2</a>", $text);
354 $text = str_replace("\1", '>', $text);
361 * show_priority_colors_key() - Show the priority colors legend
364 function show_priority_colors_key() {
366 echo '<P><B>Priority Colors:</B><BR>
368 <TABLE BORDER=0><TR>';
370 for ($i=1; $i<10; $i++) {
372 <TD BGCOLOR="'.get_priority_color($i).'">'.$i.'</TD>';
374 echo '</tr></table>';
378 * utils_buildcheckboxarray() - Build a checkbox array
380 * @param int Number of options to be in the array
381 * @param string The name of the checkboxes
382 * @param array An array of boxes to be pre-checked
385 function utils_buildcheckboxarray($options,$name,$checked_array) {
386 $option_count=count($options);
387 $checked_count=count($checked_array);
389 for ($i=1; $i<=$option_count; $i++) {
391 <BR><INPUT type="checkbox" name="'.$name.'" value="'.$i.'"';
392 for ($j=0; $j<$checked_count; $j++) {
393 if ($i == $checked_array[$j]) {
397 echo '> '.$options[$i];
402 * GraphResult() - Takes a database result set and builds a graph.
403 * The first column should be the name, and the second column should be the values
404 * Be sure to include HTL_Graphs.php before using this function
406 * @author Tim Perdue tperdue@valinux.com
407 * @param int The databse result set ID
408 * @param string The title of the graph
411 Function GraphResult($result,$title) {
412 $rows=db_numrows($result);
414 if ((!$result) || ($rows < 1)) {
420 for ($j=0; $j<db_numrows($result); $j++) {
421 if (db_result($result, $j, 0) != '' && db_result($result, $j, 1) != '' ) {
422 $names[$j]= db_result($result, $j, 0);
423 $values[$j]= db_result($result, $j, 1);
428 This is another function detailed below
430 GraphIt($names,$values,$title);
435 * GraphIt() - Build a graph
437 * @author Tim Perdue tperdue@valinux.com
438 * @param array An array of names
439 * @param array An array of values
440 * @param string The title of the graph
443 Function GraphIt($name_string,$value_string,$title) {
446 $counter=count($name_string);
449 Can choose any color you wish
453 for ($i = 0; $i < $counter; $i++) {
454 $bars[$i]=$HTML->COLOR_LTBACK1;
457 $counter=count($value_string);
460 Figure the max_value passed in, so scale can be determined
465 for ($i = 0; $i < $counter; $i++) {
466 if ($value_string[$i] > $max_value) {
467 $max_value=$value_string[$i];
471 if ($max_value < 1) {
476 I want my graphs all to be 800 pixels wide, so that is my divisor
479 $scale=(400/$max_value);
482 I create a wrapper table around the graph that holds the title
488 echo $GLOBALS['HTML']->listTableTop ($title_arr);
491 Create an associate array to pass in. I leave most of it blank
520 This is the actual call to the HTML_Graphs class
523 html_graph($name_string,$value_string,$bars,$vals);
527 <!-- end outer graph table -->';
528 echo $GLOBALS['HTML']->listTableBottom();
532 * ShowResultSet() - Show a generic result set
533 * Very simple, plain way to show a generic result set
535 * @param int The result set ID
536 * @param string The title of the result set
537 * @param bool The option to turn URL's into links
540 Function ShowResultSet($result,$title="Untitled",$linkify=false) {
541 global $group_id,$HTML;
544 $rows = db_numrows($result);
545 $cols = db_numfields($result);
548 <TABLE BORDER="0" WIDTH="100%">';
550 /* Create the title */
553 $cell_data[] = array($title, 'colspan='.$cols);
554 echo $HTML->multiTableRow('',$cell_data, TRUE);
556 /* Create the headers */
558 for ($i=0; $i < $cols; $i++) {
559 $cell_data[] = array(db_fieldname($result,$i));
561 echo $HTML->multiTableRow('',$cell_data, TRUE);
563 /* Create the rows */
564 for ($j = 0; $j < $rows; $j++) {
565 echo '<TR '. $HTML->boxGetAltRowStyle($j) . '>';
566 for ($i = 0; $i < $cols; $i++) {
567 if ($linkify && $i == 0) {
568 $link = '<A HREF="'.$PHP_SELF.'?';
570 if ($linkify == "bug_cat") {
571 $link .= 'group_id='.$group_id.'&bug_cat_mod=y&bug_cat_id='.db_result($result, $j, 'bug_category_id').'">';
572 } else if($linkify == "bug_group") {
573 $link .= 'group_id='.$group_id.'&bug_group_mod=y&bug_group_id='.db_result($result, $j, 'bug_group_id').'">';
574 } else if($linkify == "patch_cat") {
575 $link .= 'group_id='.$group_id.'&patch_cat_mod=y&patch_cat_id='.db_result($result, $j, 'patch_category_id').'">';
576 } else if($linkify == "support_cat") {
577 $link .= 'group_id='.$group_id.'&support_cat_mod=y&support_cat_id='.db_result($result, $j, 'support_category_id').'">';
578 } else if($linkify == "pm_project") {
579 $link .= 'group_id='.$group_id.'&project_cat_mod=y&project_cat_id='.db_result($result, $j, 'group_project_id').'">';
581 $link = $linkend = '';
584 $link = $linkend = '';
586 echo '<td>'.$link . db_result($result, $j, $i) . $linkend.'</td>';
597 * validate_email() - Validate an email address
599 * @param string The address string to validate
600 * @returns true on success/false on error
603 function validate_email ($address) {
604 return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $address));
608 * util_is_valid_filename() - Verifies whether a file has a valid filename
610 * @param string The file to verify
611 * @returns true on success/false on error
614 function util_is_valid_filename ($file) {
616 $invalidchars = eregi_replace("[-A-Z0-9_\.]","",$file);
618 if (!empty($invalidchars)) {
621 if (strstr($file,'..')) {
630 * valid_hostname() - Validates a hostname string to make sure it doesn't contain invalid characters
632 * @param string The optional hostname string
633 * @returns true on success/false on failur
636 function valid_hostname ($hostname = "xyz") {
639 $invalidchars = eregi_replace("[-A-Z0-9\.]","",$hostname);
641 if (!empty($invalidchars)) {
645 //double dot, starts with a . or -
646 if (ereg("\.\.",$hostname) || ereg("^\.",$hostname) || ereg("^\-",$hostname)) {
650 $multipoint = explode(".",$hostname);
652 if (!(is_array($multipoint)) || ((count($multipoint) - 1) < 1)) {