<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
require_once __DIR__ . '/app/Helpers/note-images.php';
$user=require_login();
ob_start(static function (string $html): string {
    if (language() === 'de') { return $html; }
    return strtr($html, ['<html lang="de">' => '<html lang="en">', 'Profilbild · TripTale' => t('profile_image.title') . ' · TripTale', '>← Zum Profil<' => '>' . t('profile_image.back') . '<', '>Profilbild<' => '>' . t('profile_image.title') . '<', 'alt="Profilbild"' => 'alt="' . htmlspecialchars(t('profile_image.alt'), ENT_QUOTES, 'UTF-8') . '"', '>Bild auswählen<' => '>' . t('profile_image.choose') . '<', '>JPG, PNG oder WebP · maximal 5 MB<' => '>' . t('profile_image.hint') . '<', '>Datei auswählen<input' => '>' . t('profile_image.file') . '<input', '>Noch kein Bild ausgewählt<' => '>' . t('profile_image.empty') . '<', '>Profilbild speichern<' => '>' . t('profile_image.save') . '<', '>Profilbild löschen<' => '>' . t('profile_image.delete') . '<', "confirm('Profilbild wirklich löschen?')" => 'confirm(' . tjs('profile_image.confirm_delete') . ')', ":'Noch kein Bild ausgewählt'" => ':' . tjs('profile_image.empty')]);
});
$q=db()->prepare('SELECT first_name,last_name,profile_image FROM '.table('users').' WHERE id=:id');$q->execute([':id'=>$user['id']]);$entry=$q->fetch();if(!$entry){exit('Nutzer nicht gefunden.');}$error=null;
if($_SERVER['REQUEST_METHOD']==='POST'&&isset($_POST['delete_image'])){verify_csrf();$old=(string)($entry['profile_image']??'');db()->prepare('UPDATE '.table('users').' SET profile_image=NULL WHERE id=:id')->execute([':id'=>$user['id']]);if(str_starts_with($old,'uploads/profile-images/')){@unlink(__DIR__.'/'.$old);}header('Location: /profile.php?image=deleted');exit;}
if($_SERVER['REQUEST_METHOD']==='POST'){verify_csrf();if(empty($_FILES['image']['tmp_name'])){$error=t('profile_image.error_choose');}elseif((int)$_FILES['image']['size']>5*1024*1024){$error=t('profile_image.error_size');}else{$info=@getimagesize($_FILES['image']['tmp_name']);$types=[IMAGETYPE_JPEG=>'jpg',IMAGETYPE_PNG=>'png',IMAGETYPE_WEBP=>'webp'];if(!$info||!isset($types[$info[2]])){$error=t('profile_image.error_type');}else{$dir=__DIR__.'/uploads/profile-images';if(!is_dir($dir)&&!mkdir($dir,0755,true)){$error=t('profile_image.error_dir');}else{$file='profile-'.$user['id'].'-'.bin2hex(random_bytes(8)).'.jpg';$target=$dir.'/'.$file;$stored=false;if(function_exists('imagecreatetruecolor')&&function_exists('imagejpeg')){$source=match($info[2]){IMAGETYPE_JPEG=>function_exists('imagecreatefromjpeg')?@imagecreatefromjpeg($_FILES['image']['tmp_name']):false,IMAGETYPE_PNG=>function_exists('imagecreatefrompng')?@imagecreatefrompng($_FILES['image']['tmp_name']):false,IMAGETYPE_WEBP=>function_exists('imagecreatefromwebp')?@imagecreatefromwebp($_FILES['image']['tmp_name']):false};if($source){if($info[2]===IMAGETYPE_JPEG)$source=note_correct_jpeg_orientation($source,$_FILES['image']['tmp_name']);$w=imagesx($source);$h=imagesy($source);$scale=min(1,600/max($w,$h));$nw=max(1,(int)round($w*$scale));$nh=max(1,(int)round($h*$scale));$canvas=imagecreatetruecolor($nw,$nh);imagefill($canvas,0,0,imagecolorallocate($canvas,255,255,255));imagecopyresampled($canvas,$source,0,0,0,0,$nw,$nh,$w,$h);$stored=imagejpeg($canvas,$target,86);imagedestroy($canvas);imagedestroy($source);}}if(!$stored){$file='profile-'.$user['id'].'-'.bin2hex(random_bytes(8)).'.'.$types[$info[2]];$target=$dir.'/'.$file;$stored=move_uploaded_file($_FILES['image']['tmp_name'],$target);}if(!$stored){$error=t('profile_image.error_save');}else{$old=(string)($entry['profile_image']??'');db()->prepare('UPDATE '.table('users').' SET profile_image=:image WHERE id=:id')->execute([':image'=>'uploads/profile-images/'.$file,':id'=>$user['id']]);if(str_starts_with($old,'uploads/profile-images/')){@unlink(__DIR__.'/'.$old);}header('Location: /profile.php?image=changed');exit;}}}}}
?><!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Profilbild · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:600px;margin:auto;background:#fff;padding:28px;border-radius:14px}.button,button{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font:inherit;font-weight:700;cursor:pointer}.back{background:#e3ebee;color:#126e82}.preview{width:180px;height:180px;object-fit:cover;border-radius:50%;display:block;margin:18px auto;background:#eef4f5}.file-area{margin:18px 0;padding:16px;background:#edf5f6;border-left:4px solid #75aeb9;border-radius:10px}.picker{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border-radius:7px;font-weight:700;cursor:pointer}.picker input{position:absolute;width:1px;height:1px;opacity:0}.name{display:block;margin-top:8px;color:#59636d}.save{margin-top:14px}.delete{background:#9d2d2d}.error{background:#ffe6e3;padding:12px;border-radius:8px}@media(max-width:600px){body{padding:12px}main{padding:16px}.picker,.save,button{width:100%;text-align:center}.back{width:auto!important}}</style></head><body><main><a class="button back" href="/profile.php">← Zum Profil</a><h1>Profilbild</h1><img class="preview" src="/<?=htmlspecialchars($entry['profile_image']?:'assets/images/triptale-profile-placeholder.png',ENT_QUOTES,'UTF-8')?>" alt="Profilbild"><?php if($error):?><p class="error"><?=htmlspecialchars($error,ENT_QUOTES,'UTF-8')?></p><?php endif;?><form method="post" enctype="multipart/form-data"><input type="hidden" name="csrf_token" value="<?=htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8')?>"><section class="file-area"><strong>Bild auswählen</strong><p class="name">JPG, PNG oder WebP · maximal 5 MB</p><label class="picker">Datei auswählen<input id="file" name="image" type="file" accept="image/jpeg,image/png,image/webp" required></label><span class="name" id="name">Noch kein Bild ausgewählt</span></section><button class="save">Profilbild speichern</button></form><?php if($entry['profile_image']):?><form method="post" onsubmit="return confirm('Profilbild wirklich löschen?');"><input type="hidden" name="csrf_token" value="<?=htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8')?>"><button class="delete" name="delete_image">Profilbild löschen</button></form><?php endif;?></main><script>file.onchange=()=>name.textContent=file.files.length?file.files[0].name:'Noch kein Bild ausgewählt';</script></body></html>
