corrade-http-templates – Blame information for rev 1

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