| 1 | 
joko | 
1.1 | 
<? | 
| 2 | 
  | 
  | 
//    ----------------------------------------------------------------------------- | 
| 3 | 
joko | 
1.7 | 
//    $Id: helper.php,v 1.6 2003/02/09 17:49:53 joko Exp $ | 
| 4 | 
joko | 
1.1 | 
//    ----------------------------------------------------------------------------- | 
| 5 | 
  | 
  | 
//    $Log: helper.php,v $ | 
| 6 | 
joko | 
1.7 | 
//    Revision 1.6  2003/02/09 17:49:53  joko | 
| 7 | 
  | 
  | 
//    - removed old code | 
| 8 | 
  | 
  | 
// | 
| 9 | 
joko | 
1.6 | 
//    Revision 1.5  2003/02/04 08:22:34  joko | 
| 10 | 
  | 
  | 
//    + function is_hash | 
| 11 | 
  | 
  | 
//    + function array_join_merge | 
| 12 | 
  | 
  | 
// | 
| 13 | 
joko | 
1.5 | 
//    Revision 1.4  2002/12/22 13:29:54  jonen | 
| 14 | 
  | 
  | 
//    + added number format helper functions: | 
| 15 | 
  | 
  | 
//      + number_format_locale() | 
| 16 | 
  | 
  | 
//      + is_amount() | 
| 17 | 
  | 
  | 
// | 
| 18 | 
jonen | 
1.4 | 
//    Revision 1.3  2002/12/11 01:00:22  joko | 
| 19 | 
  | 
  | 
//    + renamed function 'sai' to 'array_init' | 
| 20 | 
  | 
  | 
// | 
| 21 | 
joko | 
1.3 | 
//    Revision 1.2  2002/12/01 04:49:54  joko | 
| 22 | 
  | 
  | 
//    + function Dumper (from function dumpVar) | 
| 23 | 
  | 
  | 
// | 
| 24 | 
joko | 
1.2 | 
//    Revision 1.1  2002/11/12 05:42:31  joko | 
| 25 | 
  | 
  | 
//    + initial checkin | 
| 26 | 
  | 
  | 
// | 
| 27 | 
joko | 
1.1 | 
//    Revision 1.1  2002/10/29 19:15:57  cvsjoko | 
| 28 | 
  | 
  | 
//    + new checkin | 
| 29 | 
  | 
  | 
// | 
| 30 | 
  | 
  | 
//    Revision 1.4  2002/10/26 18:24:54  cvsmax | 
| 31 | 
  | 
  | 
//    + add note to care about UTC! | 
| 32 | 
  | 
  | 
// | 
| 33 | 
  | 
  | 
//    Revision 1.3  2002/10/25 12:05:30  cvsmax | 
| 34 | 
  | 
  | 
//    + bugfix: some db related functions wasn`t updated to new structure, done. | 
| 35 | 
  | 
  | 
//    + bugfix: reworked function validate_idle() | 
| 36 | 
  | 
  | 
// | 
| 37 | 
  | 
  | 
//    Revision 1.2  2002/10/17 03:37:40  cvsmax | 
| 38 | 
  | 
  | 
//    - removed unused function cont_auth() | 
| 39 | 
  | 
  | 
// | 
| 40 | 
  | 
  | 
//    Revision 1.1  2002/10/10 02:53:55  cvsmax | 
| 41 | 
  | 
  | 
//    + create new partly from old 'func_main.inc' | 
| 42 | 
  | 
  | 
// | 
| 43 | 
  | 
  | 
// | 
| 44 | 
  | 
  | 
//    ----------------------------------------------------------------------------- | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
 | 
| 47 | 
  | 
  | 
function get_date_forDisplay($arg_date) { | 
| 48 | 
  | 
  | 
  $date = new Date($arg_date); | 
| 49 | 
  | 
  | 
  // TODO: | 
| 50 | 
  | 
  | 
  // - determine this constant by asking $site->user->country, i believe we'll need some kind of mapping here | 
| 51 | 
  | 
  | 
  // - or can it be integrated with TimeZone? no, it's a locale setting (localization/preferences), isn't it? | 
| 52 | 
  | 
  | 
  return $date->getDate( DATE_FORMAT_GERMAN_SHORT ); | 
| 53 | 
  | 
  | 
} | 
| 54 | 
  | 
  | 
 | 
| 55 | 
  | 
  | 
function get_date_forStorage($arg_date) { | 
| 56 | 
  | 
  | 
 | 
| 57 | 
  | 
  | 
  // was: | 
| 58 | 
  | 
  | 
  //$unixtime = mktime(0, 0, 0, $arg_date[1], $arg_date[0], $arg_date[2]); | 
| 59 | 
  | 
  | 
  //return date("Y-m-d", $unixtime);       // english format | 
| 60 | 
  | 
  | 
 | 
| 61 | 
  | 
  | 
  // if $arg_date is an array, it is in "german"-format (really in locale format, isn't it?) | 
| 62 | 
  | 
  | 
  // TODO: check this! (do a generic reverse-map!) | 
| 63 | 
  | 
  | 
  if (is_array($arg_date)) { | 
| 64 | 
  | 
  | 
    $arg_date = join('.', $arg_date); | 
| 65 | 
  | 
  | 
  } | 
| 66 | 
  | 
  | 
  $date = new Date($arg_date); | 
| 67 | 
  | 
  | 
  $date_res = $date->getDate( DATE_FORMAT_ENGLISH_SHORT ); | 
| 68 | 
  | 
  | 
  return $date_res; | 
| 69 | 
  | 
  | 
} | 
| 70 | 
  | 
  | 
 | 
| 71 | 
  | 
  | 
// ------------------------------------------------- | 
| 72 | 
joko | 
1.3 | 
// array initializer | 
| 73 | 
joko | 
1.1 | 
// initializes passed variable (taken by reference) with an empty array | 
| 74 | 
  | 
  | 
// does this only if it doesn't exist yet or initialization is explicitely requested ($clear = 1) | 
| 75 | 
joko | 
1.3 | 
function array_init(&$var, $clear = 0) { | 
| 76 | 
joko | 
1.1 | 
  if (!is_array($var) || $clear) { $var = array(); } | 
| 77 | 
  | 
  | 
} | 
| 78 | 
  | 
  | 
   | 
| 79 | 
  | 
  | 
 | 
| 80 | 
  | 
  | 
// format functions | 
| 81 | 
  | 
  | 
function isvalidemailaddress($mail) { | 
| 82 | 
jonen | 
1.4 | 
  // TODO: review for regex process load | 
| 83 | 
joko | 
1.1 | 
  //return ((($string = trim($string)) != '') && (($atpos = strpos($string, '@' , 1)) > 1) && (($rdotpos = strrpos($string, '.')) > ($atpos + 2)) && (strlen($string) > $rdotpos + 2)); | 
| 84 | 
  | 
  | 
  if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@([a-z0-9]+([\.-][a-z0-9]+))*$",$mail, $regs) ) { | 
| 85 | 
  | 
  | 
        return "format"; | 
| 86 | 
  | 
  | 
  } | 
| 87 | 
  | 
  | 
  elseif( gethostbyname($regs[2]) == $regs[2] ) { | 
| 88 | 
  | 
  | 
    return "host"; | 
| 89 | 
  | 
  | 
  } | 
| 90 | 
  | 
  | 
  return "valid"; | 
| 91 | 
  | 
  | 
} | 
| 92 | 
  | 
  | 
 | 
| 93 | 
joko | 
1.6 | 
function Dumper() { | 
| 94 | 
joko | 
1.1 | 
  ob_start(); | 
| 95 | 
joko | 
1.6 | 
  $arg_list = func_get_args(); | 
| 96 | 
  | 
  | 
  $count = 1; | 
| 97 | 
  | 
  | 
  foreach ($arg_list as $arg) { | 
| 98 | 
joko | 
1.7 | 
    //print "<hr/>"; | 
| 99 | 
joko | 
1.6 | 
    print_r($arg); | 
| 100 | 
  | 
  | 
    $count++; | 
| 101 | 
  | 
  | 
  } | 
| 102 | 
joko | 
1.1 | 
  $var_dump = ob_get_contents(); | 
| 103 | 
  | 
  | 
  ob_end_clean(); | 
| 104 | 
  | 
  | 
  $var_dump = str_replace("\n", "<br>", $var_dump); | 
| 105 | 
  | 
  | 
  $var_dump = str_replace(" ", " ", $var_dump); | 
| 106 | 
  | 
  | 
  return $var_dump; | 
| 107 | 
  | 
  | 
} | 
| 108 | 
  | 
  | 
 | 
| 109 | 
  | 
  | 
function session_register_safe($varname) { | 
| 110 | 
  | 
  | 
  if (!session_is_registered($varname)) { | 
| 111 | 
  | 
  | 
    session_register($varname); | 
| 112 | 
  | 
  | 
    return 1; | 
| 113 | 
  | 
  | 
  } | 
| 114 | 
  | 
  | 
} | 
| 115 | 
  | 
  | 
 | 
| 116 | 
  | 
  | 
//------------ getMonths --------------------- | 
| 117 | 
  | 
  | 
// TODO: localize! | 
| 118 | 
  | 
  | 
function getMonthNames() { | 
| 119 | 
  | 
  | 
   for ($i=1;$i<=12;$i++) { | 
| 120 | 
  | 
  | 
     $utime = strtotime("2001-$i-24 12:00:00"); | 
| 121 | 
  | 
  | 
     $name = date("F",$utime); | 
| 122 | 
  | 
  | 
     $months[$i] = $name; | 
| 123 | 
  | 
  | 
   } | 
| 124 | 
  | 
  | 
   return $months; | 
| 125 | 
  | 
  | 
} | 
| 126 | 
  | 
  | 
 | 
| 127 | 
  | 
  | 
function null2nbsp_string($arg) { | 
| 128 | 
  | 
  | 
  if ($arg == '') { | 
| 129 | 
  | 
  | 
    $arg = ' '; | 
| 130 | 
  | 
  | 
  } | 
| 131 | 
  | 
  | 
  return $arg; | 
| 132 | 
  | 
  | 
} | 
| 133 | 
  | 
  | 
 | 
| 134 | 
  | 
  | 
function null2nbsp_arraywalker(&$item, $key) { | 
| 135 | 
  | 
  | 
    $item = null2nbsp_string($item); | 
| 136 | 
  | 
  | 
} | 
| 137 | 
  | 
  | 
 | 
| 138 | 
  | 
  | 
function null2nbsp($arg) { | 
| 139 | 
  | 
  | 
  if (is_array($arg)) { | 
| 140 | 
  | 
  | 
    array_walk($arg, 'null2nbsp_arraywalker'); | 
| 141 | 
  | 
  | 
    $result = $arg; | 
| 142 | 
  | 
  | 
  } else { | 
| 143 | 
  | 
  | 
    $result = null2nbsp_string($arg); | 
| 144 | 
  | 
  | 
  } | 
| 145 | 
  | 
  | 
  return $result; | 
| 146 | 
  | 
  | 
} | 
| 147 | 
  | 
  | 
 | 
| 148 | 
  | 
  | 
//------------ en- and decode Birthdate ----------- | 
| 149 | 
  | 
  | 
function getEncodedBirthdate($birthdate) { | 
| 150 | 
  | 
  | 
  print "birthday: $birthdate<br>"; | 
| 151 | 
  | 
  | 
  if($birthdate) { | 
| 152 | 
  | 
  | 
    $utime = strtotime($birthdate); | 
| 153 | 
  | 
  | 
    print "utime: $utime<br>"; | 
| 154 | 
  | 
  | 
    $birthday = array ( date("d",$utime), date("m",$utime), date("Y",$utime) ); | 
| 155 | 
  | 
  | 
  } | 
| 156 | 
  | 
  | 
  else { | 
| 157 | 
  | 
  | 
    $birthday = array('', '', '1960'); | 
| 158 | 
  | 
  | 
  } | 
| 159 | 
  | 
  | 
  return $birthday; | 
| 160 | 
  | 
  | 
} | 
| 161 | 
jonen | 
1.4 | 
 | 
| 162 | 
  | 
  | 
function number_format_storage($string) { | 
| 163 | 
  | 
  | 
    $locale_info = localeconv(); | 
| 164 | 
  | 
  | 
    $string =  str_replace($locale_info["decimal_point"], ".",  $string); | 
| 165 | 
  | 
  | 
    return $string; | 
| 166 | 
  | 
  | 
} | 
| 167 | 
  | 
  | 
 | 
| 168 | 
  | 
  | 
function number_format_locale1($string) { | 
| 169 | 
  | 
  | 
    $locale_info = localeconv(); | 
| 170 | 
  | 
  | 
    $string =  str_replace(".", $locale_info["decimal_point"],  $string); | 
| 171 | 
  | 
  | 
    return $string; | 
| 172 | 
  | 
  | 
} | 
| 173 | 
  | 
  | 
 | 
| 174 | 
  | 
  | 
 | 
| 175 | 
  | 
  | 
function number_format_locale ($num, $decimals = null) { | 
| 176 | 
  | 
  | 
   $locale = localeconv(); | 
| 177 | 
  | 
  | 
   if(!isset($decimals)) $decimals = $locale['frac_digits']; | 
| 178 | 
  | 
  | 
   return number_format($num, $decimals, $locale['decimal_point'], $locale['thousands_sep']); | 
| 179 | 
  | 
  | 
} | 
| 180 | 
  | 
  | 
 | 
| 181 | 
  | 
  | 
 | 
| 182 | 
  | 
  | 
function is_amount($amount) { | 
| 183 | 
  | 
  | 
  //if( (number_format_locale((string)doubleval($amount)) === number_format_locale((string)$amount)) && number_format_locale((doubleval($amount)) >number_format_locale(0)) ) { | 
| 184 | 
  | 
  | 
  if( ((string)doubleval($amount) === (string)$amount) && (doubleval($amount) > 0) ) { | 
| 185 | 
  | 
  | 
    return 1; | 
| 186 | 
  | 
  | 
  } | 
| 187 | 
  | 
  | 
} | 
| 188 | 
joko | 
1.5 | 
 | 
| 189 | 
  | 
  | 
 | 
| 190 | 
  | 
  | 
// from: php.net - http://www.php.net/manual/en/function.array-merge-recursive.php | 
| 191 | 
  | 
  | 
function is_hash( $var ) {  | 
| 192 | 
  | 
  | 
 if( is_array( $var ) ) {  | 
| 193 | 
  | 
  | 
   $keys = array_keys( $var );  | 
| 194 | 
  | 
  | 
   $all_num = true;  | 
| 195 | 
  | 
  | 
   for( $i=0; $i<count($keys); $i++ )  | 
| 196 | 
  | 
  | 
     if( is_string($keys[$i] ) ) return true;  | 
| 197 | 
  | 
  | 
 }  | 
| 198 | 
  | 
  | 
 return false;  | 
| 199 | 
  | 
  | 
}  | 
| 200 | 
  | 
  | 
 | 
| 201 | 
  | 
  | 
// from: php.net - http://www.php.net/manual/en/function.array-merge-recursive.php | 
| 202 | 
  | 
  | 
function array_join_merge( $arr1, $arr2 ) {  | 
| 203 | 
  | 
  | 
 if( is_array( $arr1 ) and is_array( $arr2 ) ) {  | 
| 204 | 
  | 
  | 
   // the same -> merge  | 
| 205 | 
  | 
  | 
   $new_array = array();  | 
| 206 | 
  | 
  | 
 | 
| 207 | 
  | 
  | 
   if( is_hash( $arr1 ) and is_hash( $arr2 ) ) {  | 
| 208 | 
  | 
  | 
    // hashes -> merge based on keys  | 
| 209 | 
  | 
  | 
     $keys = array_merge( array_keys( $arr1 ), array_keys( $arr2 ) );  | 
| 210 | 
  | 
  | 
     foreach( $keys as $key ) {  | 
| 211 | 
  | 
  | 
$new_array[$key] = array_join_merge( $arr1[$key], $arr2[$key] );  | 
| 212 | 
  | 
  | 
     }  | 
| 213 | 
  | 
  | 
   } else {  | 
| 214 | 
  | 
  | 
     // two real arrays -> merge  | 
| 215 | 
  | 
  | 
     $new_array =  | 
| 216 | 
  | 
  | 
array_reverse(array_unique(array_reverse(array_merge($arr1,$arr2))));  | 
| 217 | 
  | 
  | 
  }  | 
| 218 | 
  | 
  | 
     | 
| 219 | 
  | 
  | 
   return $new_array;  | 
| 220 | 
  | 
  | 
 } else {  | 
| 221 | 
  | 
  | 
   // not the same ... take new one if defined, else the old one stays  | 
| 222 | 
  | 
  | 
   return $arr2 ? $arr2 : $arr1;  | 
| 223 | 
  | 
  | 
 }  | 
| 224 | 
  | 
  | 
}  | 
| 225 | 
joko | 
1.1 | 
 | 
| 226 | 
  | 
  | 
?> |