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"); ?>