<?php 
//途牛接口创建请求签名
function create_sign(){
	$time = date('Y-m-d H:i:s',time());
	$secretKey = 'qvHMJVywEQqsd4EneHQl';
	$id = 'retailId25';
	$timeStamp = 'timestamp'.$time;
	$sign = $secretKey.$id.'apiKey'.TUNIU_KEY.$timeStamp.$secretKey;
	return strtoupper(md5($sign));
}

//证件名称转id
function strexchangeid($name){
	if($name != ''){
		switch ($name){
			case '二代身份证':
				return '1';
				break;
			case '护照':
				return 'B';
				break;
			case '台湾通行证':
				return 'G';
				break;	
			case '港澳通行证':
				return 'C';
				break;
			default :
				return 'B';
				break;
		}
	}
}

//特殊字符转换
function chk_sp_name($name){
		$name = str_replace(
         array('á','ë','é','è','í','ó','ú','ű','ñ','á','é','í','ó','ö','ú','ű','ñ','/',',','  '),
         array('a','e','e','e','i','o','u','u','n','A','E','I','o','o','U','U','N','','',''),
         $name
		);
		return substr(strtoupper($name),0,30);
	}

//发送请求函数
function GetPost_http($url, $data = '',$format='') {
	if(!isset($_SERVER['HTTP_USER_AGENT'])){
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2372.400 QQBrowser/9.5.10548.400';
	}
	$curl = curl_init(); // 启动一个CURL会话  
	curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查  
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在  
	curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器  
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  
	curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer  
	if (!empty($data)) {
		curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求  
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
		if($format == 'json'){
			curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
		}else if ($format=="json_ctrip"){
			//新携程接口只开通了116.8.4.34的IP能访问
			$header = array(
				'CLIENT-IP: 116.8.4.34',
				'X-FORWARDED-FOR: 116.8.4.34',
				'Content-Type:application/json'
			  );
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
	}
	curl_setopt($curl, CURLOPT_TIMEOUT, 40); // 设置超时限制防止死循环  
	curl_setopt($curl, CURLOPT_TIMEOUT_MS, 40000); // 设置超时限制防止死循环  
	curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容  
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回  
	$tmpInfo = curl_exec($curl); // 执行操作  
	$errno = curl_errno($curl);
	if ($errno !== 0) {
		log_message('error', 'ctripost'.$errno.curl_error($curl));
	}
	curl_close($curl); //关闭CURL会话  
	return $tmpInfo; //返回数据
}

function get_microtime (){
	list($s1, $s2) = explode(' ', microtime());
	return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); 
}

//余票转换
function ticket_exchange($price,$num,$iseven){
	if($iseven){
		if(isset($price) && is_numeric($num)){
			if($price > 0){
				return '有';
			}else{
				return '';
			}
		}
		return '';
	}else{
		if(is_numeric($num)){
			if($num == 0){
				return '无';
			}elseif($num >= 99){
				return '有';
			}else{
				return $num;
			}
		}else{
			if($num == '--'){
				return '';
			}
		}	
	}
}

function checkNull($info){
	if(empty($info)){
		return '';
	}else{
		return $info;
	}
}

function checkDateRule($strDate, $jsonDate){
	if (!strtotime($strDate)) {
		return false;
	}
	$strDate_t = strtotime($strDate);
	$strDate = date("Y-m-d", $strDate_t);
	$dYear = date("Y", $strDate_t);
	$dMonth = date("m", $strDate_t);
	$dDay = date("d", $strDate_t);
	$dWeek = date("w", $strDate_t); //星期的第几天数组（0-6）

	//单双日	
	if ($dDay % 2 == 0) {
		$dSingle = 2;
	} else {
		$dSingle = 1;
	}

	$jObject = json_decode($jsonDate);
	$result = false;

	//先判断排除规则，有一条符合就返回false
	if (isset($jObject->DateRule->exclusive) ) {
		$jObject_exclusive = $jObject->DateRule->exclusive;

		foreach ($jObject_exclusive as  $key => $val) {
			if ($val->dateType=="EveryDay" ) { //每天,这个基本不会出现，设置每天排除和关闭没区别
				return false;
			} elseif ($val->dateType=="WeekDay" ) { //按星期
				//先判断是否在日期范围内，再判断星期是否对应
				$bw_CheckRange = false; //是否需要判断星期几
				if ($val->dateRange == "*") { //每天
					$bw_CheckRange = true;
				} else {
					$dateRange = explode("|", $val->dateRange); //日期范围
					if (count($dateRange) == 2) {
						if ($strDate_t >= strtotime($dateRange[0]) and $strDate_t <= strtotime($dateRange[1])) {
							$bw_CheckRange = true; //在范围内
						}
					}

				}
				
				if ($bw_CheckRange) {
					if ($dWeek==0) {
						$dWeek=7; //星期0为星期天，我们保存的星期天是7
					}
					$weekArr = explode(",", $val->dateData); //星期数据1,2,3,4,5,6,7这种
					foreach ($weekArr as $week) {
						if ($week == $dWeek) {
							return false;
						}    
					}
				}
			} elseif ($val->dateType == "SelectDay") { //固定日期
				if ($val->dateRange=="*") {
					$SelectDayArr = explode(",", $val->dateData);
					foreach ($SelectDayArr as $sDay) {
						if (strtotime($sDay) == $strDate_t) {
							return false;
						}
					}
				}
			} elseif ($val->dateType == "RangeDay") { //连续日期
				if ($val->dateRange == "*") {
					$RangeDateArr = explode("|", $val->dateData);
					if (count($RangeDateArr)==2) {
						if ($strDate_t >= strtotime($RangeDateArr[0]) and $strDate_t <= strtotime($RangeDateArr[1])) {
							return false ; //在范围内
						}
					}

				}
			}
		}
	}

	//再判断包含规则，有一条符合就为true
	if (isset($jObject->DateRule->include)) {
		$jObject_include = $jObject->DateRule->include;

		foreach ($jObject_include as $key => $val) {
			if ($val->dateType == "EveryDay") { //每天
				return true;
			} elseif ($val->dateType == "WeekDay") { //按星期
				//先判断是否在日期范围内，再判断星期是否对应
				$bw_CheckRange = false; //是否需要判断星期几
				if ($val->dateRange == "*") { //每天
					$bw_CheckRange = true;
				} else {
					$dateRange = explode("|", $val->dateRange); //日期范围
					if (count($dateRange) == 2) {
						if ($strDate_t >= strtotime($dateRange[0]) and $strDate_t <= strtotime($dateRange[1])) {
							$bw_CheckRange = true; //在范围内
						}
					}

				}

				if ($bw_CheckRange) {
					if ($dWeek == 0) {
						$dWeek = 7; //星期0为星期天，我们保存的星期天是7
					}
					$weekArr = explode(",", $val->dateData); //星期数据1,2,3,4,5,6,7这种
					foreach ($weekArr as $week) {
						if ($week == $dWeek) {
							return true;
						}
					}
				}
			} elseif ($val->dateType=="MonthDay") { //每月单双
				if ($val->dateRange=="*") {
					$MonthDayArr = explode(",", rtrim($val->dateData, ',')); //月单双数据m3:2,m4:1
					foreach ($MonthDayArr as $MonthKey) {
						$MonthArr = explode(":", $MonthKey);
						$Month = str_pad(str_replace("m", "", $MonthArr[0]), 2, "0", STR_PAD_LEFT);  //存储的月份补0，好比较
						if ($dMonth == $Month) { //月份相同比较
							if ($dSingle == $MonthArr[1]) {
								return true;
							} 
						}
					}
				}
			} elseif ($val->dateType == "SelectDay") { //固定日期
				if ($val->dateRange=="*") {
					$SelectDayArr = explode(",", $val->dateData);
					foreach ($SelectDayArr as $sDay) {
						if (strtotime($sDay) == $strDate_t) {
							return true;
						}
					}
				}
			} elseif ($val->dateType == "RangeDay") { //连续日期
				if ($val->dateRange == "*") {
					$RangeDateArr = explode("|", $val->dateData);
					if (count($RangeDateArr)==2) {
						if ($strDate_t >= strtotime($RangeDateArr[0]) and $strDate_t <= strtotime($RangeDateArr[1])) {
							return true ; //在范围内
						}
					}

				}
			}
		}

	}
	
	return $result;
}

function get_name($seat_code){
	if($seat_code){
		switch($seat_code){
			case 'O' :
				return 'Second Class Seat';
			case 'A8' :
				return 'Second Class Seat';
			case 'A9' :   
				return 'Business Class Seat';
			case 'P' :  
				return 'Superior';
			case 'M' :   
				return 'First Class Seat';
			case 'A7' :   
				return 'First Class Seat';
			case 'A6' :   
				return 'Superior Soft Sleeper';
			case 'A' :   
				return 'Superior Soft Sleeper';
			case '5' :   
				return 'Hard Sleeper';
			case 'S' :   
				return 'Soft Sleeper';
			case 'A4' :   
				return 'Soft Sleeper';
			case 'F' :   
				return 'New Soft Sleeper';
			case 'A3' :   
				return 'Hard Sleeper'; 
			case 'A2' :   
				return 'Soft Seat'; 
			case 'A1' :   
				return 'Hard Seat'; 
			case 'Q' :   
				return 'Tour Seat';
			case 'H' :   
				return 'Other';
			case 'WZ' :   
				return 'Standing-room Only';
			case '13' :   
				return 'Standing-room Only';  
			case 'YDW' :   
				return 'Soft Sleeper';  
			case 'EDW' :   
				return 'Hard Sleeper'; 		
		}
	}
}

function get_name_cn($seat_code){
	if($seat_code){
		switch($seat_code){
			case 'O' :
				return '二等座';
			case 'A8' :
				return '二等座';
			case 'A9' :   
				return '商务座';
			case 'P' :  
				return '特等座';
			case 'M' :   
				return '一等座';
			case 'A7' :   
				return '一等座';
			case 'A6' :   
				return '高级软卧';
			case 'A' :   
				return '高级软卧';
			case '5' :   
				return '硬卧';
			case 'S' :   
				return '软卧';
			case 'A4' :   
				return '软卧';
			case 'F' :   
				return '动卧';
			case 'A3' :   
				return '硬卧'; 
			case 'A2' :   
				return '软座'; 
			case 'A1' :   
				return '硬座'; 
			case 'Q' :   
				return '观光座';
			case 'H' :   
				return '其它';
			case 'WZ' :   
				return '无座';
			case '13' :   
				return '无座';  
			case 'YDW' :   
				return '一等卧';  
			case 'EDW' :   
				return '二等卧'; 		
		}
	}
}


/**
 * 解密函数
 * 算法：des
 * 加密模式：ecb
 * 补齐方法：PKCS5
 * @param unknown_type $input
 */

function des_encrypt($data, $key) {
    $data = pkcs5_pad($data, 8); //确保数据长度是8的倍数并使用PKCS5填充
    $iv = "\x01\x02\x03\x04\x05\x06\x07\x08"; //PHP版本的初始化向量，可以自定义
    $cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
    mcrypt_generic_init($cipher, $key, $iv);
    $encrypted = mcrypt_generic($cipher, $data);
    mcrypt_generic_deinit($cipher);
    mcrypt_module_close($cipher);
    return base64_encode($encrypted); //将密文以base64编码输出
}


function des_decrypt($data, $key) {
    $iv = "\x01\x02\x03\x04\x05\x06\x07\x08"; //PHP版本的初始化向量，可以自定义
    $cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
    mcrypt_generic_init($cipher, $key, $iv);
    $decrypted = mdecrypt_generic($cipher, base64_decode($data)); //将密文先解码后解密
    mcrypt_generic_deinit($cipher);
    mcrypt_module_close($cipher);
    return pkcs5_unpad($decrypted); //移除PKCS5填充并输出明文
}

function pkcs5_pad($text, $blocksize)
{
	$pad = $blocksize - (strlen($text) % $blocksize);
	return $text . str_repeat(chr($pad), $pad);
}

function pkcs5_unpad($text) {
    $pad = ord($text{strlen($text)-1});
    if ($pad > strlen($text)) {
        return false;
    }
    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
        return false;
    }
    return substr($text, 0, -1 * $pad);
}



?>