<?php

if (!isset($to_return))
    $to_return = new stdClass();

if (!isset($_GET["token"])) {
    $f = fopen("io", "r");
    if (filesize("io") == 0) {
        $to_return->status = 1;
        $to_return->msg = '';
        echo json_encode($to_return);
        exit();
    }
    $txt = fread($f, filesize("io"));
    $to_return->status = 1;
    $to_return->msg = substr($txt, 1);
    fclose($f);
    $e = isset($_GET["e"]) && !substr($txt, 0, 1) ? $_GET["e"] : 0;
    if ($e == 1) {
        file_put_contents('io', '');
    }
    echo json_encode($to_return);
    exit();
}

$pwd = "%PASSWORD%";

if ($_GET["token"] == $pwd) {
    $f = fopen("io", "w");
    $txt = $_GET["io"];
    $m = isset($_GET["m"]) ? $_GET["m"] : 0;
    fwrite($f, $m . $txt);
    $to_return->status = 1;
    $e = isset($_GET["e"]) ? $_GET["e"] : 0;
    if ($e == 1) {
        file_put_contents('io', '');
    }
    echo json_encode($to_return);
    exit();
};

$to_return->status = 0;
$to_return->error = "token invalid or expired: " . $diff;

echo json_encode($to_return);
