#!/usr/bin/perl #------------------------------------------------------------------------------- # Be_Random COPYRIGHT NOTICE # # Be_Random is copyright (c) 2001-2002 by Be The Web, all rights reserved. # The use of this script is governed by the License Agreement which is # available from the WWW site at: http://www.BeTheWeb.com/ # # Pursuant to the License Agreement, this copyright notice may not be # removed or altered in any way. # #------------------------------------------------------------------------------- $Version = '2.1.0'; &SetDirectory; #Get CGI Information use CGI; $query = new CGI; #Set Variables $SettingsFile = 'be_random.cfg'; eval { require "$SettingsFile"; }; $@=''; @Fields = qw(ArticleNumber ActiveArticle Category ArticleText); $FieldFile = $DataPath . 'randomfields.txt'; $RandomFile = $DataPath . 'random.txt'; #Read in command and set to lower case $Show = $query->param("show"); $Show =~ tr/A-Z/a-z/; #Is Mode SSI or NoSSI and set to lower case $Mode=$query->param("mode"); $Mode =~ tr/A-Z/a-z/; #Read in Article Number $ArticleNum = $query->param("articlenum"); if ( $Mode eq "nossi" ) { print "Content-type: application/x-javascript\n"; print "Pragma: no-cache\n\n"; } else { print "Content-type: text/html\n\n"; } &LoadArticles; if ($Show eq "displayall") { # Display All Articles - Within an HTML Page &ShowAll; } else { #Show an Article if ( $ArticleNum ){ &ShowAll; } else { #Display a random article $Rec=@Records[int rand(@Records)]; &DisplayArticle; } } #============================================================================== # Subroutines Below here #============================================================================== #________________________________________________________________________ # This subroutine will load all article based on criteria that may # be passed into the script sub LoadArticles { #Copy records to array open (DATA, $RandomFile); @TRecords=; close (DATA); $mctr=0; #Setup Category for Matching $MatchCategory=$Category; foreach $Rec (@TRecords) { chomp($Rec); @data_fields = split(/\|/,$Rec); #Load data into $VAR array $ctr=0; foreach $rfield (@Fields) { $VAR{$rfield} = @data_fields[$ctr]; $ctr++; } if ($ArticleNum == $VAR{"ArticleNumber"} ) { @Records[$mctr]=$Rec; $mctr++; } else { if ( ! $ArticleNum ) { @Records[$mctr]=$Rec; $mctr++; } } } } #________________________________________________________________________ # This subroutine will display an article record sub DisplayArticle { #Load data into $VAR array @data_fields = split(/\|/,$Rec); $ctr=0; foreach $rfield (@Fields) { $VAR{$rfield} = @data_fields[$ctr]; $ctr++; } $out=$VAR{"ArticleText"}; if ( $Mode eq "nossi" ) { print "document.writeln(\"$out\");"; print "document.writeln(\"\");"; } else { print "$out\n"; print ""; } } #________________________________________________________________________ # This subroutine will display all articles that were loaded sub ShowAll { foreach $Rec (@Records) { &DisplayArticle; } } #________________________________________________________________________ # This subroutine sets the current directory to the directory that the # script is ran from. This is mostly needed by Windows NT sub SetDirectory { $Path=$ENV{'SCRIPT_FILENAME'}; if ( ! $Path ) { $Path=$0}; $slash = rindex($Path, "/" ); if ($slash == -1 ) { $slash = rindex ($Path, "\\" ) } $Directory = substr ( $Path, 0, $slash+1); chdir ( $Directory ); }