|
@@ -44,61 +44,44 @@ $maxWait = intval($maxWait);
|
44
|
44
|
if($maxWait < 0 || !is_numeric($_GET['maxWait']))
|
45
|
45
|
error("Invalid max wait");
|
46
|
46
|
|
47
|
|
-$select = $db->prepare("SELECT address, port, lastSeen FROM gates WHERE id=:gate");
|
|
47
|
+$select = $db->prepare("SELECT address, adb FROM gates WHERE id=:gate");
|
48
|
48
|
if(!$select->execute(array(":gate" => $gate)))
|
49
|
49
|
error("Unable to retrieve gate details");
|
50
|
50
|
|
51
|
51
|
if($g = $select->fetch())
|
52
|
52
|
{
|
53
|
|
- if(intval(strtotime($g['lastSeen'])) < time() - $gateTimeout)
|
54
|
|
- error("Gate is down");
|
55
|
|
-
|
56
|
53
|
$insert = $db->prepare("INSERT INTO `sent` (`recipient`, `text`, `status`, `gate`) VALUES (:recipient, :text, :status, :gate)");
|
57
|
54
|
if(!$insert->execute(array(":recipient" => $to, ":text" => $text, ":status" => 0, ":gate" => $gate)))
|
58
|
55
|
error("Failed to save message");
|
59
|
56
|
else
|
60
|
57
|
{
|
61
|
58
|
$id = intval($db->lastInsertId());
|
62
|
|
- $out = http_get("http:/"."/".$g['address'].":".$g['port']."/send.php?to=".urlencode($to)."&text=".urlencode($text)."&id=".$id,
|
63
|
|
- array("timeout" => 10, "connecttimeout" => 10));
|
64
|
|
- if($out == "")
|
|
59
|
+ $text = escapeshellarg($text);
|
|
60
|
+ $to = escapeshellarg($to);
|
|
61
|
+ if ($g["adb"] == null || $g["adb"] == "")
|
65
|
62
|
{
|
66
|
|
- $update = $db->prepare("UPDATE sent SET `status`=2, `timeStatus`=NOW() WHERE `id`=:id");
|
67
|
|
- if(!$update->execute(array(":id" => $id)))
|
68
|
|
- error("Failed to update database after failed to contact gate (general failure)");
|
69
|
|
- else
|
70
|
|
- echo json_encode(array("id" => $id, "status" => 2));
|
|
63
|
+ $device = escapeshellarg($g["address"] . ":5555");
|
|
64
|
+ exec("adb connect $device");
|
71
|
65
|
}
|
72
|
66
|
else
|
|
67
|
+ $device = escapeshellarg($g["adb"]);
|
|
68
|
+ exec("adb -s $device shell am broadcast -a com.rob57530.smsserver.ADB --es text $text --es number $to --ei id $id");
|
|
69
|
+ $id = intval($db->lastInsertId());
|
|
70
|
+ $status = 0;
|
|
71
|
+ $maxWait *= 1000;
|
|
72
|
+ do
|
73
|
73
|
{
|
74
|
|
- $out = json_decode(http_parse_message($out)->body);
|
75
|
|
- if(isset($out->error))
|
76
|
|
- {
|
77
|
|
- $update = $db->prepare("UPDATE sent SET `status`=7, `timeStatus`=NOW() WHERE `id`=:id");
|
78
|
|
- if(!$update->execute(array(":id" => $id)))
|
79
|
|
- error("Failed to update database after failed to contact gate (general failure)");
|
80
|
|
- else
|
81
|
|
- echo json_encode(array("id" => $id, "status" => 7, "error" => $out->error));
|
82
|
|
- }
|
83
|
|
- else
|
84
|
|
- {
|
85
|
|
- $status = 0;
|
86
|
|
- $maxWait *= 1000;
|
87
|
|
- do
|
88
|
|
- {
|
89
|
|
- $select = $db->prepare("SELECT status FROM sent WHERE id=:id");
|
90
|
|
- if($select->execute(array(":id" => $id)))
|
91
|
|
- if($f = $select->fetch())
|
92
|
|
- $status = $f['status'];
|
93
|
|
- $maxWait -= 100;
|
94
|
|
- if($maxWait > 0)
|
95
|
|
- usleep(100000);
|
96
|
|
- } while($maxWait > 0 && $status == 0);
|
97
|
|
- echo json_encode(array("id" => $id, "status" => $status));
|
98
|
|
- }
|
99
|
|
- }
|
|
74
|
+ $select = $db->prepare("SELECT status FROM sent WHERE id=:id");
|
|
75
|
+ if($select->execute(array(":id" => $id)))
|
|
76
|
+ if($f = $select->fetch())
|
|
77
|
+ $status = $f['status'];
|
|
78
|
+ $maxWait -= 100;
|
|
79
|
+ if($maxWait > 0)
|
|
80
|
+ usleep(100000);
|
|
81
|
+ } while($maxWait > 0 && $status == 0);
|
|
82
|
+ echo json_encode(array("id" => $id, "status" => $status));
|
100
|
83
|
}
|
101
|
84
|
}
|
102
|
85
|
else
|
103
|
|
- error("Gate not found");
|
|
86
|
+error("Gate not found");
|
104
|
87
|
?>
|