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