<?php
/*
@author waychar
@技术支持 150572314@qq.com
@copyright 2018 waychar.cn 声明：本代码可以用于学习交流使用及要商业化用途
*/
//config
require_once  $_SERVER['DOCUMENT_ROOT'].'/config/config.global.php';

//dbconnect
require_once $_SERVER['DOCUMENT_ROOT'].'/core/db/db.php';

/*
 * @desc get
 */
function get($value)
{
    $replace = array(
        '<p>', "</p>", "&nbsp;",'"',
    );
    foreach ($replace as $row) {
        $value = str_replace($row, "", $value);
    }
    return trim($value);
}

function View($filename)
{
    include 'view/' . $filename . '.php';
}

function go($url)
{
    ?>
    <script type="text/javascript">
        location.href='<?php echo $url?>';
    </script>
    <?php
}

function Model($model_name)
{
    require_once WEB_ROOT . '/model/' . $model_name . '.php';
}

function get_now()
{
    return date('Y-m-d H:i:s', time());
}

function get_time($v)
{
    if($v<604800)
    {
        return date('H:i:s', $v); 
    }
    else
    {
        return date('Y-m-d H:i:s', $v);       
    }
}

//阿里云邮件推送服务
use Dm\Request\V20151123 as Dm; 

function send_mail($address,$content){
    if(EMAIL =='1')
    {
        include_once 'core/aliyun-php-sdk-dm/aliyun-php-sdk-core/Config.php';          
        $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "", "");        
        $client = new DefaultAcsClient($iClientProfile);    
        $request = new Dm\SingleSendMailRequest();     
        $request->setAccountName("");
        $request->setFromAlias("mfi");
        $request->setAddressType(1);
        $request->setTagName("");
        $request->setReplyToAddress("true");
        $request->setToAddress($address);        
        $request->setSubject("welcome to waychar!");
        $request->setHtmlBody($content);        
        try {
            $response = $client->getAcsResponse($request);
           // print_r($response);
        }
        catch (ClientException  $e) {
            //print_r($e->getErrorCode());   
            //print_r($e->getErrorMessage());   
        }
        catch (ServerException  $e) {        
            //print_r($e->getErrorCode());   
            //print_r($e->getErrorMessage());
        }
    }
}  

function sendSms($m, $c)
{
    if(SMS =='1')
    {
        $url = 'http://http.duanxin.cm/OpenPlatform/OpenApi';
        $ac = '';
        $authkey = '';
        $csid = '';
        $cgid = '7244';
        $t = '';
        $data = array
        (
            'action' => 'sendOnce',                                //发送类型 ，可以有sendOnce短信发送，sendBatch一对一发送，sendParam 动态参数短信接口
            'ac' => $ac,                                             //用户账号
            'authkey' => $authkey,                                 //认证密钥
            'cgid' => $cgid,                                       //通道组编号
            'm' => $m,                                             //号码,多个号码用逗号隔开
            'c' => $c,                                             //如果页面是gbk编码，则转成utf-8编码，如果是页面是utf-8编码，则不需要转码
            'csid' => $csid,                                       //签名编号 ，可以为空，为空时使用系统默认的签名编号
            't' => $t                                              //定时发送，为空时表示立即发送
        );
        $re = postSMS($url, $data);                                 //POST方式提交
        preg_match_all('/result="(.*?)"/', $re, $res);
        if (trim($res[1][0]) == '1')  //发送成功 ，返回企业编号，员工编号，发送编号，短信条数，单价，余额
        {

            preg_match_all('/\<Item\s+(.*?)\s+\/\>/', $re, $item);
            for ($i = 0; $i < count($item[1]); $i++) {
                preg_match_all('/cid="(.*?)"/', $item[1][$i], $cid);
                preg_match_all('/sid="(.*?)"/', $item[1][$i], $sid);
                preg_match_all('/msgid="(.*?)"/', $item[1][$i], $msgid);
                preg_match_all('/total="(.*?)"/', $item[1][$i], $total);
                preg_match_all('/price="(.*?)"/', $item[1][$i], $price);
                preg_match_all('/remain="(.*?)"/', $item[1][$i], $remain);

                $send['cid'] = $cid[1][0];             //企业编号
                $send['sid'] = $sid[1][0];             //员工编号
                $send['msgid'] = $msgid[1][0];         //发送编号
                $send['total'] = $total[1][0];         //计费条数
                $send['price'] = $price[1][0];         //短信单价
                $send['remain'] = $remain[1][0];       //余额
                $send_arr[] = $send;                   //数组send_arr 存储了发送返回后的相关信息
            }
            return $res[1][0];   //发送成功返回的值

        } else  //发送失败的返回值
        {
            switch (trim($res[1][0])) {
                case  0:
                    return 0;
                    break;
                case  -1:
                    return 1;
                    break;
                case  -2:
                    return 2;
                    break;
                case  -3:
                    return 3;
                    break;
                case  -4:
                    return 4;
                    break;
                case  -5:
                    return 5;
                    break;
                case  -6:
                    return 6;
                    break;
                case  -7:
                    return 7;
                    break;
                case  -8:
                    return 8;
                    break;
                case  -9:
                    return 9;
                    break;
                case  -10:
                    return 10;
                    break;
                case  -11:
                    return 11;
                    break;

                default:
                    break;
            }
        }
    }   
}

function postSMS($url, $data = '')
{
    $row = parse_url($url);
    $host = $row['host'];
    $port = $row['port'] ? $row['port'] : 80;
    $file = $row['path'];
    while (list($k, $v) = each($data)) {
        $post .= rawurlencode($k) . "=" . rawurlencode($v) . "&";    //转URL标准码
    }
    
    $post = substr($post, 0, -1);
    $len = strlen($post);
    $fp = @fsockopen($host, $port, $errno, $errstr, 10);

    if (!$fp) {
        return "$errstr ($errno)\n";
    } else {
        $receive = '';
        $out = "POST $file HTTP/1.0\r\n";
        $out .= "Host: $host\r\n";
        $out .= "Content-type: application/x-www-form-urlencoded\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Content-Length: $len\r\n\r\n";
        $out .= $post;
        fwrite($fp, $out);
        while (!feof($fp)) {
            $receive .= fgets($fp, 128);
        }
        fclose($fp);
        $receive = explode("\r\n\r\n", $receive);
        unset($receive[0]);
        return implode("", $receive);
    }
}

function check_session()
{
    if ($_COOKIE['id'] == '') {
        go('index.php?c=account&action=login');
    }
}

function pykey($py_key)
{
    $pinyin = 65536 + pys($py_key);
    if (45217 <= $pinyin && $pinyin <= 45252) {
        $zimu = "A";
        return $zimu;
    }
    if (45253 <= $pinyin && $pinyin <= 45760) {
        $zimu = "B";
        return $zimu;
    }
    if (45761 <= $pinyin && $pinyin <= 46317) {
        $zimu = "C";
        return $zimu;
    }
    if (46318 <= $pinyin && $pinyin <= 46825) {
        $zimu = "D";
        return $zimu;
    }
    if (46826 <= $pinyin && $pinyin <= 47009) {
        $zimu = "E";
        return $zimu;
    }
    if (47010 <= $pinyin && $pinyin <= 47296) {
        $zimu = "F";
        return $zimu;
    }
    if (47297 <= $pinyin && $pinyin <= 47613) {
        $zimu = "G";
        return $zimu;
    }
    if (47614 <= $pinyin && $pinyin <= 48118) {
        $zimu = "H";
        return $zimu;
    }
    if (48119 <= $pinyin && $pinyin <= 49061) {
        $zimu = "J";
        return $zimu;
    }
    if (49062 <= $pinyin && $pinyin <= 49323) {
        $zimu = "K";
        return $zimu;
    }
    if (49324 <= $pinyin && $pinyin <= 49895) {
        $zimu = "L";
        return $zimu;
    }
    if (49896 <= $pinyin && $pinyin <= 50370) {
        $zimu = "M";
        return $zimu;
    }
    if (50371 <= $pinyin && $pinyin <= 50613) {
        $zimu = "N";
        return $zimu;
    }
    if (50614 <= $pinyin && $pinyin <= 50621) {
        $zimu = "O";
        return $zimu;
    }
    if (50622 <= $pinyin && $pinyin <= 50905) {
        $zimu = "P";
        return $zimu;
    }
    if (50906 <= $pinyin && $pinyin <= 51386) {
        $zimu = "Q";
        return $zimu;
    }
    if (51387 <= $pinyin && $pinyin <= 51445) {
        $zimu = "R";
        return $zimu;
    }
    if (51446 <= $pinyin && $pinyin <= 52217) {
        $zimu = "S";
        return $zimu;
    }
    if (52218 <= $pinyin && $pinyin <= 52697) {
        $zimu = "T";
        return $zimu;
    }
    if (52698 <= $pinyin && $pinyin <= 52979) {
        $zimu = "W";
        return $zimu;
    }
    if (52980 <= $pinyin && $pinyin <= 53640) {
        $zimu = "X";
        return $zimu;
    }
    if (53689 <= $pinyin && $pinyin <= 54480) {
        $zimu = "Y";
        return $zimu;
    }
    if (54481 <= $pinyin && $pinyin <= 62289) {
        $zimu = "Z";
        return $zimu;
    }
    $zimu = $py_key;
    return $zimu;
}

function pys($pysa)
{
    $pyi = "";
    $i = 0;
    for (; $i < strlen($pysa); $i++) {
        $_obfuscate_8w = ord(substr($pysa, $i, 1));
        if (160 < $_obfuscate_8w) {
            $_obfuscate_Bw = ord(substr($pysa, $i++, 1));
            $_obfuscate_8w = $_obfuscate_8w * 256 + $_obfuscate_Bw - 65536;
        }
        $pyi .= $_obfuscate_8w;
    }
    return $pyi;
}

/*
 *@desc 处理用户控件输入的值，并处理
 *@desc 如果为日期类型的（type=3），转换成时间戳 
 */

function input_value($type, $values)
{
    if ($type == 3) {
        $values = strtotime($values);
    }

    return $values;
}

/*
*@desc 获取用户信息
*/
function get_user_msg($user_id)
{
    $sql = "select id,username,email,mobi,fullname from w_user where id = $user_id";
    $result = $this->get_data($sql);

    return $result[0];
}

/*
 * @desc 截取字符
 */

function cut_str($values, $count)
{
    $values = strip_tags($values);
    $values = mb_substr($values, 0, $count, 'utf-8');
    return $values;
}

function admin_check()
{
    if($_COOKIE['admin_check']!="ok")
    {
        go("index.php?c=home&action=admin_login");
    }
}

function check_mobi()
{
    //判断是否是手机
   
        $agent = $_SERVER['HTTP_USER_AGENT'];
       // var_dump($agent);
        $is_pc = (strpos($agent, 'windows nt')) ? true : false;
        $is_mac = (strpos($agent, 'mac os')) ? true : false;
        $is_iphone = (strpos($agent, 'iPhone')) ? true : false;
        $is_android = (strpos($agent, 'android')) ? true : false;
        $is_An = (strpos($agent, 'Android')) ? true : false;
        $is_wx = (strpos($agent, 'MicroMessenger')) ? true : false;
        $is_ipad = (strpos($agent, 'ipad')) ? true : false;
        
        if($is_pc)
        {
            return false; 
        }

        if($is_An)
        {
            return true; 
        }

        if($is_wx)
        {
            return true; 
        }

        if($is_mac)
        {
            return false; 
        }


        if($is_iphone)
        {
            return true; 
        }

        if($is_android)
        {
            return true; 
        }

        if($is_ipad)
        {
            return true; 
        }
}

function low_price($price)
{
    if($price == '0' || $price == '')
    {
        return "免费";
    }
    else
    {
        return  $price."元起";
    }
  //判断是否是手机
}


function core_option($array , $option_name, $key = null)
{
    $out = "<select name=$option_name id=$option_name>";
    //print_r($array);
    for($i=1; $i<=count($array);$i++)
    {
        if($i == $key)
        {
            $select = "selected";
        }
        $out=$out."<option value=$i $select>".$array[$i]."</option>";
        $select = "";
    }
    $out = $out."</select>";
    return $out;
}

function core_option_list($array,$url)
{
    //print_r($news_type);
    for($i=1; $i<=count($array);$i++)
    {
        $out = $out."<a href=".$url.$i." title = ".$array[$i]." class=option_btn>".$array[$i]."</a> ";
    }  
    return $out;
}

if(check_mobi()==true)
{
    //$mobi_root  = "/mobi";
}
?>