<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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 2 of the License, or
	(at your opinion) 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/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (isSet($_GET['id'])) {
	$id = intval($_GET['id']);
	
	//===========================================
	//Database: Connect
	//===========================================
	require_once("../../inc/inc_config.php");
	require_once("../../inc/inc_database.php");
	require_once('../../inc/pdf/fpdf.php');
	
	$dblink = new database();
	$dblink->connect();
	//===========================================
	//TIMEZONE SETTING + DATABASE CHECK
	//===========================================
	$timezonerow = $dblink->get_row("SELECT timezone FROM memht_config") or die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Attention:</b> Cannot find database tables!</td></tr></table>");
	$siteConfig['timezone'] = intval($timezonerow['timezone']);
	$tzNOW = "DATE_ADD(NOW(),INTERVAL ".$siteConfig['timezone']." HOUR)";
	//====================
	require_once("../../inc/inc_login.php");
	require_once("../../inc/inc_functions.php");
	require_once("../../inc/inc_readConfig.php");
	require_once("../../inc/inc_getinfo.php");
	require_once("../../inc/inc_ban.php");
	if ($row = $dblink->get_row("SELECT *,DATE_FORMAT(data, '%d/%m/%Y %H:%i') as data FROM memht_guide WHERE id=$id AND enabled=1")) {
		$row_conf = $dblink->get_row("SELECT * FROM memht_config");
		$sitename = outCodeB($row_conf['nome']);
		$siteurl = outCodeB($row_conf['site_url']);
			
		class PDF extends FPDF {
			//Page header
			function Header() {
				global $sitename,$siteurl;
				
				$this->SetTextColor(100,100,100);
				$this->SetFont('Arial','B',15);
				$this->Cell(0,10,$sitename,0,0);
				$this->Ln(10);
				$this->SetFont('Arial','I',10);
				$this->Cell(0,5,$siteurl,0,0);
				$this->SetDrawColor(100,100,100);
				$this->Line(11,27,199,27);
				$this->Ln(10);
			}
			
			//Page footer
			function Footer() {
				global $id,$siteurl,$nome;
				
				$this->SetTextColor(100,100,100);
				$this->SetY(-15);
				$this->SetDrawColor(100,100,100);
				$this->Line(11,275,199,275);
				$this->SetFont('Arial','I',8);
				$this->Cell(0,0,$siteurl.'/index.php?page=guide&op=readGuide&id='.$id.'&title='.mem_urlencode($nome),0,0,'C');
				$this->Ln(5);
				$this->Cell(0,0,'Page '.$this->PageNo().'/{nb}',0,1,'C');
				$this->Ln(5);
				$this->SetFont('Arial','I',7);
				$this->SetTextColor(150,150,150);
				$this->Cell(0,0,'MemHT Portal',0,0,'R',0,'http://www.memht.com');
			}
			
			//===HTML===
			var $B;
			var $I;
			var $U;
			var $HREF;
			
			function PDF($orientation='P',$unit='mm',$format='A4') {
				//Call parent constructor
				$this->FPDF($orientation,$unit,$format);
				//Initialization
				$this->B=0;
				$this->I=0;
				$this->U=0;
				$this->HREF='';
			}
			function WriteHTML($html) {
				//HTML parser
				$html=str_replace("\n",' ',$html);
				$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
				foreach($a as $i=>$e)
				{
					if($i%2==0)
					{
						//Text
						if($this->HREF)
							$this->PutLink($this->HREF,$e);
						else
							$this->Write(5,$e);
					}
					else
					{
						//Tag
						if($e{0}=='/')
							$this->CloseTag(strtoupper(substr($e,1)));
						else
						{
							//Extract attributes
							$a2=explode(' ',$e);
							$tag=strtoupper(array_shift($a2));
							$attr=array();
							foreach($a2 as $v)
								if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
									$attr[strtoupper($a3[1])]=$a3[2];
							$this->OpenTag($tag,$attr);
						}
					}
				}
			}
			
			function OpenTag($tag,$attr)
			{
				//Opening tag
				if($tag=='B' or $tag=='I' or $tag=='U')
					$this->SetStyle($tag,true);
				if($tag=='A')
					$this->HREF=$attr['HREF'];
				if($tag=='BR')
					$this->Ln(5);
			}
			
			function CloseTag($tag)
			{
				//Closing tag
				if($tag=='B' or $tag=='I' or $tag=='U')
					$this->SetStyle($tag,false);
				if($tag=='A')
					$this->HREF='';
			}
			
			function SetStyle($tag,$enable)
			{
				//Modify style and select corresponding font
				$this->$tag+=($enable ? 1 : -1);
				$style='';
				foreach(array('B','I','U') as $s)
					if($this->$s>0)
						$style.=$s;
				$this->SetFont('',$style);
			}
			
			function PutLink($URL,$txt)
			{
				//Put a hyperlink
				$this->SetTextColor(0,0,255);
				$this->SetStyle('U',true);
				$this->Write(5,$txt,$URL);
				$this->SetStyle('U',false);
				$this->SetTextColor(0);
			}
		}
		
		//--------------------
		
		$nome = outCodeB($row['nome']);
		$descrizione = outCodeB($row['descrizione']);
		$testo = outCodeB($row['testo']);
		$autore = outCodeB($row['autore']);
		$data = $row['data'];
		
		$content = $testo_home."<br><br>".$testo;
		$info = "$data by $autore";
		
		$pdf=new PDF();
		$pdf->AliasNbPages();
		$pdf->AddPage();
		$pdf->SetFont('Arial','',12);
		
		//Title
		$pdf->Ln(5);
		$pdf->SetFont('Arial','B',12);
		$pdf->Cell(0,0,$nome,0,0);
		
		//Info
		$pdf->Ln(5);
		$pdf->SetFont('Arial','I',8);
		$pdf->Cell(0,0,$info,0,0);
		
		//Content
		$pdf->SetFont('Arial','',10);
		$pdf->WriteHTML(html_entity_decode($content));
		$pdf->Output();
		
		//--------------------
	} else {
		echo "Error: Selected article does not exist!";
	}
	//===========================================
	//Database: Disconnect
	//===========================================
	$dblink->disconnect();
} else {
	echo "Error: No article selected!";
}

//Data output function (from database)
function outCodeB($string,$html=1) {
	if ($html==1) { $string = unhtmlentitiesB($string); }
	return stripslashes($string);
}

function unhtmlentitiesB($string,$html=1) {
	$trans_tbl1 = get_html_translation_table(HTML_ENTITIES);
	foreach ($trans_tbl1 as $ascii => $htmlentitie) {
		$trans_tbl2[$ascii] = '&#'.ord($ascii).';';
	}
	$string = str_replace("&#039;","'",$string);
	$string = str_replace("&#39;","'",$string);
	
	$trans_tbl1 = array_flip($trans_tbl1);
	$trans_tbl2 = array_flip($trans_tbl2);
	
	$tagstostrtip = array('iframe','script','style');
	$string = strtr(strtr($string,$trans_tbl1),$trans_tbl2);
	if ($html==1) { $string = strip_selected_tagsB($string,$tagstostrtip); }
	return $string;
}

function strip_selected_tagsB($text, $tags = array()) {
	$args = func_get_args();
	$text = array_shift($args);
	$tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
	foreach ($tags as $tag){
		if( preg_match_all( '/<'.$tag.'[^>]*>([^<]*)<\/'.$tag.'>/iu', $text, $found) ){
			$text = str_replace($found[0],$found[1],$text);
		}
	}
	return preg_replace( '/(<('.join('|',$tags).')(\\n|\\r|.)*\/>)/iu', '', $text);
}

?>