源代码:
#!/usr/bin/perl
#search.pl 2.02
#
#A simple script to search .html pages in a directory.
#written by webjx.com <web@webjx.com>
#June 10, 2004
#
#Please do not redistribute this script.
#
#Feel free to modify it as you wish, but if you do make any modifications, please
#submit them to me and I will give you credit in this section of the script.
#
#Thanks.
#
# DISCLAIMER: I AM IN NO WAY RESPONSIBLE FOR ANY DAMAGE THIS SCRIPT MAY CAUSE
#Configuration
#
#
# The base directory(absolute path) which contains all of the subdirectories you wish
# to search
#
# NOTICE HAS A TRAILING SLASH
#
#
# The url of a images
#
#
# The url onto which the subdirectories in @path are appended.
#
# Notice that INCLUDES THE TRAILING SLASH.
#
# For Example:
#
# If is /ROOT/LOCAL/USERS/ represented by =
http://yourhost.here/users/# then JDOE's home directory would be /ROOT/LOCAL/USERS/JDOE/ and the link to all pages
# found in his directory which match the search string would be generated as
#http://yourhost.here/JDOE/page.html.
#
# The script simply assumes that the directory
# structure of your system matches the URL mapping of your server software.
# It just appends the user's home directory to .
#
#
# If user's home directories are called by,
# http://yourhost.here/~jdoe/ , for example, set it to '~';
# Pages containing the search string will be displayed with the "~" in place.
#
#
# If users have public_html as their home directories change this line to
#
# ='public_html/';
#
# the script will search /public_html/directory/path/
# rather than /directory/path/
#
#
# The extension for files to search(html or htm--Don't include the period)
#
#@filetypes
# Types of files that the script should search through(extensions).
#
#
# Valid URLs(HTTP Referers) that can search your site. For example, if your
# site ishttp://www.yoursite.com/you/and you want only your pages to have access
# to the script and search your directories, you could set
# to http://www.yoursite.com/you/ because all of your pages would have that part
# in common. If their in different directories, that will differ but the
#http://www.yoursite.com/you/will always be there in the HTTP referer.
#
#
# The name of the person who owns the site that the script will be used on
#
#
# The above person's email address.
#########
= '/home/httpd/';
=
http://linux.cqi.com.cn/~elvis/flame.gif';='~'; #either '' or '~'
='public_html/'; #either '' or 'public_html/'
@filetypes=('html','txt','htm','shtml');
@valid=('linux.cqi.com.cn/~elvis/');
='Elvis Tam';
#########
#check if logon matches Referer URL
if({'HTTP_REFERER'} !~ /\/{ 'logon' }/)
{
print "Content-type: text/html\n\n";
print "<html>\n<title>错误</title>\n";
print "<body bgcolor=\"\#ffffff\" text=\"\#000000\">\n\n";
print "错误的username. 你只被允许搜索 <b>你自已的</b> 页面.\n";
print "</ul>\n<center><hr width=50%></center>\n</html>";
exit 0;
}
foreach (@valid)
{
if({'HTTP_REFERER'} =~ //)
{
="yes";
}
}
if( eq "")
{
if( ne "yes")
{
print "Content-type: text/html\n\n";
print "<title>请 离 开</title>";
print "<body bgcolor=\"\#ffffff\" text=\"\#000000\">";
print "你试图在地址窗口输入脚本的URL ";
print "或使用不合法的表单来访问它,两者都是不允许的.";
print "</html>\n";
exit 0;
}
}
##########
#Get form data
########
if( ne "")
{
@bern=('search1','search','type','insensitive','user','','paths','basedir;guestbook');
%formdata=@bern;
}
else
{
&get_form_data;
}
######
#Check search string to see if it's worth searching for.
########
if ({'search1'} eq "" | {'search1'} =~ /^ /)
{
print "Content-type: text/html\n\n";
print "<title>Error!</title>";
print "<body bgcolor=\"\#ffffff\" text=\"\#000000\">";
if ({'search1'} eq "")
{
print "你并没有输入任何字符.";
}
else
{
print "你输入的字符前面有空格.";
}
print '<p>';
print '请按"后退"的按钮回去再尝试.';
print "请联系系统管理员<a href=\"mailto:\"></a> ";
print "如果你需要进一步的援助.<p><br>";
print "</html>";
exit 0;
}
#############
#Make array of directories to search.
############
else
{
= {'logon'};
= {'baseurl'};
if({'paths'} =~ /\;/)
{
@temppaths=split("\;",{'paths'});
foreach (@temppaths)
{
if( eq "basedir")
{
push(@paths,"basedir");
}
else
{
push(@paths,"/"); #put a slash onto the end of the directory name
}
}
}
else
{
push(@paths,"basedir");
}
=0;
#@paths is an array containing the directories to search.(with trailing slashes)
#if user wants to search the base directory, the array also contains "basedir"
print "Content-type: text/html\n\n";
print "<title>搜索结果</title>\n";
print "<body bgcolor=\"\#ffffff\" text=\"\#000000\">\n";
print "<h1>搜索结果</h1>\n";
print "<p>\n";
print "你输入的关键字 <b>{'search1'}</b> 在本站的搜索结果如下\n";
print "<p>\n";
print "<hr><br>\n";
#Get list of all files of specified types
#in the specified directories.
foreach (@paths)
{
foreach (@filetypes)
{
if ( eq "") #if being used to search
{
if( ne "basedir")
{
opendir(DIR,".") || &error("");
}
else
{
opendir(DIR,".") || &error("");
}
}
else #if being used by a regular user
{
if( ne "basedir")
{
opendir(DIR,"/.") || &error("/");
}
else
{
opendir(DIR,"/.") || &error("/");
}
}
@nb=grep(/\.$/,readdir(DIR));
foreach (@nb)
{
if( eq "") #superuser
{
if( ne "basedir")
{
push(@names,"");
push(@URLS,"","");
}
else
{
push(@names,"");
push(@URLS,"","");
}
}
else #regular user
{
if( ne "basedir")
{
push(@names,"/");
push(@URLS,"/","");
}
else
{
push(@names,"/");
push(@URLS,"/","");
}
}
}
close(DIR);
}#go to the next file in the directory
}#go to the next directory and do it again.
#@URLS contains "file location","file url"
#@names contains "file location","file location2"...
#Make @URLs associative so we can call up the URL just by using
#the value int @names as the key.
%URLS = @URLS;
=@names;
= + ;
#Now you have a list of the eligible files in the all of the directories.
#Search each of them for the desired string. If you find it, remember
#the path and the name of the file and extract the title.
foreach (@names) #for each .html file in the array @names, do the following
{
="nothing";
open (HTML,"") || &error("");
@contents=<HTML>; #Read the contents into an array
close (HTML); #so that you can work with it and close it.
=join('',@contents);
=0; #Will be used to count no. of occurrences
if({'type'} eq "insensitive") #If search type is "case insenstitive"
{
++ while =~ /{'search1'}/gi;
if( != 0)
{
++;
}
}
else #if search type is "case-sensitive"
{
++ while =~ /{'search1'}/g;
if( != 0)
{
++;
}
}
if ( != 0) #if search string is found, print link to the page
{
if( =~ /htm.?$/i)
{
if ( =~ /title/)
{
@a=split(/title\>/,);
chop ;
chop ;
=;
}
elsif ( =~ /TITLE/)
{
@a=split(/TITLE\>/,);
chop ;
chop ;
=;
}
else
{
='NO TITLE';
}
}
else
{
='NO TITLE';
}
#print the HTML for the file
print "<br><img src=\"\" alt=\"[\*]\"><a href=\"{}\"></a>\( 处\)\n";
}
}#Go to the next file in the list.
if ( eq "0")
{
print "<center>没有发现任何包含关键字为 <b>{'search1'}</b> 的文件.</center>\n";
}
print "<hr><br>\n";
print "<p><b>被搜索的文件数:</b> \n";
print "<br><b>搜索类型:</b> case-{'type'}\n";
print "<br><br><center>\n本程式由 <a href=\"http://oh.yeah.net/\">Oh Yeah Net</a>编制<br></center>\n";
print "</html>\n";
}
exit 0;
################
#Subroutines
######
sub get_form_data{
@pairs=split(/\&/,{'QUERY_STRING'});
foreach (@pairs)
{
(,) = split(/\=/,);
=~ s/\+/ /g;
=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
=~ s/~!/ ~!/g;
=~ s/\+/ /g;
=~ s/\r//g;
push (@formdata,);
push (@formdata,);
}
%formdata=@formdata;
return %formdata;