corrade-http-templates – Diff between revs 2 and 4

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