59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
/**
|
|
* Workout History API
|
|
* GET /api/v1/history.php
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-API-Key, X-Signature, X-Timestamp');
|
|
require_once __DIR__ . '/Database.php';
|
|
require_once __DIR__ . '/Config.php';
|
|
require_once __DIR__ . '/AuthenticationHandler.php';
|
|
require_once __DIR__ . '/JwtToken.php';
|
|
require_once __DIR__ . '/ApiAuth.php';
|
|
require_once dirname(__DIR__) . '/public/api/v1/_bootstrap.php';
|
|
|
|
try {
|
|
$db = Database::getInstance();
|
|
if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/^Bearer\s+/i', $_SERVER['HTTP_AUTHORIZATION'])) {
|
|
$user_id = ApiAuth::bearerClaims()['user_id'];
|
|
} else {
|
|
AppConfig::loadEnvironment();
|
|
if (getenv('LEGACY_HMAC_ENABLED') !== 'true') {
|
|
throw new Exception('Bearer authentication required', 401);
|
|
}
|
|
$api_key = $_SERVER['HTTP_X_API_KEY'] ?? null;
|
|
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? null;
|
|
$timestamp = $_SERVER['HTTP_X_TIMESTAMP'] ?? null;
|
|
if (!$api_key || !$signature || !$timestamp) {
|
|
throw new Exception('Unauthorized', 401);
|
|
}
|
|
$auth = new AuthenticationHandler();
|
|
$authResult = $auth->validateHmacSignature($api_key, $signature, '', $timestamp);
|
|
if (!$authResult['valid']) {
|
|
throw new Exception($authResult['error'], 401);
|
|
}
|
|
$user_id = $authResult['user_id'];
|
|
}
|
|
|
|
// Fetch workouts
|
|
$stmt = $db->prepare('SELECT * FROM workouts WHERE user_id = ? ORDER BY created_at DESC LIMIT 50');
|
|
$stmt->bind_param('i', $user_id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
$workouts = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$workouts[] = $row;
|
|
}
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'data' => $workouts
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code($e->getCode() ?: 500);
|
|
echo json_encode(['status' => 'error', 'error' => $e->getMessage()]);
|
|
}
|