<?php
function chairman_h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
function chairman_q($db,$s){ return "'" . $db->real_escape_string((string)$s) . "'"; }
function chairman_n($v,$d=0){ return is_numeric($v) ? (float)$v : $d; }
function chairman_i($v,$d=0){ return is_numeric($v) ? (int)$v : $d; }
function chairman_rows($db,$sql){ $a=array(); $r=@$db->query($sql); if($r){ while($x=$r->fetch_assoc()) $a[]=$x; } return $a; }
function chairman_table_exists($db,$t){ $r=@$db->query('SHOW TABLES LIKE ' . chairman_q($db,$t)); return $r && $r->num_rows > 0; }
function chairman_json_response($payload){ header('Content-Type: application/json; charset=utf-8'); echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); exit; }
function chairman_safe_date($s){ return preg_match('/^\d{4}-\d{2}-\d{2}$/', (string)$s) ? $s : date('Y-m-d'); }
function chairman_cache_filename($raceDate,$venue='',$raceNum=0){
    $safeVenue = preg_replace('/[^a-zA-Z0-9_\-]/','_', (string)$venue);
    $file = 'chairman_' . $raceDate;
    if($safeVenue !== '') $file .= '_' . $safeVenue;
    if(chairman_i($raceNum)>0) $file .= '_R' . chairman_i($raceNum);
    return $file . '.json';
}
function chairman_public_path($path, $config=array()){
    if(isset($config['hide_absolute_path']) && $config['hide_absolute_path']){
        return basename($path);
    }
    return $path;
}
