resolve conflicts by rm and copy

This commit is contained in:
wlx 2014-05-27 07:04:25 +00:00
parent b55e23cf2e
commit 736cc6e3b7
18 changed files with 1571 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace Helpers;
class Captcha extends \Zend_Controller_Plugin_Abstract
{
public $captcha;
private $sessionName = "captcha";
private $imgDir = "images/captcha";
function __construct($db = NULL)
{
$this->loadCaptcha();
}
public function loadCaptcha()
{
$this->captcha = new \Zend_Captcha_Image(array(
'captcha' => 'Image',
'wordLen' => 4,
'fontsize'=>16,
'width' => 100,
'height' => 38,
'dotNoiseLevel'=>2,
'lineNoiseLevel'=>1,
'timeout' => 300,
'font' => '../data/fonts/ggbi.ttf',
'imgDir' => $this->imgDir,
'imgUrl' => '/images/captcha',
));
}
public function setCaptcha(){
if(!is_dir($this->imgDir))
{
mkdir($this->imgDir);
}
$this->captcha->generate();
$_SESSION[$this->sessionName] = $this->captcha->getWord();
$url = $this->captcha->getImgUrl()
.$this->captcha->getId()
.$this->captcha->getSuffix();
return $url;
}
public function isValid($captchaword)
{
if($captchaword == $_SESSION[$this->sessionName])
{
return true;
}else{
return false;
}
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Helpers;
class MCrypt
{
function __construct()
{
$this->config = \Zend_Registry::get('config');
}
static function encrypt($decrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
$key = hash('SHA256', $salt . $password, true);
srand();
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
return $iv_base64 . $encrypted;
}
static function decrypt($encrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
$key = hash('SHA256', $salt . $password, true);
$iv = base64_decode(substr($encrypted, 0, 22) . '==');
$encrypted = substr($encrypted, 22);
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
$hash = substr($decrypted, -32);
$decrypted = substr($decrypted, 0, -32);
if (md5($decrypted) != $hash) return false;
return $decrypted;
}
}

View File

@ -0,0 +1,379 @@
<?php
namespace Users;
use \Helpers\View as view;
use \Helpers\dbh as dbh;
use \Users\Member;
use \Mail\Mail;
use \Users\Listener\AccountListener;
use \Users\Listener\EditListener;
use \Users\Listener\PwdListener;
class Account extends \Zend_Controller_Plugin_Abstract
{
public $memberTable = "users";
public $FieldIndex = "id";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $GravatarEmailField = "gravatar_email";
public $FieldRealname = "realname";
public $RoleMember = "member";
private $db;
protected $events = NULL; //事件
/*
需要挂载的事件分别放在不同的listener中将各种操作进行模块化细分
*/
function __construct($initializingListener = TRUE,$db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
if($initializingListener === TRUE)
{
$this->loadListener();
}
}
public function loadListener()
{
//主要操作账号注册登录设置session等
$AccountListener = new AccountListener();
@$this->events()->attachAggregate($AccountListener);
//账户编辑
$EditListener = new EditListener();
@$this->events()->attachAggregate($EditListener);
//账户安全性(找回密码)
$PwdListener = new PwdListener();
@$this->events()->attachAggregate($PwdListener);
}
public function events(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
//获取账号信息,数组
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);
return $rs->fetch();
}
//注册
public function register($data)
{
$params = compact('data');
$results = $this->events()->trigger('register.checkParam', $this, $params);
$cache_data = $results->bottom();
if($cache_data !== true)
{
if(!is_array($cache_data))
{
return array('error'=>$cache_data);
}else{
return $cache_data;
}
}
$results = $this->events()->trigger('register.checkUser', $this, $params);
$cache_data = $results->bottom();
if($cache_data !== true)
{
if(!is_array($cache_data))
{
return array('error'=>$cache_data);
}else{
return $cache_data;
}
}
$loginData = array(
'username'=>$data['username'],
'password'=>$data['password']
);
$data['password'] = md5($data['password']);
$data['usertype'] = "member";
unset($data['confirm_password']);
$dbh = new dbh();
$id = $dbh->insert($this->memberTable,$data,true);
if(!empty($id) && is_numeric($id))
{
$this->storeLogin($loginData);
$mb = new Member();
$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); //username, md5(password)
$params = compact('data','id');
$results = $this->events()->trigger('register.success', $this, $params);
return array("success" => 1);
}else{
if($id === false)
{
return array('error'=>'服务器开小差了,请稍后再试');
}else{
return array('error'=>'服务器处理中遇到错误,请联系管理员');
}
}
}//register
//登陆
public function login($data,$return_user_info = false)
{
$results = $this->events()->trigger('login.checkParam', $this, compact('data'));
$cache_data = $results->bottom();
if($cache_data !== true)
{
if(!is_array($cache_data))
{
return array('error'=>$cache_data);
}else{
return $cache_data;
}
}
$state = $this->storeLogin($data);
$mb = new Member();
$mb->putcookie($data[$this->FieldUsername],md5($data[$this->FieldPasword]));
if(!$return_user_info)
return $state;
else
return view::User();
}//login
//storeLogin
//将登录信息保存在session和cookie中
public function storeLogin($data,$md5verify = true)
{
$auth = \Zend_Auth::getInstance();
$authAdapter = new \Zend_Auth_Adapter_DbTable($this->db);
$authAdapter->setTableName($this->memberTable)
->setIdentityColumn($this->FieldUsername)
->setCredentialColumn($this->FieldPasword);
if(empty($data[$this->FieldPasword]))
{
$password = "0";
}else{
if($md5verify == false)
{
$password = $data[$this->FieldPasword];
}else{
$password = md5($data[$this->FieldPasword]);
}
}
$authAdapter->setIdentity($data[$this->FieldUsername])->setCredential($password);
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
$user = $authAdapter->getResultRowObject(null,$this->FieldPasword);
$email = $user->email;
$results = $this->events()->trigger('login.success.createAvatar', $this, compact('email'));
$user->avatar = $results->bottom();
$auth->getStorage()->write($user);
$id = $user->id;
@$results = $this->events()->trigger('login.success.updateStatus', $this, compact('id'));
return array('success'=>1);
}else{
return array("error"=>"登录失败,请重试");
}
return array('error'=>'处理中发现错误,请重试');
}
//注册信息参数
public function getParam(\Zend_Controller_Request_Abstract $request)
{
$data = array(
'username'=>$request->getParam('username'),
'password'=>$request->getParam('password'),
'confirm_password'=>$request->getParam('confirm_password'),
'email'=>$request->getParam('email'),
'realname'=>$request->getParam('realname')
);
return $data;
}
//获取用户账户修改参数
public function getEditParam(\Zend_Controller_Request_Abstract $request)
{
$type = $request->getParam('type');
if($type == "general")
{
$data = array(
'realname'=>$request->getParam('realname'),
'signature'=>$request->getParam('signature'),
'description'=>$request->getParam('description')
);
}
if($type == "password")
{
$data = array(
'password' => $request->getParam('password'),
'password_new'=>$request->getParam('password_new'),
'password_confirm'=>$request->getParam('password_confirm')
);
}
return $data;
}
//编辑
public function edit($data,$type)
{
$results = $this->events()->trigger('edit.checkParam', $this, compact('data','type'));
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
if($type == "general")
{
$data['signature'] = htmlspecialchars($data['signature']);
$data['description'] = htmlspecialchars($data['description']);
}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');
if($dbh->update($this->memberTable,$data," id=$uid") === true)
{
return true;
}else{
return false;
}
}
//找回密码
public function getMyPassword($email)
{
$results = $this->events()->trigger('pwd.forgot.checkParam', $this, compact('email'));
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
$sql = "SELECT * FROM {$this->memberTable} WHERE email='$email'";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(!isset($row['username']) || empty($row['username']))
{
return array('error'=>"此邮箱并未注册",'place'=>'email');
}
$salt = md5($email.'---'.$row['username']);
$sql = "UPDATE {$this->memberTable} SET salt='$salt' WHERE id={$row['id']}";
$state = $this->db->exec($sql);
if($state<1)
{
return array('error'=>"处理中出现错误,请重试",'place'=>'email');
}
$mail_template = "forgotpassword";
$mail_data = array(
'name'=>$row['realname'],
'link'=> view::getHostLink().'/account/getpassword/salt/'.$salt
);
$mail = new Mail();
$mail->loadTemplate($mail_template,$mail_data);
$mail->addTo($email,$row['realname']);
$mail->send();
return array("success"=>1);
}
//重置密码
public function resetPassword($data)
{
$results = $this->events()->trigger('pwd.reset.checkParam', $this, compact('data'));
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
$sql = "SELECT * FROM {$this->memberTable} WHERE salt=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($data['salt']));
$row = $sth->fetch();
if(!isset($row['username']) || empty($row['username']))
{
return array('error'=>"您提供的校验码不正确,请重新申请重置密码",'place'=>'confirm_password');
}
if($row['username'] !== $data['username'])
{
return array('error'=>"您提供的校验码不正确,请重新申请重置密码",'place'=>'confirm_password');
}
$sql = "UPDATE {$this->memberTable} SET password='".md5($data['password'])."',salt='' WHERE id={$row['id']}";
$this->db->exec($sql);
$mail_template = "getpassworded";
$mail_data = array(
'name'=>$row['realname'],
);
$mail = new Mail();
$mail->loadTemplate($mail_template,$mail_data);
$mail->addTo($row['email'],$row['realname']);
$mail->send();
return true;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Users\Event;
interface EditEvent
{
public function checkParam(\Zend_EventManager_Event $e);
public function editSuccess(\Zend_EventManager_Event $e);
}

View File

@ -0,0 +1,12 @@
<?php
namespace Users\Event;
interface LoginEvent
{
public function checkParam(\Zend_EventManager_Event $e);
public function updateStatus(\Zend_EventManager_Event $e);
public function createAvatar(\Zend_EventManager_Event $e);
}

View File

@ -0,0 +1,13 @@
<?php
namespace Users\Event;
interface PwdEvent
{
public function forgotPwdCheckParam(\Zend_EventManager_Event $e);
public function sendGetPasswordMail(\Zend_EventManager_Event $e);
public function resetPwdCheckParam(\Zend_EventManager_Event $e);
}

View File

@ -0,0 +1,13 @@
<?php
namespace Users\Event;
interface RegisterEvent
{
public function checkParam(\Zend_EventManager_Event $e);
public function checkUser(\Zend_EventManager_Event $e);
public function registerSuccess(\Zend_EventManager_Event $e);
}

View File

@ -0,0 +1,20 @@
<?php
namespace Users;
class Gravatar{
function Get( $email, $size='' ) {
$default = "http://heihedata.org/static/img/gCons/agent.png";
if(empty($size))
{
$size = 40;
}
$url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . urlencode( $default ) . "&s=" . $size;
return $url;
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace Users\Listener;
use \Users\Operation\RegisterOperate;
use \Users\Operation\LoginOperate;
class AccountListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
private $type;
function __construct($type = "")
{
$this->event = new \Zend_EventManager_EventManager();
if(empty($type))
{
$type = "both";
}
$this->type = $type;
}
public function attach(\Zend_EventManager_EventCollection $events)
{
if($this->type == "both")
{
$this->attachRegisterEvents($events);
$this->attachLoginEvents($events);
}
if($this->type == "register")
{
$this->attachRegisterEvents($events);
}
if($this->type == "login")
{
$this->attachLoginEvents($events);
}
}
public function detach(\Zend_EventManager_EventCollection $events)
{
}
private function attachRegisterEvents(\Zend_EventManager_EventCollection $events)
{
$_Events = new RegisterOperate();
$events->attach('register.checkParam', array($_Events, 'checkParam'), 100);
$events->attach('register.checkUser', array($_Events, 'checkUser'), 80);
$events->attach('register.success', array($_Events, 'registerSuccess'), 50);
}
private function attachLoginEvents(\Zend_EventManager_EventCollection $events)
{
$_Events = new LoginOperate();
$events->attach('login.checkParam', array($_Events, 'checkParam'), 100);
$events->attach('login.success.updateStatus', array($_Events, 'updateStatus'), 50);
$events->attach('login.success.createAvatar', array($_Events, 'createAvatar'), 50);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Users\Listener;
use \Users\Operation\EditOperate;
class EditListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
private $type;
function __construct($type = "")
{
$this->event = new \Zend_EventManager_EventManager();
if(empty($type))
{
$type = "both";
}
$this->type = $type;
}
public function attach(\Zend_EventManager_EventCollection $events)
{
$_Events = new EditOperate();
$events->attach('edit.checkParam', array($_Events, 'checkParam'), 100);
$events->attach('edit.success', array($_Events, 'editSuccess'), 50);
}
public function detach(\Zend_EventManager_EventCollection $events)
{
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Users\Listener;
use \Users\Operation\PwdOperate;
class PwdListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
private $type;
function __construct($type = "")
{
$this->event = new \Zend_EventManager_EventManager();
}
public function attach(\Zend_EventManager_EventCollection $events)
{
$_Events = new PwdOperate();
$events->attach('pwd.forgot.checkParam', array($_Events, 'forgotPwdCheckParam'), 100);
$events->attach('pwd.forgot.sendmail', array($_Events, 'sendGetPasswordMail'), 50);
$events->attach('pwd.reset.checkParam', array($_Events, 'resetPwdCheckParam'), 100);
$events->attach('pwd.reset.sendmail', array($_Events, 'sendGetPasswordMail'), 50);
}
public function detach(\Zend_EventManager_EventCollection $events)
{
}
}

View File

@ -0,0 +1,187 @@
<?php
namespace Users;
use \Helpers\View as view;
use \Helpers\dbh as dbh;
use \Users\Account;
use \Helpers\Table;
/*
\Users\Local 进行抽象,满足后台调用的需求,屏蔽一些错误等等
*/
class Local extends \Zend_Controller_Plugin_Abstract
{
private $db;
protected $events = NULL; //事件
public $table;
public $account;
function __construct($db = NULL,$mail = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('6config');
$this->table = new \Helpers\Table();
}
//添加本地帐号
public function addLocalUser($data = NULL)
{
if(empty($data) || !is_array($data))
{
$data = $this->getLocalParam();
}
if(!isset($data['userid']) || !is_numeric($data['userid']))
{
return "参数错误";
}
if(empty($data['usertype']))
{
return "请选择用户类型";
}
$user = $this->fetchUser($data['userid']);
$local_user = $this->checkLocalUserExists($data['userid']);
if(isset($local_user['uid']) && $local_user['uid'] > 0)
{
$localUserExists = TRUE;
}else{
$localUserExists = FALSE;
}
if(!isset($user['id']) || $user['id'] < 1 )
{
if($localUserExists)
{
$this->deleteUser($data['userid']);
}
return "该用户不存在";
}
$cache_data = array(
'uid'=>$data['userid'],
'usertype'=>$data['usertype']
);
if($localUserExists)
{
$status = $this->addUser($cache_data,$local_user['uid']);
}else{
$status = $this->addUser($cache_data);
}
if($status === true)
{
return true;
}else{
return $status;
}
}
# 获取添加本地用户的参数
public function getLocalParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$data = array(
'userid'=> (int)$request->getParam('userid'),
'usertype'=> trim($request->getParam('usertype'))
);
return $data;
}
# 在总数据库查询所要添加到本地的用户ID信息
public function fetchUser($id =0)
{
$sql = "SELECT * FROM {$this->table->users} WHERE id=$id LIMIT 1";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row ;
}
//检查用户是否在本地库
public function checkLocalUserExists($id)
{
$sql = "SELECT * FROM {$this->table->users_local} WHERE uid=$id LIMIT 1";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row;
}
# 如果传入用户ID为空写入新用户否则修改用户
public function addUser($data,$id = 0)
{
$dbh = new dbh();
$id = (int)$id;
if(empty($id))
{
$status = $dbh->insert($this->table->users_local,$data);
if($status)
{
return true;
}else{
return "用户信息写入失败";
}
}else{
$status = $dbh->update($this->table->users_local,$data);
if($status)
{
return true;
}else{
return "用户信息更新失败";
}
}
return;
}
# 查询用户列表
public function listUser($key)
{
#所有本地用户列表显示
if(empty($key))
{
$dbh = new dbh();
$sql = "SELECT * FROM {$this->table->users_local}";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row ;
}
#按条件查询本地用户
}
# 删除用户
public function deleteUser($id)
{
if(!empty($id) | is_numeric($id))
{
$sql = "DELETE FROM {$this->table->users_local} WHERE uid=$id";
@$this->db->exec($sql);
return "该用户已删除";
}else{
return '删除用户操作失败';
}
}
}

View File

@ -0,0 +1,138 @@
<?php
namespace Users;
class Member
{
var $ck='DCC3ER4T8L2EFX94OPDF';
var $db; //传入PDO对象
var $mid; //会员ID
public $scr; //cookie 安全码 $_COOKIE['scr']
public $user;//cookie User $_COOKIE['user']
public $srpwd;//执行checkcookie后方可调用
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";
public $RoleMember = "member";
function __construct()
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
if(!empty($_COOKIE['scr']))
{
$this->scr = $_COOKIE['scr'];
}
if(!empty($_COOKIE['user']))
{
$this->user= $_COOKIE['user'];
}
}
/**
* 检测cookie
*/
public function checkcookie()
{
$uname = $this->user;
$hash = $this->scr;
if(!empty($uname) && !empty($hash))
{
if (preg_match("/[<|>|#|$|%|^|*|(|)|{|}|'|\"|;|:]/i",$uname) || preg_match("/[<|>|#|$|%|^|*|(|)|{|}|'|\"|;|:]/i",$hash))
{
$this->mid=0;
return false;
}
else{
$sql = "select {$this->FieldUsername} as userid,{$this->FieldPasword} as pwd from {$this->memberTable} where {$this->FieldUsername}='$uname'";
$rs = $this->db->query($sql);
$row = $rs->fetch();
$scr = $this->makescr($row['userid'],$row['pwd']);
if($hash == $scr)
{
$this->srpwd=$row['pwd'];
return true;
}
else {
return false;
}
}//cookie安全
}else {
return false;
}//exit
}//function checkcookie
/**
* putcookie
*
* 登陆成功后放置cookie包含安全码
*
* @param String $uname
* @param String $pwd
* @param Int $time
*/
public function putcookie($uname,$pwd,$time = 604800)
{
try {
$scrString = $this->makescr($uname,$pwd);//加密验证串:防止用户密码被盗防止伪造cookie。
if(!is_numeric($time))
{
$time = 604800;
}
setcookie('user',$uname,time()+$time,'/');
setcookie('scr',$scrString,time()+$time,'/');
return true;
} catch (Exception $e) {
return false;
}
}//function putcookie
/**
* 生成安全码
*
* @param String $u
* @param String $p
*/
public function makescr($u,$p)
{
return substr(md5($u.$p.$this->ck),3,20);
}
/**
* 清除cookie
*/
static function flushcookie()
{
setcookie('user','',time()-99999,'/');
setcookie('scr','',time()-99999,'/');
}
public function getUser()
{
$sql = "SELECT * FROM ".$this->memberTable." m ORDER BY m.id DESC";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace Users\Operation;
use \Mail\Mail;
use \Helpers\View as view;
class EditOperate implements \Users\Event\EditEvent
{
private $db; //传入PDO对象
public $tbl_member = "users";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $FieldGravatarEmail = "gravatar_email";
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式防止出现sdtClass错误
private $config; //全局配置
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
}
public function checkParam(\Zend_EventManager_Event $e){
$data = $e->getParam('data');
$type = $e->getParam('type');
if($type == 'general')
{
if(empty($data['realname']))
{
return "请输入真实姓名";
}
if(mb_strlen($data['realname'],"UTF-8")>10 )
{
return "姓名不要超过10个字";
}
}
if($type == "password")
{
if(strlen($data['password'])>18 || strlen($data['password_new'])>18)
{
return "密码过长";
}
if(strlen($data['password_new'])<=6 || strlen($data['password_confirm'])<=6)
{
return "密码过短";
}
if(md5($data['password_new']) != md5($data['password_confirm']))
{
return "两次输入的密码不同";
}
$uid = view::User('id');
$sql = "SELECT {$this->FieldPasword} FROM {$this->tbl_member} WHERE id=$uid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(md5($data['password']) != $row[$this->FieldPasword])
{
return "原密码不正确";
}
}
return true;
}//checkParam
public function editSuccess(\Zend_EventManager_Event $e){
$data = $e->getParam('data');
return true;
}
}

View File

@ -0,0 +1,115 @@
<?php
namespace Users\Operation;
use \Helpers\dbh as dbh;
use \Users\Gravatar;
class LoginOperate implements \Users\Event\LoginEvent
{
private $db; //传入PDO对象
public $tbl_member = "users";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $FieldGravatarEmail = "gravatar_email";
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式防止出现sdtClass错误
private $config; //全局配置
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
}
public function checkParam(\Zend_EventManager_Event $e){
$data = $e->getParam('data');
if(!is_array($data))
{
return "参数错误";
}
if(empty($data['username']))
{
return array('error'=>"请输入用户名",'place'=>'username');
}
if(!empty($data['username']))
{
if(!preg_match("/^[a-zA-Z][a-zA-Z0-9_]{2,25}$/",$data['username']))
{
return array('error'=>"用户名应当以字母开头由字母数字和下划线组成并且长度在3到25个字符之间",'place'=>'username');
}
}
if(empty($data['password']))
{
return array('error'=>"请输入密码",'place'=>'password');
}
$sql = "SELECT id,{$this->FieldPasword} FROM {$this->tbl_member} WHERE {$this->FieldUsername}=?";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($data[$this->FieldUsername]));
$row = $sth->fetch();
if(isset($row['id']) && !empty($row['id']))
{
if(strlen($row[$this->FieldPasword]) !== 32)
{
return array('error'=>"您的密码或因安全原因或其他问题已经被重置,请先<a href='/account/forgotpassword'>重置密码</a>再登陆",'place'=>'password');
}
if($row[$this->FieldPasword] !== md5($data['password']))
{
return array('error'=>"密码错误",'place'=>'password');
}
return true;
}else{
return array('error'=>"用户不存在",'place'=>'username');
}
}//checkParam
public function updateStatus(\Zend_EventManager_Event $e){
$id = (int)$e->getParam('id');
if(!is_numeric($id))
{
return false;
}
$update = array(
$this->FieldLastlogin => date("Y-m-d H:i:s"),
//$this->FieldLastloginIp => $_SERVER["REMOTE_ADDR"]
);
$dbh = new dbh();
@$statusUpdate = $dbh->update($this->tbl_member,$update," id=$id ");
return true;
}//loginSuccess
public function createAvatar(\Zend_EventManager_Event $e){
$email = $e->getParam('email');
$avatar = new Gravatar();
return $avatar->Get($email);
}//loginSuccess
//检查token表记录
public function checkOAuthToken()
{
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace Users\Operation;
use Mail\Mail;
use Helpers\View as view;
class PwdOperate implements \Users\Event\PwdEvent
{
private $db; //传入PDO对象
public $tbl_member = "users";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $FieldGravatarEmail = "gravatar_email";
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式防止出现sdtClass错误
private $config; //全局配置
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
}
public function forgotPwdCheckParam(\Zend_EventManager_Event $e){
$email = $e->getParam('email');
if(empty($email))
{
return array('error'=>"请输入电子邮箱,作为找回密码和接受通知的联系方式",'place'=>'email');
}
if (!preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email))
{
return array('error'=>"请输入正确的电子邮件",'place'=>'email');
}
return true;
}//checkParam
public function sendGetPasswordMail(\Zend_EventManager_Event $e){
$email = $e->getParam('email');
return true;
}
public function resetPwdCheckParam(\Zend_EventManager_Event $e)
{
$data = $e->getParam('data');
if(empty($data['username']))
{
return array('error'=>"请输入用户名",'place'=>'username');
}
if(empty($data['password']))
{
return array('error'=>"请输入密码",'place'=>'password');
}
if(strlen($data['password']) < 6)
{
return array('error'=>"密码长度太短为了安全最少输入6位哦",'place'=>'password');
}
if(strlen($data['password']) > 14)
{
return array('error'=>"密码太长亲您记得住吗不要超过14位哦",'place'=>'password');
}
if(empty($data['confirm_password']))
{
return array('error'=>"请再次输入密码已确认输入正确",'place'=>'confirm_password');
}
if(md5($data['password']) != md5($data['confirm_password']))
{
return array('error'=>"两次输入的密码不同,请重新输入",'place'=>'confirm_password');
}
return true;
}
}

View File

@ -0,0 +1,182 @@
<?php
namespace Users\Operation;
use \Mail\Mail;
use \Helpers\View as view;
class RegisterOperate implements \Users\Event\RegisterEvent
{
private $db; //传入PDO对象
public $tbl_member = "users";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $FieldGravatarEmail = "gravatar_email";
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式防止出现sdtClass错误
private $config; //全局配置
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
}
public function checkParam(\Zend_EventManager_Event $e){
$data = $e->getParam('data');
if(!is_array($data))
{
return "参数错误";
}
if(empty($data['username']))
{
return array('error'=>"请输入用户名",'place'=>'username');
}
if(!empty($data['username']))
{
if(!preg_match("/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/",$data['username']))
{
return array('error'=>"用户名应当以字母开头由字母数字和下划线组成并且长度在5到16个字符之间",'place'=>'username');
}
}
if(empty($data['password']))
{
return array('error'=>"请输入密码",'place'=>'password');
}
if(strlen($data['password']) < 6)
{
return array('error'=>"密码长度太短为了安全最少输入6位",'place'=>'password');
}
if(strlen($data['password']) > 14)
{
return array('error'=>"密码太长请不要超过14位",'place'=>'password');
}
if(empty($data['confirm_password']))
{
return array('error'=>"请再次输入密码已确认输入正确",'place'=>'confirm_password');
}
if(md5($data['password']) != md5($data['confirm_password']))
{
return array('error'=>"两次输入的密码不同,请重新输入",'place'=>'confirm_password');
}
if(empty($data['email']))
{
return array('error'=>"请输入电子邮箱,作为找回密码和接受通知的联系方式",'place'=>'email');
}
if (!preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$data['email']))
{
return array('error'=>"请输入正确的电子邮件推荐使用QQ邮箱和Gmail邮箱",'place'=>'email');
}
if(empty($data['realname']))
{
return array('error'=>"请输入姓名",'place'=>'realname');
}
if(mb_strlen($data['realname'],"UTF-8")>10 )
{
return array('error'=>"真实姓名请不要超过10个字",'place'=>'realname');
}
return true;
}//checkParam
public function checkUser(\Zend_EventManager_Event $e){
$data = $e->getParam('data');
if(!is_array($data))
{
return "用户信息验证失败,请重新尝试";
}
$sql = "SELECT id,{$this->FieldUsername},{$this->FieldEmail} FROM ".$this->tbl_member." WHERE {$this->FieldUsername}='{$data['username']}' OR {$this->FieldEmail}='{$data['email']}'";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
if(count($rows) > 1)
{
return array('error'=>'您的用户名和邮箱已经注册过账号,您是否<a href="/account/forgotpassword">忘记了密码?</a>','place'=>'username');
}
$row = $rows[0];
if(isset($row['id']) && !empty($row['id']))
{
if($row[$this->FieldUsername] == $data['username'])
{
return array('error'=>'您的用户名已经注册过账号,您是否<a href="/account/forgotpassword">忘记了密码?</a>','place'=>'username');
}
if($row[$this->FieldEmail] == $data['email'])
{
return array('error'=>'您的邮箱已经注册过账号,请换一个邮箱','place'=>'email');
}
return array('error'=>'您的用户名或邮箱已经使用过,注册新账号请换一个用户名');
}
return true;
}//checkUser
public function registerSuccess(\Zend_EventManager_Event $e){
$data = $e->getParam('data');
if(!is_array($data))
{
return false;
}
$id = $e->getParam('id');
if(!is_numeric($id))
{
return false;
}
$mail_template = "register";
$mail_data = array(
'name'=>$data['realname'],
'content'=>$this->getMailContent()
);
$mail = new Mail();
$mail->loadTemplate($mail_template,$mail_data);
$mail->addTo($data['email'],$data['realname']);
$mail->send();
return true;
}//registerSuccess
//邮件内容
public function getMailContent()
{
$content = "欢迎注册";
return $content;
}//getMailContent();
}

View File

@ -0,0 +1,86 @@
<?php
namespace Users;
use \Helpers\View as view;
use \Helpers\dbh as dbh;
use \Users\Account;
use \Helpers\Table;
/*
\Users\Account 再次进行抽象,满足后台调用的需求,屏蔽一些错误等等
!!!!important!!!!大部分操作直接来自 \User\Account慎重修改
*/
class Users extends \Zend_Controller_Plugin_Abstract
{
private $db;
protected $events = NULL; //事件
public $table;
public $account;
function __construct($accountClass = FALSE,$db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->table = new Table();
$this->config = \Zend_Registry::get('config');
if($accountClass === TRUE)
{
$this->account = new Account();
}
}
//通过email地址返回用户信息是否存在
public function userExists($email = NULL)
{
if(empty($email))
{
return false;
}
if(empty($this->account))
{
$account = new Account(FALSE);
}else{
$account = $this->account;
}
$sql = "SELECT * FROM {$account->memberTable} WHERE {$account->FieldEmail}=? LIMIT 1";
$sth = $this->db->prepare($sql);
$sth->execute(array($email));
$row = $sth->fetch();
unset($account);
if(isset($row['id']) && !empty($row['id']))
{
return $row;
}else{
return false;
}
}
public function storeNewAuthCredential($user)
{
if(get_class($user) != 'stdClass')
{
return false;
}
$auth = \Zend_Auth::getInstance();
if($auth->getStorage()->write($user))
return true;
else
return false;
}
}