You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

send.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. Status codes:
  4. 0: sending
  5. 1: sent
  6. 2: failed to contact gate
  7. 3: generic error
  8. 4: no service
  9. 5: no pdu
  10. 6: radio off
  11. 7: unknown error
  12. */
  13. require_once "sql.php";
  14. if(isset($_GET['to']))
  15. $to = $_GET['to'];
  16. else
  17. $to = "";
  18. if(isset($_GET['gate']))
  19. $gate = $_GET['gate'];
  20. else
  21. $gate = "";
  22. if(isset($_GET['text']))
  23. $text = $_GET['text'];
  24. else
  25. $text = "";
  26. if(isset($_GET['maxWait']))
  27. $maxWait = $_GET['maxWait'];
  28. else
  29. $maxWait = "";
  30. if($to == "")
  31. error("Empty recipient");
  32. if($text == "")
  33. error("Empty text");
  34. if($gate == "")
  35. error("No gate provided");
  36. if($maxWait == "")
  37. error("Empty max wait");
  38. $gate = intval($gate);
  39. if($gate <= 0)
  40. error("Invalid gate");
  41. $maxWait = intval($maxWait);
  42. if($maxWait < 0 || !is_numeric($_GET['maxWait']))
  43. error("Invalid max wait");
  44. $select = $db->prepare("SELECT address, adb FROM gates WHERE id=:gate");
  45. if(!$select->execute(array(":gate" => $gate)))
  46. error("Unable to retrieve gate details");
  47. if($g = $select->fetch())
  48. {
  49. $insert = $db->prepare("INSERT INTO `sent` (`recipient`, `text`, `status`, `gate`) VALUES (:recipient, :text, :status, :gate)");
  50. if(!$insert->execute(array(":recipient" => $to, ":text" => $text, ":status" => 0, ":gate" => $gate)))
  51. error("Failed to save message");
  52. else
  53. {
  54. $id = intval($db->lastInsertId());
  55. $text = escapeshellarg($text);
  56. $to = escapeshellarg($to);
  57. if ($g["adb"] == null || $g["adb"] == "")
  58. {
  59. $device = escapeshellarg($g["address"] . ":5555");
  60. exec("adb connect $device");
  61. }
  62. else
  63. $device = escapeshellarg($g["adb"]);
  64. exec("adb -s $device shell am broadcast -a com.rob57530.smsserver.ADB --es text $text --es number $to --ei id $id");
  65. $id = intval($db->lastInsertId());
  66. $status = 0;
  67. $maxWait *= 1000;
  68. do
  69. {
  70. $select = $db->prepare("SELECT status FROM sent WHERE id=:id");
  71. if($select->execute(array(":id" => $id)))
  72. if($f = $select->fetch())
  73. $status = $f['status'];
  74. $maxWait -= 100;
  75. if($maxWait > 0)
  76. usleep(100000);
  77. } while($maxWait > 0 && $status == 0);
  78. echo json_encode(array("id" => $id, "status" => $status));
  79. }
  80. }
  81. else
  82. error("Gate not found");
  83. ?>