#!/usr/bin/perl # Fresh News Updater Version 1.0 by Webfresh (TM) # Copyright ©1999-2000 Webfresh All Rights Reserved # ######################################################### # DEFINE THIS CONFIGURATION VARIABLE! # Message listed when no news are stored. # Any HTML allowed (note the backslash in front of quotation marks \") $no_news = "There are currently no news to display.

Please check back soon for news."; ######################################################### # NO EDITING REQUIRED BELOW ######################################################### # PATH INFO if ($0=~m#^(.*)(\\|/)#) { $dir = $1; } else { $dir = `pwd`; chomp $dir; } $script_url = $ENV{'SCRIPT_NAME'}; $data_dir = "$dir/data"; $news_dat = "$data_dir/news.dat"; $news_info = "$data_dir/info.dat"; %in = &ReadForm; ######################################################### # GET CONTENT if($news) { open(NEWS,"<$news_dat"); while() { ($ID,$news_date,$news_head,$news_body) = split(/\|/,$_); $push = "$ID\|$news_date\|$news_head\|$news_body\n"; push(@reprint,$_); } close(NEWS); } ######################################################### # NEWS HEADLINE &Template("$data_dir/news.temp"); open(INFO,"$news_info"); @info = ; close(INFO); $content_info = @info[0]; ($news_name, $news_per_page) = split(/\|/,$content_info); open(NEWS_DAT,"<$news_dat"); @news_dat_content = ; close(NEWS_DAT); foreach$content(@news_dat_content) { ($ID,$news_date,$news_head,$news_body) = split(/\|/,$content); $push = "$ID\|$news_date\|$news_head\|$news_body"; push(@news_dat,$push); } ######################################################### # NEWS BODY if($news ne "") { foreach$news_content(sort { $b <=> $a } @news_dat) { ($ID,$news_date,$news_head,$news_body) = split(/\|/,$news_content); if($news == $ID) { $display_body .= &Cell('news_body'); } } } ######################################################### # GENERATE NEWS HEADLINE $show_max = $news_per_page; if($news eq "") { for($news_content = $show; $news_content < scalar @news_dat;) { ($ID,$news_date,$news_head,$news_body) = split(/\|/,$news_dat[$news_content]); if($ID ne "") { $display_headline .= &Cell('news_headline'); } $news_content++; last if $news_content-$show == $show_max; } if($news_content > $show_max || $news_content < @news_dat) { if($news_content > $show_max) { $show_back = $show-$show_max; $back_span = "Backa"; } if($news_content < @news_dat) { if($news_content-$show == $show_max) { $show_next = $show+$show_max; $next_span = "Nästa"; } } $display_span .= &Cell("show_span"); } if(!$display_headline) { $display_headline = "$no_news" }; } print &Template("$data_dir/news.temp",'temp'); ######################################################### # TEMPLATE sub Template { local(*FILE); if ($_[1] eq 'temp') { print "Content-type: text/html\n\n" unless ($ContentType++ > 0); } elsif ($_[1] eq 'text') { print "Content-type: text/plain\n\n" unless ($ContentType++ > 0); } if (!$_[0]) { return "
\nTemplate : No file was specified
\n"; } elsif (!-e "$_[0]") { return "
\nTemplate : File '$_[0]' does not exist
\n"; } else { open(FILE, "<$_[0]") || return "
\nTemplate : Could open $_[0]
\n"; while () { $FILE .= $_; } close(FILE); for ($FILE) { s//\1/gi; # show hidden inserts s/(?:\r\n|\n)?(.*?)/ $CELL{$1}=$2;''/ges; # read/remove template cells s/\%(\w+)\%/${$1}/g; # translate %% } } return $FILE; } ######################################################### # TRANSLATE CELL sub Cell { my($CELL); for (0..$#_) { if ($_[$_]) { $CELL .= $CELL{$_[$_]}; }} if (!$_[0]) { return "
\nCell : No cell was specified
\n"; } elsif (!$CELL) { return "
\nCell : Cell '$_[0]' is not defined
\n"; } else { $CELL =~ s/\%(\w+)\%/${$1}/g; } # translate %% return $CELL; } ######################################################### # PARSE FORM sub ReadForm { my($max) = $_[1]; # Max Input Size my($name,$value,$pair,@pairs,$buffer,%hash); # localize variables # Check input size if max input size is defined if ($max && ($ENV{'CONTENT_LENGTH'}||length $ENV{'QUERY_STRING'}) > $max) { die("ReadForm : Input exceeds max input limit of $max bytes\n"); } # Read GET or POST form into $buffer if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } if ($ENV{'QUERY_STRING'}) { $buffer .= $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); # Split into name/value pairs foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # split into $name and $value $value =~ tr/+/ /; # replace "+" with " " $value =~ s/%([A-F0-9]{2})/pack("C", hex($1))/egi; # replace %hex with char $$name = $value; } return %hash; }