corrade-http-templates – Blame information for rev 82

Subversion Repositories:
Rev:
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');
75 office 15 require_once('vendor/was/utilities/src/formats/kvp/kvp.php');
16 require_once('vendor/was/utilities/src/collections/arrays/arrays.php');
8 zed 17  
18 ###########################################################################
19 ## INTERNALS ##
20 ###########################################################################
21  
22 if(!isset($_POST["query"])) return;
23  
24 ####
25 # I. Get the query from the javascript.
26 $query = $_POST["query"];
27  
28 # This constructs the command as an array of key-value pairs.
29 $params = array(
30 'command' => 'getregiondata',
31 'group' => $GROUP,
32 'data' => $query,
33 'password' => $PASSWORD
34 );
35  
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
38 # in this example though but this will not hurt anyone).
39 array_walk($params,
40 function(&$value, $key) {
82 office 41 $value = urlencode($key)."=".urlencode($value);
8 zed 42 }
43 );
44 $postvars = implode('&', $params);
45  
46 ####
47 # II. Set the options, send the request and then display the outcome.
48 if (!($curl = curl_init())) return;
49  
50 curl_setopt($curl, CURLOPT_URL, $URL);
51 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
52 curl_setopt($curl, CURLOPT_POST, true);
53 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
54 curl_setopt($curl, CURLOPT_ENCODING, true);
55 $result = str_getcsv(
56 urldecode(
57 wasKeyValueGet(
58 "data",
59 curl_exec($curl)
60 )
61 )
62 );
63 curl_close($curl);
64  
65 ####
66 # III. Dump JSON for AJAX.
67 echo json_encode(
68 array_combine(
69 wasArrayStride(
70 $result,
71 2
72 ),
73 wasArrayStride(
74 array_slice(
75 $result,
76 1
77 ),
78 2
79 )
80 )
81 );
82  
83 ?>
84