- 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>
231 lines
8.9 KiB
PHP
231 lines
8.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CertiMine;
|
|
|
|
/**
|
|
* Cliente HTTP: modo legacy (JSON login) o API Peñasquito DC3 (token estático + passport).
|
|
*/
|
|
final class MobilisemeClient
|
|
{
|
|
/**
|
|
* @return array{token: string, profile: array<string,mixed>, dc3_result: ?array<string,mixed>}
|
|
*/
|
|
public static function login(string $username, string $password): array
|
|
{
|
|
$cfg = Config::get('mobiliseme');
|
|
if (!is_array($cfg)) {
|
|
throw new \RuntimeException('Mobiliseme config missing');
|
|
}
|
|
if (PenasquitoDc3Api::isConfigured()) {
|
|
$pack = PenasquitoDc3Api::loginWithPassport($username);
|
|
return [
|
|
'token' => $pack['token'],
|
|
'profile' => $pack['profile'],
|
|
'dc3_result' => $pack['dc3_result'],
|
|
];
|
|
}
|
|
if (!empty($cfg['mock'])) {
|
|
$profile = self::mockProfile($username);
|
|
return [
|
|
'token' => 'mock-token-' . $profile['id'],
|
|
'profile' => $profile,
|
|
'dc3_result' => null,
|
|
];
|
|
}
|
|
$base = rtrim((string) $cfg['base_url'], '/');
|
|
$path = (string) $cfg['login_path'];
|
|
$body = [
|
|
(string) $cfg['login_username_field'] => $username,
|
|
(string) $cfg['login_password_field'] => $password,
|
|
];
|
|
$resp = self::request('POST', $base . $path, null, $body);
|
|
$token = Util::arrayPath($resp['json'], (string) $cfg['token_json_path']);
|
|
if (!is_string($token) || $token === '') {
|
|
throw new \RuntimeException('Invalid login response');
|
|
}
|
|
$prof = self::fetchProfile($token);
|
|
return ['token' => $token, 'profile' => $prof, 'dc3_result' => null];
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public static function fetchProfile(string $token): array
|
|
{
|
|
$cfg = Config::get('mobiliseme');
|
|
if (!is_array($cfg)) {
|
|
throw new \RuntimeException('Mobiliseme config missing');
|
|
}
|
|
if (!empty($cfg['mock'])) {
|
|
return self::mockProfile('demo');
|
|
}
|
|
if (PenasquitoDc3Api::isConfigured()) {
|
|
return [];
|
|
}
|
|
$base = rtrim((string) $cfg['base_url'], '/');
|
|
$path = (string) $cfg['profile_path'];
|
|
$resp = self::request('GET', $base . $path, $token, null);
|
|
$json = $resp['json'];
|
|
return is_array($json) ? $json : [];
|
|
}
|
|
|
|
/** @return list<array<string,mixed>> */
|
|
public static function fetchCertificates(string $mobilisemeId, string $token): array
|
|
{
|
|
$cfgConfig = Config::get('mobiliseme');
|
|
if (PenasquitoDc3Api::isConfigured()) {
|
|
try {
|
|
$res = PenasquitoDc3Api::fetchResultForPassport($mobilisemeId);
|
|
} catch (\Throwable) {
|
|
return [];
|
|
}
|
|
$comps = $res['competencies'] ?? [];
|
|
if (!is_array($comps)) {
|
|
return [];
|
|
}
|
|
return self::dc3CompetenciesToItems($mobilisemeId, $comps);
|
|
}
|
|
if (!is_array($cfgConfig)) {
|
|
return [];
|
|
}
|
|
if (!empty($cfgConfig['mock'])) {
|
|
return self::mockCertificates($mobilisemeId);
|
|
}
|
|
$base = rtrim((string) $cfgConfig['base_url'], '/');
|
|
$tpl = (string) $cfgConfig['person_certificates_path'];
|
|
$path = str_replace('{id}', rawurlencode($mobilisemeId), $tpl);
|
|
$resp = self::request('GET', $base . $path, $token, null);
|
|
$json = $resp['json'];
|
|
$listPath = (string) $cfgConfig['certificate_list_path'];
|
|
$items = Util::arrayPath($json, $listPath);
|
|
if (is_array($items)) {
|
|
/** @var list<array<string,mixed>> $out */
|
|
$out = [];
|
|
foreach ($items as $it) {
|
|
if (is_array($it)) {
|
|
$out[] = $it;
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
return is_array($json) ? [$json] : [];
|
|
}
|
|
|
|
/**
|
|
* @param list<array<string,mixed>> $comps
|
|
* @return list<array<string,mixed>>
|
|
*/
|
|
private static function dc3CompetenciesToItems(string $mobilisemeId, array $comps): array
|
|
{
|
|
$items = [];
|
|
foreach ($comps as $c) {
|
|
if (!is_array($c)) {
|
|
continue;
|
|
}
|
|
$activity = (string) ($c['activity'] ?? '');
|
|
$frm = PenasquitoDc3Api::activityToFrmCode($activity);
|
|
$nom = $frm !== null ? PersonRepository::firstNomCodeForFrmCode($frm) : null;
|
|
$req = isset($c['request_date']) ? (string) $c['request_date'] : null;
|
|
$expRaw = $c['expiration_date'] ?? null;
|
|
$expStr = ($expRaw === null || $expRaw === '' || $expRaw === '---') ? null : (string) $expRaw;
|
|
$issued = null;
|
|
$expires = null;
|
|
if ($req !== null) {
|
|
$dt = \DateTime::createFromFormat('d/m/Y', trim($req));
|
|
$issued = $dt ? $dt->format('Y-m-d') : null;
|
|
}
|
|
if ($expStr !== null) {
|
|
$dt2 = \DateTime::createFromFormat('d/m/Y', trim($expStr));
|
|
$expires = $dt2 ? $dt2->format('Y-m-d') : null;
|
|
}
|
|
$items[] = [
|
|
'title' => $activity,
|
|
'nom_code' => (string) ($nom ?? ''),
|
|
'issued_at' => $issued,
|
|
'expires_at' => $expires,
|
|
'external_ref' => 'dc3-' . md5($mobilisemeId . '|' . $activity . '|' . $req),
|
|
];
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed>|null $jsonBody
|
|
* @return array{status:int, json:mixed, raw:string}
|
|
*/
|
|
private static function request(string $method, string $url, ?string $token, ?array $jsonBody): array
|
|
{
|
|
$cfg = Config::get('mobiliseme');
|
|
$timeout = is_array($cfg) ? (int) ($cfg['timeout_seconds'] ?? 15) : 15;
|
|
$ch = curl_init($url);
|
|
$headers = ['Accept: application/json'];
|
|
if ($jsonBody !== null) {
|
|
$headers[] = 'Content-Type: application/json';
|
|
}
|
|
if ($token !== null && is_array($cfg)) {
|
|
$hName = (string) $cfg['authorization_header'];
|
|
$scheme = (string) $cfg['authorization_scheme'];
|
|
$headers[] = $hName . ': ' . $scheme . $token;
|
|
}
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_CUSTOMREQUEST => $method,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => $timeout,
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
]);
|
|
if ($jsonBody !== null) {
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonBody, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
$raw = curl_exec($ch);
|
|
if ($raw === false) {
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
throw new \RuntimeException('cURL error: ' . $err);
|
|
}
|
|
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
if ($method !== 'POST' && ($status < 200 || $status >= 300)) {
|
|
throw new \RuntimeException('HTTP ' . $status);
|
|
}
|
|
if ($method === 'POST' && is_array($cfg) && isset($cfg['login_path']) && str_contains($url, (string) $cfg['login_path']) && ($status < 200 || $status >= 300)) {
|
|
throw new \RuntimeException('Login HTTP ' . $status);
|
|
}
|
|
$decoded = json_decode((string) $raw, true);
|
|
return ['status' => $status, 'json' => $decoded, 'raw' => (string) $raw];
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private static function mockProfile(string $username): array
|
|
{
|
|
$u = strtolower($username);
|
|
if (str_contains($u, 'contract')) {
|
|
return [
|
|
'id' => 'demo-contractor-1',
|
|
'full_name' => 'Demo Contractor SA',
|
|
'type' => 'contractor',
|
|
'employee_number' => 'C-5001',
|
|
];
|
|
}
|
|
return [
|
|
'id' => 'demo-employee-1',
|
|
'full_name' => 'Demo Employee',
|
|
'type' => 'employee',
|
|
'employee_number' => '10001',
|
|
];
|
|
}
|
|
|
|
/** @return list<array<string,mixed>> */
|
|
private static function mockCertificates(string $mobilisemeId): array
|
|
{
|
|
if ($mobilisemeId === 'demo-contractor-1') {
|
|
return [
|
|
['nom_code' => 'NOM-009', 'title' => 'DC3 Heights', 'expires_at' => date('Y-m-d', strtotime('+300 days'))],
|
|
['nom_code' => 'NOM-001', 'title' => 'DC3 Confined', 'expires_at' => date('Y-m-d', strtotime('+200 days'))],
|
|
];
|
|
}
|
|
return [
|
|
['nom_code' => 'NOM-009', 'title' => 'DC3 Heights', 'expires_at' => date('Y-m-d', strtotime('+335 days'))],
|
|
['nom_code' => 'NOM-017', 'title' => 'DC3 PPE', 'expires_at' => date('Y-m-d', strtotime('+100 days'))],
|
|
];
|
|
}
|
|
}
|