/**
* @author Ma Bingyao(andot@ujn.edu.cn)
* @copyright CoolCode.CN
* @package ASP_PHPRPC_CLIENT
* @version 2.1
* @last_update 2006-07-10
* @link http://www.coolcode.cn/?p=143
*
* Example usage:
*
* server.asp
* <%@ CodePage = 65001 %>
* <script runat="server" type="text/javascript" src="phprpc_client.js"></script>
* <%
* phprpc_client.create('rpc')
* rpc.use_service('http://test.coolcode.cn/phprpc/server.php')
* Response.Write(rpc.add(1,2))
* %>
*/
function phprpc_error(errno, errstr) {
this.errno = errno;
this.errstr = errstr;
}
function phprpc_client() {
this.__url = '';
this.__encrypt = false;
this.__cookie = '';
this.encrypt = 0;
this.args = null;
this.warning = null;
this.output = "";
this.use_service = function (url, encrypt) {
if (typeof(encrypt) == "undefined") {
encrypt = this.__encrypt;
}
if (typeof(encrypt) == "boolean") {
this.__encrypt = encrypt;
}
if (typeof(this.__name) == "undefined") {
return false;
}
this.__url = url;
if (encrypt === true) {
this.__xmlhttp.open("GET", [this.__url, '?phprpc_encrypt=true&phprpc_encode=false'].join(''), false);
this.__xmlhttp.send(null);
if (this.__xmlhttp.responseText) {
var cookie = this.__xmlhttp.getResponseHeader('Set-Cookie');
if (cookie) {
this.__cookie = cookie.replace(/path=.*($|\;\W*)|domain=.*($|\;\W*)/g, '').replace(/\;\W*$/g, '');
}
eval(this.__xmlhttp.responseText);
if (typeof(phprpc_encrypt) == "undefined") {
this.__encrypt = false;
encrypt = false;
}
else {
this.__encrypt = unserialize(phprpc_encrypt);
this.__encrypt['p'] = dec2num(this.__encrypt['p']);
this.__encrypt['g'] = dec2num(this.__encrypt['g']);
this.__encrypt['y'] = dec2num(this.__encrypt['y']);
this.__encrypt['x'] = rand(127, 1);
var key = pow_mod(this.__encrypt['y'],
this.__encrypt['x'],
this.__encrypt['p']);
key = num2str(key);
var n = 16 - key.length;
var k = [];
for (var i = 0; i < n; i++) k[i] = '\0';
k[n] = key;
this.__encrypt['k'] = k.join('');
encrypt = num2dec(pow_mod(this.__encrypt['g'],
this.__encrypt['x'],
this.__encrypt['p']));
}
}
}
this.__xmlhttp.open("GET", [this.__url, '?phprpc_encrypt=', encrypt, '&phprpc_encode=false'].join(''), false);
if (this.__cookie != '')
{
this.__xmlhttp.setRequestHeader('Cookie', this.__cookie);
}
this.__xmlhttp.send(null);
if (this.__xmlhttp.responseText) {
eval(this.__xmlhttp.responseText);
var functions = unserialize(phprpc_functions);
var func = [];
for (var i = 0, n = functions.length; i < n; i++) {
func[i] = [this.__name, ".", functions[i],
" = function () { return this.__call('",
functions[i],
"', this.__args_to_array(arguments)); }\r\n",
this.__name, ".", functions[i],
".ref = false;\r\n"].join('');
}
eval(func.join(''));
}
};
this.__call = function (func, args) {
var __args = serialize(args);
if ((this.__encrypt !== false) && (this.encrypt > 0)) {
__args = xxtea_encrypt(__args, this.__encrypt['k']);
}
__args = base64encode(__args);
var request = ['phprpc_func=', func,
'&phprpc_args=', __args,
'&phprpc_encode=false',
'&phprpc_encrypt=', this.encrypt];
var ref = eval([this.__name, ".", func, ".ref"].join(''));
if (!ref) {
request[request.length] = '&phprpc_ref=false';
}
var session = {'args': args, 'ref': ref, 'encrypt': this.encrypt};
this.__xmlhttp.open("POST", this.__url, false);
this.__xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
if (this.__cookie != '')
{
this.__xmlhttp.setRequestHeader('Cookie', this.__cookie);
}
this.__xmlhttp.send(request.join('').replace(/\+/g, '%2B'));
if (this.__xmlhttp.responseText) {
this.__get_result(this.__xmlhttp, session);
this.output = phprpc_output;
this.args = phprpc_args;
this.warning = phprpc_warning;
}
else {
phprpc_result = new phprpc_error(1, "No data received from server");
}
return phprpc_result;
};
this.__get_result = function (xmlhttp, session) {
eval(xmlhttp.responseText);
phprpc_warning = null;
if ((phprpc_errno != 1) && (phprpc_errno != 16) &&
(phprpc_errno != 64) && (phprpc_errno != 256)) {
if ((this.__encrypt !== false) && (session.encrypt > 0)) {
if (session.encrypt > 1) {
phprpc_result = xxtea_decrypt(phprpc_result, this.__encrypt['k']);
}
if (session.ref) {
phprpc_args = xxtea_decrypt(phprpc_args, this.__encrypt['k']);
}
}
phprpc_result = unserialize(phprpc_result);
if (session.ref) {
phprpc_args = unserialize(phprpc_args);
}
else {
phprpc_args = session.args;
}
phprpc_warning = new phprpc_error(phprpc_errno, phprpc_errstr);
}
else {
phprpc_result = new phprpc_error(phprpc_errno, phprpc_errstr);
phprpc_args = session.args;
}
}
this.__create_xmlhttp = function() {
var MSXML = ['MSXML2.ServerXMLHTTP.5.0', 'MSXML2.ServerXMLHTTP.4.0', 'MSXML2.ServerXMLHTTP.3.0', 'MSXML2.ServerXMLHTTP', 'Microsoft.ServerXMLHTTP'];
for (var i = 0, n = MSXML.length; i < n; i++) {
try {
return new ActiveXObject(MSXML[i]);
}
catch(e) {}
}
throw new Error("Your server does not support xmlhttp objects");
};
this.__xmlhttp = this.__create_xmlhttp();
this.__args_to_array = function (args) {
var argArray = [];
var n = args.length;
for (i = 0; i < n; i++) {
argArray[i] = args[i];
}
return argArray;
}
}
phprpc_client.create = function (name, encrypt) {
eval([name, ' = new phprpc_client();', name, '.__name = "', name, '";'].join(''));
if (encrypt) {
encrypt = true;
eval([name, '.__encrypt = ', encrypt, ';'].join(''));
}
}