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