| 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);
require_once dirname(__DIR__) . '/includes/functions.php';
require_login();
require_admin_role();
$oldPattern = '~https?://(?:www\.)?polanasmakow\.pl(?=[/?#]|$)~i';
$destination = 'https://mojapolanasmakow.pl';
$canMigrate = rtrim(site_url(), '/') === $destination;
$error = '';
$changed = [];
$found = [];
// Curated set of text columns only. No blind replacement across all DB tables:
// no serialized session values, encrypted secrets, file paths or external URLs.
$targets = [
'settings' => ['key' => 'setting_key', 'columns' => ['setting_value']],
'blog_posts' => ['key' => 'id', 'columns' => ['content', 'content_en', 'excerpt', 'excerpt_en', 'featured_image']],
'menus' => ['key' => 'id', 'columns' => ['pdf_path']],
'menu_items' => ['key' => 'id', 'columns' => ['image_path']],
];
try {
$pdo = db();
foreach ($targets as $table => $info) {
try {
$fields = $pdo->query("SHOW COLUMNS FROM `$table`")->fetchAll(PDO::FETCH_COLUMN, 0);
} catch (Throwable $ignored) {
continue; // Table absent on an older installation.
}
$key = $info['key'];
if (!in_array($key, $fields, true)) continue;
foreach ($info['columns'] as $column) {
if (!in_array($column, $fields, true)) continue;
$stmt = $pdo->prepare("SELECT `$key` AS item_id, `$column` AS old_value FROM `$table` WHERE `$column` LIKE ?");
$stmt->execute(['%polanasmakow.pl%']);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$before = (string)($row['old_value'] ?? '');
if (!preg_match($oldPattern, $before)) continue;
$after = preg_replace($oldPattern, $destination, $before);
if (!is_string($after) || $after === $before) continue;
$found[] = [
'table' => $table, 'key' => $key, 'id' => $row['item_id'],
'column' => $column, 'before' => $before, 'after' => $after,
];
}
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf();
if (!$canMigrate) throw new RuntimeException('Przełącz najpierw domenę plikiem includes/polana_domain.php z ETAPU 2.');
$pdo->beginTransaction();
foreach ($found as $item) {
$table = $item['table'];
$key = $item['key'];
$column = $item['column'];
// Values verified above, key/table/column identifiers from hard-coded allowlist.
$stmt = $pdo->prepare("UPDATE `$table` SET `$column` = ? WHERE `$key` = ? AND `$column` = ?");
$stmt->execute([$item['after'], $item['id'], $item['before']]);
if ($stmt->rowCount() > 0) {
$changed[] = $table . '.' . $column . ' (rekord ' . $item['id'] . ')';
}
}
$pdo->commit();
$found = [];
}
} catch (Throwable $e) {
if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) $pdo->rollBack();
$error = $e->getMessage();
}
render_admin_header('Migracja domeny i linków');
?>
<div class="admin-title-row"><div>
<h1>Przenoszenie odnośników na nową domenę</h1>
<p>Kontrolowana migracja absolutnych URL-i ze starej domeny. Bez zmian w innych tabelach, danych logowania i sesjach.</p>
</div></div>
<section class="form-card">
<p>Aktywny adres strony: <strong><?= e(site_url()) ?></strong></p>
<p>Nowy adres: <strong><?= e($destination) ?></strong></p>
<p><strong>Wykonaj kopię bazy przed uruchomieniem.</strong> Najpierw zobacz podgląd; modyfikacje wymagają zatwierdzenia administratora.</p>
<?php if ($error): ?><div class="alert alert-error"><?= e($error) ?></div><?php endif; ?>
<?php if ($changed): ?><div class="alert alert-success">Zmieniono <?= count($changed) ?> wartości w bazie.</div><?php endif; ?>
<?php if (!$canMigrate): ?><p>Przełączenie jest niedostępne, dopóki aktywna domena nie będzie docelowa.</p><?php endif; ?>
<?php if (!$found): ?><p>Brak starych adresów w sprawdzonych polach ustawień, wpisów, menu i zdjęć.</p><?php else: ?>
<h2>Do aktualizacji: <?= count($found) ?> wartości</h2>
<ul>
<?php foreach (array_slice($found, 0, 100) as $item): ?>
<li><code><?= e($item['table'] . '.' . $item['column']) ?></code>
(rekord <?= e((string)$item['id']) ?>):
<?= e(mb_substr($item['before'], 0, 130, 'UTF-8')) ?>…</li>
<?php endforeach; ?>
</ul>
<?php if (count($found) > 100): ?><p>Pozostałe dopasowania: <?= count($found) - 100 ?>.</p><?php endif; ?>
<?php if ($canMigrate): ?>
<form method="post"><?= csrf_field() ?>
<button type="submit" class="btn btn-primary">Zmień odnośniki na nową domenę</button>
</form>
<?php endif; ?>
<?php endif; ?>
<p><a href="/admin/settings.php">Wróć do ustawień</a></p>
</section>
<?php render_admin_footer();