spacebox/src/session_receiver.php
Cocktail Frank ac72b8cb84 Add function for sending play session data over HTTP
Add a function to the `sb:☁️:HTTP` class that sends session data
collected by a `sb::progress::Progress` object to a configured URL.

Add a PHP script for receiving the session data, intended to be
installed separately on a remote server.

Add a randomly generated ID when writing new save files. Do not
overwrite existing IDs, so the ID will persist for the life of the
file. Store the ID in the play session data.

Add a string identifying the program's build, either "windows",
"linux", "macos", "wasm", or "android".

Add function for accessing requests in the HTTP queue.

Add the `sb::math` namespace for functions in math.hpp.

Add open source SHA256 hash library to the project. Use the library to
hash the player's Steam username before saving it to the session data.

Add documentation for `sb:☁️:http::post_session` to README.

Add a function for getting a string representing the current date up to
the minute to `sb::time`. This function is used to store the start time
of the play session.

Add test of session transfer to HTTP test case.
2025-04-16 20:06:07 -04:00

76 lines
2.3 KiB
PHP

/*!
* Receive a POST request containing player analytics data from a play session.
*
* Add IP address information and write the data to a JSON file, one file per play session ID.
*/
<?php
/*!
* Save space by writing at most 7 significant figures for floating point stats.
*/
ini_set("serialize_precision", 7);
/*!
* Read JSON data containing the session data passed as a POST request. Limit to 2048 characters to protect against
* injections.
*/
$play_session_data = json_decode(file_get_contents("php://input", false, null, 0, 2048), true);
/*!
* Save the IP address, hashed for security. The "forwarded for" field is useful in case the IP address represents a
* large network of computers.
*/
$play_session_data["ip address hash"] = hash("crc32", $_SERVER["REMOTE_ADDR"]);
if (array_key_exists("HTTP_X_FORWARDED_FOR", $_SERVER))
{
$play_session_data["forwarded for hash"] = hash("crc32", $_SERVER["HTTP_X_FORWARDED_FOR"]);
}
/*!
* Root directory for the file defaults to the current directory. Set this to another folder to store data in a separate
* location. This folder must have write permission for the web server user (usually "www-data").
*/
$root = "";
/*!
* Store all play session data in a sub-folder.
*/
$subfolder = "sessions";
/*!
* Give a name to platform if the field is empty.
*/
if ($play_session_data["platform"] == "")
{
$play_session_data["platform"] = "unspecified";
}
/*!
* Sort into folder hierarchy within sessions folder: [game]/[platform]/[save file ID]/[session].json. Create folder
* hierarchy if necessary.
*/
$directory_path = $root . "/" . $subfolder . "/" . $play_session_data["game title"] . "/" .
$play_session_data["platform"] . "/" . $play_session_data["save data id"];
if (!file_exists($directory_path))
{
mkdir($directory_path, 0775, true);
}
/*!
* The file name is the session start time followed by session ID.
*/
$file_name = $play_session_data["start time"] . "_" . $play_session_data["id"] . ".json";
/*!
* The full path to the file.
*/
$play_session_path = $directory_path . "/" . $file_name;
/*!
* Write the play session data to a file. Overwrite existing data because only one file is kept per play session.
*/
file_put_contents($play_session_path, json_encode($play_session_data, JSON_PRETTY_PRINT) . "\n");
?>