<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}



if($_GET['send_key'] != 'mocuz_app_mobile_send'){
	odz_error('invaild', '-550');
}
loadcache('plugin');
if(!isset($_G['cache']['plugin']['smstong']['mobilepwd'])){
	odz_error('sms_not_exists', '-550');
}

require_once(DISCUZ_ROOT.'./source/plugin/smstong/smstong.func.php');




if($_GET['action'] == 'verifycode'){
	$mobile = trim($_GET['mobile']);
	if(!ismobile($mobile)) {
		odz_error('error_phone_number', '-10');
	}

	//超过注册限制
	$count = DB::result_first("SELECT count(mobile) FROM ".DB::table('common_member_profile')." WHERE mobile='$mobile'");
	if($count >= $_G['cache']['plugin']['smstong']['accountlimit']) {
		odz_error('mobile_registed', '-15');
	}

	$verifycode = rand(100000,999999);
	$verifycode = str_replace('1989','9819',$verifycode);
	$verifycode = str_replace('1259','9521',$verifycode);
	$verifycode = str_replace('12590','09521',$verifycode);
	$verifycode = str_replace('10086','68001',$verifycode);

	$content = $_G['cache']['plugin']['smstong']['mobileregmsg'];
	$rp = array('$mobile', '$verifycode');
	$sm = array($mobile, $verifycode);
	$content = str_replace($rp, $sm, $content);

	if(sned_msg($mobile, $content, 1, $verifycode)){
		odz_success('success');
	}
}

if($_GET['action'] == 'verify_login'){
    $mobile = trim($_GET['mobile']);
    if(!ismobile($mobile)) {
        odz_error('error_phone_number', '-10');
    }
    $uid = DB::result_first("SELECT uid FROM ".DB::table('common_member_profile')." WHERE mobile='$mobile'");
    if(empty($uid)) {
        odz_error('mobilepwd_mobile_notexists', '-15');
    }

    $verifycode = rand(100000,999999);
    $verifycode = str_replace('1989','9819',$verifycode);
    $verifycode = str_replace('1259','9521',$verifycode);
    $verifycode = str_replace('12590','09521',$verifycode);
    $verifycode = str_replace('10086','68001',$verifycode);

    $content = $_G['cache']['plugin']['smstong']['mobileregmsg'];
    $rp = array('$mobile', '$verifycode');
    $sm = array($mobile, $verifycode);
    $content = str_replace($rp, $sm, $content);

    if(sned_msg($mobile, $content, 41, $verifycode)){
        odz_success('success');
    }
}

if($_GET['action'] == 'lostpasswdsend')
{
	$mobile = trim($_GET['mobile']);
	if(!ismobile($mobile)) {
		odz_error('error_phone_number', '-10');
	}

	$mobile_result = DB::result_first("SELECT * FROM ".DB::table('common_member_profile')." WHERE mobile='$mobile'");

	if(empty($mobile_result))
	{
		odz_error('mobilepwd_mobile_notexists', '-15');
	}

	$verifycode = rand(100000,999999);
	$verifycode = str_replace('1989','9819',$verifycode);
	$verifycode = str_replace('1259','9521',$verifycode);
	$verifycode = str_replace('12590','09521',$verifycode);
	$verifycode = str_replace('10086','68001',$verifycode);

	$content = $_G['cache']['plugin']['smstong']['mobileregmsg'];
	$rp = array('$mobile', '$verifycode');
	$sm = array($mobile, $verifycode);
	$content = str_replace($rp, $sm, $content);

	//短信发送 中状态 为31
	if(sned_msg($mobile, $content, 31, $verifycode)){
		odz_success('success');
	}


}


if($_GET['action'] == 'lostpasswd'){
	$mobile = trim($_GET['mobile']);
	$verifycode = trim($_GET['verifycode']);
	$new_password = trim($_GET['new_password']);

    if($_G['setting']['pwlength']) {
        if(strlen($new_password) < $_G['setting']['pwlength']) {
            showmessage('profile_password_tooshort', '', array('pwlength' => $_G['setting']['pwlength']));
        }
    }

    if($_G['setting']['strongpw']) {
        $strongpw_str = array();

        if(in_array(1, $_G['setting']['strongpw']) && !preg_match("/\d+/", $new_password)) {
            $strongpw_str[] = lang('member/template', 'strongpw_1');
        }

        if(in_array(2, $_G['setting']['strongpw']) && !preg_match("/[a-z]+/", $new_password)) {
            $strongpw_str[] = lang('member/template', 'strongpw_2');
        }

        if(in_array(3, $_G['setting']['strongpw']) && !preg_match("/[A-Z]+/", $new_password)) {
            $strongpw_str[] = lang('member/template', 'strongpw_3');
        }

        if(in_array(4, $_G['setting']['strongpw']) && !preg_match("/[^a-zA-z0-9]+/", $new_password)) {
            $strongpw_str[] = lang('member/template', 'strongpw_4');
        }

        if($strongpw_str) {
            showmessage(lang('member/template', 'password_weak').implode(',', $strongpw_str));
        }
    }

	if(!mobile_check($mobile)) {
		odz_error('error_phone_number', '-10');
	}

	//短信通验证码有效期
	$periodofvalidity = $_G['cache']['plugin']['smstong']['periodofvalidity'];

	if(!empty($mobile) && empty($verifycode)) {
		odz_error('verifycode_empty', '-18');
	}

	$verify = DB::result_first("SELECT mobile FROM ".DB::table('common_verifycode')." WHERE mobile='$mobile' AND verifycode='$verifycode' AND getip='$_G[clientip]' AND status=31 AND dateline>'$_G[timestamp]'-$periodofvalidity");

	//验证码验证失败
	if (!empty($mobile) && !$verify)
	{
		odz_error('verifycode_invalid', '-19');
	}

	loaducenter();
	//未开启此功能
	if($_G['cache']['plugin']['smstong']['mobilepwd'] == 0) {
		odz_error('mobilepwd_closed', '-16');
	}

	$query = DB::query("SELECT a.uid FROM ".DB::table('common_member_profile')." AS a LEFT JOIN ".DB::table('common_member')." AS b ON a.uid=b.uid  WHERE mobile='$mobile'");
	$member = DB::fetch($query);
	if (!$member) {
		odz_error('mobilepwd_mobile_notexists', '-22');
	}

	$member = DB::fetch_first("SELECT uid, username, adminid, email FROM ".DB::table('common_member')." WHERE uid='$member[uid]'");

	if(!$member) {
		showmessage('getpasswd_account_notmatch');
	} elseif($member['adminid'] == 1 || $member['adminid'] == 2) {
		showmessage('getpasswd_account_invalid');
	}


	uc_user_edit($member['username'], $new_password, $new_password, $member['email'], 1, 0);

	$new_password = md5($new_password);

	DB::query("UPDATE ".DB::table('common_member')." SET password='$new_password' WHERE uid='$member[uid]'");

	DB::query("UPDATE ".DB::table('common_member_field_forum')." SET authstr='' WHERE uid='$member[uid]'");

	//更新短信发送状态 已完成 32
	DB::query("UPDATE ".DB::table('common_verifycode')." SET status=32 WHERE mobile='$mobile'");


	$count = DB::result_first("SELECT count(mobile) FROM ".DB::table('common_member_profile')." WHERE mobile='$mobile'");
	if($count == 1)
	{
		DB::query("UPDATE ".DB::table('common_member')." SET mobilestatus=1 WHERE uid=$member[uid]");
	}
	odz_success('changesuccess');
}


if($_GET['action'] == 'unbind_send') {

	if(!$_G['uid']){
		odz_error('user_not_login', '-20');
	}

	$query = DB::query("SELECT mobile FROM ".DB::table('common_member_profile')." WHERE uid='{$_G['uid']}'");
	$member = DB::fetch($query);

	if(empty($member['mobile'])){
		odz_error('user_not_bind', '-21');
	}

	$verifycode = rand(100000,999999);

	$verifycode = str_replace('1989','9819',$verifycode);
	$verifycode = str_replace('1259','9521',$verifycode);
	$verifycode = str_replace('12590','09521',$verifycode);
	$verifycode = str_replace('10086','68001',$verifycode);

	$content = $_G['cache']['plugin']['smstong']['mobilebindmsg'];
	$rp = array('$mobile', '$verifycode');
	$sm = array($member['mobile'], $verifycode);
	$content = str_replace($rp, $sm, $content);

	if(sned_msg($member['mobile'], $content, 7, $verifycode)){

		odz_success('success');
		exit;
	}

}

if($_GET['action'] == 'unbind_check'){

	if(!$_G['uid']){
		odz_error('user_not_login', '-20');
	}
	$query = DB::query("SELECT mobile FROM ".DB::table('common_member_profile')." WHERE uid='{$_G['uid']}'");
	$member = DB::fetch($query);
	$mobile = $member['mobile'];

	//验证码
	$verifycode = trim($_GET['verifycode']);
	//短信通验证码有效期
	$periodofvalidity = $_G['cache']['plugin']['smstong']['periodofvalidity'];

	if(!empty($mobile) && empty($verifycode)) {

		odz_error('verifycode_empty', '-18');
	}

	$verify = DB::result_first("SELECT mobile FROM ".DB::table('common_verifycode')." WHERE mobile='$mobile' AND verifycode='$verifycode' AND getip='$_G[clientip]' AND status=7 AND dateline>'$_G[timestamp]'-$periodofvalidity");

	//验证码验证失败
	if (!empty($mobile) && !$verify)
	{
		odz_error('verifycode_invalid', '-19');
	}

	odz_success('success');
}

if($_GET['action'] == 'bind_send') {

	if(!$_G['uid']){
		odz_error('user_not_login', '-20');
	}


	$mobile = trim($_GET['mobile']);
	if(!mobile_check($mobile)) {
		odz_error('error_phone_number', '-10');
	}

	//验证手机是否已经绑定过
	$query = DB::query("SELECT mobile FROM ".DB::table('common_member_profile')." WHERE mobile='{$mobile}'");
	$mobile_check = DB::fetch($query);
	if(!empty($mobile_check)){
		odz_error('mobile_bound', '-20');
	}
	$verifycode = rand(100000,999999);

	$verifycode = str_replace('1989','9819',$verifycode);
	$verifycode = str_replace('1259','9521',$verifycode);
	$verifycode = str_replace('12590','09521',$verifycode);
	$verifycode = str_replace('10086','68001',$verifycode);

	$content = $_G['cache']['plugin']['smstong']['mobilebindmsg'];
	$rp = array('$mobile', '$verifycode');
	$sm = array($mobile, $verifycode);
	$content = str_replace($rp, $sm, $content);

	if(sned_msg($mobile, $content, 9, $verifycode)){

		odz_success('success');
	}

}


if($_GET['action'] == 'bind') {

	if(!$_G['uid']){
		odz_error('user_not_login', '-20');
	}

	$query = DB::query("SELECT mobile FROM ".DB::table('common_member_profile')." WHERE uid='{$_G['uid']}'");
	$member = DB::fetch($query);
	$mobile = $member['mobile'];

	//没有绑定的手机号
	if(!empty($mobile)){
		//
		//
		$old_verifycode = trim($_GET['old_verifycode']);
		//短信通验证码有效期
		$periodofvalidity = $_G['cache']['plugin']['smstong']['periodofvalidity'];

		if(!empty($mobile) && empty($old_verifycode)) {

			odz_error('verifycode_empty', '-18');
		}
		//当前绑定手机的验证码
		$verify = DB::result_first("SELECT mobile FROM ".DB::table('common_verifycode')." WHERE mobile='$mobile' AND verifycode='$old_verifycode' AND getip='$_G[clientip]' AND status=7 AND dateline>'$_G[timestamp]'-$periodofvalidity");

		//验证码验证失败
		if (!empty($mobile) && !$verify)
		{
			odz_error('old_verifycode_invalid', '-19');
		}
	}

	$mobile = trim($_GET['mobile']);
	if(!mobile_check($mobile)) {
		odz_error('error_phone_number', '-10');
	}
	//新绑定手机的验证码
	$verifycode = trim($_GET['verifycode']);
	//短信通验证码有效期
	$periodofvalidity = $_G['cache']['plugin']['smstong']['periodofvalidity'];

	if(!empty($mobile) && empty($verifycode)) {

		odz_error('verifycode_empty', '-18');
	}

	//新绑定手机的验证码
	$verify = DB::result_first("SELECT mobile FROM ".DB::table('common_verifycode')." WHERE mobile='$mobile' AND verifycode='$verifycode' AND getip='$_G[clientip]' AND status=9 AND dateline>'$_G[timestamp]'-$periodofvalidity");

	//验证码验证失败
	if (!empty($mobile) && !$verify)
	{
		odz_error('new_verifycode_invalid', '-19');
	}


	//验证新手机是否已经绑定过
	$query = DB::query("SELECT mobile FROM ".DB::table('common_member_profile')." WHERE mobile='{$mobile}'");
	$mobile_check = DB::fetch($query);
	if(!empty($mobile_check)){
		odz_error('mobile_bound', '-20');
	}

	$fields = array();
	$fields['mobile'] = $mobile;
	//DB::query("UPDATE ".DB::table('common_member_profile')." SET mobile='$mobile' WHERE uid=$_G[uid]");
	DB::update('common_member_profile', $fields, array('uid'=>$_G['uid']));

	DB::query("UPDATE ".DB::table('common_verifycode')." SET reguid=$_G[uid],regdateline='$_G[timestamp]',status=10 WHERE mobile='$mobile' AND verifycode='$old_verifycode' AND getip='$_G[clientip]' AND status=7 AND dateline>'$_G[timestamp]'-$periodofvalidity");
	DB::query("UPDATE ".DB::table('common_verifycode')." SET reguid=$_G[uid],regdateline='$_G[timestamp]',status=10 WHERE mobile='$mobile' AND verifycode='$verifycode' AND getip='$_G[clientip]' AND status=9 AND dateline>'$_G[timestamp]'-$periodofvalidity");

	DB::query("UPDATE ".DB::table('common_member')." SET mobilestatus=1 WHERE uid=$_G[uid]");

	$usergroup = DB::result_first("SELECT type FROM ".DB::table('common_usergroup')." WHERE groupid='$_G[groupid]'");
	$groupid = $_G['cache']['plugin']['smstong']['mobilegroup'];

	if ($usergroup['type'] == 'm' && !empty($groupid)) {
		DB::query("UPDATE ".DB::table('common_member')." SET groupid='$groupid' WHERE uid=$_G[uid]");
	}


		odz_success('success');


}








function sned_msg($mobile, $content, $status, $verifycode, $uid = 0, $regdateline = 0){

	global $_G;

	$mobilegap = $_G['cache']['plugin']['smstong']['mobilegap'];
	$status_ok = $status + 1;


	$sended = DB::result_first("SELECT mobile FROM ".DB::table('common_verifycode')." WHERE mobile='$mobile' AND (status=$status or status=$status_ok) AND getip='$_G[clientip]' AND dateline>'$_G[timestamp]'-$mobilegap");
	//已经发送的
	if ($sended)
	{
		odz_error('msg_sended', '-11', array('time' => $mobilegap));
		return false;
	}
	//删除原有的
	DB::query("DELETE FROM ".DB::table('common_verifycode')." WHERE mobile='$mobile' AND status=$status AND getip='$_G[clientip]'");

	$ret = sendsms($_G['cache']['plugin']['smstong']['smsusername'], $_G['cache']['plugin']['smstong']['smspassword'], $mobile, $content);
	if($ret === true)
	{
		$verifycode_data = array(
		'mobile' => $mobile,
		'getip' => $_G['clientip'],
		'verifycode' => $verifycode,
		'dateline' => TIMESTAMP,
		'regdateline' => $regdateline,
		'reguid' => $uid,
		'status' => $status,
		);
		DB::insert('common_verifycode', $verifycode_data);
		return true;
	}
	else
	{
		odz_error($ret, '-13');
	}
}
