---------代码(install.php)-------
<?php
/*
* 安装脚本,访问地址为 http://domain/login/install.php
*/
header('ContentType:text/html;UTF-8');
define('THISDIR', str_replace("\\", '/', dirname(__FILE__)));
define('UC_API', 1);//要使用uc client必须声明
include_once(THISDIR .'/uc_client/client.php');
if (empty($_GET)) {
showBar();
} else {
writeConfig();
}
/*
* 根据返回信息,测试/写入配置
*/
function writeConfig() {
$server = $_POST['serverUrl'];//uc server地址
$data = "ucfounderpw={$_POST['ucfounderpw']}"
."&apptype={$_POST['appType']}"
."&appname={$_POST['appName']}"
."&appurl={$_POST['appUrl']}"
."&viewprourl={$_POST['appProUrl']}"
."&appip={$_POST['appIp']}"
."&appcharset={$_POST['appCharset']}"
."&appdbcharset={$_POST['appDbCharset']}"
."&apptagtemplates={$_POST['appTemplate']}"
."&allowips={$_POST['appAllowIps']}";
$re = uc_fopen2($server .'/?m=app&a=add', 0, $data);//访问server中./control/app.php的动作onadd,向server 登记信息,并返回配置相关信息.
echo('登记应用返回串:'.$re. '<br/>');
if ($re == '-1') {
exit('向服务器登记应用时出错,可能原因:握手串有误,请尝试重新从应用中心安装,或者版本改动.');
}
$re = explode('|', $re);
//如果返回串发生变化,需要做对应更改
$key = $re[0];
$aid = $re[1];
$dbHost = $re[2];
$dbName = $re[3];
$dbUser = $re[4];
$dbPwd = $re[5];
$dbCharset = $re[6];
$dbPre = $re[7];
$charset = $re[8];
$serverIp = '';
/* 测试返回数据库配置信息正确性 */
$link = @mysql_connect($dbHost, $dbUser, $dbPwd) or die('测试连接数据库出错' .mysql_error());
@mysql_select_db($dbName, $link) or die('无法选择库,可能是库名有误');
@mysql_query("SET NAMES {$dbCharset} ;", $link) or die('无法设置库查询编码,可能编码有误' . mysql_error($link));
$result = @mysql_query("SELECT * FROM `{$dbPre}admins` LIMIT 1 ;", $link) or die('无法查询管理员表,可能前缀有误' . mysql_error($link));
$result = mysql_num_rows($result);
if (empty($result)) {
echo '管理表中没数据,可能是库名指向弃用的库<br/>';
}
@mysql_close($link);
echo '测试连接数据库成功<br/>';
/* 根据返回拼接配置文件 */
$config = "<?php
define('UC_CONNECT', 'mysql'); // 连接 UCenter 的方式: mysql/NULL, 默认为空时为 fscoketopen()
// mysql 是直接连接的数据库, 为了效率, 建议采用 mysql
//数据库相关 (mysql 连接时, 并且没有设置 UC_DBLINK 时, 需要配置以下变量)
define('UC_DBHOST', '{$dbHost}'); // UCenter 数据库主机
define('UC_DBUSER', '{$dbUser}'); // UCenter 数据库用户名
define('UC_DBPW', '{$dbPwd}'); // UCenter 数据库密码
define('UC_DBNAME', '{$dbName}'); // UCenter 数据库名称
define('UC_DBCHARSET', '{$dbCharset}'); // UCenter 数据库字符集
define('UC_DBTABLEPRE', '{$dbPre}'); // UCenter 数据库表前缀
//通信相关
define('UC_KEY', '{$key}'); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define('UC_API', '{$server}'); // UCenter 的 URL 地址, 在调用头像时依赖此常量
define('UC_CHARSET', '{$charset}'); // UCenter 的字符集
define('UC_IP', '{$serverIp}'); // UCenter 的 IP, 当 UC_CONNECT 为非 mysql 方式时, 并且当前应用服务器解析域名有问题时, 请设置此值
define('UC_APPID', {$aid}); // 当前应用的 ID
";
@file_put_contents(THISDIR .'/uc_config.php', $config) or die ('写入配置内容出错');
echo '写入配置文件完成<br/>';
}
/*
* 显示安装配置面板
*/
function showBar() {//在uc server应用安装管理中点击提交时(URL安装形式),uc会POST几个值过来:握手串,server url,本url
?>
<html>
<head>
<style>
input{
width:400px;
border:1px solid black;
margin-top:5px;
margin-left:5px;
margin-right:5px;
}
</style>
</head>
<body>
<form action="install.php?install=1" method="POST" target="_blank">
uc服务端URL<input name="serverUrl" value="<?php echo $_POST['ucapi'];?>" readonly="true"/>不需要更改<br/>
uc服务端IP<input name="serverId" value="" />DNS无法根据域名解释时才需要填写<br/>
添加应用握手串<input name="ucfounderpw" value="<?php echo $_POST['ucfounderpw'];?>" readonly="true"/>不需要更改<br/>
应用类型<input name="appType" value="OTHER"/>一般不需要更改,可看uc应用管理有什么类型<br/>
应用名字<input name="appName" value="公用登录/注册应用" />随意<br/>
应用URL<input name="appUrl" value="<?php echo str_replace('/install.php', '', $_POST['appurl']);?>" />它会以 应用URL+ '/api/uc.php?code=xxx'形式来检测通信,所以必须存在此路径,可以根据uc examle/api/uc.php进行修改,假设不是使用它们的产品时.<br/>
应用IP<input name="appIp" value="" />一般不需要填写<br/>
应用用户页面<input name="appProUrl" value="" />不需要填写<br/>
应用页面编码<input name="appCharset" value="UTF-8" />不需要更改<br/>
应用数据库编码<input name="appDbCharset" value="UTF8" />建议使用utf8<br/>
应用模板<input name="appTemplate" value="" />可不写<br/>
应用允许ip<input name="appAllowIps" value="" />可不写<br/>
<button>安装</button>
</form>
</body>
</html>
<?php
}
--------------
---------------------检测应用通信成败独立代码(./api/uc.php),通信代码已运作正常-------
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: uc.php 22959 2011-06-09 02:19:29Z svn_project_zhangjie $
*/
error_reporting(0);
define('UC_CLIENT_VERSION', '1.6.0');
define('UC_CLIENT_RELEASE', '20110501');
define('API_DELETEUSER', 1);
define('API_RENAMEUSER', 1);
define('API_GETTAG', 1);
define('API_SYNLOGIN', 1);
define('API_SYNLOGOUT', 1);
define('API_UPDATEPW', 1);
define('API_UPDATEBADWORDS', 1);
define('API_UPDATEHOSTS', 1);
define('API_UPDATEAPPS', 1);
define('API_UPDATECLIENT', 1);
define('API_UPDATECREDIT', 1);
define('API_GETCREDIT', 1);
define('API_GETCREDITSETTINGS', 1);
define('API_UPDATECREDITSETTINGS', 1);
define('API_ADDFEED', 1);
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '1');
@define('IN_DISCUZ', true);
@define('IN_API', true);
@define('CURSCRIPT', 'api');
define('DISCUZ_ROOT', dirname(dirname(__FILE__)).'/');
define('UC_CONFIG', DISCUZ_ROOT.'config/uc_config.php');
require_once UC_CONFIG;
require_once DISCUZ_ROOT.'/uc_client/client.php';//需要使用uc_authcode
$get = $post = array();
$code = @$_GET['code'];
parse_str(uc_authcode($code, 'DECODE', UC_KEY), $get);//解密加密串到数组
if(time() - $get['time'] > 3600) {
exit('Authracation has expiried');
}
if(empty($get)) {
exit('Invalid Request');
}
include_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
$post = xml_unserialize(file_get_contents('php://input'));
@file_put_contents(DISCUZ_ROOT .'api/debug.txt', @file_get_contents(DISCUZ_ROOT .'api/debug.txt').var_export(array($get,$post, $_SERVER, "\n----------\n"), 1));//调试记录服务器调用发送什么过来
if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcredit', 'getcreditsettings', 'updatecreditsettings', 'addfeed'))) {
$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 DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
}
return xml_serialize($arr, $htmlon);
}
function uc_note() {
}
/*
* 检测应用是否通信成功
*/
function test($get, $post) {
return API_RETURN_SUCCEED;
}
/*
* 删除用户
*/
function deleteuser($get, $post) {
global $_G;
if(!API_DELETEUSER) {
return API_RETURN_FORBIDDEN;
}
//对本平台删除处理
return API_RETURN_SUCCEED;
}
function renameuser($get, $post) {
global $_G;
if(!API_RENAMEUSER) {
return API_RETURN_FORBIDDEN;
}
//对本平台改名处理
return API_RETURN_SUCCEED;
}
function gettag($get, $post) {
global $_G;
if(!API_GETTAG) {
return API_RETURN_FORBIDDEN;
}
return $this->_serialize(array($get['id'], array()), 1);
}
/*
* 同步登录
*/
function synlogin($get, $post) {
global $_G;
if(!API_SYNLOGIN) {
return API_RETURN_FORBIDDEN;
}
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
}
/*
* 同步登出
*/
function synlogout($get, $post) {
global $_G;
if(!API_SYNLOGOUT) {
return API_RETURN_FORBIDDEN;
}
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');//设置可跨域写cookie
//设置本平台cookie失效
}
/*
* 更新密码
*/
function updatepw($get, $post) {
global $_G;
if(!API_UPDATEPW) {
return API_RETURN_FORBIDDEN;
}
return API_RETURN_SUCCEED;
}
function updatebadwords($get, $post) {
global $_G;
if(!API_UPDATEBADWORDS) {
return API_RETURN_FORBIDDEN;
}
$data = array();
if(is_array($post)) {
foreach($post as $k => $v) {
$data['findpattern'][$k] = $v['findpattern'];
$data['replace'][$k] = $v['replacement'];
}
}
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/badwords.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'badwords\'] = '.var_export($data, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
function updatehosts($get, $post) {
global $_G;
if(!API_UPDATEHOSTS) {
return API_RETURN_FORBIDDEN;
}
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/hosts.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
/*
* 更新配置信息
*/
function updateapps($get, $post) {
global $_G;
if(!API_UPDATEAPPS) {
return API_RETURN_FORBIDDEN;
}
$UC_API = '';
if($post['UC_API']) {
$UC_API = $post['UC_API'];
unset($post['UC_API']);
}
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/apps.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
if($UC_API && is_writeable(UC_CONFIG)) {
if(preg_match('/^https?:\/\//is', $UC_API)) {
$configfile = trim(file_get_contents(UC_CONFIG));
$configfile = substr($configfile, -2) == '?>' ? substr($configfile, 0, -2) : $configfile;
$configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '".addslashes($UC_API)."');", $configfile);
if($fp = @fopen(UC_CONFIG, 'w')) {
@fwrite($fp, trim($configfile));
@fclose($fp);
}
}
}
return API_RETURN_SUCCEED;
}
/*
* 更新客户配置
*/
function updateclient($get, $post) {
global $_G;
if(!API_UPDATECLIENT) {
return API_RETURN_FORBIDDEN;
}
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/settings.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'settings\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
/*
* 更新某用户积分
*/
function updatecredit($get, $post) {
global $_G;
if(!API_UPDATECREDIT) {
return API_RETURN_FORBIDDEN;
}
return API_RETURN_SUCCEED;
}
/*
* 获取用户积分
*/
function getcredit($get, $post) {
global $_G;
if(!API_GETCREDIT) {
return API_RETURN_FORBIDDEN;
}
$uid = intval($get['uid']);
$credit = intval($get['credit']);
$_G['uid'] = $uid;
return getuserprofile('extcredits'.$credit);
}
/*
* 获取积分配置
*/
function getcreditsettings($get, $post) {
global $_G;
if(!API_GETCREDITSETTINGS) {
return API_RETURN_FORBIDDEN;
}
$credits = array();
foreach($_G['setting']['extcredits'] as $id => $extcredits) {
$credits[$id] = array(strip_tags($extcredits['title']), $extcredits['unit']);
}
return $this->_serialize($credits);
}
/*
* 更新积分配置
*/
function updatecreditsettings($get, $post) {
global $_G;
if(!API_UPDATECREDITSETTINGS) {
return API_RETURN_FORBIDDEN;
}
$outextcredits = array();
foreach($get['credit'] as $appid => $credititems) {
if($appid == UC_APPID) {
foreach($credititems as $value) {
$outextcredits[$value['appiddesc'].'|'.$value['creditdesc']] = array(
'appiddesc' => $value['appiddesc'],
'creditdesc' => $value['creditdesc'],
'creditsrc' => $value['creditsrc'],
'title' => $value['title'],
'unit' => $value['unit'],
'ratiosrc' => $value['ratiosrc'],
'ratiodesc' => $value['ratiodesc'],
'ratio' => $value['ratio']
);
}
}
}
$tmp = array();
foreach($outextcredits as $value) {
$key = $value['appiddesc'].'|'.$value['creditdesc'];
if(!isset($tmp[$key])) {
$tmp[$key] = array('title' => $value['title'], 'unit' => $value['unit']);
}
$tmp[$key]['ratiosrc'][$value['creditsrc']] = $value['ratiosrc'];
$tmp[$key]['ratiodesc'][$value['creditsrc']] = $value['ratiodesc'];
$tmp[$key]['creditsrc'][$value['creditsrc']] = $value['ratio'];
}
$outextcredits = $tmp;
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/creditsettings.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'creditsettings\'] = '.var_export($outextcredits, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
function addfeed($get, $post) {
global $_G;
if(!API_ADDFEED) {
return API_RETURN_FORBIDDEN;
}
return API_RETURN_SUCCEED;
}
}
-----------