#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


#this package intended to insert html code into another at certain places
# more specific, it provides the functions the edit-boxes when we are in template editor view.


package dnhtml_injector;


########################################################################
#constructor  - nothing cool

sub new{
	my $self = {};
	bless($self,$_[0]);
	return $self;
}

########################################################################
#sets html prefix which is to be injected _BEFORE the actual document
sub set_prefix{
	my $self = $_[0];
	$self->{'prefix'} = $_[1];
}

########################################################################
#sets html suffix which is to be injected _BEFORE the actual document
sub set_suffix{
	my $self = $_[0];
	$self->{'suffix'} = $_[1];
}
########################################################################
#injects prefix and suffix at suitable positions:
#-prefix at beginning or after <body>-tag if present
#-prefix at end or after </body>-tag if present
#needs html code txt in $_[1]
sub inject_html{
	my $self = $_[0];
	my $ttxt = $_[1];
	$ttxt = $self->_replace_links($ttxt);
	my $rv;	
	if($ttxt =~ m/\<body(.*)\>/i){
		$rv = $ttxt;		
		#this is a full html doc-> inject prefix after <body>, suffig before </body>
		$rv =~ s/\<body(.*)\>/($self->_prepare_string($&,$self->{'prefix'}))/ie;
		$rv =~ s/\<\/body(.*)\>/($self->_prepare_string($self->{'suffix'},$&))/ie;			
	}else{
		#no full html page

		my @tags = split(/\>/,$ttxt);
		my $found1 = -1;
		my $found2 = -1;
		my $fix_table = -1;
		my $cnt = 0;		
		
		
		
		
		#is there a <td or a <tr without a table => fix it
		while(($found1 == -1)&&($cnt < @tags)){
			if (($tags[$cnt] =~ /\<table/i)||($tags[$cnt] =~ /\<tbody/i)){
				$found1 = $cnt;			
			}
			$cnt++;
		} 
		 $cnt = 0;		
		while(($found2 == -1)&&($cnt < @tags)){
			if (($tags[$cnt] =~ /\<tr/i)||($tags[$cnt] =~ /\<td/i)){
				$found2 = $cnt;			
			}
			$cnt++;
		} 
		
		if($found2 != -1){
			if(($found1 >= $found2)||($found1 == -1)){

				$fix_table = 1;
			}
		}
		if($fix_table == -1){
			$rv = $self->{'prefix'}."\n".$ttxt."\n".$self->{'suffix'};
		}else{
			$rv = "</table>".$self->{'prefix'}."<table border = \"0\" width = \"100%\">\n".$ttxt."\n".$self->{'suffix'};
		}

	}
	return $rv;
}
########################################################################
#appends string $_[2] on $_[1] in a suitable way. 
sub _prepare_string{
	return $_[1]."\n".$_[2]."\n";
}

########################################################################
#replaces hyperlinks in html text $_[1] by links that enables html injection again 
sub _replace_links{
	my $self = $_[0];
	my $txt = $_[1];
	my $rv;
	$donparser::clientinfo->{'template_key'} = "do";
	$donparser::clientinfo->{'main_template'} = "main";
	$donparser::clientinfo->{'main_template_key'} = "main_template";
	$donparser::clientinfo->{'standard_template'} = "Standard";		
	$donparser::clientinfo->{'path_ident'} = "task";
	$donparser::clientinfo->{'default_path_ident'} = "user";
	push(@{$donparser::clientinfo->{'urls'}},$dnmain::settings->{'own_url'});

	my $parser = donparser->new();
	$parser->COF_set_link_handler_callback("dnhtml_injector::_handle_hyperlink");

	#separate tags to make sure there is only one URL to parse
	$donparser::follow_forms = "true";
	$rv = $parser->COF_scan($txt,"NO_PATH_REPLACE");
	$donparser::follow_forms = "false";
		
	
	return $rv;

}
########################################################################
#internal link handler callback for donparser_part_COF.pl
sub _handle_hyperlink{
	my $link = $_[1];
	my $rv;
	my $addstring = "username=$donparser::in{'username'}&session_key=$donparser::in{'session_key'}&editor_mode=true";
	
	#allready parsed?	
	if($link =~ m/session_key\=/){
		return $link;
	}else{
		#is first char after the url a question mark 
		if($link =~ m/\?/){
			$rv .= $link."&".$addstring;
		}else{
			$rv .= $link."?".$addstring;		
		}
		return $rv;	
	}
}
1;

