<?php
/**
 * @package Flatnux_module_forum
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 2011
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 *
 */
defined('_FNEXEC') or die('Restricted access');
global $_FN;
//--------------------config--------------------------------------------------->
$config=FN_LoadConfig("modules/forum/config.php");
$topicperpage=$config['topicperpage'];
$postperpage=$config['postperpage'];
$allowmodifypost=$config['allowmodifypost'];
$notify_message_to_all_users=$config['notify_message_to_all_users'];
$forumdir=FNFORUM_GetForumTableName();
//--------------------config---------------------------------------------------<
if (!xmltableexists("fndatabase",$forumdir . "_arguments",$_FN['datadir']))
{
	$fields=array();
	$fields[0]['name']="unirecid";
	$fields[0]['extra']="autoincrement";
	$fields[0]['frm_show']="0";
	$fields[0]['primarykey']="1";
	$fields[1]['name']="subject";
	$fields[1]['frm_size']="60";
	$fields[1]['frm_required']="1";
	$fields[2]['name']="argumentgroup";
	$fields[2]['frm_required']="1";
	$fields[3]['name']="level";
	$fields[4]['name']="description";
	echo createxmltable("fndatabase",$forumdir . "_arguments",$fields,$_FN['datadir']);
}
if (!xmltableexists("fndatabase",$forumdir . "_user_topics",$_FN['datadir']))
{
	$fields=array();
	$fields[0]['name']="unirecid";
	$fields[0]['extra']="autoincrement";
	$fields[0]['frm_show']="0";
	$fields[0]['primarykey']="1";
	$fields[1]['name']="username";
	$fields[1]['frm_required']="1";
	$fields[2]['name']="topicid";
	$fields[2]['frm_required']="1";
	echo createxmltable("fndatabase",$forumdir . "_user_topics",$fields,$_FN['datadir']);
}
if (!xmltableexists("fndatabase",$forumdir . "_topics",$_FN['datadir']))
{
	$fields=array();
	$fields[0]['name']="unirecid";
	$fields[0]['primarykey']="1";
	$fields[0]['extra']="autoincrement";
	$fields[0]['frm_show']="0";
	$fields[1]['name']="username";
	$fields[1]['frm_show']="0";
	$fields[2]['name']="subject";
	$fields[2]['frm_i18n']="title";
	$fields[2]['frm_it']="Titolo";
	$fields[2]['frm_en']="Title";
	$fields[2]['frm_required']="1";
	$fields[2]['frm_size']="60";
	$fields[3]['name']="argument";
	$fields[3]['frm_i18n']="argument";
	$fields[3]['frm_it']="Argomento";
	$fields[3]['frm_en']="Argument";
	$fields[3]['frm_required']="1";
	$fields[3]['foreignkey']=$forumdir . "_arguments";
	$fields[3]['fk_link_field']="unirecid";
	$fields[3]['fk_show_field']="subject";
	$fields[3]['frm_setonlyadmin']="1";
	$fields[5]['name']="time";
	$fields[5]['frm_show']="0";
	$fields[6]['name']="level";
	$fields[6]['frm_show']="frm_setonlyadmin";
	$fields[6]['frm_type']="select";
	$fields[6]['frm_options']="0,1,2,3,4,5,6,7,8,9,10";

	$fields[7]['name']="read";
	$fields[7]['frm_show']="0";
	$fields[8]['name']="locked";
	$fields[8]['frm_it']="Bloccato";
	$fields[8]['frm_i18n']="locked";
	$fields[8]['frm_en']="Locked";
	$fields[8]['type']="check";
	$fields[9]['name']="hidden";
	$fields[9]['frm_it']="Nascosto";
	$fields[9]['frm_i18n']="hidden";
	$fields[9]['type']="check";
	$fields[10]['name']="position";
	$fields[10]['defaultvalue']="100";
	$fields[10]['frm_show']="0";
	$fields[10]['frm_i18n']="position";
	echo createxmltable("fndatabase",$forumdir . "_topics",$fields,$_FN['datadir']);
}
if (!xmltableexists("fndatabase",$forumdir . "_posts",$_FN['datadir']))
{
	$fields=array();
	$fields[0]['name']="unirecid";
	$fields[0]['primarykey']="1";
	$fields[0]['extra']="autoincrement";
	$fields[0]['frm_show']="0";
	$fields[1]['name']="subject";
	$fields[2]['name']="text";
	$fields[2]['frm_required']="1";
	$fields[2]['type']="text";
	$fields[4]['name']="parent";
	$fields[4]['frm_show']="0";
	$fields[5]['name']="time";
	$fields[5]['frm_show']="0";
	$fields[6]['name']="topicid";
	$fields[6]['foreignkey']=$forumdir . "_topics";
	$fields[6]['fk_link_field']="unirecid";
	$fields[6]['fk_show_field']="subject";
	$fields[6]['frm_show']="0";
	$fields[7]['name']="username";
	$fields[7]['frm_show']="0";
	echo createxmltable("fndatabase",$forumdir . "_posts",$fields,$_FN['datadir'],array("indexfield"=>"topicid"));
}
//----------------------------GRAFICA------------------------------------->>>
if (!function_exists("FN_OpenForumMenu"))
{
	function FN_OpenForumMenu()
	{
		
	}
}
if (!function_exists("FN_CloseForumMenu"))
{
	function FN_CloseForumMenu()
	{
		
	}
}
if (!function_exists("FN_OpenForum"))
{
	function FN_OpenForum()
	{
		
	}
}
if (!function_exists("FN_OpenForumHeader"))
{
	function FN_OpenForumHeader()
	{
		FN_OpenForum();
	}
}
if (!function_exists("FN_CloseForumHeader"))
{
	function FN_CloseForumHeader()
	{
		FN_CloseForum();
	}
}
if (!function_exists("FN_CloseForum"))
{
	function FN_CloseForum()
	{
		
	}
}
if (!function_exists("FN_OpenPost"))
{
	function FN_OpenPost()
	{
		global $forumborder;
		FN_OpenForum();
	}
}
if (!function_exists("FN_ClosePost"))
{
	function FN_ClosePost()
	{
		FN_CloseForum();
	}
}
/**
 *
 * @global array $_FN
 * @param type $params
 * @param type $sep
 * @param type $norewrite
 * @return string 
 */
function FNFORUM_MakeLink($params = false,$sep = "&amp;",$norewrite=false)
{
	global $_FN;
	$keep=array("mod","op","order");
	$search_min="";
	$search_options="";
	$navigate_groups="";
	$search_fields="";
	$search_partfields="";
	$link=array();
	foreach ($_GET as $key=>$value)
	{
		if (in_array($key,$keep))
		{
			$link[$key]="$key=" . FN_GetParam("$key",$_GET);
		}
	}
	if (is_array($params))
	{
		foreach ($params as $key=>$value)
		{
			$link[$key]="$key=" . urlencode($params[$key]);
			if ($params[$key] === null)
				unset($link[$key]);
		}
	}
	if ($norewrite)
		return implode($sep,$link);
	$link="index.php?" . implode($sep,$link);
	return FN_RewriteLink($link);
}
//----------------------------GRAFICA-------------------------------------------
function FNFORUM_ForumHeader()
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();
//--------------------config---------------------------------------------------<
	$op=FN_GetParam("op",$_REQUEST,"html");
	FN_OpenForumHeader();
	echo "\n<table  class=\"fnforum forum-header-table\" >" . "\n	<tbody>";
//-----------------------------barra di navigazione   ------------------------->
	echo "\n<tr class=\"fnforum forum-header-navbar-tr\" >" . "\n<td  colspan=\"5\" >";
	$tmp=$_FN['sectionvalues']['title'];
	echo "<a href=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}") . "\">$tmp</a>";
	$table_forum_arguments=FN_XmlTable($forumdir . "_arguments");
	$table_forum_topics=FN_XmlTable($forumdir . "_topics");
	$argumentid=FN_GetParam("argument",$_GET,"flat");
	$topicid=FN_GetParam("topic",$_GET,"flat");

	if ($topicid != "")
	{
		$topic=$table_forum_topics->GetRecordByPk($topicid);
		$argumentid=$topic['argument'];
	}
	if ($argumentid != "")
	{
		$argument=$table_forum_arguments->GetRecordByPk($argumentid);
		if (isset($argument['subject']))
		{
			echo "-&gt;<a href=\"" . FNFORUM_MakeLink(array("argument"=>$argumentid,"op"=>"disc")) . "\">" . FNFORUM_RemoveTags($argument['subject']) . "</a>";
		}
	}
	if ($topicid != "")
	{
		echo "-&gt;<a href=\"" . FNFORUM_MakeLink(array("argument"=>$argumentid,"op"=>"readtopic","topic"=>$topic['unirecid'])) . "\" >" . FNFORUM_RemoveTags($topic['subject']) . "</a>";
	}
	if ($op == "search")
	{
		echo "-&gt;<a href=\"" . FNFORUM_MakeLink(array("op"=>"search")) . "\">" . FN_i18n("search") . "</a>";
	}
	echo "\n</td>" . "\n	</tr>";
//-----------------------------barra di navigazione   -------------------------<
	echo "\n<tr  class=\"fnforum forum-header-search-tr\" >" . "\n	<td colspan=\"5\"  >";


	echo"\n	<form action=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}&amp;op=search") . "\" method=\"post\"><div>" .
	"\n<input type=\"hidden\" name=\"where\" value=\"{$_FN['mod']}\" />" .
	"<input type=\"text\" name=\"q\" size=\"16\" />&nbsp;&nbsp;" . "\n";
	echo "<input type=\"submit\" class=\"fnforum submit\" value=\"" . FN_i18n("search") . "\" />" .
	"</div></form>";
	echo "</td></tr>";
	echo "<tr class=\"fnforum forum-header-toolbar-tr\">" .
	"<td  class=\"fnforum forum-header-wellcome-td\">" .
	FN_i18n("welcome");
	if ($_FN['user'] == "")
	{
		echo " <b>" . FN_i18n("unknown") . "</b>";
	}
	else
	{
		echo " <b>{$_FN['user']}</b>";
	}
	echo "</td>";
	if ($_FN['user'] == "")
	{
		echo "\n<td  class=\"fnforum forum-header-edit-profile-td\" >";
		echo "<a href=\"" . FN_RewriteLink("?mod=login&amp;op=register") . "\">" . FN_i18n("register") . "</a>";
		echo "</td>";
		echo "\n<td class=\"fnforum forum-header-login-td\"   >";
		echo FNFORUM_HtmlLoginForm();
		//echo "<a  href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=login") . "\">" . FN_i18n("login") . "</a>";
		echo "</td>";
	}
	else
	{
		echo "\n<td  class=\"fnforum forum-header-edit-profile-td\" >";
		echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=editreg&amp;user={$_FN['user']}") . "\">" . FN_i18n("edit your profile",false,"Aa") . "</a>";
		echo "</td>";
		echo "\n<td class=\"fnforum forum-header-login-td\"   >";
		echo "<a  href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;fnlogin=logout") . "\">" . FN_i18n("logout") . "</a>";
		echo "</td>";
	}
	$numusers=FN_CountUsers();
	echo "<td  class=\"fnforum forum-header-help-td\" ><a href=\"" . FN_RewriteLink("?mod=" . $_FN['mod'] . "&amp;op=help") . "\" >" . FN_i18n("help") . "</a></td>";
	echo "<td  class=\"fnforum forum-header-members-td\"><a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=members") . "\"><b>$numusers</b> " . FN_i18n("users") . "</a></td>";
	echo "</tr></tbody></table><br />";
	FN_CloseForumHeader();
}
//----------------------------GRAFICA------------------------------------------<
$opmod=FN_GetParam("op",$_GET,"flat");
if (empty($opmod))
{
	echo FN_HtmlContent("sections/{$_FN['mod']}");
}
switch ($opmod)
{
	case "login" :
		if ($_FN['user'] == "")
		{
			FN_LoginForm();
		}
		else
		{
			FNFORUM_ForumHeader();
			FNFORUM_Main();
		}
		break;
	case "profile" :
		FNFORUM_ForumHeader();
		FNFORUM_Profile(FN_GetParam("user",$_GET,"html"));
		break;
	case "editreg" :
		FNFORUM_ForumHeader();
		FNFORUM_EditReg(FN_GetParam("user",$_GET,"html"));
		break;
	case "readtopic" :
		FNFORUM_ForumHeader();
		FNFORUM_ReadTopic();
		break;
	case "help" :
		FNFORUM_ForumHeader();
		include ("modules/{$_FN['mod']}/help.{$_FN['lang']}.php");
		$tit=FN_i18n("back");
		echo "<br /><a accesskey=\"" . FN_GetAccessKey($tit,"javascript:history.back()") . "\" href=\"javascript:history.back()\">&lt;&lt;$tit</a>";
		break;
	case "newtopic" :
		FNFORUM_ForumHeader();
		FNFORUM_NewTopic();
		break;
	case "editpost" :
		FNFORUM_ForumHeader();
		FNFORUM_EditPost();
		break;
	case "delpost" :
		FNFORUM_ForumHeader();
		FNFORUM_DelPost();
		break;
	case "newargument" :
		FNFORUM_ForumHeader();
		FNFORUM_NewArgument();
		break;
	case "regtopic" :
		FNFORUM_ForumHeader();
		FNFORUM_RegTopic();
		break;
	case "regdisc" :
		FNFORUM_ForumHeader();
		FNFORUM_RegDisc();
		break;
	case "search" :
		FNFORUM_ForumHeader();
		echo FN_HtmlContent("modules/search/");
		break;
	case "disc" :
		FNFORUM_ForumHeader();
		FNFORUM_MainArgument();
		break;
	case "members" :
		FNFORUM_ForumHeader();
		FNFORUM_Members();
		break;
	default;
	case "" :
		FNFORUM_ForumHeader();
		FNFORUM_Main();
		break;
}
function FNFORUM_HtmlLoginForm()
{
	global $_FN;
	$html="";
	$querystring=FN_GetParam("QUERY_STRING",$_SERVER);
	$querystring=str_replace("&","&amp;",$querystring);
	$html.= "<form method=\"post\" action=\"?$querystring&amp;fnlogin=login\" ><div>";
	$username=(!empty($_FN['username_is_email'])) ? FN_Translate("email") : FN_Translate("username");
	$html.= "&nbsp;<input type=\"text\" size=\"15\" name=\"username\"  onfocus=\"this.value=''\" value=\"$username\" />";
	$html.= "&nbsp;<input value=\"" . FN_Translate("password") . "\" onfocus=\"this.value='';this.type='password'\" type=\"text\"  size=\"15\" name=\"password\" /></div>";
	//------------------------------------ captcha ---------------------------->
	$captcha_ok=true;
	if (!empty($_FN['enable_captcha']))
	{
		$captcha=FN_GetSessionValue("captcha");
		$fnlogincode=FN_GetParam("fnlogin_code",$_POST);
		if ($fnlogincode == "" || empty($captcha['fnlogin_code']) || $captcha['fnlogin_code'] != $fnlogincode)
			$captcha_ok=false;
		FN_SetSessionValue("captcha",array("fnlogin_code"=>rand(1000,9999)));
		$html.= "<div>" . FN_Translate("enter this security code") . ":<img style=\"vertical-align:middle\" src=\"captcha.php?t=fnlogin_code&amp;" . time() . "\" alt=\"\" title=\"\" /> <input size=\"4\" name=\"fnlogin_code\"  value = \"\" /></div>";
	}
	//------------------------------------ captcha ----------------------------<
	$html.= "<div><button type=\"submit\" >" . FN_Translate("login") . "</button></div>";
	//if (!empty($_FN['remember_login']))
	//	$html.= "<div>" . "<input type=\"checkbox\" name=\"rememberlogin\" value=\"1\" />&nbsp;" . FN_Translate("stay connected") . "</div>";
	$html.= "</form>";
	if (!empty($_FN['login_error']))
	{
		FN_Alert(FN_Translate("authentication failure"));
	}
	return $html;
	/*
	  global $_FN;
	  $html="";
	  $querystring=FN_GetParam("QUERY_STRING",$_SERVER);
	  $querystring=str_replace("&","&amp;",$querystring);
	  $html.= "<form method=\"post\" action=\"?$querystring&amp;fnlogin=login\" ><div>";
	  $username =(!empty($_FN['username_is_email'])) ? FN_Translate("email") : FN_Translate("username");
	  $html.= "&nbsp;<input type=\"text\" size=\"15\" name=\"username\" onfocus=\"this.value=''\" value=\"$username\" /> ";
	  $html.=  "&nbsp;<input value=\"".FN_Translate("password")."\" onfocus=\"this.value='';this.type='password'\" type=\"text\" size=\"15\" name=\"password\" />";
	  $html.= " <button type=\"submit\" >" . FN_Translate("login") . "</button> ";
	  //if (!empty($_FN['remember_login']))
	  //	$html.= " " ."<input type=\"checkbox\" name=\"rememberlogin\" value=\"1\" />&nbsp;".FN_Translate("stay connected");
	  $html.= "</div></form>";
	  $fnlogin=FN_GetParam("fnlogin",$_GET);
	  $fnuser=FN_GetParam("username",$_POST);
	  $fnpwd=FN_GetParam("password",$_POST);
	  if ($fnlogin == "login" && $fnuser != "" && $fnpwd != "")
	  {
	  $lpass=md5($fnpwd);
	  $us=FN_GetUser($fnuser);
	  $passwd=$us['passwd'];
	  if ($passwd != $lpass)
	  {
	  FN_Alert(FN_Translate("authentication failure"));
	  }
	  }
	  return $html;
	 */
}
/**
 *
 * @global array $_FN 
 */
function FNFORUM_Members()
{
	global $_FN;

	$page=FN_GetParam("page",$_GET,"flat");
	$order=FN_GetParam("order",$_GET,"flat");

	$numusers=FN_XMLQuery("SELECT count(*) AS c FROM fn_users WHERE active LIKE '1' ");

	$num_records=isset($numusers[0]['c']) ? $numusers[0]['c'] : 0;
	$recordsperpage=20;
	if ($recordsperpage != false)
	{

		if ($page == "")
			$page=1;
		$numPages=ceil($num_records / $recordsperpage);
		if ($page > $numPages)
			$page=$numPages;
		$start=1 + ($page * $recordsperpage - $recordsperpage);
		$end=$start + $recordsperpage;
	}
	$qorder="";
	if ($order != "")
	{
		$qorder="ORDER BY $order";
	}
	$users=FN_XMLQuery("SELECT * FROM fn_users WHERE active LIKE '1' $qorder LIMIT $start,$recordsperpage");


	$res=array();
	$titles=array();
	foreach ($users as $user)
	{
		$tmp['username']=$user['username'];
		$titles["username"]="user";
		if (isset($user['name']))
		{
			$tmp['name']=substr(htmlspecialchars($user['name']),0,60);
			$titles["name"]="name";
		}
		if (isset($user['job']))
		{
			$titles["job"]="job";
			$tmp['job']=substr(htmlspecialchars($user['job']),0,60);
		}
		if (isset($user['link']))
		{
			$titles["link"]="link";
			if ($user['link'] != "http://" && !FN_IsSpam($user['link']))
			{
				$user['link']=htmlspecialchars($user['link']);
				$tmp['link']="<a onclick=\"window.open(this.href);return false;\" href=\"{$user['link']}\">" . htmlspecialchars($user['link']) . "</a>";
			}
			else
			{
				$tmp['link']="";
			}
		}
		if (FN_IsAdmin())
		{
			$titles["email"]="email";
			$tmp['email']=str_replace("@"," at ",$user['email']);
		}
		$res[]=$tmp;
	}
	//dprint_r($res);
	//die("SELECT * FROM fn_users WHERE active LIKE '1' $qorder LIMIT $start,$recordsperpage");
	FNFORUM_printTable($res,$numPages,$titles);
}
/**
 *
 * @global array $_FN
 * @param array $res
 * @param string $numPages
 * @param string $titles 
 */
function FNFORUM_printTable($res,$numPages,$titles)
{
	global $_FN;

	$page=FN_GetParam("page",$_GET,"flat");
	$thcolor="#efebde";
	$bgcolor="#ffffff";
	$bgcolor2="#dadada";
	if (is_array($res) && count($res) > 0)
	{
		$first=true;
		echo "
<script type=\"text/javascript\">
trh = function (over,elem){
	var old; 
	if (over){
		old = elem.style.backgroundColor
		elem.style.backgroundColor='$bgcolor2';
	}
	else{
		elem.style.backgroundColor='$bgcolor';
	}
}
checkdelete = function (url){
	if(confirm (\"Conferma cancellazione\"))
		window.location=url;
}
</script>
";

		$htmlpages=FN_i18n("page",false,"Aa") . " :";
		for ($i=1; $i <= $numPages; $i++)
		{
			if ($i == $page)
				$htmlpages .= "<a href=\"" . FNFORUM_MakeLink(array("page"=>$i)) . "\"><b>$i</b></a> ";
			else
				$htmlpages .= "<a href=\"" . FNFORUM_MakeLink(array("page"=>$i)) . "\">$i</a> ";
		}
		echo $htmlpages;
		echo "\n<table width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" style=\"background-color:#dadada\">";
		$i=1;
		foreach ($res as $row)
		{
			if ($first)
			{
				echo "\n<tr>";
				foreach ($row as $k=>$v)
					echo "<td style=\"background-color:$thcolor;color:#000000\" ><a href=\"" . FNFORUM_MakeLink(array("order"=>"$k")) . "\">" . FN_i18n($titles[$k],false,"Aa") . "</a></td>";
				echo "</tr>";
			}
			$i++;
			echo "<tr onmouseover=\"trh(1,this)\" onmouseout=\"trh(0,this)\" style=\"background-color:$bgcolor\" >";
			foreach ($row as $k=>$v)
			{
				echo "<td>" . ($v) . "</td>";
			}
			echo "</tr>";
			$first=false;
		}
		echo "</table>";
		echo $htmlpages;
	}
}
/**
 *
 * @global array $_FN
 * @global string $forumback
 * @global string $forumborder
 */
function FNFORUM_DelPost()
{
	if (!FNFORUM_IsForumAdmin())
		return;
	global $_FN;
	//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$post=FN_GetParam("post",$_GET,"flat");
	$table_forum_posts=new XMLTable("fndatabase",$forumdir . "_posts",$_FN['datadir']);
	$table_forum_posts->DelRecord($post);
	echo FN_i18n("the post has been removed");
	echo FN_i18n("the post has been removed");
	FN_JsRedirect(FN_RewriteLink("index.php?mod={$_FN['mod']}","&"));
}
/**
 *
 * @global array $_FN
 * @return type 
 */
function FNFORUM_GetArgGroups()
{
	global $_FN;
	//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$list=array();
	$table_forum_arguments=new FieldFrm("fndatabase",$forumdir . "_arguments",$_FN['datadir'],$_FN['lang']);
	$list_forum_arguments=$table_forum_arguments->xmltable->GetRecords(false,false,false,"argumentgroup");
	$oldargumentgroup="";
	foreach ($list_forum_arguments as $forum_arguments)
	{
		if ($oldargumentgroup != $forum_arguments['argumentgroup'])
		{
			$list[]=$forum_arguments['argumentgroup'];
		}
	}
	return $list;
}
/**
 *
 * @global array $_FN
 * @global type $topicperpage
 * @global type $postperpage
 * @global type $giorni
 * @global type $mesi
 * @global type $forumtitle
 * @global string $forumback
 * @global string $forumborder 
 */
function FNFORUM_Main()
{
	global $_FN;
	$topicsthreads=array();
	//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	FN_OpenForum();
	$table_forum_arguments=new FieldFrm("fndatabase",$forumdir . "_arguments",$_FN['datadir'],$_FN['lang']);
	$table_forum_topics=new XMLTable("fndatabase",$forumdir . "_topics",$_FN['datadir']);
	$table_forum_threads=new XMLTable("fndatabase",$forumdir . "_posts",$_FN['datadir']);
	$list_forum_arguments=$table_forum_arguments->xmltable->GetRecords(false,false,false,"argumentgroup");
	if (FN_IsAdmin() || $list_forum_arguments)
	{
		echo "<table class=\"fnforum forum-argumentgroups-table\" >" . "<tbody>";
		if (FNFORUM_IsForumAdmin())
		{
			echo "<tr class=\"fnforum forum-argumentgroups-newargument-tr\"><td  colspan=\"6\" ><a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=newargument&amp;mode=new") . "\">" . FN_i18n("make new argument",false,"Aa") . "</a></td></tr>";
		}
		$image=FN_FromTheme("images/mime/dir.png");
		$oldargumentgroup="";
		//-------------leggo tutti i messaggi e li raggruppo per topic
		$threads=$table_forum_threads->GetRecords(false,false,false,"topicid",false,"topicid|time|username");
		foreach ($threads as $th)
		{
			$topicsthreads[$th['topicid']][]=$th;
		}
		//-------------leggo tutti i topics e li raggruppo per argomento
		$topics=$table_forum_topics->GetRecords(false,false,false,"argument",false,"argument|unirecid|username");
		$userlevel=FNFORUM_GetUserLevel($_FN['user']);
		$arguments_topics=array();
		foreach ($topics as $th)
		{
			$arguments_topics[$th['argument']][]=$th;
		}
		foreach ($list_forum_arguments as $forum_arguments)
		{
			if ($forum_arguments['level'] == "")
				$forum_arguments['level']=-1;
			if ($forum_arguments['level'] > $userlevel)
				continue;
			if ($oldargumentgroup != $forum_arguments['argumentgroup'])
			{
				$oldargumentgroup=$forum_arguments['argumentgroup'];
				echo "<tr class=\"fnforum forum-argumentgroups-header-tr\" ><td colspan=\"6\"  >" . $forum_arguments['argumentgroup'] . ":</td></tr>";
				//-------------titoli------------>
				echo "<tr class=\"fnforum forum-argumentgroups-thead-tr\"  >" .
				"<td class=\"fnforum forum-argumentgroups-icon-title-td\"  ></td>" .
				"<td class=\"fnforum forum-argumentgroups-argument-thead-td\"  >" . FN_i18n("argument",false,"Aa") . "</td>" .
				"<td class=\"fnforum forum-argumentgroups-subjects-thead-td\"  >" . FN_i18n("threads",false,"Aa") . "</td>" .
				"<td class=\"fnforum forum-argumentgroups-messages-thead-td\"  >" . FN_i18n("messages",false,"Aa") . "</td>" .
				"<td class=\"fnforum forum-argumentgroups-lastmessage-thead-td\" >" . FN_i18n("last message",false,"Aa") . "</td>";
				echo "<td  class=\"fnforum forum-argumentgroups-modifydelete-thead-td\"  >&nbsp;</td>";
				echo "</tr>";
				//-------------titoli------------<
			}
			echo "\n<tr class=\"fnforum forum-argumentgroups-tbody-tr\" >";
			$topics=isset($arguments_topics[$forum_arguments['unirecid']]) ? $arguments_topics[$forum_arguments['unirecid']] : array();
			$ndisc=count($topics);
			$nmessages=0;
			$lasthread=false;
			foreach ($topics as $topic)
			{
				if (isset($topicsthreads[$topic['unirecid']]))
				{
					$threads=$topicsthreads[$topic['unirecid']];
					$c=count($threads);
					$nmessages += $c;
					if ($c > 0)
					{
						if (!isset($lasthread['time']) || $lasthread['time'] < $threads[$c - 1]['time'])
							$lasthread=$threads[$c - 1];
					}
				}
			}
			//icon
			echo "\n<td class=\"fnforum forum-argumentgroups-icon-title-td\" ><img src=\"$image\" alt=\"\"/></td>";
			//argument
			echo "\n<td class=\"fnforum forum-argumentgroups-argument-tbody-td\" ><a href=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}&amp;op=disc&amp;argument=" . $forum_arguments['unirecid']) . "\">" . FNFORUM_RemoveTags($forum_arguments['subject']) . "</a>";
			echo "<br /><em>" . $forum_arguments['description'] . "</em></td>";
			//subjects
			echo "\n<td class=\"fnforum forum-argumentgroups-subgects-tbody-td\" >$ndisc</td>";
			//messages
			echo "\n<td class=\"fnforum forum-argumentgroups-messages-tbody-td\" >$nmessages</td>";
			//lastmessage
			echo "\n<td class=\"fnforum forum-argumentgroups-lastmessage-tbody-td\" >";
			if (isset($lasthread['username']))
				echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;last&amp;topic=" . $lasthread['topicid']) . "\">" . $lasthread['username'] . ":&nbsp;" . FN_GetDateTime($lasthread['time']) . "</a>";
			else
				echo "-";
			echo "</td>";
			//modifydelete
			echo "\n<td class=\"fnforum forum-argumentgroups-modifydelete-tbody-td\" >";
			if (FNFORUM_IsForumAdmin())
			{
				echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=newargument&amp;mode=modify&amp;id=" . $forum_arguments['unirecid']) . "\" >" . FN_i18n("modify") . "</a>";
				echo "&nbsp;<a href=\"javascript:check('" . FN_RewriteLink("?mod={$_FN['mod']}&op=newargument&mode=delete&id=" . $forum_arguments['unirecid']) . "');\" >" . FN_i18n("delete") . "</a>";
			}
			else
			{
				echo "&nbsp;";
			}
			echo "</td>";
			echo "\n</tr>";
		}
		echo "</tbody></table>";
	}
	FN_CloseForum();
}
/**
 *
 * @param type $aryInput 
 */
function FNFORUM_NatSort2d(& $aryInput)
{
	$aryTemp=$aryOut=array();
	foreach ($aryInput as $key=>$value)
	{
		reset($value);
		$aryTemp[$key]=current($value);
	}
	//fn_natsort($aryTemp);
	natsort($aryTemp);
	foreach ($aryTemp as $key=>$value)
	{
		//if (isset($aryInput[$key]))
		$aryOut[$key]=$aryInput[$key];
		// --------^^^^ add this if you want your keys preserved!
	}
	$aryInput=$aryOut;
}
/**
 * 
 */
function FNFORUM_MainArgument()
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$argumentid=FN_GetParam("argument",$_GET,"flat");
	$page=FN_GetParam("page",$_GET,"flat");
	if ($page == "")
		$page=1;
	$starttopic=($page * $topicperpage) - $topicperpage + 1;
	FN_OpenForum();
	$table_forum_topics=new XMLTable("fndatabase",$forumdir . "_topics",$_FN['datadir'],$_FN['lang']);
	$table_forum_threads=new XMLTable("fndatabase",$forumdir . "_posts",$_FN['datadir']);
	$table_forum_arguments=new FieldFrm("fndatabase",$forumdir . "_arguments",$_FN['datadir'],$_FN['lang']);
	$argument=$table_forum_arguments->xmltable->GetRecordByPk($argumentid);
	$userlevel=FNFORUM_GetUserLevel($_FN['user']);
	if ($argument['level'] == "")
		$argument['level']=-1;
	if ($argument['level'] > $userlevel)
		return;
	$alltopics=$table_forum_topics->GetRecords(false,false,false,false,false,"argument|unirecid");
	$topicsthreads=array();
	foreach ($alltopics as $ct)
	{
		if ($ct['argument'] == $argument['unirecid'])
			$topicsthreads[$ct['unirecid']]=array();
	}
	//-------------leggo tutti i messaggi e li raggruppo per topic
	$threads=$table_forum_threads->GetRecords(false,false,false,"unirecid",false,"unirecid|topicid|time|username");
	foreach ($threads as $th)
	{
		if (isset($topicsthreads[$th['topicid']]))
		{
			$topicsthreads[$th['topicid']]['last']=$th['unirecid'];
			$topicsthreads[$th['topicid']][]=$th;
		}
	}
	//dprint_r();
	FNFORUM_NatSort2d($topicsthreads);
	$topicsthreads=array_reverse($topicsthreads,true);
	$num_topics=count($topicsthreads);
	echo "<table  class=\"fnforum forum-table\">";
	echo "<tbody>";
	if (FNFORUM_IsForumAdmin())
	{
		$newlevel=FN_GetParam("level",$_POST,"flat");
		if ($newlevel != "")
		{
			$argument=$table_forum_arguments->UpdateRecord(array("unirecid"=>$argumentid,"level"=>$newlevel));
		}
		//cambio permessi dell' argomento
		$level=$argument['level'];
		print("<tr  class=\"fnforum forum-admin-tr\" ><td  colspan=\"7\"><form action='" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=disc&amp;argument=$argumentid") . "' method='post'><div>");
		print("<b>" . FN_i18n("level") . "</b>:");
		print("<select name='level'>");
		print("<option value='-1'>---</option>");
		for ($i=0; $i < 11; $i++)
		{
			if ($level == $i)
				print("<option value='$i' selected=\"selected\" >$i</option>");
			else
				print("<option value='$i' >$i</option>");
		}
		print("</select>");
		print("&nbsp;<input type='submit' class=\"fnforum submit\" value='OK' />");
		print("</div></form></td></tr>");
	}
	//stampa le pagine ------------->
	echo "<tr  class=\"fnforum forum-pages-tr\">";
	echo "<td  colspan=\"7\" >";
	$numPagesTopic=ceil($num_topics / $topicperpage);
	echo FN_i18n("pages",false,"Aa") . ": ";
	for ($i=1; $i <= $numPagesTopic; $i++)
	{
		if ($i == $page)
			echo " <a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=disc&amp;argument=" . $argumentid . "&amp;page=$i") . "\"><b>$i</b></a>";
		else
			echo " <a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=disc&amp;argument=" . $argumentid . "&amp;page=$i") . "\">$i</a>";
	}
	echo "</td>";
	echo "</tr>";
	//stampa le pagine -------------
	if ($_FN['user'] != "")
	{
		echo "<tr  class=\"fnforum forum-newthread-tr\" >";
		echo "<td colspan=\"7\" >";
		echo "<button onclick=\"window.location='" . FN_RewriteLink("?mod={$_FN['mod']}&argument=$argumentid&op=newtopic&mode=new","&") . "';\">" . FN_i18n("new topic") . "</a></button>";
		echo "</td></tr>";
	}
	echo "<tr class=\"fnforum forum-thead-tr\" >";
	echo "<td class=\"fnforum forum-icon-thead-td\"  >&nbsp;</td>" .
	"<td class=\"fnforum forum-title-thead-td\" >" . FN_i18n("thread title") . "</td>" .
	"<td class=\"fnforum forum-pages-thead-td\" >" . FN_i18n("pages") . "</td>" .
	"<td class=\"fnforum forum-messages-thead-td\" >" . FN_i18n("messages") . "</td>" .
	"<td class=\"fnforum forum-read-thead-td\" >" . FN_i18n("has been read") . "</td>" .
	"<td class=\"fnforum forum-firstmessage-thead-td\" >" . FN_i18n("first message") . "</td>" .
	"<td class=\"fnforum forum-lastmessage-thead-td\" >" . FN_i18n("last message") . "</td>" . "</tr>";
	$start=($page * $topicperpage - $topicperpage) + 1;
	$end=$start + $topicperpage - 1;
	$contatopic=1;
	foreach ($topicsthreads as $unirecid=>$messages)
	{
		unset($messages['last']);
		if ($contatopic < $start)
		{
			$contatopic++;
			continue;
		}
		if ($contatopic >= $end)
		{
			break;
		}
		$contatopic++;
		$topic=$table_forum_topics->GetRecordByPk($unirecid);
		$num_message=count($messages);
		//se ho eliminato tutti i posts
		if ($num_message == 0)
			continue;
		$last=array();
		if ($num_message > 0)
			$last=$messages[$num_message - 1];
		$image=FN_FromTheme("modules/forum/images/topic.png");
		if ($topic['locked'] == 1)
		{
			$image=FN_FromTheme("modules/forum/images/lock.png");
		}
		$st="";
		if ($topic['hidden'] == 1)
		{
			if (FNFORUM_IsForumAdmin())
				$st="style=\"text-decoration: line-through;\" ";
			else
				continue;
		}
		echo "<tr  class=\"fnforum forum-tbody-tr\">";
		echo "<td  class=\"fnforum forum-icon-tbody-td\" >";
		echo "<img src=\"$image\" alt=\"topic\" />";
		echo "</td>";
		echo "<td  class=\"fnforum forum-title-tbody-td\" >";
		echo "<a $st href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=" . $topic['unirecid']) . "\">" . htmlentities($topic['subject'],ENT_QUOTES,$_FN['charset_page']) . "</a>";
		echo "</td>";
		echo "<td   class=\"fnforum forum-pages-tbody-td\"  align=\"center\" >";
		$numPages=ceil($num_message / $postperpage);
		for ($i=1; $i <= $numPages; $i++)
		{
			echo "&nbsp;<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=" . $topic['unirecid'] . "&amp;page=$i") . "\">[$i]</a>";
		}
		echo "</td>";
		echo "<td class=\"fnforum forum-messages-tbody-td\" >";
		echo "" . $num_message . "";
		echo "</td>";
		echo "<td class=\"fnforum forum-read-tbody-td\"  >";
		echo "" . $topic['read'] . "";
		echo "</td>";
		echo "<td  class=\"fnforum forum-firstmessage-tbody-td\"  >";
		echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=" . $topic['unirecid']) . "\">" . $topic['username'] . "<br />" . FN_GetDateTime($topic['time']) . "</a>";
		echo "</td>";
		echo "<td  class=\"fnforum forum-lastmessage-tbody-td\"  align=\"center\">";
		echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=" . $topic['unirecid'] . "&amp;page=$numPages") . "\">" . $last['username'] . "<br />" . FN_GetDateTime($last['time']) . "</a>";
		echo "</td>";
		echo "</tr>";
	}
	//stampa le pagine ------------->
	echo "<tr  class=\"fnforum forum-pages-tr\" >";
	echo "<td  colspan=\"7\"  >";
	$numPagesTopic=ceil($num_topics / $topicperpage);
	echo FN_i18n("pages",false,"Aa") . ": ";
	for ($i=1; $i <= $numPagesTopic; $i++)
	{
		if ($i == $page)
			echo " <a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=disc&amp;argument=" . $argumentid . "&amp;page=$i") . "\"><b>$i</b></a>";
		else
			echo " <a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=disc&amp;argument=" . $argumentid . "&amp;page=$i") . "\">$i</a>";
	}
	echo "</td>";
	echo "</tr>";
	//stampa le pagine -------------<?php
	echo "</tbody>" . "</table>";
	FN_CloseForum();
}
/**
 *
 * @global array $_FN
 * @return type 
 */
function FNFORUM_NewArgument()
{
	global $_FN;
	//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	if (FNFORUM_IsForumAdmin())
	{
		$id=FN_GetParam("id",$_GET,"flat");
		$mode=FN_GetParam("mode",$_GET,"flat");
		$table_forum_arguments=new FieldFrm("fndatabase",$forumdir . "_arguments",$_FN['datadir'],$_FN['lang']);
		if ($mode == "delete")
		{
			$table_forum_arguments->xmltable->DelRecord($id);
			FN_JsRedirect(FN_RewriteLink("?mod={$_FN['mod']}"));
			return;
		}
		if ($id != "")
			$oldvalues=$table_forum_arguments->xmltable->GetRecordByPk($id);
		else
			$oldvalues=array("subject"=>"","level"=>"-1","argumentgroup"=>"","description"=>"","title"=>"","unirecid"=>"");
		echo "<form action=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=regdisc") . "\" method=\"post\" name=\"newargument\">";
		FN_OpenForum();
		echo "<table  class=\"fnforum forum\" border=\"0\"  width=\"100%\">
																						<tbody>";
		echo "	<tr>
										<td    colspan=\"2\" align=\"center\" >";
		if ($id != "")
			echo "<b>" . FN_i18n("modify",false,"Aa") . "</b><br /> ";
		else
			echo "<b>" . FN_i18n("make new argument",false,"Aa") . "</b><br /> ";
		echo FN_i18n("required fields") . "</b>
										</td>
									</tr>";
		echo "<tr>";
		echo "<td  >";
		echo "<div class=\"fnforum forum\">";
		echo ("<b>" . FN_i18n("level") . "</b>:");
		echo "</div>";
		echo "</td>";
		echo "<td  >";
		echo ("<select name='level'>");
		echo ("<option value='-1'>---</option>");
		for ($i=0; $i < 11; $i++)
		{
			if ($oldvalues['level'] == $i)
				$s="selected=\"selected\"";
			else
				$s="";
			print("<option $s value='$i' >$i</option>");
		}
		print("</select>");
		echo "</td>";
		echo "</tr>";
		//gruppo
		echo "<tr>";
		echo "<td  >";
		echo "<div class=\"fnforum forum\">";
		echo ("<b>" . FN_i18n("group") . "</b>:");
		echo "</div>";
		echo "</td>";
		echo "<td  >";
		echo "<input name='argumentgroup' value=\"" . $oldvalues['argumentgroup'] . "\" />";
		$list=FNFORUM_GetArgGroups();
		echo "<select  name=\"ssubj\"  onchange=\"document.newargument.argumentgroup.value = document.newargument.ssubj.options[document.newargument.ssubj.selectedIndex].text\">";
		echo "<option value=\"\"></option>";
		$list=array_unique($list);
		foreach ($list as $ar)
		{
			$s=isset($s) ? $s : $ar;
			echo "<option value=\"$ar\">$ar</option>";
		}
		echo "</select";
		echo "</td>";
		echo "</tr>";
		//soggetto
		echo "<tr>";
		echo "<td  >";

		echo ("<b>" . FN_i18n("title") . "</b>:");

		echo "</td>";
		echo "<td >";
		echo "<input  size=\"50\" name='subject' value=\"" . FNFORUM_RemoveTags($oldvalues['subject']) . "\" />";
		echo "</td>";
		echo "</tr>";
		//descrizione
		echo "<tr>";
		echo "<td  >";

		echo ("<b>" . FN_i18n("description") . "</b>:");

		echo "</td>";
		echo "<td  >";
		echo "<input size=\"50\" name='description' value=\"" . FNFORUM_RemoveTags($oldvalues['description']) . "\"  />";
		echo "<input type=\"hidden\" name='unirecid' value=\"" . $oldvalues['unirecid'] . "\"  />";
		echo "</td>";
		echo "</tr>";
		echo "<tr>
<td   colspan=\"2\" >
	<input type=\"submit\" class=\"fnforum submit\" value=\"" . FN_i18n("save") . "\" />
	</td>
</tr>";
		echo "</tbody></table>";
		FN_CloseForum();
		echo "</form>";
	}
}
/**
 * remove tags from string
 * 
 * @param type $str
 * @return string 
 */
function FNFORUM_RemoveTags($str)
{
	$str=str_replace("<","&lt;",$str);
	$str=str_replace(">","&gt;",$str);
	$str=str_replace("\"","&quot;",$str);
	return $str;
}
/**
 *
 * @global array $_FN 
 */
function FNFORUM_RegDisc()
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	if (FNFORUM_IsForumAdmin())
	{
		FN_OpenForum();
		echo "<table   width=\"100%\">" . "	<tbody>" . "<tr>" . "<td  colspan=\"5\" width=\"100%\" >" . "<div class=\"fnforum forum\">";
		$table_forum_arguments=new FieldFrm("fndatabase",$forumdir . "_arguments",$_FN['datadir'],$_FN['lang']);
		$id=FN_GetParam("unirecid",$_POST,"flat");
		$toupdate=false;
		if ($id != "")
		{
			$toupdate=true;
		}
		$newvalues=$table_forum_arguments->getbypost();
		$err=$table_forum_arguments->Verify($newvalues,$toupdate);
		if (count($err) == 0)
		{
			$addr=FN_GetParam("REMOTE_ADDR",$_SERVER,"flat");
			if ($toupdate)
			{
				$UserValues=$table_forum_arguments->UpdateRecord($newvalues);
				FN_log("Update argument $id : {$newvalues['subject']}.");
				echo FN_i18n("the data were successfully updated");
			}
			else
			{
				$UserValues=$table_forum_arguments->InsertRecord($newvalues);
				FN_log("user create new argument : {$newvalues['subject']} .");
				echo FN_i18n("operation complete");
			}
			echo "<br />\n<br />\n";
			echo "<a class=\"fnforum forum\" href=\"" . FN_RewriteLink("index.php?mod=" . $_FN['mod']) . "\">&nbsp;" . FN_i18n("next") . " &gt;&gt;</a>";
		}
		else
		{
			echo FN_i18n("you skipped some fields or you made some error") . " :<br /><br />";
			FNFORUM_PrintErrorArray($err);
			$tit=FN_i18n("back");

			echo "<a accesskey=\"" . FN_GetAccessKey($tit,"javascript:history.back()") . "\" href=\"javascript:history.back()\">&lt;&lt;$tit</a>";
		}
		echo "	</div>" . "</td>" . "</tr>" . "</tbody>" . "</table>";
		FN_CloseForum();
	}
}
/**
 *
 * @param array $err 
 */
function FNFORUM_PrintErrorArray($err)
{
	if (is_array($err))
	{
		foreach ($err as $values)
		{
			echo "{$values['title']} : {$values['error']}<br />";
		}
	}
	echo "<br />";
}
/**
 *
 * @global array $_FN
 * @global string $forumback
 * @global string $forumborder 
 */
function FNFORUM_NewTopic()
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();
//--------------------config---------------------------------------------------<
	$mode=FN_GetParam("mode",$_GET,"flat");
	$argument=FN_GetParam("argument",$_GET,"flat");
	$parent=FN_GetParam("parent",$_GET,"flat");
	$topicid=FN_GetParam("topic",$_GET,"flat");
	$oldtitle="";
	$oldtext="";
	$table_forum_topics=FN_XmlTable($forumdir . "_topics");
	if ($parent != "")
	{
		$table_forum_posts=FN_XmlTable($forumdir . "_posts");
		$oldpost=$table_forum_posts->GetRecordByPk($parent);
		if (isset($oldpost['subject']))
		{
			$oldtopic=$table_forum_topics->GetRecordByPk($oldpost['topicid']);
			$oldtitle="Re: " . $oldtopic['subject'];
		}
		if (isset($oldpost['text']))
		{
			$oldtext="[quote]" . $oldpost['text'] . "[/quote]\n";
		}
	}
	else
	{
		if ($topicid != "")
		{
			$oldpost=$table_forum_topics->GetRecordByPk($topicid);
			$oldtitle="Re: " . $oldpost['subject'];
		}
	}

	//--------------------------form------------------------------------------->
	echo "<form method=\"post\" action=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}&amp;op=regtopic") . "\">";
	echo "<div>
	<input type=\"hidden\" name=\"op\" value=\"regtopic\" />
    <input type=\"hidden\" name=\"argument\" value=\"$argument\" />
	<input type=\"hidden\" name=\"parent\" value=\"$parent\" />
	<input type=\"hidden\" name=\"mode\" value=\"$mode\" />	
	<input type=\"hidden\" name=\"topicid\" value=\"$topicid\" />
	";
	echo "<table class=\"fnforum forumnewtopic-table\" >\n<tbody><tr class=\"fnforum forumnewtopic-newmessage-tr\" ><td colspan=\"2\" ><b>";
	if ($mode == "new")
	{
		echo FN_i18n("make new topic",false,"Aa");
	}
	else
	{
		echo FN_i18n("new message",false,"Aa");
	}
	echo "</b></td></tr>\n";
	echo "<tr class=\"fnforum forumnewtopic-subject-tr\" >
		<td  class=\"fnforum forumnewtopic-subject-td\" >";
	echo FN_i18n("subject",false,"Aa") . ":</td>";
	echo "<td class=\"fnforum forumnewtopic-message-td\" >
	<input name=\"subject\" style=\"\" value=\"$oldtitle\" size=\"60\" /></td></tr><tr><td  valign=\"top\">" . FN_i18n("message",false,"Aa") . ": 
</td>
	<td class=\"fnforum forumnewtopic-formmessage-td\" >";
	FN_BbcodesJs();
	FN_BbcodesPanel("text","formatting");
	echo "<br />";
	FN_BbcodesPanel("text","emoticons");
	echo "<br /><textarea style=\"\" name=\"text\" rows=\"20\" cols=\"60\">$oldtext</textarea></td>
	</tr>
	<tr>
	<td  colspan=\"2\" >
	<button type=\"submit\">" . FN_i18n("send") . "</button>
	<button onclick=\"window.location='" . FNFORUM_MakeLink(array("op"=>"readtopic","topic"=>$topicid)) . "';return false;\" >" . FN_i18n("cancel") . "</button>
	</td>
	</tr>
	</tbody></table></div></form>";
	//--------------------------form-------------------------------------------<


	if ($topicid != "")
	{
		echo "<hr />";
		FNFORUM_ReadTopic($topicid,true);
	}
}
/**
 *
 * @global array $_FN
 * @return type 
 */
function FNFORUM_EditPost()
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();
//--------------------config---------------------------------------------------<
	$post=FN_GetParam("post",$_GET,"flat");
	$table_forum_posts=new XMLTable("fndatabase",$forumdir . "_posts",$_FN['datadir']);
	$oldpost=$table_forum_posts->GetRecordByPk($post);
	if ($allowmodifypost == 0 || $_FN['user'] != $oldpost['username'])
		if (!FNFORUM_IsForumAdmin())
			return;
	/*
	  echo "<form method=\"post\" action=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}&amp;op=regtopic&amp;mode=edit") . "\">";
	  echo "<table class=\"fnforum forumnewtopic-table\" ><tbody>
	  <tr><td colspan=\"2\"  >";;
	  echo "<b>".FN_i18n("modify") . ":</b>";
	  echo "</td></tr>";
	  echo "</tr>
	  <tr>
	  <td >
	  <input type=\"hidden\" name=\"op\" value=\"regtopic\" />
	  <input type=\"hidden\" name=\"unirecid\" value=\"$post\" />" . FN_i18n("subject") . ":
	  </td>";
	  echo "
	  <td>";
	  echo "<input name=\"subject\"  value=\"" . FNFORUM_RemoveTags($oldpost['subject']) . "\" size=\"60\" /></td>
	  </tr>
	  <tr>
	  <td  valign=\"top\">" . FN_i18n("message") . ": </td>
	  <td>";
	  FN_BbcodesJs();
	  FN_BbcodesPanel("text","formatting");
	  FN_BbcodesPanel("text","emoticons");
	  echo "<br /><textarea name=\"text\" rows=\"20\" cols=\"60\">" . FNFORUM_RemoveTags($oldpost['text']) . "</textarea></td>
	  </tr>
	  <tr>
	  <td  colspan=\"2\" >
	  <input type=\"submit\" value=\"" . FN_i18n("save") . "\" />
	  </td>
	  </tr>
	  </tbody>
	  </table>
	  </form>";
	 */
	$oldtitle=FNFORUM_RemoveTags($oldpost['subject']);
	$oldtext=FNFORUM_RemoveTags($oldpost['text']);
	$topicid=$oldpost['topicid'];
	//--------------------------form------------------------------------------->
	echo "<form method=\"post\" action=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}&amp;op=regtopic&amp;mode=edit") . "\">";
	echo "<input type=\"hidden\" name=\"unirecid\" value=\"$post\" />";
	echo "<table class=\"fnforum forumnewtopic-table\" >\n<tbody><tr class=\"fnforum forumnewtopic-newmessage-tr\" ><td colspan=\"2\" ><b>";
	echo FN_i18n("modify",false,"Aa");
	echo "</b></td></tr>\n";
	echo "<tr class=\"fnforum forumnewtopic-subject-tr\" >
		<td  class=\"fnforum forumnewtopic-subject-td\" >" . FN_i18n("subject",false,"Aa") . ":</td>";
	echo "<td class=\"fnforum forumnewtopic-message-td\" >
	<input name=\"subject\"  value=\"$oldtitle\" size=\"60\" /></td>
	</tr><tr><td  valign=\"top\">" . FN_i18n("message",false,"Aa") . ": 
</td>
	<td class=\"fnforum forumnewtopic-formmessage-td\" >";
	FN_BbcodesJs();
	FN_BbcodesPanel("text","formatting");
	echo "<br />";
	FN_BbcodesPanel("text","emoticons");
	echo "<br /><textarea name=\"text\" rows=\"20\" cols=\"60\">$oldtext</textarea></td>
	</tr>
	<tr>
	<td  colspan=\"2\" >
	<button type=\"submit\">" . FN_i18n("send") . "</button>
	<button onclick=\"window.location='" . FNFORUM_MakeLink(array("op"=>"readtopic","topic"=>$topicid)) . "';return false;\" >" . FN_i18n("cancel") . "</button>
	</td>
	</tr>
	</tbody></table></form>";
	//--------------------------form-------------------------------------------<
}
/**
 *
 * @global array $_FN
 */
function FNFORUM_RegTopic()
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<

	if ($_FN['user'] == "")
		die(FN_i18n("operation not allowed",false,"Aa"));
	$argument=FN_GetParam("argument",$_POST,"flat");
	$parent=FN_GetParam("parent",$_POST,"flat");
	$topicid=FN_GetParam("topicid",$_POST,"flat");
	$mode=FN_GetParam("mode",$_GET,"flat");
	$uservalues=FN_GetUser($_FN['user']);
	$sended=false;
	FN_OpenForum();

	if (!isset($_POST['subject']) || !isset($_POST['text']) || ($_POST['subject'] == "") || ($_POST['text'] == ""))
	{
		print FN_i18n("you skipped some fields or you made some error",false,"Aa") . "<br /><br /><a class=\"fnforum forum\" href=\"javascript:history.back()\">&lt;&lt;" . FN_i18n("back") . "</a><br /><br />";
	}
	else
	{
		//modifica topic
		if ($mode == "edit")
		{
			$table_forum_threads=new FieldFrm("fndatabase",$forumdir . "_posts",$_FN['datadir'],$_FN['lang']);
			$newvalues=$table_forum_threads->getbypost();
			$oldvalues=$table_forum_threads->xmltable->GetRecordByPrimaryKey($newvalues['unirecid']);
			if (!FNFORUM_IsForumAdmin() && ($allowmodifypost == 0 || $_FN['user'] != $oldvalues['username']))
			{
				echo FN_i18n("operation not allowed") . "<br />";
				$newvalues=$oldvalues;
			}
			else
			{
				$newvalues=$table_forum_threads->UpdateRecord($newvalues);
				$sended=true;
			}
		}
		else //nuovo topic
		{
			$table_forum_threads=new FieldFrm("fndatabase",$forumdir . "_posts",$_FN['datadir'],$_FN['lang']);
			$forum_topics=new FieldFrm("fndatabase",$forumdir . "_topics",$_FN['datadir'],$_FN['lang']);
			$newvalues=$table_forum_threads->getbypost();
			if ($uservalues['signature'] != "")
				$newvalues['text'] .= "\n\n" . $uservalues['signature'];
			if (!FNFORUM_IsForumAdmin() && FN_IsSpam($newvalues['subject'] . " " . $newvalues['text']))
			{
				echo (FN_i18n("operation is not allowed because it was identified as spam",false,"Aa"));
				FN_CloseForum();
				return;
			}
			$tablearguments=new XMLTable("fndatabase",$forumdir . "_arguments",$_FN['datadir']);
			//nuovo topic (e primo messaggio del thread)
			if ($newvalues['topicid'] == "")
			{
				if ($argument == "")
					die(FN_i18n("operation not allowed"));
				if ($tablearguments->GetRecordByPk($argument) == null)
				{
					die(FN_i18n("operation not allowed"));
				}
				$topic=array();
				$topic['subject']=$newvalues['subject'];
				$topic['user']=$_FN['user'];
				$topic['time']=time();
				$topic['read']="0";
				$topic['argument']=$argument;
				$topic['username']=$_FN['user'];
				$newtopic=$forum_topics->InsertRecord($topic);
				$newvalues['topicid']=$newtopic['unirecid'];
			}
			else
				$topic=$forum_topics->xmltable->GetRecordByPk($newvalues['topicid']);
			if ($argument == "")
			{
				$argument=$tablearguments->GetRecordByPk($topic['argument']);
				$argument=$argument['unirecid'];
			}
			$newvalues['time']=time();
			$newvalues['username']=$_FN['user'];
			$table_forum_threads->InsertRecord($newvalues);
			$sended=true;
//-----------------------------send message------------------------------------>
			if ($notify_message_to_all_users)
			{
				$listnotify=  FN_GetUsers();
			}
			else
				$listnotify=FNFORUM_ListUsersNotify($newvalues['topicid']);
			foreach ($listnotify as $notify)
			{
				$orimess=$newvalues['text'];
				$uservalues=FN_GetUser($notify);
				$to=$uservalues['email'];
				//skyp current user
				if ($to != $uservalues['email'])
				{
					$link=FN_RewriteLink("index.php?mod={$_FN['mod']}&op=readtopic&topic={$newvalues['topicid']}","&");
					$subject="[{$_FN['sitename']}] - " . FN_i18n("new message",false,"Aa");
					$body=FN_i18n("new message") . ":\n{$link}\n\n" . FN_i18n("from") . " {$_FN['user']}\n$orimess\n\n" . $_FN['sitename'] . "\n";
					FN_SendMail($to,$subject,$body);
				}
			}
//-----------------------------send message------------------------------------<
		}
		if ($sended == true)
		{
			if ($parent == "" && $topicid == "")
			{
				FN_log("user created make new topic : {$newvalues['subject']}.");
			}
			echo FN_i18n("message sended",false,"Aa");
			echo "<br /><br /><a class=\"fnforum forum\" href=\"" . FN_RewriteLink("index.php?mod=" . $_FN['mod'] . "&amp;op=readtopic&topic=" . $newvalues['topicid']) . "\" >" . FN_i18n("read") . " &gt;&gt;</a>";
			FN_JsRedirect(FN_RewriteLink("index.php?mod=" . $_FN['mod'] . "&op=readtopic&topic=" . $newvalues['topicid'] . "&last","&"));
		}
	}
	FN_CloseForum();
}
/**
 *
 * @param string $user
 * @return int 
 */
function FNFORUM_GetUserLevel($user)
{
	$uservalues=FN_GetUser($user);
	if (!isset($uservalues['level']))
		return -1;
	return $uservalues['level'];
}
/**
 *
 * @global array $_FN
 * @param string $topic
 * @param string $compact
 */
function FNFORUM_ReadTopic($topic = false,$compact = false)
{
	if (!$topic)
		$topic=FN_GetParam("topic",$_GET,"flat");
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$argument=FN_GetParam("argument",$_POST,"flat");
	$parent=FN_GetParam("parent",$_GET,"flat");
	$page=FN_GetParam("page",$_GET,"flat");
	$tablethreads=new XMLTable("fndatabase",$forumdir . "_posts",$_FN['datadir']);
	// aggiorno le letture ->
	$tabletopics=new FieldFrm("fndatabase",$forumdir . "_topics",$_FN['datadir'],$_FN['lang']);
	$topicsvalues=$tabletopics->xmltable->GetRecordByPk($topic);
	if (!is_array($topicsvalues))
		return;

	$userlevel=FNFORUM_GetUserLevel($_FN['user']);
	//associo il livello del topic a quello dell' argomento
	$tablearguments=new XMLTable("fndatabase",$forumdir . "_arguments",$_FN['datadir']);
	$argumentvalues=$tablearguments->GetRecordByPk($topicsvalues['argument']);
	$topicsvalues['level']=$argumentvalues['level'];
	if ($topicsvalues['level'] == "")
		$topicsvalues['level']=-1;
	if ($topicsvalues['level'] > $userlevel)
		return;
	$topicsvalues['read']++;
	$tabletopics->UpdateRecord($topicsvalues);
	// aggiorno le letture -<
	if ($page == "")
		$page=1;
	FN_OpenPost();
	echo "<table  class=\"fnforum forumtopic-table\"  >" . "<tbody >";
	//salvataggio  ----->
	if (FNFORUM_IsForumAdmin() && !$compact)
	{
		if (isset($_POST['save']))
		{
			$newvalues=$tabletopics->getbypost();
			$err=$tabletopics->Verify($newvalues,true);
			if (count($err) == 0)
				$topicsvalues=$tabletopics->UpdateRecord($newvalues);
			else
			{
				echo "<tr><td  colspan=\"2\">";
				FNFORUM_PrintErrorArray($err);
				echo "</td></tr>";
			}
		}
	}
	//salvataggio  -----<
	$r=array("topicid"=>$topic);
	$posts=$tablethreads->GetRecords($r,false,false,"unirecid");
	$totalposts=count($posts);
	$numPages=ceil($totalposts / $postperpage);
	if (isset($_GET['last']) || $compact)
		$page=$numPages;
	$starttopic=($page * $postperpage) - $postperpage + 1;
	//--------rispondi---------------------------->
	if ($topicsvalues['locked'] != 1 && !$compact)
	{
		if ($_FN['user'] != "")
		{
			echo "<tr  class=\"fnforum forumtopic-reply-tr\" >" . "<td  colspan=\"2\" >";
			echo "<button onclick=\"window.location='" . FN_RewriteLink("?mod={$_FN['mod']}&op=newtopic&mode=ris&topic=$topic") . "'\" >" . FN_i18n("reply") . "</button>" . "</td></tr>";
		}
	}
	//--------rispondi----------------------------<
	echo "<tr class=\"fnforum forumtopic-thead-tr\" >" .
	"\n<td class=\"fnforum forumtopic-user-thead-td\" ><b>" . FN_i18n("user") . "</b></td>" .
	"\n<td class=\"fnforum forumtopic-message-thead-td\" ><b>" . FN_i18n("message") . "</b></td>" .
	"\n</tr>";
	for ($ip=$starttopic - 1; $ip < ($starttopic + $postperpage) - 1; $ip++)
	{
		if (!isset($posts[$ip]))
			break;
		$post=$posts[$ip];
		echo "<tr class=\"fnforum forumtopic-tbody-tr\" >";
		echo "<td class=\"fnforum forumtopic-user-tbody-td\" ><div>";

		$poster=$post['username'];
		if (($uservalues=FN_GetUser($post['username'])) != null)
		{
			//immagine utente ---->
			$image=FN_HtmlUserImage($post['username']);
			print "$image<br />";
			print "<br /><b>$poster</b><br /><br />";
			//immagine utente ----<
			// tabella per livello  0-10 ------>
			$level=$uservalues['level'];
			print "<table  cellpadding='0' cellspacing='0' width=\"120\">
							<tr><td>0</td>
						<td style=\"text-align:center\" ><b>" . FN_i18n("level") . " $level</b></td>
						<td align='right'>10</td></tr></table>";
			// tabella per livello  0-10   ------<
			print "<hr />";
			//barra livello ----------->
			$i=0;
			while ($i < $level)
			{
				echo "<img src=\"" . FN_FromTheme("images/useronline/level_y.gif") . "\" alt=\"level\" style=\"vertical-align:middle\" />";
				$i++;
			}
			while ($i < 10)
			{
				echo "<img  src=\"" . FN_FromTheme("images/useronline/level_n.gif") . "\" alt=\"level\" style=\"vertical-align:middle\" />";
				$i++;
			}
			//barra livello -----------<
			print "<hr  />";
			print "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=profile&amp;user=$poster") . "\"><img alt=\"profile\"src=\"images/users.png\"  /></a>" . "&nbsp;";
			if ((!isset($uservalues['emailhidden']) || $uservalues['emailhidden'] == 0) || FNFORUM_IsForumAdmin()) //gli amministratori vedono tutte le email
			{
				print("<a href=\"mailto:" . $uservalues['email'] . "\"><img alt=\"mail\" src=\"" . FN_FromTheme("modules/forum/images/mail.png") . "\"  /></a>");
			}
			if (isset($uservalues['link']) && strlen($uservalues['link']) > 7)
			{
				print("&nbsp;<a href=\"" . $uservalues['link'] . "\" onclick=\"window.open(this.href);return false;\" ><img alt=\"home\" src=\"" . FN_FromTheme("images/home.png") . "\"  /></a>");
			}
		}
		else
		{
			print "<br /><b>$poster</b>";
		}
		echo "</div></td>";

		echo "<td   class=\"fnforum forumtopic-message-tbody-td\" >";
		echo FN_GetDateTime($post['time']);
		echo "<br /><br /><b>" . FNFORUM_RemoveTags($post['subject']) . ":</b><br />";
		echo FN_Tag2Html(FNFORUM_RemoveTags($post['text']));
		echo "<br /><br />";
		if ($topicsvalues['locked'] != 1 && !$compact)
			if ($_FN['user'] != "")
				echo "<button onclick=\"window.location='" . FN_RewriteLink("?mod={$_FN['mod']}&op=newtopic&mode=ris&topic=$topic&parent=" . $post['unirecid']) . "'\" >" . FN_i18n("quote the post") . "</button>";
		if ((($allowmodifypost == 1 && $_FN['user'] == $post['username']) || FNFORUM_IsForumAdmin()) && !$compact)
		{
			echo "&nbsp;<button onclick=\"window.location='" . FN_RewriteLink("?mod={$_FN['mod']}&op=editpost&post=" . $post['unirecid']) . "'\" >" . FN_i18n("edit post") . "</button>";
		}
		if (FNFORUM_IsForumAdmin() && !$compact)
		{
			echo "<button onclick=\"check('" . FN_RewriteLink("?mod={$_FN['mod']}&op=delpost&post=" . $post['unirecid']) . "')\" >" . FN_i18n("del post") . "</button>";
		}
		echo "</td>";
		echo "</tr>";
	}
	//--------rispondi---------------------------->
	if ($topicsvalues['locked'] != 1 && !$compact)
		if ($_FN['user'] != "")
		{
			echo "<tr class=\"fnforum forumtopic-reply-tr\" >" . "<td  colspan=\"2\">";
			echo "<button onclick=\"window.location='" . FN_RewriteLink("?mod={$_FN['mod']}&op=newtopic&mode=ris&topic=$topic") . "'\" >" . FN_i18n("reply") . "</button>" . "</td></tr>";
		}
	//--------rispondi----------------------------<
	//--------pagine---------------------------->
	if (!$compact)
	{
		echo "<tr class=\"fnforum forumtopic-pages-tr\" ><td  colspan=\"2\"  >";
		for ($i=1; $i <= $numPages; $i++)
		{
			if ($page == $i)
				echo "&nbsp;<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=$topic&amp;page=$i") . "\"><b>[$i]</b></a>";
			else
				echo "&nbsp;<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=$topic&amp;page=$i") . "\">[$i]</a>";
		}
		echo "</td></tr>";
		//--------pagine----------------------------<
	}
	//--------notifica---------------------------->
	if ($_FN['user'] != "" && !$compact)
	{
		$tableuser_topics=new XMLTable("fndatabase",$forumdir . "_user_topics",$_FN['datadir']);
		$donotify=FN_GetParam("donotify",$_POST,"flat");
		if ($donotify != "")
		{
			$notify=FN_GetParam("notify",$_POST,"flat");
			$notifyuser=FN_GetParam("notifyuser",$_POST,"flat");
			$notiftopic=$topic;
			//if (!FNFORUM_IsForumAdmin())
			$notifyuser=$_FN['user'];
			if ($notify != "")
				FNFORUM_AddUserNotify($topic,$notifyuser);
			else
				FNFORUM_DelUserNotify($topic,$notifyuser);
		}
		$iscritto=FNFORUM_IsUserNotify($topic,$_FN['user']);
		$ch="";
		if ($iscritto)
			$ch="checked=\"checked\"";
		echo "<tr class=\"fnforum forumtopic-notification-tr\" >" . "<td  colspan=\"2\" >";
		echo "<form method=\"post\" action=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=$topic&amp;page=$page") . "\">";
		echo "<div class=\"fnforum forum\">" . FN_i18n("notification through email of new messages in this topic");
		echo " <input $ch type=\"checkbox\" value=\"notify\" name=\"notify\" />";
		echo "<input type=\"submit\" name=\"donotify\" class=\"fnforum submit\" value=\"" . FN_i18n("save") . "\" />" . "</div></form>";
		$usersnotify=FNFORUM_ListUsersNotify($topic);
		if (FNFORUM_IsForumAdmin() && count($usersnotify) > 0)
		{
			echo FN_i18n("users") . "&nbsp;:";
			foreach ($usersnotify as $usermot)
			{
				echo " <a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=profile&amp;user=$usermot") . "\">$usermot</a>";
			}
		}
		echo "</td></tr>";
		//--------notifica----------------------------<
	}
	//form d aggiornamento del topic ---->
	if (FNFORUM_IsForumAdmin() && !$compact)
	{
		echo "<tr class=\"fnforum forumtopic-admin-tr\" ><td  colspan=\"2\">";
		echo "<b>" . FN_i18n("modify") . ":</b><br />";
		echo "<form action=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=readtopic&amp;topic=$topic&amp;page=$page") . "\" method=\"post\"><div>";
		$tabletopics->ShowUpdateForm($topic,FNFORUM_IsForumAdmin());
		echo "<input name=\"save\" type=\"hidden\" value=\"save\" />";
		echo "<br />\n<button type=\"submit\">" . FN_i18n("save") . "</button>";
		echo "</div></form></td></tr>";
	}
	//form d aggiornamento del topic ----<
	echo "</tbody>" . "</table>";
	if ($compact)
		echo "<br />";
	FN_ClosePost();
}
/**
 * 
 * @param string $topic
 * @param string $user
 */
function FNFORUM_IsUserNotify($topic,$user)
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$tableuser_topics=new XMLTable("fndatabase",$forumdir . "_user_topics",$_FN['datadir']);
	$iscritto=$tableuser_topics->GetRecord(array("username"=>$user,"topicid"=>$topic));
	if ($iscritto != null)
		return true;
	return false;
}
/**
 * 
 * @param string $topic
 * @param string $user
 */
function FNFORUM_AddUserNotify($topic,$user)
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$tableuser_topics=new XMLTable("fndatabase",$forumdir . "_user_topics",$_FN['datadir']);
	$iscritto=$tableuser_topics->GetRecord(array("username"=>$user,"topicid"=>$topic));
	if (!FNFORUM_IsUserNotify($topic,$user))
		$iscritto=$tableuser_topics->InsertRecord(array("username"=>$user,"topicid"=>$topic));
}
/**
 * 
 * @param string $topic
 * @param string $user
 */
function FNFORUM_DelUserNotify($topic,$user)
{
	global $_FN;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();

//--------------------config---------------------------------------------------<
	$tableuser_topics=new XMLTable("fndatabase",$forumdir . "_user_topics",$_FN['datadir']);
	$iscritto=$tableuser_topics->GetRecord(array("username"=>$user,"topicid"=>$topic));
	if ($iscritto != null)
		$tableuser_topics->DelRecord($iscritto['unirecid']);
}
/**
 * 
 * @param string $topic
 */
function FNFORUM_ListUsersNotify($topic)
{
	global $_FN,$postperpage;
//--------------------config--------------------------------------------------->
	$config=FN_LoadConfig("modules/forum/config.php");
	$topicperpage=$config['topicperpage'];
	$postperpage=$config['postperpage'];
	$allowmodifypost=$config['allowmodifypost'];
	$notify_message_to_all_users=$config['notify_message_to_all_users'];
	$forumdir=FNFORUM_GetForumTableName();
//--------------------config---------------------------------------------------<
	$tableuser_topics=new XMLTable("fndatabase",$forumdir . "_user_topics",$_FN['datadir']);
	$iscritti=$tableuser_topics->GetRecords(array("topicid"=>$topic));
	$list=array();
	foreach ($iscritti as $rec)
	{
		$list[]=$rec['username'];
	}
	return $list;
}
/**
 *
 * @global array $_FN
 * @return type 
 */
function FNFORUM_IsForumAdmin($topic=false)
{
	global $_FN;
	$config=FN_LoadConfig("modules/forum/config.php");
	if (FN_IsAdmin())
		return true;
	if ($_FN['user'] != "" && FN_UserInGroup($_FN['user'],$config['forumadmin']))
		return true;
	return false;
}
/**
 *
 * @global array $_FN
 * @return string 
 */
function FNFORUM_GetForumTableName()
{
	global $_FN;
	$config=FN_LoadConfig("modules/forum/config.php");
	$forumdir=$config['forumdir'];
	if ($forumdir == "")
	{
		$forumdir=$_FN['mod'];
	}
	if ($config['splitforum'])
	{
		$forumdir.= "_" . $_FN['lang'];
	}
	return $forumdir;
}
/**
 *
 * @param string $user 
 */
function FNFORUM_Profile($user)
{
	global $_FN;
	$uservalues=FN_GetUser($user);
	if ($user == "")
		$user=$_FN['user'];
	if ($_FN['user'] != "")
	{
		if (isset($uservalues['avatar']) && $uservalues['avatar'] != "")
			$image=$_FN['datadir'] . "/{$_FN['database']}/users/$user/avatar/thumbs/" . $uservalues['avatar'] . ".jpg";
		else
		{
			if (isset($uservalues['avatarimage']) && $uservalues['avatarimage'] != "")
			{
				$image=($_FN['datadir'] . "/{$_FN['database']}/fn_avatars/" . urlencode($uservalues['avatarimage']) . "/filename/" . $uservalues['avatarimage']);
			}
			else
				$image=FN_FromTheme("images/user.png");
		}
		echo "<table class=\"fnforum forum-profile-table\"><tbody>";
		echo "<tr>";
		echo "<td colspan=\"2\"  >";
		echo "<b>$user</b><br />";
		echo "<img src=\"$image\" alt=\"$user\" /><br />";
		if ($_FN['user'] == $user || FN_IsAdmin())
		{
			echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;op=editreg&amp;user=$user") . "\" >" . FN_i18n("edit your profile") . "</a> ";
		}
		echo "</td>";
		echo "</tr>";

		$fields=FN_XmlForm("fn_users");
		foreach ($fields->formvals as $key=>$value)
		{
			if (!empty($value['showinprofile']))
			{
				if ($key == 'email' && !FN_IsAdmin())
				{
					if (!empty($uservalues['emailhidden'])) //gli amministratori vedono tutte le email
						continue;
				}
				echo "<tr>";
				echo "<td><b>" . $value['title'] . ":</b></td>";
				echo "<td>" . htmlentities($uservalues[$key],ENT_QUOTES,$_FN['charset_page']) . "</td>";
				echo "</tr>";
			}
		}
		echo "<tr>";
		echo "<td colspan=\"2\"  >";
		echo "<a href=\"" . FN_RewriteLink("?mod={$_FN['mod']}&amp;fnlogin=logout") . "\" >" . FN_i18n("logout") . "</a>";
		echo "</tr>";

		echo "</tbody></table>";
		// se sono io che modifico il mio profilo o sono un'amministratore
	}
	else
	{
		echo "" . FN_i18n("error") . "";
	}
}
/**
 *
 * @param string $user 
 */
function FNFORUM_EditReg($user)
{
	require_once("modules/login/functions_login.php");
	FNREG_ManageEditRegister($user);
}
?>