Added Donns Fix for gain mill and bakery
[travianx.git] / GameEngine / Chat.php
blob2aa6bd8adb88f8d8ddaafea39da8dd42e007512f
1 <?php
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. ##
9 ## ##
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" ) {
45 $s = "{ ";
46 if ($type == "object") {
47 $value = get_object_vars($value);
49 foreach ($value as $k=>$v) {
50 $esc_key = sajax_esc($k);
51 if (is_numeric($k))
52 $s .= "$k: " . sajax_get_js_repr($v) . ", ";
53 else
54 $s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", ";
56 if (count($value))
57 $s = substr($s, 0, -2);
58 return $s . " }";
60 else {
61 $esc_val = sajax_esc($value);
62 $s = "'$esc_val'";
63 return $s;
67 function sajax_handle_client_request() {
68 global $sajax_export_list;
70 $mode = "";
72 if (! empty($_GET["rs"]))
73 $mode = "get";
75 if (!empty($_POST["rs"]))
76 $mode = "post";
78 if (empty($mode))
79 return;
81 $target = "";
83 if ($mode == "get") {
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"];
93 else
94 $args = array();
96 else {
97 $func_name = $_POST["rs"];
98 if (! empty($_POST["rsargs"]))
99 $args = $_POST["rsargs"];
100 else
101 $args = array();
104 if (! in_array($func_name, $sajax_export_list))
105 echo "-:$func_name not callable";
106 else {
107 echo "+:";
108 $result = call_user_func_array($func_name, $args);
109 echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;";
111 exit;
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";
124 ob_start();
127 // remote scripting library
128 // (c) copyright 2005 modernmethod, inc
129 // edited by ttmtt
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)
137 alert(text);
140 function sajax_init_object() {
141 sajax_debug("sajax_init_object() called..")
143 var A;
145 var msxmlhttp = new Array(
146 'Msxml2.XMLHTTP.5.0',
147 'Msxml2.XMLHTTP.4.0',
148 'Msxml2.XMLHTTP.3.0',
149 'Msxml2.XMLHTTP',
150 'Microsoft.XMLHTTP');
151 for (var i = 0; i < msxmlhttp.length; i++) {
152 try {
153 A = new ActiveXObject(msxmlhttp[i]);
154 } catch (e) {
155 A = null;
159 if(!A && typeof XMLHttpRequest != "undefined")
160 A = new XMLHttpRequest();
161 if (!A)
162 sajax_debug("Could not create connection object.");
163 return A;
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) {
174 var i, x, n;
175 var uri;
176 var post_data;
177 var target_id;
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);
189 else
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]);
197 post_data = null;
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]);
207 else {
208 alert("Illegal request type: " + sajax_request_type);
211 x = sajax_init_object();
212 if (x == null) {
213 if (sajax_failure_redirect != "") {
214 location.href = sajax_failure_redirect;
215 return false;
216 } else {
217 sajax_debug("NULL sajax object for user agent:\n" + navigator.userAgent);
218 return false;
220 } else {
221 x.open(sajax_request_type, uri, true);
222 // window.open(uri);
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)
233 return;
235 sajax_debug("received " + x.responseText);
237 var status;
238 var data;
239 var txt = x.responseText.replace(/^\s*|\s*$/g,"");
240 status = txt.charAt(0);
241 data = txt.substring(2);
243 if (status == "") {
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);
247 else {
248 if (target_id != "")
249 document.getElementById(target_id).innerHTML = eval(data);
250 else {
251 try {
252 var callback;
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;
257 } else {
258 callback = args[args.length-1];
260 callback(eval(data), extra_data);
261 } catch (e) {
262 sajax_debug("Caught error " + e + ": Could not eval " + data );
269 sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
270 x.send(post_data);
271 sajax_debug(func_name + " waiting..");
272 delete x;
273 return true;
276 <?php
277 $html = ob_get_contents();
278 ob_end_clean();
279 return $html;
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) {
297 ob_start();
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);
307 <?php
308 $html = ob_get_contents();
309 ob_end_clean();
310 return $html;
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;
332 $html = "";
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);
340 return $html;
343 function sajax_show_javascript()
345 echo sajax_get_javascript();
349 $SAJAX_INCLUDED = 1;
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;
360 $now = time();
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>";
374 return $data;
377 $sajax_request_type = "GET";
378 sajax_init();
379 sajax_export("add_data","get_data");
380 sajax_handle_client_request();