forked from fiquett/Viper_SmartStart_Control
/
viper_control.php
188 lines (166 loc) · 5.36 KB
/
viper_control.php
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
<?php
// Brandon Fiquett
// Viper SmartStart Interface
// Version 1.0
// March 1, 2011
header('Content-type: text/plain');
$smartstart_username = 'VIPER_EMAIL_ADDRESS';
$smartstart_password = 'VIPER_PASSWORD';
$smartstart_server = 'colt.calamp-ts.com';
$action = 'arm';
$smartstart_username = (isset($_REQUEST['user'])) ? $_REQUEST['user'] : $smartstart_username;
$smartstart_password = (isset($_REQUEST['pass'])) ? $_REQUEST['pass'] : $smartstart_password;
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : $action;
if (($smartstart_username == '') or ($smartstart_password == '') or ($action == '')){echo "Missing information!\n"; die();}
if(defined('STDIN') ){
echo("Running from CLI\n");
echo "\n";
echo "Requesting Session ID...\n";
$sessionID = getSessionID($smartstart_username,$smartstart_password);
echo "Session ID: " . $sessionID . " \n";
echo "Getting Vechicle List...\n";
$vehicles = getVehicles($sessionID);
echo count($vehicles->Devices) . " vehicle(s) available. \n";
$vehicleID = getVehicleID(0);
print_r(getAvailableActions(0));
echo "Enter Command: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$result = json_decode(sendCommandToVehicle($vehicleID,$sessionID,trim($line)));
if($result->Return->ResponseSummary->StatusCode == 0){
echo "Command received successfully.\n";
}
else{
echo "Error: " . $result->Return->ResponseSummary->ErrorMessage ;
}
}
else{
$sessionID = getSessionID($smartstart_username,$smartstart_password);
$vehicles = getVehicles($sessionID);
$vehicleID = getVehicleID(0);
//print_r(getAvailableActions(0));
//print_r($vehicles);
echo sendCommandToVehicle($vehicleID,$sessionID,$action);
}
function curlGet($url, $referer = null, $headers = null){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9");
if(!is_null($referer)) curl_setopt($ch, CURLOPT_REFERER, $referer);
if(!is_null($headers)) curl_setopt($ch, CURLOPT_HEADER, $headers);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
$html = curl_exec($ch);
if(curl_errno($ch) != 0){
throw new Exception("Error during GET of '$url': " . curl_error($ch));
}
return $html;
}
function json_format($json)
{
$tab = " ";
$new_json = "";
$indent_level = 0;
$in_string = false;
$json_obj = json_decode($json);
if($json_obj === false)
return false;
$json = json_encode($json_obj);
$len = strlen($json);
for($c = 0; $c < $len; $c++)
{
$char = $json[$c];
switch($char)
{
case '{':
case '[':
if(!$in_string)
{
$new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
$indent_level++;
}
else
{
$new_json .= $char;
}
break;
case '}':
case ']':
if(!$in_string)
{
$indent_level--;
$new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
}
else
{
$new_json .= $char;
}
break;
case ',':
if(!$in_string)
{
$new_json .= ",\n" . str_repeat($tab, $indent_level);
}
else
{
$new_json .= $char;
}
break;
case ':':
if(!$in_string)
{
$new_json .= ": ";
}
else
{
$new_json .= $char;
}
break;
case '"':
if($c > 0 && $json[$c-1] != '\\')
{
$in_string = !$in_string;
}
default:
$new_json .= $char;
break;
}
}
return $new_json;
}
function getSessionID($username,$password){
global $smartstart_server;
$html = curlGet("https://$smartstart_server/auth/login/$username/$password","",true);
preg_match('/^Set-Cookie: kohanasession=(.*?);/m',$html , $m);
return $m[1];
}
function getVehicleID($vehicleNum){
global $vehicles;
return $vehicles->Devices[$vehicleNum]->DeviceId;
}
function getAvailableActions($vehicleNum){
global $vehicles;
$results = array();
$actions = $vehicles->Devices[$vehicleNum]->AvailActions;
//var_dump($actions);
foreach ($actions as $action) {
array_push($results,$action->Name);
}
return $results;
}
function getVehicles($sessionID){
global $smartstart_server;
$json = curlGet("https://$smartstart_server/device/advancedsearch?sessid=$sessionID");
//echo json_format($json);
$result = json_decode($json);
return $result->Return->Results;
}
function sendCommandToVehicle($vehicleID,$sessionID,$action){
global $smartstart_server;
$json = curlGet("https://$smartstart_server/device/sendcommand/$vehicleID/$action?sessid=$sessionID");
//echo json_format($json);
return $json;
}
?>