<?php
if (!defined('puyuetian')) {
    exit('403');
}

function cloudSendSms($phonenumber, $code)
{
    global $_G;
    if (!preg_match('/^1[3456789]\d{9}$/', $phonenumber)) {
        return '手机号错误';
    }
    $_apiurl = "https://api.hadsky.com/zz/v3/sendsms?domain={$_G['SYSTEM']['DOMAIN']}&code={$code}&phonenumber={$phonenumber}&signname={$_G['SET']['APP_PUYUETIAN_SMS_SIGNNAME']}&sitekey=" . md5($_G['SET']['APP_HADSKYCLOUDSERVER_SITEKEY'] . $_G['SYSTEM']['DOMAIN']);
    $r       = json_decode(GetPostData($_apiurl, '', 10), true);
    //记录发送记录
    $_G['TABLE']['APP_PUYUETIAN_SMS_RECORD']->newData(array('pn' => $phonenumber, 'ip' => getClientInfos('ip'), 'state' => $r['state'], 'msg' => $r['msg'], 'code' => $code, 'date' => date('Ymd'), 'datetime' => date('Y-m-d H:i:s')));
    if ('ok' == $r['state']) {
        return true;
    }
    $r = $r['msg'] ? $r['msg'] : '请求失败';
    return $r;
}

//云服务js嵌入
$_G['SET']['EMBED_FOOT'] .= template('hadskycloudserver:cloudserver', true);

//用qq号登录
if (!$_G['USER']['ID'] && $_SESSION['APP_HADSKYCLOUDSERVER_QQLOGIN_OPENID']) {
    if ($_G['SET']['APP_HADSKYCLOUDSERVER_QQLOGIN_OPENREG']) {
        $_G['SET']['OPENREG'] = 1;
    }
    $regarray['qqopenid']      = $_SESSION['APP_HADSKYCLOUDSERVER_QQLOGIN_OPENID'];
    $_G['TEMP']['REGNICKNAME'] = $_SESSION['APP_HADSKYCLOUDSERVER_QQLOGIN_NICKNAME'];
    if (InArray('reg,savereg', $_G['GET']['C']) && $_G['TABLE']['USER']->getData(array('qqopenid' => $regarray['qqopenid']))) {
        $_SESSION['APP_HADSKYCLOUDSERVER_QQLOGIN_OPENID'] = '';
        ExitGourl('index.php?c=reg');
    }
}

//用微博号登录
if (!$_G['USER']['ID'] && $_SESSION['APP_HADSKYCLOUDSERVER_WEIBOLOGIN_UID']) {
    if ($_G['SET']['APP_HADSKYCLOUDSERVER_WEIBOLOGIN_OPENREG']) {
        $_G['SET']['OPENREG'] = 1;
    }
    $regarray['weibo_uid']     = $_SESSION['APP_HADSKYCLOUDSERVER_WEIBOLOGIN_UID'];
    $_G['TEMP']['REGNICKNAME'] = 'weibo_' . $regarray['weibo_uid'];
    if (InArray('reg,savereg', $_G['GET']['C']) && $_G['TABLE']['USER']->getData(array('weibo_uid' => $regarray['weibo_uid']))) {
        $_SESSION['APP_HADSKYCLOUDSERVER_WEIBOLOGIN_UID'] = '';
        ExitGourl('index.php?c=reg');
    }
}

//用百度号登录
if (!$_G['USER']['ID'] && $_SESSION['APP_HADSKYCLOUDSERVER_BAIDULOGIN_USERID']) {
    if ($_G['SET']['APP_HADSKYCLOUDSERVER_BAIDULOGIN_OPENREG']) {
        $_G['SET']['OPENREG'] = 1;
    }
    $regarray['baidu_userid']  = $_SESSION['APP_HADSKYCLOUDSERVER_BAIDULOGIN_USERID'];
    $_G['TEMP']['REGNICKNAME'] = 'baidu_' . $regarray['baidu_userid'];
    if (InArray('reg,savereg', $_G['GET']['C']) && $_G['TABLE']['USER']->getData(array('baidu_userid' => $regarray['baidu_userid']))) {
        $_SESSION['APP_HADSKYCLOUDSERVER_BAIDULOGIN_USERID'] = '';
        ExitGourl('index.php?c=reg');
    }
}

//用微信号登录
if (!$_G['USER']['ID'] && $_SESSION['APP_HADSKYCLOUDSERVER_WEIXIN_OPENID']) {
    if ($_G['SET']['APP_HADSKYCLOUDSERVER_WEIXINLOGIN_OPENREG']) {
        $_G['SET']['OPENREG'] = 1;
    }
    $regarray['wxopenid']      = $_SESSION['APP_HADSKYCLOUDSERVER_WEIXIN_OPENID'];
    $_G['TEMP']['REGNICKNAME'] = 'wx_' . CreateRandomString(7);
    if (InArray('reg,savereg', $_G['GET']['C']) && $_G['TABLE']['USER']->getData(array('wxopenid' => $regarray['wxopenid']))) {
        $_SESSION['APP_HADSKYCLOUDSERVER_WEIXIN_OPENID'] = '';
        ExitGourl('index.php?c=reg');
    }
}

//天豆兑换数量前端化
$_G['SET']['EMBED_HEADMARKS'] .= "
<script>
var HADSKY_VERSION='" . HADSKY_VERSION . "';
var \$app_hadskycloudserver_tiandouduihuanshu=parseInt('{$_G['SET']['APP_HADSKYCLOUDSERVER_TIANDOUDUIHUANSHU']}')||0;
var \$app_hadskycloudserver_tiandouname='{$_G['SET']['TIANDOUNAME']}';
</script>
";

//云短信嵌入
//添加修改手机按钮
if ($_G['SET']['APP_HADSKYCLOUDSERVER_SMS_OPEN']) {
    if ('user' == $_G['GET']['C'] && $_G['USER']['ID'] && ($_G['USER']['ID'] == $_G['GET']['ID'] || !$_G['GET']['ID']) && $_G['USER']['PHONE']) {
        $_G['SET']['EMBED_FOOT'] .= '<script>$("form[name=form_user] input[name=phone]").parent().addClass("pk-padding-top-5 pk-padding-bottom-5").html(\'' . $_G['USER']['PHONE'] . '&nbsp;<a target="_blank" class="pk-text-primary pk-hover-underline" href="index.php?c=app&a=hadskycloudserver:index&s=sms_changephone">修改</a>\')</script>';
    }

    //添加登陆注册及找回密码按钮
    if (InArray('login,reg', $_G['GET']['C'])) {
        $_G['SET']['EMBED_FOOT'] .= template('hadskycloudserver:embed_login', true);
    }

    //防止用户自行修改手机号
    if (('saveuser' == $_G['GET']['C'] || ('center' == $_G['GET']['C'] && 'submit' == $_G['GET']['TYPE'])) && $_G['USER']['ID'] == $_POST['id'] && $_G['USER']['PHONE']) {
        $_POST['phone'] = $_G['USER']['PHONE'];
    }

    //强制注册
    if ($_G['SET']['APP_PUYUETIAN_SMS_MUSTREG'] && ('reg' == $_G['GET']['C'] || 'savereg' == $_G['GET']['C'])) {
        ExitGourl('index.php?c=app&a=hadskycloudserver:index&s=sms_reg');
    }
}
