configuration page. Bug reports and feature requests welcome!
License: GPL
*/
// Include PDF/PDI functions (have a look at http://www.fpdf.org - nice library and freeware!)
define( 'FPDF_FONTPATH', dirname(__FILE__) . "/contributed/pdffonts/" );
define( 'FPDF_TEMPLATEPATH', dirname(__FILE__) . "/contributed/pdftemplates/" );
define( 'FPDF_INCLUDEPATH', dirname(__FILE__) . "/contributed/fpdf/" );
define( 'FPDI_INCLUDEPATH', dirname(__FILE__) . "/contributed/fpdi/" );
define( 'A2P_CACHEPATH', dirname(__FILE__) . "/../../uploads/" );
require_once( FPDF_INCLUDEPATH . 'fpdf.php' );
require_once( FPDI_INCLUDEPATH . 'fpdi.php' );
// The main plugin class
class article2pdf
{
var $a2p_AdminOptionsName;
var $s2p_AdminOptions;
// Construct
function __construct()
{
$this -> a2p_AdminOptionsName = 'a2pPlugin_AdminOptions';
$this -> a2p_AdminOptions = $this -> a2p_GetAdminOptions();
}
// PHP4 compatibe construct (please update to PHP 5 soon! ;) )
function article2pdf() { $this -> __construct(); }
// Get options for this plugin
function a2p_GetAdminOptions()
{
// Set default options
$a2pOptions = array( 'PDFTemplateFile' => '',
'PDFTemplateMarginLeft' => 25,
'PDFTemplateMarginRight' => 25,
'PDFTemplateMarginTop' => 25,
'PDFTemplateMarginBottom' => 25,
'PDFOptionIncludePics' => 'true',
'PDFOptionIncludeDate' => 'true',
'PDFOptionDateformat' => '%x -',
'PDFOptionDateLocale' => 'en_EN',
'PDFOptionFont' => 'Arial',
'PDFOptionFontSize' => 12,
'PDFOptionLineHeight' => 6,
'PDFOptionCacheTime' => 3600
);
// Load existing options
$_a2pOptions = get_option( $this -> a2p_AdminOptionsName );
// Overwrite defaults
if( count( $_a2pOptions ) )
{
foreach( $_a2pOptions AS $oKey => $oVal )
{
$a2pOptions[ $oKey ] = $oVal;
}
}
// Set default options to wp db if no existing options or new options are found
if( !count( $_a2pOptions ) || count( $_a2pOptions ) != count( $a2pOptions ) )
{
update_option( $this -> a2p_AdminOptionsName, $a2pOptions );
}
// Return options
return $a2pOptions;
}
// Create pdf
function a2p_CreatePdf()
{
// Get post
global $post;
// Set base url
$base_url = ($_SERVER[ "HTTPS" ] ? "https" : "http") . "://" . $_SERVER[ "HTTP_HOST" ] . "/";
if( !empty( $post -> ID ) )
{
// Get File from cache?
$cachefile = $this -> _cache_get_filename( $post );
if( $this -> _cache_recreate( $cachefile ) )
{
$pdf =& new FPDI();
$pdf -> SetAuthor( "WordPress article2pdf plugin" );
$pdf -> SetCreator( "WordPress article2pdf plugin" );
$pdf -> SetTitle( utf8_decode( strip_tags( $post -> post_title ) ) );
if( !empty( $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) )
{
$pdf -> setSourceFile( FPDF_TEMPLATEPATH . $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] );
$pdf -> tplIdx = $pdf -> importPage( 1 );
}
$pdf -> SetLineWidth( 0.2 );
$pdf -> AddPage();
if( !empty( $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) )
$pdf -> useTemplate( $pdf -> tplIdx );
$pdf -> SetLeftMargin( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] );
$pdf -> SetRightMargin( $this -> a2p_AdminOptions[ 'PDFTemplateMarginRight' ] );
$pdf -> SetTopMargin( $this -> a2p_AdminOptions[ 'PDFTemplateMarginTop' ] );
$pdf -> SetAutoPageBreak( true, $this -> a2p_AdminOptions[ 'PDFTemplateMarginBottom' ] );
$pdf -> SetXY( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $this -> a2p_AdminOptions[ 'PDFTemplateMarginRight' ] );
// Write title
if( $this -> a2p_AdminOptions[ 'PDFOptionIncludeDate' ] == 'true' )
{
$date_timestamp = strtotime( $post -> post_date );
setlocale( LC_TIME, $this -> a2p_AdminOptions[ 'PDFOptionDateLocale' ] . '.UTF8' );
$date_str = strftime( $this -> a2p_AdminOptions[ 'PDFOptionDateformat' ], $date_timestamp );
$post -> post_title = $date_str . ' ' . $post -> post_title;
}
$this -> _font_bold( $pdf );
$pdf -> Cell( 0, $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ] * ceil( $pdf -> GetStringWidth( html_entity_decode( utf8_decode( strip_tags( $post -> post_title ) ) ) ) / ($pdf -> w - $pdf -> lMargin - $pdf -> rMargin) ), html_entity_decode( utf8_decode( strip_tags( $post -> post_title ) ) ), 1, 1, 'C' );
$this -> _font_normal( $pdf );
$pdf -> Ln();
// Write body
$pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], '', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] );
// Prepare tables to be in a single paragraph (easier to parse)
preg_match_all( '#.*(
.*).*#siU', $post -> post_content, $tables );
if( count( $tables[ 1 ] ) )
{
foreach( $tables[ 1 ] AS $tkey => $tbl )
{
$tbl = str_replace( array( "\n", "\r" ), '', $tbl );
$post -> post_content = str_replace( $tables[ 1 ][ $tkey ], $tbl, $post -> post_content );
}
}
// Explore by paragraph
$paragraphs_array = explode( "\n", $post -> post_content );
$isFirstParagraph = true;
$link_href = '';
foreach( $paragraphs_array AS $pkey => $p )
{
$page_before = $pdf -> PageNo();
$p = trim( $p );
if( !empty( $p ) )
{
// Convert tables
$p = $this -> _convert_tables( $p );
// Convert links
$p = $this -> _convert_links( $p );
// Convert bold
$p = $this -> _convert_bold( $p );
// Convert underline
$p = $this -> _convert_underline( $p );
// Convert italic
$p = $this -> _convert_italic( $p );
// Convert blockquote
$p = $this -> _convert_blockquote( $p );
// Convert lists and items
$p = $this -> _convert_listitem( $p );
// Convert headings
$p = $this -> _convert_heading( $p );
// Convert horizontal lines
$p = $this -> _convert_lines( $p );
// Write text
$breaks_after = 2;
$parts = explode( '|-====-|', $p );
foreach( $parts AS $pkey => $part )
{
// Parse style of part
// ...beginnings
if( substr( $part, 0, 5 ) == 'LINK|' )
{
$this -> _font_underline( $pdf );
$part = substr( $part, 5 );
$part_arr = explode( '|', $part );
$link_href = $part_arr[ 0 ];
unset( $part_arr[ 0 ] );
$part = implode( $part_arr );
unset( $part_arr );
}
if( substr( $part, 0, 2 ) == 'U|' )
{
$this -> _font_underline( $pdf );
$part = substr( $part, 2 );
}
if( substr( $part, 0, 2 ) == 'B|' )
{
$this -> _font_bold( $pdf );
$part = substr( $part, 2 );
}
if( substr( $part, 0, 2 ) == 'I|' )
{
$this -> _font_italic( $pdf );
$part = substr( $part, 2 );
}
if( substr( $part, 0, 1 ) == 'H' && substr( $part, 2, 1 ) == '|' )
{
$hlevel = substr( $part, 1, 1 );
$this -> _font_heading( $pdf, $hlevel );
$part = substr( $part, 3 );
}
if( substr( $part, 0, 2 ) == 'Q|' )
{
$this -> _margin_quote( $pdf );
$part = substr( $part, 2 );
}
if( substr( $part, 0, 3 ) == 'UL|' || substr( $part, 0, 3 ) == 'OL|' )
{
$this -> _margin_quote( $pdf );
$part = substr( $part, 3 );
$breaks_after = 1;
}
if( substr( $part, 0, 3 ) == 'LI|' )
{
$part = substr( $part, 3 );
$part = '• ' . $part;
}
if( substr( $part, 0, 3 ) == 'TR|' )
{
if( !isset( $trheight ) )
{
$trheight = $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ];
$pdf -> SetX( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] );
}
else
{
$pdf -> SetXY( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $y + $trheight );
$trheight = $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ];
}
$ccount = intval( substr( $part, 3 ) );
$cwidth = floor( ($pdf -> w - $pdf -> lMargin - $pdf -> rMargin) / $ccount );
$part = '';
$tdcounter = 1;
}
if( substr( $part, 0, 3 ) == 'TH|' )
{
$this -> _font_bold( $pdf );
$cpart = html_entity_decode( utf8_decode( strip_tags( trim( substr( $part, 3 ) ) ) ) );
$pdf -> SetFillColor( 0xCC, 0xCC, 0xCC );
$y = $pdf -> GetY();
$x = $pdf -> GetX();
$pdf -> MultiCell( $cwidth, $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ], $cpart, 'LTR', 'C', true );
if( $pdf -> GetY() < $y )
{
$pdf -> useTemplate( $pdf -> tplIdx );
$trheight = $pdf -> GetY() - $this -> a2p_AdminOptions[ 'PDFTemplateMarginTop' ];
$pdf -> SetXY( $x + $cwidth, $this -> a2p_AdminOptions[ 'PDFTemplateMarginTop' ] );
}
else
{
$trheight = (($pdf -> GetY() - $y) > $trheight ? $pdf -> GetY() - $y : $trheight);
if( $tdcounter == $ccount )
{
for( $lc = 0; $lc <= $ccount; $lc++ )
$pdf -> Line( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] + ($cwidth * $lc), $y, $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] + ($cwidth * $lc), $y + $trheight );
}
$pdf -> SetXY( $x + $cwidth, $y );
}
$part = '';
$tdcounter++;
}
if( substr( $part, 0, 3 ) == 'TD|' )
{
$pdf -> SetFontSize( floor( $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] * 0.80 ) );
$cpart = html_entity_decode( utf8_decode( strip_tags( trim( substr( $part, 3 ) ) ) ) );
$y = $pdf -> GetY();
$x = $pdf -> GetX();
$pdf -> MultiCell( $cwidth, $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ], $cpart, 'LTR', 'L', false );
if( $pdf -> GetY() < $y )
{
$pdf -> useTemplate( $pdf -> tplIdx );
$trheight = $pdf -> GetY() - $this -> a2p_AdminOptions[ 'PDFTemplateMarginTop' ];
$pdf -> SetXY( $x + $cwidth, $this -> a2p_AdminOptions[ 'PDFTemplateMarginTop' ] );
$y = $this -> a2p_AdminOptions[ 'PDFTemplateMarginTop' ];
}
else
{
$trheight = (($pdf -> GetY() - $y) > $trheight ? $pdf -> GetY() - $y : $trheight);
if( $tdcounter == $ccount )
{
for( $lc = 0; $lc <= $ccount; $lc++ )
$pdf -> Line( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] + ($cwidth * $lc), $y, $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] + ($cwidth * $lc), $y + $trheight );
}
$pdf -> SetXY( $x + $cwidth, $y );
}
$part = '';
$tdcounter++;
$pdf -> SetFontSize( $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] );
}
if( substr( $part, 0, 3 ) == '__|' )
{
$part = '';
$y = $pdf -> GetY();
$pdf -> SetLineWidth( 0.4 );
$pdf -> Line( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $y, $pdf -> w - $this -> a2p_AdminOptions[ 'PDFTemplateMarginRight' ], $y );
$pdf -> SetLineWidth( 0.2 );
$breaks_after = 1;
}
// Get Images from text
if( $this -> a2p_AdminOptions[ 'PDFOptionIncludePics' ] == 'true' )
{
if( preg_match_all( '##iU', $part, $matches ) )
{
foreach( $matches[ 1 ] AS $ikey => $img )
{
// Convert to local img path, if current base url found in img src
if( strpos( $img, $base_url ) === 0 )
$img = $_SERVER[ 'DOCUMENT_ROOT' ] . (substr( $_SERVER[ 'DOCUMENT_ROOT' ], -1 ) != '/' ? '/' : '') . str_replace( $base_url, '', $img );
// Get dimensions
$imgsize = GetImageSize( $img );
// Set size to points at 72 dpi
$imgsize[ 0 ] = $imgsize[ 0 ] / $pdf -> k;
$imgsize[ 1 ] = $imgsize[ 1 ] / $pdf -> k;
// Begin new page if image has too much height
if( $isFirstParagraph == false )
{
if( ($pdf -> GetY() + $imgsize[ 1 ]) > ($pdf -> h - $pdf -> bMargin) )
{
$pdf -> AddPage();
$pdf -> SetXY( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $this -> a2p_AdminOptions[ 'PDFTemplateMarginRight' ] );
}
}
// Centered image
$pdf -> Image( $img, ($pdf -> w / 2) - ($imgsize[ 0 ] / 2), $pdf -> GetY(), 0, 0, '', $link_href );
// Set next X,Y position of following text
$pdf -> SetXY( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $pdf -> GetY() + $imgsize[ 1 ] + $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ] );
// Remove img from paragraph
$part = str_replace( $matches[ 0 ][ $ikey ], '', $part );
}
$breaks_after = 0;
}
}
// ...endings
if( substr( $part, 0, 3 ) == '/U|' || substr( $part, 0, 3 ) == '/B|' || substr( $part, 0, 3 ) == '/I|' )
{
$this -> _font_normal( $pdf );
$part = substr( $part, 3 );
}
if( substr( $part, 0, 6 ) == '/LINK|' )
{
$this -> _font_normal( $pdf );
$part = substr( $part, 6 );
$link_href = '';
}
if( substr( $part, 0, 4 ) == '/UL|' || substr( $part, 0, 4 ) == '/OL|' )
{
$this -> _margin_normal( $pdf );
$part = substr( $part, 4 );
$breaks_after = 2;
}
if( substr( $part, 0, 4 ) == '/LI|' )
{
$part = substr( $part, 4 );
$breaks_after = 1;
}
if( substr( $part, 0, 2 ) == '/H' )
{
$this -> _font_normal( $pdf );
$part = substr( $part, 4 );
$breaks_after = 2;
}
if( substr( $part, 0, 4 ) == '/TD|' || substr( $part, 0, 4 ) == '/TH|' || substr( $part, 0, 4 ) == '/TR|' )
{
if( substr( $part, 0, 4 ) == '/TR|' )
$pdf -> Line( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $y + $trheight, $pdf -> w - $this -> a2p_AdminOptions[ 'PDFTemplateMarginRight' ], $y + $trheight );
$this -> _font_normal( $pdf );
$part = substr( $part, 4 );
}
if( substr( $part, 0, 3 ) == '/Q|' )
{
$this -> _margin_normal( $pdf );
$part = substr( $part, 3 );
}
// Write part
$part_content = html_entity_decode( utf8_decode( strip_tags( $part ) ) );
if( !empty( $part_content ) )
$pdf -> Write( $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ], $part_content, $link_href );
}
// Begin new paragraph
for( $b = 0; $b < $breaks_after; $b++ )
$pdf -> Write( $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ], "\n" );
// Set first paragraph is printed
$isFirstParagraph = false;
}
// On page break use pdf template
if( $page_before != $pdf -> PageNo() && !empty( $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) )
$pdf -> useTemplate( $pdf -> tplIdx );
}
// Output pdf to disk or direct to browser
header( "Content-Type: application/pdf" );
header( "Content-Disposition: attachment; filename=\"" . $post -> post_name . ".pdf\"" );
if( !empty( $this -> a2p_AdminOptions[ 'PDFOptionCacheTime' ] ) )
{
$pdf -> Output( $cachefile, 'F' );
echo file_get_contents( $cachefile );
}
else echo $pdf -> Output( '', 'S' );
die();
}
else
{
// Output pdf from disk
header( "Content-Type: application/pdf" );
header( "Content-Disposition: attachment; filename=\"" . $post -> post_name . ".pdf\"" );
echo file_get_contents( $cachefile );
die();
}
}
}
// Adminpage
function a2p_AdminPage()
{
if( function_exists( 'load_plugin_textdomain' ) ) {
load_plugin_textdomain( 'article2pdf', 'wp-content/plugins/article2pdf' );
}
$open = 0;
// Delete cache
if( $_POST[ 'a2p_admin_action' ] == 'cache_delete' )
{
if( $d = opendir( A2P_CACHEPATH ) )
{
while( false !== ($cf = readdir( $d ) ) )
{
$fSuffix_Arr = explode( ".", $cf );
if( end( $fSuffix_Arr ) == 'pdf' && $fSuffix_Arr[ 0 ] == 'a2p' && $fSuffix_Arr[ 1 ] == 'cache' )
{
unlink( A2P_CACHEPATH . $cf );
}
}
}
print "" . __( "All cache files deleted.", "article2pdf" ) . "
";
$open = 4;
}
// Delete action
if( $_POST[ 'a2p_admin_action' ] == 'delete' )
{
if( !empty( $_POST[ 'PDFTemplateFile' ] ) )
{
if( $_POST[ 'PDFTemplateFile' ] != $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] )
{
if( unlink( FPDF_TEMPLATEPATH . $_POST[ 'PDFTemplateFile' ] ) )
{
print "" . __( "The file was successfully deleted.", "article2pdf" ) . "
";
}
else
{
print "" . __( "Sorry, the deletion of the file failed. Maybe the directory is not writeable from the webserver?", "article2pdf" ) . "
";
}
}
else
{
print "" . __( "Sorry, we can't delete the file that is currently set as template file.", "article2pdf" ) . "
";
}
}
else
{
print "" . __( "No file selected, no file deleted. That was easy...", "article2pdf" ) . "
";
}
$open = 3;
}
// Upload action
if( $_POST[ 'a2p_admin_action' ] == 'upload' )
{
if( is_uploaded_file( $_FILES[ 'templatefile' ][ 'tmp_name' ] ) )
{
if( is_writeable( FPDF_TEMPLATEPATH ) )
{
$suffix_arr = explode( '.', $_FILES[ 'templatefile' ][ 'name' ] );
if( strtolower( end( $suffix_arr ) ) == 'pdf' )
{
$local_filename = preg_replace( '/[^a-z0-9\.]/i', '', $_FILES[ 'templatefile' ][ 'name' ] );
if( move_uploaded_file( $_FILES[ 'templatefile' ][ 'tmp_name' ], FPDF_TEMPLATEPATH . $local_filename ) )
{
print "" . __( "Upload successful. Now you can use the pdf file as template. Just configure that in the article2pdf options.", "article2pdf" ) . "
";
$open = 1;
}
else
{
print "" . __( "Sorry, the file could not moved to the target directory. Maybe the directory is not writeable from the webserver?", "article2pdf" ) . "
";
$open = 2;
}
}
else
{
print "" . __( "This is not a pdf file. Upload failed.", "article2pdf" ) . "
";
$open = 2;
}
}
else
{
print "" . __( "Sorry, the directory is not writeable by the webserver. Please set the correct owner.", "article2pdf" ) . "
";
$open = 2;
}
}
else
{
print "" . __( "No upload file? Sorry, dave...", "article2pdf" ) . "
";
$open = 2;
}
}
// Store action
if( $_POST[ 'a2p_admin_action' ] == 'set' )
{
$a2pOptionsNew = array();
foreach( array_keys( $this -> a2p_AdminOptions ) AS $oKey )
$a2pOptionsNew[ $oKey ] = (!eregi( "[^0-9,.]", $_POST[ $oKey ] ) ? str_replace( ',', '.', $_POST[ $oKey ] ) : $_POST[ $oKey ]);
update_option( $this -> a2p_AdminOptionsName, $a2pOptionsNew );
$this -> a2p_AdminOptions = $a2pOptionsNew;
print "" . __( "Options updated.", "article2pdf" ) . "
";
}
// Container
print "\n";
print "
" . __( "article2pdf plugin admin page", "article2pdf" ) . "
\n";
print "
";
// Output setup form
print "
\n";
// Output template uploadform
print "
\n";
// Delete template form
print "
\n";
// Cache status
print "
\n";
// Donate link and support informations
print "
\n";
print "
\n";
print "
" . __( "Donate & support", "article2pdf" ) . "
\n";
print "
\n";
print __( "Please", "article2pdf" ) . "
" . __( "DONATE", "article2pdf" ) . " " . __( "if you like this plugin.", "article2pdf" ) . "
";
print __( "Many thanks to Oliver for the great", "article2pdf" ) . "
" . __( "FPDF library", "article2pdf" ) . ".
";
print "
" . __( "If you need support, want to report bugs or suggestions, drop me an ", "article2pdf" ) . "
" . __( "email", "article2pdf" ) . " " . __( "or visit the", "article2pdf" ) . "
" . __( "plugin homepage", "article2pdf" ) . ".
";
print "
" . __( "Final statements: Code is poetry. Motorcycles are cooler than cars.", "article2pdf" );
print "
";
print "
\n";
print "
\n";
// Close container
print "
\n";
// Nice display
if( version_compare( substr($wp_version, 0, 3), '2.6', '<' ) )
{
?>
)(.*)()#siU', $p, $tables );
foreach( $tables[ 2 ] AS $tkey => $tcontent )
{
$tcontent = str_replace( array( "\r", "\n" ), '', $tcontent );
$tcontent = preg_replace( '#()|()|()|()#siU', '', $tcontent );
$p_new = '';
preg_match_all( '#()(.*)()#siU', $tcontent, $rows );
foreach( $rows[ 2 ] AS $rkey => $row )
{
preg_match_all( '#(|)(.*)(|)#siU', $row, $cells );
$p_new .= '|-====-|TR|' . count( $cells[ 2 ] );
foreach( $cells[ 2 ] AS $ckey => $ccontent )
{
$cstart = $cells[ 1 ][ $ckey ];
$cend = $cells[ 3 ][ $ckey ];
if( strpos( strtolower( $cstart ), '(.*)#siU', $p, $matches );
$is_converted = false;
$p_new = '';
foreach( $matches[ 1 ] AS $mkey => $href )
{
$linkhtml = $matches[ 0 ][ $mkey ];
$linkcontent = $matches[ 2 ][ $mkey ];
if( strpos( $href, '#' ) !== 0 && !empty( $linkcontent ) )
{
$p_first = substr( $p, 0, strpos( $p, $linkhtml ) );
$p_last = substr( $p, (strpos( $p, $linkhtml ) + strlen( $linkhtml )) );
$p_new .= $p_first;
$p_new .= "|-====-|LINK|$href|$linkcontent|-====-|/LINK|";
$p_new .= $p_middle2;
$p = preg_replace( '#^.*' . preg_quote( $linkhtml ) . '#sU', '', $p );
$is_converted = true;
}
}
if( $is_converted )
{
$p_new .= $p_last;
return $p_new;
}
else return $p;
}
// Parse bold
function _convert_bold( $p )
{
return str_replace( array( "", "", "", "" ), array( "|-====-|B|", "|-====-|B|", "|-====-|/B|", "|-====-|/B|" ), $p );
}
// Parse underline
function _convert_underline( $p )
{
return str_replace( array( "", "" ), array( "|-====-|U|", "|-====-|/U|" ), $p );
}
// Parse italic
function _convert_italic( $p )
{
return str_replace( array( "", "", "", "" ), array( "|-====-|I|", "|-====-|I|", "|-====-|/I|", "|-====-|/I|" ), $p );
}
// Parse blockquote
function _convert_blockquote( $p )
{
return str_replace( array( "", " " ), array( "|-====-|Q|", "|-====-|/Q|" ), $p );
}
// Parse list items
function _convert_listitem( $p )
{
$p = str_replace( array( "", "", ), array( "|-====-|LI|", "|-====-|/LI|" ), $p );
return str_replace( array( "", "", " ", ), array( "|-====-|UL|", "|-====-|/UL|", "|-====-|OL|", "|-====-|/OL|" ), $p );
}
// Parse headings
function _convert_heading( $p )
{
preg_match_all( '//si', $p, $matches );
foreach( $matches[ 1 ] AS $h )
$p = str_replace( array( "", "" ), array( "|-====-|H$h|", "|-====-|/H$h|" ), $p );
return $p;
}
// Parse horizontal lines
function _convert_lines( $p )
{
return preg_replace( '##iU', '|-====-|__|', $p );
}
// Set font style
function _font_normal( &$pdf )
{
$pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], '', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] );
}
function _font_underline( &$pdf )
{
$pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], 'U', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] );
}
function _font_bold( &$pdf )
{
$pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], 'B', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] );
}
function _font_italic( &$pdf )
{
$pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], 'I', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] );
}
function _font_heading( &$pdf, $hlevel = 2 )
{
$heading_font_size = $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] + (($this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] / 2) / $hlevel);
$pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], 'B', $heading_font_size );
}
// Set margins
function _margin_normal( &$pdf )
{
$pdf -> SetLeftMargin( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] );
}
function _margin_quote( &$pdf )
{
$pdf -> SetLeftMargin( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] * 2 );
$pdf -> SetX( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ] * 2 );
}
// Cache: Get cachefile path
function _cache_get_filename( &$post )
{
$fname = 'a2p.cache.' . $post -> post_name . '.pdf';
return A2P_CACHEPATH . $fname;
}
// Cache: recreate pdf or deliver cached version
function _cache_recreate( $cachefile )
{
if( file_exists( $cachefile ) && !empty( $this -> a2p_AdminOptions[ 'PDFOptionCacheTime' ] ) )
{
if( filemtime( $cachefile ) < (time() - $this -> a2p_AdminOptions[ 'PDFOptionCacheTime' ]) )
return true;
else
return false;
}
else return true;
}
}
// Create class instance
$a2p_Plugin = new article2pdf();
// PDF creation wrapper
function wrapper_a2p_CreatePdf()
{
global $a2p_Plugin;
if( strpos( $_SERVER[ 'REQUEST_URI' ], 'article2pdf=' ) )
$a2p_Plugin -> a2p_CreatePdf();
}
// Admin wrapper
function wrapper_a2p_AdminPage()
{
global $a2p_Plugin;
add_options_page( 'article2pdf Options', 'article2pdf', 9, basename(__FILE__), array( &$a2p_Plugin, 'a2p_AdminPage' ) );
}
// Add action: Install
add_action( 'activate_article2pdf/article2pdf.php', array( &$a2p_Plugin, 'a2p_GetAdminOptions' ) );
// Add action: Admin page
add_action( 'admin_menu', 'wrapper_a2p_AdminPage' );
// Add action: CreatePdf
add_action( 'template_redirect', 'wrapper_a2p_CreatePdf' );
?> |