From d476d773408661c268ebef1a47bb1d12fe5f738b Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 5 Mar 2023 02:02:46 +0100 Subject: [PATCH] More detailed setup and readme. Currently missing templates, or some kind of default behaviour --- README.md | 41 ++++++++++++++++++++++++++ index.php | 76 ------------------------------------------------ src/activity.php | 37 +++++++++++++++++++++++ src/config.php | 4 +++ src/http.php | 13 +++++++++ src/index.php | 47 ++++++++++++++++++++++++++++++ src/storage.php | 19 ++++++++++++ 7 files changed, 161 insertions(+), 76 deletions(-) create mode 100644 README.md delete mode 100755 index.php create mode 100644 src/activity.php create mode 100644 src/config.php create mode 100644 src/http.php create mode 100755 src/index.php create mode 100644 src/storage.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..c8ea1d5 --- /dev/null +++ b/README.md @@ -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` | + + diff --git a/index.php b/index.php deleted file mode 100755 index 5fc4f0d..0000000 --- a/index.php +++ /dev/null @@ -1,76 +0,0 @@ - (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); - - diff --git a/src/activity.php b/src/activity.php new file mode 100644 index 0000000..f3eb5cb --- /dev/null +++ b/src/activity.php @@ -0,0 +1,37 @@ +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(), + ); + } +} \ No newline at end of file diff --git a/src/config.php b/src/config.php new file mode 100644 index 0000000..f3de2d6 --- /dev/null +++ b/src/config.php @@ -0,0 +1,4 @@ +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"); diff --git a/src/storage.php b/src/storage.php new file mode 100644 index 0000000..cb71e2c --- /dev/null +++ b/src/storage.php @@ -0,0 +1,19 @@ +