Просмотр файла includes/modules/search.php

Размер файла: 5.1Kb
<?php
$page = isset( $_GET['page'] ) ? (int) ($_GET['page'] - 1) : 0;

$template_vars['header'] = array(
'TITLE' => $lang['SEARCH'],
'CSS' => $css
);

$template->set_vars('header', $template_vars['header']);

$show_form = true;

if( isset($_REQUEST['q']) )
{
	$q = isset( $_REQUEST['q'] ) ? (string) $_REQUEST['q'] : '';
	$tr = isset( $_REQUEST['tr'] ) ? (bool) $_POST['tr'] : false;
	$logic = isset( $_REQUEST['logic'] ) ? (string) $_REQUEST['logic'] : false;
	
	if( isset($_REQUEST['s']) )
	{	
		if( is_array($_REQUEST['s']) )
		{
			$s = $_REQUEST['s'];
		}
		else
		{
			$s = explode(';', $_REQUEST['s']);	
		}
	}
	else
	{
		$s = array();
	}

	$s = array_map('intval', $s);

	if( !in_array($logic, array('and', 'or')) )
	{
		$logic = 'and';
	}

	$logic = strtoupper( $logic );

	if( get_magic_quotes_gpc() )
	{
		$q = stripslashes( $q );
	}

	if( $tr )
	{
		$q = tr( $q );
	}

	$q = preg_replace('/\s/', ' ', $q);
	$q = preg_replace('/ {2,}/', ' ', $q);
	$q = trim( $q );

	if( $q == '' )
	{
		$template->set_block_vars('body', 'error', array('MESSAGE' => $lang['ERR_SEARCH_EMPTY_QUERY']));
	}
	else if( empty($s) )
	{
		$template->set_block_vars('body', 'error', array('MESSAGE' => $lang['ERR_SEARCH_INVALID_SECTION_LIST']));
	}
	else
	{
		$q = explode(' ', $q);

		$escape_array = array(
		"\x2a" => "", //*
		"\x22" => "", //"
		"\x2b" => "", //+
		"\x2d" => "", //-
		"\x7e" => "", //~
		"\x28" => "", //(
		"\x29" => "", //)
		"\x3c" => "", //<
		"\x3e" => ""  //>
		);

		for($i = 0, $query = array(); $i < count( $q ); $i++)
		{
			$query[] = sprintf($lang['SEARCH_WORD'], output($q[$i]));
			$q[$i] = strtr($q[$i], $escape_array);
		}

		$query = implode(' ' . $lang[$logic] . ' ', $query);

		for($i = 0, $part_of_query = '', $sep = ''; $i < count( $q ); $i++, $sep = $logic)
		{
			$part_of_query .= $sep . " MATCH(`title`, `description`) AGAINST('" . $sql->escape_string($q[$i]) . "*' IN BOOLEAN MODE) ";
		}
	
		if( !$sql->query("SELECT COUNT(*) FROM `" . CATALOGUE_SITES . "` WHERE " . $part_of_query . " AND `section_id` IN(" . implode(', ', $s) . ") AND `checked` = 1;") )
		{
			put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
		}

		$total = $sql->result($sql->result, 0);

		if( $total == 0 )
		{
			$template->set_block_vars('body', 'error', array('MESSAGE' => sprintf($lang['ERR_SEARCH_NO_RESULTS'], $query)));
		}
		else
		{
			$template->load_template('templates/' . VERSION . '/search_results.tpl', 'body');

			$maxpage = maxpage($total, SEARCH_ONPAGE);
			$limit = limit($page, SEARCH_ONPAGE);

			if( !$sql->query("SELECT * FROM `" . CATALOGUE_SITES . "` WHERE " . $part_of_query . " AND `section_id` IN(" . implode(', ', $s) . ") AND `checked` = 1 ORDER BY `id` ASC LIMIT " . $limit . ";") )
			{
				put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
			}

			while( $site = $sql->fetch_assoc() )
			{
				$template->set_block_vars('body', 'search_result', array(
				'LINK_OUT' => gen_uri('out', $site['id'], $nocache, false, false),
				'TITLE' => output($site['title']),
				'SITE' => output($site['site']),
				'DESCRIPTION' => output($site['description'])
				));
			}

			$pages = pages($page, $maxpage, SEARCH_ONPAGE, 2, 'q=' . urlencode(implode(' ', $q)) . '&amp;s=' . urlencode(implode(';', $s)) . '&amp;logic=' . strtolower($logic), gen_uri($m));

			$template_vars['body'] = array(
			'RESULTS' => sprintf($lang['SEARCH_RESULTS'], $query, $total),
			'PAGES' => $pages,
			'ANOTHER_HREF' => gen_uri($m),
			'ANOTHER' => $lang['SEARCH_ANOTHER'],
			'BACK_HREF' => gen_uri('index'),
			'BACK' => $lang['BACK']
			);

			$show_form = false;
		}
	}
}

if( $show_form )
{
	if( !$sql->query("SELECT * FROM `" . CATALOGUE_SECTIONS . "` ORDER BY `reg_access` DESC, `name` ASC;") )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);	
	}

	if( $sql->num_rows() < 1 )
	{
		put_error(ERROR, $lang['ERR_SECTION_LIST_IS_EMPTY']);
	}

	$section_value = array();

	while( $section = $sql->fetch_assoc() )
	{
		$template->set_block_vars('body', 'section', array(
		'ID' => $section['id'],
		'NAME' => output($section['name'])
		));

		$section_value[] = $section['id'];
	}

	$template_vars['body'] = array(
	'QUERY' => $lang['SEARCH_QUERY'],
	'LOGIC' => $lang['SEARCH_LOGIC'],
	'SECTIONS' => $lang['SEARCH_SECTIONS'],
	'SECTION_VALUE' => implode(';', $section_value),
	'TR' => $lang['TR'],
	'Y' => $lang['Y'],
	'N' => $lang['N'],
	'AND' => $lang['SEARCH_AND'],
	'OR' => $lang['SEARCH_OR'],
	'PATH' => gen_uri($m, '', $nocache),
	'SUBMIT' => $lang['SUBMIT'],
	'BACK_HREF' => gen_uri('index'),
	'BACK' => $lang['BACK'],
	'NOCACHE' => $nocache
	);
}

$template_vars['footer'] = array(
'SWITCH_VERSION' => switch_version($m, '', $show_form ? $nocache : 'q=' . urlencode(implode(' ', $q)) . '&amp;s=' . urlencode(implode(';', $s)) . '&amp;logic=' . strtolower($logic)),
'COUNTER' => ''
);

$template->set_vars('body', $template_vars['body']);
$template->set_vars('footer', $template_vars['footer']);
?>