Files

66 lines
2.8 KiB
PHP

<?php
// ride_server/intaleq/connect.php
// Bootstrap for standalone REST endpoints under ride_server/intaleq/.
// Modeled on loction_server/siro/connect.php (the JWT-enforcing variant).
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/load_env.php';
// .env path mirrors ride_server/passenger_socket.php's own already-deployed
// convention on this box — confirm/adjust with ops at deploy time.
$env_file = file_exists(__DIR__ . '/../ride-keys/.env')
? __DIR__ . '/../ride-keys/.env'
: (file_exists('/home/intaleq-rides/env/.env') ? '/home/intaleq-rides/env/.env' : '');
if (!empty($env_file)) {
loadEnvironment($env_file);
}
require_once __DIR__ . '/functions.php';
// --- CORS + JSON headers ---
header("Access-Control-Allow-Origin: https://intaleqapp.com");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header('Content-Type: application/json');
date_default_timezone_set('Asia/Amman');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// --- JWT auth — enforced. This endpoint returns ride status tied to a
// specific passenger/driver, and backend's connect.php enforces JWT for the
// same call today; dropping auth here would be a regression. ---
$decodedToken = authenticateJWT();
$user_id = $decodedToken->sub ?? $decodedToken->user_id ?? null;
// --- DB connection: same physical database backend's Database::get('main')
// resolves to (confirmed distinct from Database::get('ride') — the getRideStatus
// endpoint this replaces has always read from 'main', so the fallback query
// below stays on 'main' too, for zero behavior change vs. today). Env var
// names match backend/core/Database/Database.php's 'main' entry exactly;
// falling back to loction_server's simpler names only if ops sets this box
// up differently. Actual values must be confirmed/provisioned by ops. ---
$dbname = getenv('DB_PRIMARY_NAME_V2') ?: getenv('dbname');
$dbhost = getenv('DB_PRIMARY_HOST_V2') ?: 'localhost';
$dbuser = getenv('DB_PRIMARY_USER_V2') ?: getenv('USER');
$dbpass = getenv('DB_PRIMARY_PASS_V2') ?: getenv('PASS');
try {
$dsn = "mysql:host=$dbhost;dbname=$dbname;charset=utf8mb4";
$con = new PDO($dsn, $dbuser, $dbpass, [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8",
PDO::ATTR_TIMEOUT => 5,
]);
} catch (PDOException $e) {
error_log("[ride_server/connect] DB connection failed: " . $e->getMessage());
http_response_code(500);
echo json_encode(['status' => 'failure', 'message' => 'A database error occurred.']);
exit;
}