corrade-http-templates – Diff between revs 73 and 81

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