Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute perl cgi prog on clicking hyperlink

Status
Not open for further replies.

akelabanda

Programmer
Dec 19, 2001
61
IN
Hi

I want to know how can I do the following.

I want to list the directories and files present
in a certain directory and create hyperlinks to
them.

On clicking these, if it's a file it will open
using the mime type. But if it's a directory,
I want to go into the directory that this points
to and do the same as above, i.e, display contents
and allow to click.

Thanks in advance for your help.

Regards
Rajeev
 
Hi Rajeev, this could be just what the doctor ordered.
It will require some reconfiguration. yourdomain.com change to the name of your server Your Domain

The aesthetics may require some rejigging

JavaScript content derived courtesy of developer.irt.org & other sources

#!/usr/bin/perl
#
print "Content-type:text/html\n\n";
#
local %form = &get_form_data;
$dir=$form{'dir'};
if ("$dir" eq "") {
$dir="tech/";
}

$dirsz=4096;
$basedir="/home/sites/yourdomain.com/web/";
#set to your directory root
$basedir2="";
$homeurl="Enter here name of server";
opendir techdir, $basedir.$dir or die "Error Reading Directory: $!";
@allfiles = readdir techdir;
closedir techdir;
&printheader;

print &quot;<tr><td colspan=\&quot;3\&quot;><a href=\&quot; Back</b></a></td><tr>\n&quot;;
foreach $file (@allfiles) {
if ((&quot;$file&quot; ne &quot;.&quot;) && (&quot;$file&quot; ne &quot;..&quot;)) {
my ($dev, $inode, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat $basedir.$dir.$file;
if ((substr $file, (length $file) - 4, 4) ne (&quot;.css&quot;)) {
if ($size==$dirsz) {
my $time2 = &format_time($mtime);
print &quot;<tr><td><a href=\&quot;javascript:go(' onMouseOver=\&quot;window.status='&copy; Ash Technologies 2002'; return true;\&quot;onClick=\&quot;window.status='&copy; Your domain'; return true;\&quot;>$file</td><td align=\&quot;right\&quot;>Dir</td><td align=\&quot;right\&quot;>$time2</td></tr>\n&quot;;

} else {
my $size2 = &format_size ($size);
my $time2= &format_time($mtime);
my $newimg = &calc_diff_time ((substr $time2,0,4).(substr $time2,5,2).(substr $time2, 8,2));
print &quot;<tr><td><a href=\&quot;javascript:go(' onMouseOver=\&quot;window.status='&copy; Your domain'; return true;\&quot; onClick=\&quot;window.status='&copy; Your Domain'; return true;\&quot;>$file $newimg</td><td align=\&quot;right\&quot;>$size2</td><td align=\&quot;right\&quot;>$time2</td></tr>\n&quot;;
}
}
}
}
&printfooter;
sub printheader {

print &quot;<html><head><meta HTTP-EQUIV=\&quot;pragma\&quot; CONTENT=\&quot;no-cache\&quot;><title>Technical Login Area - Please select one of the following links</title><link rel=\&quot;stylesheet\&quot;\n&quot;;
print &quot;href=\&quot;print &quot;<body bgcolor=\&quot;#001067\&quot;><center>&quot;;
#
# enable this section if you wish to disable right clicking to view source
#print &quot;<SCRIPT language=javascript>\n&quot;;
#print &quot;<!--\n&quot;;
#print &quot;function disable_right_click(e) {var browser = navigator.appName.substring (0,9);var event_number = 0;if (browser==\&quot;Microsoft\&quot;) event_number = event.button;\n&quot;;
#print &quot;else if (browser==\&quot;Netscape\&quot;) event_number = e.which;if ( event_number==2 || event_number==3 ){ alert (\&quot;Copyright Your Domain\&quot;); return (false); }\n&quot;;
#print &quot;return (true);}\n&quot;;
#print &quot;function check_mousekey() {var mouse_key = 93;var keycode = event.keyCode;if ( keycode == mouse_key )alert (\&quot;Copyright Your Domain\&quot; );}\n&quot;;
#print &quot;function trap_page_mouse_key_events() {var browser = navigator.appName.substring ( 0, 9 );document.onmousedown = disable_right_click;\n&quot;;
#print &quot;if ( browser == \&quot;Microsoft\&quot; ) document.onkeydown = check_mousekey; else if ( browser == \&quot;Netscape\&quot; )document.captureEvents( Event.MOUSEDOWN );}\n&quot;;
#print &quot;window.onload = trap_page_mouse_key_events;\n&quot;;
#print &quot;function relode() { location.reload(); }\n&quot;;
#print &quot;window.onresize = relode;\n&quot;;
#print &quot;document.oncontextmenu = function() { return false; }\n&quot;;
#print &quot;//--></SCRIPT>\n&quot;;
#
print &quot;<SCRIPT LANGUAGE=\&quot;JavaScript\&quot;>\n&quot;;
print &quot;<!--\n&quot;;
print &quot;function go(url) {\n&quot;;
print &quot; if (document.images)\n&quot;;
print &quot; location.replace(url);\n&quot;;
print &quot; else\n&quot;;
print &quot; location.href = url;\n&quot;;
print &quot;}\n&quot;;
print &quot;//-->\n&quot;;
print &quot;</SCRIPT>\n&quot;;
#
#
print &quot;<table with=\&quot;100%\&quot;><tr><td rowspan=\&quot;2\&quot;>\n&quot;;
print &quot;<img src=\&quot;translogo.gif\&quot;></td><td align=\&quot;center\&quot;>\n&quot;;
print &quot;<p>Welcome to the Your Domain Technical Directories<br>All the documents you need &quot;;
print &quot;have been filed according to the product and / or News<br><br>Please select one of the links below</p>Primary (left) click to view, secondary (right) click for options to save, or open in new window<br></td></tr>\n&quot;;
print &quot;</table>\n&quot;;
print &quot;<br>\n<table width=\&quot;70%\&quot;>\n&quot;;
print &quot;<tr><td bgcolor=\&quot;#FFFFFF\&quot; colspan=\&quot;3\&quot; align=\&quot;center\&quot;><font color=\&quot;#001067\&quot;><b>Your Current Directory is $dir</td></tr>\n&quot;;
print &quot;<tr bgcolor=\&quot;#ffffff\&quot;><td align=center><b><font color=\&quot;#001067\&quot;>Filename</td><td align=center><font color=\&quot;#001067\&quot;>\n&quot;;
print &quot;<b>Size</td><td align=center><font color=\&quot;#001067\&quot;><b>Date</td></tr>\n&quot;;

}

sub printfooter {
print &quot;</table><body><html>&quot;;
}
sub get_form_data {
my $temp;
my $buffer;
my @data;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
foreach $temp (split(/&|=/,$buffer)) {
$temp =~ tr/+/ /;
$temp =~ s/%([0-9a-fA-F]{2})/pack(&quot;c&quot;,hex($1))/ge;
$temp =~ s/[\r\n]/ /g;
push @data, $temp;
}
foreach $temp (split(/&|=/,$ENV{'QUERY_STRING'})) {
$temp =~ tr/+/ /;
$temp =~ s/%([0-9a-fA-F]{2})/pack(&quot;c&quot;,hex($1))/ge;
$temp =~ s/[\r\n]/ /g;
push @data, $temp;
}
return @data;
}

sub format_size {
my ($tmp) = @_;
my $len = length $tmp;
if ($len <= 3) { return $tmp; }
if ($len > 3 && $len <= 6) {
if ($len == 4) { my $tmp2 = substr $tmp, 0, 1; my $tmp3 = substr $tmp, 1, 3; return $tmp2.&quot;,&quot;.$tmp3.&quot; bytes&quot;; }
if ($len == 5) { my $tmp2 = substr $tmp, 0, 2; my $tmp3 = substr $tmp, 2, 3; return $tmp2.&quot;,&quot;.$tmp3.&quot; bytes&quot;; }
if ($len == 6) { my $tmp2 = substr $tmp, 0, 3; my $tmp3 = substr $tmp, 3, 3; return $tmp2.&quot;,&quot;.$tmp3.&quot; bytes&quot;; }
} elsif ($len >6 && $len <= 9 ) {
if ($len==7) { my $tmp2=substr $tmp,0,1;my $tmp3=substr $tmp,1,3;my $tmp4=substr $tmp,4,3;return $tmp2.&quot;,&quot;.$tmp3.&quot;,&quot;.$tmp4.&quot; bytes&quot;;}
if ($len==8) { my $tmp2=substr $tmp,0,2;my $tmp3=substr $tmp,2,3;my $tmp4=substr $tmp,5,3;return $tmp2.&quot;,&quot;.$tmp3.&quot;,&quot;.$tmp4.&quot; bytes&quot;;}
if ($len==9) { my $tmp2=substr $tmp,0,3;my $tmp3=substr $tmp,3,3;my $tmp4=susbtr $tmp,6,3;return $tmp2.&quot;,&quot;.$tmp3.&quot;,&quot;.$tmp4.&quot; bytes&quot;;}
}
}

sub format_time {
my ($time)=@_;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
$mon+=1;
if (length $hour == 1) {$hour= &quot;0&quot;.$hour;}
if (length $min == 1) {$min= &quot;0&quot;.$min;}
if (length $sec == 1) {$sec= &quot;0&quot;.$sec; }
if (length $mon == 1) {$mon= &quot;0&quot;.$mon;}

$year = &quot;20&quot;.substr $year, 1, 2;
return &quot;$year-$mon-$mday $hour:$min:$sec&quot;;
}
sub calc_diff_time {
my ($modtime) = @_;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
my $year1 = substr $modtime, 0,4;
my $mon1 = substr $modtime, 5, 2;
my $mday1 = substr $modtime, 7, 2;

$year = &quot;20&quot;.substr $year, 1, 2;
if ($year==$year1) {
if (($yday - $yday1) < 300) {
$newimg = &quot;<img src=\&quot;new2.gif\&quot; alt=\&quot;item has been added in the last 30 days\&quot;>&quot;;
} else {
$newimg = &quot;1&quot;;
}
} elsif (($year-$year1)!=1) {
my $tmon = $mon;
$tmon+=12;
if (($tmon-$mon1)==1) {
$newimg=&quot;<img src=\&quot;new2.gif\&quot; alt=\&quot;item has been added in the last 30 days\&quot;>&quot;;
} else {
$newimg=&quot;&quot;;
}
} else {
$newimg=&quot;Slipped&quot;;
}
return $newimg;
}
 
On further consideration depending on your webserver, this would be default behaviour for the server if no default pages are present in the directory specified.

It really depends on the level of security required
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top