<?php
##  ½Ã½ºÅÛ Á¤º¸ Ãâ·Â #############################################
##
##  ÀÛ¼ºÀÚ    : ±èÄ¥ºÀ[´Ð:»êÀÌ] <san2(at)linuxchannel.net>
##  ½ºÅ©¸³Æ® ¸í : PHP¸¦ ÀÌ¿ëÇÑ ½Ã½ºÅÛ Á¤º¸¸¦ Ãâ·ÂÇϴ ½ºÅ©¸³Æ®
##
#############################################################
##
## ÁÖ)
## »ç¿ë»ó ºÎÁÖÀǷΠÀÎÇÑ ÇÇÇØ´Â
## º» ÀÛ¼ºÀÚ¿¡°Ô ¾î¶°ÇÑ º¸ÁõÀ̳ª Ã¥ÀÓÀÌ ¾ø½À´Ï´Ù.
##
###############################################################

class utils
{
  
## check $TMPL['config']
  ##
  
function check_config($tmpl$config$post)
  {
    global 
$_SERVER;

    if(
$config == 'select'$key $post $post 'default';
    else 
$key $config;

    if(!
trim($tmpl[$key])) $key 'default';

    
$c['config'] = $key;
    if(
$config == 'select')
    {
        
$select[$key] = ' SELECTED';
        foreach(
$tmpl AS $k=>$v$form .= "<OPTION VALUE='$k'$select[$k]>$v\n";
        
$c['form'] = "\n".
        
'<FONT SIZE=2>'.
        
"<FORM METHOD='post' ACTION='$_SERVER[_PHP_SELF]'>\n".
        
"<SELECT NAME='utmpl'>\n$form".
        
'</SELECT>'."\n".
        
"&nbsp;<INPUT TYPE='submit' VALUE='Å׸¶Àû¿ë'>\n".
        
'</FORM>'.
        
'</FONT>'."\n";
    }

    return 
$c;
  }

  
## get tmplates file
  ##
  
function get_tmpl($file)
  {
    global 
$TMPL$_SERVER;

    
$content $this->get_file($_SERVER['_PWD'].'/templates/'.$TMPL['config'].'/'.$file);

    
$from = array('["','"]','"'); // for javascript error
    
$to = array("['","']",'\"');
    
$content str_replace($from,$to,$content);

    @eval(
'$content = "'.$content.'";');

    return 
$content;
  }

  
## get block tmplates file
  ## $from = array(), $to = array();
  ##
  
function get_block($from,$to,$content)
  {
    global 
$TMPL;

    
$content str_replace($from,$to,$content);

    
$from = array('["','"]','"'); // for javascript error
    
$to = array("['","']",'\"');
    
$content str_replace($from,$to,$content);

    @eval(
'$content = "'.$content.'";');

    return 
$content;
  }

  function 
get_width($width)
  {
    
$width *= 2// Å©±â·Î 2¹è·Î Á¤ÇÔ
    
if(!$width$width 1// ±âº»°ª ¼³Á¤
    
if($width == 200$width 199;

    return 
$width;
  }

  
## ÀÚ¹Ù ½ºÅ©¸³Æ® window.status A Å±×
  ##
  
function tag_a($str,$link$target=''$winstatus='')
  {
    if(
$winstatus)
    {
        
$winstatus str_replace("'","\'",$winstatus);
        
$status ' onMouseOver="window.status=\''.$winstatus.'\'; return true;" '.
        
'onMouseOut="window.status=\'\'; return true;"';
    }
    if(
$target$target " target='$target'";

    return 
"<A HREF='$link'$target$status>$str</A>";
  }

  function 
html_table($args='border="0" spacing="0" padding="0" width="100%"')
  {
    
$args strtoupper($args);
    return 
"\n<TABLE $args>\n";
  }

  
## if you using the PHP/4.0.x
  ## http://www.php.net/ChangeLog-4.php
  ##
  
function _globals()
  {
    global 
$_SERVER// add user vars for 4.0.x

    
if(preg_match('/^4\.0/',PHP_VERSION))
    {
        
$GLOBALS['_SERVER']  = array_merge($GLOBALS['HTTP_SERVER_VARS'],$_SERVER);
        
$GLOBALS['_GET']     = $GLOBALS['HTTP_GET_VARS'];
        
$GLOBALS['_POST']    = $GLOBALS['HTTP_POST_VARS'];
        
$GLOBALS['_COOKIE']  = $GLOBALS['HTTP_COOKIE_VARS'];
        
$GLOBALS['_SESSION'] = $GLOBALS['HTTP_SESSION_VARS'];
        
$GLOBALS['_ENV']     = $GLOBALS['HTTP_ENV_VARS'];
        
$GLOBALS['_FILES']   = $GLOBALS['HTTP_POST_FILES']; // ???
        
$GLOBALS['_REQUEST'] = array_merge($_GET,$_COOKIE);
    }
  }

  
## get $_SERVER['_URI'] and $_SERVER['_PHP_SELF'] (for includes)
  ##
  
function realuri($_PWD)
  {
    global 
$_SERVER// add user vars for 4.0.x

    
$GLOBALS['_SERVER']['_PWD'] = $_PWD realpath($_PWD); // it's override
  
    ## for system account user
    ## 
    
if(preg_match(';^/~;',$_SERVER['REQUEST_URI']))
    {
        
$_S['_SNAME'] = realpath($_SERVER['SCRIPT_FILENAME']);
        
$_S['_FNAME'] = preg_replace(';^/~[^/]+;','',$_SERVER['SCRIPT_NAME']);
        
$_S['_UURI'] = str_replace($_S['_FNAME'],'',$_SERVER['SCRIPT_NAME']);
        
$_S['_DOCUMENT_ROOT'] = str_replace($_S['_FNAME'],'',$_S['_SNAME']);
        
$_S['_DOCUMENT_PATH'] = str_replace($_S['_DOCUMENT_ROOT'],'',$_PWD);
        
$_URI $_S['_UURI'] . $_S['_DOCUMENT_PATH'];
    } else
    {
        if(
preg_match(";^$_SERVER[DOCUMENT_ROOT];",$_PWD))
        {
            
$_URI str_replace($_SERVER['DOCUMENT_ROOT'],'',$_PWD);
        } else
        {
            
$_URI str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['PATH_TRANSLATED']);
            
$_URI dirname($_URI);
        }
    }
    if(!
$_SERVER['_URI']) $GLOBALS['_SERVER']['_URI'] = $_URI// it's not override

    //$_SERVER['_PHP_SELF'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
    
$GLOBALS['_SERVER']['_PHP_SELF'] = $_SERVER['REQUEST_URI']; // includes this file at any path
  
}

  
## get machine
  ##
  
function get_machine()
  {
    if(
preg_match('/Linux/',PHP_OS))
    { return 
$this->get_machine_linux(); }

    else if(
preg_match('/FreeBSD/',PHP_OS))
    { return 
$this->get_machine_freebsd(); }

    else
    {
        echo 
'&nbsp;<P><H3>support only GNU/LINUX or FreeBSD/4.8 !!!</H3>';
        exit;
    }
  }

  
## check support OS and get OS/class type(machine)
  ## reference(archtable): /usr/src/linux/arch
  ##
  
function get_machine_linux()
  {
    @
exec('/bin/uname -srm 2>/dev/null',$m);

    if(
$m[0])
    { list(
$arch['s'],$arch['r'],$arch['m']) = preg_split('/\s+/',$m[0]); }
    else
    {
        @
exec('/bin/arch 2>/dev/null',$m);
        
$arch['m'] = $m[0];
    }
    if(!
$arch['m']) $arch['m'] = 'i686'// default arch mechine

    ## /usr/src/linux/Makefile
    ## alpha, arm, cris, i386, ia64, m68k, mips, mips64, parisc,
    ## ppc, ppc64, s390, s390x, sh, sparc, sparc64
    ##
    
$arch['t'] = preg_replace(array('/i.86/','/sun4u/','/(arm.*|sa110)/'),
        array(
'i386','sparc64','arm','arm'), $arch['m']);
 
    if(!
preg_match('/^(i386|s390)$/',$arch['t']))
    {
        echo 
"&nbsp;<P><H3>not support $arch[t] arcitecture !!!</H3>";
        exit;
    }

    return 
$arch// array
  
}

  
## add 2003.10.30
  ##
  
function get_machine_freebsd()
  {
    @
exec('/usr/bin/uname -srm 2>/dev/null',$m);

    if(
$m[0])
    { list(
$arch['s'],$arch['r'],$arch['m']) = preg_split('/\s+/',$m[0]); }
    else
    {
        @
exec('/sbin/sysctl -n kern.osrelease hw.machine_arch 2>/dev/null',$m);
        
$arch['s'] = 'FreeBSD';
        
$arch['r'] = $m[0];
        
$arch['m'] = $m[1];
    }

    
$arch['t'] = 'freebsd';

    return 
$arch;
  }

  
## replace exec()
  ##
  
function _exec($cmd, &$var)
  {
    if(
$fp = @popen($cmd,r))
    {
        while(!
feof($fp)) $var[] = fgets($fp,4096); // to array
        
pclose($fp);
    }

    return 
$var// also return $var array
  
}

  
## check PHPA(PHP Accelerator)
  ##
  
function get_phpa()
  {
    global 
$_PHPA;

    if(!
$_PHPA) return 0;
    
$_phpa['version'] = 'PHPA/' $_PHPA['VERSION'];
    
$_phpa['on'] = $_PHPA['ENABLED'] ? 'PHPA on' 'PHPA off';

    return 
$_phpa;
  }

  
## check parsing time
  ##
  
function get_microtime($_start$_end)
  {
    
$end explode(' '$_end);
    
$start explode(' '$_start);

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

  
## Direct IO access get file
  ## 2002.08.23
  ##
  
function get_diofile($file)
  {
    if(
$fp = @dio_open($file,O_RDONLY))
    {
        
$contents dio_read($fp,filesize($file));
        
dio_close($fp);
    }

    return 
$contents;
  }

  
## common get file
  ## 2002.08.23
  ##
  
function get_file($file)
  {
    if(!
file_exists($file)) return '';

    if(
function_exists(dio_open)) return $this->get_diofile($file);

    if(
$fp = @fopen($file,'r'))
    {
        
$contents fread($fp,filesize($file));
        
fclose($fp);
    }

    return 
$contents;
  }

  
## ÇöÀ砶Ǵ ƯÁ¤ÇÑ ³¯Â¥¸¦ Çѱ۷ΠÃâ·ÂÇϴ ÇÔ¼ö
  ## $sub°¡ ÀÖÀ» °æ¿ì ½Ã:ºÐ:ÃʷΠÃâ·ÂÇÑ´Ù.
  ##
  
function hdate_time($date=0$sub=0)
  {
    if(
$sub$str ' H:i:s';
    return 
date("Y³â m¿ù dÀÏ$str",$date $date time());
  }

  
## °¢ ¿äÀÏÀ» Çѱ۷ΠÃâ·ÂÇϴ ÇÔ¼ö
  ## $sub º¯¼ö°¡ ÀÖÀ» °æ¿ì ÀÚ¼¼ÇϰÔ(?) Ãâ·Â
  ## $date º¯¼ö´Â À¯´Ð½º timestamp °ªÀÌ´Ù.
  ##
  
function hday($date=0$sub=0)
  {
    
$hday = array('ÀÏ','¿ù','È­','¼ö','¸ñ','±Ý','Åä');
    
$key date('w', !$date time() : $date);

    return 
$hday[$key] . ($sub '¿äÀÏ' '');
  }

  
## ÇÏ·ç 24½Ã°£À» 12 ½Ã°£´ë·Î Ãâ·ÂÇϴ ÇÔ¼ö
  ##
  
function hday_12z($date=0)
  {
    
$date date('G'$date $date time());
    
$key intval($date/0.5);

    
$H12Z = array('õäãÁ(Ãà½Ã)','ìÙãÁ(ÀνÃ)','ÙÖãÁ(¹¦½Ã)','òããÁ(Áø½Ã)',
    
'ÞÓãÁ(»ç½Ã)','çíãÁ(¿À½Ã)','Ú±ãÁ(¹Ì½Ã)','ãéãÁ(½Å½Ã)','ë·ãÁ(À¯½Ã)',
    
'âùãÁ(¼ú½Ã)','ú¤ãÁ(ÇØ½Ã)','í­ãÁ(ÀÚ½Ã)','õäãÁ(Ãà½Ã)'); 

    return 
$H12Z[$key];
  }

  
## ÇÏ·ç 24½Ã°£À» »õº®/¿ÀÀü/¿ÀÈÄ/Àú³áÀÇ ½Ã°£´ë¸¦ Ãâ·ÂÇϴ ÇÔ¼ö
  ## $sub º¯¼ö°¡ ¾ø´Ù¸é ´Ü¼øÇϰԠ¿ÀÀü/¿ÀÈķΠÃâ·ÂÇÔ.
  ## $date º¯¼ö´Â À¯´Ð½º timestamp °ªÀÌ´Ù.
  ##
  
function hday_ap($date=0$sub=0)
  {
    if(
$sub$H = array('»õº®','¿ÀÀü','¿ÀÈÄ','Àú³á');
    else 
$H = array('¿ÀÀü','¿ÀÀü','¿ÀÈÄ','¿ÀÈÄ');

    
$key intval(date('G',$date $date time())/6);

    return 
$H[$key];
  }


  
## ÇöÀ砶Ǵ ƯÁ¤ÇÑ ³¯Â¥¸¦ Çѱ۷ΠÃâ·ÂÇϴ ÇÔ¼ö
  ## $sub°¡ ÀÖÀ» °æ¿ì ½Ã:ºÐ:ÃʷΠÃâ·ÂÇÑ´Ù.
  ##
  
function my_date($date=0)
  {
    if(!
$date$date time();

    return 
$this->hdate_time($date,1).' '.$this->hday($date).
        
'('.$this->hday_ap($date,1).')';
  }

  
## ÀÏÁ¤±â°£ÀÇ ½Ã°£(ÃÊ)¸¦ È޸ེÄÉÀϷΠº¯È¯
  ## 2002.11.14 ¾Ë°í¸®µë ¼öÁ¤
  ## 2001.02.07 ÀÏ Ãß°¡
  ##
  
function runtime($term$lang='kr')
  {
    
$l['kr'] = $l['ko'] = array('ÃÊ','ºÐ','½Ã°£','ÀÏ','´Þ');
    
$l['en'] = array('seconds','minutes','hours','days','months');

    
$months = (int)($term 2592000);
    
$term   = (int)($term 2592000);
    
$days   = (int)($term 86400);
    
$term   = (int)($term 86400);
    
$hours  = (int)($term 3600);
    
$term   = (int)($term 3600);
    
$mins   = (int)($term 60);
    
//$secs  = (int)($term % 60);

    
$months $months $months.$l[$lang][4].' ' '';
    
$days $days $days.$l[$lang][3].' ' '';
    
$hours $hours $hours.$l[$lang][2].' ' '';
    
$mins $mins $mins.$l[$lang][1] : '';
    
//$secs .= $l[$lang][0];

    
return $months.$days.$hours.$mins;
  }

  
## Æ¯Á¤ ÆÄÀÏÀÌ ¾ó¸¶µ¿¾È º¯È­µÇ¾î ÀÖ´ÂÁö °è»êÇϴ ÇÔ¼ö
  ## $arc´Â Æ¯Á¤ ÆÄÀÏÀÓ
  ## ½©¿¡¼­ ½Ã½ºÅÛÀÌ ¾ó¸¶µ¿¾È ÀÛµ¿Çϰí ÀÖ´ÂÁö ¾Ë¾Æº¸´Â ¸í·ÉÀº
  ## /usr/bin/uptime ÀÔ´Ï´Ù.
  ## ÀÛ¼ºÀÚ : ±èÄ¥ºÀ[´Ð:»êÀÌ] <san2(at)linxuchannel.net>
  ## ÀÛ¼ºÀÏ : 2001.01.11, ÇÔ¼öÀ̵¿
  ## 2001.02.07 ÀÏ Ãß°¡
  ##
  
function arc_runtime($arc$lang='kr')
  {
    global 
$_time;

    
## ½ÇÇàµÇ°í Àִ ÃÑ ½Ã°£À» ÃÊ(second)·Î È¯»ê
    ##
    
$term = ($_time $_time time()) - filemtime($arc);

    return 
$this->runtime($term,$lang);
  }

  
## ¾ÆÆÄÄ¡ µ¥¸óÀÌ ¾ó¸¶³ª ¿À·§µ¿¾È ½ÇÇàµÇ°í ÀÖ´ÂÁö ¾Ë¾Æº¸±â
  ## win32ÀÏ °æ¿ì, $pid = 'C:/Program Files/Apache Group/Apache/logs/httpd.pid'
  ## ÀÛ¼ºÀÚ : ±èÄ¥ºÀ[´Ð:»êÀÌ] <san2(at)linxuchannel.net>
  ## ÀÛ¼ºÀÏ : 2000.12.11
  ## 2001.02.07 ÀÏ Ãß°¡
  ##
  
function get_apache_runtime($pid='/usr/local/apache/logs/httpd.pid'$lang='kr')
  {
    if(@
file_exists($pid)) return $this->arc_runtime($pid,$lang);

    
$httpdpid[] = '/usr/local/apache/logs/httpd.pid'// ÀϹÝÀûÀ¸·Î ¼Ò½º ÄÄÆÄÀϽÃ
    
$httpdpid[] = '/var/run/httpd.pid';
    
$httpdpid[] = '/var/run/apache.pid';
    
$httpdpid[] = '/var/run/apache2.pid';
    
$httpdpid[] = '/usr/local/apache/logs/apache.pid';
    
$httpdpid[] = '/usr/local/apache2/logs/httpd.pid'// apache-2.x
    
$httpdpid[] = '/usr/local/apache2/logs/apache.pid'// apache-2.x
    
$httpdpid[] = '/usr/local/httpd/logs/httpd.pid';  // apache-2.x
    
$httpdpid[] = '/usr/local/httpd/logs/apache.pid';  // apache-2.x
    
$httpdpid[] = '/usr/local/etc/httpd/logs/httpd.pid'// ¿¹Àü¹æ½Ä
    
$httpdpid[] = '/usr/local/etc/httpd/logs/apache.pid'// ¿¹Àü¹æ½Ä

    
for($i=0$i<count($httpdpid); $i++)
    {
        if(@
file_exists($httpdpid[$i]))
        {
            return 
$this->arc_runtime($httpdpid[$i],$lang);
            break;
        }
    }
  }

  
## alias get_apache_runtime()
  ##
  
function apache_uptime($pid='/usr/local/apache/logs/httpd.pid'$lang='kr')
  {
    return 
$this->get_apache_runtime($pid,$lang);
  }

  
## ½Ã½ºÅÛÀÌ ¾ó¸¶³ª ¿À·¡µ¿¾È ÀÛµ¿Çϰí ÀÖ´ÂÁö ¾Ë¾Æº¸±â
  ## 2002.11.07 add boot time
  ## 2001.02.07 ÀÏ Ãß°¡
  ##
  
function system_uptime($uptime='/proc/uptime'$lang='kr')
  {
    global 
$_time;

    
$uptime = @file($uptime);
    
$t['term'] = (int)$uptime[0];

    
## boot time (/proc/stat -> btime)
    ##
    
$t['btime'] = ($_time $_time time()) - $t['term'];

    return 
$this->runtime($t['term'],$lang) .', '$this->my_date($t['btime']);
  }

  
## alias system_uptime()
  ##
  
function sys_uptime($uptime='/proc/uptime'$lang='kr')
  {
    return 
$this->system_uptime($uptime,$lang);
  }

  
## 2001.02.07 ÀÏ Ãß°¡
  ##
  
function get_addr_to_base()
  {
    global 
$_SERVER;

    
$serv explode('.',$_SERVER['SERVER_ADDR']);
    return 
$serv[0]*256*256*256 $serv[1]*256*256 $serv[2]*256 $serv[3];
  }
// end of class

?>