certimine/public/index.php
remaster a72656f9d4 CertiMine: aplicación PHP, API DC3 Peñasquito y documentación
- Portal bilingüe (Bootstrap), biblioteca FRM, consulta y perfiles con QR.
- Integración REST penasquito.wotbi.com (Token, búsqueda por passport y polling).
- Webhook de sincronización, esquema MariaDB y semillas FRM/NOM.
- Documentación en docs/ y extracto DatosPdf.txt; ejemplo de config sin secretos locales.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 17:56:36 -06:00

188 lines
8.1 KiB
PHP

<?php
declare(strict_types=1);
use CertiMine\Auth;
use CertiMine\Config;
use CertiMine\I18n;
use CertiMine\MobilisemeClient;
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;
$result = null;
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 (\CertiMine\PenasquitoDc3Api::isConfigured()) {
try {
$apiRes = \CertiMine\PenasquitoDc3Api::fetchResultForPassport($code);
$isContractor = \CertiMine\PenasquitoDc3Api::guessContractorFromCompanyPublic(
(string) ($apiRes['company'] ?? '')
);
$ptype = $type === 'contractor' || $isContractor ? 'contractor' : 'employee';
$pid = (string) ($apiRes['passport'] ?? $code);
\CertiMine\PenasquitoDc3Api::persistResult($pid, $apiRes, $ptype);
PersonRepository::logAudit('lookup_dc3_api', $ptype, $code);
$ecode = (string) ($apiRes['passport'] ?? $code);
$sig = Security::qrSignature($ptype, $ecode);
header(
'Location: ' . Util::url('profile', [
'id' => $pid,
'sig' => $sig,
])
);
exit;
} catch (Throwable $ex) {
$error = I18n::t('dc3_lookup_error');
if (getenv('CERTIMINE_DEBUG') === '1') {
$error .= ' ' . $ex->getMessage();
}
}
} else {
$error = I18n::t('no_results');
}
}
}
}
$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>';
}
}