1<?php 2# --------------------------------------------------------------------- 3# truc is a tool for requirement and use case tracking 4# Copyright (C) 2006 ASDIS - http://sf.net/projects/truc 5# 6# (rth) Initial truc version based on rth 7# Copyright (C) 2005 George Holbrook - rth@lists.sourceforge.net 8# 9# This program is distributed under the terms and conditions of the GPL 10# See the README and LICENSE files for details 11#---------------------------------------------------------------------- 12 13#------------------------------------------------------------------------------------------ 14# Returns true if $var is whitespace string or empty string 15#------------------------------------------------------------------------------------------ 16function util_is_blank( $var ) { 17 18 if ( strlen( trim($var) ) == 0 ) { 19 return true; 20 } 21 22 return false; 23} 24 25function util_pad_id( $id ) { 26 27 $padded_id = sprintf("%05s",trim($id)); 28 29 return $padded_id; 30} 31 32function util_set_page_number( &$page_number, &$options ) { 33 34 if( !empty($options['page_number']) ) { 35 36 if( $options['page_number']=='First' ) { 37 38 $page_number = $options['first_page_number']; 39 } elseif( $options['page_number']=='Previous' ) { 40 41 $page_number = $options['previous_page_number']; 42 43 } elseif( $options['page_number']=='Next' ) { 44 45 $page_number = $options['next_page_number']; 46 47 } elseif( $options['page_number']=='Last' ) { 48 49 $page_number = $options['last_page_number']; 50 51 } else { 52 53 $page_number = $options['page_number']; 54 } 55 } 56} 57 58function util_set_filter( $filter_name, &$filter, &$options ) { 59 60 # check that filter has been submitted 61 if( !empty($options[$filter_name]) ) { 62 63 $filter = $options[$filter_name]; 64 } 65} 66 67function util_set_order_by( &$order_by, &$options ) { 68 69 if( !empty($options['change_order_by']) ) { 70 71 $new_order_by = $options['change_order_by']; 72 73 $new_order_by_hidden = str_replace(" ", "_", $new_order_by); 74 if( !empty($options[$new_order_by_hidden]) ) { 75 $order_by = $options[$new_order_by_hidden]; 76 } 77//print$new_order_by;exit; 78 } else { 79 if( !empty($options['order_by']) ) { 80 $order_by = $options['order_by']; 81 } 82 } 83} 84 85#------------------------------------------------------------------------------------------ 86# Returns the opposite order by direction of $direction 87#------------------------------------------------------------------------------------------ 88function util_set_order_dir( &$order_dir, &$options ) { 89 90 if( !empty($options['change_order_by']) ) { 91 92 switch( $options['order_dir'] ) { 93 case( "ASC" ): 94 $new_order_dir = "DESC"; 95 break; 96 case( "DESC" ): 97 $new_order_dir = "ASC"; 98 break; 99 } 100 101 $order_dir = $new_order_dir; 102 } else { 103 if( !empty($options['order_dir']) ) { 104 $order_dir = $options['order_dir']; 105 } 106 } 107 108} 109#------------------------------------------------------------------------------------------ 110# Searches an array for a given value. 111# 112# INPUT: 113# Array 114# OUTPUT: 115# True or False 116#------------------------------------------------------------------------------------------ 117function util_array_value_search($search_value, $stack) { 118 119 foreach($stack as $value) { 120 121 if( $search_value == $value ) { 122 return true; 123 } 124 } 125 126 return false; 127} 128 129#------------------------------------------------------------------------------------------ 130# Searches an array for a given key. 131# 132# INPUT: 133# Array 134# OUTPUT: 135# True or False 136#------------------------------------------------------------------------------------------ 137function util_array_key_search($search_key, $stack) { 138 139 foreach($stack as $key => $value) { 140 141 if($search_key == $key) { 142 return true; 143 } 144 } 145 146 return false; 147} 148 149#----------------------------------------------------------------------------- 150# Returns cleaned version of a _POST variable, or if variable does not exist, 151# returns a default value. 152# 153# INPUT: 154# _POST variable name 155# default value for variable 156# OUTPUT: 157# cleaned version of _POST variable or the default value 158#----------------------------------------------------------------------------- 159function util_clean_post_vars( $var, $default=null ) { 160 161 $result = ''; 162 if( isset( $_POST[$var] ) && !empty($_POST[$var]) ) { 163 164 $result = trim( $_POST[$var] ); 165 $result = stripslashes( $result ); 166 $result = str_replace( "'", "\'", "$result"); 167 $result = htmlentities($result, ENT_QUOTES); 168 169 } elseif( func_num_args() > 1 ) { 170 $result = $default; 171 } 172 173 return $result; 174} 175 176#----------------------------------------------------------------------------- 177# Returns cleaned version of a _GET variable, or if variable does not exist, 178# returns a default value. 179# 180# INPUT: 181# _GET variable name 182# default value for variable 183# OUTPUT: 184# cleaned version of _GET variable or the default value 185#----------------------------------------------------------------------------- 186function util_clean_get_vars( $var, $default=null ) { 187 188 $result = ''; 189 if( isset($_GET[$var]) && !empty($_GET[$var]) ) { 190 191 $result = trim( $_GET[$var] ); 192 $result = stripslashes( $result ); 193 } elseif( !is_null($default) ) { 194 195 $result = $default; 196 } 197 198 return $result; 199} 200 201#----------------------------------------------------------------------------- 202# Returns cleaned version of a passed string, or if variable does not exist, 203# returns a default value. 204# 205# INPUT: 206# string 207# default value of string 208# OUTPUT: 209# string stripped of leading white spaces, trailing white spaces, slashes, 210# and replaces characters with a html entitity code 211# or the default value 212#----------------------------------------------------------------------------- 213function util_clean_var_html( $var, $default=null ) { 214 $result = ''; 215 if( isset($var) && !empty($var) && is_string($var) ) { 216 217 $result = trim($var); 218 $result = stripslashes($result); 219 $result = str_replace("'", "\'", "$result"); 220 $result = htmlentities($result, ENT_QUOTES); 221 } elseif( !is_null($default) ) { 222 223 $result = $default; 224 } 225 226 return $result; 227} 228 229#----------------------------------------------------------------------------- 230# Returns cleaned version of a passed string, or if variable does not exist, 231# returns a default value. 232# 233# INPUT: 234# string 235# default value of string 236# OUTPUT: 237# string stripped of leading white spaces, trailing white spaces and slashes 238# or the default value 239#----------------------------------------------------------------------------- 240function util_clean_var_strip( $var, $default=null ) { 241 242 $result = ''; 243 if( isset($var) && !empty($var) ) { 244 245 $result = trim( $var ); 246 $result = stripslashes( $result ); 247 } elseif( !is_null($default) ) { 248 249 $result = $default; 250 } 251 252 return $result; 253} 254 255#----------------------------------------------------------------------------- 256# Retrieve a cookie variable 257# You may pass in any variable as a default (including null) but if 258# you pass in *no* default then an error will be triggered if the cookie 259# cannot be found 260#----------------------------------------------------------------------------- 261function util_get_cookie( $p_var_name, $p_default=null ) { 262 $t_result = ''; 263 264 if ( isset( $_COOKIE[$p_var_name] ) ) { 265 266 $t_result = $_COOKIE[$p_var_name]; 267 #check for a default passed in (allowing null) 268 } else if ( func_num_args() > 1 ) { 269 270 $t_result = $p_default; 271 } 272 273 return $t_result; 274} 275 276#------------------------------------------------------------------------ 277# Set a cookie variable 278# If $p_expire is false instead of a number, the cookie will expire when 279# the browser is closed; if it is true, the default time from the config 280# file will be used 281# If $p_path or $p_domaain are omitted, defaults are used 282#------------------------------------------------------------------------ 283 284function util_set_cookie( $p_name, $p_value, $p_path=null, $p_domain=null ) { 285 286 return setcookie( $p_name, $p_value, time() + COOKIE_EXPIRE_LENGTH, COOKIE_PATH, COOKIE_DOMAIN ); 287} 288 289 290# ---------------------------------------------------------------------- 291# Prepare text for exporting to csv file 292# INPUT: 293# text to prepare 294# OUTPUT: 295# text without embedded commas, tags and carriage return/linefeeds 296# ---------------------------------------------------------------------- 297function util_prepare_text_for_export($text) { 298 299 $text = str_replace(",", ":", $text); 300 $text = strip_tags($text); 301 $text = str_replace("\r", " ", $text); 302 $text = str_replace("\n", " ", $text); 303 304 return $text; 305} 306 307 308# ---------------------------------------------------------------------- 309# Get file type 310# INPUT: 311# file name 312# OUTPUT: 313# file extension 314# ---------------------------------------------------------------------- 315function util_get_filetype($file_name) { 316 $extension_start = strrpos($file_name, '.'); 317 if ($extension_start === false) { 318 return ''; 319 } 320 else { 321 return substr ( $file_name, $extension_start+1); 322 } 323 } 324 325# ---------------------------------------------------------------------- 326# Validate date 327# 328# INPUT: 329# Date to verify 330# OUTPUT: 331# True if date is valid, false if date is invalid 332# ---------------------------------------------------------------------- 333function util_date_isvalid($date) { 334 335 $date_isvalid = true; 336 337 if (!empty($date)) { 338 # split date into parts 339 $year = substr($date, 0, 4); 340 $separator1 = substr($date, 4, 1); 341 $month = substr($date, 5, 2); 342 $separator2 = substr($date, 7, 1); 343 $day = substr($date, 8, 2); 344 345 if ( strlen($date) == 10 && 346 is_numeric($year) && is_numeric($month) && is_numeric($day) && 347 $separator1 == '-' && $separator2 == '-') { 348 return checkdate($month, $day, $year); 349 } 350 else { 351 $date_isvalid = false; 352 } 353 } 354 355 return $date_isvalid; 356} 357 358# ---------------------------------------------------------------------- 359# Prints table row with whitespace in td field 360# ---------------------------------------------------------------------- 361function util_add_spacer() { 362 363 print"<tr><td> </td></tr>"; 364} 365 366# ---------------------------------------------------------------------- 367# Returns Timestamp 368# OUTPUT: 369# Current unix timestamp with microseconds 370# ---------------------------------------------------------------------- 371 function util_getmicrotime() { 372 list($usec, $sec) = explode(" ",microtime()); 373 return ((float)$usec + (float)$sec); 374} 375 376# ---------------------------------------------------------------------- 377# Returns a delete message 378# 379# delete messages are defined in properties_inc.php and strings_english.txt 380# ---------------------------------------------------------------------- 381function util_get_delete_msg( $get_var ) { 382 383 $del_array = lang_get( 'del_msg' ); 384 $message = $del_array[$get_var]; 385 386 return $message; 387} 388 389# ---------------------------------------------------------------------- 390# Next version of the version given 391# ---------------------------------------------------------------------- 392function util_increment_version( $v ) { 393 394 $x = explode(".", $v); 395 $m = $x[1] + 1; 396 397 if( $m == 100 ) { 398 399 $x[0]++; 400 $m = 0; 401 } 402 403 $version = $x[0] . "." . $m; 404 405 return $version; 406} 407 408function util_get_display_options( $options, $default_order_by, $default_order_dir, $default_page_number ) { 409 410 $order_by = $default_order_by; 411 $order_dir = $default_order_dir; 412 $page_number = $default_page_number; 413 414 if( !empty($options['order_by']) ) { 415 416 $order_by = $options['order_by']; 417 $order_by_hidden = str_replace(" ", "_", $order_by); 418 $order_by = $options[$order_by_hidden]; 419 420 $order_dir = $options['order_dir']; 421 } 422 423 if( !empty($options["page_number"]) ){ 424 $page_number = util_set_page_number($options, $default_page_number); 425 } 426 427 return array( "order_by"=>$order_by, 428 "order_dir"=>$order_dir, 429 "page_number"=>$page_number ); 430} 431 432function util_strip_slashes($value) 433{ 434 if( get_magic_quotes_gpc() ) { 435 436 $value = stripslashes($value); 437 } 438 439 return $value; 440} 441 442function util_strip_html_tags( $str ) { 443 444 return preg_replace("/<[^>]*>/", "", $str); 445} 446 447function util_nl2p($str) { 448 449 return '<p align=left>' . preg_replace('#\n|\r#', "</p>\n<p align=left>", $str) . "</p>\n"; 450} 451 452function util_space2nbsp($str) { 453 454 return str_replace(' ', ' ', $str); 455} 456/* 457function util_string_insert_href($p_string) { 458 459 # Find any URL in a string and replace it by a clickable link 460 $p_string = preg_replace( '/([http|irc|ftp|https]{2,}:\/\/([a-z0-9_-]|\/|\@|:{0,1}\.{0,1}){1,})/i', 461 '<a href="\1">\1</a> [<a href="\1" target="blank">^</a>]', 462 $p_string); 463 464 # Set up a simple subset of RFC 822 email address parsing 465 # We don't allow domain literals or quoted strings 466 # We also don't allow the & character in domains even though the RFC 467 # appears to do so. This was to prevent > etc from being included. 468 # Note: we could use email_get_rfc822_regex() but it doesn't work well 469 # when applied to data that has already had entities inserted. 470 # 471 # bpfennig: '@' doesn't accepted anymore 472 $t_atom = '[^\'@\'](?:[^()<>@,;:\\\".\[\]\000-\037\177 &]+)'; 473 474 # In order to avoid selecting URLs containing @ characters as email 475 # addresses we limit our selection to addresses that are preceded by: 476 # * the beginning of the string 477 # * a < entity (allowing '<foo@bar.baz>') 478 # * whitespace 479 # * a : (allowing 'send email to:foo@bar.baz') 480 # * a \n, \r, or > (because newlines have been replaced with <br /> 481 # and > isn't valid in URLs anyway 482 # 483 # At the end of the string we allow the opposite: 484 # * the end of the string 485 # * a > entity 486 # * whitespace 487 # * a , character (allowing 'email foo@bar.baz, or ...') 488 # * a \n, \r, or < 489 $p_string = preg_replace( '/(?<=^|<|[\s\:\>\n\r])('.$t_atom.'(?:\.'.$t_atom.')*\@'.$t_atom.'(?:\.'.$t_atom.')*)(?=$|>|[\s\,\<\n\r])/s', 490 '<a href="mailto:\1" target="_new">\1</a>', 491 $p_string); 492 493 return $p_string; 494} 495*/ 496function util_string_insert_href( $p_string ) { 497 498 # Find any URL in a string and replace it by a clickable link 499 $p_string = preg_replace( '/(([[:alpha:]][-+.[:alnum:]]*):\/\/(%[[:digit:]A-Fa-f]{2}|[-_.!~*\';\/?%^\\\\:@&={\|}+$#\(\),\[\][:alnum:]])+)/se', 500 "'<a href=\"'.rtrim('\\1','.').'\">\\1</a> [<a href=\"'.rtrim('\\1','.').'\" target=\"_blank\">^</a>]'", 501 $p_string); 502 # Set up a simple subset of RFC 822 email address parsing 503 # We don't allow domain literals or quoted strings 504 # We also don't allow the & character in domains even though the RFC 505 # appears to do so. This was to prevent > etc from being included. 506 # Note: we could use email_get_rfc822_regex() but it doesn't work well 507 # when applied to data that has already had entities inserted. 508 # 509 # bpfennig: '@' doesn't accepted anymore 510 $t_atom = '[^\'@\'](?:[^()<>@,;:\\\".\[\]\000-\037\177 &]+)'; 511 512 # In order to avoid selecting URLs containing @ characters as email 513 # addresses we limit our selection to addresses that are preceded by: 514 # * the beginning of the string 515 # * a < entity (allowing '<foo@bar.baz>') 516 # * whitespace 517 # * a : (allowing 'send email to:foo@bar.baz') 518 # * a \n, \r, or > (because newlines have been replaced with <br /> 519 # and > isn't valid in URLs anyway 520 # 521 # At the end of the string we allow the opposite: 522 # * the end of the string 523 # * a > entity 524 # * whitespace 525 # * a , character (allowing 'email foo@bar.baz, or ...') 526 # * a \n, \r, or < 527 528 $p_string = preg_replace( '/(?<=^|"|<|[\s\:\>\n\r])('.$t_atom.'(?:\.'.$t_atom.')*\@'.$t_atom.'(?:\.'.$t_atom.')*)(?=$|"|>|[\s\,\<\n\r])/s', 529 '<a href="mailto:\1" target="_new">\1</a>', 530 $p_string); 531 532 return $p_string; 533} 534 535function util_html_encode_string($str) { 536 537 $return_value = $str; 538 $return_value = util_space2nbsp($return_value); 539 $return_value = util_nl2p($return_value); 540 $return_value = util_string_insert_href($return_value); 541 542 return $return_value; 543} 544 545# Remove slashes added by PHP, and convert special 546# characters to HTML character codes. 547function util_html_special_chars_string($str) { 548 549 return htmlspecialchars(util_strip_slashes($str), ENT_QUOTES); 550} 551 552function util_unhtmlentities($string) { 553 554 $trans_tbl = array( "&" => "&", 555 """=> '"', 556 "'"=> "'", 557 "<" => "<", 558 ">" => ">" ); 559 560 return strtr($string, $trans_tbl); 561} 562 563function util_page_number($page_number, $row_count, $per_page) { 564 565 # Make sure page count is at least 1 566 $page_count = ceil($row_count / $per_page ); 567 if( $page_count < 1 ) { 568 $page_count = 1; 569 } 570 571 # Make sure page_number isn't past the last page. 572 if( $page_number > $page_count ) { 573 $page_number = $page_count; 574 } 575 576 return $page_number; 577} 578 579function util_strip_get($remove_keys, $get) { 580 581 $return_value = array(); 582 583 foreach($get as $key => $value) { 584 585 if( !util_array_value_search($remove_keys, $key) ) { 586 587 $return_value[$key] = $value; 588 } 589 } 590 591 return $return_value; 592} 593 594# ---------------------------------------------------------------------- 595# Convert the number of results to display per page into an acceptable 596# range. 597# INPUT: 598# Number of results to display per page 599# OUTPUT: 600# Validated number of results to display per page 601# ---------------------------------------------------------------------- 602function util_per_page( $per_page ) { 603 604 if( empty( $per_page ) ) { 605 $per_page = 25; 606 } 607 if( $per_page > 100 ) { 608 $per_page = 100; 609 } 610 if( $per_page < 15 ) { 611 $per_page = 15; 612 } 613 return $per_page; 614} 615?>