#!/usr/bin/perl
#VLC.cgi
use warnings;
use strict;
use CGI;
my $dir = 'e:/development/mb10_bachelor_of_medicine_degree';
my $q = new CGI;
my $query = lc $q->param( "code" );
my @Path;
my @Status;
my @list_of_files;
my @list_of_subDirectory;
my @list_of_Dir;
my @list_of_txt;
my @FileExt;
my @FileName;
my @Name;
my @Author;
my @Header;
my @Ignore;
my $FilePath;
my $FileName;
my $path_translated = $ENV{path_info};
my $path;
my $referer = $ENV{http_referer};
my $NameCounter = 0;
my $count = 0;
my $Num = 0;
my $Found = 0;
my %Ext = (
pl => "Perl",
cgi => "CGI",
htm => "HTML Document",
ppt => "PowerPoint",
pdf => "ADOBE",
doc => "Word Document",
pdf => "Adobe Acrobat Document",
asp => "Active Server Page",
gif => "GIF Image",
js => "JavaScript File",
bak => "Back-up",
"" => "Folder"
);
print "Content-type:text/html\n\n";
print "
Directory Contents";
print "";
search($dir);
if($Num ne 0){
while($Num ne 0){
$Num--;
$dir = @list_of_subDirectory[$Num];
search($query);
}
}
if($Found eq 0){
print "Couldn't find '$query'";
}
FindReferer($path);
DirContents($path);
PrintItems($query);
sub search{
opendir DIR, $dir or die "couldn't open the default directory: $!";
@list_of_Dir = readdir(DIR);
closedir DIR;
foreach (@list_of_Dir){
#skip the files that has "." or ".."
next if $_ eq "." or $_ eq "..";
FindSubDirectory(@list_of_Dir);
#if query match the file name
if(grep{/$query/} lc($_) and -d "$dir/$_"){
$Found = 1;
$path = "$dir/$_";
}
}
}
sub FindSubDirectory{
# if file is a folder
if(-d "$dir/$_"){
#save path
@list_of_subDirectory[$Num] = "$dir/$_";
#increment
$Num++;
}
}
sub DirContents{
my $Counter = 0;
opendir DIR2, $path or die "couldn't open the default directory: $!";
@list_of_files = grep{!/.bak/} readdir(DIR2);
closedir DIR2;
FileExtention(@Ignore);
@list_of_txt = grep{/.txt/} @list_of_files;
foreach(@Ignore){
@list_of_files = grep{!/.@Ignore[$Counter]/} @list_of_files;
$Counter++;
}
foreach (@list_of_files){
next if $_ eq "." or $_ eq "..";
FindPath(@list_of_files);
SplitFileName(@list_of_files);
$count++;
}
}
sub FileExtention{
my $value;
my $Counter = 0;
foreach $value( $q->param( "ext" ) )
{
@Ignore[$Counter] = $value ;
$Counter++;
}
@Ignore[$Counter] = "txt" ;
}
sub SplitFileName{
my @SplitFile = split /\./, $_;
@FileExt[$count] = lc($SplitFile[1]);
@FileName[$count] = $SplitFile[0];
foreach(@list_of_txt){
my @txt = split /\./, $_;
my $txt = @txt[0];
if (@FileName[$count] eq $txt){
$FilePath = $_;
FindFile($FilePath);
}
}
}
sub FindFile{
my $Name;
my $Author;
my $lineNo = 1;
my $file = "$path/$FilePath";
open FILE, $file or die "Couldn't open File: $!";
while (){
if ($lineNo eq 1){
$Name = "$_";
}
else
{
$Author = "$_";
}
$lineNo++;
}
my @SplitName = split /\=/, $Name;
@Name[$NameCounter] = @SplitName[1];
my @SplitAuthor = split /\=/, $Author;
@Author[$NameCounter] = @SplitAuthor[1];
close FILE;
$NameCounter++;
}
sub FindPath{
my $Extention;
my $Counter = 0;
if (-d "$path/$_"){
foreach(@Ignore){
$Extention = "ext=@Ignore[$Counter]&$Extention";
$Counter++;
}
@Status[$count] = "Explore Folder";
@Path[$count] = "http://mcucharlie$path_translated?code=$_&$Extention";
}
else{
@Path[$count] = "$path/$_";
@Status[$count] = "View File";
}
}
sub FindReferer{
my $LastNum = 0;
#split the http referer
my @referer =split /\//, $referer;
#count the number of item in the array
foreach(@referer){
$LastNum++;
}
#store the folder name
my $Header = uc (@referer[$LastNum-2]);
#break folder name every time we see "_"
@Header = split /\_/, $Header;
}
sub PrintItems{
my $Counter = 0;
my $Fields = 0;
print "Resources - Below is a selection of files which have been compiled by your lecturers and which you may find helpful.";
foreach(@Header){
$Counter++;
print "@Header[$Counter] ";
}
print "";
print "" ;
print "| Name | ";
print "Author | ";
print "File Type | ";
print "Link |
";
print " | | |
\n";
my $foo = 0;
while($foo ne $count){
if (@FileExt[$foo] eq "txt"){
$foo++;
}
if($Fields eq 0){
print "| @Name[$foo] | @Author[$foo] | $Ext{@FileExt[$foo]} | @Status[$foo] |
";
$Fields++;
}
else{
print "| @Name[$foo] | @Author[$foo] | $Ext{@FileExt[$foo]} | @Status[$foo] |
";
$Fields--;
}
$foo++;
}
}