![]() |
|
#!/usr/bin/perl
#Поисковик по сайту
my $server=$ENV{'SERVER_NAME'}; #На каком сервере сидим
$server=~s/www\.ardo-diving/ardo-diving/;
my $current_dir='.'; my $misc_dir="../misc";
my $Set_Dir="$current_dir/$misc_dir/$server/Setup"; #Где настройки
my $Lib_Dir="$current_dir/../lib";
unshift(@INC, "$Lib_Dir");
use strict;
#Пытаемся поставить локаль
use locale;
use POSIX qw(setlocale LC_ALL);
#setlocale(LC_ALL, "ru_SU.CP1251");
setlocale(LC_ALL, "bg_BG.CP1251");
#require API.pm
eval { require "$Lib_Dir/API.pm" };
if ($@)
{
print "Content-type: text/html\n\n";
print "Script Execution Aborted
\n"; print "Could not load library $Lib_Dir/API.pm : $@"; die "Could not load library $Lib_Dir/API.pm : $@";; } #require CGI eval { require CGI }; if ($@) { print "Content-type: text/html\n\n"; API::CGIDie(__FILE__, __LINE__, "Could not load library CGI.pm" ,$@) } #Getting script name $_=$0; $_=$ENV{'SCRIPT_NAME'} unless $_; $_=$ENV{'SCRIPT_FILENAME'} unless $_; my $script_name; if (m|^(.*[/\\])?([a-zA-Z0-9._-]+)$|) { $script_name=$2; $script_name=~ s|^.*/([^/]+)$|$1|; $script_name=~ s/[.](cgi|pl)//; } else { print CGI::header(); API::CGIDie(__FILE__, __LINE__, 'Error in script configuration'); } #Requires all modules that we'll need cgiRequire("$Lib_Dir/Parser/CFG.pm", "$Lib_Dir/Parser/Template.pm"); #Initalize global variables my (%VAR, %CONFIG, %FORM_DATA, %SETTINGS, %TOPICS_DATA); #Load CGI parameters into %FORM_DATA hash my @params=CGI::param(); foreach (@params) { $FORM_DATA{$_}=CGI::param($_); } #Creating new object for parsing templates my $template=Parser::Template->new(\%VAR, \%CONFIG, \%FORM_DATA); #Loading global settings eval { Parser::CFG::parse("$Set_Dir/global.config", \%SETTINGS) }; if ($@) { print CGI::header(); API::CGIDie(__FILE__, __LINE__, 'Filesystem error', $@) } #Loading local settings eval { Parser::CFG::parse("$Set_Dir/client/$script_name.config", \%SETTINGS) }; if ($@) { print CGI::header(); API::CGIDie(__FILE__, __LINE__, 'Filesystem error', $@) } #Compiling data and template directories path $SETTINGS{path}{data_dir}="$misc_dir/$server/$SETTINGS{path}{data_dir}"; $SETTINGS{path}{template_dir}="$misc_dir/$server/$SETTINGS{path}{template_dir}"; #Define some template variables $SETTINGS{path}{'htdocs'}='' if ($SETTINGS{path}{'htdocs'} eq '""' || $SETTINGS{path}{'htdocs'} eq "''" || $SETTINGS{path}{'htdocs'} eq '0'); $CONFIG{'script_url'}=$SETTINGS{path}{protocol}.$SETTINGS{path}{domain}.'/'. $SETTINGS{path}{path}.'/'.$script_name.'.cgi'; $CONFIG{'script_prefix'}=$SETTINGS{path}{protocol}.$SETTINGS{path}{domain}.'/'. $SETTINGS{path}{path}; $CONFIG{'htdocs_url'}=$SETTINGS{path}{protocol}.$SETTINGS{path}{domain}; $CONFIG{'htdocs_url'}.='/'.$SETTINGS{path}{htdocs} if ($SETTINGS{path}{htdocs}); $CONFIG{'images_url'}=$CONFIG{'htdocs_url'}; $SETTINGS{path}{images}=$1 if ($SETTINGS{path}{images}=~ /^[\/\\](.+)$/); $CONFIG{'images_url'}=$CONFIG{'htdocs_url'}.'/'.$SETTINGS{path}{images} if ($SETTINGS{path}{images} && $SETTINGS{path}{images} ne '""'); #Print HTTP header print CGI::header(-charset => $SETTINGS{CGI}{charset}); #Регексп поиска my $search=$FORM_DATA{'search'}; $search=~ s/[\n\r]+/ /g; $search=~ s/\s+/ /g; if ($search){ &PrintSearch(); } else { &PrintFirst(); } exit; sub PrintSearch { #По каким модулям ("разделам", "типам" - вобщем, различным типам контекста) ищем - #к примеру, по самому сайту, по новостям, по форуму... my %MODULES; eval { Parser::CFG::parse("$current_dir/$SETTINGS{path}{data_dir}/$SETTINGS{file}{search_data}", \%MODULES) }; if ($@) { API::CGIDie(__FILE__, __LINE__, 'Filesystem error', $@) } #Читаем индексированую дату для каждого из модулей my %INDEXES; foreach my $module (keys %MODULES) { next if ($FORM_DATA{'target'} && (API::strDownCase($FORM_DATA{'target'}) ne $module)); my %INDEX; eval { Parser::CFG::parse("$current_dir/$SETTINGS{path}{data_dir}/$SETTINGS{path}{search_dir}/$module.data", \%INDEX) }; if ($@) { API::CGIDie(__FILE__, __LINE__, 'Filesystem error', $@) } foreach my $key (keys %INDEX) { $INDEXES{$key}=$INDEX{$key}; $INDEXES{$key}{module}=$module } } my @words=split(/ /, $search); #Типа, учитываем разные формы слова :-) my %WORDS; foreach (@words) { $WORDS{$_}{counts}=1; my $keyword=$_; if (length($keyword)>3 && length($keyword)<=5) { chop($keyword) } elsif (length($keyword)>5 && length($keyword)<=7) { chop($keyword); chop($keyword) } elsif (length($keyword)>7) { chop($keyword); chop($keyword); chop($keyword) } $WORDS{$_}{keyword}=$keyword; } #Собстно, ищем my %SEARCH_RESULT; foreach my $page (keys %INDEXES) { foreach my $word (keys %WORDS) { my $content=$INDEXES{$page}{content}; my $keyword=$WORDS{$word}{keyword}; while ($content=~ s/\Q$keyword\E//i) { $SEARCH_RESULT{$page}++; $WORDS{$word}{counts}++ } } } #require FormatDate.pm cgiRequire("$Lib_Dir/FormatDate.pm"); my $pages; #Кол-во страниц результата #Выдаем результат - сортируем по релевантности foreach my $page (sort { ($SEARCH_RESULT{$b} <=> $SEARCH_RESULT{$a}) || ($a cmp $b) } keys %SEARCH_RESULT) { #Дошли до нужной нам страницы? if ($pages>=($SETTINGS{CGI}{show_by}*$FORM_DATA{'page'}) && $pages<($SETTINGS{CGI}{show_by}*($FORM_DATA{'page'}+1))) { $VAR{'words'}=''; $VAR{'page'}=$page; $VAR{'found'}=$SEARCH_RESULT{$page}; $VAR{'title'}=$INDEXES{$page}{title}; $VAR{'title'}=$page unless $VAR{'title'}; $VAR{'size'}=sprintf('%0.2f', $INDEXES{$page}{bytes}/1024); $VAR{'date'}=FormatDate::format($INDEXES{$page}{updated}, $SETTINGS{CGI}{date_format}); $VAR{'partition'}=$MODULES{$INDEXES{$page}{module}}{title}; my $content=$INDEXES{$page}{content}; my $str; #Тут как-то хитро строятся цитаты из найденного текста my @ary=split(/\n/, $content); my %LINES; for (my $i=0; $i "; $out_lines++; last if ($out_lines>=$SETTINGS{CGI}{max_lines}); } foreach my $word (keys %WORDS) { $VAR{'words'}=~ s/([^\s\W]*\Q$WORDS{$word}{keyword}\E[^\s\W]*)/$1<\/b><\/font>/ig } $VAR{'count'}=$pages+1; #Выведим новую строку $template->parse("$current_dir/$SETTINGS{path}{template_dir}/$SETTINGS{template}{line}", $VAR{'list_lines'}); if ($template->errorNum()) { API::CGIDie(__FILE__, __LINE__, 'File system error', $template->errorMsg()) } } $pages++; } #Строим список страниц my $page; my $search=API::toAnchor($FORM_DATA{'search'}); for (my $i=0; $i<$pages; $i+=$SETTINGS{CGI}{show_by}) { my $from=$i+1; my $to=$i+$SETTINGS{CGI}{show_by}; $to=$pages if ($to>$pages); if ($page==$FORM_DATA{'page'}) { $VAR{'pages_list'}.="[$from-$to]\n" } else { $VAR{'pages_list'}.="[$from-$to]\n" } $page++; } #Некоторая статистика foreach my $word (@words) { $VAR{'words_stat'}.="$word: ".($WORDS{$word}{counts}-1).", " } chop($VAR{'words_stat'}); chop($VAR{'words_stat'}); $VAR{'pages'}=$pages; $VAR{'pages'}='0 ' unless $VAR{'pages'}; #Запишаем все в шаблон и выведем my $out_str; $template->parse("$current_dir/$SETTINGS{path}{template_dir}/$SETTINGS{template}{frontpage}", $out_str); if ($template->errorNum()) { API::CGIDie(__FILE__, __LINE__, 'File system error', $template->errorMsg()) } print $out_str; } sub PrintFirst{ $VAR{'no_input'}='yes'; my $out_str; $template->parse("$current_dir/$SETTINGS{path}{template_dir}/$SETTINGS{template}{frontpage}", $out_str); if ($template->errorNum()) { API::CGIDie(__FILE__, __LINE__, 'File system error', $template->errorMsg()) } print $out_str; } ############################################################################# #&CgiRequire# ############################################################################# # This subroutine checks to see whether the file that we are trying to # require actually exists and is readable by us. The reason for this # subroutine is to provide the developer with an informative error message # when attempting to debug the scripts. sub cgiRequire { # Define $require_file as a local variable and set it equal to the # filename we sent when we called this routine. my(@require_files)=@_; # Check to see if the file exists and is readable by us. If so, go ahead # and require it. foreach my $file (@require_files) { eval { require $file }; if ($@) { print CGI::header(); API::CGIDie(__FILE__, __LINE__, "Could not load library $file" ,$@) } } }#&CgiRequire# ############################################################################# |
|
|
![]() | |
|
© "Центр гомеопатии" / Гомеопатический Центр здоровья и реабилитации (Москва) При использовании материалов сайта ссылка обязательна |