isEmailSharingEnabled()) { echo json_encode(['success' => false, 'error' => 'E-Mail-Sharing ist deaktiviert']); exit; } // Config laden $configFile = dirname(__DIR__) . '/config.php'; $config = file_exists($configFile) ? require $configFile : []; $mailConfig = $config['mail'] ?? []; if (empty($mailConfig['host']) || empty($mailConfig['username'])) { echo json_encode(['success' => false, 'error' => 'E-Mail-Server nicht konfiguriert']); exit; } // === GET: Share-Link generieren === if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['generate'])) { $path = $_GET['path'] ?? ''; $type = $_GET['type'] ?? 'video'; if (empty($path)) { echo json_encode(['success' => false, 'error' => 'Kein Pfad angegeben']); exit; } // Token generieren $expiryHours = $settingsManager->getShareLinkExpiryHours(); $expiry = time() + ($expiryHours * 3600); $token = hash_hmac('sha256', $path . $expiry, session_id() . 'share_secret'); // Share-Link speichern $shareDir = dirname(__DIR__) . '/data/shares/'; if (!is_dir($shareDir)) { mkdir($shareDir, 0755, true); } $shareId = bin2hex(random_bytes(16)); $shareData = [ 'id' => $shareId, 'path' => $path, 'type' => $type, 'token' => $token, 'expiry' => $expiry, 'created_at' => date('Y-m-d H:i:s') ]; file_put_contents($shareDir . $shareId . '.json', json_encode($shareData)); // URL generieren $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; $shareUrl = $baseUrl . '/api/share.php?view=' . $shareId; echo json_encode([ 'success' => true, 'share_url' => $shareUrl, 'share_id' => $shareId, 'expires_at' => date('Y-m-d H:i:s', $expiry) ]); exit; } // === GET: Share-Link anzeigen === if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['view'])) { $shareId = preg_replace('/[^a-f0-9]/', '', $_GET['view']); $shareFile = dirname(__DIR__) . '/data/shares/' . $shareId . '.json'; if (!file_exists($shareFile)) { header('Content-Type: text/html; charset=utf-8'); echo 'Link ungültig

❌ Link nicht gefunden

Dieser Share-Link existiert nicht oder wurde bereits gelöscht.

'; exit; } $shareData = json_decode(file_get_contents($shareFile), true); // Ablauf prüfen if (time() > $shareData['expiry']) { @unlink($shareFile); header('Content-Type: text/html; charset=utf-8'); echo 'Link abgelaufen

⏰ Link abgelaufen

Dieser Share-Link ist abgelaufen. Bitte fordere einen neuen Link an.

'; exit; } // Datei existiert? $filePath = dirname(__DIR__) . $shareData['path']; if (!file_exists($filePath)) { header('Content-Type: text/html; charset=utf-8'); echo 'Datei nicht gefunden

📭 Datei nicht gefunden

Die geteilte Datei existiert nicht mehr.

'; exit; } // Redirect zur Datei oder HTML-Seite mit eingebettetem Player $isVideo = in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['mp4', 'webm', 'mov']); $isImage = in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png', 'gif', 'webp']); $siteName = $config['app']['name'] ?? 'Aurora Livecam'; $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; header('Content-Type: text/html; charset=utf-8'); echo ' Geteilte ' . ($isVideo ? 'Video' : 'Bild') . ' - ' . htmlspecialchars($siteName) . '

📤 Geteilte' . ($isVideo ? 's Video' : 's Bild') . '

'; if ($isVideo) { echo ''; } else { echo 'Geteiltes Bild'; } echo ' ⬇️ Herunterladen
'; exit; } // === POST: E-Mail senden === if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['success' => false, 'error' => 'Nur POST erlaubt']); exit; } // JSON-Body parsen $input = json_decode(file_get_contents('php://input'), true); if (!$input) { $input = $_POST; } $email = filter_var($input['email'] ?? '', FILTER_VALIDATE_EMAIL); $path = $input['path'] ?? ''; $type = $input['type'] ?? 'video'; $message = htmlspecialchars($input['message'] ?? ''); $senderName = htmlspecialchars($input['sender_name'] ?? 'Ein Freund'); if (!$email) { echo json_encode(['success' => false, 'error' => 'Ungültige E-Mail-Adresse']); exit; } if (empty($path)) { echo json_encode(['success' => false, 'error' => 'Kein Pfad angegeben']); exit; } // Share-Link generieren $expiryHours = $settingsManager->getShareLinkExpiryHours(); $expiry = time() + ($expiryHours * 3600); $shareDir = dirname(__DIR__) . '/data/shares/'; if (!is_dir($shareDir)) { mkdir($shareDir, 0755, true); } $shareId = bin2hex(random_bytes(16)); $shareData = [ 'id' => $shareId, 'path' => $path, 'type' => $type, 'expiry' => $expiry, 'created_at' => date('Y-m-d H:i:s'), 'shared_to' => $email ]; file_put_contents($shareDir . $shareId . '.json', json_encode($shareData)); $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; $shareUrl = $baseUrl . '/api/share.php?view=' . $shareId; $siteName = $config['app']['name'] ?? 'Aurora Livecam'; // E-Mail senden try { $mail = new PHPMailer(true); // SMTP Konfiguration $mail->isSMTP(); $mail->Host = $mailConfig['host']; $mail->SMTPAuth = true; $mail->Username = $mailConfig['username']; $mail->Password = $mailConfig['password']; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = $mailConfig['port'] ?? 587; $mail->CharSet = 'UTF-8'; // Absender/Empfänger $mail->setFrom($mailConfig['from_address'], $mailConfig['from_name'] ?? $siteName); $mail->addAddress($email); // Inhalt $mail->isHTML(true); $mail->Subject = $senderName . ' hat ' . ($type === 'video' ? 'ein Video' : 'ein Bild') . ' mit dir geteilt'; $mail->Body = '

📤 ' . htmlspecialchars($siteName) . '

' . htmlspecialchars($senderName) . ' hat ' . ($type === 'video' ? 'ein Video' : 'ein Bild') . ' mit dir geteilt!

' . (!empty($message) ? '
"' . nl2br($message) . '"
' : '') . ' ▶️ Jetzt ansehen

Dieser Link ist ' . $expiryHours . ' Stunden gültig.

'; $mail->AltBody = $senderName . ' hat ' . ($type === 'video' ? 'ein Video' : 'ein Bild') . ' mit dir geteilt: ' . $shareUrl; $mail->send(); echo json_encode([ 'success' => true, 'message' => 'E-Mail wurde gesendet', 'share_url' => $shareUrl ]); } catch (Exception $e) { error_log('Share email error: ' . $e->getMessage()); echo json_encode([ 'success' => false, 'error' => 'E-Mail konnte nicht gesendet werden', 'share_url' => $shareUrl // URL trotzdem zurückgeben ]); }