corrade-http-templates – Blame information for rev 8
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
8 | zed | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | ## This is a script that uses Corrade's "getregiondata" command in order ## |
||
7 | ## to retrieve statistics on the current simulator. ## |
||
8 | ########################################################################### |
||
9 | |||
10 | ########################################################################### |
||
11 | ## CONFIGURATION ## |
||
12 | ########################################################################### |
||
13 | |||
14 | require_once('config.php'); |
||
15 | require_once('functions.php'); |
||
16 | |||
17 | ########################################################################### |
||
18 | ## INTERNALS ## |
||
19 | ########################################################################### |
||
20 | |||
21 | if(!isset($_POST["query"])) return; |
||
22 | |||
23 | #### |
||
24 | # I. Get the query from the javascript. |
||
25 | $query = $_POST["query"]; |
||
26 | |||
27 | # This constructs the command as an array of key-value pairs. |
||
28 | $params = array( |
||
29 | 'command' => 'getregiondata', |
||
30 | 'group' => $GROUP, |
||
31 | 'data' => $query, |
||
32 | 'password' => $PASSWORD |
||
33 | ); |
||
34 | |||
35 | # We now escape each key and value: this is very important, because the |
||
36 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
||
37 | # in this example though but this will not hurt anyone). |
||
38 | array_walk($params, |
||
39 | function(&$value, $key) { |
||
40 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
41 | } |
||
42 | ); |
||
43 | $postvars = implode('&', $params); |
||
44 | |||
45 | #### |
||
46 | # II. Set the options, send the request and then display the outcome. |
||
47 | if (!($curl = curl_init())) return; |
||
48 | |||
49 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
50 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
51 | curl_setopt($curl, CURLOPT_POST, true); |
||
52 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
53 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
54 | $result = str_getcsv( |
||
55 | urldecode( |
||
56 | wasKeyValueGet( |
||
57 | "data", |
||
58 | curl_exec($curl) |
||
59 | ) |
||
60 | ) |
||
61 | ); |
||
62 | curl_close($curl); |
||
63 | |||
64 | #### |
||
65 | # III. Dump JSON for AJAX. |
||
66 | echo json_encode( |
||
67 | array_combine( |
||
68 | wasArrayStride( |
||
69 | $result, |
||
70 | 2 |
||
71 | ), |
||
72 | wasArrayStride( |
||
73 | array_slice( |
||
74 | $result, |
||
75 | 1 |
||
76 | ), |
||
77 | 2 |
||
78 | ) |
||
79 | ) |
||
80 | ); |
||
81 | |||
82 | ?> |
||
83 |