Add initial php data

This commit is contained in:
zontreck 2025-05-15 22:48:36 -07:00
parent 220a317d31
commit 7e78ed323d
2 changed files with 84 additions and 0 deletions

83
server/php/timetrack.php Normal file
View file

@ -0,0 +1,83 @@
<?php
if(!defined("COMMON"))
require ("/srv/sites/api.zontreck.com/Common.php");
// Get Database: timetrack
$DB = get_DB("timetrack");
// Get query information
$jsx = json_decode(file_get_contents("php://input"), true);
// Get operation information
switch($jsx['cmd']) {
case "create": {
// Get UUID from MySQL and insert into sessions table
$result = $DB->query("SELECT UUID() AS id");
if (!$result) {
http_response_code(500);
echo json_encode(["error" => "Failed to generate UUID"]);
break;
}
$row = $result->fetch_assoc();
$sessionId = $row['id'];
// Insert into `sessions` table
$stmt = $DB->prepare("INSERT INTO `sessions` (`ID`, `timestamp`) VALUES (?, CURRENT_TIMESTAMP())");
$stmt->bind_param("s", $sessionId);
if (!$stmt->execute()) {
http_response_code(500);
echo json_encode(["error" => "Failed to insert into sessions"]);
break;
}
$stmt->close();
// Prepare data as JSON and insert into `data` table
$data = json_encode($jsx['data']);
$stmt = $DB->prepare("INSERT INTO `data` (`ID`, `SessionData`) VALUES (?, ?)");
$null = NULL; // Required for bind_param with blob
$stmt->bind_param("sb", $sessionId, $null); // Temporarily bind $null for blob
// Send the actual blob content using send_long_data
$stmt->send_long_data(1, $data); // Index 1 refers to the second "?" in bind_param
if (!$stmt->execute()) {
http_response_code(500);
echo json_encode(["error" => "Failed to insert into data"]);
break;
}
$stmt->close();
echo json_encode(["status" => "ok", "session" => $sessionId]);
break;
}
case "get": {
$sessionId = $jsx['id'];
$stmt = $DB->prepare("SELECT `SessionData` FROM `data` WHERE `ID` = ?");
$stmt->bind_param("s", $sessionId);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 0) {
http_response_code(404);
echo json_encode(["error" => "Session not found"]);
$stmt->close();
break;
}
$stmt->bind_result($sessionData);
$stmt->fetch();
$stmt->close();
// Decode the JSON blob (optional — if you want raw JSON output)
header("Content-Type: application/json");
echo $sessionData;
break;
}
}
?>