/
ajaxmap.js
298 lines (266 loc) · 8.51 KB
/
ajaxmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
*
* Client side
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program as the file LICENSE.txt; if not, please see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* @author Rafa Rodriguez <rafageist@hotmail.com>
* @link https://divengine.org
* @version 1.2
*/
var DIV_AJAX_MAPPING_ACCESS_DENIED_HOST = "DIV_AJAX_MAPPING_ACCESS_DENIED_HOST";
var DIV_AJAX_MAPPING_ACCESS_DENIED_USER = "DIV_AJAX_MAPPING_ACCESS_DENIED_USER";
var DIV_AJAX_MAPPING_LOGIN_SUCCESSFUL = "DIV_AJAX_MAPPING_LOGIN_SUCCESSFUL";
var DIV_AJAX_MAPPING_LOGIN_FAILED = "DIV_AJAX_MAPPING_LOGIN_FAILED";
var DIV_AJAX_MAPPING_LOGOUT_SUCCESSFUL = "DIV_AJAX_MAPPING_LOGOUT_SUCCESSFUL";
var DIV_AJAX_MAPPING_METHOD_EXECUTED = "DIV_AJAX_MAPPING_METHOD_EXECUTED";
var DIV_AJAX_MAPPING_METHOD_NOT_EXISTS = "DIV_AJAX_MAPPING_METHOD_NOT_EXISTS";
/**
* Client instance.
*
* @param {Object} params - params.server is a string that contain the server address
*
*/
/*
* How to use?
*
* var client = new ajaxmap({server: "http://example.com/server.php"});
* var persons = client.Company.getEmployees();
* var companyPhone = client.Company.phone;
* var enterprise = client.getEnterprise();
*/
var ajaxmap = function (server) {
/**
* Get a valiXMLHttpRequest object
*/
this.getXMLHttpRequestObject = function () {
var result = false;
try {
result = new XMLHttpRequest();
} catch (e) {
var XmlHttpVersions = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
for (var i = 0; i < XmlHttpVersions.length && !result; i++) {
try {
result = new ActiveXObject(XmlHttpVersions[i]);
} catch (e) {
}
}
}
return result;
};
/**
* Send ajax request
*
* @param Object params
*/
this.ajax = function (params) {
if (typeof params.url == 'undefined') {
return null;
}
var xhr = this.getXMLHttpRequestObject();
params.data = (typeof params.data == 'undefined') ? {} : params.data;
xhr.open("POST", encodeURI(params.url), params.async);
xhr.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded; charset=UTF-8');
var s = "";
var k = 0;
for (var i in params.data) {
if (k++ > 0)
s = s + "&";
s = s + encodeURIComponent(i) + "="
+ this.serialize(params.data[i]);
}
xhr.send(s);
var result = null;
eval("result = " + xhr.responseText);
return result;
};
/**
* PHP Serializer
*
* @param mixed_value
* @returns {*}
*/
this.serialize = function (mixed_value) {
var _getType = function (inp) {
var type = typeof inp, match;
var key;
if (type == 'object' && !inp) {
return 'null';
}
if (type == "object") {
if (!inp.constructor) {
return 'object';
}
var cons = inp.constructor.toString();
match = cons.match(/(\w+)\(/);
if (match) {
cons = match[1].toLowerCase();
}
var types = ["boolean", "number", "string", "array"];
for (key in types) {
if (cons == types[key]) {
type = types[key];
break;
}
}
}
return type;
};
var type = _getType(mixed_value);
var val, ktype = '';
switch (type) {
case"function":
val = "";
break;
case"boolean":
val = "b:" + (mixed_value ? "1" : "0");
break;
case"number":
val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
break;
case"string":
mixed_value = this.utf8_encode(mixed_value);
val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
break;
case"array":
case"object":
val = "a";
var count = 0;
var vals = "";
var okey;
var key;
for (key in mixed_value) {
ktype = _getType(mixed_value[key]);
if (ktype == "function") {
continue;
}
okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
vals += this.serialize(okey) +
this.serialize(mixed_value[key]);
count++;
}
val += ":" + count + ":{" + vals + "}";
break;
case"undefined":
default:
val = "N";
break;
}
if (type != "object" && type != "array") {
val += ";";
}
return val;
};
/**
* UTF8 Encode
*
* @param {string} argString
* @return {string}
*/
this.utf8_encode = function (argString) {
var string = (argString + '');
var utftext = "";
var start, end;
var stringl = 0;
start = end = 0;
stringl = string.length;
for (var n = 0; n < stringl; n++) {
var c1 = string.charCodeAt(n);
var enc = null;
if (c1 < 128) {
end++;
} else if (c1 > 127 && c1 < 2048) {
enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
} else {
enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
}
if (enc !== null) {
if (end > start) {
utftext += string.substring(start, end);
}
utftext += enc;
start = end = n + 1;
}
}
if (end > start) {
utftext += string.substring(start, string.length);
}
return utftext;
}
/**
* Call a remote PHP method
*
* @param {string} server
* @param {string} method
* @param {string} params
*/
this.call = function (server, method, params) {
var result = this.ajax({
url: server + "?execute=" + method,
data: params
});
return result;
};
/**
* Login on server
*
* @param {string} server
* @param {string} username
* @param {string} password
*/
this.login = function (server, username, password) {
var result = this.ajax({
url: server + "?login=" + username + "&password=" + password
});
if (result == null)
return DIV_AJAX_MAPPING_LOGIN_FAILED;
return result;
};
/**
* Logout on server
*
* @param {string} server
*/
this.logout = function (server) {
return this.ajax({
url: server + "?logout"
});
};
if (typeof server !== 'undefined') {
// Call to server for retrieving the PHP mapping
var methods = this.ajax({
url: server + "?mapping"
});
// Add methods to this instance
for (m in methods) {
if (methods[m] != "function")
methods[m].__server = server;
eval("this." + m + " = methods." + m + ";");
}
}
// Add some necessary properties and methods
/* Server address */
this.__server = server;
/* Login on server */
this.__login = function (username, password) {
return this.login(this.__server, username, password);
};
/* Logout on server */
this.__logout = function () {
this.logout(this.__server);
};
};