package com.fivestars.interfaces.bbs.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* ================================================
* Discuz! Ucenter API for JAVA
* ================================================
* UC Client 通用调用该类来实现与UC Server之间的通信。
* 该类实现与UC Server通信的所有接口函数
*
* 更多信息:http://code.google.com/p/discuz-ucenter-api-for-java/
* 作者:梁平 (no_ten@163.com)
* 创建时间:2009-2-20
*/
public class Client extends PHPFunctions{
public static boolean IN_UC = true;
public static String UC_IP = "127.0.0.1";
public static String UC_API = "http://localhost/uc";
public static String UC_CONNECT = "";
public static String UC_KEY = "123456";
public static String UC_APPID = "3";
public static String UC_CLIENT_VERSION = "1.0";
public static String UC_CLIENT_RELEASE = "20090212";
public static String UC_ROOT = ""; //note 用户中心客户端的根目录 UC_CLIENTROOT
public static String UC_DATADIR = UC_ROOT+"./data/"; //note 用户中心的数据缓存目录
public static String UC_DATAURL = "UC_API"+"/data"; //note 用户中心的数据 URL
public static String UC_API_FUNC = UC_CONNECT.equals("mysql") ? "uc_api_mysql" : "uc_api_post";
public static String[] uc_controls = {};
static {
InputStream in = Client.class.getClassLoader().getResourceAsStream("config.properties");
Properties properties = new Properties();
try {
properties.load(in);
UC_API = properties.getProperty("UC_API");
UC_IP = properties.getProperty("UC_IP");
UC_KEY = properties.getProperty("UC_KEY");
UC_APPID = properties.getProperty("UC_APPID");
UC_CONNECT = properties.getProperty("UC_CONNECT");
} catch (Exception e) {
e.printStackTrace();
}
}
protected String uc_serialize(String $arr, int $htmlon) {
//return xml_serialize($arr, $htmlon);
return $arr;
}
protected String uc_unserialize(String $s) {
//include_once UC_ROOT.'./lib/xml.class.php';
//return xml_unserialize($s);
return $s;
}
protected String uc_addslashes(String $string, int $force , boolean $strip ) {
// !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;
}
protected String daddslashes(String $string, int $force) {
return uc_addslashes($string, $force, false);
}
protected String uc_stripslashes(String $string) {
// !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
// if(MAGIC_QUOTES_GPC) {
// return stripslashes($string);
// } else {
return $string;
// }
}
public String uc_api_post(String $module, String $action, Map<String,Object> $arg ) {
StringBuffer $s = new StringBuffer();
String $sep = "";
//foreach($arg as $k => $v) {
for (String $k : $arg.keySet()) {
//$k = ($k);
Object $v = $arg.get($k);
$k = urlencode($k);
if($v.getClass().isAssignableFrom(Map.class)) {
String $s2 = "";
String $sep2 = "";
//foreach($v as $k2 => $v2) {
for(String $k2 : ((Map<String,Object>)$v).keySet() ){
Object $v2 = ((Map<String,Object>)$v).get($k2);
$k2 = urlencode($k2);
$s2 += $sep2+"{"+$k+"}["+$k2+"]="+urlencode(uc_stripslashes( String.valueOf( $v2 )));
$sep2 = "&";
}
$s.append($sep).append($s2);
} else {
$s.append($sep).append($k).append("=").append(urlencode(uc_stripslashes( String.valueOf($v) )));
}
$sep = "&";
}
String $postdata = uc_api_requestdata($module, $action, $s.toString(),"");
return uc_fopen2(UC_API+"/index.php", 500000, $postdata, "", true, UC_IP, 20, true);
}
/**
* 构造发送给用户中心的请求数据
*
* @param string $module 请求的模块
* @param string $action 请求的动作
* @param string $arg 参数(会加密的方式传送)
* @param string $extra 附加参数(传送时不加密)
* @return string
*/
protected String uc_api_requestdata(String $module, String $action, String $arg, String $extra) {
String $input = uc_api_input($arg);
String $post = "m="+$module+"&a="+$action+"&inajax=2&release="+UC_CLIENT_RELEASE+"&input="+$input+"&appid="+UC_APPID+$extra;
return $post;
}
protected String uc_api_url(String $module, String $action, String $arg, String $extra) {
String $url = UC_API+"/index.php?"+uc_api_requestdata($module, $action, $arg, $extra);
return $url;
}
public String uc_api_input(String $data) {
//String $s = $data;
//String $s = urlencode(uc_authcode($data+"&agent="+md5($_SERVER["HTTP_USER_AGENT"])+"&time="+time(), "ENCODE", UC_KEY));
//String $s = urlencode(uc_authcode($data+"&agent="+md5("Java/1.5.0_01")+"&time="+time(), "ENCODE", UC_KEY));
String $s = urlencode(uc_authcode($data+"&agent="+md5("")+"&time="+time(), "ENCODE", UC_KEY));
return $s;
}
/**
* MYSQL 方式取指定的模块和动作的数据
*
* @param string $model 请求的模块
* @param string $action 请求的动作
* @param string $args 参数(会加密的方式传送)
* @return mix
*/
public String uc_api_mysql(String $model, String $action, Map $args) {
// global $uc_controls;
// if(empty($uc_controls[$model])) {
// include_once UC_ROOT.'./lib/db.class.php';
// include_once UC_ROOT.'./model/base.php';
// include_once UC_ROOT."./control/$model.php";
// eval("\$uc_controls['$model'] = new {$model}control();");
// }
if($action.charAt(0) != '_') {
// $args = uc_addslashes($args, 1, true);
// $action = "on"+$action;
// $uc_controls[$model]->input = $args;
// return $uc_controls[$model]->$action($args);
return null;
} else {
return "";
}
}
/**
* 字符串加密以及解密函数
*
* @param string $string 原文或者密文
* @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
* @param string $key 密钥
* @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
* @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
*
* @example
*
* $a = authcode('abc', 'ENCODE', 'key');
* $b = authcode($a, 'DECODE', 'key'); // $b(abc)
*
* $a = authcode('abc', 'ENCODE', 'key', 3600);
* $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
*/
public String uc_authcode(String $string, String $operation){
return uc_authcode($string, $operation, null);
}
public String uc_authcode(String $string, String $operation, String $key){
return uc_authcode($string, $operation, $key, 0);
}
public String uc_authcode(String $string, String $operation, String $key,int $expiry ) {
int $ckey_length = 4; //note 随机密钥长度 取值 0-32;
//note 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
//note 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
//note 当此值为 0 时,则不产生随机密钥
$key = md5( $key!=null ? $key : UC_KEY);
String $keya = md5(substr($key, 0, 16));
String $keyb = md5(substr($key, 16, 16));
String $keyc = $ckey_length > 0? ($operation.equals("DECODE") ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ck
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
discuz-ucenter_api_for_java_src.zip (7个子文件)
src
test
Test.java 3KB
com
fivestars
interfaces
bbs
client
PHPFunctions.java 3KB
Client.java 18KB
api
UC.java 7KB
util
XMLHelper.java 2KB
Base64.java 11KB
config.properties 363B
共 7 条
- 1
资源评论
java1soft
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功