ECSHOP 会员整合功能分析
ECSHOP后台会员整合,只允许整合一个应用。
如果想整合多个应用,就需要先和UCenter整合,之后再通过UCenter去整合更多的应用。
整合之后工作流程:
1.首先在ini.php文件中使用 ini_user()函数来判断整合插件名称,此函数定义在lib_common.php文件中。
并生成相应的插件对象,方便以后调用。
$user = new $ini_user();
/**
* 初始化会员数据整合类
*
* @access public
* @return object
*/
function &init_users()
{
$set_modules = false;
static $cls = null;
if ($cls != null)
{
return $cls;
}
include_once(ROOT_PATH . ‘includes/modules/integrates/’ . $GLOBALS['_CFG']['integrate_code'] . ‘.php’);
$cfg = unserialize($GLOBALS['_CFG']['integrate_config']);
$cls = new $GLOBALS['_CFG']['integrate_code']($cfg);
return $cls;
}
ecshop整合过程将需要整合的应用数据库看做主库。
比如ec和pw整合,就以PW为主库,将ec的会员信息全部导入到pw会员表中。
之后在ecshop登陆的时候,会通过user.php中的$user->login($username, $password)函数,
和integrates/integrate.php中的sync()函数判断该用户是否在ecshop中存在,
如果不存在,则从PW中拷贝一条记录过来。
具体分析从ecshop登陆的过程:
1:检查username是否在pw_members表存在,如果存在,通过if($this->need_sync)判断是否需要同步登陆,
如果需要同步登陆,则调用$this->sync()函数。具体参考login函数.
function login($username, $password)
{
if ($this->check_user($username, $password) > 0)
{
if ($this->need_sync)
{
$this->sync($username,$password);
}
$this->set_session($username); //同步登陆成功后,设置session
$this->set_cookie($username); //同步登陆成功后,设置cookie,保存登陆状态。
return true;
}
else
{
return false;
}
}
2:同步登陆 sync 函数分析:
首先通过 get_profile_by_name 函数从pw_members表中来获得用户基本信息。
SQL语句如下:
SELECT uid AS user_id,username AS user_name,email AS email,gender AS sex,
bday AS birthday,regdate AS reg_time, password AS password
FROM `phpwind_53`.`pw_members` WHERE username=’测试’;
然后从ecs_users表中根据 username 来获取用户信息,
如果信息为空,则表明ecshop中不存在该用户,就将该用户的信息插入到ecs_users表中。
如果信息不为空,则判断ecs_users表中和pw_members表中的用户信息是否一致,
如果不一致,则以pw_members表中的数据为准,进而需要update下ecs_users表中的信息数据,使其与pw中数据一致。
/**
* 会员同步
*
* @access public
* @param
*
* @return void
*/
function sync ($username, $password=”, $md5password=”)
{
if ((!empty($password)) && empty($md5password))
{
$md5password = md5($password);
}
$main_profile = $this->get_profile_by_name($username);
if (empty($main_profile))
{
return false;
}
$sql = “SELECT user_name, email, password, sex, birthday”.
” FROM ” . $GLOBALS['ecs']->table(‘users’).
” WHERE user_name = ‘$username’”;
$profile = $GLOBALS['db']->getRow($sql);
if (empty($profile))
{
/* 向商城表插入一条新记录 */
if (empty($md5password))
{
$sql = “INSERT INTO ” . $GLOBALS['ecs']->table(‘users’).
“(user_name, email, sex, birthday, reg_time)”.
” VALUES(‘$username’, ‘” .$main_profile['email'].”‘,’”.
$main_profile['sex'] . “‘,’” . $main_profile['birthday'] . “‘,’” . $main_profile['reg_time'] . “‘)”;
}
else
{
$sql = “INSERT INTO ” . $GLOBALS['ecs']->table(‘users’).
“(user_name, email, sex, birthday, reg_time, password)”.
” VALUES(‘$username’, ‘” .$main_profile['email'].”‘,’”.
$main_profile['sex'] . “‘,’” . $main_profile['birthday'] . “‘,’” .
$main_profile['reg_time'] . “‘, ‘$md5password’)”;
}
$GLOBALS['db']->query($sql);
return true;
}
else
{
$values = array();
if ($main_profile['email'] != $profile['email'])
{
$values[] = “email=’” . $main_profile['email'] . “‘”;
}
if ($main_profile['sex'] != $profile['sex'])
{
$values[] = “sex=’” . $main_profile['sex'] . “‘”;
}
if ($main_profile['birthday'] != $profile['birthday'])
{
$values[] = “birthday=’” . $main_profile['birthday'] . “‘”;
}
if ((!empty($md5password)) && ($md5password != $profile['password']))
{
$values[] = “password=’” . $md5password . “‘”;
}
if (empty($values))
{
return true;
}
else
{
$sql = “UPDATE ” . $GLOBALS['ecs']->table(‘users’).
” SET ” . implode(“, “, $values).
” WHERE user_name=’$username’”;
$GLOBALS['db']->query($sql);
return true;
}
}
}
3.设置cookies
ECSHOP所需的cookies是通过integrate/integrate.php中的 set_cookies来完成设置
PHPwind所需的是通过整合接口文件phpwind.php中的set_cookies来完成设置
