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

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 1 Rev 4
1 <?php 1 <?php
2   2  
3 ########################################################################### 3 ###########################################################################
4 ## Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 ## 4 ## Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 ##
5 ########################################################################### 5 ###########################################################################
6 ## This is a script that sends a message to an agent from Corrade and it ## 6 ## This is a script that sends a message to an agent from Corrade and it ##
7 ## also stores the sent message to a conversation file. ## 7 ## also stores the sent message to a conversation file. ##
8 ########################################################################### 8 ###########################################################################
9   9  
10 ########################################################################### 10 ###########################################################################
11 ## CONFIGURATION ## 11 ## CONFIGURATION ##
12 ########################################################################### 12 ###########################################################################
13   13  
14 require_once('config.php'); 14 require_once('config.php');
15 require_once('functions.php'); 15 require_once('functions.php');
16   16  
17 ########################################################################### 17 ###########################################################################
18 ## INTERNALS ## 18 ## INTERNALS ##
19 ########################################################################### 19 ###########################################################################
20   20  
21 # If there is no "message" or "name" POST variable set then bail. 21 # Check that we have all the necessary variables.
-   22 if(!isset($_POST['message']) ||
22 if(!isset($_POST['message']) || 23 empty($_POST['message']) ||
-   24 !isset($_POST['name']) ||
23 !isset($_POST['name']) || 25 empty($_POST['name']) ||
-   26 !isset($_POST['firstname']) ||
-   27 empty($_POST['firstname']) ||
24 !isset($_POST['firstname']) || 28 !isset($_POST['lastname']) ||
25 !isset($_POST['lastname'])) return; 29 empty($_POST['lastname'])) return;
26   30  
27 #### 31 ####
28 # I. Build the POST array to send to Corrade. 32 # I. Build the POST array to send to Corrade.
29 $params = array( 33 $params = array(
30 'command' => 'tell', 34 'command' => 'tell',
31 'group' => $GROUP, 35 'group' => $GROUP,
32 'password' => $PASSWORD, 36 'password' => $PASSWORD,
33 'entity' => 'avatar', 37 'entity' => 'avatar',
34 'firstname' => $_POST['firstname'], 38 'firstname' => $_POST['firstname'],
35 'lastname' => $_POST['lastname'], 39 'lastname' => $_POST['lastname'],
36 'message' => $_POST['name'].' says '.$_POST['message'] 40 'message' => $_POST['name'].' says '.$_POST['message']
37 ); 41 );
38   42  
39 #### 43 ####
40 # II. Escape the data to be sent to Corrade. 44 # II. Escape the data to be sent to Corrade.
41 array_walk($params, 45 array_walk($params,
42 function(&$value, $key) { 46 function(&$value, $key) {
43 $value = rawurlencode($key)."=".rawurlencode($value); 47 $value = rawurlencode($key)."=".rawurlencode($value);
44 } 48 }
45 ); 49 );
46 $postvars = implode('&', $params); 50 $postvars = implode('&', $params);
47   51  
48 #### 52 ####
49 # III. Use curl to send the message. 53 # III. Use curl to send the message.
50 if (!($curl = curl_init())) { 54 if (!($curl = curl_init())) {
51 print 0; 55 print 0;
52 return; 56 return;
53 } 57 }
54 curl_setopt($curl, CURLOPT_URL, $URL); 58 curl_setopt($curl, CURLOPT_URL, $URL);
55 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 59 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
56 curl_setopt($curl, CURLOPT_POST, true); 60 curl_setopt($curl, CURLOPT_POST, true);
57 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); 61 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
58 curl_setopt($curl, CURLOPT_ENCODING, true); 62 curl_setopt($curl, CURLOPT_ENCODING, true);
59 $result = curl_exec($curl); 63 $result = curl_exec($curl);
60 curl_close($curl); 64 curl_close($curl);
61   65  
62 #### 66 ####
63 # IV. Grab the status of the command. 67 # IV. Grab the status of the command.
64 $status = urldecode( 68 $status = urldecode(
65 wasKeyValueGet( 69 wasKeyValueGet(
66 "success", 70 "success",
67 $result 71 $result
68 ) 72 )
69 ); 73 );
70   74  
71 #### 75 ####
72 # IV. Check the status of the command. 76 # IV. Check the status of the command.
73 switch($status) { 77 switch($status) {
74 case "True": # The message was sent successfully so store it within a conversation file. 78 case "True": # The message was sent successfully so store it within a conversation file.
75 #### 79 ####
76 # V. Get the path to the configured chat directory. 80 # V. Get the path to the configured chat directory.
77 $chatPath = realpath($CHAT_DIRECTORY); 81 $chatPath = realpath($CHAT_DIRECTORY);
78   82  
79 #### 83 ####
80 # VI. Get the user path. 84 # VI. Get the user path.
81 $userPath = join( 85 $userPath = join(
82 DIRECTORY_SEPARATOR, 86 DIRECTORY_SEPARATOR,
83 array( 87 array(
84 $CHAT_DIRECTORY, 88 $CHAT_DIRECTORY,
85 ucfirst( 89 ucfirst(
86 strtolower( 90 strtolower(
87 $_POST['firstname'] 91 $_POST['firstname']
88 ) 92 )
89 ) .' '. 93 ) .' '.
90 ucfirst( 94 ucfirst(
91 strtolower( 95 strtolower(
92 $_POST['lastname'] 96 $_POST['lastname']
93 ) 97 )
94 ).'.log' 98 ).'.log'
95 ) 99 )
96 ); 100 );
97   101  
98 #### 102 ####
99 # VII. Check that the file will be placed within the chat directory. 103 # VII. Check that the file will be placed within the chat directory.
100 $pathPart = pathinfo($userPath); 104 $pathPart = pathinfo($userPath);
101 if(realpath($pathPart['dirname']) != $chatPath) 105 if(realpath($pathPart['dirname']) != $chatPath)
102 die; 106 die;
103 107
104 #### 108 ####
105 # VIII. Store the message. 109 # VIII. Store the message.
106 storeAvatarConversation( 110 storeAvatarConversation(
107 $_POST['name'], 111 $_POST['name'],
108 '', 112 '',
109 $_POST['message'], 113 $_POST['message'],
110 $userPath, 114 $userPath,
111 $CHAT_LINES 115 $CHAT_LINES
112 ); 116 );
113 break; 117 break;
114 default: # Otherwise, return the Corrade error message. 118 default: # Otherwise, return the Corrade error message.
115 echo 'Corrade failed to deliver the message with the error message: '.urldecode( 119 echo 'Corrade failed to deliver the message with the error message: '.urldecode(
116 wasKeyValueGet( 120 wasKeyValueGet(
117 "error", 121 "error",
118 $result 122 $result
119 ) 123 )
120 ); 124 );
121 break; 125 break;
122 } 126 }
123   127  
124 ?> 128 ?>
125   129  
126
Generated by GNU Enscript 1.6.5.90.
130
Generated by GNU Enscript 1.6.5.90.
127   131  
128   132  
129   133