-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofficebot-controller.php
More file actions
220 lines (188 loc) · 6.59 KB
/
officebot-controller.php
File metadata and controls
220 lines (188 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
require_once('officebot-include.php');
error_reporting(0);
include ('robotMessages.php');
//$phonereg = '758fa234cb7edb05';
/**
* Gets an authentication token for a Google service (defaults to
* Picasa). Puts the token in a session variable and re-uses it as
* needed, instead of fetching a new token for every call.
*
* @static
* @access public
* @param string $username Google email account
* @param string $password Password for Google email account
* @param string $source name of the calling application (defaults to your_google_app)
* @param string $service name of the Google service to call (defaults to cloud to device messaging for Android)
* @return boolean|string An authentication token, or false on failure
*/
function googleAuthenticate($username, $password, $source = 'org.abarry.telo', $service = 'ac2dm') {
//$session_token = $source . '_' . $service . '_auth_token';
$session_token = "auth_token";
if ($_SESSION[$session_token]) {
return $_SESSION[$session_token];
}
// get an authorization token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('GOOGLE')
. "&Email=" . urlencode($username)
. "&Passwd=" . urlencode($password)
. "&source=" . urlencode($source)
. "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT)); //for debugging the request
$response = curl_exec($ch);
curl_close($ch);
if (strpos($response, '200 OK') === false) {
return false;
}
// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);
if (!$matches[2]) {
return false;
}
$_SESSION[$session_token] = $matches[2];
return $matches[2];
}
/**
* Sends a push notification to an Android device using Google C2DM when given a payload (under 1024 bytes),
* the server authorization code, and the phone registration id.
*
* @param datain less than 1024 bytes of input to be sent to the device (string)
* @param serverAuth server authorization obtained from googleAuthenticate
* @param phoneRegistrationId registration of the target phone. This must be obtained from the phone, and we get it out of a database on a previous page.
*/
function sendAndroidPush($datain, $serverAuth, $phoneRegistrationId)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
$post_fields = "registration_id=" . urlencode($phoneRegistrationId)
. "&data.payload=" . urlencode($datain)
. "&collapse_key=0";
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization: GoogleLogin auth=' . $serverAuth));
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
// var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT)); //for debugging the request
$response = curl_exec($ch);
// echo $response;
curl_close($ch);
return ($response);
}
function is_valid_create_command($cmd)
// make sure it's a command we understand
{
return ($cmd);
}
function is_valid_robot_command($cmd)
// make sure it's a command we understand
{
switch ($cmd)
{
case 'u':
case 'd':
case 'l':
case 'r':
return ($cmd);
default:
return (false);
}
}
function robot_command($cmd)
{
global $conn, $createActionTable, $robotname, $sendStr, $phonereg;
$ts = microtime_long();
$latency = microtime(true);
$commandSocket = socket_create (AF_INET, SOCK_STREAM, 0);
socket_set_option($commandSocket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2, "usec"=>500));
socket_bind($commandSocket, '127.0.0.1');
socket_connect($commandSocket, '127.0.0.1', '49441');
socket_set_block($commandSocket);
if (isset($_REQUEST['speed']))
{
$s = $_REQUEST['speed'];
} else {
$s = '';
}
$xmppStr = "{$_REQUEST['robotAddr']}|$ts|$cmd|$s|\n";
socket_write($commandSocket, $xmppStr);
$json = socket_read($commandSocket, 1500);
// echo "json is $json";
$jArr = (array)json_decode($json);
$alertMessage = getRobotMessage($_REQUEST['robotAddr']);
if ($alertMessage != null)
{
$jArr['theMessage'] = $alertMessage;
} else {
$jArr['theMessage'] = '';
}
if (!isset($jArr['status'])) // there was no response from the server, so forge it
{
$jArr['status'] = 'timeout - no response from robot';
$jArr['latency'] = microtime(true) - $latency;
}
$json = json_encode($jArr);
socket_set_nonblock($commandSocket);
socket_close($commandSocket);
list($phoneName, $domain) = preg_split("/@/", $_REQUEST['robotAddr']);
$sql = "SELECT * FROM phones WHERE name=\"" . mysql_real_escape_string($phoneName) . "\" OR name=\"" . mysql_real_escape_string($_REQUEST['robotAddr']) . "\" ";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
if (strlen($myrow["registration"]) > 0) {
$phoneReg = $myrow["registration"];
$deviceId = $myrow["deviceid"];
}
$a = $_REQUEST['robotAddr'];
if ($cmd != 'u' || $cmd != 'n')
{
$speed = $s;
} else {
$speed = '';
}
$mtr = new messageToRobot($a, $a, $a, $cmd, $speed, '', $ts);
$ts = $mtr->timeStamp;
$sendStr = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $mtr->XML->asXML());
syslog (LOG_NOTICE, "{$_REQUEST['robotAddr']}: Web interface message passed to controller: $sendStr");
if (sendAndroidPush($sendStr, $_SESSION["auth_token"], $phoneReg))
{
syslog (LOG_NOTICE, "{$_REQUEST['robotAddr']}: C2DM message passed to Google for device ID $deviceId: $sendStr");
} else {
syslog (LOG_NOTICE, "{$_REQUEST['robotAddr']}: C2DM message FAILED for device ID $deviceId: $sendStr");
}
if (strlen($json))
{
return ($json);
} else {
return (false);
}
}
session_start();
header("Content-type: text/json");
if (isset ($_REQUEST['cmd']))
{
$json = robot_command($_REQUEST['cmd']);
}
if (isset ($_REQUEST['pantilt']))
{
$json = robot_command($_REQUEST['pantilt']);
}
header ("Content-type: text/json\n");
if ($json !== false)
{
echo ($json);
} else {
echo (json_encode(array('status' => 'failed')));
}
// check for an existing auth token from google
if (!isset($_SESSION["auth_token"]) || strlen($_SESSION["auth_token"]) < 5)
{
googleAuthenticate("telebotphone@gmail.com", "9thsense&");
}
// we have the phone id and the auth id, we're good to go!
//echo "have auth already";
?>