#!/usr/bin/perl
##########################################################
# News Script Xtra 05/May/2005
# © 1999-2005 EZscripting.com / Newsscript.co.uk
# Script by Alexandre Golovkine Updated by Babelnotes.be
##########################################################
# Instructions: http://www.newsscript.co.uk/instructions/
# FAQ: http://www.newsscript.co.uk/faq/
# Help Forum: http://www.newsscript.co.uk/helpforum/
##########################################################
my $script_name = 'http://www.savethebears.co.uk/scripts/newsscript.pl';
my $databasefile = "/home/sites/www.savethebears.co.uk/web/scripts/news.txt";
my $idField = "ID";
my $image_dir = "/home/sites/www.savethebears.co.uk/web/scripts/images";
my $image_url = "http://www.savethebears.co.uk/images";
my $html_template = "/home/sites/www.savethebears.co.uk/web/scripts/headlines.shtml";
my $record_template = "/home/sites/www.savethebears.co.uk/web/scripts/news.shtml";
my $admin_mode = 'editor';
my $select_image = 1;
my $add_fields = 1;
my $hspace = 9;
my $vspace = 2;
my $Username = 'jhulott';
my $Password = 'pebbles';
my $not_found = 'No results found ';
my $news_up = 1;
my $newstarget = "_self";
##########################################################
# Page Selector
my $textPrevious = "Previous";
my $textNext = "Next";
my $pageSelectorRange = 10;
my $records_per_page = 10;
##########################################################
# Scrolling News Headlines
my $Number_of_headlines_to_scroll = 5;
my $speed = 4;
my $bgcolor = "#eeeeee";
##########################################################
# News letter
my $Number_of_news = 10;
my $head_news_separator = "
";
my $news_news_separator = " ";
##########################################################
# Search Term Highlighting
my $highLighting = 1;
my @highLightColors = ("#FFFF00", "#FF0000", "#00FFFF", "#00FF00", "#C0C0C0");
##########################################################
# Pure HTML tag
my $start_pure_html = '';
my $end_pure_html = ' ';
##########################################################
# Ordering
my $order = ""; # abc, cba, 123 or 321
my $order_by = ""; # fieldname on which to order
##########################################################
# RSS Feed
my $usefeeds = 1;
my $feed = "feed.rss";
my $feed_title = "Save the Bears";
my $feed_description = "Read the latest news relating to Bears from around the world";
##########################################################
# EZscripting.com © 1999 - 2005
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts
# By downloading and using this script you agree to the terms and conditions
##########################################################
use CGI qw/:standard/;
$FORM{mode}=param('mode');
$FORM{record}=param('record');
$FORM{login}=param('login');
$FORM{password}=param('password');
$FORM{action}=param('action');
$FORM{new_field}=param('new_name');
$FORM{action}="default" if !$FORM{action};
$FORM{headlines} = param('headlines');
$FORM{search}=param('search');
$FORM{method}=param('method');
$FORM{field}=param('field');
#$FORM{order} = param('order');
#$FORM{order_by} = param('order_by');
$FORM{order} = $order;
$FORM{order_by} = $order_by;
$page=param('page')+1;
$default_mode=1 if param('mode') eq 'shtml';
$default_mode=2 if param('mode') eq 'newsletter';
$default_mode=3 if param('mode') eq 'scrollingnews';
my @field_name = ();
#check password
if($Username && $FORM{mode} eq $admin_mode){
%COOKIES = get_cookie('USER_Login');
if($FORM{login}){
if($FORM{password} eq $Password && $FORM{login} eq $Username){
print "Set-Cookie: USER_Login=login&$FORM{login}&pass&$FORM{password}; path=/\n";
print "Location: $script_name?mode=$admin_mode\n\n";
exit;
}
else{print "Content-type: text/html\n\n"; error("Wrong account info !");}
}
elsif(!$COOKIES{login}){promt();}
elsif($COOKIES{pass} ne $Password or $COOKIES{login} ne $Username){promt();}
}
if($FORM{action} eq 'logout'){
print "Set-Cookie: USER_Login=login&boerkenaas&pass&boerkenaas; path=/\n";
print "Location: $script_name\n\n";
exit;
}
sub CheckAdminMode {
if ($FORM{mode} ne $admin_mode) {
error("You need to be logged in!");
exit (1);
}
}
print "Content-type: text/html\n\n" unless $FORM{action} eq 'delete';
create_db() unless -f $databasefile;
my $txt;
%SUB = (
default=> \&default,
main => \&main,
add => \&add,
save => \&save,
logout => \&logout,
delete => \&delete,
update => \&update,
view => \&view,
show => \&show,
refresh_feed => \&refresh_feed,
add_field => \&add_field
);
$SUB{$FORM{action}}->();
html_text($txt);
exit;
##########################################################
sub read_file{
open(F, $_[0]) || error("Can't open file $_[0]!");
my @data = ;
close F;
return @data;
}
sub read_base{
my @content;
my @data = read_file("$databasefile");
chomp $data[0];
@field_name=split('\|',$data[0]);
my $p=0; my $idPosition=-1;
foreach(@field_name){
$idPosition = $p if $_ eq $idField;
$show_position=$p if $_ eq 'show'; $p++;
}
error ('Old style database, please use dbconvert.pl first!') if $idPosition<0;
error('Bad database file') if @field_name<1;
$word = $FORM{search};
$field = $FORM{field};
$method = $FORM{method};
my @keys;
$position=-1;
$p=0;
if($field){foreach(@field_name){if($_ eq $field){$position=$p; last;}$p++;}}
if($word=~m/\A"(.*)"\Z/){$keys[0]=$1;}
elsif(!$action and ($method eq "perfect" or $method eq "exact")){$keys[0]=$word;}
else{@keys= split(" ", $word);}
my $b =0;
for(1..@data-1){
chomp $data[$_];
@line=split('\|',$data[$_]);
my $a=0;
if($_[0] == 1){
if($line[$show_position] eq 'Yes'){
if(@keys){
if($field && $position>-1){
my $found=0;
foreach $word (@keys){
if($method eq "perfect"){if ($line[$position] eq $word){$found=1; last;} }
elsif($line[$position] =~m/$word/i){$found=1; last;}
}
if($found){
$content->[$b]->{'record'} = $line[0];
foreach $name (@field_name){$content->[$b]->{$name} = $line[$a++];}
$b++;
}
}
else{
my $found=0;
foreach $word (@keys){
foreach $value (@line){
if($method eq "perfect"){if($value eq $word){$found=1; last;}}
elsif($value=~m/$word/i){$found=1; last;}
}
}
if($found){
$content->[$b]->{'record'} = $line[0];
foreach $name (@field_name){$content->[$b]->{$name} = $line[$a++];}
$b++;
}
}
}
else{
$content->[$b]->{'record'} = $line[0];
foreach $name (@field_name){$content->[$b]->{$name} = $line[$a++];}
$b++;
}
}
}
else{
$content->[$_]->{'record'} = $line[0];
foreach $name (@field_name){$content->[$_]->{$name} = $line[$a++];}
}
}
return $content;
}
sub order{
my @new=();
my $valid_fieldname = 0;
my $orderfield = $FORM{order_by};
my $field = "";
my $entry;
chomp($orderfield);
my @fields = @field_name;
foreach $field(@fields)
{
if ($field eq $orderfield)
{
$valid_fieldname = 1;
};
};
if ($valid_fieldname)
{
if ($FORM{order} eq '123' or $FORM{order} eq '321')
{
@new = sort { $a->{$orderfield} <=> $b->{$orderfield} } @data;
if ($FORM{order} eq '321') { @new = reverse @new; }
}
if ($FORM{order} eq 'abc' or $FORM{order} eq 'cba')
{
@new = sort { uc($a->{$orderfield}) cmp uc($b->{$orderfield}) } @data;
if ($FORM{order} eq 'cba') { @new = reverse @new; }
}
}
else
{
print "Error : invalid field name $orderfield specified";
}
return @new;
}
sub get_record{
my $text = $_[0];
$text =~ s{\[\[(.*?)\]\]}{exists($INSERT{$1}) ? $INSERT{$1} : ""}gsex;
return $text;
}
sub get_html{
my @txt = read_file($_[0]);
foreach(@txt){$txt.=$_;}
$txt=~/(.*)(.*)<\/template>(.*)/s;
error("Template-tag not found!") if !$1 or !$2;
return ($1,$2,$3);
}
sub create_db{
open(F, ">$databasefile") || error ("Can't create DB-file!");
print F "$idField|date|headline|short_text|text|image|image_align|image2|image_align2|show\n";
close F;
}
sub default{
$records=$FORM{'headlines'};
if($FORM{mode} eq $admin_mode){main(); return;}
record($FORM{record}) if $FORM{record};
show() unless $default_mode;
if($default_mode ==3){
$records=$Number_of_headlines_to_scroll;
}
elsif($default_mode ==2){
($head2, $tmp2, $foot2) = get_html($record_template);
$records=$Number_of_news;
}
$data=read_base(1);
if($news_up){@data = reverse @$data;}
else{@data = @$data;}
if($FORM{order} || $FORM{order_by})
{
@sorteddata = order();
}
else
{
@sorteddata = @data;
}
$records=@sorteddata if $records > @sorteddata;
$records=@sorteddata if !$records;
$sorteddata =\@sorteddata; my $txt;
($head, $tmp, $foot) = get_html($html_template);
for(0..$records-1){
%INSERT={};
foreach $name (@field_name){$INSERT{$name}=$sorteddata->[$_]->{$name};}
$INSERT{image} = " " if $INSERT{image};
$INSERT{image2} = " " if $INSERT{image2};
if($INSERT{text} ne ''){
#$INSERT{image} = " " if $INSERT{image};
$INSERT{more} = " [$_]->{'record'}." target=\"$newstarget\">More... ";
}
if ($default_mode ==2){
$INSERT{headline} = "$INSERT{headline} ";
$down.= get_record($tmp2)."Top
$news_news_separator";
$INSERT{headline} =~s/name="news$_"/href="#news$_"/s;
$INSERT{more} = " More ";
}
elsif ($default_mode ==3){$marquee.=" [$_]->{'record'}." target=\"$newstarget\">$INSERT{headline} ";}
else{$INSERT{headline} = "[$_]->{'record'}." target=\"$newstarget\">$INSERT{headline} " if $INSERT{text} ne '';}
$result.= get_record($tmp);
}
if($default_mode ==3){$result="$marquee ";}
print $result;
print $head_news_separator.$down if $default_mode ==2;
exit;
}
sub record{
$recordID = shift;
$data=read_base();
($head, $tmp, $foot) = get_html($record_template);
for(@$data){
if($_->{record} eq $recordID){
foreach $name (@field_name){$INSERT{$name}=$_->{$name};}
last;
}
}
$INSERT{image} = " " if $INSERT{image};
$INSERT{image2} = " " if $INSERT{image2};
$result = get_record($tmp);
print get_record($head), $result, $foot;
exit;
}
sub add_field{
CheckAdminMode();
$data=read_base();
$txt .= qq|NewsScript.co.uk - News Content Management System
Return to list |;
$txt .= qq| Logout
Add new field in database |;
if($FORM{new_field}){
add_new_field($FORM{new_field});
$txt.= qq| Field <$FORM{new_field} > has added! |;}
else{
$txt.= qq|
\n";
return;
}
sub add_new_field{
CheckAdminMode();
my @data = read_file("$databasefile");
my $name = shift;
$name =~ s/ /_/g;
my $a=0;
chomp $data[0];
@fields = split('\|', $data[0]);
foreach(@fields){
error("Field <$name > already use in database!") if $name eq $_;
}
foreach (@data){
chomp $data[$a];
$data[0].="|".$name."\n" if $a==0;
$data[$a].="|\n" if $a!=0;
$a++;
}
open (F, ">$databasefile") || error("Can't open file for alter table");
print F join ("", @data);
close F;
}
sub not_in_tag
{
(my $string) = @_;
my @arr = split(//, $string);
my $num_tag_open = grep(/, @arr);
my $num_tag_close = grep(/>/, @arr);
return ($num_tag_open == $num_tag_close);
}
sub iindex {
(my $string, my $searchingfor) = @_;
my $idx = index(uc($string), uc($searchingfor));
if ($idx >= 0)
{
$_[1] = substr($string, $idx, length($searchingfor));
}
return $idx;
}
sub show{
my $searchTerms;
$data=read_base(1);
($head, $tmp, $foot) = get_html($html_template);
my $a=0;
if($news_up){@data = reverse @$data;}
else{@data = @$data;}
if($FORM{order} || $FORM{order_by})
{
@sorteddata = order();
}
else
{
@sorteddata = @data;
}
## do search term highlighting
my $rec;
my $searchRec = $FORM{search};
my $searchField = "";
if ($searchRec) { $searchField = "&search=$FORM{search}"; }
if ($FORM{field}) { $searchField .= "&field=$FORM{field}"; }
if ($FORM{method}) { $searchField .= "&method=$FORM{method}"; }
$searchTerms = $searchRec;
if ($highLighting)
{
foreach $rec(@sorteddata)
{
my @terms = split(/\s/, $searchTerms);
my $term;
my $cnt = 0;
foreach $term(@terms)
{
my $colorIndex = $cnt % (@highLightColors);
my $highLightColor = $highLightColors[$colorIndex];
my $fronthl = '';
my $endhl = ' ';
my $part;
foreach $part(($rec->{'headline'}, $rec->{'short_text'}))
{
my $temp1 = $part;
my $temp2 = "";
my $oldindex = 0;
my $i = iindex($temp1, $term);
$pos_in_tot_string = $i;
while ($i >= 0)
{
my $pre = substr($temp1, 0, $i);
$temp2 .= $pre;
if (not_in_tag(substr($part, 0, $pos_in_tot_string)))
{
$temp2 .= $fronthl.$term.$endhl;
}
else
{
$temp2 .= $term;
}
$temp1 = substr($temp1, $i+length($term));
$oldindex = $i;
$i = iindex($temp1, $term);
$pos_in_tot_string = length($temp2) + $i;
}
$temp2 .= $temp1;
$part = $temp2;
}
$cnt++;
}
}
}
## end ST highlighting
$sorteddata =\@sorteddata; my $txt;
$page = param('page');
$show_records = @sorteddata;
$records_per_page = $FORM{headlines} if $FORM{headlines};
$start = $page*$records_per_page;
$end = $start + $records_per_page-1;
$end = $show_records-1 if $end>$show_records-1;
for($start..$end){
%INSERT={};
foreach $name (@field_name){$INSERT{$name}=$sorteddata->[$_]->{$name};}
$INSERT{image} = " " if $INSERT{image};
$INSERT{image2} = " " if $INSERT{image2};
if($INSERT{text} ne ''){
$INSERT{more} = " [$_]->{'record'} target=\"$newstarget\">More... ";
$INSERT{headline} = "[$_]->{'record'} target=\"$newstarget\">$INSERT{headline} ";
}
$result.= get_record($tmp);
}
$heads = "&headlines=$FORM{headlines}" if $FORM{headlines};
#set page selector
$INSERT{records} = @sorteddata-1;
$INSERT{page_selector} = "";
$INSERT{page_selector}= "$textPrevious " if $page>0;
my $pageStart=0;
my $pageEnd = 0;
$pageEnd = $INSERT{records}/$records_per_page if $records_per_page;
if($pageSelectorRange){
$pageStart=$page- int($pageSelectorRange/2) if $page- int($pageSelectorRange/2)>0;
if($page$p|;}
}
$INSERT{page_selector}.= " $textNext " if $page < $INSERT{records}/$records_per_page-1 && $records_per_page>1;
$INSERT{search}.=param('search');
$foot = get_record($foot);
$head = get_record($head);
$result = $not_found unless @sorteddata;
print $head, $result, $foot;
exit;
}
sub main{
$data=read_base();
$txt .= qq|\n";
}
sub view{
$data=read_base(); my $a=0;
$length = @$data-1;
$current = $FORM{record};
my $recData = {};
my @Indexes; my $i=0; my $index;
for(@$data){
if($current==$_->{'record'}){
$recData = $_;
$index=$i;
}
push @Indexes, $_->{'record'};
$i++;
}
$txt .= qq|NewsScript.co.uk - News Content Management System Return to List Logout |;
$txt .= qq| News story #$current of $length [add new ] [ edit ]
Date: $recData->{'date'}
headline: $recData->{'headline'}
Short text: $recData->{'short_text'}
Text: $recData->{'text'} |;
$txt .= qq| Image: {'image'}" align="$recData->{'image_align'}"> | if $recData->{'image'};
$txt .= qq| Image2: {'image2'}" align="$recData->{'image_align2'}"> | if $recData->{'image2'};
$txt .= qq| Show: $recData->{'show'} |;
if($add_fields){
for(10..@field_name-1){$txt .= qq|$field_name[$_]: $recData->{$field_name[$_]} \n|;}
}
$txt .= qq| |;
$txt .= qq|<< previous | unless $index==1;
for(1..@Indexes-1){
$txt .= qq| [|;
$txt .= qq|| unless $_ eq $FORM{record};
$txt .= qq|$_|;
$txt .= qq| | unless $_ eq $FORM{record};
$txt .= qq|] |;
}
$txt .= qq| next >>| if $index<$length;
$txt .= qq|
|;
}
sub save_image{
$file=param($_[0]);
$file =~m/([^\\\/]*\.\w*\Z)/i;
$filename=$1;
open(FILE,">$_[1]/$1") || error("Can't save image file!");
binmode FILE;
while ($bytesread=read($file,$buffer,1024)) {print FILE $buffer;}
close(FILE);
return "$filename";
}
sub save{
CheckAdminMode();
$data = read_base();
#find position:
my $pos =0; my $i;
for(@$data){
if($FORM{record} eq $_->{'record'}){$pos=$i;}
$maxId = $_->{'record'} if $_->{'record'}>$maxId;
$i++;
}
$maxId++;
my $new_record;
foreach(@field_name){
my $text;
if($_ eq $idField){
$text.= $FORM{record}? $FORM{record}:$maxId;
}
elsif($_ eq 'image' && param('image') ne ''){
$dir = $image_dir; $text = $image_url."/";
$text .= save_image($_, $dir);
}
elsif($_ eq 'image' && param('_image')){$text .=param('_image');}
elsif($_ eq 'image' && param('set_image')){$text .="$image_url/".param('set_image');}
elsif($_ eq 'image2' && param('image2') ne ''){
$dir = $image_dir; $text = $image_url."/";
$text .= save_image($_, $dir);
}
elsif($_ eq 'image2' && param('_image2')){$text .=param('_image2');}
elsif($_ eq 'image2' && param('set_image2')){$text .="$image_url/".param('set_image2');}
elsif($_ eq 'show'){$text=param($_); $text="No" unless param($_);}
else{
$text=param($_);
$text=~s/\r//g;
$text=~s/\|/I/g;
if ($_ eq 'text')
{
my @split_text = split(/\n/, $text);
my $nobreaks = 0;
my $line;
foreach $line(@split_text)
{
if ($line =~/$start_pure_html/) { $nobreaks = 1;}
if ($line =~/$end_pure_html/) { $nobreaks = 0;}
if (!$nobreaks) { $line .= ' ';}
}
$text = join(//,@split_text);
}
else { $text=~s/\n/ /g; }
}
$new_record .= $text."|";
}
chop $new_record;
$new_record .= "\n";
if(param('record')){
open (F, "$databasefile") || error("Can't open databasefile!");
@data = ;
close F;
$data[$pos] = $new_record;
open (F, ">$databasefile") || error("Can't open databasefile!");
foreach(@data) {print F }
close F;
}
else{
open (F, ">>$databasefile") || error("Can't open databasefile!");
print F $new_record;
close F;
}
if ($usefeeds) { make_feed(); }
main();
}
sub delete{
CheckAdminMode();
$data=read_base();
my $pos=-1; $i;
for(@$data){
if($FORM{record}==$_->{'record'}){
$pos=$i;
last;
}
$i++;
}
if($FORM{record}){
open (F, "$databasefile") || error("Can't open databasefile!");
@data = ;
close F;
$data[$pos] = "";
open (F, ">$databasefile") || error("Can't open databasefile!");
foreach(@data) {print F }
close F;
}
if ($usefeeds) {make_feed();}
print "Location: $script_name?mode=$admin_mode\n\n";
exit;
}
sub add{ CheckAdminMode();
$data=read_base();
my $recData = {};
for(@$data){
if($_[0]==$_->{'record'}){
$recData = $_;
last;
}
}
if ($_[0]){
$hidden=qq| |;
$recData->{'short_text'}=~s/ /\n/g;
$recData->{'text'}=~s/ /\n/g;
$image = " {'image'} checked>{'image'} target=_blank>$recData->{'image'} " if $recData->{'image'};
$image2 = " {'image2'} checked>{'image2'} target=_blank>$recData->{'image2'} " if $recData->{'image2'};
}
if($select_image){
opendir(DIR, "$image_dir");
foreach(readdir DIR)
{
$img_select.="$_ \n" if -f "$image_dir/$_" && $_=~m/.\gif|.\jpg/i;
}
close DIR;
$img_select1 = "or select image $img_select ";
$img_select2 = "or select image $img_select ";
}
$show = "checked" unless $recData->{'show'} eq "No";
$left = "selected" if $recData->{'image_align'} eq "left";
$center = "selected" if $recData->{'image_align'} eq "center";
$right = "selected" if $recData->{'image_align'} eq "right";
$left2 = "selected" if $recData->{'image_align2'} eq "left";
$center2 = "selected" if $recData->{'image_align2'} eq "center";
$right2 = "selected" if $recData->{'image_align2'} eq "right";
$hidden.= qq|
| if $FORM{login};
$txt .= qq||;
}
sub update{
CheckAdminMode();
add($FORM{record});
}
sub get_cookie{
my %COOK;# print $ENV{HTTP_COOKIE};
@cookies=split('; ',$ENV{HTTP_COOKIE});
foreach $line (@cookies){
($c_name, $c_value) = split(/=/,$line,2);
if ($c_name eq $_[0]){
@cook=split(/&/,$c_value);
for($a=0; $a<@cook; $a+=2){
$cook[$a+1] =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$COOK{$cook[$a]}=$cook[$a+1];
}
}
}
return %COOK;
}
sub promt{
print "Content-Type: text/html\n\n";
print <
EOF
exit;
}
sub error{
html_text ("$_[0] \n");
exit;
}
sub html_text{
print qq|
NewsScript.co.uk - News Content Management System
$_[0]
|;
}
sub refresh_feed {
CheckAdminMode();
make_feed();
main();
}
sub make_feed {
CheckAdminMode();
$data=read_base(1);
@data = @$data;
my $feedout = "";
$feedout .= <
$feed_title
$script_name
$feed_description
EOF
my $rec; my $cnt = 1;
foreach $rec(@data)
{
if ($rec->{'show'} eq 'Yes')
{
my $shorttext = $rec->{'short_text'};
$shorttext =~ s/ //g;
$feedout .= <
$rec->{'headline'}
$script_name?record=$rec->{$idField}
EOF
} #$shorttext
$cnt++;
}
$feedout .= <
EOF
open(FEED, "> $feed") || error("Cannot create rss feed $feed");
print FEED $feedout;
close(FEED)
}
##########################################################
# EZscripting.com © 1999 - 2005
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts
# By downloading and using this script you agree to the terms and conditions
##########################################################