corrade-http-templates – Blame information for rev 9
?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 listens for Corrade's "terse" notification and ## |
||
7 | ## then uses Corrade's "getavatardata" command in order to determine the ## |
||
8 | ## shape gender and to store that data as a CSV of UUIDs by gender. ## |
||
9 | ########################################################################### |
||
10 | |||
11 | # Check if this is the terse notification, otherwise bail. |
||
12 | if(!isset($_POST['type']) || $_POST['type'] != 'avatars') return; |
||
13 | # Check if this is an avatar terse notification, otherwise bail. |
||
14 | if(!isset($_POST['entity']) || $_POST['entity'] != 'Avatar') return; |
||
15 | |||
16 | ########################################################################### |
||
17 | ## CONFIGURATION ## |
||
18 | ########################################################################### |
||
19 | |||
20 | # Set this to the name of the group. |
||
21 | $GROUP = 'My Group'; |
||
22 | # Set this to the group password. |
||
23 | $PASSWORD = 'mypassword'; |
||
24 | # Set this to Corrade's HTTP Server URL. |
||
25 | $URL = 'http://corrade.local.site:8080'; |
||
26 | # Visitors file. |
||
27 | $VISITOR_FILE = 'visitors.log'; |
||
28 | |||
29 | ########################################################################### |
||
30 | ## INTERNALS ## |
||
31 | ########################################################################### |
||
32 | |||
33 | ########################################################################### |
||
34 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
35 | ########################################################################### |
||
36 | function wasKeyValueGet($key, $data) { |
||
37 | return array_reduce( |
||
38 | explode( |
||
39 | "&", |
||
40 | $data |
||
41 | ), |
||
42 | function($o, $p) { |
||
43 | $x = explode("=", $p); |
||
44 | return array_shift($x) != $o ? $o : array_shift($x); |
||
45 | }, |
||
46 | $key |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | ########################################################################### |
||
51 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
52 | ########################################################################### |
||
53 | function atomized_put_contents($file, $data) { |
||
54 | $fp = fopen($file, "w+"); |
||
55 | if (flock($fp, LOCK_EX)) { |
||
56 | fwrite($fp, $data); |
||
57 | fflush($fp); |
||
58 | flock($fp, LOCK_UN); |
||
59 | } |
||
60 | fclose($fp); |
||
61 | } |
||
62 | |||
63 | ########################################################################### |
||
64 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
65 | ########################################################################### |
||
66 | function atomized_get_contents($file) { |
||
67 | $fp = fopen($file, "r+"); |
||
68 | $ct = ''; |
||
69 | if (flock($fp, LOCK_SH)) { |
||
70 | if (filesize($file)) { |
||
71 | $ct = fread($fp, filesize($file)); |
||
72 | } |
||
73 | flock($fp, LOCK_UN); |
||
74 | } |
||
75 | fclose($fp); |
||
76 | return $ct; |
||
77 | } |
||
78 | |||
79 | /////////////////////////////////////////////////////////////////////////// |
||
80 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
81 | /////////////////////////////////////////////////////////////////////////// |
||
82 | function wasCSVToArray($csv) { |
||
83 | $l = array(); |
||
84 | $s = array(); |
||
85 | $m = ""; |
||
86 | for ($i = 0; $i < strlen($csv); ++$i) { |
||
87 | switch ($csv{$i}) { |
||
88 | case ',': |
||
89 | if (sizeof($s) == 0 || !current($s) == '"') { |
||
90 | array_push($l, $m); |
||
91 | $m = ""; |
||
92 | break; |
||
93 | } |
||
94 | $m .= $csv{$i}; |
||
95 | continue; |
||
96 | case '"': |
||
97 | if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { |
||
98 | $m .= $csv{$i}; |
||
99 | ++$i; |
||
100 | break; |
||
101 | } |
||
102 | if (sizeof($s) == 0|| !current($s) == $csv[$i]) { |
||
103 | array_push($s, $csv{$i}); |
||
104 | continue; |
||
105 | } |
||
106 | array_pop($s); |
||
107 | break; |
||
108 | default: |
||
109 | $m .= $csv{$i}; |
||
110 | break; |
||
111 | } |
||
112 | } |
||
113 | array_push($l, $m); |
||
114 | return $l; |
||
115 | } |
||
116 | |||
117 | /////////////////////////////////////////////////////////////////////////// |
||
118 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
119 | /////////////////////////////////////////////////////////////////////////// |
||
120 | function wasArrayToCSV($a) { |
||
121 | return implode( |
||
122 | ',', |
||
123 | array_map( |
||
124 | function($o) { |
||
125 | $o = str_replace('"', '""', $o); |
||
126 | switch( |
||
127 | (strpos($o, ' ') !== FALSE) || |
||
128 | (strpos($o, '"') !== FALSE) || |
||
129 | (strpos($o, ',') !== FALSE) || |
||
130 | (strpos($o, '\r') !== FALSE) || |
||
131 | (strpos($o, '\n') !== FALSE) |
||
132 | ) |
||
133 | { |
||
134 | case TRUE: |
||
135 | return '"' . $o . '"'; |
||
136 | default: |
||
137 | return $o; |
||
138 | } |
||
139 | }, |
||
140 | $a |
||
141 | ) |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | $visitors = array(); |
||
146 | if(file_exists($VISITOR_FILE)) { |
||
147 | $visitors = explode( |
||
148 | PHP_EOL, |
||
149 | atomized_get_contents( |
||
150 | $VISITOR_FILE |
||
151 | ) |
||
152 | ); |
||
153 | array_walk( |
||
154 | $visitors, |
||
155 | function($e, $k) { |
||
156 | if(wasCSVToArray($e)[0] == $_POST['id']) die; |
||
157 | } |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | # This constructs the command as an array of key-value pairs. |
||
162 | $params = array( |
||
163 | 'command' => 'getavatardata', |
||
164 | 'group' => $GROUP, |
||
165 | 'password' => $PASSWORD, |
||
166 | 'agent' => $_POST['id'], |
||
167 | 'data' => 'VisualParameters' |
||
168 | ); |
||
169 | |||
170 | # We now escape each key and value: this is very important, because the |
||
171 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
||
172 | # in this example though but this will not hurt anyone). |
||
173 | array_walk($params, |
||
174 | function(&$value, $key) { |
||
175 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
176 | } |
||
177 | ); |
||
178 | $postvars = implode('&', $params); |
||
179 | |||
180 | # Set the options, send the request and then display the outcome |
||
181 | if (!($curl = curl_init())) { |
||
182 | print 0; |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
187 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
188 | curl_setopt($curl, CURLOPT_POST, true); |
||
189 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
190 | $return = curl_exec($curl); |
||
191 | curl_close($curl); |
||
192 | |||
193 | $success = urldecode( |
||
194 | wasKeyValueGet( |
||
195 | "success", |
||
196 | $return |
||
197 | ) |
||
198 | ); |
||
199 | |||
200 | if($success == 'False') { |
||
201 | # DEBUG: This will be triggered if getting the avatar data fails. |
||
202 | #print $uuid." ".urldecode( |
||
203 | # wasKeyValueGet( |
||
204 | # 'error', |
||
205 | # $return |
||
206 | # ) |
||
207 | #)."\n"; |
||
208 | die; |
||
209 | } |
||
210 | |||
211 | $visual = wasCSVToArray( |
||
212 | urldecode( |
||
213 | wasKeyValueGet( |
||
214 | "data", |
||
215 | $return |
||
216 | ) |
||
217 | ) |
||
218 | ); |
||
219 | |||
220 | $new = array(); |
||
221 | array_push($new, $_POST['id']); |
||
222 | switch($visual[32]) { |
||
223 | case 0: |
||
224 | # DEBUG |
||
225 | #print $uuid.' is female '."\n"; |
||
226 | array_push($new, 'female'); |
||
227 | break; |
||
228 | default: |
||
229 | # DEBUG |
||
230 | #print $uuid.' is male '."\n"; |
||
231 | array_push($new, 'male'); |
||
232 | break; |
||
233 | } |
||
234 | |||
235 | array_push($visitors, wasArrayToCSV($new)); |
||
236 | |||
237 | atomized_put_contents( |
||
238 | $VISITOR_FILE, |
||
239 | implode( |
||
240 | PHP_EOL, |
||
241 | $visitors |
||
242 | ) |
||
243 | ); |
||
244 | |||
245 | |||
246 | ?> |