/**
* @author Ma Bingyao(andot@ujn.edu.cn)
* @copyright CoolCode.CN
* @package ASP_PHPRPC_SERVER
* @version 2.1
* @last_update 2006-07-04
* @link http://www.coolcode.cn/?p=143
*
* Example usage:
*
* server.asp
* <%@ CodePage = 65001 %>
* <script runat="server" type="text/javascript" src="phprpc_server.js"></script>
* <%
* function add(a, b)
* add = a + b
* end function
* function subtract(a, b)
* subtract = a - b
* end function
* phprpc_server.create(Array('add', 'sub'));
* %>
*/
function addjsslashes(str, flag) {
var test;
if (flag == false) {
test = /([\0-\037\042\047\134])/g;
}
else {
test = /([\0-\037\042\047\134\177-\377])/g;
}
return str.replace(test, function ($1) {
var s = $1.charCodeAt(0).toString(8);
return '\\' + ((s.length == 1) ? "00" : ((s.length == 2) ? "0" : "")) + s;
});
}
function getGMTDate(date) {
var week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return week[date.getUTCDay()] + ", " + date.toGMTString();
}
function IsEmpty(o) {
if (typeof(o) == "object" && String(o) == "undefined") return true;
return false;
}
function phprpc_server(functions) {
Response.CodePage = 65001;
Session.CodePage = 65001;
var func, args, result, encrypt;
var date = getGMTDate(new Date());
Response.Buffer = true;
Response.ContentType = "text/plain";
Response.Charset = "utf-8";
Response.AddHeader("X-Powered-By", "PHPRPC Server/2.1");
Response.AddHeader("Date", date);
Response.AddHeader("Last0Modified", date);
Response.AddHeader("Cache-Control", "no-store, no-cache, must-revalidate");
Response.AddHeader("Cache-Control", "pre-check=0, post-check=0, max-age=0");
Response.AddHeader("Content-Encoding", "none");
if (functions.constructor == Array) {
this.functions = functions;
}
else if (functions.constructor == VBArray) {
this.functions = functions.toArray();
}
else {
this.functions = [functions];
}
this.encode = true;
if (!IsEmpty(Request('phprpc_encode'))) {
this.encode = String(Request('phprpc_encode')).toLowerCase();
if (this.encode == "false") {
this.encode = false;
}
}
if (!IsEmpty(Request('phprpc_callback'))) {
this.callback = utf8to16(base64decode(String(Request('phprpc_callback'))));
}
else {
this.callback = "";
}
this.ref = true;
if (!IsEmpty(Request('phprpc_ref'))) {
this.ref = String(Request('phprpc_ref')).toLowerCase();
if (this.ref == "false") {
this.ref = false;
}
}
this.errno = 0;
this.errstr = "";
try {
this.encrypt = false;
if (!IsEmpty(Request('phprpc_encrypt'))) {
this.encrypt = String(Request('phprpc_encrypt'));
if (this.encrypt === "true") this.encrypt = true;
if (this.encrypt === "false") this.encrypt = false;
}
if (!IsEmpty(Request('phprpc_func'))) {
func = String(Request('phprpc_func'));
if (this.is_defined(func)) {
if (!IsEmpty(Request('phprpc_args'))) {
args = base64decode(String(Request('phprpc_args')));
if (this.encrypt > 0) {
if (typeof(Session('PHPRPC_ENCRYPT')['k']) != "undefined") {
args = xxtea_decrypt(args, Session('PHPRPC_ENCRYPT')['k']);
}
else {
this.errno = 1;
this.errstr = "Can't find the key for decryption.";
}
}
args = unserialize(args);
}
else {
args = [];
}
result = serialize(this.call(func, args));
if (this.ref) {
args = serialize(args);
}
if (this.encrypt > 0) {
if (typeof(Session('PHPRPC_ENCRYPT')['k']) != "undefined") {
if (this.encrypt > 1) {
result = xxtea_encrypt(result, Session('PHPRPC_ENCRYPT')['k']);
}
if (this.ref) {
args = xxtea_encrypt(args, Session('PHPRPC_ENCRYPT')['k']);
}
}
else {
this.errno = 1;
this.errstr = "Can't find the key for encryption.";
}
}
if (this.encode) {
result = base64encode(result);
if (this.ref) {
args = base64encode(args);
}
}
else {
result = addjsslashes(result);
if (this.ref) {
args = addjsslashes(args);
}
}
}
else {
this.errno = 1;
this.errstr = "Can't find this function " + func + "().";
}
Response.Clear();
if (this.errno != 1) {
Response.Write('phprpc_result="' + result + '";\r\n');
if (this.ref) {
Response.Write('phprpc_args="' + args + '";\r\n');
}
}
Response.Write('phprpc_errno="' + this.errno + '";\r\n');
if (this.encode) {
Response.Write('phprpc_errstr="' + base64encode(utf16to8(this.errstr)) + '";\r\n');
Response.Write('phprpc_output="";\r\n');
}
else {
Response.Write('phprpc_errstr="' + addjsslashes(this.errstr, false) + '";\r\n');
Response.Write('phprpc_output="";\r\n');
}
}
else {
if (this.encrypt != false) {
if (this.encrypt === true) {
encrypt = phprpc_keypair[Math.floor(Math.random() * phprpc_keypair.length)];
Session('PHPRPC_ENCRYPT') = [];
Session('PHPRPC_ENCRYPT')['x'] = rand(127, 1);
Session('PHPRPC_ENCRYPT')['g'] = dec2num(encrypt['g']);
Session('PHPRPC_ENCRYPT')['p'] = dec2num(encrypt['p']);
encrypt['y'] = num2dec(pow_mod(Session('PHPRPC_ENCRYPT')['g'],
Session('PHPRPC_ENCRYPT')['x'],
Session('PHPRPC_ENCRYPT')['p']));
}
else {
Session('PHPRPC_ENCRYPT')['y'] = dec2num(this.encrypt);
var key = num2str(pow_mod(Session('PHPRPC_ENCRYPT')['y'],
Session('PHPRPC_ENCRYPT')['x'],
Session('PHPRPC_ENCRYPT')['p']));
var n = 16 - key.length;
var k = [];
for (var i = 0; i < n; i++) {
k[i] = '\0';
}
k[n] = key;
Session('PHPRPC_ENCRYPT')['k'] = k.join('');
encrypt = true;
}
if (this.encode) {
Response.Write('phprpc_encrypt="' + base64encode(serialize(encrypt)) + '";\r\n');
}
else {
Response.Write('phprpc_encrypt="' + addjsslashes(serialize(encrypt)) + '";\r\n');
}
}
if (this.encode) {
Response.Write('phprpc_functions="' + base64encode(serialize(this.functions)) + '";\r\n');
}
else {
Response.Write('phprpc_functions="' + addjsslashes(serialize(this.functions)) + '";\r\n');
}
}
Response.Write(this.callback);
}
catch (e) {
this.errno = 1;
this.errstr = e.description;
Response.Clear();
Response.Write('phprpc_errno="' + this.errno + '";\r\n');
if (this.encode) {
Response.Write('phprpc_errstr="' + base64encode(utf16to8(this.errstr)) + '";\r\n');
}
else {
Response.Write('phprpc_errstr="' + addjsslashes(this.errstr, false) + '";\r\n');
}
Response.Write('phprpc_output="";\r\n');
Response.Write(this.callback);
}
Response.End();
}
phprpc_server.prototype.is_defined = function (func) {
for (var i = 0, n = this.functions.length; i < n; i++) {
if (this.functions[i] == func) return true;
}
return false;
}
phprpc_server.prototype.call = function (func, args) {
var a = [];
for (var i = 0, n = args.length; i < n; i++) {
a[i] = 'args[' + i + ']';
}
return eval(func + "(" + a.join(', ') + ")");
}
phprpc_server.create = function (functions) {
new phprpc_server(functions);
}