|
|
|
|
<?php
|
|
|
|
|
namespace apps\sms\controllers;
|
|
|
|
|
use apps\base\controllers\FrontendController;
|
|
|
|
|
|
|
|
|
|
class IndexController extends FrontendController {
|
|
|
|
|
|
|
|
|
|
protected $mobile;
|
|
|
|
|
//短信验证码
|
|
|
|
|
protected $mobile_code;
|
|
|
|
|
//安全码
|
|
|
|
|
protected $sms_code;
|
|
|
|
|
|
|
|
|
|
protected $flag;
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->mobile = I('mobile');
|
|
|
|
|
$this->mobile_code = I('mobile_code');
|
|
|
|
|
$this->sms_code = I('sms_code');
|
|
|
|
|
$this->flag = I('flag');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//发送
|
|
|
|
|
public function actionSend() {
|
|
|
|
|
if (empty($this->mobile)) {
|
|
|
|
|
exit(json_encode(array('msg' => '手机号码不能为空')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$preg = '/^1[0-9]{10}$/'; //简单的方法
|
|
|
|
|
if (!preg_match($preg, $this->mobile)) {
|
|
|
|
|
exit(json_encode(array('msg' => '手机号码格式不正确')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($_SESSION['sms_mobile']) {
|
|
|
|
|
if (strtotime($this->read_file($this->mobile)) > (time() - 60)) {
|
|
|
|
|
exit(json_encode(array('msg' => '获取验证码太过频繁,一分钟之内只能获取一次。')));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$where['mobile_phone'] = $this->mobile;
|
|
|
|
|
$user_id = $this->db->getOne("SELECT user_id FROM {pre}users WHERE mobile_phone='".$where['mobile_phone']."'");
|
|
|
|
|
if ($this->flag == 'register') {
|
|
|
|
|
//手机注册
|
|
|
|
|
if (!empty($user_id)) {
|
|
|
|
|
exit(json_encode(array('msg' => '手机号码已存在,请更换手机号码')));
|
|
|
|
|
}
|
|
|
|
|
} elseif ($this->flag == 'forget') {
|
|
|
|
|
//找回密码
|
|
|
|
|
if (empty($user_id)) {
|
|
|
|
|
exit(json_encode(array('msg' => "手机号码不存在\n无法通过该号码找回密码")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mobile_code = $this->random(6, 1);
|
|
|
|
|
$message = "您的验证码是:" . $this->mobile_code . ",请不要把验证码泄露给其他人,如非本人操作,可不用理会";
|
|
|
|
|
$send_result = send_sms($this->mobile, $message);
|
|
|
|
|
$this->write_file($this->mobile, date("Y-m-d H:i:s"));
|
|
|
|
|
|
|
|
|
|
if ($send_result === true) {
|
|
|
|
|
$_SESSION['sms_mobile'] = $this->mobile;
|
|
|
|
|
$_SESSION['sms_mobile_code'] = $this->mobile_code;
|
|
|
|
|
exit(json_encode(array('code' => 2, 'mobile_code' => $this->mobile_code,'msg'=>'验证码已发送')));
|
|
|
|
|
} else {
|
|
|
|
|
exit(json_encode(array('msg' => $send_result)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//验证
|
|
|
|
|
public function actionCheck() {
|
|
|
|
|
if ($this->mobile != $_SESSION['sms_mobile'] or $this->mobile_code != $_SESSION['sms_mobile_code']) {
|
|
|
|
|
exit(json_encode(array('msg' => '手机验证码输入错误。','code'=>1)));
|
|
|
|
|
} else {
|
|
|
|
|
exit(json_encode(array('code' => '2')));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function random($length = 6, $numeric = 0) {
|
|
|
|
|
PHP_VERSION < '4.2.0' && mt_srand((double) microtime() * 1000000);
|
|
|
|
|
if ($numeric) {
|
|
|
|
|
$hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
|
|
|
|
|
} else {
|
|
|
|
|
$hash = '';
|
|
|
|
|
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
|
|
|
|
|
$max = strlen($chars) - 1;
|
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
|
$hash .= $chars[mt_rand(0, $max)];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function write_file($file_name, $content) {
|
|
|
|
|
$this->mkdirs(ROOT_PATH . 'data/smslog/' . date('Ymd'));
|
|
|
|
|
$filename = ROOT_PATH . 'data/smslog/' . date('Ymd') . '/' . $file_name . '.log';
|
|
|
|
|
$Ts = fopen($filename, "a+");
|
|
|
|
|
fputs($Ts, "\r\n" . $content);
|
|
|
|
|
fclose($Ts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function mkdirs($dir, $mode = 0777) {
|
|
|
|
|
if (is_dir($dir) || @mkdir($dir, $mode))
|
|
|
|
|
return TRUE;
|
|
|
|
|
if (!$this->mkdirs(dirname($dir), $mode))
|
|
|
|
|
return FALSE;
|
|
|
|
|
return @mkdir($dir, $mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function read_file($file_name) {
|
|
|
|
|
$content = '';
|
|
|
|
|
$filename = ROOT_PATH . 'data/smslog/' . date('Ymd') . '/' . $file_name . '.log';
|
|
|
|
|
if (function_exists('file_get_contents')) {
|
|
|
|
|
@$content = file_get_contents($filename);
|
|
|
|
|
} else {
|
|
|
|
|
if (@$fp = fopen($filename, 'r')) {
|
|
|
|
|
@$content = fread($fp, filesize($filename));
|
|
|
|
|
@fclose($fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$content = explode("\r\n", $content);
|
|
|
|
|
return end($content);
|
|
|
|
|
}
|
|
|
|
|
}
|