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.hostname:8080'; |
||
19 | |||
20 | ########################################################################### |
||
21 | ## INTERNALS ## |
||
22 | ########################################################################### |
||
23 | |||
24 | # If there is no "message" or "name" POST variable set then bail. |
||
25 | if(!isset($_POST['message']) || !isset($_POST['name'])) 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 | # I. Build the POST array to send to Corrade. |
||
46 | $params = array( |
||
47 | 'command' => 'tell', |
||
48 | 'group' => $GROUP, |
||
49 | 'password' => $PASSWORD, |
||
50 | 'entity' => 'group', |
||
51 | 'message' => $_POST['name'].' says '.$_POST['message'] |
||
52 | ); |
||
53 | |||
54 | #### |
||
55 | # II. Escape the data to be sent to Corrade. |
||
56 | array_walk($params, |
||
57 | function(&$value, $key) { |
||
58 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
59 | } |
||
60 | ); |
||
61 | $postvars = implode('&', $params); |
||
62 | |||
63 | #### |
||
64 | # III. Use curl to send the message. |
||
65 | if (!($curl = curl_init())) { |
||
66 | print 0; |
||
67 | return; |
||
68 | } |
||
69 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
70 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
71 | curl_setopt($curl, CURLOPT_POST, true); |
||
72 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
73 | $result = curl_exec($curl); |
||
74 | curl_close($curl); |
||
75 | |||
76 | #### |
||
77 | # IV. Grab the status of the command. |
||
78 | $status = urldecode( |
||
79 | wasKeyValueGet( |
||
80 | "success", |
||
81 | $result |
||
82 | ) |
||
83 | ); |
||
84 | |||
85 | #### |
||
86 | # V. Check the status of the command. |
||
87 | switch($status) { |
||
88 | case "True": |
||
89 | echo 'Message sent successfully!'; |
||
90 | break; |
||
91 | default: |
||
92 | echo 'Corrade returned the error: '.urldecode( |
||
93 | wasKeyValueGet( |
||
94 | "error", |
||
95 | $result |
||
96 | ) |
||
97 | ); |
||
98 | break; |
||
99 | } |
||
100 | |||
101 | ?> |