View file download/search.php

File size: 15.62Kb
<?php

require_once ( "../includes/start.php" );
require_once ( "../includes/functions.php" );
require_once ( "../includes/header.php" );
include_once ( "../themes/" . $config['themes'] . "/index.php" );

$minfindme = 3; // Минимальное кол-во символов в слове для поиска
$maxfindme = 25; // Максимальное кол-во символов в слове для поиска

show_title( 'partners.gif', 'Поиск' );

if ( is_user() )
		$config_downlist = $udata[23];
else
		$config_downlist = 10;
/////////////////////////////////////////
if ( isset( $_GET['start'] ) )
		$start = ( int )$_GET['start'];
else
		$start = 0;
if ( isset( $_GET['action'] ) )
		$action = check( $_GET['action'] );

if ( !isset( $action ) )
{
		echo '<form action="search.php?action=search&amp;' . SID . '" method="post">';
		echo '<b>Запрос:</b><br><input type="text" name="findme" /><br>';
		echo '<br><b>Искать:</b><br>';
		echo '<input name="ftype" type="radio" value="2" checked> В описании<br>';
		echo '<input name="ftype" type="radio" value="1"> В названии<br>';
		echo '<br><b>Тип запроса:</b><br>';
		echo '<input name="tip" type="radio" value="1" checked> Или<br>';
		echo '<input name="tip" type="radio" value="2"> И<br>';
		echo '<br><input type="submit" value="Поиск" /></form><hr>';

		echo '<b>Последние запросы</b><br><br>';
		$file = file( DATADIR . "datatmp/downsearch.dat" );
		$file = array_reverse( $file );
		$countf = count( $file );
		if ( $countf > 5 )
		{
				$countf = 5;
		}
		if ( $countf > 0 )
		{
				for ( $i = 0; $i < $countf; $i++ )
				{
						echo $file[$i] . '<br>';
				}
		}
		else
		{
				echo 'Запросов еще не было!<br>';
		}
}


//----------------------- Поиск файлов ----------------------------//
if ( $action == "search" )
{
		$findme = check( trim( $_POST['findme'] ) );
		$findmetol = rus_utf_tolower( $findme );
		$findmewords = explode( " ", $findmetol );
		$wordsitogos = count( $findmewords );

		$findmeword = array();

		for ( $wi = 0; $wi < $wordsitogos; $wi++ )
		{
				if ( strlen( utf_to_win( $findmewords[$wi] ) ) >= 3 )
				{
						$findmeword[] = $findmewords[$wi];
				}
		}
		$wordsitogo = count( $findmeword );

		if ( $findme != "" && strlen( utf_to_win( $findme ) ) >= $minfindme && strlen( utf_to_win
				( $findme ) ) < $maxfindme )
		{

				//-------------------------Запись последних поисков--------------------------------//
				$dows = file( DATADIR . "datatmp/downsearch.dat" );
				$i = count( $dows );
				$dows = preg_replace( "|[\r\n]+|si", "", $dows );
				if ( !in_array( $findme, $dows ) )
				{

						write_files( DATADIR . "datatmp/downsearch.dat", "$findme\r\n", 0, 0666 );

						if ( $i > 6 )
						{
								write_files( DATADIR . "datatmp/downsearch.dat", "$findme\r\n", 1, 0666 );
						}
				}

				//---------------------------------------------------------------------------------//
				$a = array();
				$dir = opendir( "." );
				while ( $file = readdir( $dir ) )
				{
						if ( !eregi( "[^a-z0-9_-]", $file ) )
						{
								$a[] = $file;
						}
				}
				closedir( $dir );

				$total = count( $a );


				//------------------------Поиск по названию---------------------------------//
				if ( $_POST['ftype'] == 1 )
				{

						$pt = array();
						$ft = array();
						$ftno = array();

						for ( $i = 0; $i < $total; $i++ )
						{
								$d = opendir( $a[$i] );
								while ( $file = readdir( $d ) )
								{
										if ( ( $file != "." ) && ( $file != ".." ) && ( $file != ".htaccess" ) && ( $file !=
												"index.php" ) && ( $file != "name.dat" ) && !ereg( ".txt$", "$file" ) && !ereg( ".JPG$", "$file" ) &&
												!ereg( ".GIF$", "$file" ) )
										{

												$pt[] = $a[$i];
												$ft[] = $file;
												$ftno[] = strtolower( substr( $file, 0, strrpos( $file, '.' ) ) );

										}
								}
								closedir( $d );
						}

						$totals = count( $ft );
						$dres = array();
						$dresult = array();
						$dresultno = array();

						//---------------------------- Поиск словосочетаний ----------------------//
						for ( $wi = 0; $wi < $wordsitogo; $wi++ )
						{
								$findmetols = $findmeword[$wi];

								for ( $it = 0; $it < $totals; $it++ )
								{


										if ( strstr( $ftno[$it], $findmetols ) )
										{

												if ( !in_array( $ftno[$it], $dresultno ) )
												{

														$dres[] = $pt[$it];
														$dresult[] = $ft[$it];
														$dresultno[] = $ftno[$it];

												}
										}
								}
						}

						$total = count( $dresult );

						//------------------------------ Вывод найденного ----------------------------------//
						if ( $total > 0 )
						{
								echo '<br>Поиск запроса <b>' . $findme . '</b> в названиях<br>';
								echo 'Найдено совпадений: <b>' . ( int )$total . '</b><br><br>';

								if ( $start < 0 || $start > $total )
								{
										$start = 0;
								}
								if ( $total < $start + $config_downlist )
								{
										$end = $total;
								}
								else
								{
										$end = $start + $config_downlist;
								}
								for ( $i = $start; $i < $end; $i++ )
								{

										$siz = filesize( "$dres[$i]/$dresult[$i]" ); # размер
										$siz = formatsize( $siz );

										echo '<img src="../images/img/files.gif" alt=""> ';
										echo '<b>' . ( $i + 1 ) . '. <a href="down.php?action=ob&amp;did=' . $dres[$i] .
												'&amp;fid=' . $dresult[$i] . '&amp;' . SID . '">' . $dresult[$i] . '</a></b> ';
										echo '(' . $siz . ')<br>';
								}

								echo '<hr>';
								if ( $start != 0 )
								{
										echo '<a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
												'&amp;start=' . ( $start - $config_downlist ) . '&amp;' . SID . '">&lt;-Назад </a> ';
								}
								else
								{
										echo '&lt;-Назад ';
								}
								echo '|';
								if ( $total > $start + $config_downlist )
								{
										echo ' <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
												'&amp;start=' . ( $start + $config_downlist ) . '&amp;' . SID . '"> Далее-&gt;</a>';
								}
								else
								{
										echo ' Далее-&gt;';
								}

								if ( $total > 0 )
								{

										$ba = ceil( $total / $config_downlist );
										$ba2 = $ba * $config_downlist - $config_downlist;

										echo '<br><hr>Страницы:';
										$asd = $start - ( $config_downlist * 3 );
										$asd2 = $start + ( $config_downlist * 4 );

										if ( $asd < $total && $asd > 0 )
										{
												echo ' <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
														'&amp;start=0&amp;' . SID . '">1</a> ... ';
										}

										for ( $i = $asd; $i < $asd2; )
										{
												if ( $i < $total && $i >= 0 )
												{
														$ii = floor( 1 + $i / $config_downlist );

														if ( $start == $i )
														{
																echo ' <b>(' . $ii . ')</b>';
														}
														else
														{
																echo ' <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
																		'&amp;start=' . $i . '&amp;' . SID . '">' . $ii . '</a>';
														}
												}


												$i = $i + $config_downlist;
										}
										if ( $asd2 < $total )
										{
												echo ' ... <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
														'&amp;start=' . $ba2 . '&amp;' . SID . '">' . $ba . '</a>';
										}
								}
								echo '<br><br>';


						}
						else
						{
								show_error( 'По вашему запросу ничего не найдено.' );
						}

				}


				//------------------------Поиск по описанию---------------------------------//
				if ( $_POST['ftype'] == 2 )
				{

						$pt = array();
						$ftno = array();
						$ftopis = array();

						for ( $i = 0; $i < $total; $i++ )
						{
								$d = opendir( $a[$i] );
								while ( $file = readdir( $d ) )
								{
										if ( ereg( ".txt$", "$file" ) )
										{

												$pt[] = $a[$i];
												$ftno[] = str_replace( ".txt", "", $file );
												$ftopis[] = rus_utf_tolower( file_get_contents( "$a[$i]/$file" ) );

										}
								}
								closedir( $d );
						}

						$ftopis = str_replace( "<br>", " ", $ftopis );

						$totals = count( $ftno );

						//--------------------------- Ищем любое сходство --------------------------//
						if ( $_POST['tip'] == 1 )
						{
								$dres = array();
								$dresult = array();
								$dresultno = array();

								for ( $wi = 0; $wi < $wordsitogo; $wi++ )
								{
										$findmetols = $findmeword[$wi];

										for ( $it = 0; $it < $totals; $it++ )
										{

												$frename = preg_replace( "/$findmetols/", '<font color="#FF0000">' . $findmetols .
														'</font>', $ftopis[$it], 1 );
												$frename = utf_to_win( $frename );

												$ma = strpos( $frename, "<font" ) - 70;
												if ( $ma < 0 )
												{
														$ma = 0;
												}

												if ( $ma > 0 )
												{
														$msgtowrite = '...';
												}
												else
												{
														$msgtowrite = '';
												}

												$msgtowrite .= substr( $frename, $ma, 160 );

												if ( strlen( $frename ) > ( $ma + 150 ) )
												{
														$msgtowrite .= '...';
												}
												else
												{
														$msgtowrite .= '';
												}


												$msgtowrite = win_to_utf( $msgtowrite );


												if ( strstr( $msgtowrite, $findmetols ) )
												{

														if ( !in_array( $msgtowrite, $dresultno ) )
														{

																$dres[] = $pt[$it];
																$dresult[] = $ftno[$it];
																$dresultno[] = $msgtowrite;

														}
												}
										}
								}
						}

						//--------------------------- Ищем полное сходство --------------------------//
						if ( $_POST['tip'] == 2 )
						{

								$dres = array();
								$dresult = array();
								$dresultno = array();

								for ( $it = 0; $it < $totals; $it++ )
								{

										$frename = preg_replace( "/$findmetol/", '<font color="#FF0000">' . $findmetol . '</font>',
												$ftopis[$it], 1 );
										$frename = utf_to_win( $frename );

										$ma = strpos( $frename, "<font" ) - 70;
										if ( $ma < 0 )
										{
												$ma = 0;
										}

										if ( $ma > 0 )
										{
												$msgtowrite = '...';
										}
										else
										{
												$msgtowrite = '';
										}

										$msgtowrite .= substr( $frename, $ma, 160 );

										if ( strlen( $frename ) > ( $ma + 150 ) )
										{
												$msgtowrite .= '...';
										}
										else
										{
												$msgtowrite .= '';
										}


										$msgtowrite = win_to_utf( $msgtowrite );


										if ( strstr( $msgtowrite, $findmetol ) )
										{

												if ( !in_array( $msgtowrite, $dresultno ) )
												{

														$dres[] = $pt[$it];
														$dresult[] = $ftno[$it];
														$dresultno[] = $msgtowrite;

												}
										}
								}
						}


						$total = count( $dresultno );

						//------------------------------ Вывод найденного ----------------------------------//
						if ( $total > 0 )
						{
								echo '<br>Поиск запроса <b>' . $findme . '</b> в описании<br>';
								echo 'Найдено совпадений: <b>' . ( int )$total . '</b><br><br>';

								if ( $start < 0 || $start > $total )
								{
										$start = 0;
								}
								if ( $total < $start + $config_downlist )
								{
										$end = $total;
								}
								else
								{
										$end = $start + $config_downlist;
								}
								for ( $i = $start; $i < $end; $i++ )
								{


										echo '<div class=b><img src="../images/img/files.gif" alt=""> ';
										echo '<b>' . ( $i + 1 ) . '. <a href="down.php?action=ob&amp;did=' . $dres[$i] .
												'&amp;fid=' . $dresult[$i] . '&amp;' . SID . '">' . $dresult[$i] . '</a></b></div>';
										echo $dresultno[$i] . '<br>';
								}

								echo '<hr>';
								if ( $start != 0 )
								{
										echo '<a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
												'&amp;tip=' . $tip . '&amp;start=' . ( $start - $config_downlist ) . '&amp;' . SID .
												'">&lt;-Назад </a> ';
								}
								else
								{
										echo '&lt;-Назад ';
								}
								echo '|';
								if ( $total > $start + $config['forumpost'] )
								{
										echo ' <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
												'&amp;tip=' . $tip . '&amp;start=' . ( $start + $config_downlist ) . '&amp;' . SID .
												'"> Далее-&gt;</a>';
								}
								else
								{
										echo ' Далее-&gt;';
								}

								if ( $total > 0 )
								{

										$ba = ceil( $total / $config_downlist );
										$ba2 = $ba * $config_downlist - $config_downlist;

										echo '<br><hr>Страницы:';
										$asd = $start - ( $config_downlist * 3 );
										$asd2 = $start + ( $config_downlist * 4 );

										if ( $asd < $total && $asd > 0 )
										{
												echo ' <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
														'&amp;tip=' . $tip . '&amp;start=0&amp;' . SID . '">1</a> ... ';
										}

										for ( $i = $asd; $i < $asd2; )
										{
												if ( $i < $total && $i >= 0 )
												{
														$ii = floor( 1 + $i / $config_downlist );

														if ( $start == $i )
														{
																echo ' <b>(' . $ii . ')</b>';
														}
														else
														{
																echo ' <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
																		'&amp;tip=' . $tip . '&amp;start=' . $i . '&amp;' . SID . '">' . $ii . '</a>';
														}
												}


												$i = $i + $config_downlist;
										}
										if ( $asd2 < $total )
										{
												echo ' ... <a href="search.php?action=search&amp;findme=' . $findme . '&amp;ftype=' . $ftype .
														'&amp;tip=' . $tip . '&amp;start=' . $ba2 . '&amp;' . SID . '">' . $ba . '</a>';
										}
								}
								echo '<br><br>';

						}
						else
						{
								show_error( 'По вашему запросу ничего не найдено.' );
						}

				}
		}
		else
		{
				echo '<br>Ваш запрос пуст, менее ' . ( int )$minfindme . ' или более ' . ( int )$maxfindme .
						' символов!<br>';
		}
		echo '<br><img src="../images/img/back.gif" alt=""> <a href="search.php?' . SID .
				'">Вернуться</a>';
}

echo '<br><img src="../images/img/panel.gif" alt=""> <a href="index.php?' . SID .
		'">В загрузки</a>';
echo '<br><img src="../images/img/homepage.gif" alt=""> <a href="../index.php?' . SID .
		'">На главную</a>';
include_once ( "../themes/" . $config['themes'] . "/foot.php" );

?>