Siteβ―Builder
Editing:
helpers.php
writable 0666
<?php /************************************************************************** * helpers.php β minimal, selfβcontained loader * --------------------------------------------------------------- * β’ Autodetects whether the request is for /social/<slug>/β¦ or * /ph/<10βdigit>/β¦ (business) and points all JSON reads there. * β’ No external assets, no global state beyond one static cache. * β’ Wrapper functions: get_profile(), get_prompts(), get_coupon(), * get_hours(), get_links(), get_rss_cache(), * get_cta() (add more if you create new files) **************************************************************************/ /* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ * 1. Detect base folder (memoised after first call) * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ function base_dir(): string { static $dir; if ($dir) return $dir; // already solved $root = $_SERVER['DOCUMENT_ROOT'] ?? ''; $uri = $_SERVER['REQUEST_URI'] ?? ''; /* A) Path already contains `/ph/1234567890/β¦` or `/social/slug/β¦` */ if (preg_match('#/ph/(\d{10})(?:/|$)#i', $uri, $m)) { return $dir = "$root/ph/{$m[1]}"; } if (preg_match('#/social/([a-z0-9_]+)(?:/|$)#i', $uri, $m)) { return $dir = "$root/social/{$m[1]}"; } /* B) Fallback to query parameters (?ph=β¦ or ?user=β¦) */ $ph = preg_replace('/\D/', '', $_GET['ph' ] ?? ''); if (strlen($ph) === 10) return $dir = "$root/ph/$ph"; $slug = preg_replace('/[^a-z0-9_]/i', '', $_GET['user'] ?? ''); if ($slug) return $dir = "$root/social/$slug"; /* C) Nothing matched β hardβfail so it surfaces in the error log */ throw new RuntimeException('helpers.php: cannot determine profile folder'); } /* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ * 2. Tiny JSON loader with oneβperβrequest cache & silent fail * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ function load_json(string $fn, array $def = []): array { static $cache = []; $abs = base_dir() . "/$fn"; if (isset($cache[$abs])) return $cache[$abs]; if (!is_readable($abs)) return $cache[$abs] = $def; $raw = file_get_contents($abs); $data = json_decode($raw, true); return $cache[$abs] = (is_array($data) ? $data : $def); } /* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ * 3. Convenience wrappers used by your modules * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ function get_profile () { return load_json('profile.json', []); } function get_prompts () { return load_json('prompts.json', []); } function get_coupon () { return load_json('coupon.json', []); } function get_hours () { return load_json('hours.json', []); } function get_links () { return load_json('links.json', []); } function get_rss_cache () { return load_json('rss-cache.json', []); } function get_cta () { return load_json('cta.json', []); } function get_config () { return load_json('config.json', []); } function get_faq () { return load_json('faq.json', []); } /* ---------- helper also used by your index.php (unchanged) ---------- */ function json_safe(string $f, array $def = []): array { return (is_file($f) && ($j = json_decode(file_get_contents($f), true))) ? $j : $def; } ?>
Save changes
Create folder
writable 0777
Create
Cancel