<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') OR die('Access denied comments directly');
$module_name = "News";
get_lang($module_name);
if (is_file("modules/News/includes/functions.php")) {include_once("modules/News/includes/functions.php");}

function format_url($comment) {
    global $nukeurl;
    unset($location);
    $comment = $comment;
    $links = array();
    $hrefs = array();
    $pos = 0;
    while (!(($pos = strpos($comment,"<",$pos)) === false)) {
	$pos++;
	$endpos = strpos($comment,">",$pos);
	$tag = substr($comment,$pos,$endpos-$pos);
	$tag = trim($tag);
	if (isset($location)) {
    	    if (!strcasecmp(strtok($tag," "),"/A")) {
        	$link = substr($comment,$linkpos,$pos-1-$linkpos);
        	$links[] = $link;
        	$hrefs[] = $location;
        	unset($location);
    	    }
	    $pos = $endpos+1;
	} else {
	    if (!strcasecmp(strtok($tag," "),"A")) {
		if (eregi("HREF[ \t\n\r\v]*=[ \t\n\r\v]*\"([^\"]*)\"",$tag,$regs));
		else if (eregi("HREF[ \t\n\r\v]*=[ \t\n\r\v]*([^ \t\n\r\v]*)",$tag,$regs));
		else $regs[1] = "";
		if ($regs[1]) {
	    	    $location = $regs[1];
		}
		$pos = $endpos+1;
		$linkpos = $pos;
	    } else {
		$pos = $endpos+1;
	    }
	}
    }
    for ($i=0; $i<sizeof($links); $i++) {
	if (!eregi("http://", $hrefs[$i])) {
	    $hrefs[$i] = $nukeurl;
	} elseif (!eregi("mailto://", $hrefs[$i])) {
	    $href = explode("/",$hrefs[$i]);
	    $href = " [$href[2]]";
	    $comment = ereg_replace(">$links[$i]</a>", " title='$hrefs[$i]'> $links[$i]</a>$href", $comment);
	}
    }
    return($comment);
}

function modone() {
    global $admin, $moderate, $module_name;
    if(((isset($admin)) && ($moderate == 1)) || ($moderate==2))
 echo "<form action=\"modules.php?name=$module_name&amp;file=comments\" method=\"post\">";
}

function modtwo($tid, $score, $reason) {
    global $admin, $user, $moderate, $reasons;
    if((((isset($admin)) && ($moderate == 1)) || ($moderate == 2)) && ($user)) {
	echo " | <select name='dkn$tid' >";
	for($i=0; $i<sizeof($reasons); $i++) {
	    echo "<option value=\"$score:$i\">$reasons[$i]</option>\n";
	}
	echo "</select>";
    }
}

function modthree($sid, $mode, $order, $thold=0) {
    global $admin, $user, $moderate;
    if((((isset($admin)) && ($moderate == 1)) || ($moderate==2)) && ($user))
 echo "<center><input type=\"hidden\" name='sid' value='$sid' /><input type=\"hidden\" name=\"mode\" value=\"$mode\" /><input type=\"hidden\" name=\"order\" value=\"$order\" /><input type=\"hidden\" name=\"thold\" value=\"$thold\" />
    <input type=\"hidden\" name=\"op\" value=\"moderate\" />
    <input type=\"image\" src=\"images/menu/moderate.gif\" border=\"0\" /></form></center>";
}

function nocomm() {
    Max_OpenTable();
    echo "<center><font class=\"content\">"._NOCOMMENTSACT."</font></center>";
    Max_CloseTable();
}

function navbar($sid, $title, $thold, $mode, $order) {
    global $user, $bgcolor1, $bgcolor2, $textcolor1, $textcolor2, $anonpost, $prefix, $db, $module_name;

    $sid = $db->XMFnum( $sid );
    $query = $db->sql_query("SELECT * FROM `".TABLE_COMMENTS."` WHERE `sid`='$sid'");
    if(!$query) {
	$count = 0;
    } else {
	$count = $db->sql_numrows($query);
    }
$sid = $db->XMFnum($sid);
    $query = $db->sql_query("SELECT `title` FROM `".TABLE_STORIES."` WHERE `sid`='$sid'");
    list($un_title) = $db->sql_fetchrow($query);
    if(!isset($thold)) {
   $thold=0;
    }
    echo "\n\n<!-- COMMENTS NAVIGATION BAR START -->\n\n";
    echo "<a name=\"comments\"></a>\n";
    Max_OpenTable();
    echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\">\n";
    if($title) {
   echo "<tr><td bgcolor=\"$bgcolor2\" align=\"center\"><font class=\"content\" color=\"$textcolor1\">\"$un_title\" | ";
	if(defined('USER_ALLOW')) {
	    echo "<a href=\"modules.php?name=Your_Account&amp;op=editcomm\"><font color=\"$textcolor1\">"._CONFIGURE."</font></a>";
	} else {
	    echo "<a href=\"modules.php?name=Your_Account\"><font color=\"$textcolor1\">"._LOGINCREATE."</font></a>";
	}
	if(($count==1)) {
	    echo " | <b>$count</b> "._COMMENT."";
	} else {
	    echo " | <b>$count</b> "._COMMENTS."";
	}
	if ($count > 0 AND is_active("Search")) {
	    echo " | <a href='modules.php?name=Search&amp;type=comments&amp;sid=$sid'>"._SEARCHDIS."</a>";
	}
	echo "</font></td></tr>\n";
    }
    echo "<tr><td bgcolor=\"$bgcolor1\" align=\"center\" width=\"100%\">\n";
    if ($anonpost==1 OR defined('ADMIN_ALLOW') OR defined('USER_ALLOW')) {
		echo "<form action=\"modules.php?name=$module_name&amp;file=comments\" method=\"post\">"
		    ."<input type=\"hidden\" name=\"pid\" value=\"$pid\" />"
		    ."<input type=\"hidden\" name=\"sid\" value=\"$sid\" />"
		    ."<input type=\"hidden\" name=\"op\" value=\"Reply\" />"
		    ."<input type=\"submit\" value=\""._REPLYMAIN."\" /></td></form></tr>";
    }
    echo "<tr><td bgcolor=\"$bgcolor2\" align=\"center\"><font class=\"tiny\">"._COMMENTSWARNING."</font></td></tr></table>"
	."\n\n<!-- COMMENTS NAVIGATION BAR END -->\n\n";
    Max_CloseTable();
    if ($anonpost == 0 AND !defined('USER_ALLOW')) {
        br('1');
	Max_OpenTable();
	echo "<center>"._NOANONCOMMENTS."</center>";
	Max_CloseTable();
    }
}

function DisplayKids ($tid, $mode, $order=0, $thold=0, $level=0, $dummy=0, $tblwidth=99) {
    global $datetime, $user, $cookie, $bgcolor1, $reasons, $anonymous, $anonpost, $commentlimit, $prefix, $textcolor2, $db, $module_name;
    $comments = 0;
    $tid = $db->XMFnum( $tid );
    cookiedecode($user);
    $result = $db->sql_query("SELECT `tid`, `pid`, `sid`, `date`, `name`, `email`, `host_name`, `subject`, `comment`, `score`, `reason` FROM `".TABLE_COMMENTS."` WHERE `pid`='$tid' ORDER BY `date`, `tid`");
    if ($mode == 'nested') {
	/* without the tblwidth variable, the tables run of the screen with netscape */
	/* in nested mode in long threads so the text can't be read. */
	while ($row = $db->sql_fetchrow($result)) {
	    $r_tid       = $db->XMFnum($row['tid']);
	    $r_pid       = $db->XMFnum($row['pid']);
	    $r_sid       = $db->XMFnum($row['sid']);
	    $r_date      = stripslashes($row['date']);
	    $r_name      = stripslashes($row['name']);
	    $r_email     = stripslashes($row['email']);
	    $r_host_name = stripslashes($row['host_name']);
	    $r_subject   = stripslashes(check_html($row['subject'], "nohtml"));
	    $r_comment   = stripslashes($row['comment']);
	    $r_score     = $db->XMFnum($row['score']);
	    $r_reason    = $db->XMFnum($row['reason']);
	    if($r_score >= $thold) {
		if (!isset($level)) {
		} else {
		    if (!$comments) {
			echo "<ul>";
			$tblwidth -= 5;
		    }
		}
		$comments++;
		if (!eregi("[a-z0-9]",$r_name)) $r_name = $anonymous;
		if (!eregi("[a-z0-9]",$r_subject)) $r_subject = "["._NOSUBJECT."]";
		// HIJO enter hex color between first two appostrophe for second alt bgcolor
		$r_bgcolor = ($dummy%2)?"":"#E6E6D2";
		echo "<a name=\"$r_tid\">";
		echo "<table border=\"0\"><tr bgcolor=\"$bgcolor1\"><td>";
		formatTimestamp($r_date);
		if ($r_email) {
		    echo "<b>$r_subject</b> <font class=\"content\">";
		    if(!$cookie[7]) {
			echo "("._SCORE." $r_score";
			if($r_reason>0) echo ", $reasons[$r_reason]";
			echo ")";
		    }
		    echo "<br />"._BY." <a href=\"mailto:$r_email\">$r_name</a> <font class=\"content\"><b>($r_email)</b></font> "._ON." $datetime";
		} else {
		    echo "<b>$r_subject</b> <font class=\"content\">";
		    if(!$cookie[7]) {
			echo "("._SCORE." $r_score";
			if($r_reason>0) echo ", $reasons[$r_reason]";
			echo ")";
		    }
		    echo "<br />"._BY." $r_name "._ON." $datetime";
		}
		if ($r_name != $anonymous) {
		    $row2 = $db->sql_fetchrow($db->sql_query("SELECT `user_id` FROM `".TABLE_USERS."` WHERE `username`='$r_name'"));
		    $r_uid = $db->XMFnum($row2['user_id']);
		    echo "<br />(<a href=\"modules.php?name=Your_Account&amp;op=userinfo&amp;username=$r_name\">"._USERINFO."</a> | <a href=\"modules.php?name=Private_Messages&amp;mode=post&amp;u=$r_uid\">"._SENDAMSG."</a>) ";
		}
		$row_url = $db->sql_fetchrow($db->sql_query("SELECT `user_website` FROM `".TABLE_USERS."` WHERE `username`='$r_name'"));
		$url = stripslashes($row_url['user_website']);
		if ($url != "http://" AND $url != "" AND eregi("http://", $url)) { echo "<a href=\"$url\" target=\"new\">$url</a> "; }
		echo "</font></td></tr><tr><td>";
		if(($cookie[10]) && (strlen($r_comment) > $cookie[10])) echo substr("$r_comment", 0, $cookie[10])."<br /><br /><b><a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$r_sid&amp;tid=$r_tid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._READREST."</a></b>";
		elseif(strlen($r_comment) > $commentlimit) echo substr("$r_comment", 0, $commentlimit)."<br /><br /><b><a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$r_sid&amp;tid=$r_tid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._READREST."</a></b>";
		else echo $r_comment;
		echo "</td></tr></table>";
		br('2');
		if ($anonpost==1 OR defined('ADMIN_ALLOW') OR defined('USER_ALLOW')) {
		    echo "<font class=\"content\" color=\"$textcolor2\"> [ <a href=\"modules.php?name=$module_name&amp;file=comments&amp;op=Reply&amp;pid=$r_tid&amp;sid=$r_sid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._REPLY."</a>";
		}
		modtwo($r_tid, $r_score, $r_reason);
		echo " ]</font>";
		br('2');
		DisplayKids($r_tid, $mode, $order, $thold, $level+1, $dummy+1, $tblwidth);
	    }
	}
	} elseif ($mode == 'flat') {
	    while ($row = $db->sql_fetchrow($result)) {
		$r_tid = $db->XMFnum($row['tid']);
		$r_pid = $db->XMFnum($row['pid']);
		$r_sid = $db->XMFnum($row['sid']);
		$r_date = $row['date'];
		$r_name = stripslashes($row['name']);
		$r_email = stripslashes($row['email']);
		$r_host_name = $row['host_name'];
		$r_subject = stripslashes(check_html($row['subject'], "nohtml"));
		$r_comment = stripslashes($row['comment']);
		$r_score = $db->XMFnum($row['score']);
		$r_reason = $db->XMFnum($row['reason']);
		if($r_score >= $thold) {
		    if (!eregi("[a-z0-9]",$r_name)) $r_name = $anonymous;
		    if (!eregi("[a-z0-9]",$r_subject)) $r_subject = "["._NOSUBJECT."]";
		    echo "<a name=\"$r_tid\">";
		    echo "<hr><table width=\"99%\" border=\"0\"><tr bgcolor=\"$bgcolor1\"><td>";
		    formatTimestamp($r_date);
		    if ($r_email) {
			echo "<b>$r_subject</b> <font class=\"content\">";
			if(!$cookie[7]) {
			    echo "("._SCORE." $r_score";
			    if($r_reason>0) echo ", $reasons[$r_reason]";
			    echo ")";
			}
			echo "<br />"._BY." <a href=\"mailto:$r_email\">$r_name</a> <font class=\"content\"><b>($r_email)</b></font> "._ON." $datetime";
 		    } else {
			echo "<b>$r_subject</b> <font class=\"content\">";
			if(!$cookie[7]) {
			    echo "("._SCORE." $r_score";
			    if($r_reason>0) echo ", $reasons[$r_reason]";
			    echo ")";
			}
			echo "<br />"._BY." $r_name "._ON." $datetime";
		    }
		    if ($r_name != $anonymous) {
			$row3 = $db->sql_fetchrow($db->sql_query("SELECT `user_id` FROM `".TABLE_USERS."` WHERE `username`='$r_name'"));
			$ruid = $db->XMFnum($row3['user_id']);
			echo "<br />(<a href=\"modules.php?name=Your_Account&amp;op=userinfo&amp;username=$r_name\">"._USERINFO."</a> | <a href=\"modules.php?name=Private_Messages&amp;mode=post&amp;u=$ruid\">"._SENDAMSG."</a>) ";
		    }
		    $row_url2 = $db->sql_fetchrow($db->sql_query("SELECT `user_website` FROM `".TABLE_USERS."` WHERE `username`='$r_name'"));
		    $url = stripslashes($row_url2['user_website']);
		    if ($url != "http://" AND $url != "" AND eregi("http://", $url)) { echo "<a href=\"$url\" target=\"new\">$url</a> "; }
		    echo "</font></td></tr><tr><td>";
		    if(($cookie[10]) && (strlen($r_comment) > $cookie[10])) echo substr("$r_comment", 0, $cookie[10])."<br /><br /><b><a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$r_sid&amp;tid=$r_tid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._READREST."</a></b>";
		    elseif(strlen($r_comment) > $commentlimit) echo substr("$r_comment", 0, $commentlimit)."<br /><br /><b><a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$r_sid&amp;tid=$r_tid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._READREST."</a></b>";
		    else echo $r_comment;
		    echo "</td></tr></table>";
			br('2');
		    if ($anonpost==1 OR defined('ADMIN_ALLOW') OR defined('USER_ALLOW')) {
			echo "<font class=\"content\" color=\"$textcolor2\"> [ <a href=\"modules.php?name=$module_name&amp;file=comments&amp;op=Reply&amp;pid=$r_tid&amp;sid=$r_sid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._REPLY."</a>";
		    }
		    modtwo($r_tid, $r_score, $r_reason);
		    echo " ]</font>";
		    br('2');
		    DisplayKids($r_tid, $mode, $order, $thold);
		}
	    }
	} else {
    	    while ($row = $db->sql_fetchrow($result)) {
		$r_tid       = $db->XMFnum($row['tid']);
		$r_pid       = $db->XMFnum($row['pid']);
		$r_sid       = $db->XMFnum($row['sid']);
		$r_date      = stripslashes($row['date']);
        	$r_name      = stripslashes($row['name']);
        	$r_email     = stripslashes($row['email']);
        	$r_host_name = stripslashes($row['host_name']);
       	 	$r_subject   = stripslashes(check_html($row['subject'], "nohtml"));
        	$r_comment   = stripslashes($row['comment']);
        	$r_score     = $db->XMFnum($row['score']);
        	$r_reason    = $db->XMFnum($row['reason']);
		if($r_score >= $thold) {
		    if (!isset($level)) {
		    } else {
			if (!$comments) {
			    echo "<ul>";
			}
		    }
		    $comments++;
		    if (!eregi("[a-z0-9]",$r_name)) $r_name = $anonymous;
		    if (!eregi("[a-z0-9]",$r_subject)) $r_subject = "["._NOSUBJECT."]";
		    formatTimestamp($r_date);
		    echo "<li><font class=\"content\" color=\"$textcolor2\"><a href=\"modules.php?name=$module_name&amp;file=comments&amp;op=showreply&amp;tid=$r_tid&amp;sid=$r_sid&amp;pid=$r_pid&amp;mode=$mode&amp;order=$order&amp;thold=$thold#$r_tid\">$r_subject</a> "._BY." $r_name "._ON." $datetime</font><br />";
		    DisplayKids($r_tid, $mode, $order, $thold, $level+1, $dummy+1);
		}
	    }
	}
    if ($level && $comments) {
        echo "</ul>";
    }
}

function DisplayBabies ($tid, $level=0, $dummy=0) {
    global $datetime, $anonymous, $prefix, $db, $module_name;
    $comments = 0;
    $tid = $db->XMFnum( $tid );
    $result = $db->sql_query("SELECT `tid`, `pid`, `sid`, `date`, `name`, `email`, `host_name`, `subject`, `comment`, `score`, `reason` FROM `".TABLE_COMMENTS."` WHERE `pid`='$tid' ORDER BY `date`, `tid`");
    while ($row = $db->sql_fetchrow($result)) {
        $r_tid       = $db->XMFnum($row['tid']);
        $r_pid       = $db->XMFnum($row['pid']);
        $r_sid       = $db->XMFnum($row['sid']);
        $r_date      = stripslashes($row['date']);
        $r_name      = stripslashes($row['name']);
        $r_email     = stripslashes($row['email']);
        $r_host_name = stripslashes($row['host_name']);
        $r_subject   = stripslashes(check_html($row['subject'], "nohtml"));
        $r_comment   = stripslashes($row['comment']);
        $r_score     = $db->XMFnum($row['score']);
        $r_reason    = $db->XMFnum($row['reason']);
	if (!isset($level)) {
	} else {
	    if (!$comments) {
		echo "<ul>";
	    }
	}
	$comments++;
	if (!eregi("[a-z0-9]",$r_name)) { $r_name = $anonymous; }
	if (!eregi("[a-z0-9]",$r_subject)) { $r_subject = "["._NOSUBJECT."]"; }
	formatTimestamp($r_date);
	echo "<a href=\"modules.php?name=$module_name&amp;file=comments&amp;op=showreply&amp;tid=$r_tid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">$r_subject</a></font><font class=\"content\"> "._BY." $r_name "._ON." $datetime<br />";
	DisplayBabies($r_tid, $level+1, $dummy+1);
    }
    if ($level && $comments) {
    	echo "</ul>";
    }
}

function DisplayTopic ($sid, $pid=0, $tid=0, $mode="thread", $order=0, $thold=0, $level=0, $nokids=0) {
    global $hr, $user, $datetime, $cookie, $admin, $commentlimit, $anonymous, $reasons, $anonpost, $foot1, $foot2, $foot3, $foot4, $prefix, $acomm, $articlecomm, $db, $module_name, $nukeurl, $admin_file, $title, $bgcolor1, $bgcolor2, $bgcolor3;

   // if ($pid!=0) {
	incoh();
  //  }
    $sid   = $db->XMFnum( $sid );
    $pid   = $db->XMFnum( $pid );
    $thold = $db->XMFnum( $thold );
    $count_times = 0;
    cookiedecode($user);
    $q = "SELECT `tid`, `pid`, `sid`, `date`, `name`, `email`, `host_name`, `subject`, `comment`, `score`, `reason` FROM `".TABLE_COMMENTS."` WHERE `sid`='$sid' and `pid`='$pid'";
    if($thold != "") {
	$q .= " AND `score`>='$thold'";
    } else {
	$q .= " AND `score`>='0'";
    }
    if ($order==1) $q .= " ORDER BY `date` DESC";
    if ($order==2) $q .= " ORDER BY `score` DESC";
    $something = $db->sql_query($q);
    $num_tid = $db->sql_numrows($something);

    if ($acomm == 1) {
		nocomm();
		return;
    }
    if (($acomm == 0) AND ($articlecomm == 1)) {
	navbar($sid, $title, $thold, $mode, $order);
    }
    modone();

    while ($count_times < $num_tid) {
	br('1');
	Max_OpenTable();


	$row_q = $db->sql_fetchrow($something);
	$tid     = $db->XMFnum($row_q['tid']);
	$pid     = $db->XMFnum($row_q['pid']);
	$sid     = $db->XMFnum($row_q['sid']);
	$date    = stripslashes($row_q['date']);
        $c_name  = stripslashes($row_q['name']);
        $email   = stripslashes($row_q['email']);
        $host_name = stripslashes($row_q['host_name']);
        $subject = stripslashes(check_html($row_q['subject'], "nohtml"));
        $comment = stripslashes($row_q['comment']);
        $score   = $db->XMFnum($row_q['score']);
        $reason  = $db->XMFnum($row_q['reason']);
	if ($c_name == "") { $c_name = $anonymous; }
	if ($subject == "") { $subject = "["._NOSUBJECT."]"; }
	echo "<a name=\"$tid\"></a>";
	echo "<table width=\"99%\" border=\"0\"><tr bgcolor=\"$bgcolor1\"><td width=\"500\">";
	formatTimestamp($date);
	if ($email) {
	    echo "<b>$subject</b> <font class=\"content\">";
	    if(!$cookie[7]) {
		echo "("._SCORE." $score";
		if($reason>0) echo ", $reasons[$reason]";
		echo ")";
	    }
	    echo "<br />"._BY." <a href=\"mailto:$email\">$c_name</a> <b>($email)</b> "._ON." $datetime";
	} else {
	    echo "<b>$subject</b> <font class=\"content\">";
	    if(!$cookie[7]) {
		echo "("._SCORE." $score";
		if($reason>0) echo ", $reasons[$reason]";
		echo ")";
	    }
	    echo "<br />"._BY." $c_name "._ON." $datetime";
	}

    /* If you are admin you can see the Poster IP address */
    /* with this you can see who is flaming you...*/

	if (is_active("Journal")) {
	    $row = $db->sql_fetchrow($db->sql_query("SELECT `jid` FROM `".$prefix."_journal` WHERE `aid`='$c_name' AND `status`='yes' ORDER BY `pdate`,`jid` DESC LIMIT 0,1"));
	    $jid = $db->XMFnum($row['jid']);
	    if ($jid != "" AND isset($jid)) {
		$journal = " | <a href=\"modules.php?name=Journal&amp;file=display&amp;jid=$jid\">"._JOURNAL."</a>";
	    } else {
		$journal = "";
	    }
	}
	if ($c_name != $anonymous) {
	    $row2 = $db->sql_fetchrow($db->sql_query("SELECT `user_id` FROM `".TABLE_USERS."` WHERE `username`='$c_name'"));
	    $r_uid = $db->XMFnum($row2['user_id']);
	    echo "<br />(<a href=\"modules.php?name=Your_Account&amp;op=userinfo&amp;username=$c_name\">"._USERINFO."</a> | <a href=\"modules.php?name=Private_Messages&amp;mode=post&amp;u=$r_uid\">"._SENDAMSG."</a>$journal) ";
	}
	$row_url = $db->sql_fetchrow($db->sql_query("SELECT `user_website` FROM `".TABLE_USERS."` WHERE `username`='$c_name'"));
	$url = stripslashes($row_url['user_website']);
	if ($url != "http://" AND $url != "" AND eregi("http://", $url)) { echo "<a href=\"$url\" target=\"new\">$url</a> "; }

	if(defined('ADMIN_ALLOW')) {
	    $row3 = $db->sql_fetchrow($db->sql_query("SELECT `host_name` FROM `".TABLE_COMMENTS."` WHERE `tid`='$tid'"));
	    $host_name = stripslashes($row3['host_name']);
	    echo "<br /><b>(IP: $host_name)</b>";
	}

	echo "</font></td></tr><tr><td>";
	if(($cookie[10]) && (strlen($comment) > $cookie[10])) echo substr("$comment", 0, $cookie[10])."<br /><br /><b><a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$sid&tid=$tid&mode=$mode&order=$order&thold=$thold\">"._READREST."</a></b>";
	elseif(strlen($comment) > $commentlimit) echo substr("$comment", 0, $commentlimit)."<br /><br /><b><a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$sid&tid=$tid&mode=$mode&order=$order&thold=$thold\">"._READREST."</a></b>";
	else echo $comment;
	echo "</td></tr></table><br /><br />";
	if ($anonpost==1 OR defined('ADMIN_ALLOW') OR defined('USER_ALLOW')) {
	    echo "<font class=\"content\"> [ <a href=\"modules.php?name=$module_name&amp;file=comments&amp;op=Reply&amp;pid=$tid&amp;sid=$sid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._REPLY."</a>";
	}
	if ($pid != 0) {
	    $row4 = $db->sql_fetchrow($db->sql_query("SELECT `pid` FROM `".TABLE_COMMENTS."` WHERE `tid`='$pid'"));
	    $erin = $db->XMFnum($row4['pid']);
	    echo " | <a href=\"modules.php?name=$module_name&amp;file=comments&amp;sid=$sid&amp;pid=$erin&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._PARENT."</a>";
	}
	modtwo($tid, $score, $reason);

	if(defined('ADMIN_ALLOW')) {
	    echo " | <a href=\"".$admin_file.".php?op=RemoveComment&amp;tid=$tid&amp;sid=$sid\">"._DELETE."</a> ]</font><br /><br />";
	} elseif ($anonpost != 0 OR defined('ADMIN_ALLOW') OR defined('USER_ALLOW')) {
	    echo " ]</font>";
	    br('2');
	}

	DisplayKids($tid, $mode, $order, $thold, $level);
	echo "</ul>";
	if($hr) echo "<hr noshade size=\"1\">";
	$count_times += 1;
	Max_CloseTable();
    }
    modthree($sid, $mode, $order, $thold);
    if ($pid==0) {
	return array($sid, $pid, $subject);

    } else {
	incof();
    }
}

function singlecomment($tid, $sid, $mode, $order, $thold) {
    global $module_name, $user, $cookie, $datetime, $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $admin, $anonpost, $prefix, $textcolor2, $db;
    incoh();
    $sid = $db->XMFnum( $sid );
    $tid = $db->XMFnum( $tid );
    $row = $db->sql_fetchrow($db->sql_query("SELECT `date`, `name`, `email`, `subject`, `comment`, `score`, `reason` FROM `".TABLE_COMMENTS."` WHERE `tid`='$tid' AND `sid`='$sid'"));
    $date    = stripslashes($row['date']);
    $name    = stripslashes($row['name']);
    $email   = stripslashes($row['email']);
    $subject = stripslashes(check_html($row['subject'], "nohtml"));
    $comment = stripslashes($row['comment']);
    $score   = $db->XMFnum($row['score']);
    $reason  = $db->XMFnum($row['reason']);
    $titlebar = "<b>$subject</b>";
    if($name == "") $name = $anonymous;
    if($subject == "") $subject = "["._NOSUBJECT."]";
    modone();
    Max_OpenTable();
    echo "<table width=\"99%\" border=\"0\"><tr bgcolor=\"$bgcolor1\"><td width=\"500\">";
    formatTimestamp($date);
    if($email) echo "<b>$subject</b> <font class=\"content\" color=\"$textcolor2\">("._SCORE." $score)<br />"._BY." <a href=\"mailto:$email\"><font color=\"$bgcolor2\">$name</font></a> <font class=content><b>($email)</b></font> "._ON." $datetime";
    else echo "<b>$subject</b> <font class=content>("._SCORE." $score)<br />"._BY." $name "._ON." $datetime";
    echo "</td></tr><tr><td>$comment</td></tr></table>";
    br('2');
    if ($anonpost==1 OR defined('ADMIN_ALLOW') OR defined('USER_ALLOW')) {
	echo "<font class=content> [ <a href=\"modules.php?name=$module_name&amp;file=comments&amp;op=Reply&amp;pid=$tid&amp;sid=$sid&amp;mode=$mode&amp;order=$order&amp;thold=$thold\">"._REPLY."</a> | <a href=\"modules.php?name=$module_name&amp;file=article&amp;sid=$sid&mode=$mode&order=$order&thold=$thold\">"._ROOT."</a>";
    }
    modtwo($tid, $score, $reason);
    echo " ]";
    modthree($sid, $mode, $order, $thold);
    Max_CloseTable();
    incof();
}

function reply($pid, $sid, $mode, $order, $thold) {
    include("config.php");
    incoh();
    global $module_name, $user, $cookie, $datetime, $bgcolor1, $bgcolor2, $bgcolor3, $db, $anonpost, $anonymous;
    $pid = $db->XMFnum( $pid );
    $sid = $db->XMFnum( $sid );
    if ($anonpost == 0 AND !defined('USER_ALLOW')) {
	Max_OpenTable();
	echo "<center><font class=title><b>"._COMMENTREPLY."</b></font></center>";
	Max_CloseTable();
	br('1');
	Max_OpenTable();
	echo "<center>"._NOANONCOMMENTS."<br /><br />"._GOBACK."</center>";
	Max_CloseTable();
    } else {
	if ($pid != 0) {
	    $row = $db->sql_fetchrow($db->sql_query("SELECT `date`, `name`, `email`, `subject`, `comment`, `score` FROM `".TABLE_COMMENTS."` WHERE `tid`='$pid'"));
   	    $date    = stripslashes($row['date']);
            $name    = stripslashes($row['name']);
            $email   = stripslashes($row['email']);
            $subject = stripslashes(check_html($row['subject'], "nohtml"));
            $comment = stripslashes($row['comment']);
            $score   = $db->XMFnum($row['score']);
	} else {
		$row2 = $db->sql_fetchrow($db->sql_query("SELECT `time`, `title`, `hometext`, `bodytext`, `informant`, `notes` FROM `".TABLE_STORIES."` WHERE `sid`='$sid'"));
		$date         = stripslashes($row2['time']);
		$subject      = stripslashes(check_html($row2['title'], "nohtml"));
		$temp_comment = stripslashes($row2['hometext']);
		$comment2     = stripslashes($row2['bodytext']);
		$name         = stripslashes($row2['informant']);
		$notes        = stripslashes($row2['notes']);
	}
	if($comment == "") {
	    $comment = "$temp_comment<br /><br />$comment2";
	}
	Max_OpenTable();
	echo "<center><font class=title><b>"._COMMENTREPLY."</b></font></center>";
	Max_CloseTable();
	br('1');
	Max_OpenTable();
	if ($name == "") $name = $anonymous;
	if ($subject == "") $subject = "["._NOSUBJECT."]";
	formatTimestamp($date);
	echo "<b>$subject</b> <font class=\"content\">";
	if (!$temp_comment) echo"("._SCORE." $score)";
	if ($email) {
	    echo "<br />"._BY." <a href=\"mailto:$email\">$name</a> <font class=\"content\"><b>($email)</b></font> "._ON." $datetime";
	} else {
	    echo "<br />"._BY." $name "._ON." $datetime";
	}
	echo "<br /><br />$comment<br /><br />";
	if ($pid == 0) {
	    if ($notes != "") {
		echo "<b>"._NOTE."</b> <i>$notes</i><br /><br />";
	    } else {
		echo "";
	    }
	}
	if (!isset($pid) || !isset($sid)) { echo "Something is not right. This message is just to keep things from messing up down the road"; exit(); }
	if ($pid == 0) {
	    $row3 = $db->sql_fetchrow($db->sql_query("SELECT `title` FROM `".TABLE_STORIES."` WHERE `sid`='$sid'"));
	    $subject = stripslashes(check_html($row3['title'], "nohtml"));
	} else {
	    $row4 = $db->sql_fetchrow($db->sql_query("SELECT `subject` FROM `".TABLE_COMMENTS."` WHERE `tid`='$pid'"));
	    $subject = stripslashes(check_html($row4['subject'], "nohtml"));
	}
	Max_CloseTable();
	br('1');
	Max_OpenTable();
	echo "<form action=\"modules.php?name=$module_name&amp;file=comments\" method=\"post\">";
	echo "<font class=option><b>"._YOURNAME.":</b></font> ";
	if (defined('USER_ALLOW')) {
	    cookiedecode($user);
	    echo "<a href=\"modules.php?name=Your_Account\">$cookie[1]</a> <font class=\"content\">[ <a href=\"modules.php?name=Your_Account&amp;op=logout\">"._LOGOUT."</a> ]</font><br /><br />";
	} else {
    	    echo "<font class=\"content\">$anonymous";
	    echo " [ <a href=\"modules.php?name=Your_Account\">"._NEWUSER."</a> ]<br /><br />";
	}
	echo "<font class=\"option\"><b>"._SUBJECT.":</b></font><br />";
	if (!eregi("Re:",$subject)) $subject = "Re: ".substr($subject,0,81)."";
	echo "<input type=\"text\" name=\"subject\" size=\"50\" maxlength=\"85\" value=\"$subject\" /><br /><br />";
	echo "<font class=\"option\"><b>"._UCOMMENT.":</b></font><br />"
	    ."<textarea wrap=\"virtual\" cols=\"50\" rows=\"10\" name=\"comment\"></textarea><br />"
	    ."<font class=\"content\">"._ALLOWEDHTML."<br />";
	while (list($key,)= each($AllowableHTML)) echo " &lt;".$key."&gt;";
	echo "<br />";
	if (defined('USER_ALLOW') AND ($anonpost == 1)) { echo "<input type=\"checkbox\" name=\"xanonpost\"> "._POSTANON."<br />"; }
	echo "<input type=\"hidden\" name=\"pid\" value=\"$pid\" />\n"
    	    ."<input type=\"hidden\" name=\"sid\" value=\"$sid\" />\n"
	    ."<input type=\"hidden\" name=\"mode\" value=\"$mode\" />\n"
	    ."<input type=\"hidden\" name=\"order\" value=\"$order\" />\n"
	    ."<input type=\"hidden\" name=\"thold\" value=\"$thold\" />\n"
	    ."<input type=\"submit\" name=\"op\" value=\""._PREVIEW."\" />\n"
	    ."<input type=\"submit\" name=\"op\" value=\""._OK."\" />\n"
	    ."<select name=\"posttype\">\n"
	    ."<option value=\"exttrans\">"._EXTRANS."</option>\n"
	    ."<option value=\"html\" >"._HTMLFORMATED."</option>\n"
	    ."<option value=\"plaintext\" selected>"._PLAINTEXT."</option>\n"
	    ."</select></font></form>\n";
	Max_CloseTable();
    }
    incof();
}

function replyPreview ($pid, $sid, $subject, $comment, $xanonpost, $mode, $order, $thold, $posttype) {
    incoh();
    global $module_name, $user, $cookie, $AllowableHTML, $anonymous, $anonpost;
    if ((eregi("<[^>]script*\"?[^>]>", $comment))) {
   die ("<center><img src='images/logo.gif' alt='' /><br /><br /><b>The script html tag is not allowed</b><br /><br />[ <a href=\"javascript:history.go(-1)\"><b>Go Back</b></a> ]");
    }
    Max_OpenTable();
    echo "<center><font class='title'><b>"._COMREPLYPRE."</b></font></center>";
    Max_CloseTable();
    br('1');
    Max_OpenTable();
    cookiedecode($user);
    $subject = stripslashes(check_html($subject, "nohtml"));
    $comment = stripslashes($comment);
    if (!isset($pid) || !isset($sid)) {
        echo ""._NOTRIGHT."";
        exit();
    }
    echo "<b>$subject</b>";
    br('1');
    echo "<font class=\"content\">"._BY." ";
    if (defined('USER_ALLOW')) {
	echo "$cookie[1]";
    } else {
        echo "$anonymous";
    }
    echo " "._ONN."</font>";
    br('2');
    if ($posttype=="exttrans") {
        echo nl2br(htmlspecialchars($comment));
    } elseif ($posttype=="plaintext") {
        echo nl2br($comment);
    } else {
        echo $comment;
    }
    Max_CloseTable();
    br('1');
    Max_OpenTable();
    echo "<form action=\"modules.php?name=$module_name&amp;file=comments\" method=\"post\"><font class=\"option\"><b>"._YOURNAME.":</b></font> ";
    if (defined('USER_ALLOW')) {
        echo "<a href=\"modules.php?name=Your_Account\">$cookie[1]</a> <font class=\"content\">[ <a href=\"modules.php?name=Your_Account&amp;op=logout\">"._LOGOUT."</a> ]</font><br /><br />";
    } else {
        echo "<font class=\"content\">$anonymous";
	br('2');
    }
    echo "<font class=\"option\"><b>"._SUBJECT.":</b></font><br />"
	."<input type=\"text\" name=\"subject\" size=\"50\" maxlength=\"85\" value=\"$subject\" /><br /><br />"
	."<font class=\"option\"><b>"._UCOMMENT.":</b></font><br />"
	."<textarea wrap=\"virtual\" cols=\"50\" rows=\"10\" name=\"comment\">$comment</textarea><br />"
	."<font class=\"content\">"._ALLOWEDHTML."<br />";
    while (list($key,) = each($AllowableHTML)) echo " &lt;".$key."&gt;";
    br('1');
    if (($xanonpost) AND ($anonpost == 1)){
        echo "<input type=\"checkbox\" name=\"xanonpost\" checked /> "._POSTANON."<br />";
    } elseif ((defined('USER_ALLOW')) AND ($anonpost == 1)) {
        echo "<input type=\"checkbox\" name=\"xanonpost\" /> "._POSTANON."<br />";
    }
    echo "<input type=\"hidden\" name=\"pid\" value=\"$pid\" />"
        ."<input type=\"hidden\" name=\"sid\" value=\"$sid\" />"
	."<input type=\"hidden\" name=\"mode\" value=\"$mode\" />"
        ."<input type=\"hidden\" name=\"order\" value=\"$order\" />"
	."<input type=\"hidden\" name=\"thold\" value=\"$thold\" />"
        ."<input type='submit' name='op' value=\""._PREVIEW."\" />"
        ."<input type='submit' name='op' value=\""._OK."\" />\n"
	."<select name=\"posttype\"><option value=\"exttrans\"";
    if ($posttype=="exttrans") {
        echo " selected";
    }
    echo ">"._EXTRANS."</option>\n"
	."<OPTION value=\"html\"";;
    if ($posttype=="html") {
        echo " selected";
    }
    echo ">"._HTMLFORMATED."</option>\n"
	."<OPTION value=\"plaintext\"";
    if (($posttype!="exttrans") && ($posttype!="html")) {
        echo " selected";
    }
    echo ">"._PLAINTEXT."</option></select></font></form>";
    Max_CloseTable();
    incof();
}

function CreateTopic ($xanonpost, $subject, $comment, $pid, $sid, $host_name, $mode, $order, $thold, $posttype) {
    global $module_name, $user, $userinfo, $EditedMessage, $cookie, $AllowableHTML, $ultramode, $prefix, $anonpost, $articlecomm;

    cookiedecode($user);
    $author  = addslashes($author);
    $subject = addslashes($subject);
    $comment = addslashes($comment);
    if($posttype=="exttrans") {
    	$comment = FixQuotes(nl2br(htmlspecialchars(check_words($comment))));
    } elseif($posttype=="plaintext") {
    	$comment = FixQuotes(nl2br(filter_text($comment)));
    } else {
	$comment = FixQuotes(filter_text($comment));
    }

    if ((defined('USER_ALLOW')) && (!$anonpost)) {
	getusrinfo($user);
	$name  = stripslashes($userinfo['username']);
	$email = stripslashes($userinfo['femail']);
	$url   = stripslashes($userinfo['user_website']);
	$score = 1;
    } else {
	$name = ""; $email = ""; $url = "";
	$score = 0;
    }
    $ip = $_SERVER["REMOTE_HOST"];
    if (empty($ip)) {
        $ip = $_SERVER["REMOTE_ADDR"];
    }
    $sid = $GLOBALS['db']->XMFnum( $sid );
    $fakeresult = $GLOBALS['db']->sql_query("SELECT `acomm` FROM `".TABLE_STORIES."` WHERE `sid`='$sid'");
    $fake = $GLOBALS['db']->sql_numrows($fakeresult);
    //$comment = trim($comment);
   // $comment = stripslashes(check_html($comment, ""));
    echo "CreateTopic $fake - $articlecomm";
    if (($fake == 1) AND ($articlecomm == 1)) {
      $fakerow = $GLOBALS['db']->sql_fetchrow($fakeresult);
      $acomm   = $GLOBALS['db']->XMFnum($fakerow['acomm']);
  if (((($anonpost == 0) AND (defined('USER_ALLOW'))) OR ($anonpost == 1)) AND ($acomm == 0)) {
      $url     = addslashes($url);
      $subject = addslashes($subject);
      $comment = addslashes($comment);
      $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_COMMENTS."` VALUES (NULL, '$pid', '$sid', now(), '$name', '$email', '$url', '$ip', '$subject', '$comment', '$score','0')");
      $GLOBALS['db']->sql_query("UPDATE `".TABLE_STORIES."` SET `comments`=comments+1 WHERE `sid`='$sid'");
     // update_points(5);

      if ($ultramode) {
        ultramode();
      }
  } else {
      echo "Nice try...";
      die();
  }
    } else {
	incoh();
	echo "According to my records, the topic you are trying "
	    ."to reply to does not exist. If you're just trying to be "
	    ."annoying, well then too bad.";
	incof();
	die();
    }
    $GLOBALS['db']->sql_freeresult($fakeresult);
    if (isset($cookie[4])) { $options .= "&mode=$cookie[4]"; } else { $options .= "&mode=thread"; }
    if (isset($cookie[5])) { $options .= "&order=$cookie[5]"; } else { $options .= "&order=0"; }
    if (isset($cookie[6])) { $options .= "&thold=$cookie[6]"; } else { $options .= "&thold=0"; }
    redirect_url("modules.php?name=$module_name&file=article&sid=$sid$options");
}
if (isset($sid)) $sid = $GLOBALS['db']->XMFnum($sid);
if (isset($tid)) $tid = $GLOBALS['db']->XMFnum($tid);
if (isset($pid)) $pid = $GLOBALS['db']->XMFnum($pid);
if (isset($order)) $order = $GLOBALS['db']->XMFnum($order);
if (isset($thold)) $thold = $GLOBALS['db']->XMFnum($thold);
switch($op) {

    case "Reply":
	reply($pid, $sid, $mode, $order, $thold);
	break;

    case ""._PREVIEW."":
	replyPreview ($pid, $sid, $subject, $comment, $xanonpost, $mode, $order, $thold, $posttype);
	break;

    case ""._OK."":
	CreateTopic($xanonpost, $subject, $comment, $pid, $sid, $host_name, $mode, $order, $thold, $posttype);
	break;

    case "moderate":
	    include("mainfile.php");
	if((defined('ADMIN_ALLOW')) || ($moderate==2)) {
	    while(list($tdw, $emp) = each($HTTP_POST_VARS)) {
		if (eregi("dkn",$tdw)) {
		    $emp = explode(":", $emp);
		    if($emp[1] != 0) {
			$tdw = ereg_replace("dkn", "", $tdw);
			$emp[0] = $GLOBALS['db']->XMFnum($emp[0]);
			$emp[1] = $GLOBALS['db']->XMFnum($emp[1]);
			$tdw    = $GLOBALS['db']->XMFnum($tdw);
			$q = "UPDATE `".TABLE_COMMENTS."` SET";
			if(($emp[1] == 9) && ($emp[0]>=0)) { # Overrated
			    $q .= " `score`=score-1 where `tid`='$tdw'";
			} elseif (($emp[1] == 10) && ($emp[0]<=4)) { # Underrated
			    $q .= " `score`=score+1 where `tid`='$tdw'";
			} elseif (($emp[1] > 4) && ($emp[0]<=4)) {
			    $q .= " `score`=score+1, `reason`='$emp[1]' where `tid`='$tdw'";
			} elseif (($emp[1] < 5) && ($emp[0] > -1)) {
			    $q .= " `score`=score-1, `reason`='$emp[1]' where `tid`='$tdw'";
			} elseif (($emp[0] == -1) || ($emp[0] == 5)) {
			    $q .= " `reason`=$emp[1] where `tid`='$tdw'";
			}
			if(strlen($q) > 20) $db->sql_query($q);
		    }
		}
	    }
	}
	redirect_url("modules.php?name=$module_name&file=article&sid=$sid&mode=$mode&order=$order&thold=$thold");
	break;

    case "showreply":
	DisplayTopic($sid, $pid, $tid, $mode, $order, $thold);
	break;

    default:
        if ( empty($pid) ) { $pid = 0; }
	if ((isset($tid)) && (!isset($pid))) {
	    singlecomment($tid, $sid, $mode, $order, $thold);
	} elseif  (!isset($pid)) {
	    redirect_url("modules.php?name=$module_name&file=article&sid=$sid&mode=$mode&order=$order&thold=$thold");
	} else {
	    if(!isset($pid)) $pid=0;
	    DisplayTopic($sid, $pid, $tid, $mode, $order, $thold);
	}
	break;

}