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 binds to Corrade's "group" chat notification. ## |
||
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 | # Set this to the location of the PHP script that stores group messages. |
||
20 | $STORE = 'http://corrade.grimore.org/groupChat/storeGroupMessage.php'; |
||
21 | |||
22 | ########################################################################### |
||
23 | ## INTERNALS ## |
||
24 | ########################################################################### |
||
25 | |||
26 | ########################################################################### |
||
27 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
28 | ########################################################################### |
||
29 | function wasKeyValueGet($key, $data) { |
||
30 | return array_reduce( |
||
31 | explode( |
||
32 | "&", |
||
33 | $data |
||
34 | ), |
||
35 | function($o, $p) { |
||
36 | $x = explode("=", $p); |
||
37 | return array_shift($x) != $o ? $o : array_shift($x); |
||
38 | }, |
||
39 | $key |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | #### |
||
44 | # I. Build the POST array to send to Corrade. |
||
45 | $params = array( |
||
46 | 'command' => 'notify', |
||
47 | 'group' => $GROUP, |
||
48 | 'password' => $PASSWORD, |
||
49 | 'type' => 'group', |
||
50 | 'action' => 'set', # Set will discard other URLs |
||
51 | 'URL' => $STORE |
||
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 'Group chat notification installed!'; |
||
90 | break; |
||
91 | default: |
||
92 | echo 'Corrade returned the error: '.urldecode( |
||
93 | wasKeyValueGet( |
||
94 | "error", |
||
95 | $result |
||
96 | ) |
||
97 | ); |
||
98 | break; |
||
99 | } |
||
100 | |||
101 | ?> |