corrade-http-templates – Blame information for rev 2

Subversion Repositories:
Rev:
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's HTTP server to get the UUID of ##
7 ## the current region map, then downloads the map as a PNG which then ##
8 ## gets encoded to a Base64 string and echoed from this script. ##
9 ###########################################################################
10  
11 ###########################################################################
12 ## CONFIGURATION ##
13 ###########################################################################
14  
15 // The configuration file for this script containing the settings.
16 include_once("config.php");
17  
18 ###########################################################################
19 ## INTERNALS ##
20 ###########################################################################
21  
22 ###########################################################################
23 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
24 ###########################################################################
25 function wasKeyValueGet($key, $data) {
26 return array_reduce(
27 explode(
28 "&",
29 $data
30 ),
31 function($o, $p) {
32 $x = explode("=", $p);
33 return array_shift($x) != $o ? $o : array_shift($x);
34 },
35 $key
36 );
37 }
38 ///////////////////////////////////////////////////////////////////////////
39 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
40 ///////////////////////////////////////////////////////////////////////////
41 function wasCSVToArray($csv) {
42 $l = array();
43 $s = array();
44 $m = "";
45 for ($i = 0; $i < strlen($csv); ++$i) {
46 switch ($csv{$i}) {
47 case ',':
48 if (sizeof($s) == 0 || !current($s) == '"') {
49 array_push($l, $m);
50 $m = "";
51 break;
52 }
53 $m .= $csv{$i};
54 continue;
55 case '"':
56 if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) {
57 $m .= $csv{$i};
58 ++$i;
59 break;
60 }
61 if (sizeof($s) == 0|| !current($s) == $csv[$i]) {
62 array_push($s, $csv{$i});
63 continue;
64 }
65 array_pop($s);
66 break;
67 default:
68 $m .= $csv{$i};
69 break;
70 }
71 }
72 array_push($l, $m);
73 return $l;
74 }
75  
76 ####
77 # I. Get the UUID of the map image for the current region
78 $params = array(
79 'command' => 'getgridregiondata',
80 'group' => $GROUP,
81 'password' => $PASSWORD,
82 'data' => 'MapImageID'
83 );
84 array_walk($params,
85 function(&$value, $key) {
86 $value = urlencode($key)."=".urlencode($value);
87 }
88 );
89 $postvars = implode('&', $params);
90 if (!($curl = curl_init())) {
91 print 0;
92 return;
93 }
94  
95 curl_setopt($curl, CURLOPT_URL, $URL);
96 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
97 curl_setopt($curl, CURLOPT_POST, true);
98 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
99 $return = curl_exec($curl);
100 curl_close($curl);
101  
102 $success = urldecode(
103 wasKeyValueGet(
104 "success",
105 $return
106 )
107 );
108  
109 // Unable to retrieve the map image?
110 if($success == 'False') return -1;
111  
112 $mapUUID = wasCSVToArray(
113 urldecode(
114 wasKeyValueGet(
115 "data",
116 $return
117 )
118 )
119 )[1];
120  
121 ####
122 # II. Download the map image as a PNG file.
123 $params = array(
124 'command' => 'download',
125 'group' => $GROUP,
126 'password' => $PASSWORD,
127 'item' => $mapUUID,
128 'type' => 'Texture',
129 'format' => 'Png'
130 );
131 array_walk($params,
132 function(&$value, $key) {
133 $value = rawurlencode($key)."=".rawurlencode($value);
134 }
135 );
136 $postvars = implode('&', $params);
137 if (!($curl = curl_init())) {
138 print 0;
139 return;
140 }
141 curl_setopt($curl, CURLOPT_URL, $URL);
142 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
143 curl_setopt($curl, CURLOPT_POST, true);
144 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
145 $return = curl_exec($curl);
146 curl_close($curl);
147  
148 $success = urldecode(
149 wasKeyValueGet(
150 "success",
151 $return
152 )
153 );
154  
155 // Unable to download the region map texture?
156 if($success == 'False') return -1;
157  
158 ####
159 # III. Convert the image data to a PNG of size 512x512
160 $im = imagescale(
161 imagecreatefromstring(
162 base64_decode(
163 rawurldecode(
164 wasKeyValueGet(
165 "data",
166 $return
167 )
168 )
169 )
170 ),
171 512,
172 512
173 );
174  
175 ####
176 # VI. Output the Base64 encoded image for AJAX.
177 ob_start();
178 imagepng($im);
179 $png = ob_get_contents();
180 imagedestroy($im);
181 ob_end_clean();
182  
183 echo base64_encode($png);
184  
185 ?>
186