| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/seaside.pacim.de/web/ |
Upload File : |
<?php
/**
* booking-code.php – Buchungscode (booking_no) einer Buchung neu generieren (Admin).
*
* Aktion (action):
* regenerate POST booking_id -> erzeugt einen systemweit eindeutigen 6-stelligen
* Buchungscode (verwechslungsarmer Zeichensatz), überschreibt booking_no und
* erzeugt ein NEUES Capability-Token (booking_edit_hash) für die Bearbeiten-Links.
* Rückgabe: { ok, code, edit_hash }
*
* Der Generator (pacim_unique_booking_code / pacim_booking_code) greift bewusst nur hier
* manuell – kein automatischer Einsatz an anderer Stelle.
*/
require_once( __DIR__ . '/includes/config.inc.php' );
@ini_set( 'display_errors', '0' );
error_reporting( 0 );
ob_start();
function bc_json( $a ) { while ( ob_get_level() > 0 ) ob_end_clean(); header( 'Content-Type: application/json; charset=utf-8' ); echo json_encode( $a ); exit(); }
function bc_fail( $m ) { bc_json( array( 'ok' => false, 'error' => $m ) ); }
if ( ! function_exists( 'is_admin' ) || ! is_admin() ) { http_response_code( 403 ); bc_json( array( 'ok' => false, 'error' => 'forbidden' ) ); }
$action = $_POST['action'] ?? ( $_GET['action'] ?? '' );
$db = new MysqliDb( MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB );
if ( $action === 'regenerate' ) {
if ( $_SERVER['REQUEST_METHOD'] !== 'POST' ) bc_fail( 'method' );
$bid = (int) ( $_POST['booking_id'] ?? 0 );
if ( $bid <= 0 ) bc_fail( 'Ungültige Buchung.' );
$bk = $db->rawQueryOne( "SELECT booking_id, booking_no, booking_invoice FROM pacim_booking WHERE booking_id = ?", array( $bid ) );
if ( ! $bk ) bc_fail( 'Buchung nicht gefunden.' );
$logBefore = class_exists( 'apilog' ) ? apilog::snapshot( $db, $bid ) : null;
// Eindeutigen Code erzeugen, schreiben und danach erneut prüfen, ob er systemweit nur EINMAL
// vorkommt (schließt das schmale Zeitfenster zwischen Prüfung und Schreiben). Bei Kollision
// wird ein neuer Code generiert. (booking_no ist nicht unique – AIDA-Gruppen teilen Nummern –
// daher kein DB-Unique-Index, die Eindeutigkeit wird hier erzwungen.)
$code = null; $hash = null;
for ( $try = 0; $try < 100; $try++ ) {
$cand = pacim_booking_code( 6 );
// 1) Vorprüfung: existiert die Nummer schon in einer ANDEREN Buchung?
if ( $db->rawQueryOne( "SELECT booking_id FROM pacim_booking WHERE booking_no = ? AND booking_id <> ? LIMIT 1", array( $cand, $bid ) ) ) continue;
// 2) Schreiben (Code + neues Bearbeiten-Token)
$tok = bin2hex( random_bytes( 16 ) );
$db->where( 'booking_id', $bid );
if ( ! $db->update( 'pacim_booking', array( 'booking_no' => $cand, 'booking_edit_hash' => $tok ) ) ) bc_fail( 'Der Code konnte nicht gespeichert werden.' );
// 3) Nachprüfung: kommt der Code jetzt systemweit nur genau einmal vor?
$cnt = $db->rawQueryOne( "SELECT COUNT(*) AS c FROM pacim_booking WHERE booking_no = ?", array( $cand ) );
if ( $cnt && (int) $cnt['c'] > 1 ) continue; // zwischenzeitliche Kollision → neuen Code generieren
$code = $cand; $hash = $tok; break;
}
if ( ! $code ) bc_fail( 'Es konnte kein eindeutiger Code erzeugt werden. Bitte erneut versuchen.' );
if ( class_exists( 'apilog' ) ) {
apilog::record( array(
'key_name' => trim( (string) ( $_SESSION['user_name'] ?? 'Administrator' ) ),
'ip' => $_SERVER['REMOTE_ADDR'] ?? '', 'method' => 'POST',
'resource' => 'booking', 'action' => 'code_regenerate', 'kind' => 'write',
'booking' => $bid, 'invoice' => (int) $bk['booking_invoice'],
'target' => 'Buchungscode neu erzeugt (' . $bk['booking_no'] . ' → ' . $code . ')',
'changes' => apilog::diff( $logBefore, apilog::snapshot( $db, $bid ) ),
) );
}
bc_json( array( 'ok' => true, 'code' => $code, 'edit_hash' => $hash, 'old' => (string) $bk['booking_no'] ) );
}
bc_fail( 'Unbekannte Aktion.' );