Browse Source

switched to intent

master
Robin Thoni 9 years ago
parent
commit
3100ed628e
3 changed files with 23 additions and 89 deletions
  1. 1
    2
      gates.php
  2. 0
    48
      register.php
  3. 22
    39
      send.php

+ 1
- 2
gates.php View File

@@ -19,8 +19,7 @@ if(!$select->execute($array))
19 19
 $gates = array();
20 20
 while($gate = $select->fetch())
21 21
 	$gates[] = array("id" => intval($gate['id']), "name" => $gate['name'],
22
-		"number" => $gate['number'], "address" => $gate['address'],
23
-		"lastSeen" => intval(time()));
22
+		"number" => $gate['number'], "lastSeen" => intval(time()));
24 23
 
25 24
 echo json_encode(array("timestamp" => time(), "gateTimeout" => $gateTimeout, "gates" => $gates));
26 25
 ?>

+ 0
- 48
register.php View File

@@ -1,48 +0,0 @@
1
-<?php
2
-require_once "sql.php";
3
-if(isset($_GET['name']))
4
-	$name = $_GET['name'];
5
-else
6
-	$name = "";
7
-if(isset($_GET['number']))
8
-	$number = $_GET['number'];
9
-else
10
-	$number = "";
11
-if(isset($_GET['port']))
12
-	$port = $_GET['port'];
13
-else
14
-	$port = "";
15
-
16
-if($name == "")
17
-	error("Empty name");
18
-if($number == "")
19
-	error("Empty number");
20
-if($port == "")
21
-	error("Empty port");
22
-$port = intval($port);
23
-if($port < 1 || $port > 65535)
24
-	error("Invalid port (1 <= port <= 65535)");
25
-
26
-$select = $db->prepare("SELECT id FROM gates WHERE number=:number");
27
-if($select->execute(array(":number" => $number)))
28
-{
29
-	if($g = $select->fetch())
30
-	{
31
-		$update = $db->prepare("UPDATE gates SET name=:name, address=:address, port=:port, lastSeen=NULL WHERE id=:id");
32
-		if($update->execute(array(":name" => $name, ":address" => $_SERVER['REMOTE_ADDR'], ":port" => $port, ":id" => $g['id'])))
33
-			echo json_encode(array("id" => intval($g['id']), "gateTimeout" => $gateTimeout));
34
-		else
35
-			error("Failed to update database");
36
-	}
37
-	else
38
-	{
39
-		$insert = $db->prepare("INSERT INTO gates (name, number, address, port) VALUES (:name, :number, :address, :port) ON DUPLICATE KEY UPDATE lastSeen=NULL");
40
-		if($insert->execute(array(":name" => $name, ":number" => $number, ":address" => $_SERVER['REMOTE_ADDR'], ":port" => $port)))
41
-			echo json_encode(array("id" => intval($db->lastInsertId()), "gateTimeout" => $gateTimeout));
42
-		else
43
-			error("Failed to insert into database");
44
-	}
45
-}
46
-else
47
-	error("Failed to query gates");
48
-?>

+ 22
- 39
send.php View File

@@ -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
 ?>

Loading…
Cancel
Save