| Current Path : /home/happyrenas/fun/public/api/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/happyrenas/fun/public/api/_bootstrap.php |
<?php
/**
* BOOTSTRAP
* - Charge la config (IDs Sheets / onglets)
* - Initialise Google Client (Service Account)
* - Prépare cache + rate limit
*/
require_once __DIR__ . '/../../vendor/autoload.php';
$PROJECT_ROOT = dirname(__DIR__, 2); // remonte depuis /public/api -> racine projet
$CONFIG_PATH = $PROJECT_ROOT . '/config/config.php';
if (!file_exists($CONFIG_PATH)) {
http_response_code(500);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok'=>false,'error'=>'config.php introuvable (chemin attendu: /config/config.php)']);
exit;
}
$config = require $CONFIG_PATH;
require_once __DIR__ . '/_helpers.php';
require_once __DIR__ . '/_cache.php';
require_once __DIR__ . '/_rate_limit.php';
init_dirs($config);
function google_sheets_service(array $config): Google_Service_Sheets {
static $service = null;
if ($service) return $service;
$jsonPath = $config['google']['service_account_json_path'] ?? '';
if (!$jsonPath || !file_exists($jsonPath)) {
json_error(500, "Clé service account introuvable (attendu: {$jsonPath})");
}
$client = new Google_Client();
$client->setApplicationName($config['google']['application_name'] ?? 'App');
$client->setAuthConfig($jsonPath);
$client->setScopes([Google_Service_Sheets::SPREADSHEETS]);
$service = new Google_Service_Sheets($client);
return $service;
}
function sheets_get_all(string $spreadsheetId, string $sheetName, array $config, int $ttl = null): array {
$ttl = $ttl ?? ($config['cache']['ttl_seconds'] ?? 1200);
$key = "all:{$spreadsheetId}:{$sheetName}";
$cached = cache_get($key, $config);
if ($cached !== null) return $cached;
$service = google_sheets_service($config);
$resp = $service->spreadsheets_values->get($spreadsheetId, $sheetName);
$values = $resp->getValues() ?: [];
cache_set($key, $values, $ttl, $config);
return $values;
}
function table_from_values(array $values): array {
if (count($values) < 1) return ['headers'=>[], 'rows'=>[]];
$headers = array_map('trim', $values[0]);
$rows = [];
for ($i=1; $i<count($values); $i++) {
$line = $values[$i];
if (!$line || (count($line) === 1 && trim((string)$line[0]) === '')) continue;
$row = [];
foreach ($headers as $c => $h) {
$row[$h] = isset($line[$c]) ? (string)$line[$c] : '';
}
$rows[] = $row;
}
return ['headers'=>$headers, 'rows'=>$rows];
}
function find_row_by_col(array $rows, string $col, string $value): ?array {
foreach ($rows as $r) {
if ((string)($r[$col] ?? '') === (string)$value) return $r;
}
return null;
}