corrade-http-templates – Blame information for rev 4

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