<?php
if(!defined('DATALIFEENGINE'))
{
  die("Hacking attempt!");
}

if($member_id['user_group'] != 1){ msg("error", "DLE Forum", $lang['db_denied']); }

require_once SYSTEM_DIR.'/inc/forum/init.class.php';

    $dle_forum_init->init_admin();

    $fcache = new cache($dir = ROOT_DIR . '/cache/forum');
	
	$action = $_REQUEST['action'];
	
	switch ($action)
	{
// ********************************************************************************
// MAIN
// ********************************************************************************
		case "":
		
		$count_options = count($options);
		
		for($i=0; $i<$count_options; $i++)
		{
			if($member_db[1] > $options[$i]['access'] AND $options[$i]['access'] != "all")
			{
				unset($options[$i]);
			}
		}
		
		$forum_stats = array();
		
		$row = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_forum_posts");
		$forum_stats['posts'] = $row['count'];
		
		$row = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_forum_topics");
		$forum_stats['topics'] = $row['count'];
		
		$forum_stats['licence'] = ($l_full) ? $f_lg['licence_trial'] : $f_lg['licence_full'];
		
		$sum_size = $db->super_query("SELECT SUM(file_size) AS sum FROM " . PREFIX . "_forum_files");
		
		$forum_stats['sum_size'] = mksize($sum_size['sum']);
		
		if (!$forum_config['offline']) $forum_stats['line'] = $f_lg['forum_online'];
		else $forum_stats['line'] = $f_lg['forum_offline'];
		
		$forum_stats['cache'] = mksize($fcache->size());
		
		$forum_mysql = $db->query("SHOW TABLE STATUS FROM `".DBNAME."`");
		
		while ($row = $db->get_array($forum_mysql))
		{
			if (strpos($row['Name'], PREFIX."_forum_") !== false)
			
			$forum_db_size += $row['Data_length'] + $row['Index_length'] ;
		}
		
		$db->free($forum_mysql);
		
		$forum_stats['db_size'] = mksize($forum_db_size);

		echo_top();
		echo_title($f_lg['m_forum']);
		
		echo "<table width=\"100%\">";
		
		foreach($options as $option)
		{
			if ($i > 1) {echo "</tr><tr>"; $i=0;}
			
			$i++;
			
			echo "<td width=\"50%\"><div class=\"quick\"><a href=\"{$option['url']}\"><img src=\"system/forum/admin/ico/{$option['image']}\" border=\"0\" align=\"left\"><h3>{$option['name']}</h3>{$option['descr']}</a></div></td>";
		}
		
		echo "</table>";
		
		echo_bottom(w);

		echo "<div id=\"update_box\" style=\"display:none\">";
		
		echo_top(w); echo_title($f_lg['check_updates']); echo "<div id=\"update_result\"></div>"; echo_bottom(w);
		
		echo "</div>";
		
		echo_top(w);
		
		echo_title($f_lg['m_stats']);
		
		echo_stats($forum_config, $forum_stats);
		
		echo_bottom();
		
		break;

// ********************************************************************************
// CATEGORY
// ********************************************************************************
		case "category":
		
		echo_top();
		
		echo_title($f_lg['cat_new']);
		
		echo_category('new');
		
		echo_bottom();
		
		break;
		
// ********************************************************************************
// CATEGORY ADD
// ********************************************************************************
		case "category_add":
		
		if ($name)
		{
			$result_posi = $db->super_query("SELECT * FROM " . PREFIX . "_forum_category ORDER BY posi DESC LIMIT 1");
			
			$posi = $result_posi['posi'];
			
			if (!$posi) $posi = '1'; else $posi = ($posi+1);
			
			$name = $db->safesql($_POST['name']);
			
			$db->query("INSERT INTO " . PREFIX . "_forum_category (cat_name, posi) values ('$name', '$posi')");
			
			$fcache->delete('start_id');
			$fcache->delete('cats_array');
			
			msg("info",$f_lg['cat_ok_add1'], $f_lg['cat_ok_add2'], "?mod=forum");
		}
		
		else msg("error",$f_lg['error'],$f_lg['cat_err_name'], "?mod=forum&action=category");
		
		break;

// ********************************************************************************
// CATEGORY EDIT
// ********************************************************************************
		case "category_edit":
		
		$row = $db->super_query("SELECT * FROM " . PREFIX . "_forum_category WHERE sid = '$sid'");
		
		$name = stripslashes(preg_replace(array("'\"'", "'\''"), array("&quot;", "&#039;"),$row['cat_name']));
		
		$sid = $row['sid'];
		
		echo_top();
		
		echo_title($f_lg['cat_edit']);
		
		echo_category('edit', $name, $sid);
		
		echo_bottom();
		
		break;

// ********************************************************************************
// CATEGORY SAVE
// ********************************************************************************
		case "category_save":
		
		$name = $db->safesql($_POST['name']);
		
		if (!$name)
		{
			msg("error",$f_lg['error'],$f_lg['cat_err_name'], "javascript:history.go(-1)");
		}
		
		$db->query("UPDATE " . PREFIX . "_forum_category SET cat_name = '$name' WHERE sid = '$sid'");
		
		$fcache->delete('start_id');
		$fcache->delete('cats_array');
		
		msg("info",$f_lg['cat_ok_edit1'], $f_lg['cat_ok_edit2'], "?mod=forum&action=content");
		
		break;

// ********************************************************************************
// CATEGORY DEL
// ********************************************************************************
		case "category_del":
		
		$category_result = $db->query("SELECT * FROM " . PREFIX . "_forum_forums WHERE main_id = '$sid'");
		
		while ($myrow = $db->get_row($category_result))
		{
			$result = $db->query("SELECT * FROM " . PREFIX . "_forum_topics WHERE forum_id = '$myrow[id]'");
			
			while ($row = $db->get_row($result))
			{
				$db->query("DELETE FROM " . PREFIX . "_forum_posts WHERE topic_id = '$row[tid]'");
			}
			
			$db->query("DELETE FROM " . PREFIX . "_forum_topics WHERE forum_id = '$myrow[id]'");
			
			$db->query("DELETE FROM " . PREFIX . "_forum_forums WHERE id = '$myrow[id]'");

// Удаление вложений вместе с категорией и всеми разделами категории

$result = $db->query("SELECT * FROM " . PREFIX . "_forum_files WHERE forum_id = '" . $myrow['id'] . "'");

while ($row = $db->get_row($result)) {

if ($row['file_type'] == "file") {

unlink(UPLOAD_DIR."files/" . $row['onserver']);

} elseif ($row['file_type'] == "thumb") {

unlink(UPLOAD_DIR."thumbs/" . $row['onserver']);

unlink(UPLOAD_DIR."images/" . $row['onserver']);

} else {

unlink(UPLOAD_DIR."images/" . $row['onserver']);

}

}

$db->query("DELETE FROM " . PREFIX . "_forum_files WHERE forum_id = '" . $myrow['id'] . "'");

// конец удаления вложений вместе с темой
		}
		
		$db->query("DELETE FROM " . PREFIX . "_forum_category WHERE sid = '$sid'");
		
		$fcache->delete('start_id');
		$fcache->delete('cats_array');
		$fcache->delete('forums_array');
		$fcache->delete('sub_forums_array');
		$fcache->delete('sub_parentid');
		
		$fcache->clear();
		
		header("Location: ?mod=forum&action=content");
		
		break;
					
// ********************************************************************************
// CATEGORY SORT
// ********************************************************************************
		case "category_sort":
		
		$result = $db->query("SELECT * FROM " . PREFIX . "_forum_category");
		
		while ($row = $db->get_row($result))
		{
			$db->query("UPDATE " . PREFIX . "_forum_category SET posi = '".$cat_posi[$row['sid']]."' WHERE sid = '$row[sid]'");
		}
		
		$fcache->delete('start_id');
		$fcache->delete('cats_array');
		
		header("Location: ?mod=forum&action=content");
		
		break;

// ********************************************************************************
// CATEGORY ACCESS for forums
// ********************************************************************************
		case "access":
		
		$access_forum = access_forum($user_group, '0');
		
		echo_top();
		
		echo_title($f_lg['access_for_f']);
		
		echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=access_add&sid=$sid\">";
		
		echo $access_forum;
		
		echo "<div class=\"unterline\"></div>";
		
		echo "<input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_save']}\"></form>";
		
		echo_bottom();
		
		break;

// ********************************************************************************
// CATEGORY ACCESS for forums ADD
// ********************************************************************************
		case "access_add":
		
		if (!count($access_read)) {$access_read = array (); $access_read[] = '0';}
		$access_read_mysql = $db->safesql(implode(':', $access_read));
		
		if (!count($access_write)) {$access_write = array (); $access_write[] = '0';}
		$access_write_mysql = $db->safesql(implode(':', $access_write));
		
		if (!count($access_mod)) {$access_mod = array (); $access_mod[] = '0';}
		$access_mod_mysql = $db->safesql(implode(':', $access_mod));
		
		if (!count($access_topic)) {$access_topic = array (); $access_topic[] = '0';}
		$access_topic_mysql = $db->safesql(implode(':', $access_topic));
		
		if (!count($access_upload)) {$access_upload = array (); $access_upload[] = '0';}
		$access_upload_mysql = $db->safesql(implode(':', $access_upload));
		
		if (!count($access_download)) {$access_download = array (); $access_download[] = '0';}
		$access_download_mysql = $db->safesql(implode(':', $access_download));
		
		$db->query("UPDATE " . PREFIX . "_forum_forums SET access_read = '$access_read_mysql', access_write = '$access_write_mysql', access_mod = '$access_mod_mysql', access_topic = '$access_topic_mysql', access_upload = '$access_upload_mysql', access_download = '$access_download_mysql' WHERE main_id = '$sid'");
		
		$fcache->delete('forums_array');
		
		header("Location: ?mod=forum&action=content");
		
		break;
		
// ********************************************************************************
// FORUM
// ********************************************************************************
		case "forum":
		
		echo_top();
		
		echo_title($f_lg['forum_new']);
		
		echo_forum('new', $sid);
		
		echo_bottom();
		
		break;

// ********************************************************************************
// FORUM ADD
// ********************************************************************************
		case "forum_add":
		
		$main_id = $db->safesql($_POST['main_id']);
		
		$parentid = $db->safesql($_POST['parentid']);
		
		if ($parentid){
		
		$result = $db->query("SELECT * FROM " . PREFIX . "_forum_forums WHERE main_id = '$main_id' and parentid = 0");
		
		while ($row = $db->get_row($result))
		{
			if ($row['id'] == $parentid) $test_parentid = TRUE;
		}
		
		}
		
		else $test_parentid = TRUE;
		
		if ($_POST['name'] AND $test_parentid)
		{
			$result_position = $db->super_query("SELECT * FROM " . PREFIX . "_forum_forums WHERE main_id = '$main_id' ORDER BY position DESC LIMIT 1");
			
			$position = $result_position['position'];
			
			if (!$position) $position = '1'; else $position = ($position+1);
			
			$name = $db->safesql($_POST['name']);
			
			$description = $db->safesql($_POST['description']);
			
			$password = $db->safesql($_POST['password']);
			
			$rules_name = $db->safesql($_POST['rules_name']);
			
			$icon = $db->safesql($_POST['icon']);
			
			$postcount = intval($_POST['postcount']);
			
			$fixpost = intval($_REQUEST['fixpost']);
            
            $banner = $db->safesql($_POST['banner']);
            
            $q_reply = intval($_POST['q_reply']);
            
            $i_edit = intval($_POST['i_edit']);
			
			include(SYSTEM_DIR.'/classes/parse.class.php');
			
			$parse = new ParseFilter(Array(), Array(), 1, 1);
			
			$rules = $db->safesql($parse->BB_Parse($parse->process($_POST['rules']), false));
			
			// ACCESS //
			if (!count($access_read)) {$access_read = array (); $access_read[] = '0';}
			$access_read_mysql = $db->safesql(implode(':', $access_read));
			
			if (!count($access_write)) {$access_write = array (); $access_write[] = '0';}
			$access_write_mysql = $db->safesql(implode(':', $access_write));
			
			if (!count($access_mod)) {$access_mod = array (); $access_mod[] = '0';}
			$access_mod_mysql = $db->safesql(implode(':', $access_mod));
			
			if (!count($access_topic)) {$access_topic = array (); $access_topic[] = '0';}
			$access_topic_mysql = $db->safesql(implode(':', $access_topic));
			
			if (!count($access_upload)) {$access_upload = array (); $access_upload[] = '0';}
			$access_upload_mysql = $db->safesql(implode(':', $access_upload));
			
			if (!count($access_download)) {$access_download = array (); $access_download[] = '0';}
			$access_download_mysql = $db->safesql(implode(':', $access_download));
			
			$db->query("INSERT INTO " . PREFIX . "_forum_forums (parentid, main_id, name, description, position, access_read, access_write, access_mod, access_topic, access_upload, access_download, password, rules_title, rules, icon, postcount, fixpost, banner, q_reply, i_edit) values ('$parentid', '$main_id', '$name', '$description', '$position', '$access_read_mysql', '$access_write_mysql', '$access_mod_mysql', '$access_topic_mysql', '$access_upload_mysql', '$access_download_mysql', '$password', '$rules_name', '$rules', '$icon', '$postcount', '$fixpost', '$banner', '$q_reply', '$i_edit')");
			
			$fcache->delete('forums_array');
			$fcache->delete('sub_forums_array');
			$fcache->delete('sub_parentid');
			
			$fcache->clear();
			
			msg("info",$f_lg['forum_ok_add1'], $f_lg['forum_ok_add2'], "?mod=forum");
		}
		
		else msg("error",$f_lg['error'],$f_lg['forum_err_name'], "?mod=forum&action=forum");
		
		break;
		
// ********************************************************************************
// FORUM EDIT
// ********************************************************************************
		case "forum_edit":
		
		echo_top();
		
		echo_title($f_lg['forum_edit']);
		
		echo_forum('edit', $id);
		
		echo_bottom();
		
		break;

// ********************************************************************************
// FORUM SAVE
// ********************************************************************************
		case "forum_save":
		
		$main_id = $db->safesql($_POST['main_id']);
		
		$parentid = $db->safesql($_POST['parentid']);
		
		if ($parentid){
		
		$result = $db->query("SELECT * FROM " . PREFIX . "_forum_forums WHERE main_id = '$main_id' and parentid = 0");
		
		while ($row = $db->get_row($result))
		{
			if ($row['id'] == $parentid) $test_parentid = TRUE;
		}
		
		}
		
		else $test_parentid = TRUE;
		
		if ($_POST['name'] AND $test_parentid)
		{	
			$name = $db->safesql($_POST['name']);
			
			$description = $db->safesql($_POST['description']);
			
			$password = $db->safesql($_POST['password']);
			
			$rules_name = $db->safesql($_POST['rules_name']);
            
            $banner = $db->safesql($_POST['banner']);
            
            $q_reply = intval($_POST['q_reply']);
            
            $i_edit = intval($_POST['i_edit']);
			
			include(SYSTEM_DIR.'/classes/parse.class.php');
			
			$parse = new ParseFilter(Array(), Array(), 1, 1);
			
			$rules = $db->safesql($parse->BB_Parse($parse->process($_POST['rules']), false));
			
			$icon = $db->safesql($_POST['icon']);
			
			$postcount = intval($_POST['postcount']);
			
			$fixpost = intval($_REQUEST['fixpost']);
			
			// ACCESS //
			if (!count($access_read)) {$access_read = array (); $access_read[] = '0';}
			$access_read_mysql = $db->safesql(implode(':', $access_read));
			
			if (!count($access_write)) {$access_write = array (); $access_write[] = '0';}
			$access_write_mysql = $db->safesql(implode(':', $access_write));
			
			if (!count($access_mod)) {$access_mod = array (); $access_mod[] = '0';}
			$access_mod_mysql = $db->safesql(implode(':', $access_mod));
			
			if (!count($access_topic)) {$access_topic = array (); $access_topic[] = '0';}
			$access_topic_mysql = $db->safesql(implode(':', $access_topic));
			
			if (!count($access_upload)) {$access_upload = array (); $access_upload[] = '0';}
			$access_upload_mysql = $db->safesql(implode(':', $access_upload));
			
			if (!count($access_download)) {$access_download = array (); $access_download[] = '0';}
			$access_download_mysql = $db->safesql(implode(':', $access_download));
			
			$db->query("UPDATE " . PREFIX . "_forum_forums SET parentid = '$parentid', main_id = '$main_id', name = '$name', description = '$description', access_read = '$access_read_mysql', access_write = '$access_write_mysql', access_mod = '$access_mod_mysql', access_topic = '$access_topic_mysql', access_upload = '$access_upload_mysql', access_download = '$access_download_mysql', password = '$password', rules_title = '$rules_name', rules = '$rules', icon= '$icon', postcount = '$postcount', fixpost = '$fixpost', banner = '$banner', q_reply = '$q_reply', i_edit = '$i_edit' WHERE id = '$id'");
			
			$fcache->delete('forums_array');
			$fcache->delete('sub_forums_array');
			$fcache->delete('sub_parentid');
			
			$fcache->clear();
			
			msg("info",$f_lg['forum_ok_edit1'], $f_lg['forum_ok_edit2'], "?mod=forum&action=content");
		}
		
		else msg("error",$f_lg['error'],$f_lg['forum_err_name'], "?mod=forum&action=forum");
		
		break;
		
// ********************************************************************************
// FORUM DEL
// ********************************************************************************
		case "forum_del":
		
		$result = $db->query("SELECT * FROM " . PREFIX . "_forum_topics WHERE forum_id = '$id'");
		
		while ($row = $db->get_row($result))
		{
			$db->query("DELETE FROM " . PREFIX . "_forum_posts WHERE topic_id = '$row[tid]'");
		}
		
		$db->query("DELETE FROM " . PREFIX . "_forum_topics WHERE forum_id = '$id'");
		
		$db->query("DELETE FROM " . PREFIX . "_forum_forums WHERE id = '$id'");

// Удаление вложений вместе с разделом

$result = $db->query("SELECT * FROM " . PREFIX . "_forum_files WHERE forum_id = '" . $id . "'");

while ($row = $db->get_row($result)) {

if ($row['file_type'] == "file") {

unlink(UPLOAD_DIR."files/" . $row['onserver']);

} elseif ($row['file_type'] == "thumb") {

unlink(UPLOAD_DIR."thumbs/" . $row['onserver']);

unlink(UPLOAD_DIR."images/" . $row['onserver']);

} else {

unlink(UPLOAD_DIR."images/" . $row['onserver']);

}

}

$db->query("DELETE FROM " . PREFIX . "_forum_files WHERE forum_id = '" . $id . "'");

// конец удаления вложений вместе с темой
		
		$db->query("UPDATE " . PREFIX . "_forum_forums SET parentid = 0 WHERE parentid = '$id'");
		
		$fcache->delete('forums_array');
		$fcache->delete('sub_forums_array');
		$fcache->delete('sub_parentid');
		
		$fcache->clear();
		
		header("Location: ?mod=forum&action=content");
		
		break;

// ********************************************************************************
// FORUM SORT
// ********************************************************************************
		case "forum_sort":
		
		$in_cid = intval($_REQUEST['in_cid']);
		
		$result = $db->query("SELECT * FROM " . PREFIX . "_forum_forums WHERE main_id = $in_cid");
		
		while ($row = $db->get_row($result))
		{
			$db->query("UPDATE " . PREFIX . "_forum_forums SET position = '".$cat_posi[$row['id']]."' WHERE id = '$row[id]'");
		}
		
		$fcache->delete('forums_array');
		$fcache->delete('sub_forums_array');
		$fcache->delete('sub_parentid');
		
		$fcache->clear();
		
		header("Location: ?mod=forum&action=content");
		
		break;
		
// ********************************************************************************
// CONTENT
// ********************************************************************************
		case "content":
		
		echo_top();
		
		$sub_forums = $db->query("SELECT * FROM ". PREFIX ."_forum_forums WHERE parentid ORDER by position");
		
		while ($row = $db->get_row($get_sub_forums))
		{
			$sub_forum[$row['id']]['id'] = $row['id'];
			$sub_forum[$row['id']]['parentid'] = $row['parentid'];
			$sub_forum[$row['id']]['name'] = $row['name'];
		}
		
		$mod_forums = $db->query("SELECT mid, forum_id, member_name, member_id FROM ". PREFIX ."_forum_moderators");
		
		while ($row = $db->get_row($mod_forums))
		{
			$moderators_array[$row['mid']]['mid'] = $row['mid'];
			$moderators_array[$row['mid']]['forum_id'] = $row['forum_id'];
			$moderators_array[$row['mid']]['member_name'] = $row['member_name'];
			$moderators_array[$row['mid']]['member_id'] = $row['member_id'];
		}
		
		$result_posi = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_forum_category");
		$result_posi = $result_posi['count'];
		
		$content = $db->query("SELECT c.*, f.* FROM " . PREFIX . "_forum_category AS c LEFT OUTER JOIN ". PREFIX ."_forum_forums AS f ON f.main_id=c.sid AND f.parentid = 0 ORDER BY c.posi, f.position");
		
		forum_menu();
		
		echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=category_sort\">";
		
		for ($i = 0; $row = $db->get_row($result_content); $i = $row['sid'])
		{	
			if ($row['sid'] != $i)
			{
				$ch_close++;
				
				if ($ch_close != 1) echo_bottom('cat');
				
				echo_top('cat');
				
				$category_menu = "<a onClick=\"return dropdownmenu(this, event, MenuCategory('".$row['sid']."'), '170px')\" href=\"#\"><img src=\"system/skins/images/browser_action.gif\" border=\"0\"></a>";
				
				$category_posi = category_posi($row['posi'], $row['sid'], $result_posi);
				
				$row['cat_name'] = stripcslashes($row['cat_name']);
				
				echo "<table width='100%' class='navigation'>
				<tr>
				<td width='95%' bgcolor='#EFEFEF' height='32' style='padding-left:10px;'><b>{$row['cat_name']}</b></td>
				<td width='5%' bgcolor='#EFEFEF' height='32' style='padding-left:10px;'>{$category_posi}</td>
				<td width='5%' bgcolor='#EFEFEF' height='32' style='padding-left:10px;'>{$category_menu}&nbsp;</td>
				</tr>
				</table><div class='unterline'></div>";
			}
			
			$row['name'] = stripcslashes($row['name']);
			
			if ($row['name'])
			{	
				if ($sub_forum)
				{
				 
				foreach ($sub_forum as $value)
				{
					if ($row['id'] == $value['parentid'])
					{
						$symbol_count++;
						
						if ($symbol_count > 1) $symbol = ", ";
						
						$value['name'] = stripcslashes($value['name']);
						
						$subforum .= $symbol."<a href='{$PHP_SELF}?mod=forum&action=forum_edit&id={$value['id']}'>{$value['name']}</a>";
					}
				}
				}
				
				if ($subforum)
				{
					$subforum = "<br />&nbsp;-&nbsp;Подфорумы:&nbsp;".$subforum;
				}
				
				if ($row['moderators'] and $moderators_array)
				{
					$moderators_id = explode(":",$row['moderators']);
					
					foreach ($moderators_id as $u_id)
					{
						foreach ($moderators_array as $key => $value)
						{
							if ($moderators_array[$key]['member_id'] == $u_id and $moderators_array[$key]['forum_id'] == $row['id'])
							{
								$moderators .= "<a onClick=\"return dropdownmenu(this, event, Moderators('".$key."'), '100px')\" href=\"#\">{$moderators_array[$key]['member_name']}</a>&nbsp;";
							}
						}
					}
				}
				
				if ($moderators)
				{
					$moderators = "<br />&nbsp;Модераторы:&nbsp;".$moderators;
				}
				
				$forum_menu = "<a onClick=\"return dropdownmenu(this, event, MenuForum('".$row['id']."'), '170px')\" href=\"#\"><img src=\"system/skins/images/browser_action.gif\" border=\"0\"></a>";
				
				if ($row['description'])
				{
					$row['description'] = stripcslashes($row['description']);
					
					$description = "<br /><span class=\"quick\">".$row['description']."</span>";
				}
				else
				{
					$description = '';
				}
				
				echo "<table width='100%' class=\"quick\" cellspacing=1>
				<tr>
				<td width='94%' height='32'><b>{$row[name]}</b>{$description}{$subforum}{$moderators}</td>
				<td width='1%' height='32'></td>
				<td width='5%' height='32'>{$forum_menu}&nbsp;</td>
				</tr>
				<tr><td background=\"system/skins/images/mline.gif\" height=1 colspan=7></td></tr>
				</table>";
				
				unset ($subforum);
				
				unset ($moderators);
				
				unset ($symbol_count);
				
				unset ($symbol);
			}
			
		}
		
		echo_bottom('cat');
		
		echo_top('action');
		
		echo "<div align=\"right\"><a href=\"?mod=forum&action=category\"><input onclick=\"document.location='?mod=forum&action=category'\" class=\"buttons\" style=\"width:150px;\" type=\"button\" value=\"{$f_lg['cat_button']}\"></a>&nbsp;<input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_sort']}\"></div>";
		
		echo "</form>";
		
		echo_bottom('action');
        echo_bottom();
		
		break;
		
// ********************************************************************************
// CONTENT -> FORUMS
// ********************************************************************************
		case "content_forums":
		
		$sub_forums = $db->query("SELECT id, parentid, name, description FROM ". PREFIX ."_forum_forums WHERE parentid ORDER by position");
		
		$sub_forum = array();
		
		while ($row = $db->get_row($get_sub_forums))
		{
			$sub_forum[$row['id']] = array ();
			
			foreach ($row as $key => $value)
			{
				$sub_forum[$row['id']][$key] = $value;
			}
		}
		
		$result_posi = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_forum_forums WHERE main_id = '$sid' AND parentid = 0");
		
		$result_posi = $result_posi['count'];
		
		$content = $db->query("SELECT * FROM " . PREFIX . "_forum_category LEFT JOIN ". PREFIX ."_forum_forums ON ". PREFIX ."_forum_category.sid=". PREFIX ."_forum_forums.main_id WHERE parentid = 0 AND main_id = '$sid' ORDER BY posi, position");
		
		echo_top();
		
		forum_menu();
		
		echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=forum_sort&in_cid=$sid\">";
		
		echo_top('content');
		
		for ($i = 0; $row = $db->get_row($content); $i = $row['sid'])
		{
			if ($row['sid'] != $i)
			{
				$row['cat_name'] = stripcslashes($row['cat_name']);
				
				echo_title($row['cat_name']);
			}
			
			if ($row['name'])
			{
				foreach ($sub_forum as $value)
				{
					if ($row['id'] == $value['parentid'])
					{
						$value['name'] = stripcslashes($value['name']);
						
						$subforum .= "<a href='{$PHP_SELF}?mod=forum&action=forum_edit&id={$value['id']}'>{$value['name']}</a>,&nbsp;";
					}
				}
				
				if ($subforum)
				{
					$subforum = "<br />&nbsp;-&nbsp;Подфорумы:&nbsp;".$subforum;
				}
				
				$forum_posi = category_posi($row['position'], $row['id'], $result_posi);
				
				$forum_menu = "<a onClick=\"return dropdownmenu(this, event, MenuForum('".$row['id']."'), '170px')\" href=\"#\"><img src=\"system/skins/images/browser_action.gif\" border=\"0\"></a>";
				
				if ($row['description'])
				{
					$row['description'] = stripcslashes($row['description']);
					
					$description = "<br /><span class=\"quick\">".$row['description']."</span>";
				}
				
				$row['name'] = stripcslashes($row['name']);
				
				echo "<table width='100%' class=\"quick\" cellspacing=1>
				<tr>
				<td width='90%' height='32'><b>{$row['name']}</b>{$description}{$subforum}</td>
				<td width='5%' height='32'>{$forum_posi}</td>
				<td width='5%' height='32'>{$forum_menu}&nbsp;</td>
				</tr>
				<tr><td background=\"system/skins/images/mline.gif\" height=1 colspan=7></td></tr>
				</table>";
			}
		}
		
		echo_bottom('content');
		
		echo_top('action');
		
		echo "<div align=\"right\"><a href=\"?mod=forum&action=forum&sid=$sid\"><input onclick=\"document.location='?mod=forum&action=forum&sid=$sid'\" class=\"buttons\" style=\"width:150px;\" type=\"button\" value=\"{$f_lg['forum_button']}\"></a>&nbsp;<input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_sort']}\"></div>";
		
		echo "</form>";
		
		echo_bottom('action');
		
		echo_bottom();
		
		break;
		
// ********************************************************************************
// FORUM -> SUB FORUMS
// ********************************************************************************
		case "content_sub_forums":
		
		$forum_row = $db->super_query("SELECT * FROM ". PREFIX ."_forum_forums WHERE id = '$id'");
		
		$sid = $forum_row['main_id'];
		
		$sub_forums = $db->query("SELECT * FROM ". PREFIX ."_forum_forums WHERE parentid = '$id' ORDER by position");
		
		$result_posi = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_forum_forums WHERE parentid = '$id'");
		
		$result_posi = $result_posi['count'];
		
		echo_top();
		
		echo_top('content');
		
		forum_menu();
		
		echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=forum_sort\">";
		
		$forum_row['name'] = stripcslashes($forum_row['name']);
		
		echo_title($forum_row['name']);
		
		while ($row = $db->get_row($sub_forums))
		{
			$forum_posi = category_posi($row['position'], $row['id'], $result_posi);
			
			$forum_menu = "<a onClick=\"return dropdownmenu(this, event, MenuForum('".$row['id']."', 'not'), '170px')\" href=\"#\"><img src=\"system/skins/images/browser_action.gif\" border=\"0\"></a>";
			
			if ($row['description'])
			{
				$row['description'] = stripcslashes($row['description']);
				
				$description = "<br /><span class=\"quick\">".$row['description']."</span>";
			}
			
			$row['name'] = stripcslashes($row['name']);
			
			echo "<table width='100%' class=\"quick\" cellspacing=1>
			<tr>
			<td width='90%' height='32'><b>{$row['name']}</b>{$description}{$subforum}</td>
			<td width='5%' height='32'>{$forum_posi}</td>
			<td width='5%' height='32'>{$forum_menu}&nbsp;</td>
			</tr>
			<tr><td background=\"system/skins/images/mline.gif\" height=1 colspan=7></td></tr>
			</table>";
		}
		
		echo_bottom('content');
		
		echo_top('action');
		
		echo "<div align=\"right\"><a href=\"?mod=forum&action=forum&sid=$sid&sub_id=$id\"><input onclick=\"document.location='?mod=forum&action=forum&sid=$sid&sub_id=$id'\" class=\"buttons\" style=\"width:150px;\" type=\"button\" value=\"{$f_lg['forum_button']}\"></a>&nbsp;<input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_sort']}\"></div>";
		
		echo "</form>";
		
		echo_bottom('action');
		
		echo_bottom();
		
		break;

// ********************************************************************************
// TOOLS
// ********************************************************************************
		case "tools":

		echo_top();
		
		echo "<form action=\"{$PHP_SELF}?mod=forum&action=tools_save\" method=\"post\">";
	
		require_once SYSTEM_DIR.'/inc/forum/tools.php';
		
		echo_top('tools_save');
		
		echo "<input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_save']}\"></form>";
		
		echo_bottom('tools_save');
		
		echo_bottom();
		
		break;
		
// ********************************************************************************
// TOOLS SAVE
// ********************************************************************************
		case "tools_save":

		if (!count($search_captcha)) {$search_captcha = array (); $search_captcha[] = '0';}
		$save_con[search_captcha] = $db->safesql(implode(':', $search_captcha));
		
		if (!count($topic_captcha)) {$topic_captcha = array (); $topic_captcha[] = '0';}
		$save_con[topic_captcha] = $db->safesql(implode(':', $topic_captcha));
		
		if (!count($post_captcha)) {$post_captcha = array (); $post_captcha[] = '0';}
		$save_con[post_captcha] = $db->safesql(implode(':', $post_captcha));
		
		if (!count($tools_upload)) {$tools_upload = array (); $tools_upload[] = '0';}
		$save_con[tools_upload] = $db->safesql(implode(':', $tools_upload));
		
		if (!count($tools_poll)) {$tools_poll = array (); $tools_poll[] = '0';}
		$save_con[tools_poll] = $db->safesql(implode(':', $tools_poll));
		
		$find[] 	= "'\r'";
		$replace[] 	= "";
		$find[] 	= "'\n'";
		$replace[] 	= "";
		
		$save_con = $save_con + $forum_config;
		
		$handler = fopen(SYSTEM_DIR.'/data/forum_config.php', "w");
		
		fwrite($handler, "<?PHP \n\n//System Configurations\n\n\$forum_config = array (\n\n");
		
		foreach($save_con as $name => $value)
		{
			$value=trim(stripslashes ($value));
			$value=htmlspecialchars ($value, ENT_QUOTES);
			$value = preg_replace($find,$replace,$value);
			fwrite($handler, "'{$name}' => \"{$value}\",\n\n");
		}
		
		fwrite($handler, ");\n\n?>");
		fclose($handler);
		
		msg("info", $f_lg['t_f_save'], "$f_lg[t_f_save1]<br /><br /><a href=$PHP_SELF?mod=forum>$f_lg[db_prev]</a>");
		
		break;

// ********************************************************************************
// RANK
// ********************************************************************************
		case "rank":
		
		$result = $db->query("SELECT * FROM " . PREFIX . "_forum_titles");
		
		echo_top();
		
		echo_title($f_lg['titles_main']);
		
		echo_rank_style ();
		
		echo "<table width=\"100%\" border=\"0\"><tr>
        <td style=\"padding:2px;\" width=\"25%\" height=\"22\"><b>{$f_lg['titles_name']}</b></td>
        <td width=\"25%\" height=\"24\"><center><b>{$f_lg['titles_pots']}</b></center></td>
        <td width=\"25%\" height=\"24\"><center><b>{$f_lg['titles_pips']}</b></center></td>
        <td width=\"25%\" height=\"24\"><center><b>{$f_lg['titles_action']}<b></center></td></tr>
		</table><div class='unterline'></div><table width=\"100%\" border=\"0\">";
		
		while ($row = $db->get_row($result))
		{
			$t_action = "[<a href=\"$PHP_SELF?mod=forum&action=rank_edit&id={$row['id']}\">{$f_lg['label_edit']}</a>]"." [<a href=\"$PHP_SELF?mod=forum&action=rank_del&id={$row['id']}\">{$f_lg['label_del']}</a>]";
			
			$rating = $row['pips'] * 17;
			
			$rank_image = "<div class=\"rank\" style=\"display:inline;\">
			<ul class=\"unit-rank\">
			<li class=\"current-rank\" style=\"width:{$rating}px;\">{$rating}</li>
			</ul>
			</div>";
			
			echo "<tr>
			<td style=\"padding:2px;\" width=\"25%\" height=\"22\">{$row['title']}</td>
			<td width=\"25%\" height=\"24\"><center>{$row['posts']}</center></td>
			<td width=\"25%\" height=\"24\"><center>{$rank_image}</center></td>
			<td width=\"25%\" height=\"24\"><center>{$t_action}</center></td></tr>
			<tr><td background=\"system/skins/images/mline.gif\" height=1 colspan=7></td></tr>";
		}
		
		echo "</table>";
		
		echo_bottom('w');
		
		echo_top('w');
		
		echo_title($f_lg['titles_uadd']);
		
		echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=rank_user\">
		<table border=\"0\" width=\"100%\">
		<tr>
		<td width=\"260\">{$f_lg['titles_uname']}</td>
		<td><input class=\"edit\" type=\"text\" name=\"user_name\" value=\"\" size=\"27\"></td></tr>
		<tr>
		<td width=\"260\">{$f_lg['titles_urname']}</td>
		<td><input class=\"edit\" type=\"text\" name=\"user_rank\" value=\"\" size=\"27\"></td></tr>
		<tr>
		<td width=\"260\">{$f_lg['titles_npips']}</td>
		<td><input class=\"edit\" type=\"text\" name=\"user_pips\" value=\"\" size=\"27\"></td></tr>
		<tr><td colspan=\"2\"><div class=\"hr_line\"></div></td></tr>
		<tr>
		<td width=\"260\">&nbsp;</td>
		<td><input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_add']}\"></td></tr>
		</table></form>";
		
		echo_bottom('w');
		
		echo_top('w');
		
		echo_title($f_lg['titles_add']);
		
		echo_rank('new');
		
		echo_bottom();
		
		break;
		
		case "rank_add":
		
		if ($t_name)
		{
			$db->query("INSERT INTO " . PREFIX . "_forum_titles (posts, title, pips) values ('$t_posts', '$t_name', '$t_pips')");
			
			$fcache->delete('rank_array');
			
			header("Location: ?mod=forum&action=rank");
		}
		
		break;
		
		case "rank_edit":
		
		echo_top();
		
		echo_title($f_lg['titles_edit']);
		
		echo_rank('edit', $id);
		
		echo_bottom();
		
		break;
		
		case "rank_save":
		
		if ($t_name and $id)
		{
			$db->query("UPDATE " . PREFIX . "_forum_titles SET title = '$t_name', posts = '$t_posts', pips = '$t_pips' WHERE id = '$id'");
			
			$fcache->delete('rank_array');
			
			header("Location: ?mod=forum&action=rank");
		}
		
		break;
		
		case "rank_del":
		
		$db->query("DELETE FROM " . PREFIX . "_forum_titles WHERE id = '$id' LIMIT 1");
		
		$fcache->delete('rank_array');
		
		header("Location: ?mod=forum&action=rank");
		
		break;
		
		case "rank_user":
		
		$user_pips = intval($user_pips);
		
		$db->query("SELECT * FROM " . USERPREFIX . "_users where name = '$user_name'");
		
		if ($db->num_rows())
		{
			$db->query("UPDATE " . PREFIX . "_users SET forum_rank = '$user_rank', forum_pips = '$user_pips' WHERE name = '$user_name'");
			
			header("Location: ?mod=forum&action=rank");
		} else 
		{
			msg("info",$f_lg['error'], $f_lg['titles_error_name'], "javascript:history.go(-1)");
		}
		
		break;
		
// ********************************************************************************
// NEW MODERATOR
// ********************************************************************************		
		case "moderator_new":
		
		if ($user_found)
		{
			$user_found = $db->super_query("SELECT * FROM " . USERPREFIX . "_users WHERE name = '$user_found'");
			
			$uid = $user_found['user_id'];
		}
		
		if ($user_found['name'])
		{
			echo_top();
			
			echo_title($f_lg['mod_config_set']);
			
			echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=moderator_add&fid=$id&uid=$uid\">";
			
			echo "<table width=\"100%\">";
			
			require_once SYSTEM_DIR.'/forum/admin/moderation.php';
			
			echo "</table>";
			
			echo "<br /><input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_save']}\">";
			
			echo "</form>";
			
			echo_bottom();
		}
		
		else
		{
			echo_top();
			
			echo_title($f_lg['mod_search_user']);
			
			echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=moderator_new&id=$id\">";
			
			echo "<table border=\"0\" width=\"100%\">
			<tr>
			<td style=\"padding:4px\" class=\"option\" width=\"260\">{$f_lg['mod_search_name']}</td>
			<td><input class=\"edit\" type=\"text\" name=\"user_found\" value=\"\" size=\"27\"></td></tr>
			<tr>
			<td width=\"260\">&nbsp;</td>
			<td><input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_search']}\"></td></tr>
			</table>";
			
			echo "</form>";
			
			echo_bottom();
		}
		
		break;
		
		case "moderator_add":
		
		$user_found = $db->super_query("SELECT * FROM " . USERPREFIX . "_users WHERE user_id = '$uid'");
		
		if ($user_found['user_id'] and $fid)
		{
			$row = $db->super_query("SELECT * FROM " . PREFIX . "_forum_moderators WHERE member_id = '$uid' and forum_id = '$fid'");
			
			$moderator_id = $row['mid'];
			
			if (!$moderator_id)
			{
				$db->query("INSERT INTO " . PREFIX . "_forum_moderators (member_id, member_name, forum_id) values ('$uid', '$user_found[name]', '$fid')");
				
				$moderator_id = $db->insert_id();
			}
			
			$db->query("UPDATE " . PREFIX . "_forum_moderators SET edit_post = '$save[edit_post]', delete_topic = '$save[delete_topic]', edit_topic = '$save[edit_topic]', edit_post = '$save[edit_post]', delete_post = '$save[delete_post]', open_topic = '$save[open_topic]', close_topic = '$save[close_topic]', delete_post = '$save[delete_post]', move_topic = '$save[move_topic]', pin_topic = '$save[pin_topic]', delete_topic = '$save[delete_topic]', unpin_topic = '$save[unpin_topic]', mass_prune = '$save[mass_prune]', combining_post = '$save[combining_post]', move_post = '$save[move_post]' WHERE mid = '$moderator_id'");
			
			$new_row = $db->query("SELECT mid, member_id FROM " . PREFIX . "_forum_moderators WHERE forum_id = '$fid'");
			
			while ($row = $db->get_row($new_row))
			{
				$update_uid[$row['member_id']] = $row['member_id'];
			}
			
			$update_uid = implode(':', $update_uid);
			
			$db->query("UPDATE " . PREFIX . "_forum_forums SET moderators = '$update_uid' WHERE id = '$fid'");
			
			$fcache->delete('forum_moderators');
			$fcache->delete('forums_array');
			
			msg("info",$f_lg['mod_add'], $f_lg['mod_add2'], "?mod=forum&action=content");
		}
		
		break;
		
		case "moderator_edit":
		
		if (intval($id))
		{
			$row = $db->super_query("SELECT * FROM " . PREFIX . "_forum_moderators WHERE mid = '$id'");
			
			$moderator_edit = true;
			
			echo_top();
			
			echo_title($f_lg['mod_config_set']);
			
			echo "<form method=\"post\" action=\"$PHP_SELF?mod=forum&action=moderator_save&id=$id\">";
			
			echo "<table width=\"100%\">";
			
			require_once SYSTEM_DIR.'/forum/admin/moderation.php';
			
			echo "</table>";
			
			echo "<br /><input type=\"submit\" class=\"buttons\" value=\"{$f_lg['button_edit']}\">";
			
			echo "</form>";
			
			echo_bottom();
		}
		
		break;
		
		case "moderator_save":
		
		if ($id)
		{
			$db->query("UPDATE " . PREFIX . "_forum_moderators SET edit_post = '$save[edit_post]', delete_topic = '$save[delete_topic]', edit_topic = '$save[edit_topic]', edit_post = '$save[edit_post]', delete_post = '$save[delete_post]', open_topic = '$save[open_topic]', close_topic = '$save[close_topic]', delete_post = '$save[delete_post]', move_topic = '$save[move_topic]', pin_topic = '$save[pin_topic]', delete_topic = '$save[delete_topic]', unpin_topic = '$save[unpin_topic]', mass_prune = '$save[mass_prune]' WHERE mid = '$id'");
			
			$fcache->delete('forum_moderators');
			
			msg("info",$f_lg['mod_edit_ok'], $f_lg['mod_edit_ok2'], "?mod=forum&action=content");
		}
		
		break;
		
		case "moderator_del":
		
		if ($id)
		{
			$row = $db->super_query("SELECT * FROM " . PREFIX . "_forum_moderators WHERE mid = '$id'");
			
			$fid = $row['forum_id'];
			
			if ($row['member_id'])
			{
				$db->query("DELETE FROM " . PREFIX . "_forum_moderators WHERE mid = '$id'");
				
				$row_forum = $db->super_query("SELECT moderators FROM " . PREFIX . "_forum_forums WHERE id = '$fid'");
				
				$new_row = $db->query("SELECT mid, member_id FROM " . PREFIX . "_forum_moderators WHERE forum_id = '$fid'");
				
				while ($row = $db->get_row($new_row))
				{
					$update_uid[$row['member_id']] = $row['member_id'];
				}
				
				$update_uid = implode(':', $update_uid);
				
				$db->query("UPDATE " . PREFIX . "_forum_forums SET moderators = '$update_uid' WHERE id = '$fid'");
				
				$fcache->delete('forum_moderators');
				$fcache->delete('forums_array');
				
				header("Location: ?mod=forum&action=content");
			}
		}
		
		break;

// ********************************************************************************
// HELP
// ********************************************************************************
		case "help":
		
		echoheader("", "");
		
		echo_top('about');
		
		echo_title("DLE Forum Version 2.4 Build 1");
		
		$licence_file = @file_get_contents(SYSTEM_DIR.'/forum/sources/components/licence.odf');
		
		echo "<div class=\"quick\">".urldecode($licence_file)."</div>";
		
		echo_bottom('about');
		
		echo_top('authors');
		
		echo_title("Developers");
		
		echo "<div class=\"quick\">";
		
		echo "<b>Author & Developer:</b>&nbsp;&nbsp;Vadim Shestakov [ShVad]<br /><br />";
		
		echo "<hr><br / >";
		
		echo "<center><b>WebMoney:</b>&nbsp;&nbsp; R164056126473 &nbsp;&nbsp; Z217028877385 &nbsp;&nbsp; E194208009125 &nbsp;&nbsp; U374273141801<br /><br />Copyright 2008 &copy; <a href=\"http://www.dle-files.ru\" target=\"_blank\">DLE Files Group</a>. All rights reserved.</center>";
		
		echo "</div>";
		
		echo_bottom('authors');
		
		echofooter();
		
		break;
		
// ********************************************************************************
// EMAIL
// ********************************************************************************
		case "email":
		
		require_once SYSTEM_DIR.'/inc/forum/email.php';
		
		break;
		
// ********************************************************************************
// USER GROUP
// ********************************************************************************
		case "usergroup":
		
		require_once SYSTEM_DIR.'/inc/forum/usergroup.php';
		
		break;
		
// ********************************************************************************
// SERVICE
// ********************************************************************************
		case "service":
		
		require_once SYSTEM_DIR.'/inc/forum/service.php';
		
		break;
		
// ********************************************************************************
// CLEAR CACHE
// ********************************************************************************
		case "clear":
		
		$fcache->delete();
		
		$fcache->clear();
		
		header("Location: ?mod=forum");
		
		break;
	}
	
?>