corrade-http-templates – Blame information for rev 2
?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 sends a group message using Corrade. ## |
||
7 | ########################################################################### |
||
8 | |||
9 | ########################################################################### |
||
10 | ## CONFIGURATION ## |
||
11 | ########################################################################### |
||
12 | |||
13 | # Set this to the name of the group. |
||
14 | $GROUP = 'My Group'; |
||
15 | # Set this to the group password. |
||
16 | $PASSWORD = 'mypassword'; |
||
17 | # Set this to Corrade's HTTP Server URL. |
||
18 | $URL = 'http://corrade.internal.sever:8080'; |
||
19 | |||
20 | ########################################################################### |
||
21 | ## INTERNALS ## |
||
22 | ########################################################################### |
||
23 | |||
24 | # If no avatars have been sent, then bail. |
||
25 | if(!isset($_POST['avatars'])) return; |
||
26 | |||
27 | ########################################################################### |
||
28 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
29 | ########################################################################### |
||
30 | function wasKeyValueGet($key, $data) { |
||
31 | return array_reduce( |
||
32 | explode( |
||
33 | "&", |
||
34 | $data |
||
35 | ), |
||
36 | function($o, $p) { |
||
37 | $x = explode("=", $p); |
||
38 | return array_shift($x) != $o ? $o : array_shift($x); |
||
39 | }, |
||
40 | $key |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | ########################################################################### |
||
45 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
46 | ########################################################################### |
||
47 | function wasArrayToCSV($a) { |
||
48 | return implode( |
||
49 | ',', |
||
50 | array_map( |
||
51 | function($o) { |
||
52 | $o = str_replace('"', '""', $o); |
||
53 | switch( |
||
54 | (strpos($o, ' ') !== FALSE) || |
||
55 | (strpos($o, '"') !== FALSE) || |
||
56 | (strpos($o, ',') !== FALSE) || |
||
57 | (strpos($o, '\r') !== FALSE) || |
||
58 | (strpos($o, '\n') !== FALSE) |
||
59 | ) |
||
60 | { |
||
61 | case TRUE: |
||
62 | return '"' . $o . '"'; |
||
63 | default: |
||
64 | return $o; |
||
65 | } |
||
66 | }, |
||
67 | $a |
||
68 | ) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | ########################################################################### |
||
73 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
74 | ########################################################################### |
||
75 | function wasCSVToArray($csv) { |
||
76 | $l = array(); |
||
77 | $s = array(); |
||
78 | $m = ""; |
||
79 | for ($i = 0; $i < strlen($csv); ++$i) { |
||
80 | switch ($csv{$i}) { |
||
81 | case ',': |
||
82 | if (sizeof($s) == 0 || !current($s) == '"') { |
||
83 | array_push($l, $m); |
||
84 | $m = ""; |
||
85 | break; |
||
86 | } |
||
87 | $m .= $csv{$i}; |
||
88 | continue; |
||
89 | case '"': |
||
90 | if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { |
||
91 | $m .= $csv{$i}; |
||
92 | ++$i; |
||
93 | break; |
||
94 | } |
||
95 | if (sizeof($s) == 0|| !current($s) == $csv[$i]) { |
||
96 | array_push($s, $csv{$i}); |
||
97 | continue; |
||
98 | } |
||
99 | array_pop($s); |
||
100 | break; |
||
101 | default: |
||
102 | $m .= $csv{$i}; |
||
103 | break; |
||
104 | } |
||
105 | } |
||
106 | array_push($l, $m); |
||
107 | return $l; |
||
108 | } |
||
109 | |||
110 | #### |
||
111 | # I. Build the list of invites to send to Corrade. |
||
112 | $invites = array(); |
||
113 | foreach( |
||
114 | array_filter(explode("\n", $_POST['avatars']), 'trim') as $avatar) { |
||
115 | $nameUUID = preg_split("/[\s]+/", $avatar); |
||
116 | switch(count($nameUUID)) { |
||
117 | case 2: |
||
118 | case 1: |
||
119 | array_push($invites, $avatar); |
||
120 | break; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | # Do not bother with an empty list of invites |
||
125 | if(count($invites) == 0) { |
||
126 | echo implode(PHP_EOL, $invites); |
||
127 | return -1; |
||
128 | } |
||
129 | |||
130 | #### |
||
131 | # II. Build the command by name. |
||
132 | $params = array( |
||
133 | 'command' => 'batchinvite', |
||
134 | 'group' => $GROUP, |
||
135 | 'password' => $PASSWORD, |
||
136 | 'avatars' => wasArrayToCSV($invites) |
||
137 | ); |
||
138 | |||
139 | #### |
||
140 | # III. Escape the data to be sent to Corrade. |
||
141 | array_walk($params, |
||
142 | function(&$value, $key) { |
||
143 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
144 | } |
||
145 | ); |
||
146 | $postvars = implode('&', $params); |
||
147 | |||
148 | #### |
||
149 | # IV. Use curl to send the message. |
||
150 | if (!($curl = curl_init())) { |
||
151 | print 0; |
||
152 | return; |
||
153 | } |
||
154 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
155 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
156 | curl_setopt($curl, CURLOPT_POST, true); |
||
157 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
158 | $result = curl_exec($curl); |
||
159 | curl_close($curl); |
||
160 | |||
161 | #### |
||
162 | # V. Grab the status of the command. |
||
163 | $status = urldecode( |
||
164 | wasKeyValueGet( |
||
165 | "success", |
||
166 | $result |
||
167 | ) |
||
168 | ); |
||
169 | |||
170 | |||
171 | #### |
||
172 | # VI. Check the status of the command. |
||
173 | switch($status) { |
||
174 | case "False": |
||
175 | return -1; |
||
176 | } |
||
177 | |||
178 | #### |
||
179 | # V. Return any avatars or UUIDs for which invites could not be sent. |
||
180 | echo implode( |
||
181 | PHP_EOL, |
||
182 | wasCSVToArray( |
||
183 | urldecode( |
||
184 | wasKeyValueGet( |
||
185 | "data", |
||
186 | $result |
||
187 | ) |
||
188 | ) |
||
189 | ) |
||
190 | ); |
||
191 | |||
192 | return 0; |
||
193 | |||
194 | ?> |