标题: 新功能:自动短信注册/修改密码
时间: 2013-07-22
『回复列表(16|隐藏机器人聊天)』
<?php
/**
* 小文短信猫平台注册接口
*
* 短信猫提供者网站:
* http://sms.wap21.net/
*/
try {
include './config.inc.php';
$index = 'hu60#';
$adminPhone = '15386332951';
$key = '保密';
if ($_POST['smskey'] !== $key) {
exit('2'); //通讯密令错误
}
$phone = $_POST['phone'];
if (($len = strlen($phone))!=11) {
throw new Exception("手机号码长度不正确,应为11位,实为{$len}位。", 22);
}
$msg = substr($_POST['message'], strlen($index));
$pos = strpos($msg, '#');
/*新注册或修改密码*/
if ($pos === false) {
$uinfo = user::getinfobyregphone($phone, 'uid,name');
if ($uinfo === false) {
regNew($phone, $msg, $phone);
} else {
changePasswd($uinfo['uid'], $msg);
}
} else {
$name = substr($msg, 0, $pos);
$pass = substr($msg, $pos+1);
$uinfo = user::getinfobyname($name);
$uinfo2 = user::getinfobyregphone($phone);
if ($uinfo === false && $uinfo2 === false) {
regNew($name, $pass, $phone);
} else {
bindPhone($name, $pass, $phone, $uinfo, $uinfo2);
}
}
} catch(Exception $e) {
$log = $e->getMessage();
if ($log !== '') {
logAdd($phone, $e->getMessage());
}
exit((string)$e->getCode());
}
/*注册新用户*/
function regNew($name, $pass, $phone) {
if (strlen($name)>25) {
throw new Exception("注册失败,用户名过长。用户名不能超过25字节,即不能超过8个汉字或25个英文字母。", 22);
}
if (!preg_match('!^[\x{4e00}-\x{9fa5}a-zA-Z0-9_-]+$!us', $name)) {
throw new Exception("注册失败,用户名包含特殊字符。用户名只允许汉字(不包括标点符号)、数字、英文大小写字母、下划线(_)和减号(-)。", 22);
}
$uinfo = user::getinfobyname($name, 'uid');
if ($uinfo !== false) {
throw new Exception("注册失败,用户“{$name}”已存在,请更换用户名。”", 22);
}
$sql = 'insert into user(name,pass,regphone,regtime,mtime,sid) values(?,?,?,?,?,?)';
$sid = microtime(true);
$rs = db::conn()->prepare($sql);
if (!$rs || !$rs->execute(array($name, md5($pass), $phone, time(), 0, $sid))) {
throw new Exception("用户注册失败,数据库错误。", 22);
} else {
throw new Exception("用户“{$name}”注册成功。", 1);
}
}
/*修改密码*/
function changePasswd($uid, $pass) {
$sql = 'update user set pass=?, mtime=0 where uid=?';
var_dump($uid);
$rs = db::conn()->prepare($sql);
if (!$rs || !$rs->execute(array(md5($pass), $uid))) {
throw new Exception("密码修改失败,数据库出现错误。", 22);
} else {
throw new Exception("密码修改成功。", 11);
}
}
/*绑定手机号*/
function bindPhone($name, $pass, $phone, $uinfo, $uinfo2) {
global $index;
//var_dump($uinfo);die;
if ($uinfo['regphone'] !== NULL) {
throw new Exception("手机号绑定失败,用户“{$name}”已绑定手机,解绑请与管理员联系。要修改您的密码,请用该手机直接发送“{$index}密码”到指定号码,不需要包括用户名。", 22);
} elseif ($uinfo2 !== false) {
throw new Exception("手机号绑定失败,该手机已与用户“{$uinfo2['name']}”绑定,更换号码请与管理员联系。要修改密码,请用绑定的手机直接发送“{$index}密码”到指定号码,不需要包括用户名。", 22);
} elseif ($uinfo['pass'] !== md5($pass)) {
throw new Exception("手机号绑定失败,密码错误。", 22);
} else {
$sql = 'update user set regphone=? where uid=?'; $rs = user::$db->prepare($sql);
if (!$rs || !$rs->execute(array($phone, $uinfo['uid']))) {
throw new Exception("手机号绑定失败,数据库出现错误。", 22);
} else {
throw new Exception("手机号绑定成功。", 1);
}
}
}
/*操作日志*/
function logAdd($phone, $msg) {
$sql = 'insert into smslog(phone, msg, time) values(?, ?, ?)';
$rs = db::conn()->prepare($sql);
$rs->execute(array($phone, $msg, time()));
}