<?php

//'文本操作类

/*
-----------------------------------------------------------------;
***************************PHP模块SDK源码*************************;
-----------------------------------------------------------------;
***************************版本 1.0*******************************;
-----------------------------------------------------------------;
***************************作者 小海*******************************;
-----------------------------------------------------------------;
*/
class aes {
  private $hex_iv = '00000000000000000000000000000000'; # converted JAVA byte code in to HEX and placed it here;
  private $key = 'U1MjU1M0FDOUZ.Qz'; #Same as in JAVA;

  function encrypt($str,$key="epasoft") {
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    mcrypt_generic_init($td, $key, $this->hexToStr($this->hex_iv));
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);
    $encrypted = mcrypt_generic($td, $str);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return base64_encode($encrypted);
  }
  function decrypt($code,$key="epasoft") {
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    mcrypt_generic_init($td, $key, $this->hexToStr($this->hex_iv));
    $str = mdecrypt_generic($td, base64_decode($code));
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $this->strippadding($str);
  }
  /*
   For PKCS7 padding;
   */
  private function addpadding($string, $blocksize = 16) {
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
  }
  private function strippadding($string) {
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if (preg_match("/$slastc{" . $slast . "}/", $string)) {
      $string = substr($string, 0, strlen($string) - $slast);
      return $string;
    } else {
      return false;
    }
  }
  function hexToStr($hex){
  
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
    
      $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
  }
}



function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
      $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
	
}




function rc4 ($pwd, $data) {

     $key[] ="";

     $box[] ="";  

     $pwd_length = strlen($pwd);

     $data_length = strlen($data);  

     for ($i = 0; $i < 256; $i++) {

      $key[$i] = ord($pwd[$i % $pwd_length]);

      $box[$i] = $i;

     }  

     for ($j = $i = 0; $i < 256; $i++) {

      $j = ($j + $box[$i] + $key[$i]) % 256;

      $tmp = $box[$i];

      $box[$i] = $box[$j];

      $box[$j] = $tmp;

     }  

     for ($a = $j = $i = 0; $i < $data_length; $i++) {
		  $a = ($a + 1) % 256;
	
		  $j = ($j + $box[$a]) % 256;  
	
		  $tmp = $box[$a];
	
		  $box[$a] = $box[$j];
	
		  $box[$j] = $tmp;  
	
		  $k = $box[(($box[$a] + $box[$j]) %256)];
	
		  @$cipher .= chr(ord($data[$i]) ^ $k);  
     }
     	return $cipher;  

    }
  

function strToHex($string){
      return substr(chunk_split(bin2hex($string)),0,-2);
}
   
function AES_加密($d,$k){
	$aes= new aes();
    return $aes->encrypt($d,$k);
}

function AES_解密($d,$k){
    $aes= new aes();
	return $aes->decrypt($d,$k);
}
   
function RC4_加密($string,$key){
	return strToHex(rc4($key,$string));
}

function RC4_解密($string,$key){
	return  @rc4($key,pack('H*',$string));
}

//'------------------------------文本函数类--------------------------------;
function 文本_取出文本中间($str,$q,$h){
	$w1 = strpos($str,$q) + strlen($q);
	$w2 = strpos($str,$h,$w1);
	return substr($str,$w1,$w2-$w1);
}

function 文本_取出文本中间_批量($str, $q , $h){
	preg_match_all("/$q(.*?)$h/",$str,$s);
	var_dump($s);
	foreach($s as $k => $v){
		$a[] = $v;
	}
	return $a;
}

function 文本_取随机数字($c){
	mt_srand();	
	for($i=1; $i <= $c; $i++){
		$str .= mt_rand(0,9);
	}
	return $str;
}
//echo 文本_取随机数字(10);

function 文本_取随机字母($c){
	$s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	mt_srand();
	for($i=1; $i <= $c; $i++){			
		$str .= substr($s,mt_rand(0,strlen($s)),1);
	}
	return $str;
}
//echo 文本_取随机字母(6)."   ";

function 文本_取随机字符($c){
	$s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	mt_srand();
	for($i=1; $i <= $c; $i++){			
		$str .= substr($s,mt_rand(0,strlen($s)),1);
	}
	return $str;
}
//echo 文本_取随机字符(6);
function 文本_加密数据($data, $key){
    $key    =   md5($key);  
    $x      =   0;  
    $len    =   strlen($data);  
    $l      =   strlen($key);  
    for ($i = 0; $i < $len; $i++) { 
      
        if ($x == $l){  
        
            $x = 0;  
		 }  
        $char .= $key{$x};  
        $x++;  
	}  
    for ($i = 0; $i < $len; $i++){  
    
        $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);  
	}  
    return base64_encode($str);  
}  

function 文本_解密数据($data, $key){

	$key = md5($key);
    $x = 0;
    $data = base64_decode($data);
    $len = strlen($data);
    $l = strlen($key);
    for ($i = 0; $i < $len; $i++){
        if ($x == $l){
        	$x = 0;
		}
        $char .= substr($key, $x, 1);
        $x++;
	}
    for ($i = 0; $i < $len; $i++) {
    
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
       
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        }else{
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
		 }
   	 }
    return $str;
}
//'------------------------------编码类------------------------------------;

function 编码_GBK转UTF8($str){
	return  iconv("GBK","UTF-8" ,$str);
}
function 编码_UTF8转GBK($str){
	return iconv("UTF-8","GBK" ,$str);
}


//'------------------------------时间函数类--------------------------------;
function 时间_到文本($g,$t){
	switch($g){
		case 1 ;
			$s= "Y-m-d H:i:s";
			break;
		case 2 ;
			$s= "Y-m-d";
			break;
		case 3 ;
			$s= "H:i:s";
			break;
		case 4 ;
			$s= "Y年m月d日 H时i分s秒";
			break;
		case 5 ;
			$s= "Y年m月d日";
			break;
		case 6 ;
			$s= "H时i分s秒";
			break; 
	}
	if($t==""){
		return date($s);
	}else{
		return date($s,$t);
	}
}
function 时间_取年份(){
	return date("Y");
}
function 时间_取月份(){
	return date("m");
}
function 时间_取日(){
	return date("d");
}
function 时间_取小时(){
	return date("H");
}
function 时间_取分钟(){
	return date("i");
}
function 时间_取秒(){


	return date("s");
}
//'------------------------------系统函数类--------------------------------;
function 系统_信息框($str){
	echo "<script>alert('$str')</script>";
}
function 系统_跳转页面($d,$s){
	if($s == 1){
		header("Location: ".$d);
		exit;
	}else{
		header("refresh:{$s};url=".$d);
		exit;
	}
}
function 系统_编码设置($d){
	if($d == 1){
		header("Content-Type:text/html;charset=utf-8");
	}else{
		header("Content-Type:text/html;charset=gbk");
	}
}

function 系统_是否电脑访问(){
        $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  
        $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';            
	   function 对比($sss,$text){  
				foreach($sss as $v){
                        if(strpos($text,$v) != false){  
                                return true;  
						}
				}
				return false;  
		}
        $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
        $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');  
        
		$found_mobile= 对比($mobile_os_list,$useragent_commentsblock) ||   对比($mobile_token_list,$useragent);  

        if ($found_mobile){  
                return true;  
		}else{  
                return false;  
		}  
}


?>