Siteβ―Builder
Editing:
statesadmin.php
writable 0666
<?php /************************************************************************** * statesadmin.phpΒ β build /geo/USA/{STATE}/{CITY}/location.txt * **************************************************************************/ require_once rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/members/lib/auth.php'; require_login(); if (current_user()['role'] !== 'admin') forbidden_page(); error_reporting(E_ALL); ini_set('display_errors','1'); set_time_limit(0); ob_implicit_flush(true); $baseGeoDir = __DIR__; // β¦/geo (script lives here) $jsonDir = $baseGeoDir.'/json-data'; // β¦/geo/json-data $locationFile = 'location.txt'; /* ---------- UI ---------- */ if (!isset($_GET['action'])) { ?> <!DOCTYPE html><html lang="en"><meta charset="utf-8"> <title>City Folder & Location File Generator</title> <style> body{font-family:sans-serif;margin:2rem;} #run{padding:.6rem 1.2rem;font-size:1rem} #bar{background:#ddd;height:22px;width:100%;max-width:640px;margin-top:1rem;position:relative} #bar span{display:block;height:100%;width:0;background:#4caf50} #log{margin-top:.5rem;font-family:monospace;white-space:pre-line} header { display: flex; align-items: center; justify-content: space-between; background: #fff; padding: 0.5rem 1rem; border-bottom: 1px solid #ddd; } header img { height: 40px; } nav a { margin-left: 1rem; text-decoration: none; color: #333; font-weight: bold; } main { padding: 1rem; max-width: 500px; margin: 0 auto; text-align: center; } </style> <header> <div><a href="/members/dashboard.php">Dashboard</a></div> <nav> <a href="/geo/statesadmin.php">Create States</a><a href="/geo/pages/update.php">Create City Pages</a> <a href="/geo/category-uploader.php">Create Categories</a> </nav> </header> <main> <h1>City Folder & Location File Generator</h1> <p>This scans <code><?=htmlspecialchars($jsonDir)?></code>, then builds <code>/geo/USA/STATE/CITY/<?=$locationFile?></code>.</p> <button id="run">Process all states</button> <div id="bar"><span></span></div> <div id="log">Idleβ¦</div> </main> <script> const btn=document.getElementById('run'), bar=document.querySelector('#bar span'), log=document.getElementById('log'); btn.onclick=()=>{ btn.disabled=true; log.textContent='Startingβ¦'; const es=new EventSource('statesadmin.php?action=run'); es.onmessage=e=>{ const j=JSON.parse(e.data); if(j.error){log.textContent+='β '+j.error+'\n';return;} bar.style.width=j.pct+'%'; log.textContent=`States ${j.stateDone}/${j.stateTotal} β’ Cities ${j.cityDone}`; if(j.done){es.close();log.textContent+=' β All done.';} }; es.onerror=_=>{es.close();log.textContent+='β connection lost.';}; }; </script></html><?php exit; } /* ---------- Worker (SSE) ---------- */ header('Content-Type:text/event-stream'); header('Cache-Control:no-cache'); $send=function($a){echo 'data:'.json_encode($a)."\n\n"; flush();}; /* twoβletter state files only */ $stateFiles=array_values(array_filter(scandir($jsonDir), fn($f)=>preg_match('/^[A-Z]{2}\.json$/i',$f))); $stateTotal=count($stateFiles); $stateDone=0; $cityDone=0; foreach($stateFiles as $file){ $jdata=json_decode(@file_get_contents("$jsonDir/$file"),true); if(!is_array($jdata)){ $send(['error'=>"Cannot parse $file"]); continue; } foreach($jdata as $city){ if(!isset($city['url'],$city['lat'],$city['lon'])) continue; $url = str_replace('\\/', '/', $city['url']); $url = trim($url, '/'); if (stripos($url,'geo/')===0) $url = substr($url,4); // Capitalize only the city segment: $segments = explode('/', $url); $citySeg = array_pop($segments); $citySeg = ucfirst($citySeg); $dest = $baseGeoDir . '/' . implode('/', $segments) . '/' . $citySeg; if(!is_dir($dest) && !mkdir($dest,0755,true)){ $send(['error'=>"mkdir failed: $dest"]); continue; } $coords = $city['lat'].', '.$city['lon']; if(file_put_contents("$dest/$locationFile",$coords)===false){ $send(['error'=>"write failed: $dest/$locationFile"]); continue; } ++$cityDone; } ++$stateDone; $send([ 'stateDone'=>$stateDone,'stateTotal'=>$stateTotal, 'cityDone'=>$cityDone,'pct'=>round(($stateDone/$stateTotal)*100,1), 'done'=>false ]); } /* final packet */ $send(['stateDone'=>$stateDone,'stateTotal'=>$stateTotal, 'cityDone'=>$cityDone,'pct'=>100,'done'=>true]); exit;
Save changes
Create folder
writable 0777
Create
Cancel