| 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/clients/client1/seaside.pacim.de/web/ |
Upload File : |
<?php
/**
* valet-api.php – VALET-Formulare-Builder (Tagesansicht, Dokumente).
*
* action=meta GET date -> { ok, date, cards:[ {id,name,plate} ] } (VALET-Anreisen des Tages)
* action=preview POST date,def -> streamt eine PDF-Vorschau (inline) gemäß Definition
* action=save POST date,def[,comment] -> { ok, version:{file,label,comment} } (PDF + JSON-Definition)
* action=load GET date,file -> { ok, definition:{removed,added,comment} }
* action=versions GET date -> { ok, versions:[ {file,created,label,comment} ] }
* action=file GET date,file[,dl] -> streamt eine gespeicherte Varianten-PDF
* action=delete POST date,file -> { ok, versions:[...] }
*
* Definition (JSON): { "removed": [<booking_id>, ...], "added": [ {name,marke,plate}, ... ], "comment": "" }
* Karten-ID = booking_id der VALET-Anreise. Varianten unter datas/doc-versions/{date}/valet__{Ymd-His}.pdf(+.json).
*/
require_once( __DIR__ . '/includes/config.inc.php' );
@ini_set( 'display_errors', '0' );
error_reporting( 0 );
ob_start();
if ( ! isset( $_SESSION['user'] ) || $_SESSION['user'] != LOGIN_USER ) {
while ( ob_get_level() > 0 ) ob_end_clean();
http_response_code( 403 );
header( 'Content-Type: application/json; charset=utf-8' );
echo json_encode( array( 'ok' => false, 'error' => 'unauthorized' ) );
exit();
}
$action = isset( $_GET['action'] ) ? $_GET['action'] : ( isset( $_POST['action'] ) ? $_POST['action'] : '' );
function va_date( $v ) { return ( is_string( $v ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $v ) ) ? $v : null; }
function va_dir( $date ) { return PATH_SYSTEM . '/datas/doc-versions/' . $date; }
function va_json( $arr ) { while ( ob_get_level() > 0 ) ob_end_clean(); header( 'Content-Type: application/json; charset=utf-8' ); echo json_encode( $arr ); exit(); }
/** Definition aus dem Request säubern. */
function va_definition() {
$raw = isset( $_POST['def'] ) ? $_POST['def'] : ( isset( $_GET['def'] ) ? $_GET['def'] : '' );
$d = ( $raw !== '' ) ? json_decode( $raw, true ) : null;
if ( ! is_array( $d ) ) $d = array();
$added = array();
if ( isset( $d['added'] ) && is_array( $d['added'] ) ) {
foreach ( $d['added'] as $a ) {
if ( ! is_array( $a ) ) continue;
$added[] = array(
'name' => isset( $a['name'] ) ? trim( (string) $a['name'] ) : '',
'marke' => isset( $a['marke'] ) ? trim( (string) $a['marke'] ) : '',
'plate' => isset( $a['plate'] ) ? trim( (string) $a['plate'] ) : '',
);
}
}
$removed = array();
if ( isset( $d['removed'] ) && is_array( $d['removed'] ) ) {
foreach ( $d['removed'] as $r ) $removed[] = (string) $r;
}
$edits = array();
if ( isset( $d['edits'] ) && is_array( $d['edits'] ) ) {
foreach ( $d['edits'] as $id => $e ) {
if ( ! is_array( $e ) ) continue;
$edits[ (string) $id ] = array(
'name' => isset( $e['name'] ) ? trim( (string) $e['name'] ) : '',
'marke' => isset( $e['marke'] ) ? trim( (string) $e['marke'] ) : '',
'plate' => isset( $e['plate'] ) ? trim( (string) $e['plate'] ) : '',
);
}
}
return array(
'removed' => $removed,
'added' => $added,
'edits' => $edits,
'comment' => isset( $d['comment'] ) ? trim( (string) $d['comment'] ) : '',
);
}
/** Formular-Liste aus Basis (VALET-Anreisen) + Definition (removed/added) bauen. */
function va_items( $date, $def ) {
$base = ( new dayview() )->valetArrivals( $date );
$rem = array_flip( $def['removed'] );
$edits = isset( $def['edits'] ) ? $def['edits'] : array();
$items = array();
foreach ( (array) $base as $b ) {
$id = (string) $b->id;
if ( isset( $rem[ $id ] ) ) continue;
if ( isset( $edits[ $id ] ) ) {
$e = $edits[ $id ];
$items[] = array( 'name' => $e['name'] !== '' ? $e['name'] : $b->name, 'plate' => $e['plate'], 'marke' => $e['marke'] );
} else {
$items[] = array( 'name' => $b->name, 'plate' => $b->plate, 'marke' => '' );
}
}
foreach ( $def['added'] as $a ) {
$items[] = array( 'name' => $a['name'], 'plate' => $a['plate'], 'marke' => $a['marke'] );
}
return $items;
}
/** Varianten eines Tages (neueste zuerst), inkl. Kommentar aus der JSON-Definition. */
function va_versions( $date ) {
$dir = va_dir( $date );
$out = array();
if ( is_dir( $dir ) ) {
foreach ( glob( $dir . '/valet__*.pdf' ) as $f ) {
$base = basename( $f );
if ( preg_match( '/__(\d{8})-(\d{6})\.pdf$/', $base, $m ) ) {
$ts = mktime( (int) substr( $m[2], 0, 2 ), (int) substr( $m[2], 2, 2 ), (int) substr( $m[2], 4, 2 ),
(int) substr( $m[1], 4, 2 ), (int) substr( $m[1], 6, 2 ), (int) substr( $m[1], 0, 4 ) );
$comment = '';
$jf = $dir . '/' . preg_replace( '/\.pdf$/', '.json', $base );
if ( is_file( $jf ) ) {
$j = json_decode( file_get_contents( $jf ), true );
if ( is_array( $j ) && isset( $j['comment'] ) ) $comment = (string) $j['comment'];
}
$out[] = array(
'file' => $base,
'created' => $ts,
'label' => date( 'd.m.Y, H:i', $ts ) . ' Uhr',
'comment' => $comment,
);
}
}
usort( $out, function ( $a, $b ) { return $b['created'] - $a['created']; } );
}
return $out;
}
/* ====================== META (Formular-Liste = VALET-Anreisen) ====================== */
if ( $action === 'meta' ) {
$date = va_date( isset( $_GET['date'] ) ? $_GET['date'] : '' );
if ( ! $date ) va_json( array( 'ok' => false, 'error' => 'bad_params' ) );
$base = ( new dayview() )->valetArrivals( $date );
$cards = array();
foreach ( (array) $base as $b ) $cards[] = array( 'id' => (string) $b->id, 'name' => $b->name, 'plate' => $b->plate );
va_json( array( 'ok' => true, 'date' => $date, 'cards' => $cards ) );
}
/* ====================== PREVIEW (PDF aus Definition, inline) ====================== */
if ( $action === 'preview' ) {
$date = va_date( isset( $_POST['date'] ) ? $_POST['date'] : ( isset( $_GET['date'] ) ? $_GET['date'] : '' ) );
if ( ! $date ) va_json( array( 'ok' => false, 'error' => 'bad_params' ) );
$def = va_definition();
global $oPDF;
$bytes = $oPDF->buildValetDay( va_items( $date, $def ), $date )->Output( 'S' );
$dl = isset( $_GET['dl'] ) && $_GET['dl'] == '1';
while ( ob_get_level() > 0 ) ob_end_clean();
header( 'Content-Type: application/pdf' );
header( 'Content-Disposition: ' . ( $dl ? 'attachment' : 'inline' ) . '; filename="' . $date . '_VALET-Formulare.pdf"' );
header( 'Content-Length: ' . strlen( $bytes ) );
header( 'X-Content-Type-Options: nosniff' );
echo $bytes;
exit();
}
/* ====================== SAVE (Variante: PDF + JSON-Definition inkl. Kommentar) ====================== */
if ( $action === 'save' ) {
$date = va_date( isset( $_POST['date'] ) ? $_POST['date'] : '' );
if ( ! $date ) va_json( array( 'ok' => false, 'error' => 'bad_params' ) );
$def = va_definition();
if ( isset( $_POST['comment'] ) ) $def['comment'] = trim( (string) $_POST['comment'] );
$dir = va_dir( $date );
if ( ! is_dir( $dir ) ) @mkdir( $dir, 0775, true );
if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) va_json( array( 'ok' => false, 'error' => 'not_writable' ) );
global $oPDF;
$ts = date( 'Ymd-His' );
$fname = 'valet__' . $ts . '.pdf';
$oPDF->buildValetDay( va_items( $date, $def ), $date )->Output( 'F', $dir . '/' . $fname );
@file_put_contents( $dir . '/valet__' . $ts . '.json', json_encode( $def ) );
if ( ! is_file( $dir . '/' . $fname ) ) va_json( array( 'ok' => false, 'error' => 'write_failed' ) );
va_json( array( 'ok' => true, 'version' => array(
'file' => $fname,
'label' => date( 'd.m.Y, H:i', time() ) . ' Uhr',
'comment' => $def['comment'],
) ) );
}
/* ====================== LOAD (Definition einer Variante) ====================== */
if ( $action === 'load' ) {
$date = va_date( isset( $_GET['date'] ) ? $_GET['date'] : '' );
$file = isset( $_GET['file'] ) ? basename( $_GET['file'] ) : '';
if ( ! $date || ! preg_match( '/^valet__\d{8}-\d{6}\.pdf$/', $file ) ) va_json( array( 'ok' => false, 'error' => 'bad_params' ) );
$json = va_dir( $date ) . '/' . preg_replace( '/\.pdf$/', '.json', $file );
$def = is_file( $json ) ? json_decode( file_get_contents( $json ), true ) : array();
if ( ! is_array( $def ) ) $def = array();
va_json( array( 'ok' => true, 'definition' => array(
'removed' => ( isset( $def['removed'] ) && is_array( $def['removed'] ) ) ? array_values( $def['removed'] ) : array(),
'added' => ( isset( $def['added'] ) && is_array( $def['added'] ) ) ? array_values( $def['added'] ) : array(),
'edits' => ( isset( $def['edits'] ) && is_array( $def['edits'] ) ) ? $def['edits'] : new stdClass(),
'comment' => isset( $def['comment'] ) ? $def['comment'] : '',
) ) );
}
/* ====================== VERSIONS ====================== */
if ( $action === 'versions' ) {
$date = va_date( isset( $_GET['date'] ) ? $_GET['date'] : '' );
if ( ! $date ) va_json( array( 'ok' => false, 'error' => 'bad_params' ) );
va_json( array( 'ok' => true, 'versions' => va_versions( $date ) ) );
}
/* ====================== FILE (Varianten-PDF streamen) ====================== */
if ( $action === 'file' ) {
$date = va_date( isset( $_GET['date'] ) ? $_GET['date'] : '' );
$file = isset( $_GET['file'] ) ? basename( $_GET['file'] ) : '';
if ( ! $date || ! preg_match( '/^valet__\d{8}-\d{6}\.pdf$/', $file ) ) { while ( ob_get_level() > 0 ) ob_end_clean(); http_response_code( 400 ); echo 'bad request'; exit(); }
$path = va_dir( $date ) . '/' . $file;
if ( ! is_file( $path ) ) { while ( ob_get_level() > 0 ) ob_end_clean(); http_response_code( 404 ); echo 'not found'; exit(); }
$dl = isset( $_GET['dl'] ) && $_GET['dl'] == '1';
while ( ob_get_level() > 0 ) ob_end_clean();
header( 'Content-Type: application/pdf' );
header( 'Content-Disposition: ' . ( $dl ? 'attachment' : 'inline' ) . '; filename="' . $date . '-VALET-Formulare-Variante.pdf"' );
header( 'Content-Length: ' . filesize( $path ) );
header( 'X-Content-Type-Options: nosniff' );
readfile( $path );
exit();
}
/* ====================== DELETE ====================== */
if ( $action === 'delete' ) {
$date = va_date( isset( $_POST['date'] ) ? $_POST['date'] : '' );
$file = isset( $_POST['file'] ) ? basename( $_POST['file'] ) : '';
if ( ! $date || ! preg_match( '/^valet__\d{8}-\d{6}\.pdf$/', $file ) ) va_json( array( 'ok' => false, 'error' => 'bad_params' ) );
$path = va_dir( $date ) . '/' . $file;
if ( ! is_file( $path ) ) va_json( array( 'ok' => false, 'error' => 'not_found' ) );
@unlink( $path );
@unlink( va_dir( $date ) . '/' . preg_replace( '/\.pdf$/', '.json', $file ) );
if ( is_file( $path ) ) va_json( array( 'ok' => false, 'error' => 'delete_failed' ) );
va_json( array( 'ok' => true, 'versions' => va_versions( $date ) ) );
}
http_response_code( 400 );
va_json( array( 'ok' => false, 'error' => 'unknown_action' ) );