diff --git a/api/netdiag_api.lib.php b/api/netdiag_api.lib.php index d69af0b..87666b0 100644 --- a/api/netdiag_api.lib.php +++ b/api/netdiag_api.lib.php @@ -298,6 +298,28 @@ function netdiag_api_read_body() return is_array($data) ? $data : array(); } +/** + * Zeitstempel der App in einen Unix-Zeitstempel (Sekunden) umrechnen. + * + * Die App (JavaScript) liefert Zeitstempel in Millisekunden (Date.now()). + * Dolibarr/`idate()` erwartet Sekunden — sonst: "Bad value ... for date". + * + * @param mixed $value Zeitstempel aus dem Request (ms, s oder leer) + * @return int Unix-Zeitstempel in Sekunden + */ +function netdiag_api_timestamp($value) +{ + $v = (int) $value; + if ($v <= 0) { + return dol_now(); + } + // 13-stellig (> ~Jahr 5138 in Sekunden) = Millisekunden -> auf Sekunden + if ($v > 100000000000) { + $v = (int) ($v / 1000); + } + return $v; +} + /** * Liste von Diagnose-Protokollen als Array zurückgeben (für API-Antworten). * diff --git a/api/protocols.php b/api/protocols.php index cf6575b..f6e2a83 100644 --- a/api/protocols.php +++ b/api/protocols.php @@ -134,7 +134,7 @@ $protocol->client_uuid = $uuid; $protocol->label = isset($p['label']) ? (string) $p['label'] : ''; $protocol->fk_soc = !empty($p['socId']) ? (int) $p['socId'] : null; $protocol->fk_commande = !empty($p['orderId']) ? (int) $p['orderId'] : null; -$protocol->date_diag = !empty($p['dateDiag']) ? (int) $p['dateDiag'] : dol_now(); +$protocol->date_diag = netdiag_api_timestamp($p['dateDiag'] ?? 0); $protocol->fk_user_techniker = (int) $user->id; $protocol->standort = isset($p['location']) ? (string) $p['location'] : ''; $protocol->subnet = isset($p['subnet']) ? (string) $p['subnet'] : ''; @@ -194,7 +194,7 @@ foreach ($measIn as $m) { $meas->params = isset($m['params']) ? json_encode($m['params'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : null; $meas->result = isset($m['result']) ? json_encode($m['result'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : null; $meas->measure_status = isset($m['measureStatus']) ? (int) $m['measureStatus'] : 0; - $meas->date_measure = !empty($m['dateMeasure']) ? (int) $m['dateMeasure'] : dol_now(); + $meas->date_measure = netdiag_api_timestamp($m['dateMeasure'] ?? 0); $meas->tms = dol_now(); if ($meas->create($user, 1) <= 0) { $db->rollback();