<?php

function isEnabled($func) {
    return is_callable($func) && false === stripos(ini_get('disable_functions'), $func);
}

$requirements = [
    'php_version' => [
        'title' => 'PHP 版本',
        'description' => '要求 PHP 版本至少为 8.0 及以上，我们强烈建议您使用 PHP 的最新版本。',
        'required' => true,
    ],
    'server_software' => [
        'title' => '服务器环境',
        'description' => '推荐使用 Apache 或 NGINX 作为本脚本的高性能服务器，所有支持 PHP+MySQL 的服务器均可兼容运行。',
    ],
    'pdo' => [
        'title' => 'PDO PHP 扩展',
        'required' => true,
    ],
    'pdo_mysql' => [
        'title' => 'PDO MySQL PHP 扩展',
        'required' => true,
    ],
    'curl' => [
        'title' => 'cURL PHP 扩展',
        'required' => true,
    ],
    'dom' => [
        'title' => 'DOM PHP 扩展',
        'required' => true,
    ],
    'openssl' => [
        'title' => 'OpenSSL PHP 扩展',
        'required' => true,
    ],
    'mbstring' => [
        'title' => 'MBString PHP 扩展',
        'required' => true,
    ],
    'exif' => [
        'title' => 'Exif PHP 扩展',
        'required' => true,
    ],
    'pcre' => [
        'title' => 'PCRE PHP 扩展',
        'required' => true,
    ],
    'sodium' => [
        'title' => 'Sodium PHP 扩展',
    ],
    'gd' => [
        'title' => 'GD PHP 扩展',
        'required' => true,
    ],
    'zip' => [
        'title' => 'Zip PHP 扩展',
        'required' => true,
    ],
    'fileinfo' => [
        'title' => 'FileInfo PHP 扩展',
        'required' => true,
    ],
    'write_permission' => [
        'title' => '文件权限',
        'required' => true,
    ],
    'json' => [
        'title' => 'Json PHP 扩展',
        'required' => true,
    ],
    'mod_rewrite' => [
        'title' => 'mod_rewrite 扩展',
        'required' => true,
    ],
    'allow_url_fopen' => [
        'title' => 'allow_url_fopen',
        'required' => true,
    ],
    'imagick' => [
        'title' => 'Imagick 扩展 (可选)',
    ],
    'output_buffering' => [
        'title' => 'Output buffering (可选)',
    ],
    'ffmpeg' => [
        'title' => 'FFmpeg (可选)',
    ],
];

if (strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), "apache") === false && strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), "litespeed") === false) {
    unset($requirements['mod_rewrite']);
} elseif (!isEnabled('apache_get_modules')) {
    unset($requirements['mod_rewrite']);
}

if (!file_exists('.htaccess')) {
    if (file_exists('htaccess.backup')) {
        rename('htaccess.backup', '.htaccess');
    }
} else if (file_exists('htaccess.backup')) {
    $htaccess_content = file_get_contents('.htaccess');
    $searchLine = 'RewriteRule ^ - [E=SUBFOLDER:%1]';

    if (strpos($htaccess_content, $searchLine) === false) {
        rename('htaccess.backup', '.htaccess');
    }
}

?>


<div class="system_requirements">
    <div class="list-group">
        <?php

        $proceed = true;

        foreach ($requirements as $index => $requirement) {
            $image = Registry::load('config')->site_url.'assets/files/defaults/installer_warning.png';

            $output = 'Disabled';
            $result = false;
            $show_status = true;

            if (isset($requirement['required']) && $requirement['required']) {
                $image = Registry::load('config')->site_url.'assets/files/defaults/installer_error.png';
            }

            if (!isset($requirement['description'])) {
                $requirement['description'] = '';
            }

            if ($index === 'php_version') {
                $output = PHP_VERSION;

                if (version_compare(PHP_VERSION, '8.0') >= 0) {
                    $result = true;
                }
            } elseif ($index === 'server_software') {
                $output = $_SERVER["SERVER_SOFTWARE"];

                if (strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), "nginx") !== false) {
                    $output .= '<br><br>你使用的是 Nginx 服务器搭配 MeiChat 程序，需配置对应的 Nginx 重写规则。.';
                }

                if (strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), "apache") !== false || strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), "litespeed") !== false || strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), "nginx") !== false) {
                    $result = true;
                }
            } elseif ($index === 'write_permission') {
                if (is_writable('assets') && is_writable('pages')) {
                    $result = true;
                    $output = 'Writable';
                } else {
                    $output = '您没有创建目录或写入文件的权限. ';
                    $output .= '请检查文件归属及文件权限。';
                }
            } elseif ($index === 'mod_rewrite') {
                if (in_array('mod_rewrite', apache_get_modules())) {
                    $result = true;
                    $output = 'Enabled';
                }
            } elseif ($index === 'output_buffering') {
                if (ini_get('output_buffering')) {
                    $result = true;
                    $output = 'Enabled';
                }
            } elseif ($index === 'allow_url_fopen') {
                if (ini_get('allow_url_fopen')) {
                    $result = true;
                    $output = 'Enabled';
                }
            } elseif ($index === 'imagick') {
                if (extension_loaded('imagick') || class_exists("Imagick")) {
                    $result = true;
                    $output = 'Enabled';
                }
            } elseif ($index === 'ffmpeg') {
                $output = 'Unrecognizable';
                $show_status = false;

                if (isEnabled('shell_exec')) {
                    $ffmpeg = shell_exec('ffmpeg -version');

                    if (!empty($ffmpeg)) {
                        if (strpos($ffmpeg, 'ffmpeg version') !== false) {
                            $result = true;
                            $output = 'Enabled';
                            $show_status = true;
                        }
                    }
                }
            } elseif (extension_loaded($index)) {
                $output = 'Enabled';
                $result = true;
            }

            if ($result) {
                $image = Registry::load('config')->site_url.'assets/files/defaults/installer_tick.png';
            } else {
                if (isset($requirement['required']) && $requirement['required']) {
                    $proceed = false;
                }
            }
            if ($show_status) {
                ?>
                <div class="list-group-item list-group-item-action d-flex gap-3 py-3" aria-current="true">
                    <img src="<?php echo $image; ?>" width="32" height="32" class="flex-shrink-0" />
                    <div class="d-flex gap-2 w-100 justify-content-between">
                        <div>
                            <h6 class="mb-0 title"><?php echo $requirement['title']; ?></h6>
                            <p class="mb-10 description">
                                <?php echo $requirement['description']; ?>
                            </p>
                            <p class="mb-0 result">
                                结果 : <?php echo $output; ?>
                            </p>
                        </div>
                    </div>
                </div>
                <?php
            }
        } ?>

    </div>
    <?php

    if (!$proceed) {
        ?>
        <div class="error mt-4 text-center">
            <div class="alert alert-danger" role="alert">
                <strong>提示 :</strong> 未满足本应用程序安装的最低要求。
            </div>
        </div>
        <?php
    } else {
        ?>
        <div class="proceed mt-4 text-center">
            <span>继续</span>
        </div>
        <?php
    } ?>
</div>