<?php
$title = 'Поиск';
require_once('Sys/head.php');

echo '<div class="title"><form action="/m/search" method="POST" name="form">
Введите запрос <small>[min 4 , max 60 символов]</small><br/>
<input type="text" name="search" maxlenght="60" value="'.htmlspecialchars($_POST['search']).'"/><br/>
Параметры поиска :<br/>
<select name="m">
<option value="1">url</option>
<option value="2">название сайта</option>
<option value="3">описание сайта</option>
</select><br/>
<input name="n" type="checkbox" value="1"/> Точный поиск<br/>
<input type="submit" name="go"value="Поиск">
</form></div>';

if (isset($_POST['go']) ) {
     if (empty($_POST['search'])) {
	     $error[1] = 'Вы не ввели адрес сайта !';
	    }
     if (mb_strlen($_POST['search']) < 3 || mb_strlen($_POST['search']) > 64 ) {
	     $error[2] = 'Длина запроса должна быть не менее 3 символов и не привышать 64 символа !';

		}
		}
	 	 if (!empty($error)) {
			     echo '<div class="title">';
			       foreach($error as $value) {
                     echo "<b>$value</b> <br />";
                    }
					echo '</div>';
					require_once ('Sys/foot.php');
					exit();
			}

	              $mm = isset ($_POST['m']) ? intval($_POST['m']) : '';
				  $mm = $mm ? $mm : rawurldecode(trim($_GET['m']));

				  $n = isset ($_POST['n']) ? $_POST['n'] : '';
				  $n = $n ? $n : rawurldecode(trim($_GET['n']));

	 			 $search = isset ($_POST['search']) ? trim($_POST['search']) : '';
				 $search = strtr($search, array('_' => '\\_', '%' => ' ','$' => ' ', '*' => '%'));
                 $search = $search ? $search : rawurldecode(trim($_GET['search']));
				 if ($search) {
				 $search_db = 'LIKE \'' . mysql_real_escape_string('%' . $search . '%') . '\'';
				 if ($_POST['n'] == false) $search_db = 'LIKE \'' . mysql_real_escape_string('%' . $search . '%') . '\'';
				 if ($_POST['n'] == true) {
				      $search_db = '= "'.mysql_real_escape_string($search).'"';
				 }
				 $zap = '`about`';
				 if ($_POST['m'] == 1 || $_GET['mm'] == 1) $zap = '`url`';
				 if ($_POST['m'] == 2 || $_GET['mm'] == 2) $zap = '`name`';
				 if ($_POST['m'] == 3 || $_GET['mm'] == 3) $zap = '`about`';



$count = mysql_num_rows(mysql_query("SELECT * FROM `top33_sait` WHERE ".$zap." ".$search_db." "));
						if($count > 0)
							{
								$total=intval(($count-1)/$page_sait)+1; 
								$page=abs(intval($_GET['page'])); 
								if(empty($page) OR $page < 0)
									{
										$page = 1; 
									}
								if($page > $total)
									{
										$page = $total; 
									}
								$past=intval($count/$page_sait);  
								$start=$page*$page_sait-$page_sait; 
	$saits = mysql_query("SELECT * FROM `top33_sait` WHERE ".$zap." ".$search_db." ORDER BY `id` DESC LIMIT ".$start.",".$page_sait."");
								while($row = mysql_fetch_array($saits)) 
									{
										$start++;
										
										echo '<div class="title2">'.$start.'.<strong>'.$row['name'].'</strong> (Хостов: <strong>'.$row['hosts'].'</strong> | Хитов: <strong>'.$row['hits'].'</strong>) <a href="http://'.$set['home'].'/stats/'.$row['id'].'"><strong> (Статистика)</strong></a><br/></div>';
										echo '<div class="main">';
										if(mb_strlen($row['about']) > 100)
											{
												$text = substr($row['about'],0,50);
												echo ''.$text.'...<br/>'; 
											}
										else
											{
												echo ''.$row['about'].'<br/>'; 
											}
										
										echo '</div>';
									}
								navigation($count,$page_sait,$page,'http://'.$set['home'].'/m/search?search='.$search.'&n=0&mm='.$mm.'&page=',$total);
}

	else {
                     echo '<div class="title">Ничего не найдено !</div>';
                   }
				   			   
echo '<div class="main">Совпадений : '.$count.'</div>';			

}


?>