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', str_replace( 'plugins/article2pdf/../../', '', 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 $a2p_AdminOptions; var $_page_counter; // Construct function __construct() { $this -> a2p_AdminOptionsName = 'a2pPlugin_AdminOptions'; $this -> a2p_AdminOptions = $this -> a2p_GetAdminOptions(); $this -> _page_counter = 0; } // 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' => '', 'PDFTemplatePath' => FPDF_TEMPLATEPATH, '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, 'PDFOptionCachePath' => A2P_CACHEPATH, 'PDFPageCountPosX' => 10, 'PDFPageCountPosY' => 10, 'PDFPageCountString' => 'Page %%page%% of %%pagestotal%%', 'PDFPageCountFontSize' => '8', 'PDFOptionDenySearchengines' => 'false' ); // Load existing options $_a2pOptions = get_option( $this -> a2p_AdminOptionsName ); // Overwrite defaults $update = false; if( count( $_a2pOptions ) ) { foreach( $_a2pOptions AS $oKey => $oVal ) { if( $oKey == 'PDFTemplatePath' && empty( $oVal ) ) { $oVal = FPDF_TEMPLATEPATH; $update = true; } if( $oKey == 'PDFOptionCachePath' && empty( $oVal ) ) { $oVal = A2P_CACHEPATH; $update = true; } $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 ) { update_option( $this -> a2p_AdminOptionsName, $a2pOptions ); } // Return options return $a2pOptions; } // Create pdf function a2p_CreatePdf( $content ) { // Get post global $post; // Overwrite post content with rendered content $post -> post_content = $content; // Set base url $base_url = get_option( 'siteurl' ); // Search engine spider check if( $this -> a2p_AdminOptions[ 'PDFOptionDenySearchengines' ] == 'true' && $this -> _is_bot() ) { ob_end_clean(); // Send a 410 gone to stop spidering and tell to remove already spidered content header( 'HTTP/1.1 410 Gone' ); header( 'Status: 410 Gone' ); die(); } 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( $this -> _decode_utf( strip_tags( $post -> post_title ) ) ); if( !empty( $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) ) { $pdf -> setSourceFile( $this -> a2p_AdminOptions[ 'PDFTemplatePath' ] . $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ); $pdf -> tplIdx = $pdf -> importPage( 1 ); } $this -> _add_page( $pdf ); if( !empty( $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) ) $pdf -> useTemplate( $pdf -> tplIdx ); $pdf -> SetLineWidth( 0.2 ); $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[ 'PDFTemplateMarginTop' ] ); $pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], '', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] ); $pdf -> AliasNbPages( '%%pagestotal%%' ); // 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( $this -> _decode_utf( html_entity_decode( strip_tags( $post -> post_title ), ENT_QUOTES, 'UTF-8' ) ) ) / ($pdf -> w - $pdf -> lMargin - $pdf -> rMargin) ), $this -> _decode_utf( html_entity_decode( strip_tags( $post -> post_title ), ENT_QUOTES, 'UTF-8' ) ), 1, 1, 'C' ); $this -> _font_normal( $pdf ); $pdf -> Ln(); // Write body // Convert html to natural tags $convertRegEx = array( '/
', '//iU' => ' ', '//iU' => ' ', '//iU' => '', '//iU' => '', '//iU' => '', '//iU' => '', '/ /iU' => '', '/ /iU' => '
', '//iU' => ' ' /*'/<.*>/iU' => '<>',*/ ); $post -> post_content = preg_replace( array_keys( $convertRegEx ), $convertRegEx, $post -> post_content ); // 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 = ''; $ol_counter = 0; foreach( $paragraphs_array AS $pkey => $p ) { $page_before = $pdf -> PageNo(); $p = trim( $p ); if( !empty( $p ) ) { // Convert tables $p = $this -> _convert_tables( $p ); // Convert code $p = $this -> _convert_code( $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, 5 ) == 'CODE|' ) { $this -> _font_code( $pdf ); $part = substr( $part, 5 ); $pdf -> MultiCell( 0, $this -> a2p_AdminOptions[ 'PDFOptionLineHeight' ], $this -> _decode_utf( html_entity_decode( $part, ENT_QUOTES, 'UTF-8' ) ), 1, 'L' ); $part = ''; } if( substr( $part, 0, 3 ) == 'UL|' || substr( $part, 0, 3 ) == 'OL|' ) { if( substr( $part, 0, 3 ) == 'OL|' ) $ol_counter = 1; $this -> _margin_quote( $pdf ); $part = substr( $part, 3 ); $breaks_after = 1; } if( substr( $part, 0, 3 ) == 'LI|' ) { $part = substr( $part, 3 ); if( $ol_counter == 0 ) { $part = ' ' . $part; } else { $part = "$ol_counter. " . $part; $ol_counter++; } } 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 = $this -> _decode_utf( html_entity_decode( strip_tags( trim( substr( $part, 3 ) ) ), ENT_QUOTES, 'UTF-8' ) ); $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 = $this -> _decode_utf( html_entity_decode( strip_tags( trim( substr( $part, 3 ) ) ), ENT_QUOTES, 'UTF-8' ) ); $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 ) { $local_img_path = str_replace( $base_url, '', $img ); $img = ABSPATH . (substr( $local_img_path, 0, 1 ) === '/' ? substr( $local_img_path, 1 ) : $local_img_path); } // 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) ) $this -> _add_page( $pdf ); } // 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 ) == '/CODE|' ) { $this -> _font_normal( $pdf ); $part = substr( $part, 6 ); } 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|' ) { if( substr( $part, 0, 4 ) == '/OL|' ) $ol_counter = 0; $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 = $this -> _decode_utf( html_entity_decode( strip_tags( $part ), ENT_QUOTES, 'UTF-8' ) ); 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() ) { if( !empty( $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) ) $pdf -> useTemplate( $pdf -> tplIdx ); $this -> _add_page_count( $pdf ); } } // Output pdf to disk or direct to browser ob_end_clean(); header( "Content-Type: application/pdf" ); header( "Content-Disposition: attachment; filename=\"" . $post -> post_name . ".pdf\"" ); if( !empty( $this -> a2p_AdminOptions[ 'PDFOptionCacheTime' ] ) && is_writable( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] ) ) { $pdf -> Output( $cachefile, 'F' ); echo file_get_contents( $cachefile ); } else echo $pdf -> Output( '', 'S' ); die(); } else { // Output pdf from disk ob_end_clean(); 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 single file from cache if( $_GET[ 'a2p_admin_action' ] == 'deletecachefile' ) { $cachefile = str_replace( '..', '', urldecode( $_GET[ 'cachefile' ] ) ); if( file_exists( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cachefile ) ) unlink( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cachefile ); $open = 4; } // Get file from cache if( $_GET[ 'a2p_admin_action' ] == 'getcachefile' ) { $cachefile = str_replace( '..', '', urldecode( $_GET[ 'cachefile' ] ) ); if( file_exists( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cachefile ) ) { ob_end_clean(); header( "Content-Type: application/pdf" ); header( "Content-Disposition: attachment; filename=\"" . $cachefile . "\"" ); echo file_get_contents( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cachefile ); die(); } $open = 4; } // Delete expired cache files if( $_GET[ 'a2p_admin_action' ] == 'cache_delete_expired' ) { if( is_writeable( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] ) ) { if( $d = opendir( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] ) ) { while( false !== ($cf = readdir( $d ) ) ) { $fSuffix_Arr = explode( ".", $cf ); if( end( $fSuffix_Arr ) == 'pdf' && $fSuffix_Arr[ 0 ] == 'a2p' && $fSuffix_Arr[ 1 ] == 'cache' ) { if( filemtime( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cf ) < (time() - $this -> a2p_AdminOptions[ 'PDFOptionCacheTime' ]) ) unlink( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cf ); } } } print " "; $open = 4; } else print __( "Sorry, the cache directory is not writeable. Please chmod the directory to a permission that allows the webserver to write into that directory.", "article2pdf" ); } // Delete cache if( $_POST[ 'a2p_admin_action' ] == 'cache_delete' ) { if( is_writeable( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] ) ) { if( $d = opendir( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] ) ) { while( false !== ($cf = readdir( $d ) ) ) { $fSuffix_Arr = explode( ".", $cf ); if( end( $fSuffix_Arr ) == 'pdf' && $fSuffix_Arr[ 0 ] == 'a2p' && $fSuffix_Arr[ 1 ] == 'cache' ) { unlink( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $cf ); } } } print "" . __( "All expired cache files deleted.", "article2pdf" ) . "
"; $open = 4; } else print "" . __( "All cache files deleted.", "article2pdf" ) . "
"; } // Delete template file if( $_POST[ 'a2p_admin_action' ] == 'delete' ) { if( !empty( $_POST[ 'PDFTemplateFile' ] ) ) { if( $_POST[ 'PDFTemplateFile' ] != $this -> a2p_AdminOptions[ 'PDFTemplateFile' ] ) { if( unlink( $this -> a2p_AdminOptions[ 'PDFTemplatePath' ] . $_POST[ 'PDFTemplateFile' ] ) ) { print "" . __( "Sorry, the cache directory is not writeable. Please chmod the directory to a permission that allows the webserver to write into that directory.", "article2pdf" ) . "
"; } else { 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" ) . "
"; } $open = 3; } // Upload template file if( $_POST[ 'a2p_admin_action' ] == 'upload' ) { if( is_uploaded_file( $_FILES[ 'templatefile' ][ 'tmp_name' ] ) ) { if( is_writeable( $this -> a2p_AdminOptions[ 'PDFTemplatePath' ] ) ) { $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' ], $this -> a2p_AdminOptions[ 'PDFTemplatePath' ] . $local_filename ) ) { print "" . __( "No file selected, no file deleted. That was easy...", "article2pdf" ) . "
"; $open = 1; } else { print "" . __( "Upload successful. Now you can use the pdf file as template. Just configure that in the article2pdf options.", "article2pdf" ) . "
"; $open = 2; } } 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; } } // Set template file 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; if( $_POST[ 'createdir' ] == 'yes' ) { if( !file_exists( $_POST[ 'PDFTemplatePath' ] ) ) mkdir( $_POST[ 'PDFTemplatePath' ] ); } print "" . __( "No upload file? Sorry, dave...", "article2pdf" ) . "
"; } // Container print "" . __( "Options updated.", "article2pdf" ) . "
\n"; 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( '#(" . __( "article2pdf plugin admin page", "article2pdf" ) . "
\n"; print "
"; // Output setup form print "\n"; print "\n"; // Output template uploadform print "\n"; print "\n"; print "\n"; print "\n"; // Delete template form print "\n"; print "\n"; print "\n"; print "\n"; // Cache status if( !empty( $_GET[ 'cache_sort_by' ] ) ) $open = 4; print "\n"; print "\n"; print "\n"; print "\n"; // Donate link and support informations print "\n"; print "\n"; print "\n"; print "\n"; // Close container print "\n"; print "\n"; print "" . __( "Donate & support", "article2pdf" ) . "
\n"; print "\n"; print __( "Please", "article2pdf" ) . " " . __( "DONATE", "article2pdf" ) . " " . __( "if you like this plugin.", "article2pdf" ) . ""; print "
"; 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 "
" . __( "Translations: ", "article2pdf" ) . " Alejandro Urrutia Daglio (Español), Marc Schieferdecker (Deutsch)
"; print "
" . __( "Final statements: Code is poetry. Motorcycles are cooler than cars.", "article2pdf" ); print ")|( )|()#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 ), ' ", " ", "", "" ), array( "|-====-|CODE|", "|-====-|CODE|", "|-====-|/CODE|", "|-====-|/CODE|" ), $p ); } // Parse links function _convert_links( $p ) { preg_match_all( '#(.*)#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|", "|-====-|UL|", "|-====-|/UL|", "|-====-|OL|", "|-====-|/OL|" ), $p ); return $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_code( &$pdf ) { $pdf -> SetFont( 'Courier', '', $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 ); } // Add page to pdf function _add_page( &$pdf ) { $pdf -> AddPage(); $this -> _add_page_count( $pdf ); // Set to start of $pdf -> SetXY( $this -> a2p_AdminOptions[ 'PDFTemplateMarginLeft' ], $this -> a2p_AdminOptions[ 'PDFTemplateMarginRight' ] ); } // Write page count function _add_page_count( &$pdf ) { if( !empty( $this -> a2p_AdminOptions[ 'PDFPageCountString' ] ) && !empty( $this -> a2p_AdminOptions[ 'PDFPageCountPosX' ] ) && !empty( $this -> a2p_AdminOptions[ 'PDFPageCountPosY' ] ) && !empty( $this -> a2p_AdminOptions[ 'PDFPageCountFontSize' ] ) ) { $this -> _page_counter++; $page_str = str_replace( '%%page%%', $this -> _page_counter, $this -> a2p_AdminOptions[ 'PDFPageCountString' ] ); $x = $pdf -> GetX(); $y = $pdf -> GetY(); $lMargin = $pdf -> lMargin; $tMargin = $pdf -> tMargin; $pdf -> SetLeftMargin( 0 ); $pdf -> SetTopMargin( 0 ); $pdf -> SetAutoPageBreak( false, 0 ); $pdf -> SetXY( $this -> a2p_AdminOptions[ 'PDFPageCountPosX' ], $this -> a2p_AdminOptions[ 'PDFPageCountPosY' ] ); $pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], '', $this -> a2p_AdminOptions[ 'PDFPageCountFontSize' ] ); $pdf -> Write( 0, $page_str ); $pdf -> SetLeftMargin( $lMargin ); $pdf -> SetTopMargin( $tMargin ); $pdf -> SetAutoPageBreak( true, $this -> a2p_AdminOptions[ 'PDFTemplateMarginBottom' ] ); $pdf -> SetFont( $this -> a2p_AdminOptions[ 'PDFOptionFont' ], '', $this -> a2p_AdminOptions[ 'PDFOptionFontSize' ] ); $pdf -> SetXY( $x, $y ); } } /* * Cache functions */ // Cache: Get cachefile path function _cache_get_filename( &$post ) { $fname = 'a2p.cache.' . $post -> post_name . '.pdf'; return $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] . $fname; } // Cache: recreate pdf or deliver cached version function _cache_recreate( $cachefile ) { if( file_exists( $cachefile ) && !empty( $this -> a2p_AdminOptions[ 'PDFOptionCacheTime' ] ) && is_writeable( $this -> a2p_AdminOptions[ 'PDFOptionCachePath' ] ) ) { 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( $content ) { global $a2p_Plugin; if( strpos( $_SERVER[ 'REQUEST_URI' ], 'article2pdf=' ) ) $a2p_Plugin -> a2p_CreatePdf( $content ); else return $content; } // Admin wrapper function wrapper_a2p_AdminPage() { global $a2p_Plugin; add_options_page( 'article2pdf Options', 'article2pdf', 9, basename(__FILE__), array( &$a2p_Plugin, 'a2p_AdminPage' ) ); } // Store page content in buffer if plugin is active function a2p_init() { if( strpos( $_SERVER[ 'REQUEST_URI' ], 'article2pdf=' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'a2p_admin_action=getcachefile' ) ) { ob_start('a2p_finish'); } } function a2p_finish() { return ''; } // With this I drop rendered content (ob_end_clean is called by a2p_CreatePdf) add_action( 'plugins_loaded', 'a2p_init' ); // Add action: Install - create default options add_action( 'activate_article2pdf/article2pdf.php', array( &$a2p_Plugin, 'a2p_GetAdminOptions' ) ); // Add action: Admin page add_action( 'admin_menu', 'wrapper_a2p_AdminPage' ); // Generate PDF from content after all shortcodes and filters are done add_filter( 'the_content', 'wrapper_a2p_CreatePdf', 9999 ); ?>