<?php
use Cake\Core\Configure;
use Cake\Error\Debugger;
use Cake\Datasource\ConnectionManager;

if (Configure::read('debug') > 0) {
    Debugger::checkSecurityKeys();
}

$memory_limit = ini_get('memory_limit');
if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
    if ($matches[2] == 'M') {
        $memory_limit = $matches[1] * 1024 * 1024;
    } else if ($matches[2] == 'K') {
        $memory_limit = $matches[1] * 1024;
    }
}
$res = preg_replace("/[^0-9]/", "", $memory_limit);
$error = false;
$filePresent = null;
if (file_exists(ROOT . '/config/app.php')) {
    $database_file = "<span class='label label-success'>Success</span>";
    $filePresent = true;
} else {
    $database_file = "<span class='label label-warning'>Failed</span>";
}

if (isset($filePresent)) {
    try {
        $connected = ConnectionManager::get('default');
    } catch (Exception $connectionError) {
        $connected = false;
        $errorMsg = $connectionError->getMessage();
    }
    if ($connected && $connected->isConnected()) {
        $database_connection = "<span class='label label-success'>Success</span>";
    } else {
        $database_connection = "<span class='label label-warning'>Failed</span>";
    }
}

if (version_compare(PHP_VERSION, '8.0.0', '>=') && version_compare(PHP_VERSION, '8.3.0', '<')) {
    $requirement1 = "<span class='label label-success'>v." . PHP_VERSION . "</span>";
} else {
    $error = true;
    $requirement1 = "<span class='label label-warning'>Your PHP version is " . PHP_VERSION . "</span>";
}

if (extension_loaded('redis')) {
    $requirement0 = "<span class='label label-success'>Redis extension is installed</span>";
} else {
    $error = true;
    $requirement0 = "<span class='label label-warning'>Redis extension is NOT installed</span>";
}

$file = ROOT . '/webroot/cron.txt';
if (file_exists($file)) {
    $cron_msg = "<span class='label label-success'>Cron running successfully. Make sure you have added deepengin key</span>";
} else {
    $cron_msg = "<span class='label label-warning'>Cron not running which means the videos cannot be moderated or can't be compressed. Please check the documentation</span>";
}

if (!extension_loaded('pdo')) {
    $error = true;
    $requirement3 = "<span class='label label-warning'>Not enabled</span>";
} else {
    $requirement3 = "<span class='label label-success'>Enabled</span>";
}

if (!extension_loaded('curl')) {
    $error = true;
    $requirement4 = "<span class='label label-warning'>Not enabled</span>";
} else {
    $requirement4 = "<span class='label label-success'>Enabled</span>";
}

if (!extension_loaded('openssl')) {
    $error = true;
    $requirement5 = "<span class='label label-warning'>Not enabled</span>";
} else {
    $requirement5 = "<span class='label label-success'>Enabled</span>";
}

if (!extension_loaded('mbstring')) {
    $error = true;
    $requirement6 = "<span class='label label-warning'>Not enabled</span>";
} else {
    $requirement6 = "<span class='label label-success'>Enabled</span>";
}

if (!extension_loaded('ctype') && !function_exists('ctype')) {
    $error = true;
    $requirement7 = "<span class='label label-warning'>Not enabled</span>";
} else {
    $requirement7 = "<span class='label label-success'>Enabled</span>";
}

if (!extension_loaded('gd')) {
    $error = true;
    $requirement9 = "<span class='label label-warning'>Not enabled</span>";
} else {
    $requirement9 = "<span class='label label-success'>Enabled</span>";
}

if (!extension_loaded('zip')) {
    $error = true;
    $requirement10 = "<span class='label label-warning'>Zip Extension is not enabled</span>";
} else {
    $requirement10 = "<span class='label label-success'>Enabled</span>";
}

$url_f_open = ini_get('allow_url_fopen');
if ($url_f_open != "1" && $url_f_open != "On") {
    $error = true;
    $requirement11 = "<span class='label label-warning'>Allow_url_fopen is not enabled!</span>";
} else {
    $requirement11 = "<span class='label label-success'>Enabled</span>";
}

$requirement12 = "<span class='label label-success'>  $memory_limit</span>";
$test_cmd = "ls";
$exec_test_output = [];
$shell_exec_test_output = "";
if (function_exists('exec')) {
    exec($test_cmd, $exec_test_output);
    if (!empty($exec_test_output)) {
        $exec = "<span class='label label-success'>Enabled</span>";
    } else {
        $exec = "<span class='label label-warning'>You have to enable exec from your server</span>";
    }
} else {
    $exec = "<span class='label label-warning'>You have to enable exec from your server.</span>";
}

if (function_exists('shell_exec')) {
    $shell_exec_test_output = shell_exec($test_cmd);
    if (strlen($shell_exec_test_output) > 2) {
        $shell_exec = "<span class='label label-success'>Enabled</span>";
    } else {
        $shell_exec = "<span class='label label-warning'>You have to enable shell_exec from your server</span>";
    }
} else {
    $shell_exec = "<span class='label label-warning'>You have to enable shell_exec from your server.</span>";
}

if (function_exists('shell_exec')) {
    $ffmpeg_output = trim(shell_exec('ffmpeg -version'));
    $ffmpeg_exploded = explode(" ", $ffmpeg_output);
    if (count($ffmpeg_exploded) < 2) {
        $ffmpeg = "<span class='label label-warning'>You have to enable shell_exec() from your server</span>";
    } else {
        $ffmpeg = "<span class='label label-success'>" . $ffmpeg_exploded[2] . "</span>";
    }
} else {
    $ffmpeg = "<span class='label label-warning'>You have to enable shell_exec() from your server</span>";
}

$upload_max_size = ini_get('upload_max_filesize');
$size = str_replace("M", "", "$upload_max_size");
if ($size >= 100) {
    $upload_max_filesize = "<span class='label label-success'>upload_max_filesize is Ok</span>";
} else {
    $upload_max_filesize = "<span class='label label-warning'>upload_max_filesize must be more than 100M</span>";
}
$upload_max_size = ini_get('post_max_size');
$size = str_replace("M", "", "$upload_max_size");
if ($size >= 100) {
    $post_max_size = "<span class='label label-success'>post_max_size is Ok</span>";
} else {
    $post_max_size = "<span class='label label-warning'>post_max_size must be more than 100M</span>";
}
if (defined('MEDIA_STORAGE') && MEDIA_STORAGE == "s3") {
    if (isset($config_data) && count($config_data) > 0) {
        if (array_key_exists("s3", $config_data)) {
            $code = $config_data['s3']['code'];
            $msg = $config_data['s3']['msg'];
            if ($code > 200) {
                $s3 = "<span class='label label-warning'>$msg</span>";
            } else {
                $s3 = "<span class='label label-success'><a href='$msg' target='_blank' style='color:white;'>View Image</a></span>";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Qboxus - Server Requirements</title>
    <style>
        body {
            padding-top: 18px;
            font-family: sans-serif;
            background: #f9fafb;
            font-size: 14px;
        }
        #container {
            width: 600px;
            margin: 0 auto;
            background: #fff;
            border-radius: 10px;
            padding: 15px;
            border: 2px solid #f0f0f0;
            -webkit-box-shadow: 0px 1px 15px 1px rgba(90, 90, 90, 0.08);
            box-shadow: 0px 1px 15px 1px rgba(90, 90, 90, 0.08);
        }
        a {
            text-decoration: none;
            color: red;
        }
        h1 {
            text-align: center;
            color: #424242;
            border-bottom: 1px solid #e4e4e4;
            padding-bottom: 25px;
            font-size: 22px;
            font-weight: normal;
        }
        table {
            width: 100%;
            padding: 10px;
            border-radius: 3px;
        }
        table thead th {
            text-align: left;
            padding: 5px 0;
        }
        table tbody td {
            padding: 5px 0;
        }
        table tbody td:last-child, table thead th:last-child {
            text-align: right;
        }
        .label {
            padding: 3px 10px;
            border-radius: 4px;
            color: #fff;
        }
        .label.label-success {
            background: #4ac700;
        }
        .label.label-warning {
            background: #dc2020;
        }
        .logo {
            margin-bottom: 30px;
            margin-top: 20px;
            display: block;
        }
        .logo img {
            margin: 0 auto;
            display: block;
            border-radius: 50%;
        }
        .scene {
            width: 100%;
            height: 100%;
            perspective: 600px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        svg {
            width: 240px;
            height: 240px;
        }
        @keyframes arrow-spin {
            50% {
                transform: rotateY(360deg);
            }
        }
    </style>
</head>
<body>
<div id="container">
    <div class="logo">
        <a href="#">
            <img width="80px" src="data:image/jpeg;base64, ">
        </a>
    </div>
    <h1>Server Requirements</h1>
    <table class="table table-hover" id="requirements">
        <thead>
            <tr>
                <th>Requirements</th>
                <th>Result</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Redis</td>
                <td><?php echo $requirement0; ?></td>
            </tr>
            <tr>
                <td>Database file</td>
                <td><?php echo $database_file; ?></td>
            </tr>
            <tr>
                <td>Database Connection</td>
                <td><?php echo $database_connection; ?></td>
            </tr>
            <tr>
                <td>PHP</td>
                <td><?php echo $requirement1; ?></td>
            </tr>
            <tr>
                <td>PDO PHP Extension</td>
                <td><?php echo $requirement3; ?></td>
            </tr>
            <tr>
                <td>cURL PHP Extension</td>
                <td><?php echo $requirement4; ?></td>
            </tr>
            <tr>
                <td>OpenSSL PHP Extension</td>
                <td><?php echo $requirement5; ?></td>
            </tr>
            <tr>
                <td>MBString PHP Extension</td>
                <td><?php echo $requirement6; ?></td>
            </tr>
            <tr>
                <td>GD PHP Extension</td>
                <td><?php echo $requirement9; ?></td>
            </tr>
            <tr>
                <td>Zip PHP Extension</td>
                <td><?php echo $requirement10; ?></td>
            </tr>
            <tr>
                <td>exec</td>
                <td><?php echo $exec; ?></td>
            </tr>
            <tr>
                <td>shell_exec</td>
                <td><?php echo $shell_exec; ?></td>
            </tr>
            <tr>
                <td>allow_url_fopen</td>
                <td><?php echo $requirement11; ?></td>
            </tr>
            <tr>
                <td>ffmpeg</td>
                <td><?php echo $ffmpeg; ?></td>
            </tr>
            <tr>
                <td>Cron - Nudity Detection / Compressing Videos or images</td>
                <td><?php echo $cron_msg; ?></td>
            </tr>
            <?php if (defined('MEDIA_STORAGE') && MEDIA_STORAGE == "s3") { ?>
            <tr>
                <td>AWS S3 Bucket Permission</td>
                <td><?php echo $s3; ?></td>
            </tr>
            <?php } ?>
            <tr>
                <td>Memory Limit</td>
                <td><?php echo $requirement12; ?></td>
            </tr>
        </tbody>
    </table>
    <br/>
    <?php if (defined('APP_STATUS') && APP_STATUS == "live") { ?>
    <div style="line-height: 24px;">
        <h1>Admin Portal Config</h1>
        <?php
        $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $base_url = substr($current_url, 0, strpos($current_url, "config"));
        if (strlen($base_url) < 1) {
            $base_url = $current_url;
        }
        echo '$baseurl= "' . $base_url . '";<br>';
        echo 'define("status" , "live");<br>';
        echo 'define("API_KEY" , "' . ADMIN_API_KEY . '");<br>';
        ?>
        <br>
        <h1>Android App Config</h1>
        <?php
        $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $base_url = substr($current_url, 0, strpos($current_url, "config"));
        if (strlen($base_url) < 1) {
            $base_url = $current_url;
        }
        echo 'public static final String BASE_URL= "' . $base_url . '";<br>';
        echo 'public static final String API_KEY= "' . API_KEY . '";<br>';
        ?>
        <br>
        <h1>IOS App Config</h1>
        <?php
        $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $base_url = substr($current_url, 0, strpos($current_url, "config"));
        if (strlen($base_url) < 1) {
            $base_url = $current_url;
        }
        echo 'var BASE_URL = "' . $base_url . '"<br>';
        echo 'let API_KEY= "' . API_KEY . '"<br>';
        ?>
    </div>
    <?php } ?>
</div>
</body>
</html>