corrade-http-templates – Diff between revs 2 and 12

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