设为首页 收藏本站
查看: 469|回复: 0

[经验分享] php ucenter client.php 解析

[复制链接]

尚未签到

发表于 2017-3-3 11:27:47 | 显示全部楼层 |阅读模式
<?php
/*
[UCenter] (C)2001-2009 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: client.php 1045 2011-02-21 05:54:50Z cnteacher $
*/
if(!defined('UC_API')) {
exit('Access denied');
}
error_reporting(0);
/*IN_UC UC标记,防止非法入口*/
define('IN_UC', TRUE);
/*当前 UCENTER 版本号*/
define('UC_CLIENT_VERSION', '1.6.0');
/*UCENTER 允可证*/
define('UC_CLIENT_RELEASE', '20110501');
/*UCENTER 根目录*/
define('UC_ROOT', substr(__FILE__, 0, -10));
/*UCETNER 数据 目录*/
define('UC_DATADIR', UC_ROOT.'./data/');
/*UCENTER中心 数据目录*/
define('UC_DATAURL', UC_API.'/data');
/*UCENTER 连结方式(mysql 数据库直连方式,post:交给UNCENTER中心处理方式*/
define('UC_API_FUNC', UC_CONNECT == 'mysql' ? 'uc_api_mysql' : 'uc_api_post');
/*UC controls:全局控件数组*/
$GLOBALS['uc_controls'] = array();
/*
*  这个函数将转义变量 $string(可以是字符串,也可以是数组),最后返回转义的结果
*
*  @param mixed  $string 将到转义的变量
*  @param int   $force  是否强制转义 (0:否,      1:是)
*  @param boolen  $strip  为$force说明(0:false,1:true)
*  @return mixed   $string 被转义的变量
*  @todo 将变量转义
*/
function uc_addslashes($string, $force = 0, $strip = FALSE) {
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(!MAGIC_QUOTES_GPC || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = uc_addslashes($val, $force, $strip);
}
} else {
$string = addslashes($strip ? stripslashes($string) : $string);
}
}
return $string;
}
/*
* 如果没定义daddslashes,则定义函数daddslashes
* @see uc_addslashes($string,$force)
*
*/
if(!function_exists('daddslashes')) {
function daddslashes($string, $force = 0) {
return uc_addslashes($string, $force);
}
}
/*
*  这个函数完成的功能去掉转义,并返回去掉转义的字符
*  @param string $string 将要转义的字符
*  @return string $string 返回已转义的字符
*/
function uc_stripslashes($string) {
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(MAGIC_QUOTES_GPC) {
return stripslashes($string);
} else {
return $string;
}
}
/*(POST 方式只不过将数据交给ucenter中心去处理,与MYSQL(直接连接数据库)处理方式差不多)
* 这个函数是post处理方式入口函数,调用 相应module中相应的方法,返回相应的结果
*
* @param string $module 模块名
* @param stirng $action 模块方法名
* @param array  $arg  相应的参数
*
* @example uc_api_post('friend', 'add', array('uid'=>$uid, 'friendid'=>$friendid, 'comment'=>$comment))
*   此函数将 调用 friendcontrol 类(control目录下)中onadd方法,参数为array('uid'=>$uid, 'friendid'=>$friendid, 'comment'=>$comment))
*   将返回 添加 friend 的ID
* @return mixed data 与调用相应模块的方法相关
* */
function uc_api_post($module, $action, $arg = array()) {
$s = $sep = '';
foreach($arg as $k => $v) {
$k = urlencode($k);
if(is_array($v)) {
$s2 = $sep2 = '';
foreach($v as $k2 => $v2) {
$k2 = urlencode($k2);
$s2 .= "$sep2{$k}[$k2]=".urlencode(uc_stripslashes($v2));
$sep2 = '&';
}
$s .= $sep.$s2;
} else {
$s .= "$sep$k=".urlencode(uc_stripslashes($v));
}
$sep = '&';
}
$postdata = uc_api_requestdata($module, $action, $s);
return uc_fopen2(UC_API.'/index.php', 500000, $postdata, '', TRUE, UC_IP, 20);
}
/*
* 这个函数构造URL参数串,返回构造的参数串
*
* @param string $module 模块名
* @param stirng $action 模块方法名
* @param string $arg  相应的参数
* @param string $extra  UC附加标记
* @return stirng $post  返加构造的url参数串
*
*/
function uc_api_requestdata($module, $action, $arg='', $extra='') {
$input = uc_api_input($arg);
$post = "m=$module&a=$action&inajax=2&release=".UC_CLIENT_RELEASE."&input=$input&appid=".UC_APPID.$extra;
return $post;
}
/*
* 这个函数构造带参数的url地址,可以通过GET发送请求
*
* @param string $module 模块名
* @param stirng $action 模块方法名
* @param string $arg  相应的参数
* @param string $extra  UC附加标记
* @return stirng $url   返加带参数的url地址
*
*/
function uc_api_url($module, $action, $arg='', $extra='') {
$url = UC_API.'/index.php?'.uc_api_requestdata($module, $action, $arg, $extra);
return $url;
}
/*
* 这个函数 将返回具有UCENTER加密方式法数据,将核心数据加密
*将$data数据+agent参数+time参数,通过 uc_authcode函数通过UC_KEY进行ENCODE,最后通过 url编码得到结果
*
* @param string $data 将要加密的数据
* @return string $s ucenter 加密过的数据
*/
function uc_api_input($data) {
$s = urlencode(uc_authcode($data.'&agent='.md5($_SERVER['HTTP_USER_AGENT'])."&time=".time(), 'ENCODE', UC_KEY));
return $s;
}
/*
* 这个函数是mysql直接数据库处理函数
* @see function uc_api_post
*/
function uc_api_mysql($model, $action, $args=array()) {
global $uc_controls;
if(empty($uc_controls[$model])) {
include_once UC_ROOT.'./lib/db.class.php';  //加载数据库连接库
include_once UC_ROOT.'./model/base.php';   //加载model base 类库是model基类
include_once UC_ROOT."./control/$model.php";  //加载相应的控件类
eval("\$uc_controls['$model'] = new {$model}control();"); //将控件类添加到全局控件数组中
}
if($action{0} != '_') {       //如果action 的第一个字符不等于下划线
$args = uc_addslashes($args, 1, TRUE);  //强制将参数转义
$action = 'on'.$action;      //方法前缀加on
$uc_controls[$model]->input = $args;  //将参数放到对象的input属性存储起来
return $uc_controls[$model]->$action($args);//执行对象方法,返回结果
} else {
return '';         //如果以下划线开头的方法,不处理
}
}
/*
* 这个函数将数组,进行xml 序列化,其实就是将数组转化为XML
*
* @param array $arr 需要序列化的数组
* @param boolen $htmlon 未知
*/
function uc_serialize($arr, $htmlon = 0) {
include_once UC_ROOT.'./lib/xml.class.php';
return xml_serialize($arr, $htmlon);
}
/*
*这个函数XML转化为数组,也就完成反序列化操作
*
*/
function uc_unserialize($s) {
include_once UC_ROOT.'./lib/xml.class.php';
return xml_unserialize($s);
}
/*
* 本函数 ucenter 独家加密 方法
*
*  @param string $string 将要操作的串
*  @param enum(DECODE,ENCODE) $operation 什么操作
*  @param string $key 密钥
*  @param int $expiry 过期期限
*
*  @return string data 返回加密或解密的串
*
*/
function uc_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
/*
* 本函数将请求的URL附加参数__time__=1
* (作用:个人认为记录本次接口请求次数?)
* @see function uc_fopen()
*
*/
function uc_fopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
$__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1;
if($__times__ > 2) {
return '';
}
$url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__";
return uc_fopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block);
}
/*
* 这个函数完成向 UCENTER 中心 完成 socket 套接字请求,返回请求结果
*
* @param string $url  请求的Url 地址
* @param int    $limit 读流限制长度,最长为8192字节,多得被停止@see fread()
* @param boolean $post 是否采用post方法
* @param string $cookie 请求中带的cookie值
* @param boolean $bysocket 未知
* @param string $ip    套接字请求地址
* @param int  $timeout 单位秒,请求超时时间
* @param boolean $block  是否在流上设置块模式
*/
function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
$return = '';
$matches = parse_url($url);
!isset($matches['host']) && $matches['host'] = '';
!isset($matches['path']) && $matches['path'] = '';
!isset($matches['query']) && $matches['query'] = '';
!isset($matches['port']) && $matches['port'] = '';
$host = $matches['host'];
$path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
$port = !empty($matches['port']) ? $matches['port'] : 80;
if($post) {
$out = "POST $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
//$out .= "Referer: $boardurl\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= 'Content-Length: '.strlen($post)."\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cache-Control: no-cache\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
$out .= $post;
} else {
$out = "GET $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
//$out .= "Referer: $boardurl\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
}
if(function_exists('fsockopen')) {
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
} elseif (function_exists('pfsockopen')) {
$fp = @pfsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
} else {
$fp = false;
}
if(!$fp) {
return '';
} else {
stream_set_blocking($fp, $block);
stream_set_timeout($fp, $timeout);
@fwrite($fp, $out);
$status = stream_get_meta_data($fp);
if(!$status['timed_out']) {
while (!feof($fp)) {
if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
break;
}
}
$stop = false;
while(!feof($fp) && !$stop) {
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
$return .= $data;
if($limit) {
$limit -= strlen($data);
$stop = $limit <= 0;
}
}
}
@fclose($fp);
return $return;
}
}
/*
* 本函数 获取应用列表
* 想知道怎么操作的,请参照  /control/app.php 中的 onls 方法,你会找到答案
* 查找所有应用,会包含 UC_DBTABLEPRE.applications表中 appid, type, name, url, tagtemplates, viewprourl, synlogin 字段
*/
function uc_app_ls() {
$return = call_user_func(UC_API_FUNC, 'app', 'ls', array());
return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}
/*
*
*
*/
function uc_feed_add($icon, $uid, $username, $title_template='', $title_data='', $body_template='', $body_data='', $body_general='', $target_ids='', $images = array()) {
return call_user_func(UC_API_FUNC, 'feed', 'add',
array(  'icon'=>$icon,
'appid'=>UC_APPID,
'uid'=>$uid,
'username'=>$username,
'title_template'=>$title_template,
'title_data'=>$title_data,
'body_template'=>$body_template,
'body_data'=>$body_data,
'body_general'=>$body_general,
'target_ids'=>$target_ids,
'image_1'=>$images[0]['url'],
'image_1_link'=>$images[0]['link'],
'image_2'=>$images[1]['url'],
'image_2_link'=>$images[1]['link'],
'image_3'=>$images[2]['url'],
'image_3_link'=>$images[2]['link'],
'image_4'=>$images[3]['url'],
'image_4_link'=>$images[3]['link']
)
);
}
function uc_feed_get($limit = 100, $delete = TRUE) {
$return = call_user_func(UC_API_FUNC, 'feed', 'get', array('limit'=>$limit, 'delete'=>$delete));
return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}
/*
* 本函数完成 一次添加好友
*
* @param int $uid    用户ID
* @param int $friendid  朋友ID
* @param string $comment 添加验证留言
*
* @return int ID   friends表中最近插入的一条ID主键值
*/
function uc_friend_add($uid, $friendid, $comment='') {
return call_user_func(UC_API_FUNC, 'friend', 'add', array('uid'=>$uid, 'friendid'=>$friendid, 'comment'=>$comment));
}
/*
*本函数完成删除多个好友
*
* @param int $uid    用户ID
* @param int $friendids  朋友ID数组
*
* @return int ROW    返回删除好友的个数
*/
function uc_friend_delete($uid, $friendids) {
return call_user_func(UC_API_FUNC, 'friend', 'delete', array('uid'=>$uid, 'friendids'=>$friendids));
}
/*
*计算用户的好朋友个数
*
*@param int $uid   用户ID
*@param int $direction 0:所有好友个数,1:单向好友个数,2:你是对方好友 的个数,3:双向好友的个数
*
*@return int COUNT  返回好友个数
*
*/
function uc_friend_totalnum($uid, $direction = 0) {
return call_user_func(UC_API_FUNC, 'friend', 'totalnum', array('uid'=>$uid, 'direction'=>$direction));
}
/*
* 获取好友列表
*
* @param int $uid   用户ID
* @param int $page  页数
* @param int $pagesize 每页个数
* @param int $totalnum 好友总数
* @param int $direction 见上 一函数
*
* @return array $data 返回好友数组,字段为 uid,username,friendid,direction,version,delstatus,comment
*
*/
function uc_friend_ls($uid, $page = 1, $pagesize = 10, $totalnum = 10, $direction = 0) {
$return = call_user_func(UC_API_FUNC, 'friend', 'ls', array('uid'=>$uid, 'page'=>$page, 'pagesize'=>$pagesize, 'totalnum'=>$totalnum, 'direction'=>$direction));
return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}
/*
* 注册用户
*
*  @param string $username  用户名
*  @param string $password  用户密码
*  @param string $email  用户邮箱
*  @param string $questionid 用户问题
*  @param string $answer   用户答案
*  @param string $regip 注册时ip
*
*  @return int flag:
*   大于 0:返回用户 ID,表示用户注册成功
-1:用户名不合法
-2:包含不允许注册的词语
-3:用户名已经存在
-4:Email 格式有误
-5:Email 不允许注册
-6:该 Email 已经被注册
*/
function uc_user_register($username, $password, $email, $questionid = '', $answer = '', $regip = '') {
return call_user_func(UC_API_FUNC, 'user', 'register', array('username'=>$username, 'password'=>$password, 'email'=>$email, 'questionid'=>$questionid, 'answer'=>$answer, 'regip' => $regip));
}
/*
*注册登录
*
*  @param string $username 用户名
*  @param string $password 用户密码
*  @param boolen $isuid      是否为uid登录方式,1:uid登录方式,2:email登录方式,其他:用户名登录 方式
*  @param boolen $checkques 是否检查验证问题和答案
*  @param string $questionid 用户问题
*  @param string $answer   用户答案
*
*  @return array $data
*   integer [0]  大于 0:返回用户 ID,表示用户登录成功
1:用户不存在,或者被删除
-2:密码错
-3:安全提问错
string [1]  用户名
string [2]  密码
string [3]  Email
bool [4]  用户名是否重名
*/
function uc_user_login($username, $password, $isuid = 0, $checkques = 0, $questionid = '', $answer = '') {
$isuid = intval($isuid);
$return = call_user_func(UC_API_FUNC, 'user', 'login', array('username'=>$username, 'password'=>$password, 'isuid'=>$isuid, 'checkques'=>$checkques, 'questionid'=>$questionid, 'answer'=>$answer));
return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}
/*
* 本函数完成 一个异步登录
*
* @param int $uid   用户ID
*
* @return string data like
* <script type="text/javascript"
* src="'.$app['url'].'/api/uc.php?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogin&username='.$this->user['username'].'&uid='.$this->user['uid'].'&password='.$this->user['password']."&time=".$this->time, 'ENCODE', $app['authkey'])).'">
* </script>
* <script...></script>..
* 在其他域下产生相同的Session,来完成异步登录
*/
function uc_user_synlogin($uid) {
$uid = intval($uid);
if(@include UC_ROOT.'./data/cache/apps.php') {
if(count($_CACHE['apps']) > 1) {
$return = uc_api_post('user', 'synlogin', array('uid'=>$uid));
} else {
$return = '';
}
}
return $return;
}
/**
* 本函数用户异步退出
*
* @see function uc_user_synlogin
*
*/
function uc_user_synlogout() {
if(@include UC_ROOT.'./data/cache/apps.php') {
if(count($_CACHE['apps']) > 1) {
$return = uc_api_post('user', 'synlogout', array());
} else {
$return = '';
}
}
return $return;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-349694-1-1.html 上篇帖子: PHP缓存技术_PHP技术 下篇帖子: php_分页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表