- Renombrado «DC3» a «API Peñasquito» (URL base del documento) en UI/docs; rutas y claves de config conservadas por compatibilidad. - Persistencia de ticket_id en nueva tabla `penasquito_tickets`: POST /search/ guarda el ticket y los siguientes requests retoman GET /result/<ticket_id>/ sin reencolar. - Polling acotado por request HTTP (~6 s) con `PenasquitoPendingException` y caja «reintentar» en lookup; ticket queda válido 1 h. - `users_cache.company` nuevo (con migración idempotente); persistResult guarda empresa + competencias en transacción y aplica source distinto. - Mensajes HTTP específicos (400/401/403/404/408/429/5xx) y `/` final forzado en rutas (DRF), FOLLOWLOCATION habilitado. - Vista de perfil muestra «Empresa» y aviso cuando no hay competencias. - Auth::login evita doble fetch cuando ya tenemos el `result` del API. - i18n ES/EN actualizados; lookup_pending_*; limpieza de archivos lang. Co-authored-by: Cursor <cursoragent@cursor.com>
211 lines
9.4 KiB
PHP
211 lines
9.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use CertiMine\Auth;
|
|
use CertiMine\Config;
|
|
use CertiMine\I18n;
|
|
use CertiMine\MobilisemeClient;
|
|
use CertiMine\PenasquitoDc3Api;
|
|
use CertiMine\PenasquitoPendingException;
|
|
use CertiMine\PersonRepository;
|
|
use CertiMine\Security;
|
|
use CertiMine\Util;
|
|
|
|
require dirname(__DIR__) . '/src/bootstrap.php';
|
|
|
|
$route = isset($_GET['r']) ? (string) $_GET['r'] : 'home';
|
|
|
|
/** @var callable $e */
|
|
$e = static function (string $msg): void {
|
|
http_response_code(404);
|
|
echo '<!DOCTYPE html><html><body><p>Not found</p></body></html>';
|
|
exit;
|
|
};
|
|
|
|
try {
|
|
switch ($route) {
|
|
case 'home':
|
|
$titleKey = 'nav_home';
|
|
require dirname(__DIR__) . '/views/home.php';
|
|
break;
|
|
|
|
case 'frm-library':
|
|
$frms = PersonRepository::frmWithNoms();
|
|
$docs = PersonRepository::publicDocuments();
|
|
$titleKey = 'frm_title';
|
|
require dirname(__DIR__) . '/views/frm_library.php';
|
|
break;
|
|
|
|
case 'lookup':
|
|
$error = null;
|
|
/** @var string|null */
|
|
$lookupErrorDetail = null;
|
|
/** @var array{passport:string,ticket_id:string,status:string,posicion:?int}|null */
|
|
$lookupPending = null;
|
|
$result = null;
|
|
$penasquitoApiOk = PenasquitoDc3Api::isConfigured();
|
|
$envTok = getenv('CERTIMINE_PENASQUITO_TOKEN');
|
|
if (!is_string($envTok) || trim($envTok) === '') {
|
|
$envTok = getenv('CERTIMINE_DC3_TOKEN');
|
|
}
|
|
$penasquitoApiTokenFromEnv = is_string($envTok) && trim($envTok) !== '';
|
|
$lookupUsingExample = !Config::loadedFromLocalFile();
|
|
$penasquitoApiBaseUrl = (string) (Config::get('penasquito_dc3', 'base_url') ?? '');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!Security::verifyCsrf($_POST['_csrf'] ?? null)) {
|
|
$error = I18n::t('csrf_invalid');
|
|
} else {
|
|
$type = (string) ($_POST['person_type'] ?? 'employee');
|
|
$code = trim((string) ($_POST['code'] ?? ''));
|
|
if (!in_array($type, ['employee', 'contractor'], true)) {
|
|
$type = 'employee';
|
|
}
|
|
if ($code === '') {
|
|
$error = I18n::t('no_results');
|
|
} else {
|
|
$result = PersonRepository::findByEmployeeNumberAndType($code, $type);
|
|
if ($result === null) {
|
|
$result = PersonRepository::findByEmployeeNumber($code);
|
|
}
|
|
if ($result !== null) {
|
|
PersonRepository::logAudit('lookup', (string) $result['person_type'], $code);
|
|
$ptype = (string) $result['person_type'];
|
|
$ecode = (string) ($result['employee_number'] ?? $result['mobiliseme_id']);
|
|
$sig = Security::qrSignature($ptype, $ecode);
|
|
header(
|
|
'Location: ' . Util::url('profile', [
|
|
'id' => $result['mobiliseme_id'],
|
|
'sig' => $sig,
|
|
])
|
|
);
|
|
exit;
|
|
}
|
|
if (PenasquitoDc3Api::isConfigured()) {
|
|
try {
|
|
$apiRes = PenasquitoDc3Api::fetchResultForPassport($code);
|
|
$isContractor = PenasquitoDc3Api::guessContractorFromCompanyPublic(
|
|
(string) ($apiRes['company'] ?? '')
|
|
);
|
|
$ptype = $type === 'contractor' || $isContractor ? 'contractor' : 'employee';
|
|
$pid = (string) ($apiRes['passport'] ?? $code);
|
|
PenasquitoDc3Api::persistResult($pid, $apiRes, $ptype);
|
|
PersonRepository::logAudit('lookup_penasquito_api', $ptype, $code);
|
|
$ecode = (string) ($apiRes['passport'] ?? $code);
|
|
$sig = Security::qrSignature($ptype, $ecode);
|
|
header(
|
|
'Location: ' . Util::url('profile', [
|
|
'id' => $pid,
|
|
'sig' => $sig,
|
|
])
|
|
);
|
|
exit;
|
|
} catch (PenasquitoPendingException $pex) {
|
|
$lookupPending = [
|
|
'passport' => $pex->passport,
|
|
'ticket_id' => $pex->ticketId,
|
|
'status' => $pex->remoteStatus,
|
|
'posicion' => $pex->posicion,
|
|
];
|
|
} catch (Throwable $ex) {
|
|
$error = I18n::t('penasquito_api_lookup_error');
|
|
if ($ex instanceof \RuntimeException) {
|
|
$lookupErrorDetail = $ex->getMessage();
|
|
} elseif (Config::debugMode()) {
|
|
$lookupErrorDetail = $ex->getMessage();
|
|
}
|
|
}
|
|
} else {
|
|
$error = I18n::t('lookup_need_dc3');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$titleKey = 'lookup_title';
|
|
require dirname(__DIR__) . '/views/lookup.php';
|
|
break;
|
|
|
|
case 'profile':
|
|
$id = (string) ($_GET['id'] ?? '');
|
|
$sig = isset($_GET['sig']) ? (string) $_GET['sig'] : null;
|
|
if ($id === '') {
|
|
$e('missing id');
|
|
}
|
|
$row = PersonRepository::findByMobilisemeId($id);
|
|
if ($row === null) {
|
|
$e('unknown');
|
|
}
|
|
$code = (string) ($row['employee_number'] ?? $row['mobiliseme_id']);
|
|
$ptype = (string) $row['person_type'];
|
|
if (!Security::verifyQrSignature($ptype, $code, $sig)) {
|
|
http_response_code(403);
|
|
echo '<!DOCTYPE html><html><body><p>Invalid signature</p></body></html>';
|
|
exit;
|
|
}
|
|
$certs = PersonRepository::certificates($id);
|
|
$auths = $ptype === 'contractor' ? PersonRepository::authorizations($id) : [];
|
|
$green = (int) ($row['green_license_flag'] ?? 0) === 1;
|
|
$qrUrl = Util::url('profile', ['id' => $id, 'sig' => Security::qrSignature($ptype, $code)]);
|
|
$titleKey = $ptype === 'contractor' ? 'contractor_profile' : 'employee_profile';
|
|
require dirname(__DIR__) . '/views/profile_public.php';
|
|
break;
|
|
|
|
case 'login':
|
|
$err = null;
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!Security::verifyCsrf($_POST['_csrf'] ?? null)) {
|
|
$err = I18n::t('csrf_invalid');
|
|
} else {
|
|
$u = (string) ($_POST['username'] ?? '');
|
|
$p = (string) ($_POST['password'] ?? '');
|
|
try {
|
|
$login = MobilisemeClient::login($u, $p);
|
|
/** @var array<string,mixed>|null $dr */
|
|
$dr = isset($login['dc3_result']) && is_array($login['dc3_result']) ? $login['dc3_result'] : null;
|
|
Auth::login($login['profile'], $login['token'], $dr);
|
|
header('Location: ' . Util::url('dashboard'));
|
|
exit;
|
|
} catch (Throwable) {
|
|
$err = I18n::t('login_error');
|
|
}
|
|
}
|
|
}
|
|
$titleKey = 'login_title';
|
|
require dirname(__DIR__) . '/views/login.php';
|
|
break;
|
|
|
|
case 'logout':
|
|
Auth::logout();
|
|
header('Location: ' . Util::url('home'));
|
|
exit;
|
|
|
|
case 'dashboard':
|
|
$user = Auth::requireUser();
|
|
$mid = (string) $user['mobiliseme_id'];
|
|
$certs = PersonRepository::certificates($mid);
|
|
$nomIds = PersonRepository::validNomIdsForPerson($mid);
|
|
$courses = PersonRepository::coursesForNoms($nomIds);
|
|
$titleKey = 'nav_dashboard';
|
|
require dirname(__DIR__) . '/views/dashboard.php';
|
|
break;
|
|
|
|
case 'api-docs':
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
$doc = dirname(__DIR__) . '/docs/mobiliseme-api.md';
|
|
if (is_readable($doc)) {
|
|
readfile($doc);
|
|
} else {
|
|
echo "Documentation not found.\n";
|
|
}
|
|
exit;
|
|
|
|
default:
|
|
$e('route');
|
|
}
|
|
} catch (Throwable $ex) {
|
|
http_response_code(500);
|
|
echo '<!DOCTYPE html><html><body><p>Server error</p></body></html>';
|
|
if (getenv('CERTIMINE_DEBUG') === '1') {
|
|
echo '<pre>' . Util::h($ex->getMessage()) . '</pre>';
|
|
}
|
|
}
|