function success($message = "success", $status = false) { if ($status) { REST::response_code($status); } elseif (REST::response_code() >= 300) { REST::response_code(200); } if (REST::preferred("text/html")) { echo '<div class="container"><div class="alert alert-success">' . $message . '</div></div>'; } else { echo $message; } }
if (!$auth_info || !isset($auth_info[0])) { return error(401, "Unauthorized!"); } // POST // $id = fetch("SELECT MAX(RID)+1 as id FROM reports")[0]["id"]; if (!$id) { $id = 1; } $content = file_get_contents("php://input"); global $db; $q = $db->prepare("INSERT INTO reports (RID, DID, Content) VALUES (:report_id, :device_id, :content)"); if (!$q->execute(array(":report_id" => $id, ":device_id" => $device_id, ":content" => $content))) { return error("failure", "Failed to post the report."); } // REPLY / RENDER // REST::response_code("created"); header('Location: ' . lnk("/reports/{$id}")); if (!REST::preferred("text/html")) { return success("Successfully posted the report."); } else { echo '<script type="text/javascript">window.location.href="' . lnk("/reports/{$id}") . '"</script>"'; } } else { return error("bad-method", "Unsupported HTTP Method"); } } handle_item_commands: if (count(REST::$ARGS) == 3 && REST::$ARGS[2] == "commands") { if (REST::$REQUEST_METHOD == "GET") { $device_id = REST::$ARGS[1]; if (isset($_REQUEST["since"])) {
} echo "Device ID:\t" . $device["id"] . "\n"; echo "Name:\t" . $device["name"] . "\n"; echo "Address:\t" . $device["address"] . "\n"; echo "Last heard:\t" . $device["lastheard"] . "\n"; echo "Reports:\t" . $device["reportcount"] . "\n"; } elseif (!REST::preferred("text/html")) { if (!$device) { REST::response_code("not-found"); echo "NULL"; return; } echo json_encode($device); } else { if (!$device) { REST::response_code("not-found"); ?> <?php echo_breadcrumbs_bar(array(lnk("/devices") => "Devices", "Not found")); ?> <div class="main"> <div class="container"> <div class="title"> Sorry, the device was not found! </div> </div> </div> <?php
<?php global $devices; if (!isset($devices)) { return error(500, "Illegal state!"); } if (count($devices) == 0) { REST::response_code("empty"); } // Plain text representation // if (!REST::preferred("text/html") && !REST::preferred("application/json")) { foreach ($devices as $device) { echo "(" . $device["id"] . ") " . $device["name"] . "\n"; } // JSON representation // } elseif (!REST::preferred("text/html")) { foreach ($devices as &$device) { $device["link"] = lnk("/devices/" . $device["id"]); unset($device["address"]); } echo json_encode($devices); // HTML representation // } else { ?> <div class="main"> <div class="container"> <div class="title">Registered devices:</div> <ul class="device-list"> <?php