Location: PHPKode > projects > Enigma > Enigma2/Enigma2_Install/modules/Mod_News_pdf.php
<?php
/*
*****************************************************************

 Mod_News_pdf.php
 
*****************************************************************
LSP: Lunabyte Systems Portal
Open-Source Project Inspired by Zef Hemel (hide@address.com)
*****************************************************************
Software Version:                  LSP 2.0 "Enigma 2"
Software by:                         Lunabyte Systems (http://www.lunabyte.net)
Copyright 2002-2005 by:       Lunabyte Systems (http://www.lunabyte.net)
Support, News, Updates at:    http://www.lunabyte.net
*****************************************************************
This program is free software; you may redistribute it and/or modify it 
under the terms of the provided license as published by Lunabyte Systems.

This program is distributed in the hope that it is and will be useful,                  
but WITHOUT ANY WARRANTIES; without even any implied warranty of           
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                        

See the "LSP_license.txt" file for details of the LSP license.
The latest version can always be found at http://www.lunabyte.net.
*****************************************************************
*/

if (!defined('SMF'))
	die('<b>Access Violation</b><br />Direct Access to this location is not allowed.');

	global $modSettings, $db_prefix, $ID_MEMBER, $user_info, $settings, $context;

	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header( "Content-Type: application/pdf" );
	//header( "Content-Disposition: inline; filename=\"post.pdf\"" );

	$_PDFDIR = $modSettings['moduledir'] . '/pdf';
	require_once $_PDFDIR . '/class.ezpdf.php';

	class Enigmapdf extends Cezpdf
	{
		// horizontal rules
		function hr($width='')
		{
			$lineFactor = '0.05';

			$this->saveState();

			$this->setLineStyle($lineFactor);
			$this->line(10,$this->y,578,$this->y);

			$this->restoreState();
		}

		function imgsrc($info)
		{
			$this->ezImage($info['p'], 0, 75, NULL, 'left', 0);
		}

		function center($info)
		{
			$this->saveState();
			$justification = 'center';
			$this->restoreState();
		}

		function code($info)
		{
			$lineFactor='0.72';
			switch($info['status'])
			{
				case 'start':
				case 'sol':
					$this->ez['leftMargin'] = 25;
					$this->saveState();

					if (!isset($this->ez['links']))
					{
						$this->ez['links']=array();
					}
					$i = $info['nCallback'];
					$this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
					break;
				case 'end':
					$i = $info['nCallback'];
					$start = $this->ez['links'][$i];

					$a = deg2rad((float)$start['angle']-90.0);
					$drop = $start['height']*$lineFactor*1.5;
					//$drop = $start['height']/3.5;
					$dropx = cos($a)*($drop*2);
					$dropy = -sin($a)*($drop*2);


					$this->setLineStyle(0.05,'round','round');

					//make the box
					$this->setStrokeColor(128,128,128);
					$this->setColor(128,128,128);
					//$this->filledRectangle($start['x']-($this->ez['leftMargin']/2), $start['y']-$dropy, $this->ez['pageWidth']-($this->ez['rightMargin']*2), $info['y']-$dropy);

					// make the border
					$this->setStrokeColor(0,0,0);
					$this->rectangle($start['x']-($this->ez['leftMargin']/2), $start['y']-$dropy, $this->ez['pageWidth']-($this->ez['rightMargin']*2), $info['y']-$dropy);

					$this->restoreState();
					break;
				case 'eol':
					$this->ez['leftMargin'] = 25;
					break;
			}
		}

		function color($info)
		{
			switch($info['status'])
			{
				case 'start':
				case 'sol':
					if (!isset($this->ez['links']))
					{
						$this->ez['links']=array();
					}
					$i = $info['nCallback'];
					$this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
					$this->saveState();

					if (substr_count($info['p'], ':') != 2)
					{
						$color = $this->hex2dec($info['p']);
					}
					else
					{
						$c1 = explode(':', $info['p']);
						$color = array(
							'r'	=> $c1[0],
							'g'	=> $c1[1],
							'b'	=> $c1[2],
						);
					}

					$this->setColor($color['r'], $color['g'], $color['b']);
					break;
				case 'end':
				case 'eol':
					$i = $info['nCallback'];
					$get = $this->ez['links'][$i];
					$this->restoreState();
					break;
			}
		}

		function strike($info)
		{
			$lineFactor=0.05;
			switch($info['status'])
			{
				case 'start':
				case 'sol':
					// the beginning of the underline zone
					if (!isset($this->ez['links']))
					{
						$this->ez['links']=array();
					}
					$i = $info['nCallback'];
					$this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
					$this->saveState();
					$thick = $info['height']*$lineFactor;
					$this->setLineStyle($thick);
					break;
				case 'end':
				case 'eol':
					// the end of the link
					// assume that it is the most recent opening which has closed
					$i = $info['nCallback'];
					$start = $this->ez['links'][$i];
					// add underlining
					$a = deg2rad((float)$start['angle']-90.0);
					//$drop = $start['height']*$lineFactor*1.5;
					$drop = $start['height']/3.5;
					$dropx = cos($a)*$drop;
					$dropy = -sin($a)*$drop;
					$this->line($start['x']-$dropx,$start['y']+$dropy,$info['x']-$dropx,$info['y']+$dropy);
					$this->restoreState();
					break;
			}
		}

		function sub($info)
		{
			$lineFactor=0.05;
			switch($info['status'])
			{
				case 'start':
				case 'sol':
					// the beginning of the underline zone
					if (!isset($this->ez['links']))
					{
						$this->ez['links']=array();
					}
					$i = $info['nCallback'];
					$this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
					$this->saveState();
					//$this->ezSetDy($info['y']-$lineFactor);
					break;
				case 'end':
				case 'eol':
					// the end of the link
					// assume that it is the most recent opening which has closed
					$i = $info['nCallback'];
					$start = $this->ez['links'][$i];
					$this->restoreState();
					//$this->ezSetDy($start['y']);
					break;
			}
		}

		function table()
		{
			$table = array(
				0 => array('blah', 'blah'),
				1 => array('blah', 'blah'),
			);

			$this->ezTable($table, array('blah', 'blah'));
		}

		function hex2dec($hex)
		{
			$color = str_replace('#', '', $hex);
			$ret = array(
				'r' => hexdec(substr($color, 0, 2)),
				'g' => hexdec(substr($color, 2, 2)),
				'b' => hexdec(substr($color, 4, 2)),
			);
			return $ret;
		}

		function setinfo($title)
		{
			$this->info['title'] = $title;
		}
	} // End class Enigmapdf

	$print_topic = (int) $_REQUEST['topic'];

	// Get the topic starter information.
	$request = db_query("
		SELECT
			n.*, m.posterTime, m.subject, m.ID_TOPIC, m.ID_MEMBER, m.ID_MSG, m.ID_BOARD, b.name AS bName,
			m.body, t2.numReplies AS Replies, t2.numViews AS Views, nc.name AS CatName, nc.image AS CatImage,
			IFNULL(mem.realName, m.posterName) AS posterName, " . ($user_info['is_guest'] ? '1 AS isRead, 0 as logTime' : '
			IFNULL(lt.logTime, IFNULL(lmr.logTime, 0)) >= GREATEST(m.posterTime, m.modifiedTime) AS isRead,
			IFNULL(lt.logTime, IFNULL(lmr.logTime, 0)) AS logTime') . "
		FROM ({$db_prefix}news AS n, {$db_prefix}messages AS m, {$db_prefix}topics AS t2, {$db_prefix}boards AS b)
			LEFT JOIN {$db_prefix}news_categories AS nc ON (n.ID_CATEGORY = nc.ID_CATEGORY)
			LEFT JOIN {$db_prefix}topics AS t ON (n.ID_TOPIC = t.ID_FIRST_MSG)
			LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)" . (!$user_info['is_guest'] ? "
			LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = m.ID_TOPIC AND lt.ID_MEMBER = $ID_MEMBER)
			LEFT JOIN {$db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = m.ID_BOARD AND lmr.ID_MEMBER = $ID_MEMBER)" : '') . "
		WHERE n.ID_TOPIC = $print_topic AND n.ID_TOPIC = m.ID_TOPIC
			AND n.featureFlag = 1
			AND n.publish = 0
			AND n.stage = 1 AND reviewFlag = 0
			AND b.ID_BOARD = m.ID_BOARD
			AND m.ID_MSG = t2.ID_FIRST_MSG
		GROUP BY m.ID_MSG DESC
		LIMIT 1", __FILE__, __LINE__);

	if (mysql_num_rows($request) == 0)
		fatal_lang_error('smf232');

	$row = mysql_fetch_assoc($request);
	mysql_free_result($request);


	$title		= ucwords ($modSettings['NewsTitle'] . ', ' . $row['CatName']);
	$author 	= $row['posterName'];
	$subject	= ucwords (cleancodetages($row['subject']));
	$body		= cleancodetages($row['body']);

	$pdf =& new Enigmapdf('a4','P');

	$pdf->addInfo(array(
		'Title'		=> $title,
		'Author'	=> $author,
		'Subject'	=> $subject,
		'Creator'	=> 'BLAH BLAH ~ AQUILO ~ BLAH BLAH',
		'Producer'	=> 'R and OS php pdf writer, http://www.ros.co.nz'));

	$pdf -> ezSetCmMargins(2,1.5,1,1);


	$pdf->selectFont("$_PDFDIR/fonts/Helvetica.afm");

	$all = $pdf->openObject();
	$pdf->saveState();
	$pdf->setStrokeColor(0,0,0,1);

	$pdf->ezSetDy(0);
	$pdf->ez['leftMargin']=10;

	$pdf->line(10,40,578,40);
	$pdf->line(10,812,578,812);

	$pdf->addText(10,822,18,$title);
	$pdf->addText(36,34,6, 'Enigma UE beta 1.0 - ' . $context['forum_name']);
	$pdf->addText(250,34,6,'<c:alink:http://www.lunabyte.com>Powered by Enigma UE</c:alink>');
	$pdf->addText(450,34,6,"Generated: " . date(" F jS, Y, H:i", time()+5*60*60));

	$pdf->restoreState();
	$pdf->closeObject();
	$pdf->addObject($all,'all');

	// Title, Author, New Cat image on first page only...
	$pdf->addText(100, 795, 14, $subject);
	$pdf->addText(100, 785, 8, 'By Aquilo');
	$pdf->line(100,780,250,780);
	$pdf->ezSetDy(10);
	$pdf->ezImage('http://localhost/smf_forum/Themes/EnigmaUE/images/News/' . $row['CatImage'], 0, 75, NULL, 'left', 0);


	$pdf->ezSetDy(45);
	$pdf->ez['leftMargin'] = 15;
	$pdf->ez['rightMargin'] = 15;
	$pdf->ezText("\n\n\n$body", 10, array('justification' => 'full'));

	$pdf->ezSetDy(0,'makeSpace');

	$pdf->ezStream();

	obExit(false);

function cleancodetages ($text)
{
	censorText($text);
	$text = strip_tags($text, '<br>');

	// highlight_php_code
	$codefrom = array(
		'~\[table\]~i',
		'~\[tr\]~i',
		'~\[td\]~i',
		'~\[\/td\]~i',
		'~\[\/tr\]~i',
		'~\[\/table\]~i',

		'~\[code\]~i',
		'~\[\/code\]~i',

		'~\[center\]~i',
		'~\[\/center\]~i',

		'~\[color=([Maroon|Limegreen|Lime|Navy|Teal|Brown|Beige|Blue|Purple|Orange|Green|Pink|Yellow|Red|Black])\]~i',
		'~\[color=(#[\da-fA-F]{3}|#[\da-fA-F]{6}|[\w]{1,12})\]~i',
		'~\[\/color\]~i',



		'~\[quote\]~i',
		'~\[\/quote\]~i',

		'~\[hr\]~i',
		'~\[b\]~i',
		'~\[\/b\]~i',
		'~\[u\]~i',
		'~\[\/u\]~i',
		'~\[i\]~i',
		'~\[\/i\]~i',
		'~\[br\]~i',
		'~\[list\]~i',
		'~\[\/list\]~i',
		'~\[li\]~i',
		'~\[\/li\]~i',
		'~<\/?p[^>]*>~i',
		'~<br[ ]?[\/]?>~i',
		"~\t~",
		'~\[s\]~i', '~\[\/s\]~i',
		'~\[sub\]~i', '~\[\/sub\]~i',

		'/\[url=(.+)\](.+)\s*\[\/url\]/',
		'/\[email=(.*)\](.*)\[\/email\]/',
		'/\[url\](.+)\[\/url\]/',
		'/\[email\](.*)\[\/email\]/',

		'~\[me=((?:&quot;)?)(.{1,80}?)\\1\](.+?)\[/me\](?:<br />)?~i',
	);

/*
		"\n<i>Start Code</i><hr>\n</c:code>",
		"\n<hr>\n<i>End Code</i>\n",

, '<strike>', '</strike>', '<c:strike>', '</c:strike>'
*/

	$codeto = array(
		"\n",
		'',
		'',
		'   ',
		"\n",
		"\n", //Table closing tag

		"Code:\n<c:code>", "</c:code>\n\n",

		'<c:center>',
		'</c:center>',

		'<c:color:' . text2hex('$1') . '>',
		'<c:color:$1>',
		'</c:color>',



		'<i>"',
		'"</i>',

		'<hr>',
		'<b>', '</b>',
		'<u>','</u>',
		'<i>','</i>',
		"\n\n",
		"\n", "\n",
		"    <b>" . chr(149) . "</b> ", "\n",
		"\n\n",
		"\n",
		'        ',
		'<c:strike>', '</c:strike>',
		'<c:sub>',
		'</c:sub>',

		'<c:alink:$1>$2</c:alink>',
		'<c:alink:mailto:$1>$2</c:alink>',
		'<c:alink:$1>$1</c:alink>',
		'<c:alink:mailto:$1>$1</c:alink>',

		'<c:color:255:0:0><c:uline>* $2 $3</c:uline></c:color>',
	); // chr(149) == "•" a bullet!!! >:)

	$text = preg_replace($codefrom, $codeto, $text);

	parsesmileys ($text);

	// <img src="http://localhost/smf_forum/Smileys/Mean Green/lame.gif" alt="Lame user" border="0" />
	// will get back to, those spaces are driving me crazy...
	// "<c:imgsrc:" . str_replace(" ", "_", "$1") . "></c:imgsrc>"
	$text = preg_replace(
		'~<img src="(.+)" alt="(.+)" border="0" />~i',
		'<i>emotion($2)</i>', $text);

	$text = decodeHTML($text);

	return $text;
}

function text2hex($color)
{
	// this is one big headache, I guess pdf documents only allow 16 or 255 colors and this...
	$colorarray = array(
		'maroon'	=> '128:0:0',
		'limegreen'	=> '0:102:0',
		'lime'		=> '0:102:0',
		'navy'		=> '0:0:128',
		'teal'		=> '0:128:128',
		'brown'		=> '102:51:0',
		'beige'		=> '186:186:167',
		'blue'		=> '0:0:255',
		'purple'	=> '153:0:204',
		'orange'	=> '255:153:0',
		'green'		=> '0:102:0',
		'pink'		=> '255:0:255',
		'yellow'	=> '255:255:0',
		'red'		=> '128:0:0',
		'black'		=> '0:0:0',
		'grey'		=> '128:128:128',

	);

	$color = strtolower($color);
	return !empty($colorarray[$color]) ? $colorarray[$color] : $color;
}

function decodeHTML($string) {
	$string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
	$string = preg_replace("/&#([0-9]+);/me", "chr('\\1')", $string);
	return $string;
}

function get_php_setting($val) {
	$r =  (ini_get($val) == '1' ? 1 : 0);
	return $r ? 'ON' : 'OFF';
}
?>
Return current item: Enigma