Guest_imported
New member
- Jan 1, 1970
- 0
I have the code below. I am trying to make it sort by the date modified. I am by no means a perl programmer so I basically have been banging my head against the wall for a month trying to get this to work. HELP!!!!!
Donnie
#!/usr/bin/perl
#
#
#########################
# This program will assemble 'on the fly' a web page full of images
# from a directory in your archive on your server. It is customizeable in that you
# use a pre-made HTML template for the output screen. You can make the template
#using all the colors and design of the rest of your site, making it fit right in.
#
# To install:
# 1. Edit the variables below.
# 2. Make a template web page. Use <--TITLE--> and <--IMAGES--> where you want
# the title and image to be inserted. It's OK to use a WYSIWYG editor. This script will
# weed out the < code.
# 3. Upload everything to it's place:
# The script: To your CGI BIN. Chmod 755
# The Template: To $TEMPLATE's path
# 4. Call the script with your browser: # 5. Note: if you do not have a template file, you will get an error.
########################################################
#
# NEW FEATURE:
# You can run this program stand alone, which will read the designated directory,
# Or you can run it to read different directories of images with a command line.
# # Where /image_dir/ is the dir you wish to display in relation to your public_html root.
# You must use the forward slashes around your image dir, or it will not work.
#
#########################################################
# E D I T V A R I A B L E S
#########################################################
$URL_ROOT = "# The HTTP root of your site.
$PAGE_TITLE = "<font face='Arial' size='4'><b>The Treasure Spot</b></font><font face='Arial' size='2'><br>Click thumbnail to see bigger picture, Click BACK to return to this page";
# The title of the results page. Set to "" for none.
$TEMPLATE= "/usr/home/spot/public_html/new/ai/aitemplate.html";
# the complete UNIX path and filename of your template
# Thumbnail size: both width and height of 0=do not thumbnail (real mode)
$width ="240";
$height = "240";
#########################################################
#
# Edit These two if you wish to process one and only one dir.
#
$UNIX_PATH = "";
# The unix path to the dir you wish to process.
$URL_PATH = "";
# The HTTP equivelent of $UNIX_PATH- but with no trailing slash.
##########################################################
#
# Enter how many images to show per page.
#
$max_images = 8;
# E N D E D I T V A R I A B L E S
#########################################################
#################
# Add and calculate the links for more and less pages
#################
print "Content-type: text/html\n\n";
#########################################
# Read and store the images in the dir
#########################################
$HTML_ROOT = "$ENV{'DOCUMENT_ROOT'}";
#
# split up path to get information
@split_up = split (/&/, $ENV{'QUERY_STRING'});
$remote_path= $split_up[0]; # get path for images to be listed
if ($remote_path ne ""
{$UNIX_PATH = "$HTML_ROOT"."$remote_path";}
opendir(DIR,"$UNIX_PATH"
|| &error ("Cannot open $UNIX_PATH"
;
#####
# Sort
#####
my @unsorted = map { [ $_, (stat "$logdir/$_"
[7,9] ] } readdir DIR;
my @sorted = sort { -C $b->[2] <=> -C $a->[2] } @unsorted;
foreach (@sorted)
{
$file = $_->[0];
$lcfile = lc($file);
if (($lcfile =~ /.gif/) || ($lcfile =~ /.tif/) || ($lcfile =~ /.jpg/) || ($lcfile =~ /.jpeg/))
{
push (@dir_list, "$file"
;
}
}
$count=@dir_list;
closedir(DIR);
# calculate image to start with here
if ($ENV{'QUERY_STRING'} =~ /&/) { $start_image = $split_up[1]; }
else { $start_image = 0; }
$total_images = $count;
$cgi_path = "ai.cgi?$remote_path";
# add to the page title.
$PAGE_TITLE .= "<br>" . &CalculatePrevNextList ($start_image,$max_images,$total_images,$cgi_path) . "<br>" . &Counts($start_image,$max_images,$total_images);
##############################
# Read and store the template:
##############################
open(TEMPLATE_file,"$TEMPLATE"
|| &error ("Can't open $TEMPLATE\n"
;
@html=<TEMPLATE_file>;
close(TEMPLATE_file);
#############################
# Start html insert routine
#############################
if ($remote_path ne ""
{$URL_PATH = "$URL_ROOT"."$remote_path";}
$output = "\n";
$image_count = 1; # only used for collecting correct images to show
$max_image_count = $start_image + $max_images; # only used for collect correct images to show
foreach $file(@dir_list)
{
if ($image_count > $start_image and $image_count <= $max_image_count)
{
if (($width eq "0"
&& ($height eq "0"
)
{
# got rid of url path from href
$output .= "<a href=\"$URL_PATH/$file\">\n<img src=\"$URL_PATH/$file\" ALT=\"Filename: $file\" border=0></a>\n";
}
else
{
$output .= "<a href=\"$URL_PATH/$file\">\n<img src=\"$URL_PATH/$file\" ALT=\"Filename: $file\" border=0 width=$width height=$height></a>\n";
}
}
$image_count ++;
}
#########################
# Now search and replace
#########################
foreach $line(@html)
{
$REP_TEXT="";
$REP_TEXT = "<--TITLE-->";
$line =~ s/$REP_TEXT/$PAGE_TITLE/gi;
$REP_TEXT="<--TITLE-->";
$line =~ s/$REP_TEXT/$PAGE_TITLE/gi;
$REP_TEXT = "<--IMAGES-->";
$line=~ s/$REP_TEXT/$output/gi;
$REP_TEXT="<--IMAGES-->";
$line =~ s/$REP_TEXT/$output/gi;
if (($line =~/<\/body>/) || ($line =~/<\/html>/)) {$line="";}
print "$line";
}
print <<__HTML;
</body>
</html>
__HTML
exit;
######################
# ERROR HANDLING
######################
sub error
{
print "<HTML><BODY>";
print "<BOLD><p><FONT size=+1>Auto Image Error:<FONT COLOR=\"#FF0000\"> <B>$_[0]</B></FONT></FONT><BR>";
exit;
}
#
# Shows a summary of how many images in list
#
sub Counts {
my $StartWith = shift;
my $PerPage = shift;
my $TotalRecords = shift;
my $StartRecord = $StartWith + 1;
my $EndRecord = $StartWith + $PerPage;
if ($EndRecord > $TotalRecords) { $EndRecord = $TotalRecords; }
return "$StartRecord-$EndRecord out of $TotalRecords";
}
#
# Calculate next and previous links
#
sub CalculatePrevNextList {
my $LimitNow = shift; ### Where the search is now.
my $PerPage = shift; ### Amount to show per page.
my $MaxRecords = shift; ### MaxRecords.
my $Link = shift; ### Link to setup links to more records.
#print "$LimitNow <br>\n";
#print "$PerPage <br>\n";
#print "$MaxRecords <br>\n";
#print "$Link <br>\n";
######################
### Setup LimitNow ###
my ($MaxOnPage);
my $TotalLinks;
###########################
### Calculate Prev Link ###
my $Prev;
my $QuitPrev = 0;
my $PrevStart = 0;
$PrevStart = $LimitNow;
#print "$QuitPrev <br>\n";
my @Prev;
my $ShowBackLink = 1;
while ($QuitPrev == 0) {
# Subtract start amount from PerPage.
#print "Prev Start -- $PrevStart - $LimitNow - $PerPage <br>\n\n";
$PrevStart = $PrevStart - $PerPage;
#print "Prev Start -- $PrevStart < 0 <br>\n\n";
# Test if less than 0
if ($PrevStart < 0) {
#print "No prev link \n\n";
$QuitPrev = 1;
}
else {
# Test if have added the back link yet
if ($ShowBackLink) {
$Prev = "<a href=\"$Link&$PrevStart&$PerPage\">Prev</a> ";
$ShowBackLink = 0;
}
my $Showing = &ShowRecords($PrevStart, $PerPage, $MaxRecords);
my $Prev .= "<a href=\"$Link&$PrevStart&$PerPage\" title=\"Show records \[$Showing]\">%%TOTALLINKS</a>\n ";
unshift (@Prev, $Prev);
#print "Prev link = $Prev <br>\n\n";
}
}
for my $p (@Prev) {
$TotalLinks ++;
$p =~ s/%%TOTALLINKS/$TotalLinks/g;
#print "$p <br>\n";
$Prev .= "$p ";
}
$TotalLinks ++;
my $Now = $TotalLinks;
###########################
### Calculate Next Link ###
my $Next;
my $QuitNext = 0;
my $NextStart = 0;
#print "$QuitNext <br>\n";
my $FirstNext = 1;
my $NextInc = $LimitNow;
my $ShowNextLink = 1; # Variable flag to show next link and collect numbers
my $NextLinkStart; # Variable for starting next page
while ($QuitNext == 0) {
# Add start amount to PerPage.
#print "$NextStart $LimitNow + $PerPage <br>\n\n";
$NextStart = $NextInc + $PerPage;
#print "Next Start -- $NextStart $NextStart >= $MaxRecords <br>\n\n";
# Test if greater than Max Records.
if ($NextStart >= $MaxRecords) {
#print "No next link <br>\n\n";
$QuitNext = 1;
}
else {
$TotalLinks ++;
# Set amounts for next link
if ($ShowNextLink) {
$NextLinkStart = $NextStart;
$ShowNextLink = 0;
}
my $Showing = &ShowRecords($NextStart, $PerPage, $MaxRecords);
#my $Showing = 20;
#print "$Showing <br>\n";
$Next .= " <a href=\"$Link&$NextStart&$PerPage \" title=\"Show records \[$Showing]\">$TotalLinks</a>\n";
#print "Next -- $Next <br>\n\n";
}
$NextInc += $PerPage;
}
# Add next link at the end
if ($NextLinkStart ne "" or $NextLinkStart != 0) {
$Next .= " <a href=\"$Link&$NextLinkStart&$PerPage\">Next</a>";
}
my $Test = "$Prev $Now $Next";
return $Test;
}
#
# Show those records for that page
#
sub ShowRecords {
my $StartWith = shift;
my $PerPage = shift;
my $TotalRecords = shift;
#print "$StartWith, $PerPage, $TotalRecords <br>\n";
my $StartRecord = $StartWith + 1;
my $EndRecord = $StartWith + $PerPage;
if ($EndRecord > $TotalRecords) { $EndRecord = $TotalRecords; }
my $Totals = "$StartRecord-$EndRecord";
return $Totals;
}
Donnie
#!/usr/bin/perl
#
#
#########################
# This program will assemble 'on the fly' a web page full of images
# from a directory in your archive on your server. It is customizeable in that you
# use a pre-made HTML template for the output screen. You can make the template
#using all the colors and design of the rest of your site, making it fit right in.
#
# To install:
# 1. Edit the variables below.
# 2. Make a template web page. Use <--TITLE--> and <--IMAGES--> where you want
# the title and image to be inserted. It's OK to use a WYSIWYG editor. This script will
# weed out the < code.
# 3. Upload everything to it's place:
# The script: To your CGI BIN. Chmod 755
# The Template: To $TEMPLATE's path
# 4. Call the script with your browser: # 5. Note: if you do not have a template file, you will get an error.
########################################################
#
# NEW FEATURE:
# You can run this program stand alone, which will read the designated directory,
# Or you can run it to read different directories of images with a command line.
# # Where /image_dir/ is the dir you wish to display in relation to your public_html root.
# You must use the forward slashes around your image dir, or it will not work.
#
#########################################################
# E D I T V A R I A B L E S
#########################################################
$URL_ROOT = "# The HTTP root of your site.
$PAGE_TITLE = "<font face='Arial' size='4'><b>The Treasure Spot</b></font><font face='Arial' size='2'><br>Click thumbnail to see bigger picture, Click BACK to return to this page";
# The title of the results page. Set to "" for none.
$TEMPLATE= "/usr/home/spot/public_html/new/ai/aitemplate.html";
# the complete UNIX path and filename of your template
# Thumbnail size: both width and height of 0=do not thumbnail (real mode)
$width ="240";
$height = "240";
#########################################################
#
# Edit These two if you wish to process one and only one dir.
#
$UNIX_PATH = "";
# The unix path to the dir you wish to process.
$URL_PATH = "";
# The HTTP equivelent of $UNIX_PATH- but with no trailing slash.
##########################################################
#
# Enter how many images to show per page.
#
$max_images = 8;
# E N D E D I T V A R I A B L E S
#########################################################
#################
# Add and calculate the links for more and less pages
#################
print "Content-type: text/html\n\n";
#########################################
# Read and store the images in the dir
#########################################
$HTML_ROOT = "$ENV{'DOCUMENT_ROOT'}";
#
# split up path to get information
@split_up = split (/&/, $ENV{'QUERY_STRING'});
$remote_path= $split_up[0]; # get path for images to be listed
if ($remote_path ne ""
opendir(DIR,"$UNIX_PATH"
#####
# Sort
#####
my @unsorted = map { [ $_, (stat "$logdir/$_"
my @sorted = sort { -C $b->[2] <=> -C $a->[2] } @unsorted;
foreach (@sorted)
{
$file = $_->[0];
$lcfile = lc($file);
if (($lcfile =~ /.gif/) || ($lcfile =~ /.tif/) || ($lcfile =~ /.jpg/) || ($lcfile =~ /.jpeg/))
{
push (@dir_list, "$file"
}
}
$count=@dir_list;
closedir(DIR);
# calculate image to start with here
if ($ENV{'QUERY_STRING'} =~ /&/) { $start_image = $split_up[1]; }
else { $start_image = 0; }
$total_images = $count;
$cgi_path = "ai.cgi?$remote_path";
# add to the page title.
$PAGE_TITLE .= "<br>" . &CalculatePrevNextList ($start_image,$max_images,$total_images,$cgi_path) . "<br>" . &Counts($start_image,$max_images,$total_images);
##############################
# Read and store the template:
##############################
open(TEMPLATE_file,"$TEMPLATE"
@html=<TEMPLATE_file>;
close(TEMPLATE_file);
#############################
# Start html insert routine
#############################
if ($remote_path ne ""
$output = "\n";
$image_count = 1; # only used for collecting correct images to show
$max_image_count = $start_image + $max_images; # only used for collect correct images to show
foreach $file(@dir_list)
{
if ($image_count > $start_image and $image_count <= $max_image_count)
{
if (($width eq "0"
{
# got rid of url path from href
$output .= "<a href=\"$URL_PATH/$file\">\n<img src=\"$URL_PATH/$file\" ALT=\"Filename: $file\" border=0></a>\n";
}
else
{
$output .= "<a href=\"$URL_PATH/$file\">\n<img src=\"$URL_PATH/$file\" ALT=\"Filename: $file\" border=0 width=$width height=$height></a>\n";
}
}
$image_count ++;
}
#########################
# Now search and replace
#########################
foreach $line(@html)
{
$REP_TEXT="";
$REP_TEXT = "<--TITLE-->";
$line =~ s/$REP_TEXT/$PAGE_TITLE/gi;
$REP_TEXT="<--TITLE-->";
$line =~ s/$REP_TEXT/$PAGE_TITLE/gi;
$REP_TEXT = "<--IMAGES-->";
$line=~ s/$REP_TEXT/$output/gi;
$REP_TEXT="<--IMAGES-->";
$line =~ s/$REP_TEXT/$output/gi;
if (($line =~/<\/body>/) || ($line =~/<\/html>/)) {$line="";}
print "$line";
}
print <<__HTML;
</body>
</html>
__HTML
exit;
######################
# ERROR HANDLING
######################
sub error
{
print "<HTML><BODY>";
print "<BOLD><p><FONT size=+1>Auto Image Error:<FONT COLOR=\"#FF0000\"> <B>$_[0]</B></FONT></FONT><BR>";
exit;
}
#
# Shows a summary of how many images in list
#
sub Counts {
my $StartWith = shift;
my $PerPage = shift;
my $TotalRecords = shift;
my $StartRecord = $StartWith + 1;
my $EndRecord = $StartWith + $PerPage;
if ($EndRecord > $TotalRecords) { $EndRecord = $TotalRecords; }
return "$StartRecord-$EndRecord out of $TotalRecords";
}
#
# Calculate next and previous links
#
sub CalculatePrevNextList {
my $LimitNow = shift; ### Where the search is now.
my $PerPage = shift; ### Amount to show per page.
my $MaxRecords = shift; ### MaxRecords.
my $Link = shift; ### Link to setup links to more records.
#print "$LimitNow <br>\n";
#print "$PerPage <br>\n";
#print "$MaxRecords <br>\n";
#print "$Link <br>\n";
######################
### Setup LimitNow ###
my ($MaxOnPage);
my $TotalLinks;
###########################
### Calculate Prev Link ###
my $Prev;
my $QuitPrev = 0;
my $PrevStart = 0;
$PrevStart = $LimitNow;
#print "$QuitPrev <br>\n";
my @Prev;
my $ShowBackLink = 1;
while ($QuitPrev == 0) {
# Subtract start amount from PerPage.
#print "Prev Start -- $PrevStart - $LimitNow - $PerPage <br>\n\n";
$PrevStart = $PrevStart - $PerPage;
#print "Prev Start -- $PrevStart < 0 <br>\n\n";
# Test if less than 0
if ($PrevStart < 0) {
#print "No prev link \n\n";
$QuitPrev = 1;
}
else {
# Test if have added the back link yet
if ($ShowBackLink) {
$Prev = "<a href=\"$Link&$PrevStart&$PerPage\">Prev</a> ";
$ShowBackLink = 0;
}
my $Showing = &ShowRecords($PrevStart, $PerPage, $MaxRecords);
my $Prev .= "<a href=\"$Link&$PrevStart&$PerPage\" title=\"Show records \[$Showing]\">%%TOTALLINKS</a>\n ";
unshift (@Prev, $Prev);
#print "Prev link = $Prev <br>\n\n";
}
}
for my $p (@Prev) {
$TotalLinks ++;
$p =~ s/%%TOTALLINKS/$TotalLinks/g;
#print "$p <br>\n";
$Prev .= "$p ";
}
$TotalLinks ++;
my $Now = $TotalLinks;
###########################
### Calculate Next Link ###
my $Next;
my $QuitNext = 0;
my $NextStart = 0;
#print "$QuitNext <br>\n";
my $FirstNext = 1;
my $NextInc = $LimitNow;
my $ShowNextLink = 1; # Variable flag to show next link and collect numbers
my $NextLinkStart; # Variable for starting next page
while ($QuitNext == 0) {
# Add start amount to PerPage.
#print "$NextStart $LimitNow + $PerPage <br>\n\n";
$NextStart = $NextInc + $PerPage;
#print "Next Start -- $NextStart $NextStart >= $MaxRecords <br>\n\n";
# Test if greater than Max Records.
if ($NextStart >= $MaxRecords) {
#print "No next link <br>\n\n";
$QuitNext = 1;
}
else {
$TotalLinks ++;
# Set amounts for next link
if ($ShowNextLink) {
$NextLinkStart = $NextStart;
$ShowNextLink = 0;
}
my $Showing = &ShowRecords($NextStart, $PerPage, $MaxRecords);
#my $Showing = 20;
#print "$Showing <br>\n";
$Next .= " <a href=\"$Link&$NextStart&$PerPage \" title=\"Show records \[$Showing]\">$TotalLinks</a>\n";
#print "Next -- $Next <br>\n\n";
}
$NextInc += $PerPage;
}
# Add next link at the end
if ($NextLinkStart ne "" or $NextLinkStart != 0) {
$Next .= " <a href=\"$Link&$NextLinkStart&$PerPage\">Next</a>";
}
my $Test = "$Prev $Now $Next";
return $Test;
}
#
# Show those records for that page
#
sub ShowRecords {
my $StartWith = shift;
my $PerPage = shift;
my $TotalRecords = shift;
#print "$StartWith, $PerPage, $TotalRecords <br>\n";
my $StartRecord = $StartWith + 1;
my $EndRecord = $StartWith + $PerPage;
if ($EndRecord > $TotalRecords) { $EndRecord = $TotalRecords; }
my $Totals = "$StartRecord-$EndRecord";
return $Totals;
}