#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
#######################################################################################
#
# Site File Finder V1.0
# ©2002, Jim Melanson
#
# Requirements: UNIX Server, Perl5+
# Created: February 5th, 2002
# Author: Jim Melanson
# Contact: www.perlservices.net
# Phone: 1-877-751-5900, ext. 920
# Fax: 1-208-694-1613
# E-Mail: info@perlservices.net
#
#######################################################################################
#
# INSTRUCTIONS
#
# 1.) Make sure the path to Perl at the top of the script is correct.
#
# 2.) Place this script on your server, make sure you FTP it in ASCII mode
# and not Binary mode. Make sure you place the script in an executable
# directory
#
# 3.) Call the script by entering the full URL to it in your browsers address bar
#
# This script does one thing and it does it very well. It will only run on a
# UNIX server. If it runs on any other platform, please let us know.
#
# We do not accept support requests for this script, it does not require any
# support. All you need to do is make sure the path to Perl is correct, you
# FTP it in ASCII mode and not Binary mode, that you CHMOD it 755 and that the
# directory and parent directories it resides in are CHMOD 755.
#
#######################################################################################
#
# Edit nothing below this line
#
#######################################################################################
#Set the primary path
$FORM{search_in} = $ENV{'DOCUMENT_ROOT'};
&Parse;
$ScriptURL = qq~http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}~;
my($fselected, $dselected, $aselected);
if($FORM{finding} =~ /^f/) {$fselected = qq~ selected~}
elsif($FORM{finding} =~ /^d/) {$dselected = qq~ selected~}
else {$aselected = qq~ selected~}
print "Pragma: no-cache\nContent-type: text/html\n\n";
print qq~
Perl Services - Site File Lister
Perl Services - Site File Finder
$ENV{'SERVER_NAME'}
~;
if($FORM{action} eq 'process_search') {
#Initialize the hashes. Yeah, yeah, expensive I know but that are part of the beauty and power of Perl dammit!
my(%dirs, %dirs2beread, %dirs2bexfer, %files2bexfer);
#Add the primary path to the hash of all directories
$dirs{$FORM{search_in}} = 1;
#Add the primary path to the list of directories to be parsed
$dirs2beread{$FORM{search_in}} = 1;
#Initialize to true the var which will tell the loop when to exit
my $read_dirs = 1;
#Start the loop
while($read_dirs) {
#Get a list of all the currently held directory paths not yet parsed
my @dirkeys = sort keys %dirs2beread;
#Shift the first directory from this list
my $this_dir = shift(@dirkeys);
#Remove this first directory from the hash so that it will not be in the list during
#the next iteration through the loop
delete $dirs2beread{$this_dir};
#Now get a list of everyting in this directory
my @new = &readit($this_dir);
#Now step through each every item that is in the directory currently being examined
foreach(@new) {
if(-d $_) {
#If the item is a directory, add it to the hash of directories to be parsed
#and add it to the list of direcotries to be created on remote server
$dirs{$_} = 1;
$dirs2beread{$_} = 1;
} else {
#If the item is a binary file, add it to the hash of binary files to be xfer'd
$files2bexfer{$_} = 1;
}
}
my @checkloop = keys %dirs2beread;
unless((-d $checkloop[0]) && ($checkloop[0] =~ /[a-zA-Z1-9]/)) {
$read_dirs = 0;
}
}
#Print out the counts
print qq~
Results of Search
~;
#Finally, search everything
if($FORM{finding} =~ /(d|a)/) {
print "Directories Matching Search Request:
\n";
my $found_one = 0;
my @all_items = sort {$a cmp $b} keys %dirs;
for(my $a = 0; $a <= $#all_items; $a++) {
if($all_items[$a] =~ /$FORM{containing}/) {
$all_items[$a] =~ s/$FORM{search_in}//;
print "- $all_items[$a]\n";
$found_one = 1;
}
}
unless($found_one) {print "
- No directories matched your search criteria.\n"}
print "
\n";
}
if($FORM{finding} =~ /(f|a)/) {
print "Files Matching Search Request:
\n";
my $found_one = 0;
my @all_items = sort {$a cmp $b} keys %files2bexfer;
for(my $a = 0; $a <= $#all_items; $a++) {
my $print_it = 0;
my @parts = split(/\//, $all_items[$a]);
if($parts[$#parts] =~ /$FORM{containing}/) {
$print_it = 1;
}
if($FORM{extension} =~ /[a-zA-Z1-9]/) {
unless($parts[$#parts] =~ /$FORM{extension}$/) {
$print_it = 0;
}
}
if($print_it) {
$all_items[$a] =~ s/$FORM{search_in}//;
print "- $all_items[$a]\n";
$found_one = 1;
}
}
unless($found_one) {print "
- No files matched your search criteria.\n"}
print "
\n";
}
}
print qq~
Perl Services - Site File Finder
© 2002, Perl Services