2 #################################################################################
3 ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
4 ## --------------------------------------------------------------------------- ##
5 ## Filename Chat.php ##
6 ## Developed by: TTMMTT ##
7 ## License: TravianX Project ##
8 ## Copyright: TravianX (c) 2010-2011. All rights reserved. ##
10 #################################################################################
12 if (!isset($SAJAX_INCLUDED)) {
14 $GLOBALS['sajax_version'] = '0.12';
15 $GLOBALS['sajax_debug_mode'] = 0;
16 $GLOBALS['sajax_export_list'] = array();
17 $GLOBALS['sajax_request_type'] = 'GET';
18 $GLOBALS['sajax_remote_uri'] = '';
19 $GLOBALS['sajax_failure_redirect'] = '';
22 function sajax_init() {
25 function sajax_get_my_uri() {
26 return $_SERVER["REQUEST_URI"];
28 $sajax_remote_uri = sajax_get_my_uri();
31 function sajax_get_js_repr($value) {
32 $type = gettype($value);
34 if ($type == "boolean") {
35 return ($value) ?
"Boolean(true)" : "Boolean(false)";
37 elseif ($type == "integer") {
38 return "parseInt($value)";
40 elseif ($type == "double") {
41 return "parseFloat($value)";
43 elseif ($type == "array" ||
$type == "object" ) {
46 if ($type == "object") {
47 $value = get_object_vars($value);
49 foreach ($value as $k=>$v) {
50 $esc_key = sajax_esc($k);
52 $s .= "$k: " . sajax_get_js_repr($v) . ", ";
54 $s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", ";
57 $s = substr($s, 0, -2);
61 $esc_val = sajax_esc($value);
67 function sajax_handle_client_request() {
68 global $sajax_export_list;
72 if (! empty($_GET["rs"]))
75 if (!empty($_POST["rs"]))
85 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
86 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
88 header ("Cache-Control: no-cache, must-revalidate");
89 header ("Pragma: no-cache");
90 $func_name = $_GET["rs"];
91 if (! empty($_GET["rsargs"]))
92 $args = $_GET["rsargs"];
97 $func_name = $_POST["rs"];
98 if (! empty($_POST["rsargs"]))
99 $args = $_POST["rsargs"];
104 if (! in_array($func_name, $sajax_export_list))
105 echo "-:$func_name not callable";
108 $result = call_user_func_array($func_name, $args);
109 echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;";
114 function sajax_get_common_js() {
115 global $sajax_debug_mode;
116 global $sajax_request_type;
117 global $sajax_remote_uri;
118 global $sajax_failure_redirect;
120 $t = strtoupper($sajax_request_type);
121 if ($t != "" && $t != "GET" && $t != "POST")
122 return "// Invalid type: $t.. \n\n";
127 // remote scripting library
128 // (c) copyright 2005 modernmethod, inc
130 var sajax_debug_mode
= <?php
echo $sajax_debug_mode ?
"true" : "false"; ?
>;
131 var sajax_request_type
= "<?php echo $t; ?>";
132 var sajax_target_id
= "";
133 var sajax_failure_redirect
= "<?php echo $sajax_failure_redirect; ?>";
135 function sajax_debug(text
) {
136 if (sajax_debug_mode
)
140 function sajax_init_object() {
141 sajax_debug("sajax_init_object() called..")
145 var msxmlhttp
= new Array(
146 'Msxml2.XMLHTTP.5.0',
147 'Msxml2.XMLHTTP.4.0',
148 'Msxml2.XMLHTTP.3.0',
150 'Microsoft.XMLHTTP');
151 for (var i
= 0; i
< msxmlhttp
.length
; i++
) {
153 A
= new ActiveXObject(msxmlhttp
[i
]);
159 if(!A
&& typeof XMLHttpRequest
!= "undefined")
160 A
= new XMLHttpRequest();
162 sajax_debug("Could not create connection object.");
166 var sajax_requests
= new Array();
168 function sajax_cancel() {
169 for (var i
= 0; i
< sajax_requests
.length
; i++
)
170 sajax_requests
[i
].abort();
173 function sajax_do_call(func_name
, args
) {
179 sajax_debug("in sajax_do_call().." + sajax_request_type +
"/" + sajax_target_id
);
180 target_id
= sajax_target_id
;
181 if (typeof(sajax_request_type
) == "undefined" || sajax_request_type
== "")
182 sajax_request_type
= "GET";
184 uri
= "<?php echo $sajax_remote_uri; ?>";
185 if (sajax_request_type
== "GET") {
187 if (uri
.indexOf("?") == -1)
188 uri +
= "?rs=" +
escape(func_name
);
190 uri +
= "&rs=" +
escape(func_name
);
191 uri +
= "&rst=" +
escape(sajax_target_id
);
192 uri +
= "&rsrnd=" +
new Date().getTime();
194 for (i
= 0; i
< args
.length
-1; i++
)
195 uri +
= "&rsargs[]=" +
escape(args
[i
]);
199 else if (sajax_request_type
== "POST") {
200 post_data
= "rs=" +
escape(func_name
);
201 post_data +
= "&rst=" +
escape(sajax_target_id
);
202 post_data +
= "&rsrnd=" +
new Date().getTime();
204 for (i
= 0; i
< args
.length
-1; i++
)
205 post_data
= post_data +
"&rsargs[]=" +
escape(args
[i
]);
208 alert("Illegal request type: " + sajax_request_type
);
211 x
= sajax_init_object();
213 if (sajax_failure_redirect
!= "") {
214 location
.href
= sajax_failure_redirect
;
217 sajax_debug("NULL sajax object for user agent:\n" + navigator
.userAgent
);
221 x
.open(sajax_request_type
, uri
, true);
224 sajax_requests
[sajax_requests
.length
] = x
;
226 if (sajax_request_type
== "POST") {
227 x
.setRequestHeader("Method", "POST " + uri +
" HTTP/1.1");
228 x
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
231 x
.onreadystatechange
= function() {
232 if (x
.readyState
!= 4)
235 sajax_debug("received " + x
.responseText
);
239 var txt
= x
.responseText
.replace(/^\s
*|\s
*$
/g
,"");
240 status
= txt
.charAt(0);
241 data
= txt
.substring(2);
244 // let's just assume this is a pre-response bailout and let it slide for now
245 } else if (status
== "-")
246 alert("Error: " + data
);
249 document
.getElementById(target_id
).innerHTML
= eval(data
);
253 var extra_data
= false;
254 if (typeof args
[args
.length
-1] == "object") {
255 callback
= args
[args
.length
-1].callback
;
256 extra_data
= args
[args
.length
-1].extra_data
;
258 callback
= args
[args
.length
-1];
260 callback(eval(data
), extra_data
);
262 sajax_debug("Caught error " + e +
": Could not eval " + data
);
269 sajax_debug(func_name +
" uri = " + uri +
"/post = " + post_data
);
271 sajax_debug(func_name +
" waiting..");
277 $html = ob_get_contents();
282 function sajax_show_common_js() {
283 echo sajax_get_common_js();
286 // javascript escape a value
287 function sajax_esc($val)
289 $val = str_replace("\\", "\\\\", $val);
290 $val = str_replace("\r", "\\r", $val);
291 $val = str_replace("\n", "\\n", $val);
292 $val = str_replace("'", "\\'", $val);
293 return str_replace('"', '\\"', $val);
296 function sajax_get_one_stub($func_name) {
300 // wrapper for <?php echo $func_name; ?>
302 function x_
<?php
echo $func_name; ?
>() {
303 sajax_do_call("<?php echo $func_name; ?>",
304 x_
<?php
echo $func_name; ?
>.arguments
);
308 $html = ob_get_contents();
313 function sajax_show_one_stub($func_name) {
314 echo sajax_get_one_stub($func_name);
317 function sajax_export() {
318 global $sajax_export_list;
320 $n = func_num_args();
321 for ($i = 0; $i < $n; $i++
) {
322 $sajax_export_list[] = func_get_arg($i);
326 $sajax_js_has_been_shown = 0;
327 function sajax_get_javascript()
329 global $sajax_js_has_been_shown;
330 global $sajax_export_list;
333 if (! $sajax_js_has_been_shown) {
334 $html .= sajax_get_common_js();
335 $sajax_js_has_been_shown = 1;
337 foreach ($sajax_export_list as $func) {
338 $html .= sajax_get_one_stub($func);
343 function sajax_show_javascript()
345 echo sajax_get_javascript();
352 function add_data($data) {
353 global $session,$database;
355 $data = explode("|",$data);
356 $name = $session->username
;
357 $msg = htmlspecialchars($data[1]);
358 $id_user = $session->uid
;
359 $alliance = $session->alliance
;
361 $q = "INSERT into ".TB_PREFIX
."chat (id_user,name,alli,date,msg) values ('$id_user','$name','$alliance','$now','$msg')";
362 mysql_query($q, $database->connection
);
365 function get_data() {
366 global $session,$database;
368 $alliance = $session->alliance
;
369 $query = mysql_query("select * from ".TB_PREFIX
."chat where alli='$alliance' order by id desc limit 0,13");
370 while ($r = mysql_fetch_array($query)) {
371 $dates = date("g:i",$r[date
]);
372 $data .= "[{$dates}] <a href='spieler.php?uid={$r['id_user']}'>{$r['name']}</a>: {$r[msg]} <br>";
377 $sajax_request_type = "GET";
379 sajax_export("add_data","get_data");
380 sajax_handle_client_request();