<?php //***************

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isset($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isset($_GET['id'])) { $id = intval($_GET['id']); } else { $id = ""; }

if (isset($_POST['gname'])) { $gname = trim(censure(inCode($_POST['gname']))); } else { $gname = ""; }
if (isset($_POST['gemail'])) { $gemail = inCode($_POST['gemail']); } else { $gemail = ""; }
if (isset($_POST['name'])) { $name = preg_replace("#[/_']#is","-",trim(censure(inCode($_POST['name'])))); } else { $name = ""; }
if (isset($_POST['text'])) {
	if (!isset($_POST['Preview'])) {
		$text = censure(inCode($_POST['text']));
	} else {
		$text = censure(inCode($_POST['text'],0));
	}
} else { $text = ""; }
if (isset($_POST['tags'])) { $tags = censure(inCode($_POST['tags'])); } else { $tags = ""; }
if (isset($_GET['quote'])) { $quote = intval($_GET['quote']); } else { $quote = ""; }
if (isset($_POST['reason'])) { $reason = inCode($_POST['reason']); } else { $reason = ""; }
if (isset($_POST['smiles'])) { $smiles = intval($_POST['smiles']); } else { $smiles = 0; }
if (isset($_POST['notification'])) { $notification = intval($_POST['notification']); } else { $notification = 0; }
if (isset($_POST['operation'])) { $operation = inCode($_POST['operation']); } else {
	if (isset($_GET['operation'])) { $operation = inCode($_GET['operation']); } else { $operation = ""; }
}
if (isset($_POST['ok'])) { $ok = inCode($_POST['ok']); } else { if (isset($_GET['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; } }

if (isset($_GET['pg'])) { $pg = intval($_GET['pg']); } else { $pg = 1; }
if ($op=="viewForum") { $ofsppg = 20; } else { $ofsppg = 10; }
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function viewMain() {
	global $dblink;

	echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";
	$result = $dblink->get_list("SELECT * FROM memht_forum_categories ORDER BY position");
	foreach ($result as $row) {
		$c_id = intval($row['id']);
		$c_name = outCode($row['name']);
		$c_description = outCode($row['description']);
		
		if ($c_description!="") { $c_more = "<br><span class='small'>".$c_description."</span>"; } else { $c_more = ""; }
				
		echo "<tr><td colspan='6' class='forum_category'><a href='index.php?page=forum&op=viewCategory&id=$c_id&title=".mem_urlencode($c_name)."' title='$c_name'><b>$c_name</b></a>$c_more</td></tr>";
		echo "<tr><td width='5%' class='forum_legend'></td><td width='50%' class='forum_legend'><b>"._FORUM_."</b></td><td width='10%' align='center' class='forum_legend'><b>"._THREADS_."</b></td><td width='10%' align='center' class='forum_legend'><b>"._POSTS_."</b></td><td width='25%' align='center' class='forum_legend'><b>"._LASTPOST_."</b></td></tr>";
		$result_forum = $dblink->get_list("SELECT * FROM memht_forum_forums WHERE category=$c_id AND parent=0 ORDER BY position");
		foreach ($result_forum as $row_forum) {
			$f_id = intval($row_forum['id']);
			$f_name = outCode($row_forum['name']);
			$f_description = outCode($row_forum['description']);
			$f_auth_view = intval($row_forum['auth_view']);
			$f_auth_read = intval($row_forum['auth_read']);
			$f_auth_write = intval($row_forum['auth_write']);
			$f_auth_delete = intval($row_forum['auth_delete']);
			$f_status = intval($row_forum['status']);
			
			if (isAuthorized($f_id,"view")) {
				$f_more = "";
				$mods = array();
				$authres = $dblink->get_list("SELECT m.groupid,g.name FROM memht_forum_moderation AS m JOIN memht_groups AS g ON m.groupid=g.id WHERE m.forum=$f_id");
				foreach ($authres as $authrow) {
					$mods[] = outCode($authrow['name']);
				}
				$forum_mods = implode(", ",$mods);
				if ($forum_mods!="") { $f_more .= "<br><span class='small'>"._MODERATORS_.": <i>".$forum_mods."</i></span>"; }
				
				if ($f_description!="") { $f_more .= "<br><span class='small'>".$f_description."</span>"; }
				
				echo "<tr><td width='5%' class='forum_title'>".getIcon($f_id,"forum")."</td><td width='50%' class='forum_title'><a href='index.php?page=forum&op=viewForum&id=$f_id&title=".mem_urlencode($f_name)."' title='$f_name'><b>$f_name</b></a>$f_more</td><td width='10%' align='center' class='forum_title'>".getThreadsNumber($f_id)."</td><td width='10%' align='center' class='forum_title'>".getPostsNumber($f_id)."</td><td width='25%' align='center' class='forum_title'>".getLastPostInfoByForum($f_id)."</td></tr>";
			}
		}
	}
	
	echo "<tr><td colspan='6' class='forum_category'><b>"._INFORMATIONS_."</b></td></tr>";
	echo "<tr><td colspan='6' class='forum_spacer'>&nbsp;</td></tr>\n"; 
	echo "<tr><td colspan='6' class='forum_title'>";
		$nusers = $dblink->get_num("SELECT id FROM memht_utenti");
		$last_user = $dblink->get_row("SELECT user FROM memht_utenti ORDER BY id DESC LIMIT 1");
		$lastuser = outCode($last_user['user']);
		$nthreads = $dblink->get_num("SELECT * FROM memht_forum_posts WHERE parent='0' AND flag!=2");
		$nposts = $dblink->get_num("SELECT * FROM memht_forum_posts WHERE flag!=2");
		echo _TOTUSERS_.": <i>$nusers</i><br>";
		echo _LASTUSER_.": <a href='index.php?page=users&op=userInfo&usert=$lastuser' title='$lastuser'><i>$lastuser</i></a><br>";
		echo _THREADS_.": <i>$nthreads</i><br>";
		echo _POSTS_.": <i>$nposts</i>";
	echo "</td></tr>";
	
	echo "<tr><td colspan='6' class='forum_category'><b>Top 5 "._USERS_."</b></td></tr>";
	echo "<tr><td colspan='6' class='forum_spacer'>&nbsp;</td></tr>\n"; 
	echo "<tr><td colspan='6' class='forum_title'>";
	$i = 0;
	$result = $dblink->get_list("SELECT user,post FROM memht_utenti ORDER BY post DESC LIMIT 5");
	foreach ($result as $row) {
		$tuser = outCode($row['user']);
		$tpost = intval($row['post']);
		$i++;
		echo "<div><a href='index.php?page=users&op=userInfo&usert=$tuser' title='$tuser'>$tuser</a>: <i>$tpost</i> "._POSTS_."</div>";
	}
	echo "</td></tr>";
	echo "</table>\n";
}

function viewCategory($id) {
	global $dblink,$siteConfig;
	
	$id = intval($id);
	
	echo "<div style='margin-bottom:4px;'><b>".getTitleByCategory($id)."</b></div>";
	echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";
	$row = $dblink->get_row("SELECT * FROM memht_forum_categories WHERE id=$id");
	$c_id = intval($row['id']);
	$c_name = outCode($row['name']);
	$c_description = outCode($row['description']);
		
		if ($c_description!="") { $c_more = "<br><span class='small'>".$c_description."</span>"; } else { $c_more = ""; }
		echo "<tr><td colspan='6' class='forum_intitle'><b>$c_name</b>$c_more</td></tr>";
		
		echo "<tr><td width='5%' class='forum_legend'></td><td width='50%' class='forum_legend'><b>"._FORUM_."</b></td><td width='10%' align='center' class='forum_legend'><b>"._THREADS_."</b></td><td width='10%' align='center' class='forum_legend'><b>"._POSTS_."</b></td><td width='25%' align='center' class='forum_legend'><b>"._LASTPOST_."</b></td></tr>";
		$result = $dblink->get_list("SELECT * FROM memht_forum_forums WHERE category=$c_id AND parent=0 ORDER BY position");
		foreach ($result as $row) {
			$f_id = intval($row['id']);
			$f_name = outCode($row['name']);
			$f_description = outCode($row['description']);
			$f_auth_view = intval($row['auth_view']);
			$f_auth_read = intval($row['auth_read']);
			$f_auth_write = intval($row['auth_write']);
			$f_auth_delete = intval($row['auth_delete']);
			$f_status = intval($row['status']);
			
			if (isAuthorized($f_id,"view")) { 		
				$f_more = "";
				$mods = array();
				$authres = $dblink->get_list("SELECT m.groupid,g.name FROM memht_forum_moderation AS m JOIN memht_groups AS g ON m.groupid=g.id WHERE m.forum=$f_id");
				foreach ($authres as $authrow) {
					$mods[] = outCode($authrow['name']);
				}
				$forum_mods = implode(", ",$mods);
				if ($forum_mods!="") { $f_more .= "<br><span class='small'>"._MODERATORS_.": <i>".$forum_mods."</i></span>"; }
				
				if ($f_description!="") { $f_more .= "<br><span class='small'>".$f_description."</span>"; }
				
				echo "<tr><td width='5%' class='forum_title'>".getIcon($f_id,"forum")."</td><td width='50%' class='forum_title'><a href='index.php?page=forum&op=viewForum&id=$f_id&title=".mem_urlencode($f_name)."' title='$f_name'><b>$f_name</b></a>$f_more</td><td width='10%' align='center' class='forum_title'>".getThreadsNumber($f_id)."</td><td width='10%' align='center' class='forum_title'>".getPostsNumber($f_id)."</td><td width='25%' align='center' class='forum_title'>".getLastPostInfoByForum($f_id)."</td></tr>";
			}
		}
	echo "</table>\n";
}

function viewForum($forumid,$ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig,$userid,$userInfo;
	
	$forumid = intval($forumid);
	if (isAuthorized($forumid,"read")) {
		$showforum = ($dblink->get_num("SELECT id FROM memht_forum_forums WHERE id=$forumid")>0) ? true : false;
		
		if ($row = $dblink->get_row("SELECT parent FROM memht_forum_forums WHERE id=$forumid")) {
			$parent = intval($row['parent']);
			$showforum = true;
		} else {
			$parent = 0;
			$showforum = false;
		}
		
		echo "<div><b>".getTitleByForum($forumid)."</b></div>";
		
		if ($result = $dblink->get_list("SELECT * FROM memht_forum_forums WHERE parent=$forumid")) {
			echo "<div style='margin:4px 0;'>\n";
				
				echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";
					echo "<tr><td colspan='6' class='forum_intitle'><b>"._FORUM_."</b></td></tr>";
					echo "<tr><td width='5%' class='forum_legend'></td><td width='50%' class='forum_legend'><b>"._TITLE_."</b></td><td width='10%' align='center' class='forum_legend'><b>"._THREADS_."</b></td><td width='10%' align='center' class='forum_legend'><b>"._POSTS_."</b></td><td width='25%' align='center' class='forum_legend'><b>"._LASTPOST_."</b></td></tr>";
					$result = $dblink->get_list("SELECT * FROM memht_forum_forums WHERE parent=$forumid ORDER BY position");
					foreach ($result as $row) {
						$f_id = intval($row['id']);
						$f_name = outCode($row['name']);
						$f_description = outCode($row['description']);
						$f_auth_view = intval($row['auth_view']);
						$f_auth_read = intval($row['auth_read']);
						$f_auth_write = intval($row['auth_write']);
						$f_auth_delete = intval($row['auth_delete']);
						$f_status = intval($row['status']);
						
						if (isAuthorized($f_id,"view")) { 		
							$f_more = "";
							$mods = array();
							$authres = $dblink->get_list("SELECT m.groupid,g.name FROM memht_forum_moderation AS m JOIN memht_groups AS g ON m.groupid=g.id WHERE m.forum=$forumid");
							foreach ($authres as $authrow) {
								$mods[] = outCode($authrow['name']);
							}
							$forum_mods = implode(", ",$mods);
							if ($forum_mods!="") { $f_more .= "<br><span class='small'>"._MODERATORS_.": <i>".$forum_mods."</i></span>"; }
							
							if ($f_description!="") { $f_more .= "<br><span class='small'>".$f_description."</span>"; }
							
							echo "<tr><td width='5%' class='forum_title'>".getIcon($f_id,"forum")."</td><td width='50%' class='forum_title'><a href='index.php?page=forum&op=viewForum&id=$f_id&title=".mem_urlencode($f_name)."' title='$f_name'><b>$f_name</b></a>$f_more</td><td width='10%' align='center' class='forum_title'>".getThreadsNumber($f_id)."</td><td width='10%' align='center' class='forum_title'>".getPostsNumber($f_id)."</td><td width='25%' align='center' class='forum_title'>".getLastPostInfoByForum($f_id)."</td></tr>";
						}
					}
				echo "</table>\n";
				
			echo "</div>\n";
		}
		
		if ($showforum) {			
			$modcheck = ($parent==0) ? $forumid : $parent ;
			$forum_mods = "";
			$mods = array();
			$authres = $dblink->get_list("SELECT m.groupid,g.name FROM memht_forum_moderation AS m JOIN memht_groups AS g ON m.groupid=g.id WHERE m.forum=$modcheck");
			foreach ($authres as $authrow) {
				$mods[] = outCode($authrow['name']);
			}
			$forum_mods = implode(", ",$mods);
			if ($forum_mods!="") { echo "<div class='small'>"._MODERATORS_.": <i>".$forum_mods."</i></div>"; }
			
			echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td valign='bottom' style='padding-bottom:4px;'><a href='rss.php?page=forum&forum=$forumid' title='"._FORUMRSSFEED_."'><img src='images/rss-syndicate_box.gif' border='0' alt='"._FORUMRSSFEED_."'></a>";
			
			if (getStatus($forumid,"forum")==1) {
				echo "</td><td align='right' valign='bottom' style='padding-bottom:4px;'><a href='index.php?page=forum&op=newThread&id=$forumid' title='"._NEWTHREAD_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/newthread.gif' border='0' alt='"._NEWTHREAD_."'></a></td></tr></table>";
			} else {
				echo "</td><td align='right' valign='bottom' style='padding-bottom:4px;'><img src='templates/".$siteConfig['template']."/images/forum/buttons/locked.gif' border='0' alt='Locked'></td></tr></table>";
			}
			
			echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";
			
			$row = $dblink->get_row("SELECT * FROM memht_forum_forums WHERE id='$forumid'");
			$f_name = outCode($row['name']);
			
			echo "<tr><td colspan='6' class='forum_intitle'><b>"._THREADS_."</b></td></tr>";
			
			echo "<tr><td width='5%' class='forum_legend'></td><td width='50%' class='forum_legend'><b>"._TITLE_."</b></td><td width='5%' align='center' class='forum_legend'><b>"._REPLIES_."</b></td><td width='5%' align='center' class='forum_legend'><b>"._VIEWS_."</b></td><td width='35%' align='center' class='forum_legend'><b>"._LASTPOST_."</b></td></tr>";
		
			//Sticky
			$n = 0;
			if ($pg==1) {
				$result = $dblink->get_list("SELECT p.id,p.lastchild,p.name,p.status,p.author AS tauthor,DATE_FORMAT(d.date,'".$siteConfig['timestamp']."') as date,d.author,(SELECT COUNT(*) FROM memht_forum_posts WHERE id=p.id OR parent=p.id) AS pstnum FROM memht_forum_posts AS p JOIN memht_forum_forums AS f JOIN memht_forum_posts AS d ON p.forum=f.id AND p.lastchild=d.id WHERE p.forum=$forumid AND p.flag=1 ORDER BY p.lastchild DESC");
				foreach ($result as $row) {
					$p_id = intval($row['id']);
					$p_ch = intval($row['lastchild']);
					$p_name = outCode($row['name'],0);
					$p_tauthor = outCode($row['tauthor']);
					$p_author = outCode($row['author']);
					$p_date = $row['date'];
					$p_status = intval($row['status']);
					$pstnum = intval($row['pstnum']);
					
					$link = "index.php?page=forum&op=viewThread&id=$p_id&title=".mem_urlencode($p_name);
					if ($pstnum>10) { $link .= "&pg=".ceil($pstnum/10); }
					if ($p_id!=$p_ch) { $link .= "#post{$p_ch}"; }
					
					echo "<tr><td class='forum_threadlist'>".getIcon($p_id,"thread")."</td><td class='forum_threadlist'><b>Sticky:</b> <a href='index.php?page=forum&op=viewThread&id=$p_id&title=".mem_urlencode($p_name)."' title='$p_name'>$p_name</a><div class='small'>"._AUTHOR_.": $p_tauthor</div></td><td align='center' class='forum_threadlist'>".getRepliesNumber($p_id)."</td><td align='center' class='forum_threadlist'>".getViewsNumber($p_id)."</td><td align='center' class='forum_threadlist'><a href='$link' title='$p_name'><b>$p_author</b><br>$p_date</a></td></tr>";
					$n++;
				}
			}
			
			//Standard
			$result = $dblink->get_list("SELECT p.id,p.lastchild,p.name,p.status,p.flag,p.text,p.author AS tauthor,DATE_FORMAT(d.date,'".$siteConfig['timestamp']."') as date,d.author,(SELECT COUNT(*) FROM memht_forum_posts WHERE id=p.id OR parent=p.id) AS pstnum FROM memht_forum_posts AS p JOIN memht_forum_forums AS f JOIN memht_forum_posts AS d ON p.forum=f.id AND p.lastchild=d.id WHERE p.forum=$forumid AND (p.flag=0 OR p.flag=2) ORDER BY p.lastchild DESC LIMIT $ofsbgn,$ofsppg");
			foreach ($result as $row) {
				$p_id = intval($row['id']);
				$p_ch = intval($row['lastchild']);
				$p_name = outCode($row['name'],0);
				$p_tauthor = outCode($row['tauthor']);
				$p_author = outCode($row['author']);
				$p_date = $row['date'];
				$p_status = intval($row['status']);
				$p_flag = intval($row['flag']);
				$pstnum = intval($row['pstnum']);
				
				$link = "index.php?page=forum&op=viewThread&id=$p_id&title=".mem_urlencode($p_name);
				if ($pstnum>10) { $link .= "&pg=".ceil($pstnum/10); }
				if ($p_id!=$p_ch) { $link .= "#post{$p_ch}"; }
				
				if ($p_flag==0) {
					echo "<tr><td class='forum_threadlist' align='center'>".getIcon($p_id,"thread")."</td><td class='forum_threadlist'><a href='index.php?page=forum&op=viewThread&id=$p_id&title=".mem_urlencode($p_name)."' title='$p_name'>$p_name</a><div class='small'>"._AUTHOR_.": $p_tauthor</div></td><td align='center' class='forum_threadlist'>".getRepliesNumber($p_id)."</td><td align='center' class='forum_threadlist'>".getViewsNumber($p_id)."</td><td align='center' class='forum_threadlist'><a href='$link' title='$p_name'><b>$p_author</b><br>$p_date</a></td></tr>";
				} else {
					//2 (Moved)
					$p_redirect = intval($row['text']);
					echo "<tr><td class='forum_threadlist' align='center'>-</td><td class='forum_threadlist'><img src='images/right.gif' border='0' alt='Moved'> <b>"._MOVED_.":</b> <a href='index.php?page=forum&op=viewThread&id=$p_redirect&title=".mem_urlencode($p_name)."' title='$p_name'>$p_name</a><div class='small'>"._AUTHOR_.": $p_tauthor</div></td><td align='center' class='forum_threadlist'>-</td><td align='center' class='forum_threadlist'>-</td><td align='center' class='forum_threadlist'>-</td></tr>";
				}
				$n++;
			}
			
			if ($n==0) { echo "<tr><td class='forum_threadlist' align='center' colspan='5'>"._EMPTY_."</td></tr>"; }
		
			echo "</table>\n";

			if (getStatus($forumid,"forum")==1) {
				echo "<div align='right' style='margin:4px 0;'><a href='index.php?page=forum&op=newThread&id=$forumid' title='"._NEWTHREAD_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/newthread.gif' border='0' alt='NewThread'></a></div>";
			} else {
				echo "<div align='right' style='margin:4px 0;'><img src='templates/".$siteConfig['template']."/images/forum/buttons/locked.gif' border='0' alt='Locked'></a></div>";
			}
			
			//Pages
			include_once("inc/class/paginationSystem.class.php");
			$ps = new paginationSystem();
			$ps->items = $ofsppg;
			$ps->actpg = $pg;
			$ps->query = "SELECT id FROM memht_forum_posts WHERE forum=$forumid AND parent=0 AND flag=0";
			$ps->url = "index.php?page=forum&op=viewForum&id=$forumid&title=".mem_urlencode($f_name)."&pg={{N}}";
			$ps->show();
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._YOUNOPERMTOACCESSTHISFORUM_."</b></div>\n";
		if (!isUser($userid)) {
			echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>\n";
			@session_start();
			$_SESSION['redirect_url'] = "index.php?page=forum&op=viewForum&id=$forumid";
			$_SESSION['redirect_age'] = time();
		}
	}
}

function viewThread($threadid,$ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig,$userid,$userInfo;

	$threadid = intval($threadid);
	
	if ($row = $dblink->get_row("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date,DATE_FORMAT(edited, '".$siteConfig['timestamp']."') as edited FROM memht_forum_posts WHERE id='$threadid'")) {
		$ft_id = intval($row['id']);
		$ft_parent = intval($row['parent']);
		$ft_forum = intval($row['forum']);
		$ft_name = outCode($row['name'],0);
		$ft_flag = intval($row['flag']);
		
		$lastp = $ft_id; /*Last post cookie*/
		
		if ($ft_flag==2) {
			//Moved
			$ft_redirect = intval($row['text']);
			echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$ft_redirect&title=".mem_urlencode($ft_name)."'>"; 
			exit;
		}
		
		if ($ft_parent!=0) {
			echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$ft_parent&title=".mem_urlencode($ft_name)."#post$threadid'>"; 
			exit;
		}
		
		if (isAuthorized($ft_forum,"read")) {
			$dblink->query("UPDATE memht_forum_posts SET hits=hits+1 WHERE id='$threadid'");
			
			$row = $dblink->get_row("SELECT contributes FROM memht_utenti ORDER BY contributes DESC LIMIT 1");
			$maxcontr = intval($row['contributes']);
			
			echo "<div><b>".getTitleByThread($threadid)."</b></div>";
			
			$forum_mods = "";
			$mods = array();
			$authres = $dblink->get_list("SELECT m.groupid,g.name FROM memht_forum_moderation AS m JOIN memht_groups AS g ON m.groupid=g.id WHERE m.forum=$ft_forum");
			foreach ($authres as $authrow) {
				$mods[] = outCode($authrow['name']);
			}
			$forum_mods = implode(", ",$mods);
			if ($forum_mods!="") { echo "<span class='small'>"._MODERATORS_.": <i>".$forum_mods."</i></span>"; }
			
			//Pages
			include_once("inc/class/paginationSystem.class.php");
			$ps = new paginationSystem();
			$ps->items = $ofsppg;
			$ps->actpg = $pg;
			$ps->query = "SELECT * FROM memht_forum_posts WHERE id=$threadid OR parent=$threadid";
			$ps->url = "index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($ft_name)."&pg={{N}}";
			$ps->show();
			
			if (getStatus($ft_forum,"forum")==1 AND getStatus($threadid,"thread")==1) {
				echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td valign='bottom' style='padding-bottom:4px;'><a href='rss.php?page=forum&forum=$ft_forum' title='"._FORUMRSSFEED_."'><img src='images/rss-syndicate_box.gif' border='0' alt='"._FORUMRSSFEED_."'></a></td><td align='right' valign='bottom' style='padding-bottom:4px;'><a href='index.php?page=forum&op=reply&id=$threadid' title='"._REPLY_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/reply.gif' border='0' alt='Reply'></a></td></tr></table>";
			} else {
				echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td valign='bottom' style='padding-bottom:4px;'><a href='rss.php?page=forum&forum=$ft_forum' title='"._FORUMRSSFEED_."'><img src='images/rss-syndicate_box.gif' border='0' alt='"._FORUMRSSFEED_."'></a></td><td align='right' valign='bottom' style='padding-bottom:4px;'><img src='templates/".$siteConfig['template']."/images/forum/buttons/locked.gif' border='0' alt='Locked'></td></tr></table>";
			}
			
			echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";
			
			echo "<tr><td colspan='3' class='forum_post_title'><b>$ft_name</b></td></tr>";
			echo "<tr><td class='forum_legend'><b>"._AUTHOR_."</b></td><td colspan='2' class='forum_legend'><b>"._TEXT_."</b></td></tr>";
			
			$result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date,DATE_FORMAT(edited, '".$siteConfig['timestamp']."') as edited2 FROM memht_forum_posts WHERE id='$threadid' OR parent='$threadid' ORDER BY id LIMIT $ofsbgn,$ofsppg");
			foreach ($result as $row) {
				$ot_id = intval($row['id']);
				$ot_name = outCode($row['name'],0);
				$ot_text = bb2html(outCode($row['text'],0),0);
				$ot_author = outCode($row['author']);
				$ot_date = $row['date'];
				$ot_edited_o = $row['edited'];
				$ot_edited = $row['edited2'];
				$ot_reason = outCode($row['reason'],0);
				$ot_ip = outCode($row['ip']);
				$ot_attachment = outCode($row['attachment']);
				$ot_smiles = intval($row['smiles']);
				$ot_status = intval($row['status']);
				
				if ($ot_smiles==1) { $ot_text = bb2smile($ot_text); }
				if ($ot_edited_o=="0000-00-00 00:00:00") { $ot_edited = ""; }
				
				$lastp = $ot_id; /*Last post cookie*/
				
				echo postTemplate($ot_id,$ot_name,$ot_text,$ot_author,$ot_date,$ot_edited,$ot_reason,$ot_ip,$ot_attachment,$ot_status,$maxcontr);
			}
			
			//3.7.5
			addVisitedPostCookie($lastp);
			
			echo "</table>\n";

			if (getStatus($ft_forum,"forum")==1 AND getStatus($threadid,"thread")==1) {
				echo "<div align='right' style='margin:4px 0;'><a href='index.php?page=forum&op=reply&id=$threadid' title='"._REPLY_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/reply.gif' border='0' alt='Reply'></a></div>";
			} else {
				echo "<div align='right' style='margin:4px 0;'><img src='templates/".$siteConfig['template']."/images/forum/buttons/locked.gif' border='0' alt='Locked'></a></div>";
			}
			
			//---------------------------------------
			if (isUser($userid)) {
				if (isAuthorized($ft_forum,"view") AND isAuthorized($ft_forum,"read") AND isAuthorized($ft_forum,"write")) {
					if (getStatus($ft_forum,"forum")==1 AND getStatus($threadid,"thread")==1) {
						echo "<form name='quick_reply' method='post' action='index.php?page=forum&op=reply&id=$threadid'>\n"; 
							echo "<div align='center'><table class='box'>";
							echo "<tr><td align='center'><textarea name='text' rows='62' cols='25' style='width:300px; height:100px;'></textarea></td></tr>\n";
							echo "<tr><td align='center'><input type='submit' name='Send' value='"._QUICK_REPLY_."'></td></tr>\n";
							echo "</table></div>\n";
							
							echo "<input type='hidden' name='smiles' value='1'>";
							echo "<input type='hidden' name='operation' value='send'>";
							echo "<input type='hidden' name='name' value='Re: $ft_name'>";
							echo "<input type='hidden' name='ok' value='true'>";
						echo "</form>\n";
					}
				}
			}
			//---------------------------------------
			
			if (isModerator($ft_forum,$userid)) {
				echo "<br><form name='f_vt_mod' method='post' action='index.php?page=forum&op=moderate&id=$threadid'>\n";
					echo "<select name='operation'>\n";
						echo "<option value='' selected>-- "._SELECT_." --</option>\n";
						echo "<option value='lock'>"._LOCK_."</option>\n";
						echo "<option value='sticky'>"._STICKY_."</option>\n";
						echo "<option value='move'>"._MOVE_."</option>\n";
						echo "<option value='delete'>"._DELETE_."</option>\n";
					echo "</select>\n";
					echo " <input type='submit' name='Submit' value='"._MODERATE_."'>\n";
				echo "</form>\n";
			}
			
			//Pages
			include_once("inc/class/paginationSystem.class.php");
			$ps = new paginationSystem();
			$ps->items = $ofsppg;
			$ps->actpg = $pg;
			$ps->query = "SELECT * FROM memht_forum_posts WHERE id=$threadid OR parent=$threadid";
			$ps->url = "index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($ft_name)."&pg={{N}}";
			$ps->show();
			
			//Tags
			include_once("inc/class/tags.class.php");
			$tg = new tags();
			$tg->whr = 8;
			$tg->wid = $threadid;
			$tg->show();
			
			echo "<div><br><b>".getTitleByThread($threadid)."</b></div>";
		} else {
			echo "<div align='center' id='errorText'><b>"._YOUNOPERMTOACCESSTHISFORUM_."</b></div>\n";
			if (!isUser($userid)) {
				echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>\n";
				@session_start();
				$_SESSION['redirect_url'] = "index.php?page=forum&op=viewThread&id=$threadid";
				$_SESSION['redirect_age'] = time();
			}
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._REQPOSTDONOTEXIST_."</b></div>\n";
	}
}

function newThread($forumid,$gname,$gemail,$name,$text,$tags,$smiles,$notification,$operation,$ok=false) {
	global $dblink,$siteConfig,$visitorInfo,$userid,$userInfo,$tzNOW;

	$forumid = intval($forumid);
	$smiles = intval($smiles);
	$notification = intval($notification);
	
	if (isAuthorized($forumid,"write")) {
		if (getStatus($forumid,"forum")==1) {
			echo "<div class='small'><b>".getTitleByForum($forumid)."</b><br><br></div>";

			echo "<form name='forum_reply' method='post' action='index.php?page=forum&op=newThread&id=$forumid' enctype='multipart/form-data'>\n";
			echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";

			if (!$ok) {
				echo "<tr><td colspan='3' class='forum_post_title'><b>"._NEWTHREAD_."</b></td></tr>";
				echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
				if (!isUser($userid)) {
					echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._NAME_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gname' style='width:50%;' maxlength='255'></td></tr>\n";
					echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._EMAIL_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gemail' style='width:50%;' maxlength='255'></td></tr>\n";
				} 			
				echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._TITLE_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='name' style='width:95%;' maxlength='255'></td></tr>\n";
				echo "<tr><td class='forum_post' valign='top'><b>"._MESSAGE_."</b><div align='center'>";
					smileBox('forum_reply','text');
				echo "</div></td><td width='80%' class='forum_post' valign='top'>";
					bbcodeToolbar('forum_reply','text');
				echo "<textarea name='text' cols='62' rows='25' style='width:95%; height:200px;'></textarea></td></tr>\n";
				
				echo "<tr><td class='forum_post'><b>"._TAGS_."</b></td><td class='forum_post'><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
				
				if (isUser($userid) AND $siteConfig['forumattachment']) {
					echo "<tr><td class='forum_post'><b>"._ATTACHMENT_."</b></td><td class='forum_post'>";
						echo "<input type='file' name='attachment' size='25'>\n";
					echo "</td></tr>\n";
				}
?>
				<tr>
					<td class="forum_post"><b><?php echo _SMILES_ ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_ ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_ ?>]"></td>
					<td class="forum_post"><input name="smiles" type="radio" value="1" checked="checked"><?php echo _ON2_ ?> <input name="smiles" type="radio" value="0"><?php echo _OFF_ ?></td>
				</tr>
<?php
				if (isUser($userid)) {
					echo "<tr><td class='forum_post'><b>"._NOTIFICATION_."</b></td>";
?>
				<td class='forum_post'><input name="notification" type="radio" value="1"><?php echo _YES_ ?> <input name="notification" type="radio" value="0" checked="checked"><?php echo _NO_ ?></td>
<?php
					echo "</tr>\n";
				} else {
					echo "<input type='hidden' name='notification' value='0'>";
				}

				echo "<tr><td colspan='3' class='forum_post' align='center' valign='top'>";
					echo '<input type="submit" name="Preview" value="'._PREVIEW_.'"> <input type="submit" name="Send" value="'._SEND_.'">';
				echo "</td></tr>\n";

				echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;<input type='hidden' name='ok' value='true'></td></tr>\n";
			} else {
					if (isset($_POST['Send'])) {
						$save = true;
						if (!isUser($userid)) {
							if ($gname=="" OR !validate($gname)) { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._NAME_; }
							if (!validEmail($gemail)) { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._EMAIL_; }
						} else {
							if ($name=="") { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._NAME_; }
							if ($text=="") { $save = false; $msg = "<b>"._EMPTY_."</b>: "._TEXT_; }
						}
						
						//-----------------------------
						require_once("inc/class/uploadFile.class.php");
						$upload = new uploadFile();
						$upload->show_errors = false;
						$upload->path = "pages/forum/".$siteConfig['forumattachmentfolder']."/";
						$upload->field = "attachment";

						if ($attachment = $upload->upload()) {
							$attachmentmime = $upload->getMime(); 		
						} else {
							if ($upload->selected) {
								$save = false;
								$msg = $upload->error;
							} else {
								$attachment = "";
								$attachmentmime = "";
							}
						}
						//-----------------------------

						if ($save) {
							if (!isUser($userid)) {
								$add = $dblink->query("INSERT INTO memht_forum_posts (id,parent,forum,name,text,author,email,guest,date,ip,smiles)
													VALUES (null,'0','$forumid','$name','$text','$gname','$gemail','1',$tzNOW,'".$visitorInfo['ip']."','$smiles')");
							} else {
								$add = $dblink->query("INSERT INTO memht_forum_posts (id,parent,forum,name,text,author,date,ip,attachment,attachmentmime,smiles)
													VALUES (null,'0','$forumid','$name','$text','".$userInfo['user']."',$tzNOW,'".$visitorInfo['ip']."','$attachment','$attachmentmime','$smiles')");
								//3.9.0
								$dblink->query("UPDATE memht_utenti SET contributes=contributes+1 WHERE id=$userid");
								addToGroups(2);
							}

							if ($add) {
								$row = $dblink->get_row("SELECT id FROM memht_forum_posts ORDER BY id DESC LIMIT 1");
								$newthreadid = intval($row['id']);

								//3.7.5
								$dblink->query("UPDATE memht_forum_posts SET lastchild='$newthreadid' WHERE id=$newthreadid");

								if ($notification==1) { $dblink->query("INSERT INTO memht_forum_notify (id,threadid,user) VALUES (null,'$newthreadid','".$userInfo['user']."')"); }

								$dblink->query("UPDATE memht_utenti SET post=post+1 WHERE id=$userid");
								addToGroups(1);
								
								//3.9.5
								if ($tags!="") {
									$tags = explode(",",$tags);
									foreach ($tags as $tag) {
										$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$newthreadid','8')");
									}
								}
								
								echo "<tr><td colspan='3' class='forum_post' align='center'><b>"._THREADADDED_."</b><br><br>"._PLSWAITWHILEREDIRECTINGTOTHEPOST_."</td></tr>\n";
								echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$newthreadid'>";
							} else {
								echo "<tr><td colspan='3' class='forum_post' align='center' id='errorText'><b>"._POSTINGERROR_."</b></td></tr>\n";
							}
						} else {
							if (!isset($msg)) { $msg = ""; }
							echo "<tr><td colspan='3' class='forum_post' align='center' id='errorText'>$msg</td></tr>\n";
						}
					}

				if (isset($_POST['Preview'])) {
						if (!isUser($userid)) {
							$gname = outCode($gname);
							$gemail = outCode($gemail);
						}
						$name = outCode($name,0);
						$otext = outCode($text,0);
						$text = bb2html($otext,0);
						$tags = outCode($tags,0);
						if ($smiles==1) { $text = bb2smile($text); }

						echo "<tr><td colspan='3' class='forum_post_title'><b>"._NEWTHREAD_."</b>: $name</td></tr>";
						echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
							echo "<tr><td colspan='3' class='forum_post'>$text</td></tr>\n";
						echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";

						if (!isUser($userid)) {
							echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._NAME_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gname' value=\"$gname\" style='width:50%;' maxlength='255'></td></tr>\n";
							echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._EMAIL_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gemail' value=\"$gemail\" style='width:50%;' maxlength='255'></td></tr>\n";
						}
						echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._TITLE_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='name' value=\"$name\" style='width:95%;' maxlength='255'></td></tr>\n";
						echo "<tr><td class='forum_post' valign='top'><b>"._MESSAGE_."</b><div align='center'>";
							smileBox('forum_reply','text');
						echo "</div></td><td width='80%' class='forum_post' valign='top'>";
							bbcodeToolbar('forum_reply','text');
						echo "<textarea name='text' cols='62' rows='25' style='width:95%; height:200px;'>$otext</textarea></td></tr>\n";
						
						echo "<tr><td class='forum_post'><b>"._TAGS_."</b></td><td class='forum_post'><input type='text' name='tags' value=\"$tags\" size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";

						if (isUser($userid) AND $siteConfig['forumattachment']) {
							echo "<tr><td class='forum_post'><b>"._ATTACHMENT_."</b></td><td class='forum_post'>";
								echo "<input type='file' name='attachment' size='25' disabled>\n";
							echo "</td></tr>\n";
						}

?>
				<tr>
					<td class="forum_post"><b><?php echo _SMILES_ ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_ ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_ ?>]"></td>
					<td class="forum_post"><input name="smiles" type="radio" value="1"<?php if ($smiles==1) { echo ' checked="checked"'; } ?>><?php echo _ON2_ ?> <input name="smiles" type="radio" value="0"<?php if ($smiles==0) { echo ' checked="checked"'; } ?>><?php echo _OFF_ ?></td>
				</tr>
<?php

						if (isUser($userid)) {
							echo "<tr><td class='forum_post'><b>"._NOTIFICATION_."</b></td><td class='forum_post'>";
?>
				<input name="notification" type="radio" value="1" <?php if ($notification==1) { echo 'checked="checked"'; } ?> ><?php echo _YES_ ?> <input name="notification" type="radio" value="0" <?php if ($notification==0) { echo 'checked="checked"'; }?>><?php echo _NO_ ?>
<?php

							echo "</td></tr>\n";
						} else {
							echo "<input type='hidden' name='notification' value='0'>";
						}

						echo "<tr><td colspan='3' class='forum_post' align='center' valign='top'>";
					echo '<input type="submit" name="Preview" value="'._PREVIEW_.'"> <input type="submit" name="Send" value="'._SEND_.'">';
						echo "</td></tr>\n";

						echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;<input type='hidden' name='ok' value='true'></td></tr>\n";
					}
			}

			echo "</table>\n";
			echo "</form>\n";
		} else {
			echo "<div align='center' id='errorText'><b>"._FORUMORTHREADLOCKED_."</b></div>\n";
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._YOUNOPERMTOWRITEINFORUM_."</b></div>\n";
		if (!isUser($userid)) {
			echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>\n";
			@session_start();
			$_SESSION['redirect_url'] = "index.php?page=forum&op=newThread&id=$forumid";
			$_SESSION['redirect_age'] = time();
		}
	}
}

function reply($threadid,$gname,$gemail,$name,$text,$quote,$smiles,$notification,$operation,$ok=false) {
	global $dblink,$siteConfig,$visitorInfo,$userid,$userInfo,$tzNOW;

	$threadid = intval($threadid);
	$quote = intval($quote);
	$smiles = intval($smiles);
	$notification = intval($notification);

	if ($row = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$threadid'")) {
		$ft_forum = intval($row['forum']);
		$ft_name = outCode($row['name'],0);
		$ft_status = intval($row['status']);

		if (isAuthorized($ft_forum,"write")) {
			if (getStatus($ft_forum,"forum")==1 AND getStatus($threadid,"thread")==1) {
				if ($quote!=0) {
					$quoterow = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$quote'");
					$quoteauthor = outCode($quoterow['author']);
					$quotetxt = outCode($quoterow['text'],0);
					$quotetxt = "[quote][b]$quoteauthor:[/b]\n".$quotetxt."[/quote]";
				} else { $quotetxt = ""; }

				echo "<div class='small'><b>".getTitleByThread($threadid)."</b><br><br></div>";

				echo "<form name='forum_reply' method='post' action='index.php?page=forum&op=reply&id=$threadid' enctype='multipart/form-data'>\n";
				echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";

				if (!$ok) {
					echo "<tr><td colspan='3' class='forum_post_title'><b>$ft_name</b></td></tr>";
					echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";

					if (!isUser($userid)) {
						echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._NAME_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gname' style='width:50%;' maxlength='255'></td></tr>\n";
						echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._EMAIL_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gemail' style='width:50%;' maxlength='255'></td></tr>\n";
					}
					echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._TITLE_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='name' value=\"Re: $ft_name\" style='width:95%;' maxlength='255'></td></tr>\n";
					echo "<tr><td class='forum_post' valign='top'><b>"._MESSAGE_."</b><div align='center'>";
						smileBox('forum_reply','text');
					echo "</div></td><td width='80%' class='forum_post' valign='top'>";
						bbcodeToolbar('forum_reply','text');
					echo "<textarea name='text' cols='62' rows='25' style='width:95%; height:200px;'>$quotetxt</textarea></td></tr>\n";

					if (isUser($userid) AND $siteConfig['forumattachment']) {
						echo "<tr><td class='forum_post'><b>"._ATTACHMENT_."</b></td><td class='forum_post'>";
							echo "<input type='file' name='attachment' size='25'>\n";
						echo "</td></tr>\n";
					}

?>
				<tr>
					<td class="forum_post"><b><?php echo _SMILES_ ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_ ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_ ?>]"></td>
					<td class="forum_post"><input name="smiles" type="radio" value="1" checked="checked"><?php echo _ON2_ ?> <input name="smiles" type="radio" value="0"><?php echo _OFF_ ?></td>
				</tr>
<?php

					if (isUser($userid)) {
						echo "<tr><td class='forum_post'><b>"._NOTIFICATION_."</b></td><td class='forum_post'>";
?>
				<input name="notification" type="radio" value="1"><?php echo _YES_ ?> <input name="notification" type="radio" value="0" checked="checked"><?php echo _NO_ ?>
<?php
						echo "</td></tr>\n";
					} else {
						echo "<input type='hidden' name='notification' value='0'>";
					}
					
					echo "<tr><td colspan='3' class='forum_post' align='center' valign='top'>";
					echo '<input type="submit" name="Preview" value="'._PREVIEW_.'"> <input type="submit" name="Send" value="'._SEND_.'">';
					echo "<input type='hidden' name='ok' value='true'>\n";
					echo "</td></tr>\n";
					
					//4.0.0
					echo "<tr><td colspan='3'>&nbsp;</td></tr>\n";
					echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
					$result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date,DATE_FORMAT(edited, '".$siteConfig['timestamp']."') as edited2 FROM memht_forum_posts WHERE id='$threadid' OR parent='$threadid' ORDER BY id DESC LIMIT 5");
					foreach ($result as $row) {
						$ot_id = intval($row['id']);
						$ot_name = outCode($row['name'],0);
						$ot_text = bb2html(outCode($row['text'],0),0);
						$ot_author = outCode($row['author']);
						$ot_date = $row['date'];
						$ot_edited_o = $row['edited'];
						$ot_edited = $row['edited2'];
						$ot_reason = outCode($row['reason'],0);
						$ot_ip = outCode($row['ip']);
						$ot_attachment = outCode($row['attachment']);
						$ot_smiles = intval($row['smiles']);
						$ot_status = intval($row['status']);
						
						if ($ot_smiles==1) { $ot_text = bb2smile($ot_text); }
						if ($ot_edited_o=="0000-00-00 00:00:00") { $ot_edited = ""; }
						
						$lastp = $ot_id; /*Last post cookie*/
						
						echo postTemplateSimple($ot_id,$ot_name,$ot_text,$ot_author,$ot_date,$ot_edited,$ot_reason,$ot_ip,$ot_attachment,$ot_status);
					}
				} else {
						if (isset($_POST['Send'])) {
							$save = true;
							if (!isUser($userid)) {
								if ($gname=="" OR !validate($gname)) { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._NAME_; }
								if (!validEmail($gemail)) { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._EMAIL_; }
							} else {
								if ($name=="") { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._NAME_; }
								if ($text=="") { $save = false; $msg = "<b>"._EMPTY_."</b>: "._TEXT_; }
							}
							
							//-----------------------------
							require_once("inc/class/uploadFile.class.php");
							$upload = new uploadFile();
							$upload->show_errors = false;
							$upload->path = "pages/forum/".$siteConfig['forumattachmentfolder']."/";
							$upload->field = "attachment";
							
							if ($attachment = $upload->upload()) {
								$attachmentmime = $upload->getMime(); 		
							} else {
								if ($upload->selected) {
									$save = false;
									$msg = $upload->error;
								} else {
									$attachment = "";
									$attachmentmime = "";
								}
							}
							//-----------------------------
								
							if ($save) {
								if (!isUser($userid)) {
									$add = $dblink->query("INSERT INTO memht_forum_posts (id,parent,forum,name,text,author,email,guest,date,ip,smiles)
														VALUES (null,'$threadid','$ft_forum','$name','$text','$gname','$gemail','1',$tzNOW,'".$visitorInfo['ip']."','$smiles')");
								} else {
									$add = $dblink->query("INSERT INTO memht_forum_posts (id,parent,forum,name,text,author,date,ip,attachment,attachmentmime,smiles)
														VALUES (null,'$threadid','$ft_forum','$name','$text','".$userInfo['user']."',$tzNOW,'".$visitorInfo['ip']."','$attachment','$attachmentmime','$smiles')");
									//3.9.0
									$dblink->query("UPDATE memht_utenti SET contributes=contributes+1 WHERE id=$userid");
									addToGroups(2);
								}

								if ($add) {
									$row = $dblink->get_row("SELECT id FROM memht_forum_posts ORDER BY id DESC LIMIT 1");
									$lastid = intval($row['id']);
									
									//3.7.5
									$dblink->query("UPDATE memht_forum_posts SET lastchild='$lastid' WHERE id=$threadid");

									$dblink->query("UPDATE memht_utenti SET post=post+1 WHERE id=$userid");
									addToGroups(1);
									echo "<tr><td colspan='3' class='forum_post' align='center'><b>"._REPLYADDED_."</b><br><br>"._PLSWAITWHILEREDIRECTINGTOTHEPOST_."</td></tr>\n";
									
									$throw = $dblink->get_row("SELECT parent,name FROM memht_forum_posts WHERE id=$lastid");
									$thname = outCode($throw['name']);
									$threadid = intval($throw['parent']);
									
									if ($notification==1 AND $dblink->get_num("SELECT id FROM memht_forum_notify WHERE threadid='$threadid' AND user='".$userInfo['user']."'")==0) {
										$dblink->query("INSERT INTO memht_forum_notify (id,threadid,user) VALUES (null,'$threadid','".$userInfo['user']."')");
									}
									
									$pstnum = $dblink->get_num("SELECT id FROM memht_forum_posts WHERE id=$threadid OR parent=$threadid");
									if ($pstnum>10) { $plus = "&pg=".ceil($pstnum/10); } else { $plus = ""; }
									
									//Send mail notification (added in 3.2)
									$result = $dblink->get_list("SELECT * FROM memht_forum_posts AS mp JOIN memht_utenti AS mu ON mp.author=mu.user WHERE (mp.id='$threadid' OR mp.parent='$threadid') AND mp.guest=0 GROUP BY user");
									foreach ($result as $row) {
										$nuser = outCode($row['user']);
										$nemail = outCode($row['email']);
										$nforum = intval($row['notify_forum']);
										
										if (($nforum==1 OR $nr = $dblink->get_row("SELECT id FROM memht_forum_notify WHERE threadid='$threadid' AND user='$nuser'")) AND $nuser!=$userInfo['user']) {
											$subject = "There are new replies on your forum post on ".$siteConfig['site_name'];
											$msg = "There are new replies on your forum post on ".$siteConfig['site_name']."!\n\n";
											$msg .= "Link: ".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($name)."$plus#post$lastid\n\n";
											$msg .= "You're receiving this email because you have chosen to receive the notification of new replyes on your forum post on ".$siteConfig['site_name'].". If you don't want to receive it anymore, ";
											if (isset($nr['id'])) {
												$msg .= "disable the single thread notification from here:\n".$siteConfig['site_url']."/index.php?page=forum&op=notifyoff&id=".intval($nr['id'])."&user=$nuser&threadid=$threadid";
											} else {
												$msg .= "disable the forum notification in your account profile:\n".$siteConfig['site_url']."/index.php?page=users";
											}
										
											sendEmail($nemail,$subject,$msg,$siteConfig['site_name'],$siteConfig['webmaster_mail'],0);
										}
									}
									echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($thname)."$plus#post$lastid'>";
								} else {
									echo "<tr><td colspan='3' class='forum_post' align='center' id='errorText'><b>"._POSTINGERROR_."</b></td></tr>\n";
								}
							} else {
								echo "<tr><td colspan='3' class='forum_post' align='center' id='errorText'>$msg</td></tr>\n";
							}
						}
						
						if (isset($_POST['Preview'])) {
							if (!isUser($userid)) {
								$gname = outCode($gname);
								$gemail = outCode($gemail);
							}
							$name = outCode($name,0);
							$otext = outCode($text,0);
							$text = bb2html($otext,0);
							if ($smiles==1) { $text = bb2smile($text); }
							
							echo "<tr><td colspan='3' class='forum_post_title'><b>$ft_name</b></td></tr>";
							echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
								echo "<tr><td colspan='3' class='forum_post'>$text</td></tr>\n";
							echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
							
							if (!isUser($userid)) {
								echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._NAME_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gname' value=\"$gname\" style='width:50%;' maxlength='255'></td></tr>\n";
								echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._EMAIL_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='gemail' value=\"$gemail\" style='width:50%;' maxlength='255'></td></tr>\n";
							}
							echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._TITLE_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='name' value=\"$name\" style='width:95%;' maxlength='255'></td></tr>\n";
							echo "<tr><td class='forum_post' valign='top'><b>"._MESSAGE_."</b><div align='center'>";
								smileBox('forum_reply','text');
							echo "</div></td><td width='80%' class='forum_post' valign='top'>";
								bbcodeToolbar('forum_reply','text');
							echo "<textarea name='text' cols='62' rows='25' style='width:95%; height:200px;'>$otext</textarea></td></tr>\n";
							
							if (isUser($userid) AND $siteConfig['forumattachment']) {
								echo "<tr><td class='forum_post'><b>"._ATTACHMENT_."</b></td><td class='forum_post'>";
									echo "<input type='file' name='attachment' size='25' disabled>\n";
								echo "</td></tr>\n";
							}

?>
				<tr>
					<td class="forum_post"><b><?php echo _SMILES_ ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_ ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_ ?>]"></td>
					<td class="forum_post"><input name="smiles" type="radio" value="1"<?php if ($smiles==1) { echo ' checked="checked"'; } ?>><?php echo _ON2_ ?> <input name="smiles" type="radio" value="0"<?php if ($smiles==0) { echo ' checked="checked"'; } ?>><?php echo _OFF_ ?></td>
				</tr>
<?php
							
							if (isUser($userid)) {
								echo "<tr><td class='forum_post'><b>"._NOTIFICATION_."</b></td><td class='forum_post'>";
?>
				<input name="notification" type="radio" value="1" <?php if ($notification==1) { echo 'checked="checked"'; } ?> ><?php echo _YES_ ?> <input name="notification" type="radio" value="0" <?php if ($notification==0) { echo 'checked="checked"'; }?>><?php echo _NO_ ?>
<?php

								echo "</td></tr>\n";
							} else {
								echo "<input type='hidden' name='notification' value='0'>";
							}
							
							echo "<tr><td colspan='3' class='forum_post' align='center' valign='top'>";
							echo '<input type="submit" name="Preview" value="'._PREVIEW_.'"> <input type="submit" name="Send" value="'._SEND_.'">';
							echo "<input type='hidden' name='ok' value='true'>\n";
							echo "</td></tr>\n";
							
							//4.0.0
							echo "<tr><td colspan='3'>&nbsp;</td></tr>\n";
							echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
							$result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date,DATE_FORMAT(edited, '".$siteConfig['timestamp']."') as edited2 FROM memht_forum_posts WHERE id='$threadid' OR parent='$threadid' ORDER BY id DESC LIMIT 5");
							foreach ($result as $row) {
								$ot_id = intval($row['id']);
								$ot_name = outCode($row['name'],0);
								$ot_text = bb2html(outCode($row['text'],0),0);
								$ot_author = outCode($row['author']);
								$ot_date = $row['date'];
								$ot_edited_o = $row['edited'];
								$ot_edited = $row['edited2'];
								$ot_reason = outCode($row['reason'],0);
								$ot_ip = outCode($row['ip']);
								$ot_attachment = outCode($row['attachment']);
								$ot_smiles = intval($row['smiles']);
								$ot_status = intval($row['status']);
								
								if ($ot_smiles==1) { $ot_text = bb2smile($ot_text); }
								if ($ot_edited_o=="0000-00-00 00:00:00") { $ot_edited = ""; }
								
								$lastp = $ot_id; /*Last post cookie*/
								
								echo postTemplateSimple($ot_id,$ot_name,$ot_text,$ot_author,$ot_date,$ot_edited,$ot_reason,$ot_ip,$ot_attachment,$ot_status);
							}
						}
				}
				
				echo "</table>\n";
				echo "</form>\n";
			} else {
				echo "<div align='center' id='errorText'><b>"._FORUMORTHREADLOCKED_."</b></div>\n";
			}
		} else {
			echo "<div align='center' id='errorText'><b>"._YOUNOPERMTOWRITEINFORUM_."</b></div>";
			if (!isUser($userid)) {
				echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>\n";
				@session_start();
				$redurl = "index.php?page=forum&op=reply&id=$threadid";
				if ($quote>0) { $redurl .= "&quote=$quote"; }
				$_SESSION['redirect_url'] = $redurl;
				$_SESSION['redirect_age'] = time();
			}
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._REQPOSTDONOTEXIST_."</b></div>\n";
	}
}

function editPost($postid,$name,$text,$tags,$reason,$smiles,$operation,$ok=false) {
	global $dblink,$siteConfig,$userid,$userInfo,$tzNOW;

	$postid = intval($postid);
	$smiles = intval($smiles);
	
	$row = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$postid'");
	$ft_parent = intval($row['parent']);
	$ft_forum = intval($row['forum']);
	$ft_name = outCode($row['name'],0);
	$ft_text = outCode($row['text'],0);
	$ft_author = outCode($row['author']);
	$ft_guest = intval($row['guest']);
	$ft_attachment = outCode($row['attachment']);
	$ft_attachmentmime = outCode($row['attachmentmime']);
	$ft_smiles = intval($row['smiles']);
	$ft_status = intval($row['status']);
	
	if ($ft_parent==0) { $threadid = $postid; } else { $threadid = $ft_parent; }
	
	if (isAuthorized($ft_forum,"view") AND isAuthorized($ft_forum,"read") AND isAuthorized($ft_forum,"write")) {
		if ($userInfo['user']==$ft_author AND isUser($userid) AND $ft_guest==0 OR isModerator($ft_forum,$userid)) {
			if ((getStatus($ft_forum,"forum")==1 AND getStatus($threadid,"thread")==1) OR isModerator($ft_forum,$userid)) {
				if ($name!="") { $ft_name = $name; }
				if ($text!="") { $ft_text = $text; }
					
				echo "<div class='small'><b>".getTitleByThread($postid)."</b></div><br><br>";
				
				echo "<table width='100%' border='0' cellspacing='1' cellpadding='2'>\n";
				
				echo "<form name='forum_reply' method='post' action='index.php?page=forum&op=editPost&id=$postid' enctype='multipart/form-data'>\n";
				
				if (!$ok) {
					if ($ft_parent==0) {
						if ($result = $dblink->get_list("SELECT tag FROM memht_tags WHERE whr=8 AND cid='$postid'")) {
							$tags = array();
							foreach ($result as $row) {
								$tags[] = outCode($row['tag']);
							}
							$tags = implode(",",$tags);
						} else {
							$tags = "";
						}
					}
					
					echo "<tr><td colspan='3' class='forum_post_title'><b>$ft_name</b></td></tr>";
					echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
						
					echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._TITLE_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='name' value=\"$ft_name\" style='width:95%;' maxlength='255'></td></tr>\n";
					echo "<tr><td class='forum_post' valign='top'><b>"._MESSAGE_."</b><div align='center'>";
						smileBox('forum_reply','text');
					echo "</div></td><td width='80%' class='forum_post' valign='top'>";
						bbcodeToolbar('forum_reply','text');
					echo "<textarea name='text' cols='62' rows='25' style='width:95%; height:200px;'>$ft_text</textarea></td></tr>\n";
					if ($ft_parent==0) {
						echo "<tr><td class='forum_post'><b>"._TAGS_."</b></td><td class='forum_post'><input type='text' name='tags' value=\"$tags\" size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
					}
					
					if (isUser($userid) AND $siteConfig['forumattachment']) {
						echo "<tr><td class='forum_post'><b>"._ATTACHMENT_."</b></td><td class='forum_post'>";
							if ($ft_attachment!="") {
								echo "<div class='forum_post_edited'><b>"._ATTACHMENT_.":</b> <a href='pages/forum/attachment.php?id=$postid' target='_blank'>$ft_attachment</a></div>";
							}
							echo "<input type='file' name='attachment' size='25'>\n";
						echo "</td></tr>\n";
					}

?>
				<tr>
					<td class="forum_post"><b><?php echo _SMILES_ ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_ ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_ ?>]"></td>
					<td class="forum_post"><input name="smiles" type="radio" value="1"<?php if ($ft_smiles==1) { echo ' checked="checked"'; } ?>><?php echo _ON2_ ?> <input name="smiles" type="radio" value="0"<?php if ($ft_smiles==0) { echo ' checked="checked"'; } ?>><?php echo _OFF_ ?></td>
				</tr>
<?php
					
					echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._EDITREASON_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='reason' style='width:95%;' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='3' class='forum_post' align='center' valign='top'>";
						echo '<input type="submit" name="delete" value="'._DELETE_.'"> <input type="submit" name="Preview" value="'._PREVIEW_.'"> <input type="submit" name="Send" value="'._SEND_.'">';
					echo "</td></tr>\n";
						
					echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;<input type='hidden' name='ok' value='true'></td></tr>\n";
				} else {
						if (isset($_POST['Send'])) {
							$save = true;
							if ($text=="") { $save = false; $msg = "<b>"._EMPTY_."</b>: "._TEXT_; }
							
							//-----------------------------
							require_once("inc/class/uploadFile.class.php");
							$upload = new uploadFile();
							$upload->show_errors = false;
							$upload->path = "pages/forum/".$siteConfig['forumattachmentfolder']."/";
							$upload->field = "attachment";
							
							if ($attachment = $upload->upload()) {
								$attachmentmime = $upload->getMime();
								@unlink($upload->path.$ft_attachment);			
							} else {
								if ($upload->selected) {
									$save = false;
									$msg = $upload->error;
								} else {
									$attachment = $ft_attachment;
									$attachmentmime = $ft_attachmentmime;
								}
							}
							//-----------------------------
								
							if ($save) {
								if ($ft_author!=$userInfo['user']) { $reason .= " (Edited by ".$userInfo['user'].")"; }
								$edit = $dblink->query("UPDATE memht_forum_posts SET name='$name',text='$text',edited=$tzNOW,reason='$reason',attachment='$attachment',attachmentmime='$attachmentmime',smiles='$smiles' WHERE id='$postid'");
								if ($ft_parent==0) {
									$dblink->query("DELETE FROM memht_tags WHERE whr=8 AND cid='$postid'");
									
									if ($tags!="") {
										$tags = explode(",",$tags);
										foreach ($tags as $tag) {
											$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$postid','8')");
										}
									}
								}
								
								if ($edit) {	
									$throw = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$postid'");
									$threadid = intval($throw['parent']);
									if ($threadid==0) { $threadid = $postid; }
												
									echo "<tr><td colspan='3' class='forum_post' align='center'><b>"._POSTEDITED_."</b><br><br>"._PLSWAITWHILEREDIRECTINGTOTHEPOST_."</td></tr>\n";
									
									$pstnum = $dblink->get_num("SELECT * FROM memht_forum_posts WHERE id='$threadid' OR (parent='$threadid' AND id<=$postid)");
									if ($pstnum>10) { $plus = "&pg=".ceil($pstnum/10); } else { $plus = ""; } 							
									
									echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid$plus#post$postid'>";
								} else {
									echo "<tr><td colspan='3' class='forum_post' align='center' id='errorText'><b>"._POSTINGERROR_."</b></td></tr>\n";
								}
							} else {
								echo "<tr><td colspan='3' class='forum_post' align='center' id='errorText'>$msg</td></tr>\n";
							}
						}
						
						if (isset($_POST['Preview'])) {
							$name = outCode($name,0);
							$otext = outCode($text,0);
							$text = bb2html($otext,0);
							if ($smiles==1) { $text = bb2smile($text); }
							
							echo "<tr><td colspan='3' class='forum_post_title'><b>$ft_name</b></td></tr>";
							echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
								echo "<tr><td colspan='3' class='forum_post'>$text</td></tr>\n";
							echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
							
								
							echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._TITLE_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='name' value=\"$name\" style='width:95%;' maxlength='255'></td></tr>\n";
							echo "<tr><td class='forum_post' valign='top'><b>"._MESSAGE_."</b><div align='center'>";
								smileBox('forum_reply','text');
							echo "</div></td><td width='80%' class='forum_post' valign='top'>";
								bbcodeToolbar('forum_reply','text');
							echo "<textarea name='text' cols='62' rows='25' style='width:95%; height:200px;'>$otext</textarea></td></tr>\n";
							
							if (isUser($userid) AND $siteConfig['forumattachment']) {
								echo "<tr><td class='forum_post'><b>"._ATTACHMENT_."</b></td><td class='forum_post'>";
									echo "<input type='file' name='attachment' size='25' disabled>\n";
								echo "</td></tr>\n";
							}

?>
				<tr>
					<td class="forum_post"><b><?php echo _SMILES_ ?></b> <img src="images/info.gif" alt="Info" title="header=[<?php echo _INFORMATIONS_ ?>] body=[<?php echo _SETOFFWHENPOSTINGCODE_ ?>]"></td>
					<td class="forum_post"><input name="smiles" type="radio" value="1"<?php if ($smiles==1) { echo ' checked="checked"'; } ?>><?php echo _ON2_ ?> <input name="smiles" type="radio" value="0"<?php if ($smiles==0) { echo ' checked="checked"'; } ?>><?php echo _OFF_ ?></td>
				</tr>
<?php
							echo "</td></tr>\n";
							
							echo "<tr><td width='20%' class='forum_post' valign='top'><b>"._EDITREASON_."</b></td><td width='80%' class='forum_post' valign='top'><input type='text' name='reason' value=\"$reason\" style='width:95%;' maxlength='255'></td></tr>\n";
							echo "<tr><td colspan='3' class='forum_post' align='center' valign='top'>";
					echo '<input type="submit" name="delete" value="'._DELETE_.'"> <input type="submit" name="Preview" value="'._PREVIEW_.'"> <input type="submit" name="Send" value="'._SEND_.'">';
									if (isAuthorized($ft_forum,"delete") AND $ft_parent!=0) {
									}
							echo "</td></tr>\n";
								
							echo "<tr><td colspan='3' class='forum_spacer'>&nbsp;<input type='hidden' name='ok' value='true'></td></tr>\n";
						}
						
						if (isset($_POST['delete'])) {
							if (isAuthorized($ft_forum,"delete") AND $ft_parent!=0) {
								if (isModerator($ft_forum,$userid)) {
									$dblink->query("DELETE FROM memht_forum_posts WHERE id='$postid'");
								} else {
									$dblink->query("DELETE FROM memht_forum_posts WHERE id='$postid' AND author='".$userInfo['user']."'");
								}
								//3.8.0
								if ($row = $dblink->get_row("SELECT id FROM memht_forum_posts WHERE parent=$threadid ORDER BY id DESC LIMIT 1")) {
									$lastchild = intval($row['id']);
									$dblink->query("UPDATE memht_forum_posts SET lastchild='$lastchild' WHERE id=$threadid");
								}
								
								$path = "pages/forum/".$siteConfig['forumattachmentfolder']."/";
								@unlink($path.$ft_attachment);
								
								echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid'>"; 
							} else {
								echo "<tr><td id='errorText' class='forum_post' align='center'><b>"._YOUNOPERMTODELETETHEPOST_."</b></td></tr>\n";
							}
					}
				}
				
				echo "</form>\n";
				
				echo "</table>\n";
			} else {
				echo "<div align='center' id='errorText'><b>"._FORUMORTHREADLOCKED_."</b></div>\n";
			}
		} else {
			echo "<div align='center' id='errorText'><b>"._YOUNOPERMTOEDITTHEPOST_."</b></div>\n";
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._YOUNOPERMTOWRITEINFORUM_."</b></div>\n";
		if (!isUser($userid)) {
			echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>\n";
			@session_start();
			$_SESSION['redirect_url'] = "index.php?page=forum&op=editPost&id=$postid";
			$_SESSION['redirect_age'] = time();
		}
	}
}

function deletePost($id,$ok=false) {
	global $dblink,$siteConfig,$userid,$userInfo;
	
	$id = intval($id);
	
	$row = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id=$id");
	$ft_parent = intval($row['parent']);
	$ft_forum = intval($row['forum']);
	$ft_attachment = outCode($row['attachment']);
	
	if ($ft_parent==0) { $threadid = $id; } else { $threadid = $ft_parent; }
	
	if (isAuthorized($ft_forum,"delete") AND $ft_parent!=0) {
		if (isModerator($ft_forum,$userid)) {
			$delete = "DELETE FROM memht_forum_posts WHERE id=$id";
		} else {
			$delete = "DELETE FROM memht_forum_posts WHERE id=$id AND author='".$userInfo['user']."'";
		}
		if ($ok) {
			$dblink->query($delete);
			
			//3.8.0
			if ($row = $dblink->get_row("SELECT id FROM memht_forum_posts WHERE (parent=$threadid OR id=$threadid) AND id!=$id ORDER BY id DESC LIMIT 1")) {
				$lastchild = intval($row['id']);
				$dblink->query("UPDATE memht_forum_posts SET lastchild='$lastchild' WHERE id=$threadid");
			}

			$path = "pages/forum/".$siteConfig['forumattachmentfolder']."/";
			@unlink($path.$ft_attachment);
			echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid'>";
		} else {
			echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='index.php?page=forum&op=deletePost&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='index.php?page=forum&op=viewThread&id=$threadid' title='"._NO_."'>"._NO_."</a></div>";
		}
	} else {
		if (isModerator($ft_forum,$userid)) {
			if ($ok) {
				$dblink->query("DELETE FROM memht_forum_posts WHERE id=$threadid");
				$dblink->query("DELETE FROM memht_forum_posts WHERE parent=$threadid");
				$dblink->query("DELETE FROM memht_tags WHERE whr=8 AND cid=$threadid");
				echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewForum&id=$ft_forum'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='index.php?page=forum&op=deletePost&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='index.php?page=forum&op=viewThread&id=$threadid' title='"._NO_."'>"._NO_."</a></div>";
			}
		} else {
			echo "<div id='errorText' align='center'><b>"._YOUNOPERMTODELETETHEPOST_."</b></div>\n";
			if (!isUser($userid)) {
				echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'><b>"._LOGIN_."</b></a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'><b>"._REGISTER_."</b></a> "._FORFREE_."</div>\n";
				@session_start();
				$_SESSION['redirect_url'] = "index.php?page=forum&op=deletePost&id=$id";
				$_SESSION['redirect_age'] = time();
			}
		}
	}
}

function postTemplate($post_id,$post_name,$post_text,$post_author,$post_date,$post_edited,$post_reason,$post_ip,$post_attachment,$post_status,$maxcontr) {
	global $dblink,$siteConfig,$userid,$userInfo;
	
	$postStructure = "";
	if (memRunHooks('ViewForumPost',array(&$postStructure,$post_id,$post_name,$post_text,$post_author,$post_date,$post_edited,$post_reason,$post_ip,$post_attachment,$post_status))) {
		$throw = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$post_id'");
		$threadid = intval($throw['parent']);
		$forumid = intval($throw['forum']);
		$aip = outCode($throw['ip']);
		if ($threadid==0) {
			$threadid = $post_id;
		}
		$guest = intval($throw['guest']);
		$gemail = outCode($throw['email']);
		
		if ($guest==0) {
			$userow = $dblink->get_row("SELECT *,DATE_FORMAT(regdate, '".$siteConfig['timestamp']."') as regdate FROM memht_utenti WHERE user='$post_author'");
			$user_id = intval($userow['id']);
			$user_regdate = $userow['regdate'];
			$user_sito = outCode($userow['sito']);
			$user_icq = outCode($userow['icq']);
			$user_msn = outCode($userow['msn']);
			$user_aol = outCode($userow['aol']);
			$user_yahoo = outCode($userow['yahoo']);
			$user_avatar = outCode($userow['avatar']);
			$user_post = intval($userow['post']);
			$user_gender = intval($userow['gender']);
			$user_title_enabled = intval($userow['title_enabled']);
			$user_title_text = outCode($userow['title_text']);
			$user_signature = censure(bb2html(outCode($userow['signature'],0)));
			$user_contributes = intval($userow['contributes']);
			
			$authorinfo = "<a href='index.php?page=users&op=userInfo&usert=$post_author' title='$post_author'><b>$post_author</b></a>";
			if ($user_title_enabled==1) {
				$authorinfo .= "<br><span class='small'>$user_title_text</span>"; 
			} else {
				$authorinfo .= "<br><span class='small'>".getTitle($user_post)."</span>"; 
			}
			
			if ($user_avatar!="") { $authorinfo .= "<br><img src='images/avatar/$user_avatar' alt='Avatar'>"; }
			$authorinfo .= "<br><br><b>"._POSTS_.":</b> $user_post";
			
			$imgcontrib = ceil(@($user_contributes/$maxcontr)*9);
			if ($imgcontrib<0) { $imgcontrib = 0; }
			if ($imgcontrib>9) { $imgcontrib = 9; }
			$authorinfo .= "<br><img src='images/bars/{$imgcontrib}.gif' alt='"._CONTRIBUTES_.": $user_contributes' title='"._CONTRIBUTES_.": $user_contributes'><br>\n";
			
			if ($user_gender==1) {
				$authorinfo .= "<br><b>"._GENDER_.":</b> <img src='images/male.gif' title='"._MALE_."' alt='_MALE_'>";
			} else if ($user_gender==2) {
				$authorinfo .= "<br><b>"._GENDER_.":</b> <img src='images/female.gif' title='"._FEMALE_."' alt='_FEMALE_'>";
			} else {
				$authorinfo .= "<br><b>"._GENDER_.":</b> <img src='images/neutral.gif' title='"._NEUTRAL_."' alt='_NEUTRAL_'>";
			}
			$authorinfo .= "<br><b>"._ONLINE_.":</b> ";
			$authorinfo .= ($dblink->get_num("SELECT ip FROM memht_online WHERE user='$post_author'")>0 AND !($siteConfig['admin_ghosts'] AND $userInfo['rank']>1)) ? _YES_ : _NO_ ;
			
			//Customized profile
			//3.8.5
			if ($result = $dblink->get_list("SELECT title,content FROM memht_utenti_profile AS p JOIN memht_utenti_profile_data AS d ON p.id=d.pid WHERE p.showinforum=1 AND d.uid=$user_id")) {
				$authorinfo .= "<br>";
				foreach ($result as $row) {
					$ptitle = outCode($row['title']);
					$pcontent = outCode($row['content']);
					if ($pcontent!="") { $authorinfo .= "<br><b>{$ptitle}:</b> $pcontent"; }
				}
			}
			
			if ($user_signature!="") { $user_signature = "<div class='forum_signature'>$user_signature</div>"; }
		} else {
			$authorinfo = "<b>$post_author</b>";
			$authorinfo .= "<br><span class='small'>"._GUEST_."</span>";
			$gemail = maskEmail($gemail);
			$authorinfo .= "<br><br><img src='images/email.gif' border='0' title='$gemail' alt='Email'>";
			if (isAuth($userid,3) OR isModerator($forumid,$userid)) {
				$authorinfo .= "<br><span class='small'><b>IP:</b> $aip</span>";
			}
			$user_signature = "";
		}
		
		$plus = "";
		if ($post_attachment!="") {
			$plus .= "<div class='small box'><b>"._ATTACHMENT_.":</b> <a href='pages/forum/attachment.php?id=$post_id' target='_blank'>$post_attachment</a></div>";
		}
		
		if ($post_edited!="") {
			$plus .= "<div class='small box'><b>"._EDITED_.":</b> $post_edited";
			if ($post_reason!="") {
				$plus .= "<br><b>"._REASON_.":</b> $post_reason</div>";
			} else { $plus .= "</div>"; }
		}
		
		if ($post_name!="") { $subtitle = "<br><b>$post_name</b>"; } else { $subtitle = ""; }
	
		$postStructure .= "<tr><td width='20%' class='forum_post' valign='top'>$authorinfo</td><td width='80%' class='forum_post' valign='top'>\n";
		
			$postStructure .= "<table width='100%' cellspacing='0' cellpadding='0'>";
				$postStructure .= "<tr><td class='forum_post_minititle'>"._DATE_.": $post_date$subtitle</td><td class='forum_post_minititle' align='right' valign='top'><a name='post$post_id' id='post$post_id'>#post$post_id</a></td></tr>";
				$postStructure .= "<tr><td colspan='2'><div style='padding: 15px 0;'>$post_text</div>$plus$user_signature</td></tr>";
			$postStructure .= "</table>";
		$postStructure .= "</td></tr>\n";
		$postStructure .= "<tr><td colspan='3' class='forum_post' align='right'>";
			$postStructure .= "<a href='index.php?page=forum&op=deletePost&id=$post_id' title='"._DELETE_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/delete.gif' border='0' alt='Delete'></a> <a href='index.php?page=forum&op=editPost&id=$post_id' title='"._EDIT_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/edit.gif' border='0' alt='Edit'></a> <a href='index.php?page=forum&op=reply&id=$threadid&quote=$post_id' title='"._QUOTE_."' rel='nofollow'><img src='templates/".$siteConfig['template']."/images/forum/buttons/quote.gif' border='0' alt='Quote'></a>";
		$postStructure .= "</td></tr>\n";
		$postStructure .= "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
		
		memRunHooks('ViewForumPostEnd',array(&$postStructure,$post_id,$post_name,$post_text,$post_author,$post_date,$post_edited,$post_reason,$post_ip,$post_attachment,$post_status));
	}
	return $postStructure;
}

function postTemplateSimple($post_id,$post_name,$post_text,$post_author,$post_date,$post_edited,$post_reason,$post_ip,$post_attachment,$post_status) {
	global $dblink,$siteConfig,$userid,$userInfo;
	
	$postStructure = "";
		$throw = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$post_id'");
		$threadid = intval($throw['parent']);
		$forumid = intval($throw['forum']);
		$aip = outCode($throw['ip']);
		if ($threadid==0) {
			$threadid = $post_id;
		}
		$guest = intval($throw['guest']);
		$gemail = outCode($throw['email']);
		
		if ($guest==0) {
			$userow = $dblink->get_row("SELECT *,DATE_FORMAT(regdate, '".$siteConfig['timestamp']."') as regdate FROM memht_utenti WHERE user='$post_author'");
			$user_id = intval($userow['id']);
			$user_regdate = $userow['regdate'];
			$user_sito = outCode($userow['sito']);
			$user_icq = outCode($userow['icq']);
			$user_msn = outCode($userow['msn']);
			$user_aol = outCode($userow['aol']);
			$user_yahoo = outCode($userow['yahoo']);
			$user_avatar = outCode($userow['avatar']);
			$user_post = intval($userow['post']);
			$user_gender = intval($userow['gender']);
			$user_title_enabled = intval($userow['title_enabled']);
			$user_title_text = outCode($userow['title_text']);
			$user_signature = censure(bb2html(outCode($userow['signature'],0)));
			$user_contributes = intval($userow['contributes']);
			
			$authorinfo = "<a href='index.php?page=users&op=userInfo&usert=$post_author' title='$post_author'><b>$post_author</b></a>";
			if ($user_title_enabled==1) {
				$authorinfo .= "<br><span class='small'>$user_title_text</span>"; 
			} else {
				$authorinfo .= "<br><span class='small'>".getTitle($user_post)."</span>"; 
			}			
		} else {
			$authorinfo = "<b>$post_author</b>";
			$authorinfo .= "<br><span class='small'>"._GUEST_."</span>";
			$gemail = maskEmail($gemail);
			$authorinfo .= "<br><br><img src='images/email.gif' border='0' title='$gemail' alt='Email'>";
			if (isAuth($userid,3) OR isModerator($forumid,$userid)) {
				$authorinfo .= "<br><span class='small'><b>IP:</b> $aip</span>";
			}
			$user_signature = "";
		}
		
		$plus = "";
		
		if ($post_edited!="") {
			$plus .= "<div class='small box'><b>"._EDITED_.":</b> $post_edited";
			if ($post_reason!="") {
				$plus .= "<br><b>"._REASON_.":</b> $post_reason</div>";
			} else { $plus .= "</div>"; }
		}
		
		if ($post_name!="") { $subtitle = "<br><b>$post_name</b>"; } else { $subtitle = ""; }
	
		$postStructure .= "<tr><td width='20%' class='forum_post' valign='top'>$authorinfo</td><td width='80%' class='forum_post' valign='top'>\n";
		
			$postStructure .= "<table width='100%' cellspacing='0' cellpadding='0'>";
				$postStructure .= "<tr><td class='forum_post_minititle'>"._DATE_.": $post_date$subtitle</td><td class='forum_post_minititle' align='right' valign='top'><a name='post$post_id' id='post$post_id'>#post$post_id</a></td></tr>";
				$postStructure .= "<tr><td colspan='2'><div style='padding: 15px 0;'>$post_text</div>$plus</td></tr>";
			$postStructure .= "</table>";
		$postStructure .= "</td></tr>\n";
		$postStructure .= "<tr><td colspan='3' class='forum_spacer'>&nbsp;</td></tr>\n";
		

	return $postStructure;
}

function moderate($threadid,$operation,$ok=false) {
	global $dblink,$siteConfig,$userid,$userInfo;
	
	$threadid = intval($threadid);
	
	$row = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id=$threadid AND parent=0");
	$forum = intval($row['forum']);
	$name = outCode($row['name']);
	$author = outCode($row['author']);
	$status = intval($row['status']);
	$flag = intval($row['flag']);

	if (isModerator($forum,$userid)) {
		switch($operation) {
			case "lock":
				if ($status==0) { $newstatus = 1; } else { $newstatus = 0; }
				$dblink->query("UPDATE memht_forum_posts SET status='$newstatus' WHERE id=$threadid");
				echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid'>";
			break;
			
			case "sticky":
				if ($flag==0) { $newflag = 1; } else { $newflag = 0; }
				$dblink->query("UPDATE memht_forum_posts SET flag='$newflag' WHERE id=$threadid");
				echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid'>";
			break;
			
			case "move":
				if ($ok) {
					if (isset($_POST['destination'])) {
						$new_forum = intval($_POST['destination']);
						
						$result = $dblink->get_list("SELECT id FROM memht_forum_posts WHERE id=$threadid OR parent=$threadid ORDER BY id");
						foreach ($result as $row) {
							$fid = intval($row['id']);
							
							$dblink->query("UPDATE memht_forum_posts SET forum='$new_forum' WHERE id=$fid");
						}

						echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewThread&id=$threadid'>";
					} else {
						echo "<div id='errorText' align='center'><b>"._FIELDINVALID_."</b></div>\n";
					}
				} else {
					echo "<form name='form' method='post' action='index.php?page=forum&op=moderate&operation=move&id=$threadid&ok=true'>";
					echo "<div align='center'><select name='destination'>";
						echo "<option value='' selected>-- "._FORUM_." --</option>\n";
						$result = $dblink->get_list("SELECT id,name FROM memht_forum_forums ORDER BY name");
						foreach ($result as $row) {
							echo "<option value='".intval($row['id'])."'>".outCode($row['name'])."</option>\n";
						}
					echo "</select> <input type='submit' name='Submit' value='"._MOVE_."'></div>";
					echo "</form>";
				}
			break;
			
			case "delete":
				if ($ok) {
					$dblink->query("DELETE FROM memht_forum_posts WHERE id=$threadid");
					$dblink->query("DELETE FROM memht_forum_posts WHERE parent=$threadid");
					$dblink->query("DELETE FROM memht_forum_notify WHERE threadid='$threadid'");
					
					echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=forum&op=viewForum&id=$forum'>";
				} else {
					echo "<div align='center'><b>"._SUREDELETETHETHREAD_."</b><br><a href='index.php?page=forum&op=moderate&operation=delete&id=$threadid&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='index.php?page=forum&op=viewThread&id=$threadid' title='"._NO_."'>"._NO_."</a></div>";
				}
			break;
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._ACCESSDENIED_."</b></div>\n";
	}

}

function notifyoff($id) {
	global $dblink;
	
	$nid = intval($id);
	$nuser = inCode($_GET['user']);
	$nthreadid = intval($_GET['threadid']);
	
	echo "<div id='errorText' align='center'><b>";
	echo ($dblink->query("DELETE FROM memht_forum_notify WHERE id=$nid AND user='$nuser' AND threadid=$nthreadid")) ? _DONE_ : _WRONGDATA_ ;
	echo "</b></div>";
}

require_once("pages/forum/inc_functions.php");
setLastPostCookie();

	openTable();
	
	switch($op) {
		case "viewCategory":
			viewCategory($id);
		break;
		
		case "viewForum":
			viewForum($id,$ofsbgn,$ofsppg,$pg);
		break;
	
		case "viewThread":
			viewThread($id,$ofsbgn,$ofsppg,$pg);
		break;
		
		case "newThread":
			newThread($id,$gname,$gemail,$name,$text,$tags,$smiles,$notification,$operation,$ok);
		break;
		
		case "reply":
			reply($id,$gname,$gemail,$name,$text,$quote,$smiles,$notification,$operation,$ok);
		break;
		
		case "editPost":
			editPost($id,$name,$text,$tags,$reason,$smiles,$operation,$ok);
		break;
		
		case "deletePost":
			deletePost($id,$ok);
		break;
		
		case "moderate":
			moderate($id,$operation,$ok);
		break;
		
		case "notifyoff":
			notifyoff($id);
		break;
		
		case "viewMain":
		default:
			viewMain();
		break;
	}
	
	closeTable();

?>
