| 1 | 
joko | 
1.1 | 
<? | 
| 2 | 
  | 
  | 
//    ----------------------------------------------------------------------------- | 
| 3 | 
jonen | 
1.9 | 
//    $Id: helper.php,v 1.8 2003/03/03 21:21:43 joko Exp $ | 
| 4 | 
joko | 
1.1 | 
//    ----------------------------------------------------------------------------- | 
| 5 | 
  | 
  | 
//    $Log: helper.php,v $ | 
| 6 | 
jonen | 
1.9 | 
//    Revision 1.8  2003/03/03 21:21:43  joko | 
| 7 | 
  | 
  | 
//    refactored most reusable code to org.netfrag.glib/php_extensions.php | 
| 8 | 
  | 
  | 
// | 
| 9 | 
joko | 
1.8 | 
//    Revision 1.7  2003/03/01 05:05:55  joko | 
| 10 | 
  | 
  | 
//    minor modification to 'Dumper' | 
| 11 | 
  | 
  | 
// | 
| 12 | 
joko | 
1.7 | 
//    Revision 1.6  2003/02/09 17:49:53  joko | 
| 13 | 
  | 
  | 
//    - removed old code | 
| 14 | 
  | 
  | 
// | 
| 15 | 
joko | 
1.6 | 
//    Revision 1.5  2003/02/04 08:22:34  joko | 
| 16 | 
  | 
  | 
//    + function is_hash | 
| 17 | 
  | 
  | 
//    + function array_join_merge | 
| 18 | 
  | 
  | 
// | 
| 19 | 
joko | 
1.5 | 
//    Revision 1.4  2002/12/22 13:29:54  jonen | 
| 20 | 
  | 
  | 
//    + added number format helper functions: | 
| 21 | 
  | 
  | 
//      + number_format_locale() | 
| 22 | 
  | 
  | 
//      + is_amount() | 
| 23 | 
  | 
  | 
// | 
| 24 | 
jonen | 
1.4 | 
//    Revision 1.3  2002/12/11 01:00:22  joko | 
| 25 | 
  | 
  | 
//    + renamed function 'sai' to 'array_init' | 
| 26 | 
  | 
  | 
// | 
| 27 | 
joko | 
1.3 | 
//    Revision 1.2  2002/12/01 04:49:54  joko | 
| 28 | 
  | 
  | 
//    + function Dumper (from function dumpVar) | 
| 29 | 
  | 
  | 
// | 
| 30 | 
joko | 
1.2 | 
//    Revision 1.1  2002/11/12 05:42:31  joko | 
| 31 | 
  | 
  | 
//    + initial checkin | 
| 32 | 
  | 
  | 
// | 
| 33 | 
joko | 
1.1 | 
//    Revision 1.1  2002/10/29 19:15:57  cvsjoko | 
| 34 | 
  | 
  | 
//    + new checkin | 
| 35 | 
  | 
  | 
// | 
| 36 | 
  | 
  | 
//    Revision 1.4  2002/10/26 18:24:54  cvsmax | 
| 37 | 
  | 
  | 
//    + add note to care about UTC! | 
| 38 | 
  | 
  | 
// | 
| 39 | 
  | 
  | 
//    Revision 1.3  2002/10/25 12:05:30  cvsmax | 
| 40 | 
  | 
  | 
//    + bugfix: some db related functions wasn`t updated to new structure, done. | 
| 41 | 
  | 
  | 
//    + bugfix: reworked function validate_idle() | 
| 42 | 
  | 
  | 
// | 
| 43 | 
  | 
  | 
//    Revision 1.2  2002/10/17 03:37:40  cvsmax | 
| 44 | 
  | 
  | 
//    - removed unused function cont_auth() | 
| 45 | 
  | 
  | 
// | 
| 46 | 
  | 
  | 
//    Revision 1.1  2002/10/10 02:53:55  cvsmax | 
| 47 | 
  | 
  | 
//    + create new partly from old 'func_main.inc' | 
| 48 | 
  | 
  | 
// | 
| 49 | 
  | 
  | 
// | 
| 50 | 
  | 
  | 
//    ----------------------------------------------------------------------------- | 
| 51 | 
  | 
  | 
 | 
| 52 | 
  | 
  | 
 | 
| 53 | 
  | 
  | 
function get_date_forDisplay($arg_date) { | 
| 54 | 
  | 
  | 
  $date = new Date($arg_date); | 
| 55 | 
  | 
  | 
  // TODO: | 
| 56 | 
  | 
  | 
  // - determine this constant by asking $site->user->country, i believe we'll need some kind of mapping here | 
| 57 | 
  | 
  | 
  // - or can it be integrated with TimeZone? no, it's a locale setting (localization/preferences), isn't it? | 
| 58 | 
  | 
  | 
  return $date->getDate( DATE_FORMAT_GERMAN_SHORT ); | 
| 59 | 
  | 
  | 
} | 
| 60 | 
  | 
  | 
 | 
| 61 | 
  | 
  | 
function get_date_forStorage($arg_date) { | 
| 62 | 
  | 
  | 
 | 
| 63 | 
  | 
  | 
  // was: | 
| 64 | 
  | 
  | 
  //$unixtime = mktime(0, 0, 0, $arg_date[1], $arg_date[0], $arg_date[2]); | 
| 65 | 
  | 
  | 
  //return date("Y-m-d", $unixtime);       // english format | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
  // if $arg_date is an array, it is in "german"-format (really in locale format, isn't it?) | 
| 68 | 
  | 
  | 
  // TODO: check this! (do a generic reverse-map!) | 
| 69 | 
  | 
  | 
  if (is_array($arg_date)) { | 
| 70 | 
  | 
  | 
    $arg_date = join('.', $arg_date); | 
| 71 | 
  | 
  | 
  } | 
| 72 | 
  | 
  | 
  $date = new Date($arg_date); | 
| 73 | 
  | 
  | 
  $date_res = $date->getDate( DATE_FORMAT_ENGLISH_SHORT ); | 
| 74 | 
  | 
  | 
  return $date_res; | 
| 75 | 
  | 
  | 
} | 
| 76 | 
  | 
  | 
 | 
| 77 | 
  | 
  | 
   | 
| 78 | 
  | 
  | 
 | 
| 79 | 
  | 
  | 
// format functions | 
| 80 | 
joko | 
1.8 | 
// refactor to org.netfrag.glib::php??? | 
| 81 | 
  | 
  | 
// refactor to Data::Validator??? | 
| 82 | 
joko | 
1.1 | 
function isvalidemailaddress($mail) { | 
| 83 | 
jonen | 
1.4 | 
  // TODO: review for regex process load | 
| 84 | 
joko | 
1.1 | 
  //return ((($string = trim($string)) != '') && (($atpos = strpos($string, '@' , 1)) > 1) && (($rdotpos = strrpos($string, '.')) > ($atpos + 2)) && (strlen($string) > $rdotpos + 2)); | 
| 85 | 
  | 
  | 
  if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@([a-z0-9]+([\.-][a-z0-9]+))*$",$mail, $regs) ) { | 
| 86 | 
  | 
  | 
        return "format"; | 
| 87 | 
  | 
  | 
  } | 
| 88 | 
jonen | 
1.9 | 
  elseif( gethostbynamel($regs[2]) == $regs[2] ) { | 
| 89 | 
joko | 
1.1 | 
    return "host"; | 
| 90 | 
  | 
  | 
  } | 
| 91 | 
  | 
  | 
  return "valid"; | 
| 92 | 
  | 
  | 
} | 
| 93 | 
  | 
  | 
 | 
| 94 | 
  | 
  | 
 | 
| 95 | 
  | 
  | 
//------------ getMonths --------------------- | 
| 96 | 
  | 
  | 
// TODO: localize! | 
| 97 | 
  | 
  | 
function getMonthNames() { | 
| 98 | 
  | 
  | 
   for ($i=1;$i<=12;$i++) { | 
| 99 | 
  | 
  | 
     $utime = strtotime("2001-$i-24 12:00:00"); | 
| 100 | 
  | 
  | 
     $name = date("F",$utime); | 
| 101 | 
  | 
  | 
     $months[$i] = $name; | 
| 102 | 
  | 
  | 
   } | 
| 103 | 
  | 
  | 
   return $months; | 
| 104 | 
  | 
  | 
} | 
| 105 | 
  | 
  | 
 | 
| 106 | 
joko | 
1.8 | 
 | 
| 107 | 
  | 
  | 
// refactor to Data::Lift??? | 
| 108 | 
joko | 
1.1 | 
function null2nbsp_string($arg) { | 
| 109 | 
  | 
  | 
  if ($arg == '') { | 
| 110 | 
  | 
  | 
    $arg = ' '; | 
| 111 | 
  | 
  | 
  } | 
| 112 | 
  | 
  | 
  return $arg; | 
| 113 | 
  | 
  | 
} | 
| 114 | 
  | 
  | 
 | 
| 115 | 
joko | 
1.8 | 
// refactor to Data::Lift??? | 
| 116 | 
joko | 
1.1 | 
function null2nbsp_arraywalker(&$item, $key) { | 
| 117 | 
  | 
  | 
    $item = null2nbsp_string($item); | 
| 118 | 
  | 
  | 
} | 
| 119 | 
  | 
  | 
 | 
| 120 | 
joko | 
1.8 | 
// refactor to Data::Lift??? | 
| 121 | 
joko | 
1.1 | 
function null2nbsp($arg) { | 
| 122 | 
  | 
  | 
  if (is_array($arg)) { | 
| 123 | 
  | 
  | 
    array_walk($arg, 'null2nbsp_arraywalker'); | 
| 124 | 
  | 
  | 
    $result = $arg; | 
| 125 | 
  | 
  | 
  } else { | 
| 126 | 
  | 
  | 
    $result = null2nbsp_string($arg); | 
| 127 | 
  | 
  | 
  } | 
| 128 | 
  | 
  | 
  return $result; | 
| 129 | 
  | 
  | 
} | 
| 130 | 
  | 
  | 
 | 
| 131 | 
  | 
  | 
//------------ en- and decode Birthdate ----------- | 
| 132 | 
  | 
  | 
function getEncodedBirthdate($birthdate) { | 
| 133 | 
  | 
  | 
  print "birthday: $birthdate<br>"; | 
| 134 | 
  | 
  | 
  if($birthdate) { | 
| 135 | 
  | 
  | 
    $utime = strtotime($birthdate); | 
| 136 | 
  | 
  | 
    print "utime: $utime<br>"; | 
| 137 | 
  | 
  | 
    $birthday = array ( date("d",$utime), date("m",$utime), date("Y",$utime) ); | 
| 138 | 
  | 
  | 
  } | 
| 139 | 
  | 
  | 
  else { | 
| 140 | 
  | 
  | 
    $birthday = array('', '', '1960'); | 
| 141 | 
  | 
  | 
  } | 
| 142 | 
  | 
  | 
  return $birthday; | 
| 143 | 
  | 
  | 
} | 
| 144 | 
jonen | 
1.4 | 
 | 
| 145 | 
  | 
  | 
function number_format_storage($string) { | 
| 146 | 
  | 
  | 
    $locale_info = localeconv(); | 
| 147 | 
  | 
  | 
    $string =  str_replace($locale_info["decimal_point"], ".",  $string); | 
| 148 | 
  | 
  | 
    return $string; | 
| 149 | 
  | 
  | 
} | 
| 150 | 
  | 
  | 
 | 
| 151 | 
  | 
  | 
function number_format_locale1($string) { | 
| 152 | 
  | 
  | 
    $locale_info = localeconv(); | 
| 153 | 
  | 
  | 
    $string =  str_replace(".", $locale_info["decimal_point"],  $string); | 
| 154 | 
  | 
  | 
    return $string; | 
| 155 | 
  | 
  | 
} | 
| 156 | 
  | 
  | 
 | 
| 157 | 
  | 
  | 
 | 
| 158 | 
  | 
  | 
function number_format_locale ($num, $decimals = null) { | 
| 159 | 
  | 
  | 
   $locale = localeconv(); | 
| 160 | 
jonen | 
1.9 | 
   //if(!isset($decimals)) $decimals = $locale['frac_digits']; | 
| 161 | 
  | 
  | 
   if(!isset($decimals)) $decimals = 2; | 
| 162 | 
jonen | 
1.4 | 
   return number_format($num, $decimals, $locale['decimal_point'], $locale['thousands_sep']); | 
| 163 | 
  | 
  | 
} | 
| 164 | 
  | 
  | 
 | 
| 165 | 
  | 
  | 
 | 
| 166 | 
joko | 
1.8 | 
// refactor to org.netfrag.glib::php??? | 
| 167 | 
  | 
  | 
// rename to 'check_amount'? | 
| 168 | 
  | 
  | 
// move to Data::Validator??? | 
| 169 | 
jonen | 
1.4 | 
function is_amount($amount) { | 
| 170 | 
  | 
  | 
  //if( (number_format_locale((string)doubleval($amount)) === number_format_locale((string)$amount)) && number_format_locale((doubleval($amount)) >number_format_locale(0)) ) { | 
| 171 | 
  | 
  | 
  if( ((string)doubleval($amount) === (string)$amount) && (doubleval($amount) > 0) ) { | 
| 172 | 
  | 
  | 
    return 1; | 
| 173 | 
  | 
  | 
  } | 
| 174 | 
  | 
  | 
} | 
| 175 | 
joko | 
1.5 | 
 | 
| 176 | 
jonen | 
1.9 | 
function format_amount(&$amount) { | 
| 177 | 
  | 
  | 
  $amount = str_replace(array(".", ","), array("", "."), $amount); | 
| 178 | 
  | 
  | 
  //print "streplace amount: '$amount'<br>"; | 
| 179 | 
  | 
  | 
  //return $amount; | 
| 180 | 
  | 
  | 
} | 
| 181 | 
joko | 
1.8 | 
 | 
| 182 | 
joko | 
1.1 | 
 | 
| 183 | 
  | 
  | 
?> |