Raumstatus/room.php

Wechseln zu: Navigation, Suche

zurück zu Raumstatus <highlightsyntax lang="php"><?php

  1. API Key

$API_KEY = "APIKEY";

  1. room status file

$ROOM_STATUS_FILE = "/var/www/room_status.txt";

  1. timeout in minutes when to mark room closed (after last "open message")

$ROOM_TIMEOUT = 20;

  1. lower people limit for status "open"

$PEOPLE_LIMIT = 3;

  1. lower people limit for status "open" on fridays after 19.00

$PEOPLE_LIMIT_FR = 1;

  1. FUNCTIONS

function isHttps() { return ($_SERVER["HTTPS"] != ""); }

function isAuthd($apikey) { global $API_KEY; return ($apikey == $API_KEY); }

function determineStatus($openTime, $people) { global $ROOM_TIMEOUT, $PEOPLE_LIMIT, $PEOPLE_LIMIT_FR;

$open = ($openTime > (time() - $ROOM_TIMEOUT*60));

# on friday evenings, use special rule $fridayNights = ( date("N", $openTime) == 5 && date("G", $openTime) >= 18 ) || ( date("N", $openTime) == 6 && date("G", $openTime) <= 3 ); $enoughPeople = ( ($fridayNights && $PEOPLE_LIMIT_FR) || $people >= $PEOPLE_LIMIT); return ($open && $enoughPeople); }

function getStatus() { global $ROOM_STATUS_FILE, $ROOM_TIMEOUT;

# GET REQUEST - Get status $fp = fopen($ROOM_STATUS_FILE, "r"); $lastOpenSignal = intval(fgets($fp)); $lastStatusSignal = intval(fgets($fp)); $people = intval(fgets($fp)); fclose($fp);

if (determineStatus($lastOpenSignal, $people)) { $status = "open"; $since = $lastStatusSignal; } else { $status = "closed"; $since = ($lastOpenSignal != 0) ? $lastOpenSignal + $ROOM_TIMEOUT*60 : $lastOpenSignal; }

return array("roomStatus" => $status, "since" => $since); }


  1. POST REQUEST - set Status

if (count($_POST) > 0) {

if (!isHttps()) { $returnArr = array("success" => false, "reason" => "Please use SSL.");

} else if (!isset($_POST["apikey"])) { $returnArr = array("success" => false, "reason" => "Please provide API key.");

} else if (!isset($_POST["people"])) { $returnArr = array("success" => false, "reason" => "Please provide number of people online.");

} else if (!isAuthd($_POST["apikey"])) { $returnArr = array("success" => false, "reason" => "Please provide *correct* API key.");

} else if (determineStatus(time(), $_POST["people"])) {

$fp = @fopen($ROOM_STATUS_FILE, "r"); if ($fp) { $lastOpenSignal = intval(fgets($fp)); $lastStatusSignal = intval(fgets($fp)); $people = intval(fgets($fp)); fclose($fp); } else { $lastOpenSignal = 0; $lastStatusSignal = 0; $people = 0; }

# status changed? if (determineStatus($lastOpenSignal, $people) != determineStatus(time(), $_POST["people"])) { $writeContent = time()."\n".time()."\n".intval($_POST["people"]); } else { $writeContent = time()."\n".$lastStatusSignal."\n".intval($_POST["people"]); }

$fp = fopen($ROOM_STATUS_FILE, "w"); fputs($fp, $writeContent); fclose($fp);

$returnArr = array("success" => true);

} else { # people limit not reached, but thx anyway $returnArr = array("success" => true);

}

echo json_encode($returnArr);

} else {

echo json_encode(getStatus()); } ?> </highlightsyntax>