ob_end_flush();
                            exit;
                        }
                        sendLine('<hr><br><strong>Update to ' . $versionAvailable . ' done.</strong><br><br>');
                        sendLine('Keep thumbs pressed and give it a try.');
                        sendLine('<form name="update" action="#" method="get"><input type="submit" onClick="window.close()" value="Close"></form>');
                        sendLine('<br>');
                        ob_end_flush();
                        exit;
                    } else {
                        sendLine('<br><br><font color="red"><strong>Error getting version-file</font><br><br>Please restore backup and try again (or do manual update).</strong><br><br>');
                        ob_end_flush();
                        exit;
                    }
                } else {
                    updateError("\n" . "cant perform file-update." . "\n" . $updateFileList);
                }
                break;
        }
        exit;
    }
}
// queue
if (isset($_REQUEST["q"])) {
    $queueAction = trim($_REQUEST["q"]);
    if ($queueAction != "") {
        buildPage("q");
        switch ($queueAction) {
            case "0":
                // tfqmgr-main
                $htmlTitle = "tfqmgr";
/**
 * update
 *
 * @param $action
 */
function sa_update($action = "")
{
    global $cfg, $error, $statusImage, $statusMessage, $htmlTitle, $htmlTop, $htmlMain;
    if ($action == "") {
        return;
    }
    switch ($action) {
        case "0":
            // get updateIndex to check if update from this version possible
            $updateIndexData = trim(getDataFromUrl(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY . "?u=0&v=" . _VERSION));
            if (isset($updateIndexData) && $updateIndexData != "") {
                $updateIndexVars = explode("\n", $updateIndexData);
                $updatePossible = trim($updateIndexVars[0]);
                if ($updatePossible != "1") {
                    buildPage("-u");
                    printPage();
                    exit;
                } else {
                    $htmlTop = "<strong>Update - Check</strong>";
                    $htmlMain = "<br>Update from your Version possible.";
                    $htmlMain .= '<br><br>';
                    $htmlMain .= '<form name="update" action="' . _FILE_THIS . '" method="post">';
                    $htmlMain .= '<input type="Hidden" name="u" value="1">';
                    $htmlMain .= '<input type="submit" value="Next Step - Database-Update">';
                    $htmlMain .= '</form>';
                    $htmlMain .= '<br>';
                    $statusImage = "yellow.gif";
                    $htmlTitle = "Update";
                    printPage();
                    exit;
                }
            } else {
                buildPage("-u");
                printPage();
                exit;
            }
            break;
        case "1":
            // get db-settings
            $updateDBData = trim(getDataFromUrl(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY . "?u=1&v=" . _VERSION));
            if (isset($updateDBData) && $updateDBData != "") {
                $updateDBVars = explode("\n", $updateDBData);
                $updateNeeded = trim($updateDBVars[0]);
                if ($updateNeeded == "0") {
                    $htmlTop = "<strong>Update - Database</strong>";
                    $htmlMain = "<br>Database-Update in this Update not needed.";
                    $htmlMain .= '<br><br>';
                    $htmlMain .= '<form name="update" action="' . _FILE_THIS . '" method="post">';
                    $htmlMain .= '<input type="Hidden" name="u" value="3">';
                    $htmlMain .= '<input type="submit" value="Next Step - File-Update">';
                    $htmlMain .= '</form>';
                    $htmlMain .= '<br>';
                    $statusImage = "yellow.gif";
                    $htmlTitle = "Update";
                    printPage();
                    exit;
                } else {
                    if ($updateNeeded == "1") {
                        $htmlTop = "<strong>Update - Database</strong>";
                        $htmlMain = "<br>Database-Update in this Update is needed.";
                        $htmlMain .= '<br><br>';
                        $htmlMain .= 'Type: <em>' . $cfg["db_type"] . '</em>';
                        $htmlMain .= '<br><br>';
                        $htmlMain .= '<form name="update" action="' . _FILE_THIS . '" method="post">';
                        $htmlMain .= '<input type="Hidden" name="u" value="2">';
                        $htmlMain .= '<input type="submit" value="Next Step - Perform Database-Update">';
                        $htmlMain .= '</form>';
                        $htmlMain .= '<br>';
                        $statusImage = "yellow.gif";
                        $htmlTitle = "Update";
                        printPage();
                        exit;
                    } else {
                        updateError($updateDBData);
                        exit;
                    }
                }
            } else {
                updateError();
            }
            break;
        case "2":
            // get sql-data
            $updateSQLData = @trim(gzinflate(getDataFromUrl(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY . "?u=2&v=" . _VERSION . "&d=" . $cfg["db_type"])));
            if (isset($updateSQLData) && $updateSQLData != "") {
                sendLine('<strong>Update - Database</strong><br><br><em>Updating Database... Please Wait...</em><ul>');
                $updateSQLStates = explode("\n", $updateSQLData);
                // get ado-connection
                $dbCon = getAdoConnection();
                if (!$dbCon) {
                    echo '</em></li></ul><font color="red"><strong>Error updating Database.</strong></font><br><br>Please restore backup and try again (or do manual update).<br><br>';
                    echo $dbCon->ErrorMsg();
                    exit;
                } else {
                    foreach ($updateSQLStates as $sqlState) {
                        $sqlState = trim($sqlState);
                        if (isset($sqlState) && $sqlState != "" && substr($sqlState, 0, 2) != "--") {
                            sendLine('<li>' . $sqlState);
                            $dbCon->Execute($sqlState);
                            if ($dbCon->ErrorNo() == 0) {
                                sendLine(' <font color="green">Ok</font></li>');
                            } else {
                                // damn there was an error
                                // close ado-connection
                                $dbCon->Close();
                                // talk and out
                                echo '</em></li></ul><font color="red"><strong>Error updating Database.</strong></font><br><br>Please restore backup and try again (or do manual update).<br><br>';
                                exit;
                            }
                        }
                    }
                    // close ado-connection
                    $dbCon->Close();
                    // talk and continue
                    sendLine('</ul><p><font color="green">Database-Update done.</font><br><br>');
                    sendLine('<form name="update" action="' . _FILE_THIS . '" method="post"><input type="Hidden" name="u" value="3"><input type="submit" value="Next Step - File-Update"></form><br>');
                    exit;
                }
            } else {
                updateError("\n" . "cant get update-sql." . "\n" . $updateSQLData);
            }
            break;
        case "3":
            // get file-list
            $updateFileList = @trim(gzinflate(getDataFromUrl(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY . "?u=3&v=" . _VERSION)));
            if (isset($updateFileList) && $updateFileList != "") {
                sendLine('<strong>Update - Files</strong>');
                sendLine('<br><br>');
                sendLine('Files that require an update in this Version:');
                sendLine('<pre>');
                sendLine($updateFileList);
                sendLine('</pre>');
                sendLine('<form name="update" action="' . _FILE_THIS . '" method="post">');
                sendLine('<input type="Hidden" name="u" value="4">');
                sendLine('<input type="submit" value="Next Step - Perform File-Update">');
                sendLine('</form>');
                sendLine('<strong>Ensure script can write to docroot <em>' . $cfg['docroot'] . '</em> now !</strong>');
                exit;
            } else {
                updateError("\n" . "cant get file-list." . "\n" . $updateFileList);
            }
            break;
        case "4":
            sendLine('<strong>Update - Files</strong><br><br><em>Updating Files... Please Wait...</em><br><ul>');
            sendLine('<li>Getting Update-Archive:<br>');
            @ini_set("allow_url_fopen", "1");
            @ini_set("user_agent", "torrentflux-b4rt/" . _VERSION);
            // get md5
            $md5hash = getDataFromUrl(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY . "?u=4&v=" . _VERSION);
            if (!isset($md5hash) || strlen($md5hash) != 32) {
                sendLine('</li></ul><br><br><font color="red"><strong>Error getting Update-Archive.</strong></font><br><br>Please restore backup and try again (or do manual update).<br><br>');
                exit;
            }
            // download archive
            $fileHandle = @fopen($cfg['docroot'] . _UPDATE_ARCHIVE, "w");
            $urlHandle = @fopen(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY . "?u=5&v=" . _VERSION, 'r');
            if ($fileHandle && $urlHandle) {
                $results = array();
                $i = 0;
                stream_set_timeout($urlHandle, 15);
                $info = stream_get_meta_data($urlHandle);
                while (!feof($urlHandle) && !$info['timed_out']) {
                    $data = @fgets($urlHandle, 8192);
                    $results[$i] = @fwrite($fileHandle, $data);
                    $info = stream_get_meta_data($urlHandle);
                    sendLine('.');
                    $i++;
                }
                @fclose($fileHandle);
                @fclose($urlHandle);
                $done = true;
                foreach ($results as $result) {
                    if ($result === false) {
                        $done = false;
                    }
                }
                if ($done) {
                    sendLine('<font color="green">done</font></li>');
                } else {
                    sendLine('<br></li></ul><br><br><strong><font color="red">Error writing archive <em>' . $cfg['docroot'] . _UPDATE_ARCHIVE . '</em>.</font></strong><br><br>Please restore backup and try again (or do manual update).<br><br>');
                    exit;
                }
            } else {
                sendLine('</li></ul><br><br><strong><font color="red">Error updating files.</font></strong><br><br>Please restore backup and try again (or do manual update).<br><br>');
                exit;
            }
            // validate archive
            sendLine('<li>Validating Update-Archive: ');
            if (file_exists($cfg['docroot'] . _UPDATE_ARCHIVE) && $md5hash == @md5_file($cfg['docroot'] . _UPDATE_ARCHIVE)) {
                sendLine('<font color="green">Ok</font> (<em>' . $md5hash . '</em>)<br></li>');
            } else {
                sendLine('<font color="red">failed</font></ul><br><br>Please restore backup and try again (or do manual update).</strong><br><br>');
                exit;
            }
            // extract archive
            sendLine('<li>Extracting Update-Archive: <br>');
            sendLine('<em>');
            $cmd = 'cd ' . tfb_shellencode($cfg['docroot']) . ' && tar jxvf ' . _UPDATE_ARCHIVE;
            $cmd .= ' 2>&1';
            $handle = @popen($cmd, 'r');
            while (!@feof($handle)) {
                $read = @fread($handle, 64);
                sendLine(nl2br($read));
            }
            @pclose($handle);
            sendLine('</em>');
            sendLine('<font color="green">done</font></li>');
            // delete archive
            sendLine('<li>Deleting Update-Archive: ');
            if (@unlink($cfg['docroot'] . _UPDATE_ARCHIVE)) {
                sendLine('<font color="green">done</font></li>');
            } else {
                sendLine('<font color="red">failed</font><br>remove archive ' . $cfg['docroot'] . _UPDATE_ARCHIVE . ' manual now.</li>');
            }
            // version-file
            sendLine('<li>Updating Version-Information: ');
            $versionAvailable = trim(getDataFromUrl(_SUPERADMIN_URLBASE . _SUPERADMIN_PROXY));
            if (isset($versionAvailable) && $versionAvailable != "") {
                if ($handle = @fopen("version.php", "w")) {
                    if (@fwrite($handle, '<?php define("_VERSION", "' . $versionAvailable . '"); ?>')) {
                        @fclose($handle);
                        sendLine(' <font color="green">done</font></li>');
                    } else {
                        @fclose($handle);
                        sendLine('</li></ul><br><br><font color="red"><strong>Error writing version-file</strong></font><br><br>Please restore backup and try again (or do manual update).<br><br>');
                        exit;
                    }
                } else {
                    sendLine('<br><br>');
                    sendLine('</li></ul><font color="red"><strong>Error writing version-file</strong></font><br><br>Please restore backup and try again (or do manual update).<br><br>');
                    exit;
                }
            } else {
                sendLine('</li></ul><br><br><font color="red"><strong>Error getting version-file</strong></font><br><br>Please restore backup and try again (or do manual update).<br><br>');
                exit;
            }
            sendLine('</ul>');
            // done
            sendLine('<p><em>Done Updating Files.</em></p>');
            sendLine('<hr><br><strong>Update to ' . $versionAvailable . ' completed.</strong><br><br>');
            sendLine('<form name="update" action="#" method="get"><input type="submit" onClick="window.close()" value="Close"></form>');
            sendLine('<br>');
            // flush cache
            cacheFlush();
            // exit
            exit;
    }
    exit;
}