<?php

if (!defined("ADMIN_FILE") || !is_admin_modul("Forums")) die("Illegal File Access");



require_once("config/config_forums.php");



function pre($in) {

echo "<pre>";

print_r($in);

echo "</pre>";

}





function ForumsNavi() {

	global $admin_file;

	echo "<center><font class=\"title\"><b>"._FORUM."</b></font><br><br>"

	."<a href=\"".$admin_file.".php?op=forums\">"._LISTCATS."</a>"

	." | <a href=\"".$admin_file.".php?op=forums_moder\">"._MODERS."</a>"

	." | <a href=\"".$admin_file.".php?op=forums_config\">"._PREFERENCES."</a>";

	if (file_exists("modules/Forums/admin/update.php")) {

		echo " | <a href=\"".$admin_file.".php?op=forums_update\"><font color=red>Обновить форум</font></a>";

	}

}





function forums() {

	global $prefix, $db, $admin_file;

	include ("header.php");

	GraphicAdmin();

	OpenTable();

	ForumsNavi();

	CloseTable();



	OpenTable ();

	$module_name = "Forums";

	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"middle\"><tr><td><table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" class=\"bgcolor4\">"

	."<tr><th width=100% colspan=\"2\">"._NAMECATS."</th><th>"._FUNCTIONS."</th></tr>";

	$id ? $catid = $id : $catid = 0;

	$categories = getCategoriesSSDF('0');

	$count = count($categories);

	for($i=0;$i<$count;$i++) {

		$catvalue = $categories[$i];

		if($catvalue) {

			if($id && $id == $categories[$i]['id'] OR !$id) { 

				echo "<tr onmouseover=\"this.style.backgroundColor='#BED9FB';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\"  bgcolor=\"#FFFFFF\"><td   colspan=\"2\">";

#				if($id) echo "<a href=\"index.php?name=$module_name\">Форум</a>&nbsp;->&nbsp;"; 

				echo "<br>

				<a href=\"index.php?name=$module_name&op=showforum&id=".$catvalue['id']."\"><b>".$catvalue['title']."</b></a>&nbsp;&nbsp;ID= ".$catvalue['id']." 

				</td><td nowrap=\"nowrap\" align=\"right\" valign=\"bottom\">

				<a href=\"$admin_file.php?op=forums_sort&id=".$catvalue['id']."&sor=ASC&pid=".$catvalue['pid']."\"><img src=\"modules/$module_name/images/up.gif\"  border=\"0\" hspace=\"2\"></a>&nbsp;

				<a href=\"$admin_file.php?op=forums_sort&id=".$catvalue['id']."&sor=DESC&pid=".$catvalue['pid']."\"><img src=\"modules/$module_name/images/down.gif\"  border=\"0\" hspace=\"1\"></a></td></tr>";	

				$sub = getCategoriesSSDF($catvalue['id']);

				foreach($sub as $subkey=>$subvalue) {

					echo "<tr class=\"bgcolor1\" onmouseover=\"this.style.backgroundColor='#BED9FB';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\"><td width=\"32\">&nbsp;&nbsp;&nbsp;</td>"

					."<td width=100%><a href=\"index.php?name=$module_name&op=showforum&id=".$subvalue['id']."\"><b>".$subvalue['title']."</b></a><br /><i>".$subvalue['description']."</i></td>"

					."<td align=\"center\" nowrap=\"nowrap\">

					<a href=\"$admin_file.php?op=forums_cat_edit&id=".$subvalue['id']."\"><img src=\"images/edit.gif\" border=\"0\"></a>&nbsp;

					<a href=\"$admin_file.php?op=forums_cat_del&id=".$subvalue['id']."\"><img src=\"images/delete.gif\" border=\"0\"></a>&nbsp;

					<a href=\"$admin_file.php?op=forums_sort&id=".$subvalue['id']."&sor=ASC&pid=".$subvalue['pid']."\"><img src=\"modules/$module_name/images/up.gif\" border=\"0\" hspace=\"2\"></a>&nbsp;

					<a href=\"$admin_file.php?op=forums_sort&id=".$subvalue['id']."&sor=DESC&pid=".$subvalue['pid']."\"><img src=\"modules/$module_name/images/down.gif\"  border=\"0\" hspace=\"1\"></a></td></tr>";			

				}

			}

		}

	}

  

	if($catid == 0) {

		$nocat = getCategoriesSSDF('-1');

		if($nocat) {

			 echo "<tr><td colspan=\"3\" bgcolor=\"#FFFFFF\"><br><b>Вне категорий</b> ID= -1</td></tr>";

			foreach($nocat as $nockey=>$nocvalue) {

				echo "<tr class=\"bgcolor1\" onmouseover=\"this.style.backgroundColor='#BED9FB';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\"><td width=\"32\">&nbsp;&nbsp;&nbsp;</td>"

				."<td><a href=\"index.php?name=$module_name&op=showforum&id=".$nocvalue['id']."\"><b>".$nocvalue['title']."</b></a><br /><i>".$nocvalue['description']."</i></td>"

				."<td align=\"center\" nowrap=\"nowrap\">

				<a href=\"$admin_file.php?op=forums_cat_edit&id=".$nocvalue['id']."\"><img src=\"images/edit.gif\" border=\"0\"></a>&nbsp;

				<a href=\"$admin_file.php?op=forums_cat_del&id=".$nocvalue['id']."\"><img src=\"images/delete.gif\" border=\"0\"></a>&nbsp;

				<a href=\"$admin_file.php?op=forums_sort&id=".$nocvalue['id']."&sor=ASC&pid=".$nocvalue['pid']."\"><img src=\"modules/$module_name/images/up.gif\" border=\"0\" hspace=\"2\"></a>&nbsp;

				<a href=\"$admin_file.php?op=forums_sort&id=".$nocvalue['id']."&sor=DESC&pid=".$nocvalue['pid']."\"><img src=\"modules/$module_name/images/down.gif\"  border=\"0\" hspace=\"1\"></a>

				</td></tr>";

			}

		}

		echo "</table></td></tr></table><br />";

	}

	CloseTable ();



	OpenTable ();

	 echo "<center><font class=\"option\"><b>"._ADDCAT."</b></font></center>"

	."<form name=\"post\" action=\"".$admin_file.".php\" method=\"post\">"

	."<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" align=\"center\">"

	."<tr><td>"._CATNAME.":</td><td><input type=\"text\" name=\"catname\" size=\"65\" style=\"width:200px\"></td></tr>"

	."<tr><td>"._CATDESC.":</td><td><input type=\"text\" name=\"catdesc\" size=\"65\" style=\"width:200px\"></td></tr>"

	."<tr><td>"._CATPID.":</td><td><input type=\"text\" name=\"catpid\" value= \"1\" size=\"65\" style=\"width:200px\"></td></tr>"

	."<tr><td align=\"center\" colspan=\"2\"><table><tr><td><input type=\"hidden\" name=\"op\" value=\"forums_save\"><input type=\"submit\" value=\""._ADDCAT."\"></td></form>"

	."</tr></form></table></tr></td></table>";

	CloseTable();

	include("footer.php");

}



function getCategoriesSSDF($id = 0) {

	global $db, $prefix;

	static $categories;

	if($categories == 'none') return false;

	if($categories) {

		if($id == '*') {

			return $categories;

        } else {

        	if($categories[$id]) {

            	return $categories[$id];

           } else {

				return false;

           }

       }

   } else {

		$sql_result = $db->sql_query("SELECT * FROM ".$prefix."_forums_cats ORDER BY sort");

		if(!$categories) {

			if($db->sql_numrows($sql_result) == 0) $categories = 'none';

		}

        $data = $db->sql_fetchrowset($sql_result);

        if(is_array($data)) {

        	foreach($data as $key=>$value) {

        		$categories[$value['pid']][] = $value;

        	}

        }

    	return getCategoriesSSDF($id);

    }

}



function forums_save($catname, $catdesc, $catpid) {

	global $prefix, $db, $admin_file;

	if ($catname != "" && $catpid != "") {



	list($max_sort) = $db->sql_fetchrow($db->sql_query("SELECT MAX(sort) AS max_sort FROM ".$prefix."_forums_cats"));

	$max_sort++;

	$db->sql_query("INSERT INTO ".$prefix."_forums_cats (`id`, `title`, `description`, `pid`, `sort`) VALUES (NULL, '$catname', '$catdesc', '$catpid', '".$max_sort."')");

	Header("Location: ".$admin_file.".php?op=forums");

	} else {

	include ("header.php");

	GraphicAdmin();

	warning(""._CERROR1."<br /><br />"._GOBACK."", "", "", 1);

	include("footer.php");

	exit;

	}

}



function forums_cat_edit($id) {

	global $user, $db, $prefix, $admin_file, $id;

	include ("header.php");

	GraphicAdmin();

	OpenTable();

	ForumsNavi();

	CloseTable();

	list($id, $title, $description, $pid) = $db->sql_fetchrow($db->sql_query("SELECT id, title, description, pid FROM ".$prefix."_forums_cats WHERE id='$id'"));

	OpenTable();

	echo "<table border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\"><form name=\"form1\" method=\"post\" action=\"".$admin_file.".php\">"

	."<tr><td>"._CATNAME.":</td><td><input name=\"title\" type=\"text\" value=\"$title\" style=\"width:300px\"></td></tr>"

	."<tr><td>"._CATDESC.":</td><td><input name=\"description\" type=\"text\" value=\"$description\" style=\"width:300px\"></td></tr>"

	."<tr><td>"._SECTION.":</td><td><input name=\"pid\" type=\"text\" value=\"$pid\" style=\"width:300px\"></td></tr>"

	."<tr><td align=\"center\" colspan=\"2\"><input type=\"submit\" value=\""._SAVE."\"></td></tr>"

	."<input name=\"id\" type=\"hidden\" value=\"$id\"><input name=\"op\" type=\"hidden\" value=\"forums_cat_save\"></form></table>";

	CloseTable();

	include("footer.php");

}



function forums_cat_save($id, $title, $description, $pid) {

	global $prefix, $db, $admin_file, $stop;

	if ($title == "" || $pid == "") $stop = ""._CERROR1."";

	if (!$stop) {

	$title = text_filter($title);

	$description = nl2br(text_filter($description));

	$db->sql_query("UPDATE ".$prefix."_forums_cats SET title='$title', description='$description', pid='$pid' WHERE id='$id'");

	Header("Location: ".$admin_file.".php?op=forums");

	}

}



function forums_moder() {

	global $user, $db, $prefix, $admin_file, $rtitle_moder, $rtitle_admin;

	include ("header.php");

	GraphicAdmin();

	OpenTable();

	ForumsNavi();

	CloseTable();

	$result = $db->sql_query("SELECT s.user_id, s.user_name, s.user_last_ip, d.user_moder FROM ".$prefix."_users AS s LEFT JOIN ".$prefix."_forums_users AS d ON s.user_id=d.user_id WHERE user_moder != '0' ORDER BY user_name");

	OpenTable ();

	echo "<table class='bgcolor4' width=100%  border=0 cellpadding=\"3\" cellspacing=\"1\">"

	."<tr>"

	."<th class=\"bgcolor1\" nowrap><center>№</center></th>"

	."<th class=\"bgcolor1\" nowrap><center>"._USERNAME."</center></th>"

	."<th class=\"bgcolor1\" nowrap><center>"._IP."</center></th>"

	."<th class=\"bgcolor1\" nowrap><center>"._STATUS."</center></th>"

	."</tr>";

	while (list($id, $username, $ip, $moder) = $db->sql_fetchrow($result)) {

	if ($moder == 1) $status = $rtitle_moder; elseif ($moder == 2) $status = $rtitle_admin;

	echo "<tr><td class=\"bgcolor1\" width=\"5%\"><center>$id</center></td>"

	."<td class=\"bgcolor1\" width=\"65%\"><b><a href=\"index.php?name=Account&op=userinfo&user_name=$username\" target=\"_blank\">$username</a></b></td>"

	."<td class=\"bgcolor1\" width=\"15%\"><a href=\"http://www.dnsstuff.com/tools/whois.ch?ip=$ip\" target=\"_blank\">$ip</a></td>"

	."<td class=\"bgcolor1\" width=\"15%\">$status</td>"

	."</td></tr>";

	}

	echo  "</td></tr></table>";

	CloseTable ();

	OpenTable ();

	 echo "<center><font class=\"option\"><b>"._ADDMODER."</b></font></center>"

	."<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" align=\"center\">"

	."<form action=\"".$admin_file.".php\" method=\"post\">"

	."<tr><td>"._USERNAME.":</td><td><input type=\"text\" name=\"modername\" size=\"65\" style=\"width:200px\"></td></tr>"

	."<tr><td>"._NAME.":</td><td><select name=\"moderstat\" style=\"width:200px\"><option value=\"0\">"._USER."</option><option value=\"1\">$rtitle_moder</option><option value=\"2\">$rtitle_admin</option></td></tr>"

	."<tr><td align=\"center\" colspan=\"2\"><table><tr><td><input type=\"hidden\" name=\"op\" value=\"forums_moder_save\"><input type=\"submit\" value=\""._ADDMODER."\"></td></form>"

	."</tr></form></table></tr></td></table>";

	CloseTable();

	include("footer.php");

}



function forums_config() {

	global $prefix, $db, $admin_file;

include("header.php");

GraphicAdmin();

include("config/config_forums.php");

	$permtest = end_chmod("config/config_forums.php", 666);

	if ($permtest) warning($permtest, "", "", 1);

	OpenTable();

	ForumsNavi();

	CloseTable();

	OpenTable();

	echo "<h2>"._PREFERENCES."</h2>"

	."<form action=\"".$admin_file.".php\" method=\"post\">"

	."<table border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">"

	."<tr><td>"._TOPICSPG.":</td><td><input type=\"text\" name=\"xtopicspg\" value=\"$topicspg\" size=\"45\" style=\"width:300px\"></td></tr>"

	."<tr><td>"._POSTSPG.":</td><td><input type=\"text\" name=\"xpostspg\" value=\"$postspg\" size=\"45\" style=\"width:300px\"></td></tr>"

	."<tr><td>"._RANG."1:</td><td><input type=\"text\" name=\"xrank1\" value=\"$rank1\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle1\" value=\"$rtitle1\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."2:</td><td><input type=\"text\" name=\"xrank2\" value=\"$rank2\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle2\" value=\"$rtitle2\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."3:</td><td><input type=\"text\" name=\"xrank3\" value=\"$rank3\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle3\" value=\"$rtitle3\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."4:</td><td><input type=\"text\" name=\"xrank4\" value=\"$rank4\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle4\" value=\"$rtitle4\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."5:</td><td><input type=\"text\" name=\"xrank5\" value=\"$rank5\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle5\" value=\"$rtitle5\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."6:</td><td><input type=\"text\" name=\"xrank6\" value=\"$rank6\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle6\" value=\"$rtitle6\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."7:</td><td><input type=\"text\" name=\"xrank7\" value=\"$rank7\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle7\" value=\"$rtitle7\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."8:</td><td><input type=\"text\" name=\"xrank8\" value=\"$rank8\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle8\" value=\"$rtitle8\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."9:</td><td><input type=\"text\" name=\"xrank9\" value=\"$rank9\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle9\" value=\"$rtitle9\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._RANG."10:</td><td><input type=\"text\" name=\"xrank10\" value=\"$rank10\" size=\"5\" style=\"width:40px\"><input type=\"text\" name=\"xrtitle10\" value=\"$rtitle10\" size=\"45\" style=\"width:260px\"></td></tr>"

	."<tr><td>"._MODERRANG.":</td><td><input type=\"text\" name=\"xrtitle_moder\" value=\"$rtitle_moder\" size=\"45\" style=\"width:300px\"></td></tr>"

	."<tr><td>"._ADMINRANG.":</td><td><input type=\"text\" name=\"xrtitle_admin\" value=\"$rtitle_admin\" size=\"45\" style=\"width:300px\"></td></tr>";

	echo "</table>"

	."<center><br><input type=\"hidden\" name=\"op\" value=\"forums_config_save\"><input type=\"submit\" value=\"" . _SAVECHANGES . "\"></center></form>";

	CloseTable();

include("footer.php");

}



function forums_config_save($xtopicspg, $xpostspg, $xrank1, $xrank2, $xrank3, $xrank4, $xrank5, $xrank6, $xrank7, $xrank8, $xrank9, $xrank10, $xrtitle1, $xrtitle2, $xrtitle3, $xrtitle4, $xrtitle5, $xrtitle6, $xrtitle7, $xrtitle8, $xrtitle9, $xrtitle10, $xrtitle_moder, $xrtitle_admin) {

	global $admin_file;

	$content = "define(\"SDD_INSTALLED\",true);\n";

	$content .= "\$topicspg = \"$xtopicspg\";\n";

	$content .= "\$postspg = \"$xpostspg\";\n\n";

	$content .= "\$rank1 = \"$xrank1\";\n";

	$content .= "\$rank2 = \"$xrank2\";\n";

	$content .= "\$rank3 = \"$xrank3\";\n";

	$content .= "\$rank4 = \"$xrank4\";\n";

	$content .= "\$rank5 = \"$xrank5\";\n";

	$content .= "\$rank6 = \"$xrank6\";\n";

	$content .= "\$rank7 = \"$xrank7\";\n";

	$content .= "\$rank8 = \"$xrank8\";\n";

	$content .= "\$rank9 = \"$xrank9\";\n";

	$content .= "\$rank10 = \"$xrank10\";\n\n";

	$content .= "\$rtitle1 = \"$xrtitle1\";\n";

	$content .= "\$rtitle2 = \"$xrtitle2\";\n";

	$content .= "\$rtitle3 = \"$xrtitle3\";\n";

	$content .= "\$rtitle4 = \"$xrtitle4\";\n";

	$content .= "\$rtitle5 = \"$xrtitle5\";\n";

	$content .= "\$rtitle6 = \"$xrtitle6\";\n";

	$content .= "\$rtitle7 = \"$xrtitle7\";\n";

	$content .= "\$rtitle8 = \"$xrtitle8\";\n";

	$content .= "\$rtitle9 = \"$xrtitle9\";\n";

	$content .= "\$rtitle10 = \"$xrtitle10\";\n";

	$content .= "\$rtitle_moder = \"$xrtitle_moder\";\n";

	$content .= "\$rtitle_admin = \"$xrtitle_admin\";\n";

	save_conf("config/config_forums.php", $content);

	Header("Location: ".$admin_file.".php?op=forums_config");

}



function forums_sort () {

	global $prefix, $db, $admin_file;

include("header.php");

GraphicAdmin();

	OpenTable();

	ForumsNavi();

	CloseTable();

	OpenTable();



	$result = $db->sql_query("SELECT id, sort FROM ".$prefix."_forums_cats WHERE pid='".$_GET['pid']."' ORDER BY sort ".$_GET['sor']."");

	while (list($id, $sort)=$db->sql_fetchrow($result)) {

		$mass1[] = $id; 

		$mass2[$id] = $sort; 

	}



	pre($mass1);		pre($mass2);	



$key = array_search($_GET['id'], $mass1);

if ($key!=0) {

$keyold = $key -1;

$old = $mass2[$mass1[$keyold]];

$mass2[$mass1[$keyold]] = $mass2[$mass1[$key]];

$mass2[$mass1[$key]] = $old;



	$db->sql_query("UPDATE ".$prefix."_forums_cats SET sort='".$mass2[$mass1[$key]]."' WHERE id = '".$mass1[$key]."'");

	$db->sql_query("UPDATE ".$prefix."_forums_cats SET sort='".$mass2[$mass1[$keyold]]."' WHERE id = '".$mass1[$keyold]."'");



}

	Header("Location: ".$admin_file.".php?op=forums");	

		CloseTable();

include("footer.php");

}







switch ($op) {

	case "forums":

	forums();

	break;



	case "forums_save":

	forums_save($catname, $catdesc, $catpid);

	break;



	case "forums_cat_edit":

	forums_cat_edit($id);

	break;



	case "forums_cat_save":

	forums_cat_save($id, $title, $description, $pid);

	break;



	case "forums_cat_del":

	$db->sql_query("DELETE FROM ".$prefix."_forums_cats WHERE id = '$id'");

	Header("Location: ".$admin_file.".php?op=forums");

	break;



	case "forums_moder":

	forums_moder();

	break;



	case "forums_moder_save":

	list($user_id) = $db->sql_fetchrow($db->sql_query("SELECT user_id FROM ".$prefix."_users WHERE user_name = '$modername'"));

	$db->sql_query("UPDATE ".$prefix."_forums_users SET user_moder='$moderstat' WHERE user_id = '$user_id'");

	Header("Location: ".$admin_file.".php?op=forums_moder");

	break;



	case "forums_config":

	forums_config();

	break;



	case "forums_sort":

	forums_sort();

	break;



	case "forums_config_save":

	forums_config_save($xtopicspg, $xpostspg, $xrank1, $xrank2, $xrank3, $xrank4, $xrank5, $xrank6, $xrank7, $xrank8, $xrank9, $xrank10, $xrtitle1, $xrtitle2, $xrtitle3, $xrtitle4, $xrtitle5, $xrtitle6, $xrtitle7, $xrtitle8, $xrtitle9, $xrtitle10, $xrtitle_moder, $xrtitle_admin);

	break;



	case "forums_update": 

	require_once ("modules/Forums/admin/update.php");

	forums_update();

	break;

}



?>