/**
 * misc
 *
 * @param $action
 */
function sa_misc($action = "")
{
    global $cfg, $error, $statusImage, $statusMessage, $htmlTitle, $htmlTop, $htmlMain;
    if ($action == "") {
        return;
    }
    buildPage("y");
    switch ($action) {
        case "0":
            // misc-main
            $htmlTitle = "Miscellaneous Admin Tasks";
            $htmlMain .= '<br>Select the task you wish to perform from below:<p>';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=1"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="List files installed" border="0"> Lists</a> - view a list of currently installed torrentflux-b4rt files';
            $htmlMain .= '<p>';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=3"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Checksum Validation" border="0"> Checksum Validation</a> - check the integrity of installed torrentflux-b4rt files';
            $htmlMain .= '<p>';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=5"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Check Requirements" border="0"> Check Requirements</a> - check your server meets the requirements to run torrentflux-b4rt';
            $htmlMain .= '<br><br>';
            break;
        case "1":
            // misc - Lists
            $htmlTitle = "Misc - File Lists";
            $htmlMain .= '<br>Select an option from below:<p>';
            $htmlMain .= '<img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Files" border="0"> Files (';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=11" target="_blank">html</a>';
            $htmlMain .= ' / ';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=12" target="_blank">text</a>';
            $htmlMain .= ') - list files currently installed';
            $htmlMain .= '<p>';
            $htmlMain .= '<img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Checksums" border="0"> Checksums (';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=13" target="_blank">html</a>';
            $htmlMain .= ' / ';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=14" target="_blank">text</a>';
            $htmlMain .= ') - list checksums of files currently installed';
            $htmlMain .= '<br><br>';
            break;
        case "11":
            // Misc - File-List - html
            printFileList($cfg['docroot'], 1, 2);
            exit;
        case "12":
            // Misc - File-List - text
            @header("Content-Type: text/plain");
            printFileList($cfg['docroot'], 1, 1);
            exit;
        case "13":
            // Misc - Checksums-List - html
            printFileList($cfg['docroot'], 2, 2);
            exit;
        case "14":
            // Misc - Checksums-List - text
            @header("Content-Type: text/plain");
            printFileList($cfg['docroot'], 2, 1);
            exit;
        case "3":
            // Misc - Checksums
            $htmlTitle = "Misc - Checksum Validation";
            $htmlMain .= '<p>';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=35" target="_blank"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Checksums of ' . _VERSION . '" border="0"> Checksums of ' . _VERSION . '</a>';
            $htmlMain .= '<p>';
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=36" target="_blank"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Validate local files" border="0"> Validate local files</a>';
            $htmlMain .= '<br><br>';
            break;
        case "35":
            // Misc - Checksums - Latest
            @header("Content-Type: text/plain");
            echo getDataFromUrl(_SUPERADMIN_URLBASE . _FILE_CHECKSUMS_PRE . _VERSION . _FILE_CHECKSUMS_SUF);
            exit;
        case "36":
            // Misc - Checksums - Validate
            validateLocalFiles();
            exit;
        case "5":
            // misc - Check
            $htmlTitle = "Misc - Check Requirements";
            $htmlMain .= '<br>Select the requirements you wish to check from below:<p>';
            $htmlMain .= "<strong>PHP Web</strong><br>Check your PHP web installation meets the requirements for web based activities in torrentflux-b4rt:<br>";
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=51"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="php-web" border="0"> Check PHP Web Requirements</a><br><br>';
            $htmlMain .= "<strong>PHP CLI</strong><br>Check your PHP commandline binary installation meets the requirements for commandline based activities in torrentflux-b4rt:<br>";
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=52"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="php-cli" border="0"> Check PHP CLI Binary Requirements</a><br><br>';
            $htmlMain .= "<strong>Perl</strong><br>Check your Perl installation meets the requirements for perl based activities in torrentflux-b4rt:<br>";
            $htmlMain .= '<a href="' . _FILE_THIS . '?y=53"><img src="themes/' . $cfg["theme"] . '/images/arrow.gif" width="9" height="9" title="Perl" border="0"> Check Perl Requirements</a>';
            $htmlMain .= '<br><br>';
            break;
        case "51":
            // misc - Check - php-web
            $htmlTitle = "Misc - Check Requirements - PHP Web Installation";
            $htmlMain .= '<div align="left" id="BodyLayer" name="BodyLayer" style="border: thin solid ' . $cfg['main_bgcolor'] . '; position:relative; width:740; height:498; padding-left: 5px; padding-right: 5px; z-index:1; overflow: scroll; visibility: visible">';
            $htmlMain .= phpCheckWeb();
            $htmlMain .= '</div>';
            break;
        case "52":
            // misc - Check - php-cli
            $htmlTitle = "Misc - Check Requirements - PHP CLI Binary Installation";
            $htmlMain .= '<div align="left" id="BodyLayer" name="BodyLayer" style="border: thin solid ' . $cfg['main_bgcolor'] . '; position:relative; width:740; height:498; padding-left: 5px; padding-right: 5px; z-index:1; overflow: scroll; visibility: visible">';
            $htmlMain .= '<pre>';
            $htmlMain .= tfb_htmlencode(safePhpCli($cfg["bin_php"], tfb_shellencode($cfg["docroot"] . "bin/check/check-cli.php")));
            $htmlMain .= '</pre>';
            $htmlMain .= '</div>';
            break;
        case "53":
            // misc - Check - Perl
            $htmlTitle = "Misc - Check Requirements - Perl";
            $htmlMain .= '<div align="left" id="BodyLayer" name="BodyLayer" style="border: thin solid ' . $cfg['main_bgcolor'] . '; position:relative; width:740; height:498; padding-left: 5px; padding-right: 5px; z-index:1; overflow: scroll; visibility: visible">';
            $htmlMain .= '<pre>';
            $htmlMain .= tfb_htmlencode(shell_exec($cfg["perlCmd"] . " " . tfb_shellencode($cfg["docroot"] . "bin/check/check.pl") . " all"));
            $htmlMain .= '</pre>';
            $htmlMain .= '</div>';
            break;
        case "531":
            // misc - Check - Perl - nzbperl
            $htmlTitle = "Misc - Check Requirements - Nzbperl";
            $htmlMain .= '<div align="left" id="BodyLayer" name="BodyLayer" style="border: thin solid ' . $cfg['main_bgcolor'] . '; position:relative; width:740; height:498; padding-left: 5px; padding-right: 5px; z-index:1; overflow: scroll; visibility: visible">';
            $htmlMain .= '<pre>';
            $htmlMain .= tfb_htmlencode(shell_exec($cfg["perlCmd"] . " " . tfb_shellencode($cfg["docroot"] . "bin/check/check.pl") . " nzbperl"));
            $htmlMain .= '</pre>';
            $htmlMain .= '</div>';
            break;
    }
    printPage();
    exit;
}
/**
 * Validates existence + exec + valid version of PHP-cli and returns the status image
 *
 * @param $the_file
 * @return string
 */
function validatePhpCli($the_file)
{
    if (!isFile($the_file)) {
        return validationMsg(false, 'Path is not valid');
    }
    if (!is_executable($the_file)) {
        return validationMsg(false, 'File exists but is not executable');
    }
    $phpVersion = safePhpCli($the_file, '-v');
    if (strpos($phpVersion, 'PHP') === false || strpos($phpVersion, '(cli)') === false) {
        return validationMsg(false, 'Executable is not PHP-CLI');
    }
    return validationMsg(true);
}