corrade-http-templates – Diff between revs 4 and 52
?pathlinks?
Rev 4 | Rev 52 | |||
---|---|---|---|---|
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 | |
12 | |
|
13 | require_once('config.php'); |
13 | require_once('config.php'); |
|
14 | require_once('functions.php'); |
14 | require_once('functions.php'); |
|
15 | |
15 | |
|
16 | ########################################################################### |
16 | ########################################################################### |
|
17 | ## INTERNALS ## |
17 | ## INTERNALS ## |
|
18 | ########################################################################### |
18 | ########################################################################### |
|
- | 19 | |
||
- | 20 | # CRSF. |
||
- | 21 | session_start(); |
||
- | 22 | if (empty($_POST['token']) || !hash_equals($_SESSION['token'], $_POST['token'])) { |
||
- | 23 | http_response_code(403); |
||
- | 24 | die('Forbidden.'); |
||
- | 25 | } |
||
19 | |
26 | |
|
20 | # If there is no message set or no name set or if the message or the name |
27 | # If there is no message set or no name set or if the message or the name |
|
21 | # are empty then do not proceed any further. |
28 | # are empty then do not proceed any further. |
|
22 | if(!isset($_POST['message']) || |
29 | if(!isset($_POST['message']) || |
|
23 | empty($_POST['message']) || |
30 | empty($_POST['message']) || |
|
24 | !isset($_POST['name']) || |
31 | !isset($_POST['name']) || |
|
25 | empty($_POST['name'])) return; |
32 | empty($_POST['name'])) return; |
|
26 | |
33 | |
|
27 | #### |
34 | #### |
|
28 | # I. Build the POST array to send to Corrade. |
35 | # I. Build the POST array to send to Corrade. |
|
29 | $params = array( |
36 | $params = array( |
|
30 | 'command' => 'tell', |
37 | 'command' => 'tell', |
|
31 | 'group' => $GROUP, |
38 | 'group' => $GROUP, |
|
32 | 'password' => $PASSWORD, |
39 | 'password' => $PASSWORD, |
|
33 | 'entity' => 'group', |
40 | 'entity' => 'group', |
|
34 | 'message' => $_POST['name'].' says '.$_POST['message'] |
41 | 'message' => $_POST['name'].' says '.$_POST['message'] |
|
35 | ); |
42 | ); |
|
36 | |
43 | |
|
37 | #### |
44 | #### |
|
38 | # II. Escape the data to be sent to Corrade. |
45 | # II. Escape the data to be sent to Corrade. |
|
39 | array_walk($params, |
46 | array_walk($params, |
|
40 | function(&$value, $key) { |
47 | function(&$value, $key) { |
|
41 | $value = rawurlencode($key)."=".rawurlencode($value); |
48 | $value = rawurlencode($key)."=".rawurlencode($value); |
|
42 | } |
49 | } |
|
43 | ); |
50 | ); |
|
44 | $postvars = implode('&', $params); |
51 | $postvars = implode('&', $params); |
|
45 | |
52 | |
|
46 | #### |
53 | #### |
|
47 | # III. Use curl to send the message. |
54 | # III. Use curl to send the message. |
|
48 | if (!($curl = curl_init())) { |
55 | if (!($curl = curl_init())) { |
|
49 | print 0; |
56 | print 0; |
|
50 | return; |
57 | return; |
|
51 | } |
58 | } |
|
52 | curl_setopt($curl, CURLOPT_URL, $URL); |
59 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
53 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
60 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
54 | curl_setopt($curl, CURLOPT_POST, true); |
61 | curl_setopt($curl, CURLOPT_POST, true); |
|
55 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
62 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
56 | curl_setopt($curl, CURLOPT_ENCODING, true); |
63 | curl_setopt($curl, CURLOPT_ENCODING, true); |
|
57 | $result = curl_exec($curl); |
64 | $result = curl_exec($curl); |
|
58 | curl_close($curl); |
65 | curl_close($curl); |
|
59 | |
66 | |
|
60 | #### |
67 | #### |
|
61 | # IV. Grab the status of the command. |
68 | # IV. Grab the status of the command. |
|
62 | $status = urldecode( |
69 | $status = urldecode( |
|
63 | wasKeyValueGet( |
70 | wasKeyValueGet( |
|
64 | "success", |
71 | "success", |
|
65 | $result |
72 | $result |
|
66 | ) |
73 | ) |
|
67 | ); |
74 | ); |
|
68 | |
75 | |
|
69 | #### |
76 | #### |
|
70 | # V. Check the status of the command. |
77 | # V. Check the status of the command. |
|
71 | switch($status) { |
78 | switch($status) { |
|
72 | case "True": # Be silent if the message has been sent successfully. |
79 | case "True": # Be silent if the message has been sent successfully. |
|
73 | # echo 'Message sent successfully!'; |
80 | # echo 'Message sent successfully!'; |
|
74 | break; |
81 | break; |
|
75 | default: # If an error occured, then return the error message. |
82 | default: # If an error occured, then return the error message. |
|
76 | echo 'Corrade failed to send the group message and reported the error: '.urldecode( |
83 | echo 'Corrade failed to send the group message and reported the error: '.urldecode( |
|
77 | wasKeyValueGet( |
84 | wasKeyValueGet( |
|
78 | "error", |
85 | "error", |
|
79 | $result |
86 | $result |
|
80 | ) |
87 | ) |
|
81 | ); |
88 | ); |
|
82 | break; |
89 | break; |
|
83 | } |
90 | } |
|
84 | |
91 | |
|
85 | ?> |
92 | ?> |
|
86 | |
93 | |