Siteβ―Builder
Editing:
save-profile.php
writable 0666
<?php /***************************************************************** * Dynamic Profile Wizard βΒ SAVE HANDLER * β’ collects each stepβs POST * β’ if not last step β redirect back to build-profile.php?step=next * β’ on last step β write profile.json + blank {} for skipped steps *****************************************************************/ require_once __DIR__.'/../lib/db.php'; require_once __DIR__.'/../lib/paths.php'; require_once __DIR__.'/../lib/write-profile.php'; session_start(); //require_login(); /* ---------- basic inputs ---------------- */ $kind = $_POST['kind'] ?? ''; $step = intval($_POST['step'] ?? 1); $tplList = json_decode($_POST['tplList'] ?? '[]', true); // full template list $total = count($tplList); if (!$kind || !$total) exit('Bad request'); /* ---------- store this step in session --- */ $skipped = !empty($_POST['skip']); // user pressed βSkip βΆβ $_SESSION['wizard'][$kind][$step] = $_POST; $_SESSION['wizard']['skip'][$kind][$step] = $skipped; /* ---------- advance if not last ---------- */ if ($step < $total) { header("Location: build-profile.php?kind=$kind&step=".($step+1)); exit; } /* ---------- final step: merge + write ---- */ $user = current_user(); $first = $_SESSION['wizard'][$kind][1] ?? []; $slug = $first['phone'] ?? $first['handle'] ?? ''; if ($kind==='business') { if (!preg_match('/^\d{10}$/', $slug)) exit('Bad phone'); $dir = business_path($slug); } else { if (!preg_match('/^[A-Za-z0-9_-]{3,32}$/', $slug)) exit('Bad handle'); $dir = social_path($slug); } /* lock slug in DB (idempotent) */ global $db; $db->beginTransaction(); $db->prepare('UPDATE users SET site_slug=? WHERE id=? AND site_slug IS NULL') ->execute([$slug, $user['id']]); $db->commit(); /* merge posted chunks, stripping helper keys */ $profile = []; foreach ($_SESSION['wizard'][$kind] as $arr) { $profile += array_diff_key($arr, ['kind'=>1,'step'=>1,'tplList'=>1,'skip'=>1]); } /* write main profile.json */ write_profile_json($dir, $profile); /* write blank {} for EVERY skipped step */ foreach ($tplList as $i=>$tplPath) { $idx = $i + 1; // 1βbased $name = basename($tplPath, '.template.json') . '.json'; if (!empty($_SESSION['wizard']['skip'][$kind][$idx]) && !is_file($dir.$name)) { file_put_contents($dir.$name, "{}"); } } /* cleanup session */ unset($_SESSION['wizard'][$kind]); unset($_SESSION['wizard']['skip'][$kind]); /* redirect user to their public page */ header("Location: /$slug/"); exit;
Save changes
Create folder
writable 0777
Create
Cancel