<?php
##
## this file name is 'func.misc.php'
##
## misc functions
##
## [author]
##  - Chilbong Kim<san2(at)linuxchannel.net>
##
## [changes]
##  - 2003.06.14 : add ip2base(), base2ip()
##  - 2003.04.14 : new build
##
## [references]
##
## [usage]
##
## [example]
##

function get_microtime($_start$_end)
{
  
$end explode(' '$_end);
  
$start explode(' '$_start);

  return 
sprintf('%.4f', ($end[1] + $end[0]) - ($start[1] + $start[0]));
}

function 
print_microtime($_start$_end)
{
  global 
$_PHPA;

  
$time[total] = get_microtime($_start,$_end);
  
$time[sql]   = get_microtime(__SQLSTART__,__SQLEND__);
  
$time[tmpl]  = get_microtime(__TMPLSTART__,$_end);
  
$time[init]  = $time[total] - $time[sql] - $time[tmpl];
  
$phpa '(PHPA '.($_PHPA[ENABLED] ? 'on' : ($_PHPA 'off' 'not install')).
    
' '.$_PHPA[VERSION].')';

  echo <<<__EOP__
  \n<P><PRE>
   initial read = $time
[init]
   sql get time = $time
[sql]
   tmpl parsing = $time
[tmpl]
  total execute = $time
[total] seconds $phpa
  </PRE>
__EOP__;
}

function 
ip2base($ipv4='')
{
  global 
$_SERVER;

  if(!
$ipv4$ipv4 $_SERVER['SERVER_ADDR'];

  
//$i = explode('.',$ipv4);
  //return ($i[0]*16777216 + $i[1]*65536 + $i[2]*256 + $i[3]);

  
return sprintf('%u',ip2long($ipv4));
}

function 
base2ip($base)
{
  
/***
  $a = ($base / 16777216) % 256;
  $b = ($base / 65536) % 256;
  $multi = floor($base / 256);
  $c = $multi % 256;
  $d = $base - ($multi * 256); // or fmod($base,256)
  return $a.'.'.$b.'.'.$c.'.'.$d;
  ***/

  
return long2ip($base);
}

## http://dbforums.com/arch/150/2002/7/424177
##
function mysql_password($pass)
{
  
$nr 1345345333;
  
$add 7;
  
$nr2 0x12345671;
  
$size strlen($pass);

  for(
$i=0;$i<$size;$i++)
  {
    
/* skipp space in password */
    
if($pass[$i] == ' ' || $pass[$i] == '\t') continue;
    
$tmp ord($pass[$i]);
    
$nr ^= ((($nr 63)+$add)*$tmp) + ($nr << 8);
    
$nr2 += ($nr2 << 8) ^ $nr;
    
$add += $tmp;
  }

  
$result1 $nr & ((<< 31) -1); /* Don't use sign bit (str2int) */
  
$result2 $nr2 & ((<< 31) -1);
  
$result sprintf('%08x%08x',$result1,$result2);

  return 
$result;
}

?>