<?php
function e($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
function url($path=''){ $base=rtrim(APP_URL,'/'); return $base.'/'.ltrim($path,'/'); }
function redirect($path){ header('Location: '.url($path)); exit; }
function csrf_token(){ if(empty($_SESSION['csrf'])) $_SESSION['csrf']=bin2hex(random_bytes(24)); return $_SESSION['csrf']; }
function csrf_field(){ return '<input type="hidden" name="csrf" value="'.e(csrf_token()).'">'; }
function verify_csrf(){ return isset($_POST['csrf'],$_SESSION['csrf']) && hash_equals($_SESSION['csrf'],$_POST['csrf']); }
function flash($type,$message){ $_SESSION['flash']=array('type'=>$type,'message'=>$message); }
function pull_flash(){ $f=isset($_SESSION['flash'])?$_SESSION['flash']:null; unset($_SESSION['flash']); return $f; }
function json_out($data,$status=200){ http_response_code($status); header('Content-Type: application/json'); echo json_encode($data); exit; }
function role_home($role){ $map=array('business'=>'business/','provider'=>'provider/','operator'=>'operations/','admin'=>'admin/'); return isset($map[$role])?$map[$role]:'dashboard/'; }
function money($n,$currency){ return number_format((float)$n,2).' '.e($currency); }
