More detailed setup and readme. Currently missing templates, or some kind of default behaviour

This commit is contained in:
Maximilian Giller 2023-03-05 02:02:46 +01:00
parent d8c9e3d388
commit d476d77340
7 changed files with 161 additions and 76 deletions

41
README.md Normal file
View file

@ -0,0 +1,41 @@
# My Status
Simple script to store and update a personal status. The current status can be publicly accessed and referenced
elsewhere.
The status can only be updated with a valid secret key. The current status is stored in a simple json file.
## Setup
Make sure the config file contains a good key, that the key is usable in a GET request and that the specified storage
file is writable.
The storage file is created if it does not exist. Since the storage file does not contain any sensitive information, it
can be made publicly accessible. At least it contains nothing that is not already available over the status itself.
## Usage
### Get status
Simple GET request to the `index.php`.
The status is returned as a json object. Some information is dynamically updated. Durations are usually given in seconds
and timestamps as UNIX timestamps in seconds.
### Update status
For simplicity, this also consists of a simple GET request. The secret key must be passed as a GET parameter and is the
only required parameter. All additional parameters are optional. They can and should be used to specify the status to
your liking.
Here is an overview:
| Parameter | Description | Example |
|---------------|-------------------------------------------------------------------|-----------------------------------------------------|
| `title` | The title of the status. | `title=Working` |
| `description` | A description of the status. | `description=Making progress on personal projects.` |
| `duration` | The expected duration of the status in seconds. | `duration=3600` |
| `available` | Whether you are available to other people. | `available=0` |
| `working` | Whether you are working, as opposed to having some personal time. | `working=1` |

View file

@ -1,76 +0,0 @@
<?php
// Config
$SECRET_KEY = "ENTER_SECRET_HERE";
$FILENAME = "last_activity.txt";
$SEPARATOR = " ";
$IDLE_DURATION_THRESHOLD = 60 * 30; // In seconds
$SLEEP_DURATION_THRESHOLD = 60 * 60 * 3; // In seconds
$EXPECTED_SLEEP_DURATION = 60 * 60 * 9.5; // In seconds
function storeActivity($time, $activity = "")
{
global $FILENAME, $SEPARATOR;
$file = fopen($FILENAME, "w");
fwrite($file, $time . $SEPARATOR . $activity);
fclose($file);
}
function getActivity()
{
global $FILENAME, $SEPARATOR;
$file = fopen($FILENAME, "r");
$line = fgets($file);
fclose($file);
$parts = explode($SEPARATOR, $line, 2);
return array(
"time" => (int)$parts[0],
"activity" => $parts[1]
);
}
// Is secret given?
if (isset($_GET['secret']) && $_GET['secret'] == $SECRET_KEY) {
$activity = isset($_GET['activity']) ? $_GET['activity'] : "";
// Update the file
storeActivity(time(), $activity);
echo "Last activity updated";
die();
}
// Current status requested
$last_activity = getActivity();
$response = array(
"last_activity" => $last_activity['time'],
"status" => $last_activity['activity'],
);
// Estimate current activity
$now = time();
$diff = $now - $last_activity["time"]; // DIFFERENCE IN SECONDS
if ($response['status'] == "") {
if ($diff >= $SLEEP_DURATION_THRESHOLD) {
$response['status'] = "Sleeping";
} else if ($diff >= $IDLE_DURATION_THRESHOLD) {
$response['status'] = "Awake";
} else {
$response['status'] = "Busy";
}
}
// Estimated time until wake up
if (strtolower($response['status']) == "sleeping") {
$response['time_until_wake'] = $EXPECTED_SLEEP_DURATION - $diff;
} else {
$response['time_until_wake'] = 0;
}
// Respond with JSON
header('Content-Type: application/json');
echo json_encode($response);

37
src/activity.php Normal file
View file

@ -0,0 +1,37 @@
<?php
class Activity
{
public int $startTime; // Unix timestamp in seconds
public int $expectedDuration; // In seconds, 0 if open-ended
public string $title;
public string $description; // Additional information, but might be kept empty
public bool $available; // Am I available for requests and to talk?
public bool $working; // Am I working on something?
public function getCurrentDuration(): int
{
return time() - $this->startTime;
}
public function getRemainingDuration(): int
{
return $this->expectedDuration - $this->getCurrentDuration();
}
public function getPublicData(): array
{
return array(
"startTime" => $this->startTime,
"expectedDuration" => $this->expectedDuration,
"title" => $this->title,
"description" => $this->description,
"available" => $this->available,
"working" => $this->working,
"currentDuration" => $this->getCurrentDuration(),
"remainingDuration" => $this->getRemainingDuration(),
);
}
}

4
src/config.php Normal file
View file

@ -0,0 +1,4 @@
<?php
$SECRET_KEY = "ENTER_SECRET_HERE";
$STORAGE_FILE = "last_activity.json";

13
src/http.php Normal file
View file

@ -0,0 +1,13 @@
<?php
function respondWithJson($data): void
{
header('Content-Type: application/json');
echo json_encode($data);
}
function respondAndDie(string $message): void
{
echo $message;
die();
}

47
src/index.php Executable file
View file

@ -0,0 +1,47 @@
<?php
require_once("config.php");
require_once("storage.php");
require_once("activity.php");
require_once("http.php");
// If no secret key is provided, respond with the current activity
global $SECRET_KEY;
if (!isset($_GET["secret"]) || $_GET["secret"] != $SECRET_KEY) {
// Respond with the current activity
$activity = loadActivity();
respondWithJson($activity->getPublicData());
}
// Update activity
$activity = new Activity();
$activity->startTime = time();
if (isset($_GET["title"])) {
$activity->title = $_GET["title"];
} else {
$activity->title = "Awake";
}
if (isset($_GET["description"])) {
$activity->description = $_GET["description"];
} else {
$activity->description = "";
}
if (isset($_GET["duration"])) {
$activity->expectedDuration = (int)$_GET["duration"];
} else {
$activity->expectedDuration = 0;
}
if (isset($_GET["available"])) {
$activity->available = filter_var($_GET["available"], FILTER_VALIDATE_BOOLEAN);
} else {
$activity->available = false;
}
if (isset($_GET["working"])) {
$activity->working = filter_var($_GET["working"], FILTER_VALIDATE_BOOLEAN);
} else {
$activity->working = false;
}
storeActivity($activity);
respondAndDie("Activity updated");

19
src/storage.php Normal file
View file

@ -0,0 +1,19 @@
<?php
require_once("config.php");
function storeActivity(Activity $activity): void
{
global $STORAGE_FILE;
$file = fopen($STORAGE_FILE, "w");
fwrite($file, json_encode($activity));
fclose($file);
}
function loadActivity(): Activity
{
global $STORAGE_FILE;
$file = fopen($STORAGE_FILE, "r");
$line = fgets($file);
fclose($file);
return json_decode($line);
}