mirror of
https://github.com/Doodle3D/doodle3d-connect.git
synced 2024-12-25 01:53:48 +01:00
REST like interface (json responses), auto refreshing index page
This commit is contained in:
parent
c28818c46f
commit
f5a09e7c0f
11
connect.php
11
connect.php
@ -6,7 +6,12 @@
|
|||||||
$password = "mysql";
|
$password = "mysql";
|
||||||
$table = "signins";
|
$table = "signins";
|
||||||
|
|
||||||
$db = new PDO($dsn, $username, $password);
|
try {
|
||||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$db = new PDO($dsn, $username, $password);
|
||||||
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$response = array( "status" => "error",
|
||||||
|
"msg" => $e->getMessage()." (".$e->getCode().")");
|
||||||
|
exit(json_encode($response));
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -1,15 +1,3 @@
|
|||||||
<?php
|
|
||||||
require 'connect.php';
|
|
||||||
|
|
||||||
$hourago = time() - 60*60;
|
|
||||||
|
|
||||||
$remoteip = getenv('REMOTE_ADDR');
|
|
||||||
|
|
||||||
$statement = $db->prepare("SELECT * FROM $table where date >= FROM_UNIXTIME(:hourago) AND remoteip = :remoteip");
|
|
||||||
$statement->execute(array( ':hourago' => $hourago,
|
|
||||||
':remoteip' => $remoteip));
|
|
||||||
$boxes = $statement->fetchAll();
|
|
||||||
?>
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@ -24,15 +12,13 @@
|
|||||||
|
|
||||||
<link href="css/normalize.css" rel="stylesheet" media="screen">
|
<link href="css/normalize.css" rel="stylesheet" media="screen">
|
||||||
<link href="css/main.css" rel="stylesheet" media="screen">
|
<link href="css/main.css" rel="stylesheet" media="screen">
|
||||||
|
|
||||||
|
<script src="js/libs/jquery-1.8.3.min.js" type="text/javascript"></script>
|
||||||
|
<script src="js/main.js" type="text/javascript"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<p>Hi, we found the following Doodle3D WiFi boxes near you:</p>
|
<p>Hi, we found the following Doodle3D WiFi boxes near you:</p>
|
||||||
<ul>
|
<ul id="list">
|
||||||
<?php
|
|
||||||
foreach($boxes as $box) {
|
|
||||||
echo "<li><a href='http://".$box['localip']."' >".$box['wifiboxid']."</a></li>";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
2
js/libs/jquery-1.8.3.min.js
vendored
Normal file
2
js/libs/jquery-1.8.3.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
35
js/main.js
Normal file
35
js/main.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
var retrieveListDelay; // retry setTimout instance
|
||||||
|
var retrieveListInterval = 5000;
|
||||||
|
var $list;
|
||||||
|
|
||||||
|
$(function() {
|
||||||
|
//console.log("ready");
|
||||||
|
retrieveList();
|
||||||
|
|
||||||
|
$list = $("#list");
|
||||||
|
})
|
||||||
|
function retrieveList() {
|
||||||
|
$.ajax({
|
||||||
|
url: "/list.php",
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(response){
|
||||||
|
//console.log("retrieveList response: ",response);
|
||||||
|
if(response.status == "success") {
|
||||||
|
updateList(response.data);
|
||||||
|
}
|
||||||
|
clearTimeout(retrieveListDelay);
|
||||||
|
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval);
|
||||||
|
}
|
||||||
|
}).fail(function() {
|
||||||
|
//console.log("retrieveList: failed");
|
||||||
|
clearTimeout(retrieveListDelay);
|
||||||
|
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval); // retry after delay
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function updateList(boxes) {
|
||||||
|
//console.log("list: ",boxes);
|
||||||
|
$list.empty();
|
||||||
|
jQuery.each(boxes, function (index,box) {
|
||||||
|
$list.append("<li><a href='/"+box.localip+"'>"+box.wifiboxid+"</a></li>");
|
||||||
|
});
|
||||||
|
}
|
21
list.php
Normal file
21
list.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
require 'connect.php';
|
||||||
|
|
||||||
|
$hourago = time() - 60*60;
|
||||||
|
$remoteip = getenv('REMOTE_ADDR');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$statement = $db->prepare("SELECT * FROM $table where date >= FROM_UNIXTIME(:hourago) AND remoteip = :remoteip");
|
||||||
|
$statement->execute(array( ':hourago' => $hourago,
|
||||||
|
':remoteip' => $remoteip));
|
||||||
|
$boxes = $statement->fetchAll();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$response = array( "status" => "error",
|
||||||
|
"msg" => $e->getMessage()." (".$e->getCode().")");
|
||||||
|
exit(json_encode($response));
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = array( "status" => "success",
|
||||||
|
"data" => $boxes);
|
||||||
|
exit(json_encode($response));
|
||||||
|
?>
|
59
signin.php
59
signin.php
@ -1,42 +1,59 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] != "POST") return;
|
if ($_SERVER['REQUEST_METHOD'] != "POST") {
|
||||||
|
$response = array( "status" => "error",
|
||||||
|
"msg" => "'signin' can only be accessed with the POST method");
|
||||||
|
exit(json_encode($response));
|
||||||
|
}
|
||||||
|
|
||||||
require 'connect.php';
|
require 'connect.php';
|
||||||
|
|
||||||
if(!isset($_POST['localip'])) echo "missing localip </br>";
|
if(!isset($_POST['localip'])) echo "missing localip </br>";
|
||||||
$localip = $_POST['localip'];
|
$localip = $_POST['localip'];
|
||||||
echo "localip: $localip </br>";
|
|
||||||
|
|
||||||
if(!isset($_POST['wifiboxid'])) echo "missing wifiboxid </br>";
|
if(!isset($_POST['wifiboxid'])) echo "missing wifiboxid </br>";
|
||||||
$wifiboxid = $_POST['wifiboxid'];
|
$wifiboxid = $_POST['wifiboxid'];
|
||||||
echo "wifiboxid: $wifiboxid </br>";
|
|
||||||
|
|
||||||
$remoteip = getenv('REMOTE_ADDR');
|
$remoteip = getenv('REMOTE_ADDR');
|
||||||
echo "remoteip: $remoteip </br>";
|
|
||||||
|
|
||||||
$timestamp = time();
|
$timestamp = time();
|
||||||
echo "timestamp: $timestamp </br>";
|
|
||||||
|
|
||||||
$id = $remoteip.'/'.$wifiboxid;
|
$id = $remoteip.'/'.$wifiboxid;
|
||||||
echo "id: $id </br>";
|
|
||||||
|
|
||||||
$statement = $db->prepare( "REPLACE INTO $table " .
|
try {
|
||||||
"SET id = :id, " .
|
$statement = $db->prepare( "REPLACE INTO $table " .
|
||||||
" remoteip = :remoteip, " .
|
"SET id = :id, " .
|
||||||
" localip = :localip, " .
|
" remoteip = :remoteip, " .
|
||||||
" wifiboxid = :wifiboxid, " .
|
" localip = :localip, " .
|
||||||
" date = FROM_UNIXTIME(:timestamp)");
|
" wifiboxid = :wifiboxid, " .
|
||||||
$statement->execute(array( ':id' => $id,
|
" date = FROM_UNIXTIME(:timestamp)");
|
||||||
':remoteip' => $remoteip,
|
$statement->execute(array( ":id" => $id,
|
||||||
':localip' => $localip,
|
":remoteip" => $remoteip,
|
||||||
':wifiboxid' => $wifiboxid,
|
":localip" => $localip,
|
||||||
':timestamp' => $timestamp));
|
":wifiboxid" => $wifiboxid,
|
||||||
|
":timestamp" => $timestamp));
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$response = array( "status" => "error",
|
||||||
|
"msg" => $e->getMessage()." (".$e->getCode().")");
|
||||||
|
exit(json_encode($response));
|
||||||
|
}
|
||||||
|
|
||||||
|
$responseData = array( "remoteip" => $remoteip,
|
||||||
|
"localip" => $localip,
|
||||||
|
"wifiboxid" => $wifiboxid,
|
||||||
|
"timestamp" => $timestamp);
|
||||||
|
$response = array( "status" => "success",
|
||||||
|
"data" => $responseData);
|
||||||
|
exit(json_encode($response));
|
||||||
|
|
||||||
// Remove old signins
|
// Remove old signins
|
||||||
$hourago = time() - 60*60;
|
$hourago = time() - 60*60;
|
||||||
echo "hourago: $hourago (time: ".time().")</br>";
|
try {
|
||||||
|
$statement = $db->prepare("DELETE FROM $table WHERE date < FROM_UNIXTIME(:hourago)");
|
||||||
$statement = $db->prepare("DELETE FROM $table WHERE date < FROM_UNIXTIME(:hourago)");
|
$statement->execute(array( ':hourago' => $hourago));
|
||||||
$statement->execute(array( ':hourago' => $hourago));
|
} catch (PDOException $e) {
|
||||||
|
$response = array( "status" => "error",
|
||||||
|
"msg" => $e->getMessage()." (".$e->getCode().")");
|
||||||
|
exit(json_encode($response));
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user