123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /*
- Status codes:
- 0: sending
- 1: sent
- 2: failed to contact gate
- 3: generic error
- 4: no service
- 5: no pdu
- 6: radio off
- 7: unknown error
- */
- require_once "sql.php";
- if(isset($_GET['to']))
- $to = $_GET['to'];
- else
- $to = "";
- if(isset($_GET['gate']))
- $gate = $_GET['gate'];
- else
- $gate = "";
- if(isset($_GET['text']))
- $text = $_GET['text'];
- else
- $text = "";
- if(isset($_GET['maxWait']))
- $maxWait = $_GET['maxWait'];
- else
- $maxWait = "";
-
- if($to == "")
- error("Empty recipient");
- if($text == "")
- error("Empty text");
- if($gate == "")
- error("No gate provided");
- if($maxWait == "")
- error("Empty max wait");
-
- $gate = intval($gate);
- if($gate <= 0)
- error("Invalid gate");
- $maxWait = intval($maxWait);
- if($maxWait < 0 || !is_numeric($_GET['maxWait']))
- error("Invalid max wait");
-
- $select = $db->prepare("SELECT address, adb FROM gates WHERE id=:gate");
- if(!$select->execute(array(":gate" => $gate)))
- error("Unable to retrieve gate details");
-
- if($g = $select->fetch())
- {
- $insert = $db->prepare("INSERT INTO `sent` (`recipient`, `text`, `status`, `gate`) VALUES (:recipient, :text, :status, :gate)");
- if(!$insert->execute(array(":recipient" => $to, ":text" => $text, ":status" => 0, ":gate" => $gate)))
- error("Failed to save message");
- else
- {
- $id = intval($db->lastInsertId());
- $text = escapeshellarg($text);
- $to = escapeshellarg($to);
- if ($g["adb"] == null || $g["adb"] == "")
- {
- $device = escapeshellarg($g["address"] . ":5555");
- exec("adb connect $device");
- }
- else
- $device = escapeshellarg($g["adb"]);
- exec("adb -s $device shell am broadcast -a com.rob57530.smsserver.ADB --es text $text --es number $to --ei id $id");
- $id = intval($db->lastInsertId());
- $status = 0;
- $maxWait *= 1000;
- do
- {
- $select = $db->prepare("SELECT status FROM sent WHERE id=:id");
- if($select->execute(array(":id" => $id)))
- if($f = $select->fetch())
- $status = $f['status'];
- $maxWait -= 100;
- if($maxWait > 0)
- usleep(100000);
- } while($maxWait > 0 && $status == 0);
- echo json_encode(array("id" => $id, "status" => $status));
- }
- }
- else
- error("Gate not found");
- ?>
|