<?php
if(!defined('DATALIFEENGINE')){die("Hacking attempt!");}
	$act = $_REQUEST['act'];
    
	require_once SYSTEM_DIR.'/forum/sources/components/init.php';
	
	switch ($act)
	{

// ********************************************************************************
// SHOW INDEX
// ********************************************************************************		
		case "":
		
		if ($forum_config['hide_forum']){
            $access_hide = "and access_read regexp '[[:<:]](".$member_id['user_group'].")[[:>:]]'";
        }else{
			$access_hide = "";
		}
		
		$result_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' {$access_hide} ORDER BY posi, position");
		
		for ($i = 0; $row = $db->get_row($result_content); $i = $row['sid'])
		{
			if ($row['name'])
			{
				if (!$fcache->open('sub-'.$row['id']))
				{
					if ($sub_forums_array != 'empty')
					{
						foreach ($sub_forums_array as $value)
						{
							if ($row['id'] == $value['parentid'])
							{
								$symbol_count++;
								
								if ($symbol_count > 1) $symbol = ", ";
								
								$sub_forums .= link_forum($value['id'], $value['name'], $symbol);
							}
						}
					}
					
					$fcache->save('sub-'.$row['id'], $sub_forums);
				}
				else { $sub_forums = $fcache->open('sub-'.$row['id']); }
				
				if (!$fcache->open('moder-'.$row['id']))
				{
					foreach ($forum_moderators as $moderators)
					{
						if ($moderators['forum_id'] == $row['id'] and $moderators['member_name'])
						{
							$mod_count++;
							
							if ($mod_count > 1) $mod_symbol = ", ";
							
							$moderators_list .= $mod_symbol . link_user($moderators['member_name']);
						}
					}
					
					$fcache->save('moder-'.$row['id'], $moderators_list);
				}
				else { $moderators_list = $fcache->open('moder-'.$row['id']); }
				
				$forum_link = link_forum($row['id'], $row['name']);
				
				$tpl->load_template($tpl_dir.'forums.tpl');

                $tpl->set('{forum_id_rss}', $forum_url."/rss/forum_".$row['id'].".xml");				
				$tpl->set('{status}', forum_status ($row['f_last_date'], $row['password'], $row['icon']));				
				$tpl->set('{name}', $forum_link);				
				$tpl->set('{description}', stripslashes($row['description']));
				
				if ($sub_forums){$tpl->set('{forums}', '<br />' . $forum_config['forum_pr_sub'].' '.$sub_forums);
				} else {$tpl->set('{forums}', '');}				
				if ($moderators_list){$tpl->set('{moderators}', '<br />' . $f_lang['moderators'] . ' ' . $moderators_list);
				} else {$tpl->set('{moderators}', '');}
				
				$tpl->set('{topics}', $row['topics']);				
				$tpl->set('{post}', $row['posts']);				
				$tpl->set('{last}', forum_last ($row['f_last_tid'], $row['f_last_title'], $row['f_last_poster_name'], $row['f_last_date'], $row['password'], $row['id'], $row['access_read'], $row['last_post_id']));
				
				$tpl->compile("forums_{$row['main_id']}");				
				$tpl->clear();
				
				unset ($sub_forums);				
				unset ($symbol_count);				
				unset ($symbol);				
				unset ($moderators_list);				
				unset ($mod_count);				
				unset ($mod_symbol);
			}
			
			if ($row['sid'] != $start_id){
				$category_link = link_category($start_id, $start_name);				
				$tpl->load_template($tpl_dir.'category.tpl');				
				$tpl->set('{category}', $category_link);				
				$tpl->set('{forums}', $tpl->result["forums_{$start_id}"]);				
				$tpl->compile('dle_forum');				
				$tpl->clear();
			}
			
			$start_id = $row['sid'];			
			$start_name = stripslashes($row['cat_name']);
		}
		
		$category_link = link_category($start_id, $start_name);
		
		$tpl->load_template($tpl_dir.'category.tpl');
		
		$tpl->set('{category}', $category_link);		
		$tpl->set('{forums}', $tpl->result["forums_{$start_id}"]);
		
		$tpl->compile('dle_forum');		
		$tpl->clear();
		
		if ($forum_config['stats']){require_once SYSTEM_DIR.'/forum/sources/modules/stats.php';}
		
		break;

		case "category":		require_once SYSTEM_DIR.'/forum/sources/category.php';				break;
		case "forum":			require_once SYSTEM_DIR.'/forum/sources/showforum.php';				break;
		case "topic":			require_once SYSTEM_DIR.'/forum/sources/showtopic.php';				break;		
		case "add_topic":		require_once SYSTEM_DIR.'/forum/action/addtopic.php';				break;
		case "getforum":		require_once SYSTEM_DIR.'/forum/action/forum.php';					break;
		case "getnew":			require_once SYSTEM_DIR.'/forum/sources/modules/getnew.php';		break;
		case "search":			require_once SYSTEM_DIR.'/forum/sources/modules/search.php';		break;
		case "subscription":	require_once SYSTEM_DIR.'/forum/sources/modules/subscription.php';	break;
		case "moderation":		require_once SYSTEM_DIR.'/forum/action/moderation.php';				break;
		case "post":			require_once SYSTEM_DIR.'/forum/action/post.php';					break;
		case "_topic":			require_once SYSTEM_DIR.'/forum/action/topic.php';					break;
        case "user_posts":		require_once SYSTEM_DIR.'/forum/sources/shapeshifter/forum_posts.php';	break;

// ********************************************************************************
// ATTACHMENT
// ********************************************************************************	
		case "attachment":
		$file_id = intval($_REQUEST['id']);
		if ($file_id)
		{
			$row = $db->super_query("SELECT * FROM " . PREFIX . "_forum_files WHERE file_id = '$file_id'");
			
			if ($row['file_id'])
			{
				if (count($forums_array))
				{
					$access_download = $forums_array[$row['forum_id']]['access_download'];
					
					if (check_access($access_download))
					{
						if ($row['file_type'] == "file"){
							$FILE_DIR = "files";
						} else {
							$FILE_DIR = "images";
						}
						
						$db->query("UPDATE " . PREFIX . "_forum_files SET dcount = dcount+1 WHERE file_id = '$file_id'");
						@header("Location: {$config['http_home_url']}uploads/forum/{$FILE_DIR}/{$row['onserver']}");
					}
					else
					{
						$group_name = $user_group[$member_id['user_group']]['group_name'];
						forum_msg($f_lang['f_msg'], $f_lang['forum_down'], 'user_group', $group_name);
					}
				}
			}
			else
			{
				@header("HTTP/1.0 404 Not Found");
				forum_msg($f_lang['f_msg'], $f_lang['f_404']);
			}
		}
		
		else
		{
			@header("HTTP/1.0 404 Not Found");
			forum_msg($f_lang['f_msg'], $f_lang['f_404']);
		}
		break;
	}

	if ($forum_config['forum_bar'])
	{
		$forum_bar = "<a href=\"{$forum_url}/\">".$forum_config['forum_title']."</a>";
		if ($bbr_cid and $bbr_name){$forum_bar .= " &raquo; ".link_category($bbr_cid, $bbr_name);}
		if ($bbr_fname and $bbr_fid){$forum_bar .= " &raquo; ".link_forum($bbr_fid, $bbr_fname);}
		if ($bbr_tname and $bbr_tid){$forum_bar .= " &raquo; ".link_topic($bbr_tid, $bbr_tname);}
		if ($bbr_app){$forum_bar .= " &raquo; ".$bbr_app."</a>";}
		$forum_bar = stripslashes($forum_bar);
	}
	
	if (!defined('FORUM_SUB_DOMAIN')){require_once SYSTEM_DIR . $compile_php;}

	if ($forum_config['meta_descr'] and !$meta_topic){$metatags['description'] = $forum_config['meta_descr'];}
	if ($forum_config['meta_keywords'] and !$meta_topic){$metatags['keywords'] = $forum_config['meta_keywords'];}
?>