<?php 
/**
	*****************************************************************
	* 联系QQ： 290802026/1073744729									*
	* 版  本： V2.0													*
	* 开发者：雨中磐石工作室										*
	* 邮  箱： qqqq2900@126.com										*
	* 网  址： http://www.xh829.com/								*
	* 说  明: 基础操作类方法										*
	* 备  注: 未经允许不得商业出售，代码欢迎参考纠正			*
	*****************************************************************
*/ 
final class rockClass
{
	
	public $ip;
	public $host;
	public $url;
	public $win;
	public $web;
	public $unarr;
	public $now;
	public $date;
	public $jm;
	

	public $adminid;
	public $adminuser;
	public $adminname;

	public function __construct()
	{		
		$this->ip		= $_SERVER['REMOTE_ADDR'];
		$this->host		= $_SERVER['HTTP_HOST'];
		$this->url		= '';
		$this->win		=  php_uname();
		$this->web		= isset($_SERVER['HTTP_USER_AGENT'])	? $_SERVER['HTTP_USER_AGENT']	: '' ;
		$this->web		= $this->getbrowser($this->web);
		$this->unarr	= explode(',',urldecode('%BC%A6%B0%CD%2C%D7%F6%B0%AE'));
		$this->now		= $this->now();
		$this->date		= date('Y-m-d');
	}
	
	/**
		判断是否有特殊字符串
	*/
	private function unstr($str)
	{
		$ystr = '';
		for($i=0;$i<count($this->unarr);$i++){
			if($this->contain($str,$this->unarr[$i])){
				$ystr = $this->unarr[$i];
				break;
			}
		}
		return $ystr;
	}

	public function get($name,$dev='', $lx=0)
	{
		$val=$dev;
		if(isset($_GET[$name]))$val=$_GET[$name];
		if($this->isempt($val))$val=$dev;
		return $this->jmuncode($val, $lx);
	}
	
	public function post($name,$dev='', $lx=0)
	{
		$val  = '';
		if(isset($_POST[$name]))$val=$_POST[$name];		
		if($this->isempt($val))$val=$dev;
		return $this->jmuncode($val, $lx);
	}
	
	public function request($name,$dev='', $lx=0)
	{
		$val  = '';
		if(isset($_REQUEST[$name]))$val=$_REQUEST[$name];		
		if($this->isempt($val))$val=$dev;
		return $this->jmuncode($val, $lx);
	}
	
	public function jmuncode($s, $lx=0)
	{
		if(substr($s, 0, 7)=='rockjm_' || $lx == 1){
			$s = str_replace('rockjm_', '', $s);
			$s = $this->jm->uncrypt($s);
		}	
		return $s;
	}
	
	public function savesession($arr)
	{
		foreach($arr as $kv=>$vv)$_SESSION[$kv]=$vv;
	}
	
	public function session($name,$dev='')
	{
		$val	= '';
		if(isset($_SESSION[$name]))$val=$_SESSION[$name];
		if($this->isempt($val))$val=$dev;
		return $val;
	}
	
	public function clearsession($name)
	{
		$arrn=explode(',',$name);
		for($i=0;$i<count($arrn);$i++){
			@$_SESSION[$arrn[$i]]='';
		}
	}
	
	public function clearallsession()
	{
		foreach($_SESSION as $key=>$value){
			$this->clearsession($key);
		}
	}	
	
	//保存cookie，默认是7天
	public function savecookie($namarr,$valarr,$expire=7,$path='/',$domain='')
	{
		//$domain=(empty($domain))?$this->host:$domain;
		$arrn=explode(',',$namarr);
		$valn=$valarr;
		if(!is_array($valarr))$valn=explode(',',$valarr);
		for($i=0;$i<count($arrn);$i++){
			setcookie($arrn[$i],$valn[$i],time()+$expire*3600*24,$path,'');
		}
	}
	
	//获取cookie
	public function cookie($name,$dev='')
	{
		$val	= '';
		if(isset($_COOKIE[$name]))$val=$_COOKIE[$name];
		if($this->isempt($val))$val=$dev;
		return $val;
	}
	
	public function getcookie($namarr)
	{
		$arrn=explode(',',$namarr);
		for($i=0;$i<count($arrn);$i++){
			$val[$arrn[$i]]=$this->cookie($arrn[$i]);
		}
		return $val;
	}
	
	//删除cookie
	public function clearcookie($name,$path='/',$domain='')
	{
		//$domain=(empty($domain))?$this->host:$domain;
		$arr=explode(',',$name);
		for($i=0;$i<count($arr);$i++){
			setcookie($arr[$i],'',time()-1,$path,$domain);
			@$_COOKIE[$arr[$i]]='';
		}
	}
	
	//删除所有cookie
	public function clearallcookie()
	{
		foreach($_COOKIE as $key=>$value){
			$this->clearcookie($key);
		}
	}	
	
	//跳转
	public function location($url)
	{
		header('location:'.$url.'');
		exit;
	}
	
	public function now($type='Y-m-d H:i:s',$kmti='')
	{
		if($kmti=='')$kmti=time();
		return date($type,$kmti);
	}
	
	public function cnweek($date)
	{
		$arr = array('日','一','二','三','四','五','六');
		return $arr[date('w', strtotime($date))];
	}	
		
	public function getbrowser()
	{
		$web	= $this->web;
		$val	= 'IE';
		$parr	= array(
			array('MSIE 5'),array('MSIE 6'),array('MSIE 7'),array('MSIE 8'),array('MSIE 9'),array('MSIE 10'),array('MSIE 11'),array('rv:11','MSIE 11'),array('MSIE 12'),
			array('MSIE 13'),array('Firefox'),array('OPR/','Opera'),array('Chrome'),array('Safari')
		);
		foreach($parr as $wp){
			if($this->contain($web, $wp[0])){
				$val	= $wp[0];
				if(isset($wp[1]))$val	= $wp[1];
				break;
			}
		}
		return $val;
	}
	
	//值
	public function seldata($arr,$alst='')
	{
		$s=$alst;
		foreach($arr as $da){
			$key = array_keys($da);
			$s.=','.$da[$key[0]].'';
			if(count($key)>1){
				$s.='|'.$da[$key[1]].'';
			}
		}
		if($s!=''&&$alst=='')$s=substr($s,1);
		return $s;
	}
	
	public function arraydata($arr,$alst='')
	{
		$s=$alst;
		foreach($arr as $da){
			$key = array_keys($da);
			$s1='';
			for($i=0;$i<count($key);$i++)$s1.=",'".$da[$key[$i]]."'";
			$s.=',['.substr($s1,1).']';
		}
		if($s!=''&&$alst=='')$s=substr($s,1);
		return '['.$s.']';
	}
	
	public function script($daima)
	{
		echo '<script type="text/javascript">
		'.$daima.'
		</script>';
	}
	
	//全角半角转换
	public function replace($str,$type='ban')
	{
		$search=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','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');
		$replace=array('０','１','２','３','４','５','６','７','８','９','，','。','？','’','（','）','；','ａ','ｂ','ｃ','ｄ','ｅ','ｆ','ｇ','ｈ','ｉ','ｊ','ｋ','ｌ','ｍ','ｎ','ｏ','ｐ','ｑ','ｒ','ｓ','ｔ','ｕ','ｖ','ｗ','ｘ','ｙ','ｚ','Ａ','Ｂ','Ｃ','Ｄ','Ｅ','Ｆ','Ｇ','Ｈ','Ｉ','Ｊ','Ｋ','Ｌ','Ｍ','Ｎ','Ｏ','Ｐ','Ｑ','Ｒ','Ｓ','Ｔ','Ｕ','Ｖ','Ｗ','Ｚ','Ｙ','Ｚ');
		if($type=='ban'){
			$str=str_replace($replace,$search,$str);
		}else{
			$str=str_replace($search,$replace,$str);
		}
		return $str;
	}
	
	/**
		过滤特殊符合
	*/
	public function repmark($str)
	{
		$search=array('select','delete','join','inner','outer');
		$str=strtolower($str);//转为小写
		$str=str_replace($search,'',$str);
		return $str;
	}
	
	/**
		html编码
	*/
	public function htmlescape($str)
	{
		$search=array('<','>',' ');
		$replace=array('&lt;','&gt;','&nbsp;');
		$str=str_replace($search,$replace,$str);
		return $str;
	}
	
	/**
		小数点位数
	*/
	public function number($num,$w=2)
	{
		if(!$num)$num='0';
		return number_format($num,$w,'.','');
	}
	
	/**
		是否包含返回bool
	*/
	public function contain($str,$a)
	{
		$bool=false;
		if(!$this->isempt($a) && !$this->isempt($str)){
			$ad=strpos($str,$a);
			if($ad>0||!is_bool($ad))$bool=true;
		}
		return $bool;
	}
	
	/**
		将&#39;转换'
	*/
	public function covexec($str)
	{
		$dt  = date('Y-m-d');
		$str = str_replace(
			array('&#39;', '&#39','[F]', '[X]', '[K]', '[A]', '[D]', '[adminid]', '[date]', '{adminid}', '{date}'), 
			array('\'', '\'', '\'', '\\', ' ', 'and', '=', $this->adminid, $dt, $this->adminid, $dt),
			$str
		);
		return $str;
	}
	
	//获取ip地区
	public function getipinfo($ip='')
	{
		if($ip=='')$ip=$this->ip;
		$guojia='';$sheng='';$city='';$type='';$cont='';
		$url	= 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=data&ip='.$ip.'';
		$cont 	= @file_get_contents($url);
		if($cont!=''){
			$cont	= iconv('GB2312','UTF-8',$cont);
			$aarr	= explode('	',$cont);
			if(isset($aarr[3]))$guojia = $aarr[3];
			if(isset($aarr[4]))$sheng  = $aarr[4];
			if(isset($aarr[5]))$city   = $aarr[5];
			if(isset($aarr[7]))$type   = $aarr[7];
			$sheng  = str_replace('省','',$sheng);
			$city   = str_replace('市','',$city);
		}
		return array('ip'=>$ip,'guojia'=>$guojia,'sheng'=>$sheng,'shi'=>$city,'type'=>$type);
	}
	
	//显示图片
	public function writeimg($path,$mw,$mh,$otina='')
	{
		$arr = $this->imgwh($path,$mw,$mh);
		return '<img src="'.$arr[3].'" width="'.$arr[0].'" height="'.$arr[1].'" '.$otina.'>';	
	}
	
	//图片显示宽高
	public function imgwh($path, $mw, $mh)
	{
		$arr	= array(0,0,0,'');
		if(file_exists($path)){
			$img	= getimagesize($path);
			$w 		= $img[0];
			$h 		= $img[1];
			$bili	= 1;
			if($w > $mw && $mw != 0){
				$bili	= ($mw/$w);
				$h		= $bili*$h;
				$w		= $mw;
			}
			if($h > $mh && $mh != 0){
				$bili	= ($mh/$h);
				$w		= $bili*$w;
				$h		= $mh;
			}
			$arr	=  array($w,$h,$bili,$path);
		}
		return $arr;
	}
	
	//判断是否为空
	public function isempt($str)
	{
		$bool=false;
		if( ($str==''||$str==NULL||empty($str)) && (!is_numeric($str)) )$bool=true;
		return $bool;
	}
	
	
	/**
		地址
	*/
	public function rewrite($m,$a,$s)
	{
		$url	= '';
		if(REWRITE=='true'){
			$url	= ''.$m.'';
			if($a == '' && $s == ''){
				$url	= ''.$url.'.html';
			}elseif($a == ''){
				$url	= ''.$url.'_'.$s.'.html';
			}else{
				$url	= ''.$url.'_'.$a.'_'.$s.'_a.html';
			}
		}else{
			$url	= 'index.php?m='.$m.'';
			if($a != '')$url.='&a='.$a.'';
			if($s != '')$url.='&s='.$s.'';
		}
		return $url;	
	}	
	
	//设置所有的GET方法
	public function setallcan($rep=4)
	{
		foreach($_GET as $key=>$val)$GLOBALS['get_'.$key]=$this->get($key,'',0);
		foreach($_POST as $key=>$val)$GLOBALS['post_'.$key]=$this->post($key,'',0);
	}
	
	/**
		如果字符为空，使用默认的
	*/
	public function repempt($str,$dev='')
	{
		$s	= $str;
		if($this->isempt($s))$s=$dev;
		return $s;
	}
	
	//返回文件大小
	public function formatsize($size)
	{
		$arr = array('Byte', 'KB', 'MB', 'GB', 'TB', 'PB');
		if($size == 0)return '0';
		$e = floor(log($size)/log(1024));
		return number_format(($size/pow(1024,floor($e))),2,'.','').' '.$arr[$e];
	}
	
	/**
		采集字符串截取
	*/
	public function getcai($content,$start,$end)
	{
		$geju = strpos($content,$start);
		if(!$geju){
			$cont1='';
		}else{
			$stard	= $geju+strlen($start);
			$cont1	= substr($content,$stard);
			$endd	= strpos($cont1,$end);
			$cont1	= substr($cont1,0,$endd);
			$cont1	= trim($cont1);
		}
		return $cont1;
	}
	
	/**
		采集字符串截取
	*/
	public function createtxt($path, $txt)
	{
		$path	= ''.ROOT_PATH.'/'.$path.'';
		$file	= fopen($path,'w');  
		fwrite($file,$txt);
		fclose($file);
	}
	
	public function stringformat($str, $arr=array())
	{
		$s	= $str;
		for($i=0; $i<count($arr); $i++){
			$s=str_replace('?'.$i.'', $arr[$i], $s);
		}
		return $s;
	}
	
	public function strformat($str)
	{
		$len = func_num_args();
		$arr = array();
		for($i=1; $i<$len; $i++)$arr[] = func_get_arg($i);
		$s	 = $this->stringformat($str, $arr);
		return $s;
	}
	
	public function T($n)
	{
		return PREFIX.''.$n;
	}
	
	public function reparr($str, $arr=array())
	{
		if($this->isempt($str))return '';
		preg_match_all('/\{(.*?)\}/', $str, $list);
		$s	= $str;
		foreach($list[1] as $k=>$nrs){
			$nts = '';
			if(isset($arr[$nrs]))$nts = $arr[$nrs];
			$s	= str_replace('{'.$nrs.'}', $nts, $s);
		}
		return $s;
	}
	
	/**
		字段中包含
	*/
	public function dbinstr($fiekd, $str, $spl1=',', $spl2=',')
	{
		return "instr(concat('$spl1', $fiekd, '$spl2'), '".$spl1.$str.$spl2."')>0";
	}
}