corrade-http-templates – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | eva | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | ## This is a script that uses Corrade to get the avatar positions and ## |
||
7 | ## then aggregates that data to a JSON object that will be read by AJAX. ## |
||
8 | ## ## |
||
9 | ## The output JSON object that will be read via AJAX is of the form: ## |
||
10 | ## { ## |
||
11 | ## header: ["x", "y", "z"], ## |
||
12 | ## positions: [[x1, y1, z1], [x2, y2, z2], ...], ## |
||
13 | ## names: ["First Resident", "Second Resident", ...], ## |
||
14 | ## colors: [[0.5, 1, 0.75], [0.3, 0.55, 0.34], ...] ## |
||
15 | ## } ## |
||
16 | ########################################################################### |
||
17 | |||
18 | ########################################################################### |
||
19 | ## CONFIGURATION ## |
||
20 | ########################################################################### |
||
21 | |||
22 | // The configuration file for this script containing the settings. |
||
23 | include_once("config.php"); |
||
24 | |||
25 | ########################################################################### |
||
26 | ## INTERNALS ## |
||
27 | ########################################################################### |
||
28 | |||
29 | ########################################################################### |
||
30 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
31 | ########################################################################### |
||
32 | function wasKeyValueGet($key, $data) { |
||
33 | return array_reduce( |
||
34 | explode( |
||
35 | "&", |
||
36 | $data |
||
37 | ), |
||
38 | function($o, $p) { |
||
39 | $x = explode("=", $p); |
||
40 | return array_shift($x) != $o ? $o : array_shift($x); |
||
41 | }, |
||
42 | $key |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | /////////////////////////////////////////////////////////////////////////// |
||
47 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
48 | /////////////////////////////////////////////////////////////////////////// |
||
49 | function wasCSVToArray($csv) { |
||
50 | $l = array(); |
||
51 | $s = array(); |
||
52 | $m = ""; |
||
53 | for ($i = 0; $i < strlen($csv); ++$i) { |
||
54 | switch ($csv{$i}) { |
||
55 | case ',': |
||
56 | if (sizeof($s) == 0 || !current($s) == '"') { |
||
57 | array_push($l, $m); |
||
58 | $m = ""; |
||
59 | break; |
||
60 | } |
||
61 | $m .= $csv{$i}; |
||
62 | continue; |
||
63 | case '"': |
||
64 | if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { |
||
65 | $m .= $csv{$i}; |
||
66 | ++$i; |
||
67 | break; |
||
68 | } |
||
69 | if (sizeof($s) == 0|| !current($s) == $csv[$i]) { |
||
70 | array_push($s, $csv{$i}); |
||
71 | continue; |
||
72 | } |
||
73 | array_pop($s); |
||
74 | break; |
||
75 | default: |
||
76 | $m .= $csv{$i}; |
||
77 | break; |
||
78 | } |
||
79 | } |
||
80 | array_push($l, $m); |
||
81 | return $l; |
||
82 | } |
||
83 | |||
84 | /////////////////////////////////////////////////////////////////////////// |
||
85 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
86 | /////////////////////////////////////////////////////////////////////////// |
||
87 | function wasArrayStride($a, $s) { |
||
88 | return array_filter($a, |
||
89 | function($e, $i) use($s) { |
||
90 | return $i % $s == 0; |
||
91 | }, |
||
92 | ARRAY_FILTER_USE_BOTH |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /////////////////////////////////////////////////////////////////////////// |
||
97 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
98 | /////////////////////////////////////////////////////////////////////////// |
||
99 | function wasColorHash($a) { |
||
100 | $hash = sha1($a); |
||
101 | $size = strlen($hash); |
||
102 | |||
103 | return substr($hash, 0, 2). |
||
104 | substr($hash, $size/2, 2). |
||
105 | substr($hash, $size-2, 2); |
||
106 | } |
||
107 | |||
108 | /////////////////////////////////////////////////////////////////////////// |
||
109 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
110 | /////////////////////////////////////////////////////////////////////////// |
||
111 | function mapValueToRange($value, $xMin, $xMax, $yMin, $yMax) { |
||
112 | return $yMin + ( |
||
113 | ( |
||
114 | $yMax - $yMin |
||
115 | ) |
||
116 | * |
||
117 | ( |
||
118 | $value - $xMin |
||
119 | ) |
||
120 | / |
||
121 | ( |
||
122 | $xMax - $xMin |
||
123 | ) |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') { |
||
128 | $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); |
||
129 | $rgbArray = array(); |
||
130 | switch(strlen($hexStr)) { |
||
131 | case 6: |
||
132 | $colorVal = hexdec($hexStr); |
||
133 | $rgbArray['red'] = 0xFF & ($colorVal >> 0x10); |
||
134 | $rgbArray['green'] = 0xFF & ($colorVal >> 0x8); |
||
135 | $rgbArray['blue'] = 0xFF & $colorVal; |
||
136 | break; |
||
137 | case 3: |
||
138 | $rgbArray['red'] = hexdec( |
||
139 | str_repeat(substr($hexStr, 0, 1), 2)); |
||
140 | $rgbArray['green'] = hexdec( |
||
141 | str_repeat(substr($hexStr, 1, 1), 2)); |
||
142 | $rgbArray['blue'] = hexdec( |
||
143 | str_repeat(substr($hexStr, 2, 1), 2)); |
||
144 | break; |
||
145 | default: |
||
146 | return false; |
||
147 | } |
||
148 | return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; |
||
149 | } |
||
150 | |||
151 | #### |
||
152 | # I. Get the avatar positions. |
||
153 | $params = array( |
||
154 | 'command' => 'getavatarpositions', |
||
155 | 'group' => $GROUP, |
||
156 | 'entity' => 'region', |
||
157 | 'password' => $PASSWORD |
||
158 | ); |
||
159 | |||
160 | # We now escape each key and value: this is very important, because the |
||
161 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
||
162 | # in this example though but this will not hurt anyone). |
||
163 | array_walk($params, |
||
164 | function(&$value, $key) { |
||
165 | $value = urlencode($key)."=".urlencode($value); |
||
166 | } |
||
167 | ); |
||
168 | $postvars = implode('&', $params); |
||
169 | |||
170 | # Set the options, send the request and then display the outcome |
||
171 | if (!($curl = curl_init())) { |
||
172 | print 0; |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
177 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
178 | curl_setopt($curl, CURLOPT_POST, true); |
||
179 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
180 | $result = curl_exec($curl); |
||
181 | curl_close($curl); |
||
182 | |||
183 | $success = urldecode( |
||
184 | wasKeyValueGet( |
||
185 | "success", |
||
186 | $result |
||
187 | ) |
||
188 | ); |
||
189 | |||
190 | // Unable to get avatar positions? |
||
191 | if($success == 'False') return -1; |
||
192 | |||
193 | $data = wasCSVToArray( |
||
194 | urldecode( |
||
195 | wasKeyValueGet( |
||
196 | "data", |
||
197 | $result |
||
198 | ) |
||
199 | ) |
||
200 | ); |
||
201 | |||
202 | #### |
||
203 | # II. Construct and echo the JSON object. |
||
204 | echo json_encode( |
||
205 | array( |
||
206 | 'header' => array('x', 'y', 'z'), |
||
207 | 'positions' => array_values( |
||
208 | array_map( |
||
209 | function($e) { |
||
210 | $e = preg_replace('/</', '', $e); |
||
211 | $e = preg_replace('/>/', '', $e); |
||
212 | $e = explode(',', $e); |
||
213 | return array($e[0], $e[1], $e[2]); |
||
214 | }, |
||
215 | wasArrayStride( |
||
216 | array_slice($data, 2), |
||
217 | 3 |
||
218 | ) |
||
219 | ) |
||
220 | ), |
||
221 | 'names' => array_values( |
||
222 | wasArrayStride( |
||
223 | $data, |
||
224 | 3 |
||
225 | ) |
||
226 | ), |
||
227 | 'colors' => array_values( |
||
228 | array_map( |
||
229 | function($e) { |
||
230 | $e = hex2RGB(wasColorHash($e)); |
||
231 | return array( |
||
232 | round ( |
||
233 | mapValueToRange($e['red'], 0, 255, 0, 1), 2 |
||
234 | ), |
||
235 | round( |
||
236 | mapValueToRange($e['green'], 0, 255, 0, 1), 2 |
||
237 | ), |
||
238 | round( |
||
239 | mapValueToRange($e['blue'], 0, 255, 0, 1), 2 |
||
240 | ) |
||
241 | ); |
||
242 | }, |
||
243 | wasArrayStride( |
||
244 | $data, |
||
245 | 3 |
||
246 | ) |
||
247 | ) |
||
248 | ) |
||
249 | ), |
||
250 | JSON_NUMERIC_CHECK |
||
251 | ); |
||
252 | |||
253 | return 0; |
||
254 | |||
255 | ?> |
||
256 |