Site Builder
Editing:
backup.php
writable 0666
<?php /************************************************************************** * BACKUP & RESTORE (single‑file bundle) * ---------------------------------------------------------------------- * • Works for *either* business or social accounts. * • Produces one JSON bundle: { * "meta": { "slug": "...", "type": "biz|social", "created": "…" }, * "files": { "profile.json": {…}, "coupon.json": {…}, … } * } * • Restore: verifies slug ↔ logged‑in user, then rewrites only JSON files. * • Requires PHP ≥ 8.1 **************************************************************************/ require_once __DIR__.'/lib/auth.php'; require_login(); $user = current_user(); // username is your slug $slug = $user['username']; $isBiz = ctype_digit($slug); // 10‑digit → /ph/ $root = rtrim($_SERVER['DOCUMENT_ROOT'],'/'); $dir = $isBiz ? "$root/ph/$slug" : "$root/social/$slug"; if (!is_dir($dir)) { http_response_code(404); exit('Profile folder not found: '.$dir); } /* ---------- POST (restore) ----------------------------------------- */ if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_FILES['bundle'])) { if (!hash_equals($_SESSION['csrf'] ?? '', $_POST['csrf'] ?? '')) { exit('Bad CSRF token'); } $tmp = $_FILES['bundle']['tmp_name'] ?? ''; $raw = $tmp && is_uploaded_file($tmp) ? file_get_contents($tmp) : ''; $j = json_decode($raw, true); /* minimal sanity checks */ if (!$j || !isset($j['meta'],$j['files']) || $j['meta']['slug'] !== $slug) { exit('Invalid or foreign bundle.'); } /* rewrite only *.json that were in the bundle */ foreach ($j['files'] as $name=>$data) { if (!str_ends_with($name,'.json')) continue; $path = $dir.'/'.basename($name); file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE), LOCK_EX ); } header('Location: backup.php?restored=1'); exit; } /* ---------- GET (download) ---------------------------------------- */ if (isset($_GET['download'])) { /* grab every JSON in profile folder (one level) */ $files = []; foreach (glob($dir.'/*.json') as $f) { $files[basename($f)] = json_decode(file_get_contents($f), true); } $bundle = [ 'meta'=>[ 'slug' => $slug, 'type' => $isBiz ? 'biz' : 'social', 'created'=> date(DATE_ATOM) ], 'files'=> $files ]; header('Content-Type: application/json'); header('Content-Disposition: attachment; filename="backup-'.$slug.'.json"'); echo json_encode($bundle, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); exit; } /* ---------- CSRF + tiny UI ---------------------------------------- */ $_SESSION['csrf'] = bin2hex(random_bytes(16)); ?> <!doctype html> <title>Backup & Restore – BestDealOn</title> <meta name=viewport content="width=device-width,initial-scale=1"> <style> :root{--b:#0066ff;--bg:#f6f9ff;--fg:#111;font-family:-apple-system,BlinkMacSystemFont, Segoe UI,Roboto,Helvetica,Arial,sans-serif} body{margin:0;background:var(--bg);color:var(--fg)} main{max-width:600px;margin:3rem auto;padding:2rem;background:#fff;border-radius:12px; box-shadow:0 4px 22px #0001;text-align:center} h1{margin-top:0;font-size:1.6rem} button,label{display:inline-block;margin:.8rem 0;padding:.7rem 1.6rem;border-radius:8px; cursor:pointer;font-weight:700;font-size:1.05rem;border:none} button.dl{background:var(--b);color:#fff} button.up{background:#27ae60;color:#fff} input[type=file]{display:none} .notice{margin:1rem 0;color:#0a7b38;font-weight:700} </style> <main> <h1>Backup / Restore<br><small><?= htmlspecialchars($slug) ?></small></h1> <?php if(isset($_GET['restored'])):?> <div class=notice>✔ Profile restored successfully</div> <?php endif; ?> <p> <a href="?download=1"><button class="dl">⬇️ Download backup</button></a> </p> <form method=post enctype="multipart/form-data"> <input type=hidden name=csrf value="<?= $_SESSION['csrf'] ?>"> <label class="up"> ⬆️ Restore from backup <input type=file name=bundle accept="application/json" onchange="this.form.submit()"> </label> </form> <p style="margin-top:2rem"> <a href="/members/dashboard.php">← Back to dashboard</a> </p> </main>
Save changes
Create folder
writable 0777
Create
Cancel