<?php
/**
 * 兼容层：部分虚拟主机没有启用 mbstring，但站点大量使用 mb_* 函数。
 * 这里仅在扩展缺失时注册等价函数，避免安装向导或首页直接 fatal。
 */

if (!function_exists('mb_strlen')) {
    function mb_strlen($string, $encoding = null)
    {
        $string = (string) $string;
        if (preg_match('//u', $string)) {
            $count = preg_match_all('/./us', $string, $matches);
            return $count === false ? strlen($string) : $count;
        }
        return strlen($string);
    }
}

if (!function_exists('mb_substr')) {
    function mb_substr($string, $start, $length = null, $encoding = null)
    {
        $string = (string) $string;
        $start = (int) $start;
        if (preg_match('//u', $string)) {
            $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
            if (!is_array($chars)) {
                return $length === null ? substr($string, $start) : substr($string, $start, (int) $length);
            }
            if ($length === null) {
                return implode('', array_slice($chars, $start));
            }
            return implode('', array_slice($chars, $start, (int) $length));
        }
        return $length === null ? substr($string, $start) : substr($string, $start, (int) $length);
    }
}

if (!function_exists('mb_strpos')) {
    function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
    {
        return strpos((string) $haystack, (string) $needle, (int) $offset);
    }
}

if (!function_exists('mb_stripos')) {
    function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
    {
        return stripos((string) $haystack, (string) $needle, (int) $offset);
    }
}

if (!function_exists('mb_strtolower')) {
    function mb_strtolower($string, $encoding = null)
    {
        return strtolower((string) $string);
    }
}

if (!function_exists('mb_strtoupper')) {
    function mb_strtoupper($string, $encoding = null)
    {
        return strtoupper((string) $string);
    }
}

if (!function_exists('mb_check_encoding')) {
    function mb_check_encoding($string, $encoding = null)
    {
        return preg_match('//u', (string) $string) === 1;
    }
}

if (!function_exists('mb_convert_encoding')) {
    function mb_convert_encoding($string, $toEncoding, $fromEncoding = null)
    {
        $string = (string) $string;
        if (function_exists('iconv') && is_string($fromEncoding) && $fromEncoding !== ''
            && strcasecmp($fromEncoding, $toEncoding) !== 0) {
            $converted = @iconv($fromEncoding, $toEncoding, $string);
            if ($converted !== false) {
                return $converted;
            }
        }
        return $string;
    }
}

if (!function_exists('mb_detect_encoding')) {
    function mb_detect_encoding($string, $encodings = null, $strict = false)
    {
        $string = (string) $string;
        if (preg_match('//u', $string)) {
            return 'UTF-8';
        }
        if (is_array($encodings)) {
            foreach ($encodings as $encoding) {
                if (strcasecmp((string) $encoding, 'ASCII') === 0) {
                    return 'ASCII';
                }
            }
        }
        return false;
    }
}


/**
 * 虚拟主机 session.save_path 经常不可写，导致安装向导和登录会话丢失。
 * 优先使用站点 data/sessions，失败时退回系统临时目录；两者都不可用时保留原设置。
 */
if (!function_exists('km_session_prepare')) {
    function km_session_prepare($root = null)
    {
        if ($root === null) {
            if (defined('APP_ROOT')) {
                $root = APP_ROOT;
            } else {
                $root = basename(__DIR__) === 'inc' ? dirname(__DIR__) : __DIR__;
            }
        }
        $root = rtrim((string) $root, '/\\');
        $candidates = [
            $root . '/data/sessions',
            rtrim(sys_get_temp_dir(), '/\\') . '/komorebi-sessions',
        ];
        foreach ($candidates as $dir) {
            if (!is_dir($dir)) {
                @mkdir($dir, 0775, true);
            }
            if (is_dir($dir) && is_writable($dir)) {
                @ini_set('session.save_path', $dir);
                return $dir;
            }
        }
        return '';
    }
}
