Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

send.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, port, lastSeen 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. if(intval(strtotime($g['lastSeen'])) < time() - $gateTimeout)
  50. error("Gate is down");
  51. $insert = $db->prepare("INSERT INTO `sent` (`recipient`, `text`, `status`, `gate`) VALUES (:recipient, :text, :status, :gate)");
  52. if(!$insert->execute(array(":recipient" => $to, ":text" => $text, ":status" => 0, ":gate" => $gate)))
  53. error("Failed to save message");
  54. else
  55. {
  56. $id = intval($db->lastInsertId());
  57. $out = http_get("http:/"."/".$g['address'].":".$g['port']."/send.php?to=".urlencode($to)."&text=".urlencode($text)."&id=".$id,
  58. array("timeout" => 10, "connecttimeout" => 10));
  59. if($out == "")
  60. {
  61. $update = $db->prepare("UPDATE sent SET `status`=2, `timeStatus`=NOW() WHERE `id`=:id");
  62. if(!$update->execute(array(":id" => $id)))
  63. error("Failed to update database after failed to contact gate (general failure)");
  64. else
  65. echo json_encode(array("id" => $id, "status" => 2));
  66. }
  67. else
  68. {
  69. $out = json_decode(http_parse_message($out)->body);
  70. if(isset($out->error))
  71. {
  72. $update = $db->prepare("UPDATE sent SET `status`=7, `timeStatus`=NOW() WHERE `id`=:id");
  73. if(!$update->execute(array(":id" => $id)))
  74. error("Failed to update database after failed to contact gate (general failure)");
  75. else
  76. echo json_encode(array("id" => $id, "status" => 7, "error" => $out->error));
  77. }
  78. else
  79. {
  80. $status = 0;
  81. $maxWait *= 1000;
  82. do
  83. {
  84. $select = $db->prepare("SELECT status FROM sent WHERE id=:id");
  85. if($select->execute(array(":id" => $id)))
  86. if($f = $select->fetch())
  87. $status = $f['status'];
  88. $maxWait -= 100;
  89. if($maxWait > 0)
  90. usleep(100000);
  91. } while($maxWait > 0 && $status == 0);
  92. echo json_encode(array("id" => $id, "status" => $status));
  93. }
  94. }
  95. }
  96. }
  97. else
  98. error("Gate not found");
  99. ?>