westdc-core/Westdc/Member/Account.php

380 lines
9.6 KiB
PHP
Raw Normal View History

<?php
namespace Westdc\Member;
2014-12-18 13:14:30 +00:00
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
2014-11-14 10:02:49 +00:00
use Westdc\EventModel\AbstractEventManager;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\Session as SessionStorage;
use Zend\Authentication\Adapter\DbTable;
2014-11-14 10:02:49 +00:00
use Westdc\Helpers\Assist as view;
use Westdc\Helpers\Config;
2015-02-09 03:37:25 +00:00
use Westdc\Db\Dbh as dbh;
use Westdc\Db\Db as Zend_Db;
2015-02-09 03:37:25 +00:00
use Zend\Db\TableGateway\TableGateway;
2015-02-09 03:37:25 +00:00
class Account extends AbstractEventManager implements ServiceManagerAwareInterface {
public $memberTable = "users";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $GravatarEmailField = "gravatar_email";
2015-02-09 03:37:25 +00:00
public $RoleMember = "member";
2015-02-09 03:37:25 +00:00
private $db;
protected $events = NULL; //事件
private $config;
function __construct () {
$this->config = Config::get();
}
2014-12-18 13:14:30 +00:00
2015-02-09 03:37:25 +00:00
public function init () {
$dbService = $this->serviceManager->get('Db');
$this->db = $dbService->getPdo();
}
2014-12-18 13:14:30 +00:00
2015-02-09 03:37:25 +00:00
public function setServiceManager (ServiceManager $serviceManager) {
$this->serviceManager = $serviceManager;
2014-12-18 13:14:30 +00:00
2015-02-09 03:37:25 +00:00
$this->init();
return $this;
}
2014-12-18 13:14:30 +00:00
//获取账号信息,数组
2015-02-09 03:37:25 +00:00
public function getAccountInfo ($id = 0) {
if($id == 0) {
$id = view::User('id');
}
$sql = "SELECT * FROM {$this->memberTable} WHERE id=$id";
$rs = $this->db->query($sql);
2015-02-09 03:37:25 +00:00
return $rs->fetch();
}
2015-02-09 03:37:25 +00:00
/**
* 用户注册
* @param $data
* @return array
*/
public function register ($data) {
$params = compact('data');
2015-02-09 03:37:25 +00:00
$results = $this->getEventManager()->trigger('register.pre', $this, $params);
$cache_data = $results->last();
2015-02-09 03:37:25 +00:00
if($cache_data !== true) {
if(!is_array($cache_data)) {
return array('error' => $cache_data);
}
else {
return $cache_data;
}
}
2015-01-18 16:00:34 +00:00
2015-02-09 03:37:25 +00:00
/**
* 将数据分为两组,一组用于做注册成功后的登陆,一组用于写入数据库
* 写入数据库的数据需要完全重建一个新的,防止因为前端传入时有人恶意增加字段造成破坏
*/
$loginData = array(
2015-02-09 03:37:25 +00:00
'username' => $data['username'],
'password' => $data['password']
);
2015-01-18 16:00:34 +00:00
2015-02-09 03:37:25 +00:00
$registerData = [
'username' => $data['username'],
'password' => md5($data['password']),
'usertype' => $this->RoleMember,
'email' => $data['email'],
'realname' => $data['realname'],
'phone' => $data['phone'],
'unit' => $data['unit'],
'address' => $data['address']
2015-02-09 03:37:25 +00:00
];
// $dbh = new TableGateway($this->memberTable, Zend_Db::getInstance());
// $results = $dbh->insert($registerData);
$dbh = new dbh();
2015-02-09 03:37:25 +00:00
$id = $dbh->insert($this->memberTable, $registerData, true);
if(!empty($id) && is_numeric($id)) {
$this->storeLogin($loginData);
2015-01-18 16:00:34 +00:00
2015-02-09 03:37:25 +00:00
if(isset($state['success'])) {
2015-01-19 12:18:46 +00:00
$mb = new Member();
2015-02-09 03:37:25 +00:00
$mb->putcookie($data[$this->FieldUsername], $data[$this->FieldPasword]);
}
2015-01-18 16:00:34 +00:00
2015-02-09 03:37:25 +00:00
$params = compact('data', 'id');
$this->getEventManager()->trigger('register.success', $this, $params);
return array("success" => 1);
2015-02-09 03:37:25 +00:00
}
else {
if($id === false) {
return array('error' => '服务器开小差了,请稍后再试');
}
else {
return array('error' => '服务器处理中遇到错误,请联系管理员');
}
}
2015-02-09 03:37:25 +00:00
}//register
2015-01-18 16:00:34 +00:00
2015-02-09 03:37:25 +00:00
/**
* 用户登陆
* @param $data
* @return array
*/
public function login ($data) {
$results = $this->getEventManager()->trigger('login.pre', $this, compact('data'));
$cache_data = $results->last();
2015-02-09 03:37:25 +00:00
if($cache_data !== true) {
if(!is_array($cache_data)) {
return array('error' => $cache_data);
}
else {
return $cache_data;
}
}
2015-02-09 03:37:25 +00:00
$state = $this->storeLogin($data);
2015-02-09 03:37:25 +00:00
if(isset($state['success'])) {
$mb = new Cookie();
$mb->putcookie($data[$this->FieldUsername], $data[$this->FieldPasword]);
$user = (array)$state['user'];
$this->getEventManager()->trigger('login.success', $this, compact('user'));
}
else {
$this->getEventManager()->trigger('login.failed', $this, compact('data'));
}
2015-01-18 16:00:34 +00:00
return $state;
}//login
2015-01-18 16:00:34 +00:00
2015-02-09 03:37:25 +00:00
/**
* 存储用户登陆信息
* 为了防止login中的用户信息检查不规范再加入一层内置的数据库权限检查以防通过漏洞登入系统
* @param $data
* @param bool $md5 是否对密码进行md5加密再校验
* @return array
*/
private function storeLogin ($data, $md5 = true) {
$auth = new AuthenticationService();
$auth->setStorage(new SessionStorage($this->config->session_namespace));
2015-01-26 01:54:47 +00:00
$dbAdapter = Zend_Db::getInstance();
2015-02-09 03:37:25 +00:00
$authAdapter = new DbTable($dbAdapter, 'users', 'username', 'password');
if($md5 === true) {
$password = md5($data['password']);
2015-02-09 03:37:25 +00:00
}
else {
$password = $data['password'];
}
2015-02-09 03:37:25 +00:00
$authAdapter->setIdentity($data['username'])->setCredential($password);
$result = $authAdapter->authenticate();
2015-01-16 15:37:40 +00:00
2015-02-09 03:37:25 +00:00
if(!$result->isValid()) {
return array("error" => "用户信息验证失败");
}
2015-01-16 15:37:40 +00:00
2015-02-09 03:37:25 +00:00
$user = $authAdapter->getResultRowObject(null, array('password'));
$auth->getStorage()->write($user);
2015-01-16 15:37:40 +00:00
return array(
2015-02-09 03:37:25 +00:00
'success' => 1,
'user' => $user
);
2015-01-18 16:00:34 +00:00
}//storeLogin
2015-02-09 03:37:25 +00:00
public function cookieLogin ($data) {
return $this->storeLogin($data, false);
}
2015-02-09 03:37:25 +00:00
//注册信息参数
2015-02-09 03:37:25 +00:00
public function getParam (\Zend_Controller_Request_Abstract $request) {
$data = array(
2015-02-09 03:37:25 +00:00
'username' => $request->getParam('username'),
'password' => $request->getParam('password'),
'confirm_password' => $request->getParam('confirm_password'),
'email' => $request->getParam('email'),
'realname' => $request->getParam('realname'),
'phone' => $request->getParam('phone'),
'unit' => $request->getParam('unit'),
'address' => $request->getParam('address')
);
2015-02-09 03:37:25 +00:00
return $data;
}
2015-02-09 03:37:25 +00:00
//获取用户账户修改参数
2015-02-09 03:37:25 +00:00
public function getEditParam ($request) {
$request = new \Zend\Http\PhpEnvironment\Request;
2015-02-09 03:37:25 +00:00
$type = $request->getPost('type');
2015-02-09 03:37:25 +00:00
if($type == "general") {
$data = array(
2015-02-09 03:37:25 +00:00
'realname' => $request->getPost('realname'),
'signature' => $request->getPost('signature'),
'description' => $request->getPost('description')
);
}
2015-02-09 03:37:25 +00:00
if($type == "password") {
$data = array(
2015-02-09 03:37:25 +00:00
'password' => $request->getPost('password'),
'password_new' => $request->getPost('password_new'),
'password_confirm' => $request->getPost('password_confirm')
);
}
2015-02-09 03:37:25 +00:00
return $data;
}
2015-02-09 03:37:25 +00:00
//编辑
2015-02-09 03:37:25 +00:00
public function edit ($data, $type) {
$results = $this->getEventManager()->trigger('edit.checkParam', $this, compact('data', 'type'));
$cache_data = $results->last();
2015-02-09 03:37:25 +00:00
if($cache_data !== true) {
return $cache_data;
}
2015-02-09 03:37:25 +00:00
if($type == "general") {
$data['signature'] = htmlspecialchars($data['signature']);
$data['description'] = htmlspecialchars($data['description']);
}
2015-02-09 03:37:25 +00:00
else {
if($type == "password") {
$data['password'] = md5($data['password_new']);
unset($data['password_new']);
unset($data['password_confirm']);
}
else {
return "参数错误";
}
}
$dbh = new dbh();
$uid = view::User('id');
2015-02-09 03:37:25 +00:00
if($dbh->update($this->memberTable, $data, " id=$uid") === true) {
return true;
2015-02-09 03:37:25 +00:00
}
else {
return false;
}
}
2015-02-09 03:37:25 +00:00
//找回密码
2015-02-09 03:37:25 +00:00
public function getMyPassword ($email) {
$pwdListener = new PwdListener;
$this->getEventManager()->attachAggregate($pwdListener);
2015-02-09 03:37:25 +00:00
$results = $this->getEventManager()->trigger('pwd.forgot.checkParam', $this, compact('email'));
$cache_data = $results->last();
2015-02-09 03:37:25 +00:00
if($cache_data !== true) {
return $cache_data;
}
$sql = "SELECT * FROM {$this->memberTable} WHERE email='$email'";
$rs = $this->db->query($sql);
$row = $rs->fetch();
2015-02-09 03:37:25 +00:00
if(!isset($row['username']) || empty($row['username'])) {
return array(
'error' => "此邮箱并未注册",
'place' => 'email'
);
}
$salt = md5($email.'---'.$row['username']);
2015-02-09 03:37:25 +00:00
$sql = "UPDATE {$this->memberTable} SET salt='$salt' WHERE id={$row['id']}";
$state = $this->db->exec($sql);
2015-02-09 03:37:25 +00:00
if($state < 1) {
return array(
'error' => "处理中出现错误,请重试",
'place' => 'email'
);
}
2015-02-09 03:37:25 +00:00
$mail_template = "forgotpassword";
$mail_data = array(
2015-02-09 03:37:25 +00:00
'name' => $row['realname'],
'link' => view::getHostLink().'/account/getpassword/?salt='.$salt
);
2015-02-09 03:37:25 +00:00
try {
$mail = new Mail();
2015-02-09 03:37:25 +00:00
$mail->loadTemplate($mail_template, $mail_data);
$mail->addTo($email, $row['realname']);
$mail->send();
2015-02-09 03:37:25 +00:00
}
catch (Exception $e) {
echo "".$e->getMessage();
}
2015-02-09 03:37:25 +00:00
return array("success" => 1);
}
2015-02-09 03:37:25 +00:00
//重置密码
2015-02-09 03:37:25 +00:00
public function resetPassword ($data) {
$results = $this->getEventManager()->trigger('pwd.reset.checkParam', $this, compact('data'));
$cache_data = $results->last();
2015-02-09 03:37:25 +00:00
if($cache_data !== true) {
return $cache_data;
}
2015-02-09 03:37:25 +00:00
$sql = "SELECT * FROM {$this->memberTable} WHERE salt=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($data['salt']));
$row = $sth->fetch();
2015-02-09 03:37:25 +00:00
if(!isset($row['username']) || empty($row['username'])) {
return array(
'error' => "您提供的校验码不正确,请重新申请重置密码",
'place' => 'confirm_password'
);
}
2015-02-09 03:37:25 +00:00
if($row['username'] !== $data['username']) {
return array(
'error' => "您提供的校验码不正确,请重新申请重置密码",
'place' => 'confirm_password'
);
}
2015-02-09 03:37:25 +00:00
$sql = "UPDATE {$this->memberTable} SET password='".md5($data['password'])."',salt='' WHERE id={$row['id']}";
$this->db->exec($sql);
2015-02-09 03:37:25 +00:00
$mail_template = "getpassworded";
$mail_data = array(
2015-02-09 03:37:25 +00:00
'name' => $row['realname'],
);
$mail = new Mail();
2015-02-09 03:37:25 +00:00
$mail->loadTemplate($mail_template, $mail_data);
$mail->addTo($row['email'], $row['realname']);
$mail->send();
2015-02-09 03:37:25 +00:00
return true;
2015-02-09 03:37:25 +00:00
}
2015-02-09 03:37:25 +00:00
}