bericht/ajax/create_upload_token.php
Eduard Wisch 9271ec279f
All checks were successful
Deploy bericht / deploy (push) Successful in 2s
Foto-Upload vom Bericht entkoppeln: Fotos landen im Auftragsordner
- Token-Tabelle: fk_bericht → fk_element + element_type (generisch)
- Migration: bestehende Tokens auf neue Spalten migrieren
- upload_photo API: Foto direkt nach commande/{ref}/, kein Bericht/BerichtPage mehr
- mobile_upload.php: Upload-Ziel über Token-Methode getUploadDir() ermitteln
- Token-Erstellung: element_id + element_type statt berichtid (abwärtskompatibel)
- QR-Modal: Token für Auftrag statt für Bericht; Polling auf Anhänge-Änderung [deploy]

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 13:48:09 +02:00

50 lines
1.9 KiB
PHP

<?php
/* Erstellt einen Upload-Token für ein Dolibarr-Objekt (Auftrag/Rechnung/Angebot).
* POST: element_id, element_type (oder berichtid für Abwärtskompatibilität), token (Dolibarr CSRF)
* Liefert: { token, expires_at, url }
*/
require_once __DIR__.'/_inc.php';
require_once __DIR__.'/../class/upload_token.class.php';
global $db, $user, $conf;
if (!$user->hasRight('bericht', 'write')) bericht_ajax_fail('Permission denied', 403);
$element_id = (int) ($_POST['element_id'] ?? 0);
$element_type = (string) ($_POST['element_type'] ?? 'order');
// Abwärtskompatibilität: berichtid akzeptieren, daraus element_id ableiten
if (!$element_id && !empty($_POST['berichtid'])) {
require_once __DIR__.'/../class/bericht.class.php';
$b = new Bericht($db);
if ($b->fetch((int) $_POST['berichtid']) > 0) {
$element_id = $b->fk_element;
$element_type = $b->element_type;
}
}
if (!$element_id) bericht_ajax_fail('element_id fehlt');
// Parent-Objekt validieren
$valid_types = array('order', 'invoice', 'propal');
if (!in_array($element_type, $valid_types)) bericht_ajax_fail('element_type ungültig');
// Prüfen ob Objekt existiert
$tok_check = new BerichtUploadToken($db);
$tok_check->fk_element = $element_id;
$tok_check->element_type = $element_type;
$parent = $tok_check->fetchParentObject();
if (!$parent) bericht_ajax_fail('Objekt nicht gefunden', 404);
$tok = new BerichtUploadToken($db);
$hex = $tok->create($element_id, $element_type, $user->id);
if (!$hex) bericht_ajax_fail('Token-Erstellung fehlgeschlagen');
$base = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'];
$url = $base.dol_buildpath('/bericht/mobile_upload.php', 1).'?token='.$hex;
bericht_ajax_ok(array(
'token' => $hex,
'expires_at' => $tok->expires_at,
'expires_in_min' => round(($tok->expires_at - dol_now()) / 60),
'url' => $url,
));