前提条件:
1 uc_client包已经放置好。
2 api目录以及 uc.php程序已经设置好。
背景资料:
在uCenter中,当安装一个新的应用的时候,如果选择默认安装方式,会自动执行一个接口程序,默认情况下是,http://your_application_host/install.php,
当然这个程序并不是固定的。执行该程序的主要目的有两个,一个是安装应用,另外一个是uCenter获取应用安装所必须的信息,并进行通信测试。
1. 当在UCenter中按照推荐方式添加新应用的时候,UCenter默认向指定的应用程序提交以下数据:
//var_dump($_POST);
array
'appurl' => string 'http://192.168.1.199/totuba/install/uc_install.php' (length=50)
'ucapi' => string 'http://192.168.1.199/ucenter' (length=28)
'ucfounderpw' => string '20cce0d757291c55b3615f63a9eea8c0' (length=32)
'installsubmit' => string ' 安 装 ' (length=9)
2. 通过PHP SOCKET方式获取UCenter相关信息
运行代码片段:
$ucinfo = sfopen($ucapi.'/index.php?m=app&a=ucinfo', 500, '', '', 1, $ucip);
执行结果:
[var_dump:] string 'UC_STATUS_OK|1.0.0|20080429|utf-8|utf8|DISCUZ UCHOME OTHER' (length=58)
[1] | [2] | [3] | [4] | [5]| [6]
[1] 当前UC状态
[2] 当前UC版本
[3] UC发行日期
[4] UC页面字符集
[5] UC数据库字符集
[6] 应用类型
3. 发送正式应用注册信息后的返回信息。
'e6kdVer70bn3A0p1N9s7D3yaH0d7998fUccdr7J1peh9N4Dd08I8Iar3c0y5j7L4|10|localhost|dev_ucenter|jason|jason|utf8|uc_|utf-8' (length=116)
格式化信息如下:
array
0 => string 'n5x7e72bRfDbl0t5ObM3k6H522A4G182xc78hbqf88A0Ba50ye5d18t222D4x5h5' (length=64)
1 => string '13' (length=2)
2 => string 'localhost' (length=9)
3 => string 'dev_ucenter' (length=11)
4 => string 'jason' (length=5)
5 => string 'jason' (length=5)
6 => string 'utf8' (length=4)
7 => string 'uc_' (length=3)
8 => string 'utf-8' (length=5)
4. 什么时候进行通信验证?
当你在用户中心点击应用管理或新添加一个应用的时候,UCenter会自动发起通信测试。
5. 由DISCUZ用户中心发起的到应用端的请求数据要先进行解密,然后正常使用,DISCUZ中各类产品统一使用authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)来实现。
/**
* DISCUZ 加密解密通用函数
*
* param $string [string]: the content what you want to DECODE or ENCODE
* param $operation [string]: enum('DECODE','ENCODE');
* param $key [string]: Generally is "base 64 code" which return from UCenter when you install application to UCenter
*
* * * * */
function 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));
}
}
用户在UCenter登录后返回的信息
array (
0 => '2',
1 => 'blackhailang',
2 => '000000',
3 => 'blackhailang@gmail.com',
4 => '0',
)