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