corrade-http-templates – Diff between revs 2 and 7
?pathlinks?
Rev 2 | Rev 7 | |||
---|---|---|---|---|
1 | <?php |
1 | <?php |
|
2 | |
2 | |
|
3 | ########################################################################### |
3 | ########################################################################### |
|
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
|
5 | ########################################################################### |
5 | ########################################################################### |
|
6 | ## This is a script that uses Corrade's: ## |
6 | ## This is a script that uses Corrade's: ## |
|
7 | ## * getregiondata ## |
7 | ## * getregiondata ## |
|
8 | ## * getavatarpositions ## |
8 | ## * getavatarpositions ## |
|
9 | ## * download ## |
9 | ## * download ## |
|
10 | ## commands in order to display the positions of avatars on the region. ## |
10 | ## commands in order to display the positions of avatars on the region. ## |
|
11 | ########################################################################### |
11 | ########################################################################### |
|
12 | |
12 | |
|
13 | ########################################################################### |
13 | ########################################################################### |
|
14 | ## CONFIGURATION ## |
14 | ## CONFIGURATION ## |
|
15 | ########################################################################### |
15 | ########################################################################### |
|
16 | |
- | ||
17 | # Set this to the name of the group. |
- | ||
18 | $GROUP = 'My Group'; |
16 | |
|
19 | # Set this to the group password. |
17 | require_once('config.php'); |
|
20 | $PASSWORD = 'mypassword'; |
- | ||
21 | # Set this to Corrade's HTTP Server URL. |
- | ||
22 | $URL = 'http://corrade.internal.site:8080'; |
18 | require_once('functions.php'); |
|
23 | |
19 | |
|
24 | ########################################################################### |
20 | ########################################################################### |
|
25 | ## INTERNALS ## |
21 | ## INTERNALS ## |
|
26 | ########################################################################### |
22 | ########################################################################### |
|
27 | |
- | ||
28 | ########################################################################### |
- | ||
29 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
- | ||
30 | ########################################################################### |
- | ||
31 | function wasKeyValueGet($key, $data) { |
- | ||
32 | return array_reduce( |
- | ||
33 | explode( |
- | ||
34 | "&", |
- | ||
35 | $data |
- | ||
36 | ), |
- | ||
37 | function($o, $p) { |
- | ||
38 | $x = explode("=", $p); |
- | ||
39 | return array_shift($x) != $o ? $o : array_shift($x); |
- | ||
40 | }, |
- | ||
41 | $key |
- | ||
42 | ); |
- | ||
43 | } |
- | ||
44 | /////////////////////////////////////////////////////////////////////////// |
- | ||
45 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
- | ||
46 | /////////////////////////////////////////////////////////////////////////// |
- | ||
47 | function wasCSVToArray($csv) { |
- | ||
48 | $l = array(); |
- | ||
49 | $s = array(); |
- | ||
50 | $m = ""; |
- | ||
51 | for ($i = 0; $i < strlen($csv); ++$i) { |
- | ||
52 | switch ($csv{$i}) { |
- | ||
53 | case ',': |
- | ||
54 | if (sizeof($s) == 0 || !current($s) == '"') { |
- | ||
55 | array_push($l, $m); |
- | ||
56 | $m = ""; |
- | ||
57 | break; |
- | ||
58 | } |
- | ||
59 | $m .= $csv{$i}; |
- | ||
60 | continue; |
- | ||
61 | case '"': |
- | ||
62 | if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { |
- | ||
63 | $m .= $csv{$i}; |
- | ||
64 | ++$i; |
- | ||
65 | break; |
- | ||
66 | } |
- | ||
67 | if (sizeof($s) == 0|| !current($s) == $csv[$i]) { |
- | ||
68 | array_push($s, $csv{$i}); |
- | ||
69 | continue; |
- | ||
70 | } |
- | ||
71 | array_pop($s); |
- | ||
72 | break; |
- | ||
73 | default: |
- | ||
74 | $m .= $csv{$i}; |
- | ||
75 | break; |
- | ||
76 | } |
- | ||
77 | } |
- | ||
78 | array_push($l, $m); |
- | ||
79 | return $l; |
- | ||
80 | } |
- | ||
81 | |
- | ||
82 | ########################################################################## |
- | ||
83 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
- | ||
84 | ########################################################################### |
- | ||
85 | function mapValueToRange($value, $xMin, $xMax, $yMin, $yMax) { |
- | ||
86 | return $yMin + ( |
- | ||
87 | ( |
- | ||
88 | $yMax - $yMin |
- | ||
89 | ) |
- | ||
90 | * |
- | ||
91 | ( |
- | ||
92 | $value - $xMin |
- | ||
93 | ) |
- | ||
94 | / |
- | ||
95 | ( |
- | ||
96 | $xMax - $xMin |
- | ||
97 | ) |
- | ||
98 | ); |
- | ||
99 | } |
- | ||
100 | |
- | ||
101 | ########################################################################### |
- | ||
102 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
- | ||
103 | ########################################################################### |
- | ||
104 | function wasLSLVectorToArray($vector) { |
- | ||
105 | $components = array(); |
- | ||
106 | if(!preg_match( |
- | ||
107 | "/^<\s*([0-9\.]+?)\s*,\s*([0-9\.]+?)\s*,\s*([0-9\.]+?)\s*>$/", |
- | ||
108 | $vector, |
- | ||
109 | $components |
- | ||
110 | )) return; |
- | ||
111 | array_shift($components); |
- | ||
112 | return $components; |
- | ||
113 | } |
- | ||
114 | |
23 | |
|
115 | #### |
24 | #### |
|
116 | # I. Get the UUID of the map image for the current region |
25 | # I. Get the UUID of the map image for the current region |
|
117 | $params = array( |
26 | $params = array( |
|
118 | 'command' => 'getgridregiondata', |
27 | 'command' => 'getgridregiondata', |
|
119 | 'group' => $GROUP, |
28 | 'group' => $GROUP, |
|
120 | 'password' => $PASSWORD, |
29 | 'password' => $PASSWORD, |
|
121 | 'data' => 'MapImageID' |
30 | 'data' => 'MapImageID' |
|
122 | ); |
31 | ); |
|
123 | array_walk($params, |
32 | array_walk($params, |
|
124 | function(&$value, $key) { |
33 | function(&$value, $key) { |
|
125 | $value = rawurlencode($key)."=".rawurlencode($value); |
34 | $value = rawurlencode($key)."=".rawurlencode($value); |
|
126 | } |
35 | } |
|
127 | ); |
36 | ); |
|
128 | $postvars = implode('&', $params); |
37 | $postvars = implode('&', $params); |
|
129 | if (!($curl = curl_init())) { |
38 | if (!($curl = curl_init())) { |
|
130 | print 0; |
39 | print 0; |
|
131 | return; |
40 | return; |
|
132 | } |
41 | } |
|
133 | |
42 | |
|
134 | curl_setopt($curl, CURLOPT_URL, $URL); |
43 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
135 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
44 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
136 | curl_setopt($curl, CURLOPT_POST, true); |
45 | curl_setopt($curl, CURLOPT_POST, true); |
|
137 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
46 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
- | 47 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
138 | $return = curl_exec($curl); |
48 | $return = curl_exec($curl); |
|
139 | curl_close($curl); |
49 | curl_close($curl); |
|
140 | |
50 | |
|
141 | $success = urldecode( |
51 | $success = urldecode( |
|
142 | wasKeyValueGet( |
52 | wasKeyValueGet( |
|
143 | "success", |
53 | "success", |
|
144 | $return |
54 | $return |
|
145 | ) |
55 | ) |
|
146 | ); |
56 | ); |
|
147 | |
57 | |
|
148 | if($success == 'False') { |
58 | if($success == 'False') { |
|
149 | echo 'Unable to get the region map image.'; |
59 | echo 'Unable to get the region map image.'; |
|
150 | die; |
60 | die; |
|
151 | } |
61 | } |
|
152 | |
62 | |
|
153 | $mapUUID = wasCSVToArray( |
63 | $mapUUID = wasCSVToArray( |
|
154 | urldecode( |
64 | urldecode( |
|
155 | wasKeyValueGet( |
65 | wasKeyValueGet( |
|
156 | "data", |
66 | "data", |
|
157 | $return |
67 | $return |
|
158 | ) |
68 | ) |
|
159 | ) |
69 | ) |
|
160 | )[1]; |
70 | )[1]; |
|
161 | |
71 | |
|
162 | #### |
72 | #### |
|
163 | # II. Download the map image as a PNG file. |
73 | # II. Download the map image as a PNG file. |
|
164 | $params = array( |
74 | $params = array( |
|
165 | 'command' => 'download', |
75 | 'command' => 'download', |
|
166 | 'group' => $GROUP, |
76 | 'group' => $GROUP, |
|
167 | 'password' => $PASSWORD, |
77 | 'password' => $PASSWORD, |
|
168 | 'item' => $mapUUID, |
78 | 'item' => $mapUUID, |
|
169 | 'type' => 'Texture', |
79 | 'type' => 'Texture', |
|
170 | 'format' => 'Png' |
80 | 'format' => 'Png' |
|
171 | ); |
81 | ); |
|
172 | array_walk($params, |
82 | array_walk($params, |
|
173 | function(&$value, $key) { |
83 | function(&$value, $key) { |
|
174 | $value = rawurlencode($key)."=".rawurlencode($value); |
84 | $value = rawurlencode($key)."=".rawurlencode($value); |
|
175 | } |
85 | } |
|
176 | ); |
86 | ); |
|
177 | $postvars = implode('&', $params); |
87 | $postvars = implode('&', $params); |
|
178 | if (!($curl = curl_init())) { |
88 | if (!($curl = curl_init())) { |
|
179 | print 0; |
89 | print 0; |
|
180 | return; |
90 | return; |
|
181 | } |
91 | } |
|
182 | curl_setopt($curl, CURLOPT_URL, $URL); |
92 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
183 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
93 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
184 | curl_setopt($curl, CURLOPT_POST, true); |
94 | curl_setopt($curl, CURLOPT_POST, true); |
|
185 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
95 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
- | 96 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
186 | $return = curl_exec($curl); |
97 | $return = curl_exec($curl); |
|
187 | curl_close($curl); |
98 | curl_close($curl); |
|
188 | |
99 | |
|
189 | if($success == 'False') { |
100 | if($success == 'False') { |
|
190 | echo 'Unable to download the region map texture.'; |
101 | echo 'Unable to download the region map texture.'; |
|
191 | die; |
102 | die; |
|
192 | } |
103 | } |
|
193 | |
104 | |
|
194 | |
105 | |
|
195 | #### |
106 | #### |
|
196 | # III. Convert the image data to a PNG of size 512x512 |
107 | # III. Convert the image data to a PNG of size 512x512 |
|
197 | $im = imagescale( |
108 | $im = imagescale( |
|
198 | imagecreatefromstring( |
109 | imagecreatefromstring( |
|
199 | base64_decode( |
110 | base64_decode( |
|
200 | urldecode( |
111 | urldecode( |
|
201 | wasKeyValueGet( |
112 | wasKeyValueGet( |
|
202 | "data", |
113 | "data", |
|
203 | $return |
114 | $return |
|
204 | ) |
115 | ) |
|
205 | ) |
116 | ) |
|
206 | ) |
117 | ) |
|
207 | ), |
118 | ), |
|
208 | 512, |
119 | 512, |
|
209 | 512 |
120 | 512 |
|
210 | ); |
121 | ); |
|
211 | |
122 | |
|
212 | #### |
123 | #### |
|
213 | # IV. Get the avatar positions on the map. |
124 | # IV. Get the avatar positions on the map. |
|
214 | $params = array( |
125 | $params = array( |
|
215 | 'command' => 'getavatarpositions', |
126 | 'command' => 'getavatarpositions', |
|
216 | 'group' => $GROUP, |
127 | 'group' => $GROUP, |
|
217 | 'password' => $PASSWORD, |
128 | 'password' => $PASSWORD, |
|
218 | 'entity' => 'region' |
129 | 'entity' => 'region' |
|
219 | ); |
130 | ); |
|
220 | array_walk($params, |
131 | array_walk($params, |
|
221 | function(&$value, $key) { |
132 | function(&$value, $key) { |
|
222 | $value = rawurlencode($key)."=".rawurlencode($value); |
133 | $value = rawurlencode($key)."=".rawurlencode($value); |
|
223 | } |
134 | } |
|
224 | ); |
135 | ); |
|
225 | $postvars = implode('&', $params); |
136 | $postvars = implode('&', $params); |
|
226 | if (!($curl = curl_init())) { |
137 | if (!($curl = curl_init())) { |
|
227 | print 0; |
138 | print 0; |
|
228 | return; |
139 | return; |
|
229 | } |
140 | } |
|
230 | curl_setopt($curl, CURLOPT_URL, $URL); |
141 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
231 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
142 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
232 | curl_setopt($curl, CURLOPT_POST, true); |
143 | curl_setopt($curl, CURLOPT_POST, true); |
|
233 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
144 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
- | 145 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
234 | $return = curl_exec($curl); |
146 | $return = curl_exec($curl); |
|
235 | curl_close($curl); |
147 | curl_close($curl); |
|
236 | |
148 | |
|
237 | $success = urldecode( |
149 | $success = urldecode( |
|
238 | wasKeyValueGet( |
150 | wasKeyValueGet( |
|
239 | "success", |
151 | "success", |
|
240 | $return |
152 | $return |
|
241 | ) |
153 | ) |
|
242 | ); |
154 | ); |
|
243 | |
155 | |
|
244 | if($success == 'False') { |
156 | if($success == 'False') { |
|
245 | echo 'Unable to get the avatars positions.'; |
157 | echo 'Unable to get the avatars positions.'; |
|
246 | die; |
158 | die; |
|
247 | } |
159 | } |
|
248 | |
160 | |
|
249 | |
161 | |
|
250 | #### |
162 | #### |
|
251 | # V. Display the coordinates on the map. |
163 | # V. Display the coordinates on the map. |
|
252 | array_walk( |
164 | array_walk( |
|
253 | array_chunk( |
165 | array_chunk( |
|
254 | wasCSVToArray( |
166 | wasCSVToArray( |
|
255 | urldecode( |
167 | urldecode( |
|
256 | wasKeyValueGet( |
168 | wasKeyValueGet( |
|
257 | "data", |
169 | "data", |
|
258 | $return |
170 | $return |
|
259 | ) |
171 | ) |
|
260 | ) |
172 | ) |
|
261 | ), |
173 | ), |
|
262 | 3 |
174 | 3 |
|
263 | ), |
175 | ), |
|
264 | function(&$value, $key) use(&$im) { |
176 | function(&$value, $key) use(&$im) { |
|
265 | if(count($value) != 3) return; |
177 | if(count($value) != 3) return; |
|
266 | $components = wasLSLVectorToArray($value[2]); |
178 | $components = wasLSLVectorToArray($value[2]); |
|
267 | if(count($components) != 3) return; |
179 | if(count($components) != 3) return; |
|
268 | $x = mapValueToRange($components[0], 0, 255, 0, 512); |
180 | $x = mapValueToRange($components[0], 0, 255, 0, 512); |
|
269 | $y = 512 - mapValueToRange($components[1], 0, 255, 0, 512); |
181 | $y = 512 - mapValueToRange($components[1], 0, 255, 0, 512); |
|
270 | imagefilledellipse( |
182 | imagefilledellipse( |
|
271 | $im, |
183 | $im, |
|
272 | $x, |
184 | $x, |
|
273 | $y, |
185 | $y, |
|
274 | 8, |
186 | 8, |
|
275 | 8, |
187 | 8, |
|
276 | imagecolorallocate( |
188 | imagecolorallocate( |
|
277 | $im, |
189 | $im, |
|
278 | 0, |
190 | 0, |
|
279 | 0, |
191 | 0, |
|
280 | 0 |
192 | 0 |
|
281 | ) |
193 | ) |
|
282 | ); |
194 | ); |
|
283 | imagefilledellipse( |
195 | imagefilledellipse( |
|
284 | $im, |
196 | $im, |
|
285 | $x, |
197 | $x, |
|
286 | $y, |
198 | $y, |
|
287 | 6, |
199 | 6, |
|
288 | 6, |
200 | 6, |
|
289 | imagecolorallocate( |
201 | imagecolorallocate( |
|
290 | $im, |
202 | $im, |
|
291 | 0, |
203 | 0, |
|
292 | 255, |
204 | 255, |
|
293 | 0 |
205 | 0 |
|
294 | ) |
206 | ) |
|
295 | ); |
207 | ); |
|
296 | } |
208 | } |
|
297 | ); |
209 | ); |
|
298 | |
210 | |
|
299 | #### |
211 | #### |
|
300 | # VI. Output the Base64 encoded image for AJAX. |
212 | # VI. Output the Base64 encoded image for AJAX. |
|
301 | ob_start(); |
213 | ob_start(); |
|
302 | imagepng($im); |
214 | imagepng($im); |
|
303 | $png = ob_get_contents(); |
215 | $png = ob_get_contents(); |
|
304 | imagedestroy($im); |
216 | imagedestroy($im); |
|
305 | ob_end_clean(); |
217 | ob_end_clean(); |
|
306 | |
218 | |
|
307 | echo base64_encode($png); |
219 | echo base64_encode($png); |
|
308 | |
220 | |
|
309 | ?> |
221 | ?> |
|
310 | |
222 | |
|
311 | |
223 | |