<?php
/*
Plugin Name: Post to PDF
Plugin URI: http://sentidoweb.com
Description: Exports post to PDF file (co-author <a href="http://www.buayacorp.com/">Alexander Concha</a>)
Author: Luis Sacristán
Version: 1.4
Author URI: http://displaynone.blogspot.com
*/

/*  Copyright 2006  LUIS SACRISTÁN PASCUAL  (email : displaynone@gmail.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 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 * Modificaciones: 
 *   1.1 6/11/2006 
 *   Corregido error en la selección de tabla
 *   Eliminado acceso incorrecto
 *   
 */   

require_once('sw_config.php');

define("__SW_PDF_TITLE_DEFAULT__", "Post2PDF Wordpress plugin");


$__SW_PDF_LANG__ = isset($_POST["sw_pdf_lang"])? $_POST["sw_pdf_lang"] : (get_option('sw_pdf_lang') ? get_option('sw_pdf_lang') : "en");

include_once (dirname(__FILE__) . '/lang/' . $__SW_PDF_LANG__ .'.php');

function Post2PDF() {
	global $wp_rewrite;
	// Icono obtenido de http://www.famfamfam.com/lab/icons/silk/preview.php
	if ( $wp_rewrite->using_permalinks() ) {
		echo '<a href="'.rtrim(get_permalink(), '/').'.pdf" target="_blank">';
	}
	else {
		echo '<a href="'.add_query_arg('pdf_version', '1', get_permalink()).'" target="_blank">';
	}	
	echo '<img src="'.get_option('sw_pdf_icon').'" alt="Post2PDF">';
	echo '</a>';
}

function sw_print_fonts($fuentes, $option) {	
	echo '<select name="'. $option . '" id="' . $option . '">' . "\n";	
	foreach($fuentes as $_fuente) {
		echo '<option value="' . $_fuente . '"' . 
			(get_option($option) == $_fuente? ' selected="selected">':'>') .
			$_fuente . '</option>' . "\n";
	}
	echo '</select>' . "\n";

}
function sw_write_plugin_submenu() {
	global $translate;
	global $langNames;
	global $__SW_PDF_LANG__;
	
	if (isset($_POST["sw_form"])) {
		check_admin_referer('sw-pdf-plugin');
		sw_setup_pdf_plugin();
	}
	function phpFiles($val) {
		return preg_match("/\.php$/", $val);
	}
	// Leo los ficheros php del directorio fonts para saber que fuentes existen
	$fuentes = array();
	if ($handle = opendir(dirname(__FILE__) . "/fonts")) {
		while (false !== ($file = readdir($handle))) {
		   	if ( preg_match("/(.*)\.php$/", $file, $match) ) {
		   		$fuentes[] = $match[1];
		   	}
	   	}
	   	closedir($handle);
	}	
	
	// Leo los ficheros php del directorio lang para saber que traducciones existen
	$langs = array();
	if ($handle = opendir(dirname(__FILE__) . '/lang')) {
	   while (false !== ($file = readdir($handle))) {
		   if ( preg_match("/(.*)\.php$/", $file, $match) ) {
		   		$langs[] = $match[1];
		   	}
	   }
	   closedir($handle);
	}	
	?>
	<div class="wrap">
		<h2><?php echo $translate['post2pdf_options']; ?></h2>
		<fieldset class="options">
    		<div style="float: right">
			<form name="form" method="post">
    			<select name="sw_pdf_lang" id="sw_pdf_lang">
    			<?php
    				foreach($langs as $_lang) :
    			?>
    					<option value="<?php echo $_lang; ?>" 
    						<?php echo $__SW_PDF_LANG__ == $_lang? " selected ":""; ?>
    					><?php echo $langNames[$_lang]; ?></option>
    			<?php
    				endforeach;
    			?>
    			</select>
    		</div>
			<input type="hidden" name="sw_form" value="1" />
			<h3><?php echo $translate['pdf_header']; ?></h3>
			<table class="optiontable">
				<tr valign="top">
					<th scope="row"><?php echo $translate['title']; ?>:</th>
					<td><input name="sw_pdf_title" type="text" id="sw_pdf_title" value="<?php echo (get_option("sw_pdf_title") != "")? get_option("sw_pdf_title"): get_option('blogname'); ?>" size="40" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['subtitle']; ?>:</th>
					<td><input name="sw_pdf_subtitle" type="text" id="sw_pdf_subtitle" value="<?php echo (get_option("sw_pdf_subtitle") != "")? get_option("sw_pdf_subtitle"): "[#the_title()#]"; ?>" size="40" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font_size']; ?>:</th>
					<td><input name="sw_pdf_header_title_font_size" type="text" id="sw_pdf_header_title_font_size" value="<?php echo (get_option("sw_pdf_header_title_font_size") != "")? get_option("sw_pdf_header_title_font_size"): "12"; ?>" size="3" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font']; ?>:</th>
					<td>
						<?php sw_print_fonts($fuentes, 'sw_pdf_header_title_font'); ?>
					</td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font_color']; ?>:</th>
					<td>
						<input name="sw_pdf_header_title_font_color_R" type="text" id="sw_pdf_header_title_font_color_R" value="<?php echo (get_option("sw_pdf_header_title_font_color_R") != "")? get_option("sw_pdf_header_title_font_color_R"): "0"; ?>" size="3" />
						&nbsp;
						<input name="sw_pdf_header_title_font_color_G" type="text" id="sw_pdf_header_title_font_color_G" value="<?php echo (get_option("sw_pdf_header_title_font_color_G") != "")? get_option("sw_pdf_header_title_font_color_G"): "0"; ?>" size="3" />
						&nbsp;
						<input name="sw_pdf_header_title_font_color_B" type="text" id="sw_pdf_header_title_font_color_B" value="<?php echo (get_option("sw_pdf_header_title_font_color_B") != "")? get_option("sw_pdf_header_title_font_color_B"): "0"; ?>" size="3" />
					</td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['image']; ?>:</th>
					<td><input name="sw_pdf_header_image" type="text" id="sw_pdf_header_image" value="<?php echo (get_option("sw_pdf_header_image") != "")? get_option("sw_pdf_header_image"): ""; ?>" size="40" onchange="document.getElementById('sw_head_image').src=this.value" />
					<br /><strong><?php echo $translate['only_png_and_jpeg']; ?></strong>
					<br /><em><?php echo $translate['url_example']; ?></em>
					</td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['image_width']; ?>:</th>
					<td><input name="sw_pdf_header_image_width" type="text" id="sw_pdf_header_image_width" value="<?php echo (get_option("sw_pdf_header_image_width") != "")? get_option("sw_pdf_header_image_width"): ""; ?>" size="3" />
					</td> 
				</tr> 				
				<tr valign="top">
					<td colspan="2" align="center"><img id="sw_head_image" src="<?php echo (get_option("sw_pdf_header_image") != "")? get_option("sw_pdf_header_image"): get_option('siteurl')."/wp-content/plugins/Post2PDF/no-image.png"; ?>" alt="No image available"/></td> 
				</tr> 				
			</table>
			<h3><?php echo $translate['pdf_body']; ?></h3>
			<table class="optiontable">
				<tr valign="top">
					<th scope="row"><?php echo $translate['font']; ?>:</th>
					<td>
						<?php sw_print_fonts($fuentes, 'sw_pdf_body_font'); ?>
					</td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font_size']; ?>:</th>
					<td><input name="sw_pdf_body_font_size" type="text" id="sw_pdf_body_font_size" value="<?php echo (get_option("sw_pdf_body_font_size") != "")? get_option("sw_pdf_body_font_size"): "12"; ?>" size="3" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font_color']; ?>:</th>
					<td>
						<input name="sw_pdf_body_title_font_color_R" type="text" id="sw_pdf_body_title_font_color_R" value="<?php echo (get_option("sw_pdf_body_title_font_color_R") != "")? get_option("sw_pdf_body_title_font_color_R"): "0"; ?>" size="3" />
						&nbsp;
						<input name="sw_pdf_body_title_font_color_G" type="text" id="sw_pdf_body_title_font_color_G" value="<?php echo (get_option("sw_pdf_body_title_font_color_G") != "")? get_option("sw_pdf_body_title_font_color_G"): "0"; ?>" size="3" />
						&nbsp;
						<input name="sw_pdf_body_title_font_color_B" type="text" id="sw_pdf_body_title_font_color_B" value="<?php echo (get_option("sw_pdf_body_title_font_color_B") != "")? get_option("sw_pdf_body_title_font_color_B"): "0"; ?>" size="3" />
					</td> 
				</tr> 				
			</table>
			<h3><?php echo $translate['pdf_footer']; ?></h3>
			<table class="optiontable">
				<tr valign="top">
					<th scope="row"><?php echo $translate['footer']; ?>:</th>
					<td><input name="sw_pdf_footer" type="text" id="sw_pdf_footer" value="<?php echo (get_option("sw_pdf_footer") != "")? get_option("sw_pdf_footer"): "Page {nb}"; ?>" size="40" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font']; ?>:</th>
					<td>
						<?php sw_print_fonts($fuentes, 'sw_pdf_footer_font'); ?>
					</td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font_size']; ?>:</th>
					<td><input name="sw_pdf_footer_font_size" type="text" id="sw_pdf_footer_font_size" value="<?php echo (get_option("sw_pdf_footer_font_size") != "")? get_option("sw_pdf_footer_font_size"): "8"; ?>" size="3" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['font_color']; ?>:</th>
					<td>
						<input name="sw_pdf_footer_title_font_color_R" type="text" id="sw_pdf_footer_title_font_color_R" value="<?php echo (get_option("sw_pdf_footer_title_font_color_R") != "")? get_option("sw_pdf_footer_title_font_color_R"): "0"; ?>" size="3" />
						&nbsp;
						<input name="sw_pdf_footer_title_font_color_G" type="text" id="sw_pdf_footer_title_font_color_G" value="<?php echo (get_option("sw_pdf_footer_title_font_color_G") != "")? get_option("sw_pdf_footer_title_font_color_G"): "0"; ?>" size="3" />
						&nbsp;
						<input name="sw_pdf_footer_title_font_color_B" type="text" id="sw_pdf_footer_title_font_color_B" value="<?php echo (get_option("sw_pdf_footer_title_font_color_B") != "")? get_option("sw_pdf_footer_title_font_color_B"): "0"; ?>" size="3" />
					</td> 
				</tr> 				
			</table>
			<h3><?php echo $translate['miscellaneous']; ?></h3>
			<table class="optiontable">
				<tr valign="top">
					<th scope="row"><?php echo $translate['author_pdf_file']; ?>:</th>
					<td><input name="sw_pdf_author" type="text" id="sw_pdf_author" value="<?php echo (get_option("sw_pdf_author") != "")? get_option("sw_pdf_author"): __SW_PDF_TITLE_DEFAULT__; ?>" size="40" /></td> 
				</tr> 				
				<tr valign="top">
					<th scope="row"><?php echo $translate['pdf_icon']; ?>:</th>
					<td><input name="sw_pdf_icon" type="text" id="sw_pdf_icon" size="40" onchange="document.getElementById('sw_pdf_icon_image').src=this.value" value="<?php echo (get_option("sw_pdf_icon") != "")? get_option("sw_pdf_icon"): get_option('siteurl') ."/wp-content/plugins/Post2PDF/pdf.gif"; ?>"/>
					<br /><em>[http://domain.com/images/pdf.gif]</em>
					</td> 
				</tr> 				
				<tr valign="top">
					<td colspan="2" align="center"><img id="sw_pdf_icon_image" src="<?php echo (get_option("sw_pdf_icon") != "")? get_option("sw_pdf_icon"): get_option('siteurl') ."/wp-content/plugins/Post2PDF/pdf.gif"; ?>" alt="No image available"/></td> 
				</tr> 				
			</table>
			<p class="submit"><input type="submit" name="Update" value="<?php echo $translate['update_settings']; ?> &raquo;" />
			<?php wp_nonce_field('sw-pdf-plugin'); ?>
			</p>
		</fieldset>
	</div>
	<?php
}

function sw_add_plugin_submenu() {
	if (function_exists('add_submenu_page')) {
		add_submenu_page('plugins.php', 'Post2PDF', 'Post2PDF', 1, __FILE__, 'sw_write_plugin_submenu');
	}
}	
function sw_setup_pdf_plugin() {	
	update_option("sw_pdf_title", stripslashes(isset($_POST["sw_pdf_title"])? $_POST["sw_pdf_title"]: get_option('blogname')));
	update_option("sw_pdf_subtitle", stripslashes(isset($_POST["sw_pdf_subtitle"])? $_POST["sw_pdf_subtitle"]: "__TITLE__"));
	update_option("sw_pdf_author", stripslashes(isset($_POST["sw_pdf_author"])? $_POST["sw_pdf_author"]: __SW_PDF_TITLE_DEFAULT__));
	update_option("sw_pdf_header_image", stripslashes(isset($_POST["sw_pdf_header_image"])? $_POST["sw_pdf_header_image"]: ""));
	update_option("sw_pdf_header_image_width", stripslashes(isset($_POST["sw_pdf_header_image_width"])? $_POST["sw_pdf_header_image_width"]: ""));
	update_option("sw_pdf_header_title_font", stripslashes(isset($_POST["sw_pdf_header_title_font"])? $_POST["sw_pdf_header_title_font"]: "freesans"));
	update_option("sw_pdf_header_title_font_size", stripslashes(isset($_POST["sw_pdf_header_title_font_size"])? $_POST["sw_pdf_header_title_font_size"]: "12"));
	update_option("sw_pdf_header_title_font_color_R", stripslashes(isset($_POST["sw_pdf_header_title_font_color_R"])? $_POST["sw_pdf_header_title_font_color_R"]: "0"));
	update_option("sw_pdf_header_title_font_color_G", stripslashes(isset($_POST["sw_pdf_header_title_font_color_G"])? $_POST["sw_pdf_header_title_font_color_G"]: "0"));
	update_option("sw_pdf_header_title_font_color_B", stripslashes(isset($_POST["sw_pdf_header_title_font_color_B"])? $_POST["sw_pdf_header_title_font_color_B"]: "0"));
	update_option("sw_pdf_body_font_size", stripslashes(isset($_POST["sw_pdf_body_font_size"])? $_POST["sw_pdf_body_font_size"]: "12"));
	update_option("sw_pdf_body_font", stripslashes(isset($_POST["sw_pdf_body_font"])? $_POST["sw_pdf_body_font"]: "freesans"));
	update_option("sw_pdf_body_title_font_color_R", stripslashes(isset($_POST["sw_pdf_body_title_font_color_R"])? $_POST["sw_pdf_body_title_font_color_R"]: "0"));
	update_option("sw_pdf_body_title_font_color_G", stripslashes(isset($_POST["sw_pdf_body_title_font_color_G"])? $_POST["sw_pdf_body_title_font_color_G"]: "0"));
	update_option("sw_pdf_body_title_font_color_B", stripslashes(isset($_POST["sw_pdf_body_title_font_color_B"])? $_POST["sw_pdf_body_title_font_color_B"]: "0"));
	update_option("sw_pdf_footer", stripslashes(isset($_POST["sw_pdf_footer"])? $_POST["sw_pdf_footer"]: "Page {nb}"));
	update_option("sw_pdf_footer_font", stripslashes(isset($_POST["sw_pdf_footer_font"])? $_POST["sw_pdf_footer_font"]: "freesans"));
	update_option("sw_pdf_footer_font_size", stripslashes(isset($_POST["sw_pdf_footer_font_size"])? $_POST["sw_pdf_footer_font_size"]: "8"));
	update_option("sw_pdf_footer_title_font_color_R", stripslashes(isset($_POST["sw_pdf_footer_title_font_color_R"])? $_POST["sw_pdf_footer_title_font_color_R"]: "0"));
	update_option("sw_pdf_footer_title_font_color_G", stripslashes(isset($_POST["sw_pdf_footer_title_font_color_G"])? $_POST["sw_pdf_footer_title_font_color_G"]: "0"));
	update_option("sw_pdf_footer_title_font_color_B", stripslashes(isset($_POST["sw_pdf_footer_title_font_color_B"])? $_POST["sw_pdf_footer_title_font_color_B"]: "0"));
	update_option("sw_pdf_icon", stripslashes(isset($_POST["sw_pdf_icon"])? $_POST["sw_pdf_icon"]: get_option('siteurl')."/wp-content/plugins/Post2PDF/pdf.gif"));
	update_option("sw_pdf_lang", stripslashes(isset($_POST["sw_pdf_lang"])? $_POST["sw_pdf_lang"]: "en"));
}
function fix_pre($the_content) {
	return  preg_replace_callback('|<pre>(.*?)</pre>|si', create_function('$m', 'return nl2br($m[1]);'), $the_content);
}

function bc_handle_pdf() {	
	global $wp_rewrite;
	
	$req = $_SERVER['REQUEST_URI'];
	if ( $wp_rewrite->using_permalinks() ) {
		if ( $pdf_version = preg_match('/(.*)\.pdf$/i', rtrim($req, '/'), $match) ) {
			$req = $match[1];
		}
	}
	else {
		$pdf_version = isset($_GET['pdf_version']);
	}
	
	if ( $pdf_version && ($id = url_to_postid($req)) != 0 ) {
		global $langNames;
		
		add_filter('the_content', 'fix_pre');
		
		require_once (dirname(__FILE__) . '/SW_Post2PDF.php');
		$pdf = new SW_Post2PDF();
		$post = get_post($id, 'ARRAY_A');
		
		//var_dump(wpautop($post["post_content"]));
		$texto = '<h1>'.wp_specialchars($post["post_title"]).'</h1><sup>'.
			$post["post_date"].'</sup><br /><br />'.
			apply_filters('the_content', $post["post_content"]);
		$pdf->setHeader();
		$pdf->AddPage();
		$pdf->SetFont(get_option("sw_pdf_body_font"), '', get_option("sw_pdf_body_font_size"));
		$pdf->SetTextColor(get_option("sw_pdf_body_title_font_color_R"),
					       get_option("sw_pdf_body_title_font_color_G"),
						   get_option("sw_pdf_body_title_font_color_B"));
		
		if (function_exists('mb_convert_encoding')) {	
		    $texto = mb_convert_encoding($texto, "UTF-8", get_option("blog_charset"));
		}			   
		$pdf->writeHTML($texto, true, 0);
		$pdf->Output();
		die();
	}
}

add_action('admin_menu', 'sw_add_plugin_submenu');
add_action('init', 'bc_handle_pdf');
# Shame on me!, this is hardcoded
add_action('activate_Post2PDF/Post2PDF.php', 'sw_setup_pdf_plugin');
?>
