#!/usr/bin/perl
##########################################################
# NewsScript.co.uk Xtra 16/Nov/2004
# © 1999-2004 EZscripting.co.uk
# Script by Alexandre Golovkine
##########################################################
# Instructions: http://www.NewsScript.co.uk/instructions/
# FAQ:          http://www.NewsScript.co.uk/faq/
##########################################################

my $script_name       = 'http://www.gigigalli.com/cgi-lib/news/newsscript.cgi';
my $databasefile      = "/home/mhd/www.gigigalli.com/htdocs/cgi-lib/news/news.txt"; 
my $image_dir         = "/home/mhd/www.gigigalli.com/htdocs/imagesnews/";
my $image_url         = "http://www.gigigalli.com/imagesnews";
my $html_template     = "/home/mhd/www.gigigalli.com/htdocs/cgi-lib/news/headlines.htm";
my $record_template   = "/home/mhd/www.gigigalli.com/htdocs/cgi-lib/news/news.htm";
my $select_image      = 1;
my $add_fields        = 0;
my $hspace            = 9;
my $vspace            = 2;
my $Username          = 'news';
my $Password          = 'gigi4p';
my $not_found         =	'<b>Nessun risultato trovato</b><br><br><br>';
my $news_up           = 1;
##########################################################
# Page Selector
my $textPrevious      = "Precedente";
my $textNext          = "Prossima";
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  = "<hr noshade color=#000000><br>";
my $news_news_separator  = "<hr noshade color=#000000><br>";
##########################################################
# NewsScript.co.uk © 1999 - 2004
# The scripts are available for private and commercial use.
# You can use the scripts in any website you build.
# It is prohibited to sell the scripts in any format to anybody.
# The scripts may only be distributed by NewsScript.co.uk
# The redistribution of modified versions of the scripts is prohibited.
# NewsScript.co.uk 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');
$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';

#check password
if($Username && $FORM{mode} eq 'admin'){
	if($FORM{login}){
		unless($FORM{password} eq $Password && $FORM{login} eq $Username){
			print "Content-type: text/html\n\n"; error("Incorrect account!");
		}
		$addQs = "&login=$FORM{login}&password=$FORM{password}";
	}
	else{ promt();}
	
}
if($FORM{action} eq 'logout'){
	print "Location: $script_name\n\n";
}

print "Content-type: text/html\n\n";
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,
	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 = <F>;
	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;
	foreach(@field_name){$show_position=$p if $_ eq 'show'; $p++;}
	error('Bad database file') if @field_name<1;
	
	$word = param('search');
	my @keys;
	$method = param('method');
	$field = param('field');
	$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'} = $_;
						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'} = $_;
						foreach $name (@field_name){$content->[$b]->{$name} = $line[$a++];}
						$b++;
					}
				    }	
				}
				else{
					$content->[$b]->{'record'} = $_;
					foreach $name (@field_name){$content->[$b]->{$name} = $line[$a++];}
					$b++;
				}
			
			}
			
		}
		else{
			$content->[$_]->{'record'} = $_;
			foreach $name (@field_name){$content->[$_]->{$name} = $line[$a++];}
		}
		
	}
	return $content;
}
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>(.*)<\/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 "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'){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;}
	$records=@data if $records > @data;
	$records=@data if !$records;
	$data =\@data; my $txt;
	($head, $tmp, $foot) = get_html($html_template);
	
	for(0..$records-1){
		%INSERT={};
		foreach $name (@field_name){$INSERT{$name}=$data->[$_]->{$name};}
		$INSERT{image} = "<img src=\"$INSERT{image}\" border=0 align=$INSERT{image_align} hspace=$hspace vspace=$vspace>" if $INSERT{image};
		$INSERT{image2} = "<img src=\"$INSERT{image2}\" border=0 align=$INSERT{image_align2} hspace=$hspace vspace=$vspace>" if $INSERT{image2};
		if($INSERT{text} ne ''){
			#$INSERT{image} = "<img src=\"$INSERT{image}\" border=0 align=$INSERT{image_align} hspace=$hspace vspace=$vspace>" if $INSERT{image};
			$INSERT{more} = "&nbsp;&nbsp;<a href=$script_name?record=".$data->[$_]->{'record'}.">CONTINUA...</a>";
			
		}
		if ($default_mode ==2){
			$INSERT{headline} = "<a name=\"news$_\">$INSERT{headline}</a>";
		 	$down.= get_record($tmp2)."<p align=right><a href=\"#top\">Top</a></p>$news_news_separator";
			$INSERT{headline} =~s/name="news$_"/href="#news$_"/s;	
			$INSERT{more} = "&nbsp;&nbsp;<a href=\"#news$_\">CONTINUA</a>";
		}
		elsif ($default_mode ==3){$marquee.=" <a href=$script_name?record=".$data->[$_]->{'record'}.">$INSERT{headline}</font></a> &nbsp;&nbsp;&nbsp;";}
		else{$INSERT{headline} = "<a href=$script_name?record=".$data->[$_]->{'record'}.">$INSERT{headline}</a>" if $INSERT{text} ne '';}
		
		$result.= get_record($tmp);
		
	}
if($default_mode ==3){$result="<marquee scrollamount=$speed  bgcolor=$bgcolor>$marquee</marquee>";} 
print $result;
print $head_news_separator.$down if $default_mode ==2;
exit;	
}

sub record{
	$data=read_base();
	($head, $tmp, $foot) = get_html($record_template);
	foreach $name (@field_name){$INSERT{$name}=$data->[$_[0]]->{$name};}
	$INSERT{image} = "<img src=\"$INSERT{image}\" border=0 align=$INSERT{image_align} hspace=$hspace vspace=$vspace>" if $INSERT{image};
	$INSERT{image2} = "<img src=\"$INSERT{image2}\" border=0 align=$INSERT{image_align2} hspace=$hspace vspace=$vspace>" if $INSERT{image2};
	$result = get_record($tmp);

print get_record($head), $result, $foot;
exit;
}
sub add_field{
	$data=read_base();
	$txt .= qq|<table width=600><tr><th class=of colspan=4>NewsScript.co.uk - News Content Management System</th></tr><tr><td colspan=4 align=right>
		<a href=$script_name?mode=admin$addQs&action=add>Add new record</a>
		<a href=$script_name?mode=admin$addQs>Return to list</a>|;
	$txt .= qq|	<a href=$script_name?mode=admin$addQs&action=logout>Logout</a></td></tr>
		<tr><td colspan=4>&nbsp;</td><tr>
		<tr><th class=of colspan=4>Add new field in database</th><tr>|;
	
	if($FORM{new_field}){
		add_new_field($FORM{new_field});
		$txt.= qq|<td colspan=4 align=center><br><br><br>Field &lt;<b>$FORM{new_field}</b>&gt; has added!</td>|;}
	else{
		$txt.= qq|
		  <form action=$script_name>
		  <input type=hidden name=mode value=admin>
		  <input type=hidden name=action value=add_field>|;
		  
		$txt.= qq|<input type=hidden name=password value=$FORM{password}>
		  <input type=hidden name=login value=$FORM{login}>| if $FORM{login};
		$txt.= qq|  <tr><td colspan=2>Field name:</td><td colspan=2><input type=text name=new_name></td></tr>
		  <tr><td colspan=4 align=center><input type=submit value="[ submit ]"> <input type=reset value="[ reset ]"></td>
		  </form>
		|;
	}
	$txt.="</tr></table>\n";
return;
}
sub add_new_field{
	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 &lt;<i>$name</i>&gt; 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 show{
	$data=read_base(1);
	($head, $tmp, $foot) = get_html($html_template);
	my $a=0;
	if($news_up){@data = reverse @$data;}
	else{@data =  @$data;}
	$data =\@data; my $txt;
	$page = param('page');	
	$show_records = @data;
	$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}=$data->[$_]->{$name};}
			$INSERT{image} = "<img src=\"$INSERT{image}\" border=0 align=$INSERT{image_align} hspace=$hspace vspace=$vspace>" if $INSERT{image};
			$INSERT{image2} = "<img src=\"$INSERT{image2}\" border=0 align=$INSERT{image_align} hspace=$hspace vspace=$vspace>" if $INSERT{image2};
			if($INSERT{text} ne ''){
				$INSERT{more} = "&nbsp;&nbsp;<a href=$script_name?record=$data->[$_]->{'record'}>CONTINUA...</a>";
				$INSERT{headline} = "<a href=$script_name?record=$data->[$_]->{'record'}>$INSERT{headline}</a>";
			}
			$result.= get_record($tmp);
	}

	$heads = "&headlines=$FORM{headlines}" if $FORM{headlines};
	
        #set page selector
	$INSERT{records} = @data-1;
	$INSERT{page_selector} = "";  
	my $searchRec =param('search');
        $INSERT{page_selector}= "<a href=\"$script_name?page=".($page-1)."&action=show&search=$searchRec"."$heads\"\">$textPrevious</a>" 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<int($pageSelectorRange/2)){$pageEnd=$pageSelectorRange if $pageSelectorRange < $pageEnd;}
		else{$pageEnd=$page+ int($pageSelectorRange/2) if $page+ int($pageSelectorRange/2)<$pageEnd;}
	}
	
	for($pages=$pageStart; $pages <= $pageEnd; $pages++){
		$p=$pages+1;
		
		if($page==$pages){$INSERT{page_selector}.= " $p ";}
		else{$INSERT{page_selector}.= qq| <a href="$script_name?page=$pages&action=show&search=$searchRec|.qq|$heads">$p</a>|;}
		
	}  
	
	$INSERT{page_selector}.= " <a href=\"$script_name?page=".(1+$page)."&action=show&search=$searchRec"."$heads\"\">$textNext</a>" 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 @data;
	print $head, $result, $foot;
	exit;
}

sub main{
	$data=read_base();
	$txt .= qq|<table width=600><tr><th class=of colspan=4>NewsScript.co.uk - News Content Management System</th></tr><tr><td colspan=4 align=right><a href=$script_name?mode=admin&action=add$addQs>Add new record</a>|;
	$txt .= qq| <a href=$script_name?mode=admin&action=add_field$addQs>Add new field</a>| if $add_fields;	
	$txt .= qq|	<a href=$script_name?mode=admin&action=logout>Logout</a></td></tr>
		<tr class=on><th class=of width=120>Actions</th><th class=of width=80>Date</th><th class=of width=400>Headline</th><th class=of width=80>Show</th></tr>|;
		$start=1; $end=@$data-1;
		
		if($news_up){
			foreach($i=$end; $i>=$start;$i--){
				$txt.= qq|<tr class=off><td align=center valign=top><a href=$script_name?action=delete&record=$i$addQs onclick="return confirm('Delete record: «$data->[$i]->{headline}»')">Del.</a> <a href=$script_name?mode=admin&action=update&record=$i$addQs>Edit</a> <a href=$script_name?mode=admin&action=view&record=$i$addQs>View</a></td><td valign=top>$data->[$i]->{'date'}</td><td>$data->[$i]->{'headline'}</td>|;
				$txt.= qq|<td align=center>$data->[$i]->{'show'}</td>|;
				$txt.= qq| </tr>|;
			}
		}
		else{
			for($start..$end){
				$txt.= qq|<tr class=off><td align=center valign=top><a href=$script_name?action=delete&record=$_$addQs onclick="return confirm('Delete record: «$data->[$_]->{headline}»')">Del.</a> <a href=$script_name?mode=admin&action=update&record=$_$addQs>Edit</a> <a href=$script_name?mode=admin&action=view&record=$_$addQs>View</a></td><td valign=top>$data->[$_]->{'date'}</td><td>$data->[$_]->{'headline'}</td>|;
				$txt.= qq|<td align=center>$data->[$_]->{'show'}</td>|;
				$txt.= qq| </tr>|;
			}
		}
	$txt.="</table>\n";	

}
sub view{
	$data=read_base(); my $a=0;
	$length = @$data-1;
	$current = $FORM{record};
	$txt .= qq|<table width=600><tr><th class=of colspan=2>NewsScript.co.uk - News Content Management System</th></tr><tr><td colspan=4 align=right><a href=$script_name?mode=admin$addQs>Return to List</a> <a href=$script_name?mode=admin&action=logout>Logout</a></td></tr>|;
	$txt .= qq|<tr><td class=of colspan=2 align=left>&nbsp;&nbsp; <b>News story #$current of $length</b> [<a href=$script_name?mode=admin$addQs&action=add>add new</a>] [ <a href=$script_name?mode=admin$addQs&action=update&record=$FORM{record}>edit</a> ] </td></tr>
			<tr class=off><td width=100>Date:</td><td width=488>$data->[$FORM{record}]->{'date'}</td></tr>
			<tr class=off><td>headline:</td><td>$data->[$FORM{record}]->{'headline'}</td></tr>
			<tr class=off><td valign=top>Short text:</td><td>$data->[$FORM{record}]->{'short_text'}</td></tr>
			<tr class=off><td valign=top>Text:</td><td>$data->[$FORM{record}]->{'text'}</td></tr>|;
	$txt .= qq|	<tr class=off><td valign=top>Image:</td><td><img src="$data->[$FORM{record}]->{'image'}" border=0 alt"$data->[$FORM{record}]->{'image'}" align="$data->[$FORM{record}]->{'image_align'}"></td></tr>| if $data->[$FORM{record}]->{'image'};
	$txt .= qq|	<tr class=off><td valign=top>Image2:</td><td><img src="$data->[$FORM{record}]->{'image2'}" border=0 alt"$data->[$FORM{record}]->{'image2'}" align="$data->[$FORM{record}]->{'image_align2'}"></td></tr>| if $data->[$FORM{record}]->{'image2'};


	$txt .= qq|	<tr class=off><td valign=top>Show:</td><td>$data->[$FORM{record}]->{'show'}</td></tr>|;
	
	if($add_fields){
		for(9..@field_name-1){$txt .= qq|<tr class=off><td valign=top>$field_name[$_]:</td><td>$data->[$FORM{record}]->{$field_name[$_]}</td></tr>\n|;}
	}
	
	$txt .= qq|	<tr class=off><td colspan=2>&nbsp;&nbsp;|;
	$txt .= qq|&lt;&lt; <a href="$script_name?mode=admin$addQs&action=view&record=|.($current-1).qq|">previos</a> | unless $current==1;
	for(1..$length){
		$txt .= qq| [|;
		$txt .= qq|<a href="$script_name?mode=admin$addQs&action=view&record=$_">| unless $_ eq $FORM{record};
		$txt .= qq|$_|;
		$txt .= qq|</a>| unless $_ eq $FORM{record};
		$txt .= qq|] |;
	}
	$txt .= qq|&nbsp; <a href="$script_name?mode=admin$addQs&action=view&record=|.($current+1).qq|">next</a> &gt;&gt;| if $current<$length;
	$txt .= qq|</td></tr></table>|;

}
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{
	read_base();
	my $new_record;
	foreach(@field_name){
		my $text;
		if($_ 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/\n/<br>/g;
			$text=~s/\r//g;
			$text=~s/\|/I/g;
		}
		$new_record .= $text."|";
	}  
	chop $new_record;
	$new_record .= "\n";
	if(param('record')){
		open (F, "$databasefile") || error("Can't open databasefile!");
		@data = <F>;
		close F;	
		$data[$FORM{record}] = $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;
	}
	main();
}
sub delete{
	if($FORM{record}){
		open (F, "$databasefile") || error("Can't open databasefile!");
		@data = <F>;
		close F;	
		$data[$FORM{record}] = "";	
		open (F, ">$databasefile") || error("Can't open databasefile!");
		foreach(@data) {print F }
		close F;
	}
	main();
}
sub add{
	$data=read_base();
	if ($_[0]){
		$hidden=qq|<input type=hidden name=record value=$_[0]>|;
		
		$data->[$FORM{record}]->{'short_text'}=~s/<br>/\n/g;
		$data->[$FORM{record}]->{'text'}=~s/<br>/\n/g;
		$image = "<input type=checkbox name=_image value=$data->[$FORM{record}]->{'image'} checked><a href=$data->[$FORM{record}]->{'image'} target=_blank>$data->[$FORM{record}]->{'image'}</a><br>" if $data->[$FORM{record}]->{'image'};
		$image2 = "<input type=checkbox name=_image2 value=$data->[$FORM{record}]->{'image2'} checked><a href=$data->[$FORM{record}]->{'image2'} target=_blank>$data->[$FORM{record}]->{'image2'}</a><br>" if $data->[$FORM{record}]->{'image2'};
	
	}
	if($select_image){
		opendir(DIR, "$image_dir");
		foreach(readdir DIR)
		{
			$img_select.="<option>$_</option>\n" if -f "$image_dir/$_" && $_=~m/.\gif|.\jpg/i;
		}
		close DIR;
		$img_select1 = "<br><select name=set_image><option value=''>or select image</option>$img_select</select>";
		$img_select2 = "<br><select name=set_image2><option value=''>or select image</option>$img_select</select>";
	
	}
	$show = "checked" unless $data->[$FORM{record}]->{'show'} eq "No";
	$left = "selected" if $data->[$FORM{record}]->{'image_align'} eq "left";
	$center = "selected" if $data->[$FORM{record}]->{'image_align'} eq "center";
	$right = "selected" if $data->[$FORM{record}]->{'image_align'} eq "right";
	$left2 = "selected" if $data->[$FORM{record}]->{'image_align2'} eq "left";
	$center2 = "selected" if $data->[$FORM{record}]->{'image_align2'} eq "center";
	$right2 = "selected" if $data->[$FORM{record}]->{'image_align2'} eq "right";	
	$hidden.= qq|<input type=hidden name=password value=$FORM{password}>
		  <input type=hidden name=login value=$FORM{login}>| if $FORM{login};
	
	$txt .= qq|<table width=600><tr><th class=of colspan=2>NewsScript.co.uk - News Content Management System</th></tr><tr><td colspan=4 align=right><a href=$script_name?mode=admin$addQs>Return to List</a> <a href=$script_name?mode=admin$addQs&action=logout>Logout</a></td></tr>|;
	$txt .= qq|<form action=$script_name method=post  name=myform ENCTYPE="multipart/form-data">
		<input type=hidden name=mode value=admin>
		<input type=hidden name=action value=save>$hidden
			<tr class=off><td>Date:</td><td><input name=date value="$data->[$FORM{record}]->{'date'}"></td></tr>
			<tr class=off><td>headline:</td><td><input name=headline size=45  value="$data->[$FORM{record}]->{'headline'}"></td></tr>
			<tr class=off><td>Short text:</td><td><textarea name=short_text rows=4 cols=35>$data->[$FORM{record}]->{'short_text'}</textarea></td></tr>
			<tr class=off><td>Text:</td><td><textarea name=text rows=4 cols=35>$data->[$FORM{record}]->{'text'}</textarea></td></tr>|;
	$txt .= qq|	<tr class=off><td valign="top">Image:</td><td>$image<input type=file name=image>$img_select1</td></tr>
			<tr class=off><td>Image aligement:</td><td><select name=image_align><option $left>left<option $center>center<option $right>right</select></td></tr>
			<tr class=off><td valign="top">Image2:</td><td>$image2<input type=file name=image2>$img_select2</td></tr>
			<tr class=off><td>Image2 aligement:</td><td><select name=image_align2><option $left2>left<option $center2>center<option $right2>right</select></td></tr>
		        <tr class=off><td colspan=2><input type=checkbox name=show value="Yes" $show>Show this record.</td></td></tr>|;
			
	if($add_fields){
		for(9..@field_name-1){$txt .= qq|<tr class=off><td valign=top>$field_name[$_]:</td><td><input type=text name="$field_name[$_]" value="$data->[$FORM{record}]->{$field_name[$_]}"></td></tr>\n|;}
	}			
			
	$txt .= qq|	<tr class=off><td colspan=2 align=center><input type=submit value=" [ submit] "> <input type=reset value=" [ reset] "></td></tr></table></form></center>|;
}

sub update{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";
	    html_text(qq|
	      <br><br><br><br><center><form method=post action=$script_name>
	      <input type=hidden name=mode value=admin>
	      <table border=0 width=280>
	        <tr><th colspan=2 class=of>Content-Manager Login</th></tr>
		<tr class=on><td width=43% align=right>Login: </td>
		    <td width=57%><input type=text name=login size=13></td></tr>
		<tr class=on><td align=right>Password: </td>
		    <td><input type=password name=password size=13></td></tr>
		<tr class=on><td colspan=2 align=center><input type=submit></td></tr>
	      </table></form></center>|);
	exit;
}
sub error{
	html_text ("<br><br><br><center><font color=red><h3>$_[0]</h3></font></center>\n");
	exit;
} 
sub html_text{
	print qq|<html>
		<head>
		<title>NewsScript.co.uk - News Content Management System</title>
		<style>
			tr,td,body {font-family: Tahoma, Arial; font-size:10pt;}  
			.of {border-style:solid; border-width: 1; border-color : #999999; background-color:#dddddd}
			.on {border-style:none;background-color:#ddeedd}
			.off {border-style:none;background-color:#f3f3f3}
		</style>
		</head>
		<body>
		$_[0]
		</body></html>|;
} 
##########################################################
# NewsScript.co.uk © 1999 - 2004
# The scripts are available for private and commercial use.
# You can use the scripts in any website you build.
# It is prohibited to sell the scripts in any format to anybody.
# The scripts may only be distributed by NewsScript.co.uk
# The redistribution of modified versions of the scripts is prohibited.
# NewsScript.co.uk 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.
##########################################################
