function translator_uri($in)
{
    $uri = comscroll_sanitize($in);
    $uri = cmd_sanitize($uri);
    if (substr($uri, -1) == "?") {
        $uri = substr($uri, 0, -1);
    }
    return $uri;
}
function tlbutton_push($indata, $hot = false, $namespace = FALSE)
{
    global $translatorbuttons;
    global $translation_is_enabled, $seentlbuttons, $session;
    if (!$translation_is_enabled) {
        return;
    }
    if (!$namespace) {
        $namespace = "unknown";
    }
    if ($session['user']['superuser'] & SU_IS_TRANSLATOR) {
        if (preg_replace("/[ \t\n\r]|`./", '', $indata) > "") {
            if (isset($seentlbuttons[$namespace][$indata])) {
                $link = "";
            } else {
                $seentlbuttons[$namespace][$indata] = true;
                require_once "lib/sanitize.php";
                $uri = cmd_sanitize($namespace);
                $uri = comscroll_sanitize($uri);
                $link = "translatortool.php?u=" . rawurlencode($uri) . "&t=" . rawurlencode($indata);
                $link = "<a href='{$link}' target='_blank' onClick=\"" . popup($link) . ";return false;\" class='t" . ($hot ? "hot" : "") . "'>T</a>";
            }
            array_push($translatorbuttons, $link);
        }
        return true;
    } else {
        //when user is not a translator, return false.
        return false;
    }
}
function talkform($section, $talkline, $limit = 10, $schema = false)
{
    require_once "lib/forms.php";
    global $REQUEST_URI, $session, $translation_namespace;
    if ($schema === false) {
        $schema = $translation_namespace;
    }
    tlschema("commentary");
    $jump = false;
    if (isset($session['user']['prefs']['nojump']) && $session['user']['prefs']['nojump'] == true) {
        $jump = true;
    }
    $counttoday = 0;
    if (substr($section, 0, 5) != "clan-") {
        $sql = "SELECT author FROM " . db_prefix("commentary") . " WHERE section='{$section}' AND postdate>'" . date("Y-m-d 00:00:00") . "' ORDER BY commentid DESC LIMIT {$limit}";
        $result = db_query($sql);
        while ($row = db_fetch_assoc($result)) {
            if ($row['author'] == $session['user']['acctid']) {
                $counttoday++;
            }
        }
        if (round($limit / 2, 0) - $counttoday <= 0 && getsetting('postinglimit', 1)) {
            if ($session['user']['superuser'] & ~SU_DOESNT_GIVE_GROTTO) {
                output("`n`)(You'd be out of posts if you weren't a superuser or moderator.)`n");
            } else {
                output("`n`)(You are out of posts for the time being.  Once some of your existing posts have moved out of the comment area, you'll be allowed to post again.)`n");
                return false;
            }
        }
    }
    if (translate_inline($talkline, $schema) != "says") {
        $tll = strlen(translate_inline($talkline, $schema)) + 11;
    } else {
        $tll = 0;
    }
    $req = comscroll_sanitize($REQUEST_URI) . "&comment=1";
    $req = str_replace("?&", "?", $req);
    if (!strpos($req, "?")) {
        $req = str_replace("&", "?", $req);
    }
    if ($jump) {
        $req .= "#{$section}";
    }
    addnav("", $req);
    // *** AJAX CHAT MOD START ***
    //output_notl("<form action=\"$req\" method='POST' autocomplete='false'>",true);
    $args1 = array("formline" => "<form action=\"{$req}\" id='commentaryform' method='post' autocomplete='false'", "section" => $section, "talkline" => $talkline, "schema" => $schema);
    $args1 = modulehook("commentarytalkline", $args1);
    rawoutput('<div id="commentaryformcontainer">');
    output_notl($args1['formline'] . ">", true);
    // *** AJAX CHAT MOD END ***
    previewfield("insertcommentary", $session['user']['name'], $talkline, true, array("size" => "40", "maxlength" => 200 - $tll));
    rawoutput("<input type='hidden' name='talkline' value='{$talkline}'>");
    rawoutput("<input type='hidden' name='schema' value='{$schema}'>");
    rawoutput("<input type='hidden' name='counter' value='{$session['counter']}'>");
    $session['commentcounter'] = $session['counter'];
    if ($section == "X") {
        $vname = getsetting("villagename", LOCATION_FIELDS);
        $iname = getsetting("innname", LOCATION_INN);
        $sections = commentarylocs();
        reset($sections);
        output_notl("<select name='section'>", true);
        while (list($key, $val) = each($sections)) {
            output_notl("<option value='{$key}'>{$val}</option>", true);
        }
        output_notl("</select>", true);
    } else {
        output_notl("<input type='hidden' name='section' value='{$section}'>", true);
    }
    $add = htmlentities(translate_inline("Add"), ENT_QUOTES, getsetting("charset", "ISO-8859-1"));
    // *** DRAGONBG.COM CORE PATCH START***
    output_notl("<input type='submit' class='button' value='{$add}'>\t", true);
    modulehook("commentarytrail", array());
    // *** DRAGONBG.COM CORE PATCH END***
    // *** AJAX CHAT MOD START ***
    //if (round($limit/2,0)-$counttoday < 3 && getsetting('postinglimit',1)){
    //	output("`)(You have %s posts left today)`n`0",(round($limit/2,0)-$counttoday));
    //}
    rawoutput("<div id='previewtext'></div></form>");
    rawoutput('</div>');
    // *** AJAX CHAT MOD END ***
    tlschema();
}
function buildcommentarylink($append, $returnlink = false)
{
    //TODO: Check for removecomment and restorecomment flags, possibly via regexp
    global $session, $REQUEST_URI;
    $jump = false;
    if (isset($session['user']['prefs']['nojump']) && $session['user']['prefs']['nojump'] == true) {
        $jump = true;
    }
    if (!$returnlink) {
        $nlink = comscroll_sanitize($REQUEST_URI);
    } else {
        $decoded = urldecode($returnlink);
        //debug($decoded);
        if (strpos($decoded, '/') !== false) {
            //debug("Decoded!");
            $nlink = str_replace("/", "", $decoded);
            //debug($nlink);
        }
        $nlink = comscroll_sanitize($nlink);
    }
    $nlink = preg_replace("'&r(emovecomment)?=([[:digit:]]|-)*'", "", $nlink);
    $nlink = preg_replace("'\\?r(emovecomment)?=([[:digit:]]|-)*'", "?", $nlink);
    $nlink = preg_replace("'&r(estorecomment)?=([[:digit:]]|-)*'", "", $nlink);
    $nlink = preg_replace("'\\?r(estorecomment)?=([[:digit:]]|-)*'", "?", $nlink);
    // $nlink = preg_replace("'&a(utomod_inc)?=([[:digit:]]|-)*'", "", $nlink);
    // $nlink = preg_replace("'\\?a(utomod_inc)?=([[:digit:]]|-)*'", "?", $nlink);
    // $nlink = preg_replace("'&a(utomod_dec)?=([[:digit:]]|-)*'", "", $nlink);
    // $nlink = preg_replace("'\\?a(utomod_dec)?=([[:digit:]]|-)*'", "?", $nlink);
    //solve world map re-fight exploit
    $nlink = str_replace("?op=fight", "?op=continue", $nlink);
    $nlink = str_replace("&op=fight", "&op=continue", $nlink);
    //stop multiple addnav counters getting added to the end
    $nlink = preg_replace("'&c?=([[:digit:]]|-)*'", "", $nlink);
    $nlink = preg_replace("'\\?c?=([[:digit:]]|-)*'", "?", $nlink);
    $nlink = str_replace("?enable_auto_update=true", "", $nlink);
    $nlink = str_replace("?disable_auto_update=true", "", $nlink);
    $nlink = str_replace("&enable_auto_update=true", "", $nlink);
    $nlink = str_replace("&disable_auto_update=true", "", $nlink);
    $nlink = str_replace("&bulkdelete=true", "", $nlink);
    $nlink = str_replace("?bulkdelete=true", "", $nlink);
    $nlink = str_replace("&frombio=true", "", $nlink);
    $nlink .= $append;
    $nlink = str_replace(".php&", ".php?", $nlink);
    $nlink = str_replace("&switchstack=1", "", $nlink);
    $nlink = str_replace("?switchstack=1", "", $nlink);
    $nlink = str_replace("&switchmultichat=1", "", $nlink);
    $nlink = str_replace("&switchmultichat=2", "", $nlink);
    $nlink = str_replace("&switchmultichat=3", "", $nlink);
    $nlink = str_replace("?switchmultichat=1", "", $nlink);
    $nlink = str_replace("?switchmultichat=2", "", $nlink);
    $nlink = str_replace("?switchmultichat=3", "", $nlink);
    //debug($nlink);
    if (!strpos($nlink, "?")) {
        $nlink = str_replace("&", "?", $nlink);
        if (!strpos($nlink, "?")) {
            $nlink .= "?";
        }
    }
    //debug($nlink);
    if ($jump && $section) {
        $nlink .= "#commentarystart";
    }
    addnav("", $nlink);
    return $nlink;
}
function buildcommentarylink($append)
{
    //TODO: Check for removecomment and restorecomment flags, possibly via regexp
    global $session, $REQUEST_URI;
    $jump = false;
    if (isset($session['user']['prefs']['nojump']) && $session['user']['prefs']['nojump'] == true) {
        $jump = true;
    }
    $nlink = comscroll_sanitize($REQUEST_URI);
    $nlink .= $append;
    $nlink = str_replace(".php&", ".php?", $nlink);
    $nlink = str_replace("&switchstack=1", "", $nlink);
    $nlink = str_replace("?switchstack=1", "", $nlink);
    $nlink = str_replace("&switchmultichat=1", "", $nlink);
    $nlink = str_replace("&switchmultichat=2", "", $nlink);
    $nlink = str_replace("&switchmultichat=3", "", $nlink);
    $nlink = str_replace("?switchmultichat=1", "", $nlink);
    $nlink = str_replace("?switchmultichat=2", "", $nlink);
    $nlink = str_replace("?switchmultichat=3", "", $nlink);
    // $nlink = str_replace("&bulkdelete=true","",$nlink);
    // $nlink = str_replace("?bulkdelete=true","",$nlink);
    if (!strpos($nlink, "?")) {
        $nlink = str_replace("&", "?", $nlink);
        if (!strpos($nlink, "?")) {
            $nlink .= "?";
        }
    }
    if ($jump) {
        $nlink .= "#{$section}";
    }
    return $nlink;
}