#!/usr/bin/perl --
#-------------------------------------------------------------------------------------#
#
#
#   ######         #                ###  #########
#   ##   ##                  #      ###     ###
#   ######  # ###  #  #*   #####    ###     ###
#   ##      ##     #  ####   #      ###     ###
#   ##      #      #  #  #   #      ###     ###
#   ##      #      #  #  #   ##     ###     ###
#
#
#
#-	PRINT IT					addon by:						 -#
#-														 -#
#- Kristjan Kolde - kolde@hot.ee - ICQ: 111059060 - http://www.npfactory.f2s.com/    -#
#-														 -#
#- Check out NewsPro Factory, the best place for NP users/addons/mods/tutorials and	 -#
#- much more.												 -#
#-------------------------------------------------------------------------------------#
# For instructions please read the readme file or dowload it at my addons page.       #
#-------------------------------------------------------------------------------------#
$VNSub = 'DoPrinterFriendlyHTML';	# Subrutin for the news, usually should be	  #
						# DoPrinterFriendlyHTML					  #

$VNTmpl = 'printit.tmpl';		# Template file for printing (normal printit.tmpl)#
$abspath= '.';				# Absoulte path to NewsPro if in same dir, leave "." #
#-------------------------------------------------------------------------------------#
# You should not edit nothing belove
#-------------------------------------------------------------------------------------#

eval { &main; };

sub DoPrinterFriendlyHTML {
$newshtml = qq~
<font face="Verdana, Arial"><font size="4">~;
if ($newscat) {
$newshtml .= qq~ $newscat - ~;
}
$newshtml .= qq~ $newssubject </font></b><br>
<font size="2" color="#333333"><br>Enviada por: $newsname [$newsemail] el $newsdate<br><a href="/">PUERTO MADRYN<a></font>
<hr width="100%" noshade size="1">
<font size="2" color="black">~;
if ($newssnippit) {
$newshtml .= qq~\n$newssnippit~;
}
if ($newssnippit & $newstext) {$newshtml .= qq~\n<br><img src="/1pix.gif" width="1" height="4"><br>~;}
if ($newstext) {
$newshtml .= qq~\n$newstext~;
}
$newshtml .= qq~\n</font>
~;
}

sub main {
# Microsoft servers. Try and be compatible...
if ($IIS != 2) {
	if ($IIS == 0) {
		if ($ENV{'SERVER_SOFTWARE'} =~ m!IIS!) {
			$IIS = 1 
		}
	}
	if (($IIS) && ($0 =~ m!(.*)(\\|\/)!)) {
		chdir($1);
	}
	if ($IIS == 1) {
		print "HTTP/1.0 200 OK\n";
	}
}
# That's over!

# Server problem workaround #1
if ($abspath) {
	push(@INC, $abspath);
}

require "npconfig.pl";
require "ndisplay.pl";
require "nplib.pl";
ReadForm();
SubmitFormFields();
push(@formfields, 'newstext');

unless ($scripturl) {
	$scripturl = &GetScriptURL;
}

ReadConfigInfo();
if (query_string() =~ /^print/) {
	&ShowNews;
}
}

sub ShowNews {
	print header();
	local ($content) = "";
	$OnlyNewNews = 0;
	query_string() =~ /^print(\S+)/;
	$newsparam = $1;
	if ($newsparam ne "") {
		&loadND;
		$newsnum = @NewsData;
		if ($newsparam eq "all") {
			$newsstart = 0;
			$newsend = $newsnum - 1;
		} elsif ($newsparam =~ /item(\d+)/) {
			$newsstart = $1;
			$newsend = $newsstart;
		} elsif ($newsparam =~ /(\S+)/) {
			$nid = $1;
			$nid =~ s/\_/\,/g;
			$nid =~ s/[^\d,]//g;
			$newsstart = $NewsID{$nid};
			$newsend = $newsstart;
		} else {
			$newsparam =~ /start(\d+)end(\d+)/;
			$newsstart = $1;
			$newsend = $2;
		}
		if (($newsstart < 0) || ($newsstart >= $newsnum) || ($newsend < $newsstart) || ($newsend >= $newsnum) || !defined $newsstart) {
		}
		$newscount = $newsend;
		while ($newscount >= $newsstart) {
			&getNDvar($newscount);
			$newsnum = $newscount;
			$DynamicPage = 1;
			&{$VNSub};
			$content .= $newshtml;
			$newscount--;
		}
		if ($newsstart == $newsend) { $gptitle = $newssubject; }
		&GenPage($gptitle, $content);
	}
}

sub GenPage {
	my $title = shift;
	my $content = shift;
	if ($abspath) {
		NPopen(TMPL, "$abspath/$VNTmpl");
	}
	else {
		NPopen(TMPL, "$VNTmpl");
	}
	@tmpl = <TMPL>;
	close(TMPL);
	$tmpl = join('', @tmpl);
	$tmpl =~ s/<!--INSTRUCTIONS[\s\S]+?END INSTRUCTIONS-->//g; 
	$tmpl =~ s/<Field\: ([^\s\>]+)>/${$1}/gi;
	$tmpl =~ s/<TextField\: ([^\s\>]+)>/${\(HTMLtoText(${$1}))}/g;
	$tmpl =~ s/<\!--#include file\s*?=\s*?"(\S+?)"\s*?-->/${\(VNFileLoad($1))}/gi;
	$tmpl =~ s/<InsertTitle>/$title/g;
	$tmpl =~ s/<InsertID>/$newsid/g;
	$tmpl =~ s/<InsertContent>/$content/;
	$tmpl =~ s/<InsertCategory>/$newscat/;
	$tmpl =~ s/<Insert>/$newscat/;
	$tmpl =~ s/<InsertTextContent>/${\(HTMLtoText($content))}/g;
	print $tmpl;
}

sub NPopen {
	my $handle = shift; my $path = shift;
	$path =~ s/([\&;\`'\|\"*\?\~\^\(\)\[\]\{\}\$\n\r])//g;
	open($handle, $path) ||
	return 1;
}



############### THE END #################