Skip to content

Commit

Permalink
移除原作者的Base64实现使用Java8自带库实现,并解决乱码问题
Browse files Browse the repository at this point in the history
  • Loading branch information
renfei committed Dec 23, 2020
1 parent fd9b641 commit 1f6f6f0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 418 deletions.
19 changes: 9 additions & 10 deletions src/main/java/net/renfei/discuz/ucenter/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* ================================================
Expand Down Expand Up @@ -395,7 +394,7 @@ protected String ucFopen(String url, int limit, String post, String cookie, bool
}

public String ucAppLs() {
String returnString = call_user_func(UC_API_FUNC, "app", "ls", null);
String returnString = callUserFunc(UC_API_FUNC, "app", "ls", null);
return UC_CONNECT.equals("mysql") ? returnString : ucUnserialize(returnString);
}

Expand All @@ -414,8 +413,8 @@ public String ucAppLs() {
* -6 : 该 email 已经被注册
* >1 : 表示成功,数值为 UID
*/
public String uc_user_register(String username, String password, String email) {
return uc_user_register(username, password, email, "", "");
public String ucUserRegister(String username, String password, String email) {
return ucUserRegister(username, password, email, "", "");
}

/**
Expand All @@ -435,14 +434,14 @@ public String uc_user_register(String username, String password, String email) {
* -6 : 该 email 已经被注册
* >1 : 表示成功,数值为 UID
*/
public String uc_user_register(String username, String password, String email, String questionid, String answer) {
public String ucUserRegister(String username, String password, String email, String questionid, String answer) {
Map<String, Object> args = new HashMap<String, Object>();
args.put("username", username);
args.put("password", password);
args.put("email", email);
args.put("questionid", questionid);
args.put("answer", answer);
return call_user_func(UC_API_FUNC, "user", "register", args);
return callUserFunc(UC_API_FUNC, "user", "register", args);
}

/**
Expand Down Expand Up @@ -500,7 +499,7 @@ public String ucUserLogin(String username, String password, int isuid, int check
args.put("checkques", checkques);
args.put("questionid", questionid);
args.put("answer", answer);
String returnString = call_user_func(UC_API_FUNC, "user", "login", args);
String returnString = callUserFunc(UC_API_FUNC, "user", "login", args);
return UC_CONNECT.equals("mysql") ? returnString : ucUnserialize(returnString);
}

Expand Down Expand Up @@ -538,7 +537,7 @@ public String ucGetUser(String username, int isuid) {
Map<String, Object> args = new HashMap<String, Object>();
args.put("username", username);
args.put("isuid", isuid);
String returnString = call_user_func(UC_API_FUNC, "user", "get_user", args);
String returnString = callUserFunc(UC_API_FUNC, "user", "get_user", args);
return UC_CONNECT.equals("mysql") ? returnString : ucUnserialize(returnString);
}

Expand Down Expand Up @@ -571,7 +570,7 @@ public String ucUserEdit(String username, String oldpw, String newpw, String ema
args.put("ignoreoldpw", ignoreoldpw);
args.put("questionid", questionid);
args.put("answer", answer);
return call_user_func(UC_API_FUNC, "user", "edit", args);
return callUserFunc(UC_API_FUNC, "user", "edit", args);
}

/**
Expand All @@ -585,7 +584,7 @@ public String ucUserEdit(String username, String oldpw, String newpw, String ema
public String ucUserDelete(String uid) {
Map<String, Object> args = new HashMap<String, Object>();
args.put("uid", uid);
return call_user_func(UC_API_FUNC, "user", "delete", args);
return callUserFunc(UC_API_FUNC, "user", "delete", args);
}

/**
Expand Down
183 changes: 97 additions & 86 deletions src/main/java/net/renfei/discuz/ucenter/client/PHPFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Map;

import net.renfei.discuz.ucenter.util.Base64;
/**
* ================================================
* Discuz! Ucenter API for JAVA
* ================================================
* 构造本接口运行所需要PHP的内置函数
*
* <p>
* 更多信息:http://code.google.com/p/discuz-ucenter-api-for-java
* 原作者:梁平 (no_ten@163.com)
* 创建时间:2009-2-20
Expand All @@ -21,89 +22,99 @@
* 修改时间:2020-12-17
*/
public abstract class PHPFunctions {
//JAVA EXTRA METHOD

protected String urlencode(String value) {
try {
return URLEncoder.encode(value,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
protected String md5(String input){
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
return byte2hex(md.digest(input.getBytes()));
}
protected String md5(long input){
return md5(String.valueOf(input));
}
protected String base64Decode(String input){
try {
return new String(Base64.decode(input.toCharArray()),"iso-8859-1");
} catch (Exception e) {
return e.getMessage();
}
}

protected String base64Encode(String input){
try {
return new String(Base64.encode(input.getBytes("iso-8859-1")));
} catch (Exception e) {
return e.getMessage();
}
}
protected String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs.append("0").append(stmp);
} else {
hs.append(stmp);
}
}
return hs.toString();
}
protected String substr(String input,int begin, int length){
return input.substring(begin, begin+length);
}
protected String substr(String input,int begin){
if(begin>0){
return input.substring(begin);
}else{
return input.substring(input.length()+ begin);
}
}
protected long microTime(){
return System.currentTimeMillis();
}
protected long time(){
return System.currentTimeMillis()/1000;
}
protected String sprintf(String format, long input){
String temp = "0000000000"+input;
return temp.substring(temp.length()-10);
}
protected String call_user_func(String function, String model, String action, Map<String,Object> args){
if("uc_api_mysql".equals(function)){
return this.ucApiMysql(model, action, args);
}
if("uc_api_post".equals(function)){
return this.ucApiPost(model, action, args);
}
return "";
}

public abstract String ucApiPost(String module, String action, Map<String,Object> arg );

public abstract String ucApiMysql(String model, String action, Map args);
//JAVA EXTRA METHOD

protected String urlencode(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}

protected String md5(String input) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
return byte2hex(md.digest(input.getBytes()));
}

protected String md5(long input) {
return md5(String.valueOf(input));
}

protected String base64Decode(String input) {
try {
return new String(Base64.getDecoder().decode(input), StandardCharsets.ISO_8859_1);
} catch (Exception e) {
return e.getMessage();
}
}

protected String base64Encode(String input) {
try {
return new String(Base64.getEncoder().encode(input.getBytes(StandardCharsets.ISO_8859_1)));
} catch (Exception e) {
return e.getMessage();
}
}

protected String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs.append("0").append(stmp);
} else {
hs.append(stmp);
}
}
return hs.toString();
}

protected String substr(String input, int begin, int length) {
return input.substring(begin, begin + length);
}

protected String substr(String input, int begin) {
if (begin > 0) {
return input.substring(begin);
} else {
return input.substring(input.length() + begin);
}
}

protected long microTime() {
return System.currentTimeMillis();
}

protected long time() {
return System.currentTimeMillis() / 1000;
}

protected String sprintf(String format, long input) {
String temp = "0000000000" + input;
return temp.substring(temp.length() - 10);
}

protected String callUserFunc(String function, String model, String action, Map<String, Object> args) {
if ("uc_api_mysql".equals(function)) {
return this.ucApiMysql(model, action, args);
}
if ("uc_api_post".equals(function)) {
return this.ucApiPost(model, action, args);
}
return "";
}

public abstract String ucApiPost(String module, String action, Map<String, Object> arg);

public abstract String ucApiMysql(String model, String action, Map args);

}
Loading

0 comments on commit 1f6f6f0

Please sign in to comment.