| Server IP : 188.114.96.3 / Your IP : 216.73.216.143 Web Server : Apache System : Linux sv058.home.net.pl 6.8.2 husak_dirty_pages_fix #1 SMP PREEMPT_DYNAMIC Tue Apr 2 22:06:51 CEST 2024 x86_64 User : ( 3194769) PHP Version : 8.5.12 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /polana/admin/ |
Upload File : |
<?php
declare(strict_types=1);
/** POLANA V17.6 — authenticated newsletter Excel export, no schema changes. */
require_once dirname(__DIR__) . '/includes/functions.php';
require_admin_role();
require_once dirname(__DIR__) . '/includes/polana_v176_xlsx.php';
header('Cache-Control: private, no-store, no-cache, max-age=0');
header('X-Robots-Tag: noindex, nofollow');
header('X-Content-Type-Options: nosniff');
$status = (string)($_GET['status'] ?? 'active');
if (!in_array($status, ['active','pending','unsubscribed'], true)) $status='active';
$q = trim((string)($_GET['q'] ?? ''));
$q = function_exists('mb_substr') ? mb_substr($q,0,100,'UTF-8') : substr($q,0,100);
try {
$db = db();
$check = $db->query("SHOW TABLES LIKE 'newsletter_subscribers'")->fetchColumn();
if (!$check) throw new RuntimeException('Brak tabeli newslettera. Sprawdź instalację V17.2.');
$columns = array_column($db->query('SHOW COLUMNS FROM newsletter_subscribers')->fetchAll(PDO::FETCH_ASSOC), 'Field');
foreach (['email','name','source','status','consent_at','verified_at','withdrawn_at'] as $col) {
if (!in_array($col, $columns, true)) throw new RuntimeException('Brak kolumny '.$col.' — wymagana instalacja V17.2.');
}
$sql = 'SELECT email,name,source,status,consent_at,verified_at,withdrawn_at FROM newsletter_subscribers WHERE status=?';
$params = [$status];
if ($status==='active') $sql .= ' AND verified_at IS NOT NULL';
if ($q!=='') { $sql .= ' AND (email LIKE ? OR name LIKE ?)'; $params[]='%'.$q.'%'; $params[]='%'.$q.'%'; }
$sql .= ' ORDER BY created_at DESC, id DESC LIMIT 50000';
$stmt=$db->prepare($sql); $stmt->execute($params);
$rows = (function () use ($stmt): Generator {
while ($row=$stmt->fetch(PDO::FETCH_NUM)) yield array_map(static fn($x)=>(string)($x??''), $row);
})();
$xlsx = pv176_make_xlsx($rows);
$filename='polana-newsletter-'.$status.'-'.date('Ymd-His').'.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.strlen($xlsx));
echo $xlsx;
} catch (Throwable $e) {
http_response_code(500);
header('Content-Type: text/html; charset=utf-8');
render_admin_header('Eksport Excel — błąd');
echo '<div class="card"><h1>Nie udało się przygotować eksportu</h1><p>'.htmlspecialchars($e->getMessage(),ENT_QUOTES,'UTF-8').'</p>'
.'<p><a href="/admin/polana-newsletter.php?status=active&export=csv">Pobierz CSV (działa w Excelu)</a> · '
.'<a href="/admin/polana-v176-diagnostyka.php">Diagnostyka</a></p></div>';
render_admin_footer();
}