<?php
class Helper
{
	function get_ip()
	{
			if ($_SERVER["HTTP_X_FORWARDED_FOR"]) 
			{ 
				$ip_val = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
			}elseif ($_SERVER["HTTP_CLIENT_IP"]) 
			{ 
				$ip_val = $_SERVER["HTTP_CLIENT_IP"]; 
			}elseif ($_SERVER["REMOTE_ADDR"]) 
			{ 
				$ip_val = $_SERVER["REMOTE_ADDR"]; 
			}elseif ($_SERVER["HTTP_X_FORWARDED_FOR"]) 
			{ 
				$ip_val = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
			}elseif ($_SERVER["HTTP_CLIENT_IP"]) 
			{ 
				$ip_val = $_SERVER["HTTP_CLIENT_IP"]; 
			}elseif ($_SERVER["REMOTE_ADDR"]) 
			{ 
				$ip_val = $_SERVER["REMOTE_ADDR"]; 
			}elseif (getenv("HTTP_X_FORWARDED_FOR")) 
			{ 
				$ip_val = getenv("HTTP_X_FORWARDED_FOR"); 
			}elseif (getenv("HTTP_CLIENT_IP")) 
			{ 
				$ip_val = getenv("HTTP_CLIENT_IP"); 
			}elseif (getenv("REMOTE_ADDR"))
			{
				$ip_val = getenv("REMOTE_ADDR"); 
			}else 
			{ 
				$ip_val = "Unknown"; 
			} 
			//["QUERY_STRING"]
			return  $ip_val;	}
	
	//ID映射对值
	public function mapped_key_value( $arr = array() , $key_index = 'prs_id')
	{
		$re_array = array();
		if(is_array($arr) && !empty( $arr[0]) ){
		
			$field_arr = array_keys( $arr[0] );
			
			foreach((array)$arr as $key => $val)
			{
				foreach((array)$field_arr as $f_key => $f_val){
					$re_array[$val[$key_index]][$f_val] = $val[$f_val];
				}
			
			}
		}
		return $re_array;
	}
	
	//默认最多6级
	function grade_info_sort( $sort_array = array(), $key_id = 'prs_id', $parent_id = 'prs_parent_id', $grade_val = 6 )
	{
		$all_array = array();
		if(is_array($sort_array[0])){
			$field_arr = array_keys( $sort_array[0] );
		}
		
		foreach((array)$sort_array as $key => $val)
		{
			foreach((array)$field_arr as $fkey => $fval){
				$all_array[$val[$key_id]][$fval] = $val[$fval];
			}
		}
		//var_dump($all_array);
		//$ret_array = array();
		$ret_array = $this -> small_grade( $all_array , $key_val = 0 , $parent_id , $key_id , $grade_val );

		if($ret_array==false){ return false; }else{	return $ret_array; }
	}
	
	function small_grade( $all_arr = array() , $key_val = 0 , $parent_id = 'prs_parent_id', $key_id = 'prs_id', $grade_val = 6 )
	{
		$ret_arr   = array();
		
		foreach((array)$all_arr as $akey => $aval ){
				$field_arr = array_keys( $aval );
				if( $grade_val >= 0 ){
					//echo $aval[$parent_id];
					if($aval[$parent_id]==$key_val ){
						foreach((array)$field_arr as $fkey => $fval){
							$ret_arr[$aval[$key_id]][$fval] = $aval[$fval];
						}
							unset($all_arr[$akey]);
							$ret_arr[$aval[$key_id]]['arr'] = $this -> small_grade( $all_arr , $keysm_val = $aval[$key_id] , $parent_id , $key_id , $grade_val -1 );
					}
				}
				
		}
		
		//var_dump( $field_arr );
		
		if(count($ret_arr)<1){ return false; }else{	return $ret_arr; }
	}
	

	//,$grade_val = 6 
	//对象类型种类仅select分级处理，$view_type = 'select' 
	//展示数组，生成对象名，对象ID，对象组成值标记，对象组成文本标记，默认选中值，
	function build_out_str( $sort_array = array(), $object_name = '', $object_id = '' , $object_val_str = 'id', $object_txt_str = 'name', $object_chosen_val = '' , $lang_txt = '' , $key_id = 'prs_id', $parent_id = 'prs_parent_id', $grade_val = 6 )
	{
		$info = $this -> grade_info_sort( $sort_array , $key_id, $parent_id , $grade_val );
		$retrun_str = '';
		if( $object_id == '' and $object_name !='' ){ $object_id =  $object_name ; }
		
		
		$retrun_str .= '<select   name="'.$object_name.'"  id="'.$object_id.'" class="form-control" >';
			if( $lang_txt !='' ){
				$retrun_str .= '<option value="" >'.$lang_txt.'</option>'; //$this-> lang['txt_select']
			}
			foreach((array)$info as $one_key => $one_val )
			{
				if($object_chosen_val==$one_val[$object_val_str]){
					$retrun_str .= '<option value="'.$one_val[$object_val_str].'" selected="selected" >'.$one_val[$object_txt_str].'</option>';
				}else{
					$retrun_str .= '<option value="'.$one_val[$object_val_str].'" >'.$one_val[$object_txt_str].'</option>';
				}
				if(!empty($one_val['arr'])){
					foreach((array)$one_val['arr'] as $two_key => $two_val )
					{
						if($object_chosen_val==$two_val[$object_val_str]){
							$retrun_str .= '<option value="'.$two_val[$object_val_str].'" selected="selected" >|_ '.$two_val[$object_txt_str].'</option>';
						}else{
							$retrun_str .= '<option value="'.$two_val[$object_val_str].'">|_ '.$two_val[$object_txt_str].'</option>';
						}
						if(!empty($two_val['arr'])){
							foreach((array)$two_val['arr'] as $three_key => $three_val )
							{
								if($object_chosen_val==$three_val[$object_val_str]){
									$retrun_str .= '<option value="'.$three_val[$object_val_str].'"  selected="selected">|_ _ '.$three_val[$object_txt_str].'</option>';
								}else{
									$retrun_str .= '<option value="'.$three_val[$object_val_str].'">|_ _ '.$three_val[$object_txt_str].'</option>';
								}
								if(!empty($three_val['arr'])){
									foreach((array)$three_val['arr'] as $four_key => $four_val )
									{
										if($object_chosen_val==$four_val[$object_val_str]){
											$retrun_str .= '<option value="'.$four_val[$object_val_str].'" selected="selected">|_ _ _ '.$four_val[$object_txt_str].'</option>';
										}else{
											$retrun_str .= '<option value="'.$four_val[$object_val_str].'">|_ _ _ '.$four_val[$object_txt_str].'</option>';
										}
										if(!empty($four_val['arr'])){
											foreach((array)$four_val['arr'] as $five_key => $five_val )
											{
												if($object_chosen_val==$five_val[$object_val_str]){
													$retrun_str .= '<option value="'.$five_val[$object_val_str].'" selected="selected" >|_ _ _ _ '.$five_val[$object_txt_str].'</option>';
												}else{
													$retrun_str .= '<option value="'.$five_val[$object_val_str].'">|_ _ _ _ '.$five_val[$object_txt_str].'</option>';
												}
												if(!empty($five_val['arr'])){
													foreach((array)$five_val['arr'] as $six_key => $six_val )
													{
														if($object_chosen_val==$six_val[$object_val_str]){
															$retrun_str .= '<option value="'.$six_val[$object_val_str].'" selected="selected" >|_ _ _ _ _ '.$six_val[$object_txt_str].'</option>';
														}else{
															$retrun_str .= '<option value="'.$six_val[$object_val_str].'">|_ _ _ _ _ '.$six_val[$object_txt_str].'</option>';
														}
													
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
								
			}
		$retrun_str .= '</select>';
		return $retrun_str;
	}


	//,$grade_val = 6 
	//对象类型种类仅select分级处理，$view_type = 'select' 
	//展示数组，生成对象名，对象ID，对象组成值标记，对象组成文本标记，默认选中值，
	function build_list_str( $sort_array = array(), $object_name = '', $object_id = '' , $object_val_str = 'id', $object_txt_str = 'name', $object_chosen_val = '' , $lang_txt = '' , $key_id = 'prs_id', $parent_id = 'prs_parent_id', $grade_val = 6 )
	{
		$info = $this -> grade_info_sort( $sort_array , $key_id, $parent_id , $grade_val );
		$retrun_str = '';
		if( $object_id == '' and $object_name !='' ){ $object_id =  $object_name ; }
		
			if( $lang_txt !='' ){
				$retrun_str .= '<div class="mainleftmenutop">'.$lang_txt.'</div>'; //$this-> lang['txt_select']
			}
		$retrun_str .= '<ul>';

			foreach((array)$info as $one_key => $one_val )
			{
				
				$retrun_str .= ' <li style="font-weight:bold;"><a href="/index.php?/item-info_list-'.$one_key.'.html">'.$one_val[$object_txt_str].'</a></li>';
 
				if(!empty($one_val['arr'])){
					foreach((array)$one_val['arr'] as $two_key => $two_val )
					{
						 
						$retrun_str .= '<li>&nbsp;&nbsp;<a href="/index.php?/item-info_list-'.$two_key.'.html">'.$two_val[$object_txt_str].'</a></li>';
						 
						if(!empty($two_val['arr'])){
							foreach((array)$two_val['arr'] as $three_key => $three_val )
							{
								 
								$retrun_str .= '<li>&nbsp;&nbsp;&nbsp;<a href="/index.php?/item-info_list-'.$three_key.'.html">'.$three_val[$object_txt_str].'</a></li>';
								 
								if(!empty($three_val['arr'])){
									foreach((array)$three_val['arr'] as $four_key => $four_val )
									{
										$retrun_str .= '<li>&nbsp;&nbsp;&nbsp;&nbsp;<a href="/index.php?/item-info_list-'.$four_key.'.html">'.$four_val[$object_txt_str].'</a></li>';
										if(!empty($four_val['arr'])){
											foreach((array)$four_val['arr'] as $five_key => $five_val )
											{
												$retrun_str .= '<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/index.php?/item-info_list-'.$five_key.'.html">'.$five_val[$object_txt_str].'</a></li>';
												if(!empty($five_val['arr'])){
													foreach((array)$five_val['arr'] as $six_key => $six_val )
													{
														$retrun_str .= '<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/index.php?/item-info_list-'.$six_key.'.html">'.$six_val[$object_txt_str].'</a></li>';
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
								
			}
		$retrun_str .= '</ul>';
		return $retrun_str;
	}

	
	function build_select_obj( $info = array(), $object_name = '', $object_id = '' , $object_val_str = 'id', $object_txt_str = 'name', $object_chosen_val = '' , $lang_txt = '' ,$js_fun = '' )
	{
		$retrun_str = '';
		if( $object_id == '' and $object_name !='' ){ $object_id =  $object_name ; }
		if( $js_fun !='' ){
			$retrun_str .= '<select   name="'.$object_name.'"  id="'.$object_id.'"  onchange="'.$js_fun.'"   class="form-control" >';
		}else{
			$retrun_str .= '<select   name="'.$object_name.'"  id="'.$object_id.'"   class="form-control" >';
		}
			//echo $lang_txt;
			if( $lang_txt !='' ){
				$retrun_str .= '<option value="" >'.$lang_txt.'</option>'; //$this-> lang['txt_select']
			}
			foreach((array)$info as $one_key => $one_val )
			{
				if($one_val[$object_val_str] == $object_chosen_val){
					$retrun_str .= '<option value="'.$one_val[$object_val_str].'" selected="selected" >'.$one_val[$object_txt_str].'</option>';
				}else{
					$retrun_str .= '<option value="'.$one_val[$object_val_str].'">'.$one_val[$object_txt_str].'</option>';
				}		
			}
		$retrun_str .= '</select>';
		return $retrun_str;
	}
	
/*
	function add_keyword( $newkeyword ='' , $type = 'art',$countval = 1 )
	{
		$keyid = '';
		if( $newkeyword != '')
		{
			//查询是否存在关键字
			$selectsql = " SELECT key_id FROM ".$this -> db -> prefix."keyword WHERE key_word ='".$newkeyword."' ";
			$idarray   = $this -> db -> selectOne( $selectsql );

			if($idarray["key_id"]!='')
			{
				$keyid = $idarray["key_id"];
				$upsql = " UPDATE  ".$this -> db -> prefix."keyword SET key_count = key_count + 1 WHERE key_id ='".$idarray["key_id"]."' ";
				$this -> db -> querySql( $upsql );
				
			}else{
				$insertsql = " INSERT INTO `".$this -> db -> prefix."keyword` ( `key_word` , `key_isup` , `key_count` ) VALUES (  '".$newkeyword."', '0', '$countval' )";
				$keyid = $this -> db -> insertSql( $insertsql );
				
			}
		}
		return $keyid;
	}
*/
	
	function get_config()
	{
		$sql = " SELECT sys_id, sys_codestr, sys_value ,sys_type FROM ".$this -> db -> prefix."sys_config WHERE  sys_type = 'web' or sys_type='temp'  GROUP BY sys_id ORDER BY  sys_id  LIMIT 100 ";
		//sys_codestr ='default' and
		
		$info = $this -> db -> select_info($sql);
		
		$configRe = array();
		foreach((array)$info as $row)
		{
			if($row["sys_codestr"]=='webcountcode')
			{
				$configRe[$row["sys_codestr"]] = str_replace("\\\"","\"",base64_decode($row["sys_value"])); 
 			}else if($row["sys_codestr"]=='webcertificate')
			{
				$configRe[$row["sys_codestr"]] = '<a href="http://www.miitbeian.gov.cn" target="_blank">'.$row["sys_value"].'</a>'; 
 			}else if($row["sys_type"]=='temp')
			{
				$configRe["webtemp"] = $row["sys_codestr"];
				$configRe["webcss"] = $row["sys_value"]; 
 			}else{
				 $configRe[$row["sys_codestr"]] = $row["sys_value"]; 
			}
		}
		
		//var_dump($configRe);
		
		//查询是否授权
	    //$r = $this -> checkPowered( $configRe["webpowered"] );
		$configRe["copyby"] = $r["copyby"]; 
		$configRe["poweredby"] = " - Powered by ".$r["poweredby"]; 

		return $configRe;
	}
	
	
	function build_contact($valarray)
	{		
		$reArray = array();
		$type_arr = array('web_qq','web_phote','web_mail','web_admin','web_address');
		foreach($valarray as $vkey => $vval)
		{
			if(in_array($vkey,$type_arr)){
				//echo $vval;
				$keyarray = explode(",", $vval);
			
				switch($vkey)
				{
					case 'web_qq':
						foreach($keyarray as $kkkey => $kkval)
						{
							if($kkval!='')
							{
								$reArray[] = '<a href="http://wpa.qq.com/msgrd?V=1&amp;Uin='.$kkval.'&amp;Site=在线客服&amp;Menu=yes" target="_black" title="您好！很高兴为您服务，请问有什么可以帮您吗？"><img border="0" src="http://wpa.qq.com/pa?p=1:'.$kkval.':41" /></a>';
							}
						}
						break;
					case 'web_phote':
						foreach($keyarray as $kkkey => $kkval)
						{
							if($kkval!='')
							{
								$reArray[] = '电话：'.$kkval;
							}
						}
						break;
					case 'web_mail':
						foreach($keyarray as $kkkey => $kkval)
						{
							if($kkval!='')
							{
								$reArray[] = '邮箱：<a href="mailto:'.$kkval.'">'.$kkval.'</a>';
							}
						}
						break;
					case 'web_admin':
						foreach($keyarray as $kkkey => $kkval)
						{
							if($kkval!='')
							{
								$reArray[] = '联系人：'.$kkval;
							}
						}
						break;
					case 'web_address':
						foreach($keyarray as $kkkey => $kkval)
						{
							if($kkval!='')
							{
								$reArray[] = '地址：'.$kkval;
							}
						}
						break;
				}
			}
		 }
		 return $reArray;
	}

	function get_bbsconfig()
	{
		$sql = " SELECT sys_id, sys_name, sys_codestr, sys_value FROM ".$this -> db -> prefix."configsys WHERE 	sys_type = 'bbs'  GROUP BY sys_id ORDER BY  sys_id  LIMIT 100 ";
		
		$info = $this -> db -> select_info($sql);
 
		$configRe = array();
		foreach ((array)$info as $row)
		{
			$configRe[$row["sys_codestr"]] = $row["sys_value"]; 
		}
 
		if($configRe["default"]!=''){ $GLOBALS['tpl_path'] = $configRe["default"]; }
		
		if($configRe["webclose"]==1)
		{
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
			echo '<br><center>'.$configRe["webclosevalue"].'</center>'; 
			exit('');
		}
		return $configRe;	
	}


	
	function get_artinfo( $typeval = '' )
	{
		$sqlQuery = " SELECT art_id, art_title, art_smalltitle, art_color, art_bolder, art_keyword, art_pic, art_description,left(art_detail ,200) as art_detailsmall FROM  ".$this -> db -> prefix."article  WHERE  art_sysfile = '".$typeval."'  GROUP BY art_id ORDER BY `art_id` LIMIT  0 , 1";
		$info = $this -> db -> selectOne($sqlQuery);

		$styleval = '';
		if($info["art_bolder"]=='1')
		{
			$styleval = "font-weight:bold;";
		}
		if($info["art_color"]!='')
		{
			$styleval .= "color:".$info["art_color"];
		}
		if($styleval!='')
		{
			$info["art_style"] = 'style="'.$styleval.'"';
		}

		if($info["art_description"]=='')
		{
			$info["art_description"]= strip_tags($info["art_detailsmall"]);
		}else{
			$info["art_description"]= strip_tags($info["art_description"]);
		}
		$info["get_url"]  = $this -> helper  -> build_url( $valid = $info["art_id"],'art');
		return $info;
		
	}
	

	

	
	

	
	
	function build_xml( $arrval = array(), $contenthtml ='', $file_name ='')
	{
		foreach($arrval as $key => $val)
		{
			$contenthtml = str_replace("{".$key."}", $val , $contenthtml);
		}
		$this -> write_file($file_name,$contenthtml,$method="w+");
	}
	
	//写文件生成文件
	function write_file($file_name,$content,$method="w+")
	{
		if($filenum=fopen($file_name,$method))
		{
			flock($filenum,LOCK_EX);
			$file_data=fwrite($filenum,$content);
			flock($filenum,LOCK_UN);
			fclose($filenum);
			return true;
		}else{
			return false;
		}
	}
	
	function get_areadict( $parentid = 0 , $are_type=0)
	{

		$sql = " SELECT are_id, are_parent_id, are_name  FROM ".$this -> db -> prefix."areadict WHERE are_parent_id = $parentid GROUP BY are_id ORDER BY are_id  ";
		$info = $this -> db -> select_info($sql);
		
		$linksRe = array();
		foreach ((array)$info as $row)
		{
			$linksRe[$row["lin_id"]]["relurl"] = '<a href="'.$row["lin_url"].'" target="_blank">'.$row["lin_name"].'</a>' ; 
		}
 
		return $linksRe;
	}
	
	
	function get_rank( $num = 0 , $type = 'all' )
	{
		$basenum = 4 ;
		
		//星星 月亮 太阳 金 紫金 黄金 白金 宝石 钻石 至尊王者
		//1 X 4 = 4 星星 
		//4 X 4 = 16 月亮
		//16 X 4 =64 太阳
		//64 X 4 = 256 金
		//256 X 4 = 1024 紫金
		//1024 X 4 = 4096 黄金
		//4096 X 4 = 16384 白金
		//16384 X 4 = 65536 宝石
		//65536 X 4 = 262144 钻石
		//262144 X4 = 1048576 至尊王者

		$rankArray = array( 'ten'=> 0, 'nine'=> 0, 'eight'=> 0, 'seven'=> 0, 'six'=> 0, 'five'=> 0, 'foure'=> 0, 'three'=> 0, 'two'=> 0, 'one'=> 0 );
		
		$nextnum = $num ;
		$rankArray['ten'] =  floor( $num / 1048576 );
		
		
		if($nextnum >= 0 )
		{
			$nextnum = $num - ( $rankArray['ten'] * 1048576);
			$rankArray['nine'] = floor( $nextnum / 262144 );
		}
		

		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ( $rankArray['nine'] * 262144);
			$rankArray['eight'] = floor($nextnum/65536);	
		}


		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ( $rankArray['eight'] * 65536);
			$rankArray['seven'] = floor($nextnum/16384);
		}

		if($nextnum >= 0 )
		{	
			$nextnum = $nextnum - ($rankArray['seven'] * 16384);
			$rankArray['six'] = floor($nextnum/4096);
		}
		

		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ( $rankArray['six'] * 4096);
			$rankArray['five'] = floor($nextnum/1024);
		}
		
		

		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ($rankArray['five'] * 1024);
			$rankArray['foure'] = floor($nextnum/256);
		}



		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ($rankArray['foure'] * 256);
			$rankArray['three'] = floor($nextnum/64);
		}



		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ($rankArray['three'] * 64);
			$rankArray['two'] = floor($nextnum/16);
		}


		if($nextnum >= 0 )
		{
			$nextnum = $nextnum - ($rankArray['two'] * 16);
			$rankArray['one'] = floor($nextnum/4);
			$this -> get_userRankimg($rankArray['one'],1);
		}
		
		$str = '';
		if($type =='all')
		{
			$str .= $this -> get_userRankimg($rankArray['ten'],10);
			$str .= $this -> get_userRankimg($rankArray['nine'],9);
			$str .= $this -> get_userRankimg($rankArray['eight'],8);
			$str .= $this -> get_userRankimg($rankArray['seven'],7);
			$str .= $this -> get_userRankimg($rankArray['six'],6);
			$str .= $this -> get_userRankimg($rankArray['five'],5);
			$str .= $this -> get_userRankimg($rankArray['foure'],4);
			$str .= $this -> get_userRankimg($rankArray['three'],3);
			$str .= $this -> get_userRankimg($rankArray['two'],2);
			$str .= $this -> get_userRankimg($rankArray['one'],1);
			
		}else{
			
			$str = $this -> get_userRankimg($rankArray['one'],1);
			$strtwo = $this -> get_userRankimg($rankArray['two'],2);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['three'],3);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['foure'],4);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['five'],5);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['six'],6);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['seven'],7);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['eight'],8);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['nine'],9);
			$str = $strtwo !='' ?   $strtwo : $str;
			$strtwo = $this -> get_userRankimg($rankArray['ten'],10);
			$str = $strtwo !='' ?   $strtwo : $str;
			

		}
		//var_dump($str);
		return $str;
		
	}
	
	//用户等级图标
	function get_userRankimg($num,$rank)
	{
		for($i=0; $i < $num; $i++)
		{
			$str .= '<img src="'.BASE_WEBURL.'/images/g/'.$rank.'.gif">';
		}
		//echo $str;
		return $str;
	}
	
	/*
		编辑器生成方法
		$toolbar = 'Full' , 'Basic';
	*/
	function load_ckeditor( $textName = 'textContent', $textValue = '' , $toolbar = 'Full' , $uiColor = '#D3D3D3', $width = '800',$height = '220', $ckeditorLoadName = 'ckeditorLoad' ,$Loadjs = true )
	{
		$ckeditorLoad = '';
		if($Loadjs==true)
		{  
			$ckeditorLoad = '<script type="text/javascript" src="'.BASE_WEBURL.'/application/ckeditor/ckeditor.js"></script>';
		}
		if($width=='')
		{
			$ckeditorLoad .= '<textarea name="'.$textName.'" id="'.$textName.'">'.$textValue.'</textarea>
							<script type="text/javascript">
							var ckwidthval = window.screen.width - 300;
							CKEDITOR.replace(\''.$textName.'\',{ toolbar :\''.$toolbar.'\',uiColor : \'#9AB8F3\',height:120 ,width:ckwidthval });
							</script>';
		}else{
			$ckeditorLoad .= '<textarea name="'.$textName.'" id="'.$textName.'">'.$textValue.'</textarea>
							<script type="text/javascript">
							CKEDITOR.replace(\''.$textName.'\',{ toolbar :\''.$toolbar.'\',uiColor : \'#9AB8F3\', height:'.$height.' ,width:'.$width.' });
							</script>';
		}
		//echo $ckeditorLoad;
		//exit();
		return $ckeditorLoad;
		// $this -> view -> assign( $ckeditorLoadName ,$ckeditorLoad);
	}

	//用户及图片
	function builddir( $rootdir = 'com' , $type = 'num', $strfornum = 2, $buildforNum = 2  )
	{
		$numArray = array('0','1','2','3','4','5','6','7','8','9'); //全数字字符 10个
		
		$strArray = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');  //全合适符号共,36个符号
		$rArray = array();//$strfornum = 2; //位数处理 1 , 2 ,3 , 4, 5 组合 规定不超过5 位数
		
		if($type=='num')
		{
			$toforArray = $numArray;
		}else{
			$toforArray = $strArray;
		}
		
		for($i=0; $i < $strfornum; $i++)
		{
			if($i==0)
			{
				$rArray[$i+1] =  $toforArray;
			}else{
				foreach($toforArray as $nval )
				{
					foreach($rArray[$i] as $dval)
					{
						$rArray[$i+1][] = $nval.$dval;	
					}
				}
			}
		}
 
		
		for($bi=0; $bi < $buildforNum; $bi++)
		{
			//生成目录
			if($bi==0)
			{
				foreach($rArray[$strfornum] as $fval)
				{
					$fordir[$bi+1][] =  $fval;
					$this -> mkdiraction( $rootdir , $fval );
				}
			}else{
				foreach( $rArray[$strfornum] as $twodnval )
				{
					foreach($fordir[$bi] as $tddval)
					{
						$fordir[$bi+1][] = $tddval.'/'.$twodnval;
						$this -> mkdiraction( $rootdir , $tddval.'/'.$twodnval );
					}
				}
			}
		}
		return true;
		
	}
	
	function mkdiraction( $rootdir , $dirname )
	{
			//生成目录
			if($rootdir!='')
			{
				$filedir = DOCUMENT_ROOT.'/'.$rootdir.'/'.$dirname;
			}else{
				$filedir = DOCUMENT_ROOT.'/com' .$dirname;
			}
			if(!file_exists($filedir)){ mkdir($filedir, 0755); }
			return true;
	}
	
	
	

	function SecuritySumbit()
	{
		$strServerName 	= $_SERVER["SERVER_NAME"];
		$intLen 		= strlen($strServerName);
		$strFromURL 	= $_SERVER["HTTP_REFERER"];
		$strFromURL 	= substr($strFromURL, 7, $intLen);
		if($strFromURL != $strServerName)
		{
			die("对不起，本站禁止跨站提交信息！");
		}
	}


	function CheckEmail( $mailval ) 
	{ 
		if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$mailval))
		{ 
			return false; 
		}else{
			return true; 
		}
	} 
	
	// 生成HTML文件时的路径
	function getHtmlPage( $username="" , $subdir="" ,$rootdir = 'list/')
	{
	
		//生成目录
		$filedir = ROOT_PATH.$rootdir. substr($username,0,3);
 
		if(!file_exists($filedir)) 
			mkdir($filedir, 0755);

		return substr($username,0,3);
	}
	
	// 生成HTML文件时的路径
	function getHtmlitem( $username="" , $subdir="" ,$rootdir = 'item/' )
	{
	
		//生成目录
		$ret = $first = substr($username,0,2);
		$filedir = ROOT_PATH.$rootdir . $first ;
 
		if(!file_exists($filedir)) 
			mkdir($filedir, 0755);
			
		$two = substr($username,2,2);
		
		if($two!='')
		{
		$filedir .= '/' .$two;
		$ret .= '/' .$two;	
		}else{
		$filedir .= '/0' ;
		$ret .= '/0';	
		}
		if(!file_exists($filedir)) 
			mkdir($filedir, 0755);

		return $ret;
	}

	function mystr_toabc($intstr) 
	{ 
		static $arr=array( //16=>'o',
		10=> 'a', 20 =>'b', 30=>'c',40=>'d',50=>'e',60=>'f',70=>'g',80=>'h',90=>'i',
		11=>'j',12=>'k',13=>'L',14=>'m',15=>'n',17=>'p',18=>'q',19=>'r',
		21=>'s',22=>'t',23=>'u',24=>'v',25=>'w',26=>'x',27=>'y',28=>'z'
		);
		
		foreach( $arr as $key => $val ){
			  $intstr = str_replace($key,$val ,$intstr );
		
		}
		
		return $intstr; 
	}

	function myabc_tostr($intstr) 
	{ 
		 $arr=array( //16=>'o',
		10=> 'a', 20 =>'b', 30=>'c',40=>'d',50=>'e',60=>'f',70=>'g',80=>'h',90=>'i',
		11=>'j',12=>'k',13=>'L',14=>'m',15=>'n',17=>'p',18=>'q',19=>'r',
		21=>'s',22=>'t',23=>'u',24=>'v',25=>'w',26=>'x',27=>'y',28=>'z'
		);
		
		foreach( $arr as $key => $val ){
			  $intstr = str_replace($val,$key,$intstr );
		}
		
		return $intstr; 
	}
	
	function orderby_list($oderbydefault ,$listval , $listtype )
	{
		$orderbyval = "";
		$listhtml = '';	//反向排序参数
		if($listval!='' and $listtype =='DESC' )
		{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			$endArray["listtype"] = '';
			$endArray["listval"]  = $listval;

		}else{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = 'art_uptime';
			$orderbyval = " a.art_uptime DESC , `art_id` DESC ";	
		}
		
		$this -> view ->assign('listhtml',$listhtml);
		$this -> view ->assign('listval',$listval);
		$this -> view ->assign('listtype',$listtype);
		
		return $re;
	}
	
	function getdirs( $dir ) 
    {
         $array = array();
         $d = @dir($dir);
         if($d)
         {
               while (false !== ($entry = $d->read())) 
               {
                   if($entry!='.' && $entry!='..') 
                   {
                       $back = $entry;
					   $entry = $dir.'/'.$entry;
                      
					   if(is_dir($entry)) 
                       {

                          $array[] = $back;
                       }
                   }
               }
               $d->close();
         }
         return $array;
     } 
	 
	//取一字母作为目录前缀
	function buildsmartydir( $type = 'n', $smdirid = '0')
	{
		if($smdirid==''){ //$smdirid = 0 ;
			return 	'';
		}else{
			return 	substr( $smdirid ,-1).$type;
		}
	}

	function build_img( $fileName )
	{
		//echo $fileName;
		$filestr = explode('.', $fileName);
		$ftype = end($filestr);
		//$ftype = end(explode('.', $fileName));
		return $small_img  = 	$fileName.IMG_SMALLWIDTH."X".IMG_SMALLHEIGHT.".".$ftype;
	}

	//伪静态及动态生成URL处理
	function build_url( $valid , $type = 'pro' ,$page_num_val = 1 )
	{
		//伪静态
		switch ($type)
		{
			case 'art':
				$retUrl = BASE_WEBURL."/index.php?/article-show-".$valid.".html";
				break;
			case 'pro':
				$retUrl = BASE_WEBURL."/index.php?/item-show-".$valid.".html";
				break;
			case 'pic':
				$retUrl = BASE_WEBURL."/index.php?/picture-show-".$valid.".html";
				break;
			case 'vid':
				$retUrl = BASE_WEBURL."/index.php?/video-show-".$valid.".html";
				break;
			case 'vot':
				$retUrl = BASE_WEBURL."/index.php?/vote-show-".$valid.".html";
				break;
			case 'inv':
				$retUrl = BASE_WEBURL."/index.php?/invite-show-".$valid.".html";
				break;
			case 'dow':
				$retUrl = BASE_WEBURL."/index.php?/download-show-".$valid.".html";
				break;
			case 'hel':
				$retUrl = BASE_WEBURL."/index.php?/help-show-".$valid.".html";
				break;
			case 'for':
				$retUrl = BASE_WEBURL."/index.php?/forum-show-".$valid.".html";
				break;
			case 'thr':
				$retUrl = BASE_WEBURL."/index.php?/thread-show-".$valid.".html";
				break;

			default:
				$retUrl = BASE_WEBURL."/";
		}

 		return $retUrl;
	}
	
	


	function restrval($val)
	{
		return str_replace(array("&39;"), array('\''), $val);
	}
	
	function CheckDomain( $domainval ) 
	{
		//echo $domainval = 'http://Www.HuCart.Com';
		///英文域名 .com , .net , .org ,.cn , .gov.cn  .biz .info, .name , .cc , .tv  , .mobi .me .asia .co .so .tel
		//com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk  (com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum
		if (!eregi("^(http|ftp|https):\/\/+[_a-z0-9-]+(\.[_a-z0-9-]+)+(\.[a-z0-9-]+)*$",$domainval))
		{ 
			return false;
		}else{
			return true;
		}
	}
	
	function CheckIp( $ipval ) 
	{
		//echo $ipval = 'http://127.0.0.1';
		if (!eregi("^(http|ftp|https):\/\/+[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$",$ipval))
		{ 
			return false;
		}else{
			return true;
		}
	}
	
	function checkPowered( $webpowered )
	{
		
		if(strchr($webpowered,'R1YzMi5DT00='))
		{
			$poweredval = str_replace('R1YzMi5DT00=','',$webpowered);
			$poweredval = base64_decode(base64_decode(strrev($poweredval)));
			
			if( DOMAIN_NAME == $poweredval and $this -> CheckDomain( $poweredval ))
			{
				if(strchr($poweredval,'/www.'))
				{
					if(strchr($poweredval,'https'))
					{
						$reArray["poweredby"] = strtoupper(str_replace("https://www.","",$poweredval));
					}else{
						$reArray["poweredby"] = strtoupper(str_replace("http://www.","",$poweredval));
					}
				}else{
					if(strpos($poweredval,'https'))
					{
						$reArray["poweredby"] = strtoupper(str_replace("https://","",$poweredval));
					}else{
						$reArray["poweredby"] = strtoupper(str_replace("http://","",$poweredval));
					}
				}
				
				$reArray["copyby"] = '&copy; 2005-'.date("Y").' '.strtolower($reArray["poweredby"]).' 版权所有 Powered by <a href="'.$poweredval.'"  target="_blank">'.$reArray["poweredby"].'</a>';
				$reArray["poweredtrue"] = true;
				
				return $reArray;
			}
		}

		$reArray["copyby"] 		= base64_decode(strrev(COPYRIGHT_KEY));
		$reArray["poweredby"] 	= strtoupper(str_replace("http://www.","",COPYRIGHT));
		$reArray["poweredtrue"] = false;
		return $reArray;
		
	}
	
	function subPhoneStr( $phoneNum )
	{
		 return substr($phoneNum,0,3)."******".substr($phoneNum,9,2);
	}
	
	
	function get_th_list( $show_arr = array() , $list_val = '' , $list_type = '', $lang_arr = array(), $act_val='info_list')
	{

		foreach($show_arr as $key => $val)
		{
			if( $val['th']==1 ){
				if($list_type=='ASC' and $key ==  $list_val){
					$re_show[$key] = '<a href="javascript:list_search(\''.$key.'\',\'DESC\',\''.$act_val.'\')"  class="glyphicon glyphicon-arrow-up">'.$lang_arr[$key].'</a>';	
				}else if($list_type=='DESC' and $key ==  $list_val){
					$re_show[$key] = '<a href="javascript:list_search(\''.$key.'\',\'ASC\',\''.$act_val.'\')"  class="glyphicon glyphicon-arrow-down">'.$lang_arr[$key].'</a>';
				}else{
					$re_show[$key] = '<a href="javascript:list_search(\''.$key.'\',\'ASC\',\''.$act_val.'\')"  class="glyphicon">'.$lang_arr[$key].'</a>';
				}
			}else{
				$re_show[$key] = $lang_arr[$key];	
			}
		}
		
		return 	$re_show ;
	}
	
	//自己生成列表查询的SQL语句	
	function get_list_sql( $table_arr = array() )
	{
		$table_arr['field_val'] = implode(",",$table_arr['field_arr']);

		if($table_arr['field_arr'] != '' && $table_arr['table'] !='' && $table_arr['def_where'] != '')
		{
			$re_sql = array();
			//LEFT JOIN 使用了
			if($table_arr['left']!=''){
				$re_sql['sql_list']  = " SELECT ".$table_arr['field_val']." FROM  ".$table_arr['prefix'].$table_arr['table']." LEFT JOIN ".$table_arr['prefix'].$table_arr['left']['table']." ON ".$table_arr['left']['on']."   WHERE ".$table_arr['def_where'];
				$re_sql['sql_count'] = " SELECT count(1) as CNUM  FROM  ".$table_arr['prefix'].$table_arr['table']." LEFT JOIN ".$table_arr['prefix'].$table_arr['left']['table']." ON ".$table_arr['left']['on']." WHERE ".$table_arr['def_where'];
			}else{
				$re_sql['sql_list']  = " SELECT ".$table_arr['field_val']." FROM  ".$table_arr['prefix'].$table_arr['table']."  WHERE ".$table_arr['def_where'];
				$re_sql['sql_count'] = " SELECT count(1) as CNUM  FROM  ".$table_arr['prefix'].$table_arr['table']."  WHERE ".$table_arr['def_where'];
			}
			if($table_arr['where_add'] != ''){
				$re_sql['sql_list']  = $re_sql['sql_list'].$table_arr['where_add'];
				$re_sql['sql_count'] = $re_sql['sql_count'].$table_arr['where_add'];
			}
			if($table_arr['group_by'] != ''){
				$re_sql['sql_list'] = $re_sql['sql_list']." GROUP BY ". $table_arr['group_by'];
			}
		
			if($table_arr['order_by'] != ''){
				$re_sql['sql_list'] = $re_sql['sql_list']." ORDER BY ". $table_arr['order_by'];
			}


			if($table_arr['limit_val'] != ''){
				$re_sql['sql_list'] = $re_sql['sql_list']." LIMIT ". $table_arr['limit_val'];
			}
		
			return $re_sql;
		 }
	}
	
	
	//$opt = array('edit'=>'edit','delete'=>'delete');
	//$lang_arr = array() ,删除 些参数
	function get_td_list( $info = array() , $td_arr = array(),$key_arr = array() ,$master_act = '', $opt = array('edit'=>'edit','delete'=>'delete'))
	{
		
		foreach($info as $key => $val)
		{
			unset($key_id);
			foreach( $val as $smkey => $smval ){
				
				switch ( $td_arr[$smkey]['td'] ){
					case 'id':
						$re_show[$key][] = '<input  type="checkbox"   name="'.$smkey.'[]"  id="radobj[]" value="'.$smval.'" /><strong>'.$smval.'</strong>';
						$key_id = $smval;
						break;
					case 'img':
						if($smval!=''){
							$re_show[$key][] = '<img src="'.$smval.'"  width="40">';
						}else{ $re_show[$key][] = ''; }
						break;
					case 'date':
						$re_show[$key][] = date("Y-m-d",$smval);
						break;
					case 'time':
						$re_show[$key][] = date("Y-m-d H:i:s",$smval);
						break;
					case 'arr':
						$re_show[$key][] = $key_arr[$smkey][$smval];
						break;
					default :
						$re_show[$key][] = $smval;
						break;
					}
			}
			
			$opt_str = '';

			foreach((array)$opt as $o_key => $o_val){
				switch ( $o_key ){
					case 'edit':
					$opt_str .= '&nbsp;&nbsp;<a href="./index.php?load='.$master_act.'&act=info&act_type=edit&id='.$key_id.'" >'.$o_val.'</a>';
						break;
					case 'recycle':
					$opt_str .= '&nbsp;&nbsp;<a href="./index.php?load='.$master_act.'&act=recycle&id='.$key_id.'" onclick="return is_recycle()">'.$o_val.'</a>';
						break;
					case 'restore':
					$opt_str .= '&nbsp;&nbsp;<a href="./index.php?load='.$master_act.'&act=restore&id='.$key_id.'" >'.$o_val.'</a>';
						break;
					case 'delete':
					$opt_str .= '&nbsp;&nbsp;<a href="./index.php?load='.$master_act.'&act=delete&id='.$key_id.'" onclick="return is_delete()">'.$o_val.'</a>';
						break;
					case 'item_list':
					$opt_str .= '&nbsp;&nbsp;<a href="./index.php?load='.$master_act.'&act=item_list&&id='.$key_id.'" >'.$o_val.'</a>';
						break;
					case '':
					//$opt_str .= '<a href="./index.php?load='.$master_act.'&act=info&act_type=edit&id='.$key_id.'" >'.$lang_arr['edit'].'</a>';
						break;
				}
			}
			$re_show[$key][] = $opt_str ;
		}
		
		return 	$re_show ;
	}
	


		
}
