目的:使用uc_authcode 獲取論壇當前登錄用戶信息
曲折:看了Discuz官方的ucenter二次開發手冊,其中的Example如下:
if(!empty($_COOKIE['Example_auth'])) {
list($Example_uid, $Example_username) = explode("\t", uc_authcode($_COOKIE['Example_auth'], 'DECODE'));
}
嘗試使用uc_authcode 去解密論壇的auth時,解密結果一直爲空,搜索了下資料,提示UC_KEY 與加密時的密鑰不一致
於是將Discuz在Ucenter中的key 附到了參數後, uc_authcode($_COOKIE['Example_auth'], 'DECODE','62cf0b3c3e6a4c9468e7216839721d8e')
思索了一下,決定去看下Discuz的登錄邏輯,看到synlogin() 方法時,我全局搜索了一下,找到了/api/uc.php 文件,其中的
dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
正是authcookie的內容,原來在discuz內部,並沒有使用uc_authcode,而是使用了authcode,方法位於source/function/function_core.php
中,其加密key爲 getglobal('authkey'),而function getglobal $_G['authkey'] 的值 在 source/class/class_core.php中被定義爲
$this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey']);
關鍵的幾個信息就都找到了,解密的邏輯步驟如下:
authinfo.php (存放至根目錄下)
------------------------------------
<?php
/*
*author:織夢的魚
*time:2011.10.16
*title:discuz x2 當前用戶登錄信息解密
*/
echo "<pre>";
if (!defined('APPROOT')) {
define('APPROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
}
if(!require_once(APPROOT . 'config' . DIRECTORY_SEPARATOR . 'config_global.php')){
exit("該文件應存放於根目錄下!");
}
include APPROOT . 'config' . DIRECTORY_SEPARATOR . 'config_ucenter.php';
include APPROOT . 'uc_client' . DIRECTORY_SEPARATOR . 'client.php';
if(substr($_config['cookie']['cookiepath'], 0, 1) != '/') {
$_config['cookie']['cookiepath']= '/' . $_config['cookie']['cookiepath'];
}
$cookiepre = $_config['cookie']['cookiepre'] . substr(md5($_config['cookie']['cookiepath'] . '|' . $_config['cookie']['cookiedomain']), 0, 4) . '_';//COOKIE前綴
$auth = $cookiepre.'auth';//存儲用戶信息的COOKIE名
$saltkey = $_COOKIE[ $cookiepre . 'saltkey'];//解密auth用到的key
//$discuz_auth_key = md5($_config['security']['authkey'] . $_SERVER['HTTP_USER_AGENT']);//x1.5的密鑰
$discuz_auth_key = md5($_config['security']['authkey'] . $saltkey);//x2的密鑰
$auth_value = uc_authcode($_COOKIE[$auth],'DECODE',$discuz_auth_key);
//var_dump($_COOKIE);
//echo '<hr>';
echo "auth_cookie_value: " . $_COOKIE[$auth] . '<br>';
list($pwd,$uid ) = explode("\t", $auth_value);
echo "當前用戶登錄信息<br>";
echo 'UID:' . $uid." | 密碼(md5):" . $pwd;
?>