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