<?
function GetIP(){
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  $cip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
  $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif(!empty($_SERVER["REMOTE_ADDR"])){
  $cip = $_SERVER["REMOTE_ADDR"];
}
else{
  $cip = "无法获取！";
}
return $cip;
}

function ipn($ip){
	$ip = explode(".",$ip);
	$ips = $ip[0].".".$ip[1].".".$ip[2].".*";
	return $ips;
	
}

function url($id,$menu,$sz){
	global $PL_CFG;
	 if ($menu=='0'){
		$linkurl = $sz.'.php?id='.$id; 
	 }elseif ($menu=='1'){
		$linkurl = $PL_CFG['urldir'].$sz.'_'.$id.$PL_CFG['html'];  
	 }elseif ($menu=='2'){
		$linkurl = $sz.'.php?'.$id.$PL_CFG['html']; 
	 }
	 return $linkurl;
}
function modurl($moduleid){
	global $PL_CFG;
    $filesp = "menu.php";
    $menulist = cache_read($filesp,PL_ROOT.$PL_CFG['cache']."/");
    foreach($menulist as $id=>$m) {
       if ($id==$moduleid){
	   $mudurl=$PL_CFG['url'].$m['path'].'/';
        }
	}
	 return $mudurl;
}
function modpath($moduleid){
	global $PL_CFG;
    $filesp = "menu.php";
    $menulist = cache_read($filesp,PL_ROOT.$PL_CFG['cache']."/");
    foreach($menulist as $id=>$m) {
       if ($id==$moduleid){
	   $modpath=$m['path'];
        }
	}
	 return $modpath;	
	
}

function modname($moduleid,$style = 'module'){
	global $PL_CFG;
    $filesp = "menu.php";
    $menulist = cache_read($filesp,PL_ROOT.$PL_CFG['cache']."/");
    foreach($menulist as $id=>$m) {
       if ($id==$moduleid){
	   $mudname=$m[$style];
        }
	}
	 return $mudname;
}

function catname($moduleid,$i){
	global $PL_CFG;
   $file = "catid_".$moduleid.".php";
   $catlist = cache_read($file,PL_ROOT.$PL_CFG['cache']."/");
   $catlist = $catlist ? $catlist:array();
    foreach($catlist as $id=>$m) {
       if ($m['id']==$i){
	   $catname=$m['name'];
        }
	}
	 return $catname;
}
function tname($moduleid,$id){
 $tag = sql_show("moduleid=$moduleid&where=id=".$id);
 return $tag['title'];
}
function catarr($moduleid = 1,$catid = 0){
	global $CATEGORY, $PL_CFG;
	$file = "catid_".$moduleid.".php";
    $CATEGORY = cache_read($file,PL_ROOT.$PL_CFG['cache']."/");
	$CATEGORY = $CATEGORY ? $CATEGORY : array();
	$parents = array();
	$cid = $catid;
	if($catid && $CATEGORY[$catid]['child']) $parents[] = $catid;
	while($catid) {
		if($CATEGORY[$cid]['parent']) {
			$parents[] = $cid = $CATEGORY[$cid]['parent'];
		} else {
			break;
		}
	}

	$parents = array_reverse($parents);
	foreach($CATEGORY as $c) {
		
	if($c['parent'] == '0') {
		$cated = $c['id'] == $catid ? ' style="font-weight:bold"' : '';
		$catarr .= '<li><a href="'.modurl($moduleid).$c['linkurl'].'"'.$cated.'>'.$c['name'].'</a></li>';
		if ($c['id'] == $parents[0]){
        foreach($CATEGORY as $c) {
	    foreach($parents as $k=>$v) {
		$cated = isset($parents[$k+1]) ? $parents[$k+1] : $catid;
		$cated = $c['id'] == $cated ? ' style="font-weight:bold"' : '';
		$PL=$k*20+20;
		if($c['parent'] == $v) {
				$catarr .= '<li style="margin-left:'.$PL.'px"><a href="'.modurl($moduleid).$c['linkurl'].'"'.$cated.'>'.$c['name'].'</a></li>';
			}
		}
		}
	}}
	}	
	
	return $catarr;
	
}

function filetemplate($module = '',$tmp = ''){
	 global $PL_CFG;
     $dir = PL_ROOT.'template/'.$PL_CFG['template'].'/'.$module.'/';  // 文件夹的名称
     if (is_dir($dir)){
      if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
			if(strpos('phpleft'.$file,".htm") > 0 and strpos('phpleft'.$file,$tmp) > 0){
       $template[] = $file;
             }
        }
        closedir($dh);
    }
     }
	 return $template;
}

function select($array = '', $value = '' , $title = '' , $name = '' , $extend = ''){
	    if ($array=='') $array=array();
	    $select .= '<select name="'.$name.'"'.$extend.'>';
		if($title) $select .= '<option value="">'.$title.'</option>';
		foreach($array as $c) {
				$selected = $c == $value ? ' selected' : '';
				$select .= '<option value="'.$c.'"'.$selected.'>'.$c.'</option>';
			
		}
		$select .= '</select> ';
	
	return $select;
}
function tmpset($me3,$me2,$me1,$me){
   if ($me3<>'') $tmp = $me3;
   if ($me2<>'' and $me3=='') $tmp = $me2;
   if ($me1<>'' and $me2=='' and $me3=='') $tmp = $me1;
   if ($me2=='' and $me3=='' and $me1=='') $tmp = $me;
   return delhtmls($tmp);
}


function cattmp($moduleid = 1,$catid = 0,$i = 'template'){
	global $CATEGORY, $PL_CFG;
	$file = "catid_".$moduleid.".php";
    $CATEGORY = cache_read($file,PL_ROOT.$PL_CFG['cache']."/");
	$CATEGORY = $CATEGORY ? $CATEGORY : array();
	$parents = array();
	$cid = $catid;
	if($catid && $CATEGORY[$catid]['child']) $parents[] = $catid;
	while($catid) {
		if($CATEGORY[$cid]['parent']) {
			$parents[] = $cid = $CATEGORY[$cid]['parent'];
		} else {
			break;
		}
	}
	$parents[] = $catid;
	$parents = array_reverse($parents);
		foreach(array_reverse($parents) as $k=>$v) {
	foreach($CATEGORY as $c) {
		if($c['id'] == $v) {
       if ($i<>'template'){
		   if ($c['template_show']){
			$cattmp= $c['template_show'];	 
		   }
			 }else{
				 if ($c['template']){
	   $cattmp= $c['template'];
				 }
	   }
				
			}
		}
	}
	return $cattmp;
	
}

function add_select($array = '', $value = '' , $title = '' , $name = '' ,$id = '' , $extend = ''){
	    if ($array=='') $array=array();
	    $select .= '<select id="'.$id.'" name="'.$name.'" '.$extend.'>';
		if($title) $select .= '<option value="">'.$title.'</option>';
		foreach($array as $i=> $c) {
				$selected = $c[0] == $value ? ' selected' : '';
				$select .= '<option value="'.$c[0].'"'.$selected.'>'.$c[1].'</option>';
			
		}
		$select .= '</select> ';
	
	return $select;
}

function add_radio($array = '', $value = '' , $title = '' , $name = '' ,$id = '' , $extend = ''){
	    if ($array=='') $array=array();
	  
		foreach($array as $i=> $c) {
				$selected = $c[0] == $value ? ' checked' : '';
				$select .= '<input type="radio" name="'.$name.'" value="'.$c[0].'" id="'.$id.'" '.$selected.'/>'.$c[1];
			
		}
	
	return $select;
}

function add_checkbox($array = '', $value = '' , $title = '' , $name = '' ,$id = '' , $extend = ''){
	    if ($array=='') $array=array();
		
		$value = explode(',',$value);
		$value = $value ? $value : array();
	 	foreach($array as $i=> $c) {
				$selected = in_array($c[0],$value) ? ' checked' : '';
				$select .= '<input type="checkbox" name="'.$name.'" value="'.$c[0].'" id="'.$id.'" '.$selected.'/>'.$c[1];
			
		}
	
	
	return $select;
}
function tag($id = ''){
	global $PL_CFG;
	$file = "tags_".$id.".php";
    $tags = cache_read($file,PL_ROOT.$PL_CFG['cache']."/");
	$tags = $tags ? $tags : array();
	$tagst = str_replace('\\"','"',$tags['content']);
	return $tagst;
	
}
function comments_n($menuid = '',$id = ''){
	global $PL_CFG;
	$list = sql_cache("表=comments&字段=id,menuid,showid&条件=menuid=$menuid and showid=$id");
	$n = count($list);
	return $n;
	
}
 function uptime($id)
{
	global $PL_CFG;
	$m = sql_list("表=login&where=name='".$id."'&pagesize=2&order=id desc");
	foreach($m as $i=> $c) {
		if($i=='1'){
		$s = date('Y/m/d H:i',$c['time']);			
		}
	}
	return $s;
}

?>