<?php
/*
=====================================================
 Модуль создал: REZER (http://rezer.net)
=====================================================
 Файл: board.php
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Назначение: ядро модуля DLE Board
=====================================================
*/

if( !defined( "DATALIFEENGINE" ) ) die( "Hacking attempt!" );

//--------------------------------------------------=-=-=-=-=
//	Запускаем класс
//--------------------------------------------------=-=-=-=-=

if( !class_exists( "Board" ) ) require_once( ENGINE_DIR."/inc/board/board.class.php" );

//--------------------------------------------------=-=-=-=-=
//	Настройки модуля
//--------------------------------------------------=-=-=-=-=

$MainLink = $Board->AltUrl ? "/board/" : "/index.php?do=board";
$module['title'] = array( $Board->Config['main_title'] );
$module['speedbar'] = array( "<a href=\"{$MainLink}\">{$Board->Config['main_title']}</a>" );

if( $Board->Config['on'] == "yes" )
	{
		$action = totranslit( $_REQUEST['action'] );
		
		//--------------------------------------------------=-=-=-=-=
		//	В каком городе находится пользователь
		//--------------------------------------------------=-=-=-=-=
		
		if( $Board->Config['region_on'] == "on" )
			{
				$OkRegion = false;
				if( isset( $_REQUEST['select_region'] ) )
					{
						$CountryID = intval( $_REQUEST['country'] );
						$CityID = intval( $_REQUEST['city'] );
						$Board->LoadListCity( $CountryID );
						
						if( $Board->DB['country'][ $CountryID ] && $Board->DB['city'][ $CountryID ][ $CityID ] )
							{
								$UserCountry = htmlspecialchars( stripslashes( $Board->DB['country'][ $CountryID ]['name'] ) );
								$UserCity = htmlspecialchars( stripslashes( $Board->DB['city'][ $CountryID ][ $CityID ]['name'] ) );
								$SqlRegion = "AND `country`='{$CountryID}' AND `city`='{$CityID}'";
								
								@session_register( "BoardCountry" );
								@session_register( "BoardCity" );
								@session_register( "BoardCityName" );
								$_SESSION['BoardCountry'] = $CountryID;
								$_SESSION['BoardCity'] = $CityID;
								$_SESSION['BoardCityName'] = $UserCity;
								$OkRegion = true;
							}
								else
							{
								$_SESSION['BoardCountry'] = "";
								$_SESSION['BoardCity'] = "";
								$_SESSION['BoardCityName'] = "";
							}
					}
					
				if( $OkRegion !== true )
					{
						if( $_SESSION['BoardCountry'] != "" && $_SESSION['BoardCity'] != "" && $_SESSION['BoardCityName'] != "" )
							{
								$CountryID = intval( $_SESSION['BoardCountry'] );
								$CityID = intval( $_SESSION['BoardCity'] );
								if( $Board->DB['country'][ $CountryID ] )
									{
										$OkRegion = true;
										$UserCountry = htmlspecialchars( stripslashes( $Board->DB['country'][ $CountryID ]['name'] ) );
										$UserCity = htmlspecialchars( stripslashes( $_SESSION['BoardCityName'] ) );
										$SqlRegion = "AND `country`='{$CountryID}' AND `city`='{$CityID}'";
									}
							}
						
						if( $OkRegion !== true )
							{
								$UserCountry = "Все страны";
								$UserCity = "Все регионы";
								$SqlRegion = "";
							}
					}
			}
		
		//--------------------------------------------------=-=-=-=-=
		//	Грузим файлы
		//--------------------------------------------------=-=-=-=-=
		
		$BoardLoadMainTpl = false;
		$BoardAllowSearch = true;
		switch( $action ){
			
			case "post":
				include( ENGINE_DIR."/modules/board/full.post.php" );
				break;
				
			case "add":
				include( ENGINE_DIR."/modules/board/add.post.php" );
				break;
				
			case "edit":
				include( ENGINE_DIR."/modules/board/edit.post.php" );
				break;
				
			case "del":
				include( ENGINE_DIR."/modules/board/del.post.php" );
				break;
				
			case "my":
				include( ENGINE_DIR."/modules/board/my.post.php" );
				break;
				
			case "userpost":
				include( ENGINE_DIR."/modules/board/user.post.php" );
				break;
				
			case "notepad":
				include( ENGINE_DIR."/modules/board/notepad.post.php" );
				break;
				
			case "boardcat":
				include( ENGINE_DIR."/modules/board/category.php" );
				break;
				
			case "fullsearch":
				include( ENGINE_DIR."/modules/board/fullsearch.php" );
				break;
				
			default:
				$action = "main";
				include( ENGINE_DIR."/modules/board/main.php" );
				break;
		}
		
		//--------------------------------------------------=-=-=-=-=
		//	Обработка сгенерированного
		//--------------------------------------------------=-=-=-=-=
		
		if( $BoardLoadMainTpl === true )
			{
				$SelectBoardType = $Board->ReturnSelectBoardType( $BoardType, $BoardCategory );
				$MyBoardPost = $Board->AltUrl ? "/board/my" : "/index.php?do=board&action=my";
				$MyBoardNotepad = $Board->AltUrl ? "/board/notepad" : "/index.php?do=board&action=notepad";
				$AddBoardPost = $Board->ReturnLinkAddPost();
				$FullBoardSearch = "/index.php?do=board&action=fullsearch";
				$SelectCountry = $Board->ReturnSelectCountry( $_SESSION['BoardCountry'] );
				$SelectCity = $Board->ReturnSelectCity( $_SESSION['BoardCountry'], $_SESSION['BoardCity'] );
				
				$Folder = $Folder != "" ? $Folder : "default";
				$tpl->Load_Template( "board/{$Folder}/main.tpl" );
				$tpl->set( "{cats}", $cats );
				$tpl->set( "{my_board}", $MyBoardPost );
				$tpl->set( "{my_notepad}", $MyBoardNotepad );
				$tpl->set( "{add_post}", $AddBoardPost );
				$tpl->set( "{full_search_link}", $FullBoardSearch );
				$tpl->set( "{content}", $tpl->result['board_content'] );
				$tpl->set( "{Search:boardtype}", "<select name=\"board_type\">{$SelectBoardType}</select>" );
				$tpl->set( "{MainLink}", $MainLink );
				$tpl->set( "{rssLink}", $rssLink );
				$tpl->set( "{UserCountry}", $UserCountry );
				$tpl->set( "{UserCity}", $UserCity );
				$tpl->set( "{UserCityID}", intval( $_SESSION['BoardCity'] ) );
				
				$tpl->set( "{SelectCountry}", "<select name=\"country\" id=\"SelectListCountry\" onkeyup=\"SelectCountry( '', 'Выберите' );\" onkeydown=\"SelectCountry( '', 'Выберите' );\" onchange=\"SelectCountry( '', 'Выберите' );\"><option value=\"\">- Без разницы -</option>{$SelectCountry}</select>" );
				$tpl->set( "{SelectCity}", "<span id=\"FormSelectCity\"><select name=\"city\" id=\"SelectCity\"><option value=\"\">- Без разницы -</option>{$SelectCity}</select> <span id=\"FormSelectCityStatus\"></span></span>" );
				$tpl->set_block( "'\\[SelectRegion\\](.*?)\\[/SelectRegion\\]'si", "<form method=\"post\" action=\"\">\\1</form>" );
				
				$tpl->set_block( "'\\[cats\\](.*?)\\[/cats\\]'si", ( $cats ? "\\1" : "" ) );
				$tpl->set_block( "'\\[rss\\](.*?)\\[/rss\\]'si", ( $rssLink ? "\\1" : "" ) );
				$tpl->set_block( "'\\[allow_search\\](.*?)\\[/allow_search\\]'si", ( $BoardAllowSearch === true ? "\\1" : "" ) );
				
				$SearchTextForm = htmlspecialchars( $SearchTextForm );
				$tpl->set_block( "#{search:text->(.+?)}#is", ( ( $SearchTextForm != "" ) ? $SearchTextForm : "\\1" ) );
				
				$tpl->compile( "board" );
				$tpl->clear();
			}
		
		$tpl->result['content'] = $tpl->result['content'].$tpl->result['board'];
		$tpl->result['content'] = preg_replace( "#\\[(Not|Yes)BoardAllow:(.+?)\\](.+?)\\[/(Not|Yes)BoardAllow\\]#ies", "\$Board->ReplaceTagBlock( '\\2', '\\3', '\\1' )", $tpl->result['content'] );
		$tpl->result['content'] = preg_replace( "#\\[(Not|Yes)BoardAllowCat:(.+?)\\](.+?)\\[/(Not|Yes)BoardAllowCat\\]#ies", "\$Board->ReplaceTagBlockCat( '\\2', '\\3', '\\1' )", $tpl->result['content'] );
		
		if( $Board->Config['region_on'] == "on" )
			{
				$tpl->result['content'] = preg_replace( "#\\[allow_region\\]([^\\[]*)\\[/allow_region\\]#is", "\\1", $tpl->result['content'] );
				$tpl->result['content'] = preg_replace( "#\\[not-allow_region\\]([^\\[]*)\\[/not-allow_region\\]#is", "", $tpl->result['content'] );
			}
				else
			{
				$tpl->result['content'] = preg_replace( "#\\[allow_region\\]([^\\[]*)\\[/allow_region\\]#is", "", $tpl->result['content'] );
				$tpl->result['content'] = preg_replace( "#\\[not-allow_region\\]([^\\[]*)\\[/not-allow_region\\]#is", "\\1", $tpl->result['content'] );
			}
		
		unset( $tpl->result['board'] );
	}
		else
	{
		msgbox( "Информация", "Объявления в данный момент отключены администрацией сайта." );	
	}

//--------------------------------------------------=-=-=-=-=
//	Метатег <title> и модуль SpeedBar
//--------------------------------------------------=-=-=-=-=

$NewTitle = array();
for( $i = count( $module['title'] ); $i > 0; $i-- ) $NewTitle[] = $module['title'][ $i - 1 ];
$metatags['header_title'] = implode( " &raquo; ", $NewTitle )." &raquo; ".$config['home_title'];
$module['speedbar'] = implode( " &raquo; ", $module['speedbar'] );
$module['title'] = "";

?>