corrade-http-templates – Blame information for rev 48

Subversion Repositories:
Rev:
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  
4 eva 13 require_once('config.php');
2 eva 14  
15 ###########################################################################
16 ## INTERNALS ##
17 ###########################################################################
18  
19 ###########################################################################
20 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
21 ###########################################################################
22 function wasKeyValueGet($key, $data) {
23 return array_reduce(
24 explode(
25 "&",
26 $data
27 ),
28 function($o, $p) {
29 $x = explode("=", $p);
30 return array_shift($x) != $o ? $o : array_shift($x);
31 },
32 $key
33 );
34 }
35  
36 ####
37 # I. Build the POST array to send to Corrade.
38 $params = array(
39 'command' => 'notify',
40 'group' => $GROUP,
41 'password' => $PASSWORD,
42 'type' => 'group',
48 office 43 'action' => 'add', # Set will discard other URLs
44 'tag' => $NOTIFICATION_TAG,
2 eva 45 'URL' => $STORE
46 );
47  
48 ####
49 # II. Escape the data to be sent to Corrade.
50 array_walk($params,
51 function(&$value, $key) {
52 $value = rawurlencode($key)."=".rawurlencode($value);
53 }
54 );
55 $postvars = implode('&', $params);
56  
57 ####
58 # III. Use curl to send the message.
59 if (!($curl = curl_init())) {
60 print 0;
61 return;
62 }
63 curl_setopt($curl, CURLOPT_URL, $URL);
64 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
65 curl_setopt($curl, CURLOPT_POST, true);
66 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
67 $result = curl_exec($curl);
68 curl_close($curl);
69  
70 ####
71 # IV. Grab the status of the command.
72 $status = urldecode(
73 wasKeyValueGet(
74 "success",
75 $result
76 )
77 );
78  
79 ####
80 # V. Check the status of the command.
81 switch($status) {
82 case "True":
48 office 83 echo 'Group chat notification installed!'.PHP_EOL;
2 eva 84 break;
85 default:
86 echo 'Corrade returned the error: '.urldecode(
87 wasKeyValueGet(
88 "error",
89 $result
90 )
48 office 91 ).PHP_EOL;
2 eva 92 break;
93 }
94  
95 ?>