<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function array_unique_fb($array2D)
{
	foreach ($array2D as $v)
	{
		$v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
		$temp[] = $v;
	}
	$temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
	foreach ($temp as $k => $v)
	{
		$temp[$k] = explode(",",$v); //再将拆开的数组重新组装
	}
	return $temp;
}

function my_array_unique($array, $keep_key_assoc = false)
{
    $duplicate_keys = array();
    $tmp         = array();

    foreach ($array as $key=>$val)
    {
        // convert objects to arrays, in_array() does not support objects
        if (is_object($val))
            $val = (array)$val;

        if (!in_array($val, $tmp))
            $tmp[] = $val;
        else
            $duplicate_keys[] = $key;
    }

    foreach ($duplicate_keys as $key)
        unset($array[$key]);

    return $keep_key_assoc ? $array : array_values($array);
}

//根据URL获取月份
function getaqiMonth($url){
	$monArr = array('January','February','March','April','May','June','July','August','September','October','November','December');
	$monObj = array(
		'January' => '01',
		'February' => '02',
		'March' => '03',
		'April' => '04',
		'May' => '05' ,
		'June' => '06',
		'July' => '07',
		'August' => '08',
		'September' => '09',
		'October' => '10',
		'November' => '11',
		'December' => '12');
	$urlarr = explode("/",$url);
	$tmp = $urlarr[(count($urlarr)-1)];
	$tmp = ucfirst(str_ireplace('.htm','',$tmp));
	//$d=strtotime("00:01am ".$tmp." 15 2015");
	if(in_array($tmp,$monArr)){
		return $monObj[$tmp];
	}else{
		return false;
	}
}
/*
 * 把数组元素组合为字符串
 * $container:用来包含元素的符号
 * $se:分隔符
 * $arr：需要重新组合的数组
 * 例如：$arr=['aaaa','bbbb'];
 *       my_implode("'",",",$arr);得到
 *       'aaaa','bbbb'
 */
function my_implode($container,$se,$arr)
{
    $str = "";
    if ($arr != '') {
        $str = "";
        $tcount = count($arr);
        $tcountInt = 0;
        foreach ($arr as $i) {
            $tcountInt++;
            if ($tcount == $tcountInt) {
                $str .= $container . $i . $container;
            } else {
                $str .= $container . $i . $container . $se;
            }
        }
    }
    return $str;
}
/*!
 * json_encode($a, JSON_UNESCAPED_UNICODE )
 * for PHP Version < 5.4
 */
function raw_json_encode($input, $flags = 0) {
    $fails = implode('|', array_filter(array(
        '\\\\',
        $flags & JSON_HEX_TAG ? 'u003[CE]' : '',
        $flags & JSON_HEX_AMP ? 'u0026' : '',
        $flags & JSON_HEX_APOS ? 'u0027' : '',
        $flags & JSON_HEX_QUOT ? 'u0022' : '',
    )));
    $pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
    $callback = function ($m) {
        return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
    };
    return preg_replace_callback($pattern, $callback, json_encode($input, $flags));
}
/*!
 * 目的地项目组的订单计划的团号分析
 * 去除添加的后缀, 只保留前两部分: XXXXXX-YYYYYYYYYYYY
 * @date   2018-05-02
 * @param  [type]     $groupCode 从地接系统获取到的团号
 */
function analysis_groupCode($groupCode)
{
    mb_regex_encoding("UTF-8");
    $groupCode = trim_str(trim($groupCode));
    preg_match('/[\w\s\-]+/', characet($groupCode, "UTF-8"), $temp_array);
    // $temp_array[0] = strrchr($temp_array[0], " ") ? mb_strstr($temp_array[0], " ",true) : $temp_array[0];
    if (empty($temp_array)) {
        return trim_str(trim($groupCode));
    }
    $tmp_groupCode  = explode("-", trim($temp_array[0]));
    $real_groupCode = $tmp_groupCode[0];
    if (isset($tmp_groupCode[1])) {
      $real_groupCode .= "-";
      // $real_groupCode .= mb_strstr($tmp_groupCode[1], "（", true)!==false ? mb_strstr($tmp_groupCode[1], "（", true) : $tmp_groupCode[1];
        $order_id = mb_strstr($tmp_groupCode[1], "（", true)!==false ? mb_strstr($tmp_groupCode[1], "（", true) : $tmp_groupCode[1];
        $order_id = mb_strstr($order_id, " ", true)!==false ? mb_strstr($order_id, " ", true) : $order_id;
      $real_groupCode .= $order_id;
    }
    $ret["cut"] = trim_str(trim($real_groupCode));
    $ret["order"] = trim_str(trim($order_id));
    for ($i=2; $i < count($tmp_groupCode); $i++) {
      if (strlen($tmp_groupCode[$i]) > 4) {
        $real_groupCode .= "-";
        $real_groupCode .= mb_strstr($tmp_groupCode[$i], "（", true)!==false ? mb_strstr($tmp_groupCode[$i], "（", true) : $tmp_groupCode[$i];
      }
    }
    $real_groupCode = trim_str(trim($real_groupCode));
    $ret["all"] = $real_groupCode;
    if (strlen($ret['all']) < 9 ) {
      $ret['all'] = trim_str(trim($groupCode));
    }
    return $ret;
}
function trim_str($groupCode)
{
    return mb_ereg_replace('(　| | )', '', $groupCode);
}
/*!
 * 转换字符集编码
 * @param $data
 * @param $targetCharset
 * @return string
 */
function characet($data, $targetCharset) {
    if (!empty($data)) {
        $fileType = "UTF-8";
        if (strcasecmp($fileType, $targetCharset) != 0) {
            $data = mb_convert_encoding($data, $targetCharset, $fileType);
            // $data = iconv($fileType, $targetCharset.'//IGNORE', $data);
        }
    }
    return $data;
}

function real_phone_number($phone, $nation_code)
{
    $nation_str = "+" . $nation_code;
    $cut_nation = str_replace($nation_str, "", $phone);
    return mb_ereg_replace('[\D]', '', $cut_nation);
}

/*!
 * @Author: LYT
 * @Date: 2019-06-27 10:32:11
 * @Desc: 异步的curl,1秒超时,被请求的地址需要设置: ignore_user_abort(true);
 */
function async_curl($url, $second = 1)
{
    // log_message('error','Webht Async Call: '. $url);
    $ch = curl_init();
    $curlVersion = curl_version();
    $ua = "Webht (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." " . "AsyncJob";
    //设置超时
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    curl_setopt($ch,CURLOPT_URL, $url);
    if(stripos($url,"https://")!==FALSE){
        // curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    } else {
        // curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
        // curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
    }
    curl_setopt($ch,CURLOPT_USERAGENT, $ua);
    //设置header
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    //运行curl
    $data = curl_exec($ch);
    if (curl_errno($ch) && !in_array(curl_errno($ch),array(0,28)) ) {
        log_message('error',"Webht Async Curl Call 出错，错误码:" . curl_errno($ch) . ": " . curl_error($ch) . ",  url: " . $url);
    }
    if (!in_array(curl_getinfo($ch, CURLINFO_HTTP_CODE),array(0,200)) ) {
        log_message('error', "Webht Async Curl Call Request html Status Code: ".curl_getinfo($ch, CURLINFO_HTTP_CODE)."; curl url: ".$url);
    }
    curl_close($ch);
    return $data;
}
