<?php
//获得文本部分内容
function get_lentxt($txtstr,$maxlen=180){
	$txtstr=strip_tags($txtstr);
	if(strlen($txtstr) > $maxlen){
	  return sub_str(filt_word($txtstr),$maxlen).'...';
	}else{
	  return filt_word($txtstr);
	}
}

function is_utf8($word){
	if(preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true){
		return true;
	}else{
		return false;
	}
}

//格式化日期文本函数
function format_datetime_txt($event_time){
  $nyear=date("Y", time());
  $nmonth=date("m", time());
  $nday=date("d", time());
  $nhour=date("H", time());
  $nminute=date("i", time());

  $etimeArr=explode('-',$event_time);
  $eyear=$etimeArr[0];
  $emonth=$etimeArr[1];
  $etimeArr2=explode(' ',$etimeArr[2]);
  $eday=$etimeArr2[0];
  $etimeArr3=explode(':',$etimeArr2[1]);
  $ehour=$etimeArr3[0];
  $eminute=$etimeArr3[1];
  $tmpStr='';
  if(time()-strtotime($event_time) >= 60*60*24*365){
  	$tmpStr=$eyear."年".$emonth."月";
  }else{
		if(time()-strtotime($event_time) < 60*60){
			$tmpStr=intval((time()-strtotime($event_time))/60);
			$tmpStr=empty($tmpStr) ? 1:$tmpStr;
			$tmpStr=intval($tmpStr)."分钟前";
		}
		elseif(time()-strtotime($event_time) < 60*60*9 && time()-strtotime($event_time) >= 60*60){
			$tmpStr=intval((time()-strtotime($event_time))/(60*60))."小时前";
		}
		elseif(strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) < 60*60*24  && time()-strtotime($event_time) >= 60*60*9){
			$tmpStr="今天".$ehour."时".$eminute."分";
		}
		elseif(strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) >= 60*60*24 && strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) < 60*60*24*2){
			$tmpStr="昨天".$ehour."时".$eminute."分";
		}
		elseif(strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) < 60*60*24*3 && strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) >= 60*60*24*2){
			$tmpStr="前天".$ehour."时".$eminute."分";
		}
		else{
			$tmpStr=$emonth."月".$eday."日";
		}
	}
	return $tmpStr;
}

//格式化日期文本函数(短的)
function format_datetime_short($event_time){
  $nyear=date("Y", time());
  $nmonth=date("m", time());
  $nday=date("d", time());
  $nhour=date("H", time());
  $nminute=date("i", time());

  $etimeArr=explode('-',$event_time);
  $eyear=$etimeArr[0];
  $emonth=$etimeArr[1];
  $etimeArr2=explode(' ',$etimeArr[2]);
  $eday=$etimeArr2[0];
  $etimeArr3=explode(':',$etimeArr2[1]);
  $ehour=$etimeArr3[0];
  $eminute=$etimeArr3[1];
  $tmpStr='';
  if(time()-strtotime($event_time) >= 60*60*24*365){
  	$tmpStr=$eyear."年".$emonth."月";
  }else{
		if(time()-strtotime($event_time) < 60*60){
			$tmpStr=intval((time()-strtotime($event_time))/60);
			$tmpStr=empty($tmpStr) ? 1:$tmpStr;
			$tmpStr=intval($tmpStr)."分钟前";
		}
		elseif(time()-strtotime($event_time) < 60*60*9 && time()-strtotime($event_time) >= 60*60){
			$tmpStr=intval((time()-strtotime($event_time))/(60*60))."小时前";
		}
		elseif(strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) < 60*60*24  && time()-strtotime($event_time) >= 60*60*9){
			$tmpStr="今天";
		}
		elseif(strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) >= 60*60*24 && strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) < 60*60*24*2){
			$tmpStr="昨天";
		}
		elseif(strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) < 60*60*24*3 && strtotime($nyear."-".$nmonth."-".$nday) - strtotime($eyear."-".$emonth."-".$eday) >= 60*60*24*2){
			$tmpStr="前天";
		}
		else{
			$tmpStr=$emonth."月".$eday."日";
		}
	}
	return $tmpStr;
}

function leave_time($date_value,$life_time){
	if(preg_match("/\d{4}-\d{2}-\d{2}/",$date_value)){
		$date_value=strtotime($date_value);
	}
	$life_sec=$life_time*60*60;
	$time=time();
	$pass_sec=$time-$date_value;
	if($pass_sec >= $life_sec){
		return '已过期';
	}else{
		$leave_sec=$life_sec-$pass_sec;
		return conver_time($leave_sec);
	}
}

function conver_time($second){
	$result_str='';
	$leave_time='';
	$leave_time=$leave_time ? $leave_time:$second;
	if($leave_day=floor($leave_time/(3600*24))){
		$leave_time=$leave_time-($leave_day*3600*24);
		$result_str.=$leave_day.'天';
	}
	$leave_time=$leave_time ? $leave_time:$second;
	if($leave_hour=floor($leave_time/3600)){
		$leave_time=$leave_time-($leave_hour*3600);
		$result_str.=$leave_hour.'小时';
	}
	$leave_time=$leave_time ? $leave_time:$second;
	if($leave_min=floor($leave_time/60)){
		$leave_time=$leave_time-($leave_min*60);
		$result_str.=$leave_min.'分钟';
	}
	if($leave_time==$second){
		$result_str='1分钟以内';
	}
	return $result_str;
}

function info_item_format($defaultTxt,$inputTxt){
    if($inputTxt!=''){
    	 return $inputTxt;
    }else{
    	 return $defaultTxt;
    }
}

function brithday_format($y,$m,$d){
	$tmp='';
    if($y!=''){
    	$tmp=$y."年";
    }
    if($m!=''){
    	$tmp=$tmp.$m."年";
    }
    if($d!=''){
    	$tmp=$tmp.$d."日";
    }
    return $tmp;
}

function radio_checked($input_val,$rs_val){
	if($input_val==$rs_val){
	  return ' checked ';
	}
}

//列表控制
$def_show_list=0;
function newline($num){
	global $def_show_list;
	if($def_show_list%$num==0){
		if($def_show_list>0){ echo "</div>"; }
		echo "<div style='clear:both'>";
	}
	$def_show_list++;
}

//取得后缀名
function get_ext($file_name)
{
	$extend=explode("." , $file_name);
	$va=count($extend)-1;
	$ext_name=trim($extend[$va]);
	if($ext_name=='jpeg'){
		 $ext_name='jpg';
	}
	return $ext_name;
}

function get_module_content($url_str){
	Global $siteDomain;
//	echo $url_str; echo'<br>';
  if(strstr($url_str,'app=hstart')){
  	 preg_match_all( "/\?(.*?)\&/s", $url_str, $tmp_value);
		 $tmp=$tmp_value[1][0];
		 $tmp='hstart/'.$tmp;
		 //echo $tmp;
  }else{
		$tmp=explode("app=" ,$url_str);
		$tmp=$tmp[1];
		$tmparr=explode("&" ,$tmp);$tmp='';
		foreach($tmparr as $key => $value){
			if(!strstr($value,'vid=')){
				if($key>0){ $and_str='&'; }
				$tmp.=$and_str.$value;
			}
		}
	}
	$tmp=str_replace(array('&','='),'/',$tmp);
	$cache_file=setdir("iwebcache/home/".$tmp.'/c');
	
	if(file_exists($cache_file)){
		 if(time()-filemtime($cache_file)>(3600*12)){
	      $get_new=1;
	    }else{
	    	$get_new=0;
	    }
	}else{
		$get_new=1;
	}
	
	if($get_new==1){
		 $module=file_get_contents($siteDomain.$url_str);
		 if(strstr($module,'</body>')){
		 		$tmp=explode("<body" ,$module);
		 		$tmp=$tmp[1];
		 		$tmp=explode("</body>" ,$tmp);
		 		$tmp=$tmp[0];
		 		$tmp=strstr($tmp,'>');
		 		$tmp=substr($tmp,1,strlen($tmp));
		 		$module=str_replace(array('modules.php','&user_id=','app=album&','app=photo_list&'),array('home.php','&h=','app=halbum&','app=hphoto&'),$tmp);		 		
		 }
		 $module=reset_content_iurl($module);		 
	   file_put_contents($cache_file,$module);
	}else{
		 $module=file_get_contents($cache_file);
	}	
	
	//赋值成功返回值，否则重新运行该函数
	if($module){
		return $module;
	}else{
		return $siteDomain.$url_str." 数据出错<br>".$module;
	}
}
//自动生成cache目录
function setdir($filepath){
	$dir_arr=explode('/',$filepath);
	//var_dump($dir_arr);echo '<br>';
	$dir_count=count($dir_arr)-1;
	//echo $dir_count;echo '<br>';
	$dir_str='';
	for($i=0;$i<$dir_count;$i++){
		$dir_str.=$dir_arr[$i].'/';
		if(!is_dir($dir_str)){	mkdir($dir_str);}
		//echo $dir_str;echo '<br>';
	}		
	return $filepath;	
}

//url伪静态处理函数==配合htacess使用
function get_iurl($content){	
	if(strstr($content,'home.php?h=')&&!strstr($content,'&')){
		$content=str_replace(array('home.php?','h='),array('home/',''),$content);		
	}else{
		$content=str_replace(array('home.php?','app=','&','='),array('home/','','/','-'),$content);
	}
	//var_dump($content);
	return $content;
}

function reset_content_iurl($content){
		
	$tmparr=explode('"',$content);
	$recontent='';
	$i=0;
	foreach($tmparr as $value){

		if($i>0){ $quot='"'; } $i++;
		$recontent.=$quot;
		if(strstr($value,"home.php?")){			
			 $tmp_value=del_repeat_para($value);
			 $tmp_url=get_iurl($tmp_value);
			 $recontent.=$tmp_url;
//			 echo '&&'.$tmp_value.'<br>';
//			 echo '**'.$tmp_url.'<br>';
		}else{
			 $recontent.=$value;
		}		
	}
	//var_dump($recontent);echo '<br>';
	//return $content;
	return $recontent;	
}

function del_repeat_para($url){
	//echo '==!'.$url.'<br>';
	$reurl=$url;
	
	$tmp=explode('?',$url);
	$reurl=$tmp[0].'?';	
	$tmparr=explode('&',$tmp[1]);
	
	$i=0;$h_count=0;$quot='';

	if(substr_count($url,'?h=')+substr_count($url,'&h=')>1){
		
		foreach($tmparr as $value){
						
			if(strstr($value,"h=")){
				$h_count++;
			}
			if($h_count==2||!strstr($value,"h=")){
				if($i>0){ $quot='&'; } $i++;
				$reurl.=$quot.$value;
			}
		}
		
	}else{
		
		foreach($tmparr as $key => $value){
			if($key==0&&strstr($value,'h=')){
				$setlast=$value;
				$reurl.='';
			}else{
				if($i>0){ $quot='&'; } $i++;
				$reurl.=$quot.$value;
			}			
		}
		if($setlast!=''){
			$reurl.=$quot.$setlast;
		}
	}
	//echo '++'.$reurl.'<BR>';
	return $reurl;
	
}
?>