<?php

include 'door-settings.php';

if (!isset($_REQUEST['door_id'])) die('Please send door id');

$id = intval($_REQUEST['door_id']);
if ($id < 1 || $id > NUM_DOORS) die('Invalid door id');

if (!isset($_REQUEST['status'])) die('Please send door status');

$status = intval($_REQUEST['status']);
if ($status != 0 && $status != 1) die ('Invalid status');

$verify = mt_rand();

$pub = file_get_contents("public$id.pem");
if ($pub === false) die("No public key found for door $id");

$request_file = "request$id";
$ip = $_SERVER['HTTP_X_REAL_IP'];

$time = time();

file_put_contents($request_file, "$ip\n$time\n$verify\n$status");

$ret = openssl_public_encrypt("$verify", $encrypted, $pub, OPENSSL_PKCS1_OAEP_PADDING);

if (!$ret) die("Encryption error: ". openssl_error_string());
echo htmlentities(base64_encode($encrypted));
