<?php
function cms_admin_config(){
    static $cfg = null;
    if($cfg !== null) return $cfg;
    $file = __DIR__ . '/../config/yanscms.php';
    $cfg = is_file($file) ? include $file : array();
    if(!is_array($cfg)) $cfg = array();
    $dir = trim((string)($cfg['admin_dir'] ?? 'yanscms'), '/');
    $dir = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dir);
    if($dir === '') $dir = 'yanscms';
    $cfg['admin_dir'] = $dir;
    return $cfg;
}
function cms_admin_dir(){
    $cfg = cms_admin_config();
    return $cfg['admin_dir'];
}
function cms_admin_url($path=''){
    return '/' . cms_admin_dir() . ($path === '' ? '/' : '/' . ltrim($path, '/'));
}
function cms_is_admin_request(){
    $uri = $_SERVER['REQUEST_URI'] ?? '';
    return strpos($uri, '/' . cms_admin_dir() . '/') === 0 || rtrim($uri, '/') === '/' . cms_admin_dir();
}
