资源描述
ucenter是康盛的一款用户中心功能,可以很方便的实现数个网站的用户注册登录短信息。
下面就如何扩展ucenter功能进行一个讲解。我们的任务是要给ucenter的用户增加扩展资料。
长期以来ucenter中各个应用下的积分是各自为政,很不统一。比如说积分在各个程序中都是独立的。我们要实现的功能是统一用户的积分,在某一个应用中修改了积分在别的应用中同步修改积分和地址,你可以扩展到更多的字段。
第一步在运行ucenter的数据库创建扩展表,这里建立的是uc_object,其中uc是安装ucenter时选择的数据表前缀。
CREATE TABLE IF NOT EXISTS `uc_object` (
`uid` mediumint(8) NOT NULL COMMENT '用户ID',
`username` char(15) NOT NULL COMMENT '用户名',
`credit` int(11) NOT NULL default '0' COMMENT '积分',
`addr` varchar(255) NOT NULL default '' COMMENT '地址',
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
上面部分内容如果无法看到你可以去二次开发增加用户的扩展资料并实现同步功能/进行查看。
第二步扩展uc_client
这个地方比较重要是整个工作的基础,如果使用mysql链接的话那么只要修改uc_client.php就可以了。
在client.php下增加函数
/**
*
*/
function uc_get_object($username,$isuid=0,$type=null){
$data=uc_get_user($username,$isuid);
if($data<=0){
return $data;
}
$return= call_user_func(UC_API_FUNC, 'object', 'get', array('isuid'=>intval($isuid), 'username'=>$username,'type'=>$type));
return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}
/**
*
*/
function uc_object_set($username,$isuid=0,$filed=null,$value=null){
$data=uc_get_user($username,$isuid);
if($data<=0){
return $data;
}
if($filed==null){
return false;
}
$return=call_user_func('uc_api_post', 'object', 'set', array('isuid'=>intval($isuid), 'username'=>$username,'filed'=>$filed,'value'=>$value));
return $return ;
}
/**
* 返回-11分数不足
*/
function uc_object_addint($username,$isuid=0,$filed=null,$value=null){
$data=uc_get_user($username,$isuid);
if($data<=0){
return $data;
}
if(!in_array($filed,array('credit'))){
$return=false;
}else{
$return= call_user_func('uc_api_post', 'object', 'add', array('isuid'=>intval($isuid), 'username'=>$username,'filed'=>$filed,'value'=>$value));
}
return $return;
}
这里面的三个函数主要功能是
uc_get_object是返回用户的扩展资料。第三个参数如果指定为扩展资料的某一项时则返回数组(用户ID,用户名,指定扩展项)否则返回数组(用户ID,用户名, 数据库中的所有扩展项)。
uc_object_set(),uc_object_addint(),这两个函数是设置,增减用户扩展资料功能。其中uc_object_addint是专门针对int型扩展字段。如果这些扩展资料在应用中的数据库里也有那么这些字段在设置和更新时要进行同步,而只要将返回的字符输出到页面就可以了。如
echo uc_object_set(1,1,'credit',1);
echo uc_object_set(1,1,'addr','北京市');
上面将同步各个应用中的积分,当然还是要修改每一个应用下面的api/uc.php。改法附后。
设置多个字段目前无法同步 因此echo uc_object_set(1,1,array('credit'=>1,'addr'=>'北京市'));是没有效果的。但是ucenter中会更新资料,应用程序可以找一个合适的时机自行同步。
uc_object_addint()中的$filed不能为数组,$value为增加的值,如果为负就会减少减少用户的指定扩展int型数据。
uc_object_addint(1,1,'credit',1);增加用户1分。
uc_object_addint(1,1,'credit',-5);扣除5分。
同样增加如果应用中有相应的字段那么也要进行同步。同步方法是echo ,同uc_object_set。
uc_object_addint中的if(!in_array($filed,array('credit'))){里面可以增加更多int类型的字段以便扩展你的功能,请根据需要进行增加。
对于同步用户扩展的功能和从uc中取用户资料是差别同步是要同应用的api/uc.php进行通讯,而且uc_client是不知道其它应用的authkey但是ucenter知道,所以同步只能使用ucenter是知道所有的应用authkey。看下面是两种区别。
要求同步的必须使用远程ucenter中的方法
$return= call_user_func('uc_api_post', 'object', 'add', array('isuid'=>intval($isuid), 'username'=>$username,'filed'=>$filed,'value'=>$value));
return $return;
只取用户资料
一般语句如下
$return= call_user_func(UC_API_FUNC, 'object', 'get', array('isuid'=>intval($isuid), 'username'=>$username,'type'=>$type));
return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
注意call_user_func中的第一个参数。
第三步在control目录下创建object.php
内容如下
<?php
/*
顾留中
*/
!defined('IN_UC') && exit('Access Denied');
class objectcontrol extends base{
function __construct() {
$this->objectcontrol();
}
function objectcontrol() {
parent::__construct();
$this->load('object');
//note client 仅在需要时初始化 $this->app
$this->app = $this->cache['apps'][UC_APPID];
}
//note public 外部接口
function onget() {
$this->init_input();
$isuid=$this->input('isuid');
$username=$this->input('username');
$type=$this->input('type');
return $_ENV['object']->get_object($username,$isuid,$type);
}
}
这个里面只包含一个方法就是取用户扩展资料。
第四步在model目录下创建object.php
内容如下
<?php
/*
顾留中
*/
!defined('IN_UC') && exit('Access Denied');
class objectmodel{
var $db;
var $base;
function __construct(&$base) {
$this->objectmodel($base);
}
function objectmodel(&$base) {
$this->base = $base;
$this->db = $base->db;
}
function get_object($username,$isuid,$type=null) {
if($type==null){
$type='*';
}else{
$type='uid,username,'.$type;
}
if(!$isuid){
$where='username=\''.$username.'\'';
}else{
$where='uid=\''.$username.'\'';
}
$arr = $this->db->fetch_first("SELECT ".$type." FROM ".UC_DBTABLEPRE."object WHERE ".$where);
return $arr;
}
}
这个里面只包含一个方法就是取用户扩展资料。
经过以上的修改uc_client部分就算完成了。已经成功了一半了。
本作品版权属于顾留中
网站
本作品版权属于顾留中
网站
下面开始修改ucenter.
第五步在ucenter的根目录的index.php中找到
if(in_array($m, array('app', 'frame', 'user', 'pm', 'pm_client', 'tag', 'feed', 'friend', 'domain', 'credit', 'mail', 'version')))
改为
if(in_array($m, array('app', 'frame', 'user', 'pm', 'pm_client', 'tag', 'feed', 'friend', 'domain', 'credit', 'mail', 'version','object')))
主要是增加了,'object',这样就可以对用户扩展资料进行ucenter方面的调用了。
第六步在control目录下创建object.php
内容如下
<?php
/*
顾留中
*/
!defined('IN_UC') && exit('Access Denied');
class objectcontrol extends base {
function __construct() {
$this->objectcontrol();
}
function objectcontrol() {
parent::__construct();
$this->load('object');
$this->load('user');
}
//note public 外部接口
function onget() {
$this->init_input();
$isuid=$this->input('isuid');
$username=$this->input('username');
$type=$this->input('type');
return $_ENV['object']->get_object($username,$isuid,$type);
}
function onset() {
$this->init_input();
$isuid=$this->input('isuid');
$username=$this->input('username');
$filed=$this->input('filed');
$value=$this->input('value');
$result= $_ENV['object']->set_object($username,$isuid,$filed,$value);
if($result<0){
return $result;
}else{
$data=$_ENV['object']->get_object($username,$isuid,$filed);
$synstr = '';
foreach($this->cache['apps'] as $appid => $app) {
$synstr .= '<script type="text/javascript" src="'.$app['url'].'/api/uc.php?time='.$this->time.'&code='.urlencode($this->authcode('action=synlobject&username='.$username.'&isuid='.$isuid.'&filed='.$filed.'&value='.$data[$filed].'&time='.$this->time, 'ENCODE', $app['authkey'])).'"></script>';
}
return $synstr;
}
}
function onadd() {
$this->init_input();
$isuid=$this->input('isuid');
$username=$this->input('username');
$filed=$this->input('filed');
$value=$this->input('value');
$result=$_ENV['object']->add($username,$isuid,$filed,$value);
if($result<0){
return $result;
}else{
$data=$_ENV['object']->get_object($username,$isuid,$filed);
$synstr = '';
foreach($this->cache['apps'] as $appid => $app) {
$synstr .= '<script type="text/javascript" src="'.$app['url'].'/api/uc.php?time='.$this->time.'&code='.urlencode($this->authcode('action=synlobject&username='.$username.'&isuid='.$isuid.'&filed='.$filed.'&value='.$data[$filed].'&time='.$this->time, 'ENCODE', $app['authkey'])).'"></script>';
}
return $synstr;
}
}
}
?>
请看onset,onadd中有同步代码。你也可以作一定的修改比如说使用服务器进行同步。但是这样可能会加重ucenter的负担。同步的本质就是请求上面的script中的src属性中的网址。
开发要点
注意
function objectcontrol() {
parent::__construct();
$this->load('object');
$this->load('user');
}
这里面的
$this->load('user');
是因为这里的需要user模块支持,同样你可以开发发来比如用户得分了短信息通知功能。只要增加$this->load('pm');
这里就不示范了。
调用模块的方法
$_ENV['user']->xxx();看下面的第七步代码就有。
本作品版权属于顾留中
网站
第七步在model目录下创建object.php
内容如下
<?php
/*
顾留中
*/
!defined('IN_UC') && exit('Access Denied');
class objectmodel {
var $db;
var $base;
function __construct(&$base) {
$this->objectmodel($base);
}
function objectmodel(&$base) {
$this->base = $base;
$this->db = $base->db;
}
function get_object($username,$isuid,$type=null) {
if($type==null){
$type='*';
}else{
$type='uid,username,'.$type;
}
if(!$isuid){
$where='username=\''.$username.'\'';
}else{
$where='uid=\''.$username.'\'';
}
$arr = $this->db->fetch_first("SELECT ".$type." FROM ".UC_DBTABLEPRE."object WHERE ".$where);
return $arr;
}
function set_object($username,$isuid,$filed,$value) {
file_put_contents('sql.txt',$sqladd);
if(!$isuid){
$where='username=\''.$username.'\'';
}else{
$where='uid=\''.$username.'\'';
}
$sqladd='';
if(is_array($filed)){
foreach ($filed as $k=>$v){
$sqladd.=',`'.$k.'`=\''.$v.'\'';
}
}elseif($filed && $value){
$sqladd=','.$filed.'=\''.$value.'\'';
}
if($this->get_object($username,$isuid)){
if(is_array($filed)){
foreach ($filed as $k=>$v){
$sqladd.=',`'.$k.'`=\''.$v.'\'';
}
}
$this->db->query("UPDATE ".UC_DBTABLEPRE."object SET ".substr($sqladd,1)." WHERE ".$where);
file_put_contents('update.txt',"UPDATE ".UC_DBTABLEPRE."object SET ".substr($sqladd,1)." WHERE ".$where);
return $this->db->affected_rows();
}else{
if(!$isuid) {
$status = $_ENV['user']->get_user_by_username($username);
} else {
$status = $_ENV['user']->get_user_by_uid($username);
}
if($status){
$uid=$status['uid'];
$username=$status['username'];
}
$this->db->query("INSERT INTO ".UC_DBTABLEPRE."object SET username='$username', uid=$uid".$sqladd);
return $this->db->affected_rows();
}
return $arr;
}
function add($username,$isuid,$filed,$value) {
$data=$this->get_object($username,$isuid,$filed,$value);
if(!$data){
$this->set_object($username,$isuid,null,null);
$data=$this->get_object($username,$isuid,$filed,$value);
}
if($data[$filed]+$value<0){
return -11;
}
$sqladd=$filed.'='.$filed.'+'.$value;
if(!$isuid){
$where='username=\''.$username.'\'';
}else{
$where='uid=\''.$username.'\'';
}
$this->db->query("UPDATE ".UC_DBTABLEPRE."object SET ".$sqladd." WHERE ".$where);
return $this->db->affected_rows();
}
}
?>
到此ucenter修改完成。
下面就讲讲如何进行积分同步。这里只以uchome为例。其它自行修改或找本人付费修改。
打开uchome下的api/uc.php
查找
if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcredit', 'getcreditsettings', 'updatecreditsettings', 'addfeed'))) {
修改为
if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcredit', 'getcreditsettings', 'updatecreditsettings', 'addfeed','synlobject'))) {
上面只是增加了,'synlobject'
在note的类中增加如下函数
function synlobject ($get,$post){
global $_SGLOBAL;
if(intval($get['isuid'])){
$uid=intval($get['username']);
}else{
$username=$get['username'];
}
//print_r($get);
$value=$get['value'];
$filed=$get['filed'];
switch($filed){
case 'credit':
if(intval($get['isuid'])){
//echo "UPDATE ".tname('space')." SET credit='$value' WHERE uid='$uid'";
$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit='$value' WHERE uid='$uid'");
}else{
//echo "UPDATE ".tname('space')." SET credit='$value' WHERE username='$username'";
$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit='$value' WHERE username='$username'");
}
default:
}
//return API_RETURN_SUCCEED;
}
本作品版权属于顾留中
网站
完整内容如下
<?php
/*
[UCenter Home] (C) 2007-2008 Comsenz Inc.
$Id: uc.php 10988 2009-01-19 05:44:31Z zhengqingpeng $
*/
define('UC_CLIENT_VERSION', '1.5.0');
//note UCenter 版本标识
define('UC_CLIENT_RELEASE', '20081212');
define('API_DELETEUSER', 1);
//用户删除 API 接口开关
define('API_RENAMEUSER', 1);
//用户名修改 API 接口开关
define('API_GETTAG', 1);
//获取标签 API 接口开关
define('API_SYNLOGIN', 1);
//同步登录 API 接口开关
define('API_SYNLOGOUT', 1);
//同步登出 API 接口开关
define('API_UPDATEPW', 1);
//更改用户密码 开关
define('API_UPDATEBADWORDS', 1);
//更新关键字列表 开关
define('API_UPDATEHOSTS', 1);
//更新HOST文件 开关
define('API_UPDATEAPPS', 1);
//更新应用列表 开关
define('API_UPDATECLIENT', 1);
//更新客户端缓存 开关
define('API_UPDATECREDIT', 1);
//更新用户积分 开关
define('API_GETCREDIT', 1);
//向 UC 提供积分 开关
define('API_GETCREDITSETTINGS', 1);
//向 UC 提供积分设置 开关
define('API_UPDATECREDITSETTINGS', 1);
//更新应用积分设置 开关
define('API_ADDFEED', 1);
//向 UCHome 添加feed 开关
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');
define('IN_UCHOME', TRUE);
define('S_ROOT', substr(dirname(__FILE__), 0, -3));
$_SGLOBAL = $_SCONFIG = $_SBLOCK = $_TPL = $_SCOOKIE = $space = array();
//获取时间
$_SGLOBAL['timestamp'] = time();
if(defined('IN_UC')) {
global $_SGLOBAL, $_SCONFIG, $_SC, $space, $_SCOOKIE, $_SBLOCK, $_TPL;
include_once S_ROOT.'./config.php';
include_once S_ROOT.'./data/data_config.php';
include_once S_ROOT.'./source/function_common.php';
//链接数据库
dbconnect();
} else {
error_reporting(0);
set_magic_quotes_runtime(0);
defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
include_once S_ROOT.'./config.php';
include_once S_ROOT.'./data/data_config.php';
include_once S_ROOT.'./source/function_common.php';
//链接数据库
dbconnect();
$get = $post = array();
$code = @$_GET['code'];
parse_str(authcode($code, 'DECODE', UC_KEY), $get);
if(MAGIC_QUOTES_GPC) {
$get = sstripslashes($get);
}
if($_SGLOBAL['timestamp'] - $get['time'] > 3600) {
exit('Authracation has expiried');
}
if(empty($get)) {
exit('Invalid Request');
}
include_once S_ROOT.'./uc_client/lib/xml.class.php';
$post = xml_unserialize(file_get_contents('php://input'));
if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcredit', 'getcreditsettings', 'updatecreditsettings', 'addfeed','synlobject'))) {
$uc_note = new uc_note();
echo $uc_note->$get['action']($get, $post);
exit();
} else {
exit(API_RETURN_FAILED);
}
}
class uc_note {
var $dbconfig = '';
var $db = '';
var $tablepre = '';
var $appdir = '';
function _serialize($arr, $htmlon = 0) {
if(!function_exists('xml_serialize')) {
include_once S_ROOT.'./uc_client/lib/xml.class.php';
}
return xml_serialize($arr, $htmlon);
}
function uc_note() {
global $_SGLOBAL, $_SC;
$this->appdir = substr(dirname(__FILE__), 0, -3);
$this->dbconfig = S_ROOT.'./config.php';
$this->db = $_SGLOBAL['db'];
$this->tablepre = $_SC['tablepre'];
}
function test($get, $post) {
return API_RETURN_SUCCEED;
}
function deleteuser($g
展开阅读全文