function viewcommentary($section, $message = "Interject your own commentary?", $limit = 25, $talkline = "says", $schema = false, $skipfooter = false, $customsql = false, $skiprecentupdate = false, $overridemod = false)
{
    global $session, $REQUEST_URI, $doublepost, $translation_namespace;
    global $emptypost;
    global $chatloc, $chatsonpage, $afk, $dni, $moderating;
    if (httpget('comment')) {
        invalidatedatacache("latestcommentary_" . $section);
    }
    if ($session['user']['superuser'] & SU_EDIT_COMMENTS || $overridemod) {
        $showmodlink = true;
    } else {
        $showmodlink = false;
    }
    output_notl("`n");
    //skip assigning chatloc if this chatloc's id ends with "_aux" - this way we can have dual chat areas
    if (!$afk && $session['user']['chatloc'] != "DNI") {
        if (substr($section, strlen($section) - 4, strlen($section)) != "_aux") {
            $chatloc = $section;
        } else {
            $chatloc = substr($section, 0, -4);
        }
        $session['user']['chatloc'] = $chatloc;
    } else {
        $chatloc = "AFK";
    }
    if ($section) {
        rawoutput("<a name='{$section}'></a>");
        // Let's add a hook for modules to block commentary sections
        $args = modulehook("blockcommentarea", array("section" => $section));
        if (isset($args['block']) && $args['block'] == "yes") {
            return;
        }
    }
    $commentbuffer = getcommentary($section, $limit, $talkline, $customsql, $showmodlink);
    $rowcount = count($commentbuffer);
    if ($doublepost) {
        output("`\$`bDouble post?`b`0`n");
    }
    if ($emptypost) {
        output("`\$`bWell, they say silence is a virtue.`b`0`n");
    }
    //output the comments!
    if ($moderating && $rowcount || !$moderating) {
        $new = 0;
        if (!isset($session['user']['prefs']['commentary_recentline'])) {
            $session['user']['prefs']['commentary_recentline'] = 1;
        }
        if (!$session['user']['prefs']['commentary_reverse']) {
            for ($i = $rowcount - 1; $i >= 0; $i--) {
                $line = preparecommentaryline($commentbuffer[$i]);
                //highlight new commentary lines
                if ($commentbuffer[$i]['postdate'] > $session['user']['recentcomments'] && !$new && $session['user']['prefs']['commentary_recentline']) {
                    rawoutput("<hr style=\"border-bottom: 1px dotted #333333; border-top: 0; border-left: 0; border-right: 0;\" />");
                    $new = 1;
                }
                output_notl("{$line}`n", true);
            }
        } else {
            for ($i = 0; $i <= $rowcount - 1; $i++) {
                $line = preparecommentaryline($commentbuffer[$i]);
                output_notl("{$line}`n", true);
                if ($commentbuffer[$i]['postdate'] > $session['user']['recentcomments'] && !$new && $session['user']['prefs']['commentary_recentline']) {
                    rawoutput("<hr style=\"border-bottom: 1px dotted #333333; border-top: 0; border-left: 0; border-right: 0;\" />");
                    $new = 1;
                }
            }
        }
        if ($showmodlink) {
            //moderation link mass delete button
            rawoutput("<input type=\"submit\" value=\"Mass Delete\"></form>");
        }
        if (!$skipfooter) {
            commentaryfooter($section, $message, $limit, $talkline, $schema);
        }
    } else {
        //		output("There are no comments in this area.");
    }
    if (!$skiprecentupdate) {
        $session['user']['recentcomments'] = date("Y-m-d H:i:s");
    }
}
function preparecommentaryblock($section, $message = "Interject your own commentary?", $limit = 25, $talkline = "says", $schema = false, $skipfooter = false, $customsql = false, $skiprecentupdate = false, $overridemod = false, $returnlink = false)
{
    global $session, $REQUEST_URI, $doublepost, $translation_namespace;
    global $emptypost;
    global $chatloc, $afk, $dni, $moderating;
    // if (httpget('comment')){
    // invalidatedatacache("commentary/latestcommentary_".$section);
    // invalidatedatacache("commentary/commentarycount_".$section);
    // invalidatedatacache("commentary/whosonline_".$section);
    // }
    if ($session['user']['superuser'] & SU_EDIT_COMMENTS || $overridemod) {
        $showmodlink = true;
    } else {
        $showmodlink = false;
    }
    $ret = "";
    //skip assigning chatloc if this chatloc's id ends with "_aux" - this way we can have dual chat areas
    if (!$afk && $session['user']['chatloc'] != "DNI") {
        if (substr($section, strlen($section) - 4, strlen($section)) != "_aux") {
            $chatloc = $section;
        } else {
            $chatloc = substr($section, 0, -4);
        }
        $session['user']['chatloc'] = $chatloc;
    } else {
        $chatloc = "AFK";
    }
    if ($section) {
        $ret .= "<a name='{$section}'></a>";
        // Let's add a hook for modules to block commentary sections
        $args = modulehook("blockcommentarea", array("section" => $section));
        if (isset($args['block']) && $args['block'] == "yes") {
            return;
        }
    }
    $commentbuffer = getcommentary($section, $limit, $talkline, $customsql, $showmodlink, $returnlink);
    $rowcount = count($commentbuffer);
    if ($doublepost) {
        $ret .= "`\$`bDouble post?`b`0`n";
    }
    if ($emptypost) {
        $ret .= "`\$`bWell, they say silence is a virtue.`b`0`n";
    }
    //output the comments!
    $start = microtime(true);
    if ($moderating && $rowcount || !$moderating) {
        $new = 0;
        if (!isset($session['user']['prefs']['commentary_recentline'])) {
            $session['user']['prefs']['commentary_recentline'] = 1;
        }
        if (!$session['user']['prefs']['commentary_reverse']) {
            //debug($session['recentcomments']);
            for ($i = $rowcount - 1; $i >= 0; $i--) {
                $line = preparecommentaryline($commentbuffer[$i]);
                //highlight new commentary lines
                if ($commentbuffer[$i]['postdate'] > $session['recentcomments'] && !$new && $session['user']['prefs']['commentary_recentline']) {
                    $ret .= "<hr style=\"border-bottom: 1px dotted #333333; border-top: 0; border-left: 0; border-right: 0;\" />";
                    $new = 1;
                    $ret .= "<a name=\"commentaryjump_" . $section . "\"></a>";
                    //debug("Jump at comment id".$commentbuffer[$i]['commentid']);
                } else {
                    if ($i == 2) {
                        $ret .= "<a name=\"commentaryjump_" . $section . "\"></a>";
                        //debug("Jump at comment id".$commentbuffer[$i]['commentid']);
                    }
                }
                $ret .= "{$line}`n";
            }
        } else {
            for ($i = 0; $i <= $rowcount - 1; $i++) {
                $line = preparecommentaryline($commentbuffer[$i]);
                $ret .= "{$line}`n";
                if ($commentbuffer[$i]['postdate'] > $session['recentcomments'] && !$new && $session['user']['prefs']['commentary_recentline']) {
                    $ret .= "<hr style=\"border-bottom: 1px dotted #333333; border-top: 0; border-left: 0; border-right: 0;\" />";
                    $new = 1;
                }
            }
        }
    }
    $end = microtime(true);
    $tot = $end - $start;
    //debug("Preparecommentaryblock loop: ".$tot);
    return $ret;
}
        if ($section || $talkline || $comment) {
            $tcom = color_sanitize($comment);
            if ($tcom == "" || $tcom == ":" || $tcom == "::" || $tcom == "/me") {
                $emptypost = 1;
            } else {
                injectcommentary($section, $talkline, $comment, $schema);
            }
            //echo("injectcommentary($section, $talkline, $comment, $schema)");
            //exit;
        }
        $output = '';
        $lastid = getcommentary($_REQUEST['section'], 25, $_REQUEST['talkline'], $_REQUEST['lastcommentid']);
    }
} else {
    $output = '';
    $lastid = getcommentary($_REQUEST['section'], 25, $_REQUEST['talkline'], $_REQUEST['lastcommentid']);
    if (array_key_exists("mute", $args) && $args['mute'] && !($session['user']['superuser'] & SU_EDIT_COMMENTS)) {
        $addboxreplace = 1;
        output_notl("%s", $args['mutemsg']);
    } elseif ($counttoday > 13 && !($session['user']['superuser'] & ~SU_DOESNT_GIVE_GROTTO)) {
        $addboxreplace = 1;
        output("Sorry, you've exhausted your posts in this section for now.`0`n");
    } elseif (13 - $counttoday < 3) {
        output("`)(You have %s posts left today)`n`0", 13 - $counttoday);
    }
}
$commentaryout = array("commentary" => array("newcommentary" => $output, "lastid" => $lastid, "replaceform" => $addboxreplace));
$json = new Services_JSON();
print $json->encode($commentaryout);
function getcommentary($section, $limit = 25, $talkline = "says", $commentid = 0)
{