17d101cc1SGerry Weißbach<?php 27d101cc1SGerry Weißbach 37d101cc1SGerry Weißbachif (!defined('DOKU_PLUGIN')) die('meh'); 47d101cc1SGerry Weißbach 55393176fSGerry Weißbachif (!empty($_REQUEST['pdfExport']) && intval($_REQUEST['pdfExport']) == 1 && file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php')) { 67d101cc1SGerry Weißbach 77d101cc1SGerry Weißbach require_once(DOKU_PLUGIN . 'siteexport/inc/mpdf.php'); 87d101cc1SGerry Weißbach class siteexport_pdfgenerator 97d101cc1SGerry Weißbach { 107d101cc1SGerry Weißbach private $functions; 117d101cc1SGerry Weißbach 12b324a190SMichael Hamann public function __construct($functions = null) 137d101cc1SGerry Weißbach { 147d101cc1SGerry Weißbach $this->functions = $functions; 157d101cc1SGerry Weißbach } 167d101cc1SGerry Weißbach 17a8c17ab5Si-net /// software public function createPDFFromFile($filename, &$NAME) { 187d101cc1SGerry Weißbach 19022fb779Si-net /// software global $INPUT; 20022fb779Si-net /// software 21a8c17ab5Si-net /// software if (!preg_match("/" . $this->functions->settings->fileType . "$/", $NAME)) { 22a8c17ab5Si-net /// software $this->functions->debug->message("Filetype " . $this->functions->settings->fileType . " did not match filename '$NAME'", null, 4); 237d101cc1SGerry Weißbach return false; 247d101cc1SGerry Weißbach } 257d101cc1SGerry Weißbach 267f6f0f99SGerry Weißbach $mpdf = new siteexportPDF($this->functions->debug); 277d101cc1SGerry Weißbach 287d101cc1SGerry Weißbach if (!$mpdf) { 297d101cc1SGerry Weißbach $this->functions->debug->message("Could not instantiate MPDF", null, 4); 307d101cc1SGerry Weißbach return false; 317d101cc1SGerry Weißbach } 327d101cc1SGerry Weißbach 33e6ebb3b0SGerry Weißbach $html = @file_get_contents($filename); 347d101cc1SGerry Weißbach 357d101cc1SGerry Weißbach if (!strstr($html, "<html")) { 367d101cc1SGerry Weißbach $this->functions->debug->message("Filecontent had no HTML starting tag", null, 4); 377d101cc1SGerry Weißbach return false; 387d101cc1SGerry Weißbach } 397d101cc1SGerry Weißbach 407d101cc1SGerry Weißbach // Save HTML too 417d101cc1SGerry Weißbach $this->functions->debug->message("Arranging HTML", null, 2); 427d101cc1SGerry Weißbach $this->arrangeHtml($html, 'bl,acronym'); 437d101cc1SGerry Weißbach $this->functions->debug->message("Done arranging HTML:", $html, 1); 447d101cc1SGerry Weißbach 45a4a5b91dSGerry Weißbach $mpdf->debug = false; 467d101cc1SGerry Weißbach $mpdf->list_indent_first_level = 1; // Indents the first level of lists. 47a8c17ab5Si-net /// software 487d101cc1SGerry Weißbach $mpdf->usepre = false; 497d101cc1SGerry Weißbach $mpdf->margin_bottom_collapse = true; 507d101cc1SGerry Weißbach $mpdf->SetDisplayMode('fullpage'); 517d101cc1SGerry Weißbach $mpdf->restoreBlockPageBreaks = true; 52022fb779Si-net /// software 53022fb779Si-net /// software $mpdf->dpi = $INPUT->int('dpi', 96, true); 54022fb779Si-net /// software $mpdf->img_dpi = $INPUT->int('dpi', 96, true); 557d101cc1SGerry Weißbach 56a4a5b91dSGerry Weißbach $mpdf->setBasePath(empty($this->functions->settings->depth) ? './' : $this->functions->settings->depth); 577d101cc1SGerry Weißbach 587d101cc1SGerry Weißbach $mpdf->ignore_invalid_utf8 = true; 59876523d1SGerry Weißbach $mpdf->mirrorMargins = $this->functions->getConf('useOddEven'); // don't mirror margins 60876523d1SGerry Weißbach $mpdf->setAutoTopMargin = 'pad'; 61876523d1SGerry Weißbach $mpdf->setAutoBottomMargin = 'pad'; 628914cf7eSGerry Weißbach 637d101cc1SGerry Weißbach $mpdf->WriteHTML($html); 647d101cc1SGerry Weißbach $mpdf->Output($filename, "F"); 65a8c17ab5Si-net /// software 66e6ebb3b0SGerry Weißbach return $html; 677d101cc1SGerry Weißbach } 687d101cc1SGerry Weißbach 69a8c17ab5Si-net /// software private function arrangeHtml(&$html, $norendertags = '') 707d101cc1SGerry Weißbach { 717d101cc1SGerry Weißbach global $conf; 727d101cc1SGerry Weißbach 737d101cc1SGerry Weißbach // add bookmark links 7406a6f0adSGerry Weißbach $html = preg_replace_callback("/<h(\d)(.*?)>(.*?)<\/h\\1>/s", array($this, '__pdfHeaderCallback'), $html); 757d101cc1SGerry Weißbach $html = preg_replace_callback("/<\/div>\s*?<h({$conf['plugin']['siteexport']['PDFHeaderPagebreak']})(.*?)>/s", array($this, '__pdfHeaderCallbackPagebreak'), $html); 767d101cc1SGerry Weißbach $html = preg_replace("/(<img.*?mediacenter.*?\/>)/", "<table style=\"width:100%; border: 0px solid #000;\"><tr><td style=\"text-align: center\">$1</td></tr></table>", $html); 77e7fd0196SGerry Weißbach 78e7fd0196SGerry Weißbach // Remove p arround img and table 79e7fd0196SGerry Weißbach $html = preg_replace("/<p[^>]*?>(\s*?<img[^>]*?\/?>\s*?)<\/p>/s", "$1", $html); 80e7fd0196SGerry Weißbach $html = preg_replace("/<p[^>]*?>(\s*?<table.*?<\/table>\s*?)<\/p>/s", "$1", $html); 817d101cc1SGerry Weißbach $html = preg_replace_callback("/<pre(.*?)>(.*?)<\/pre>/s", array($this, '__pdfPreCodeCallback'), $html); 827d101cc1SGerry Weißbach $html = preg_replace_callback("/<a href=\"mailto:(.*?)\".*?>(.*?)<\/a>/s", array($this, '__pdfMailtoCallback'), $html); 837d101cc1SGerry Weißbach /**/ 847d101cc1SGerry Weißbach 857d101cc1SGerry Weißbach $standardReplacer = array( 867d101cc1SGerry Weißbach // insert a pagebreak for support of WRAP and PAGEBREAK plugins 877d101cc1SGerry Weißbach '<br style="page-break-after:always;">' => '<pagebreak />', 887d101cc1SGerry Weißbach '<div class="wrap_pagebreak"></div>' => '<pagebreak />', 897d101cc1SGerry Weißbach '<sup>' => '<sup class="sup">', 907d101cc1SGerry Weißbach '<sub>' => '<sub class="sub">', 916792d0cfSGerry Weißbach '<code>' => '<code class="code">' 927d101cc1SGerry Weißbach ); 937d101cc1SGerry Weißbach $html = str_replace(array_keys($standardReplacer), array_values($standardReplacer), $html); 947d101cc1SGerry Weißbach 957d101cc1SGerry Weißbach // thanks to Jared Ong 967d101cc1SGerry Weißbach // Customized to strip all span tags so that the wiki <code> SQL would display properly 977d101cc1SGerry Weißbach $norender = explode(',', $norendertags); 987d101cc1SGerry Weißbach $html = $this->strip_only($html, $norender); //array('span','acronym')); 997d101cc1SGerry Weißbach $html = $this->strip_htmlencodedchars($html); 1007d101cc1SGerry Weißbach // Customized to strip all span tags so that the wiki <code> SQL would display properly 1017d101cc1SGerry Weißbach } 1027d101cc1SGerry Weißbach 1037d101cc1SGerry Weißbach private function __pdfMailtoCallback($DATA) { 1047d101cc1SGerry Weißbach if ($DATA[1] == $DATA[2]) { 1057d101cc1SGerry Weißbach $DATA[2] = $this->deobfuscate($DATA[2]); 1067d101cc1SGerry Weißbach } 1077d101cc1SGerry Weißbach $DATA[1] = $this->deobfuscate($DATA[1]); 1087d101cc1SGerry Weißbach return "<a href=\"mailto:{$DATA[1]}\">{$DATA[2]}</a>"; 1097d101cc1SGerry Weißbach } 1107d101cc1SGerry Weißbach 1117d101cc1SGerry Weißbach private function __pdfPreCodeCallback($DATA) { 1127d101cc1SGerry Weißbach 1137d101cc1SGerry Weißbach $code = nl2br($DATA[2]); 1147d101cc1SGerry Weißbach $code = preg_replace_callback("/(^|<br \/>)(\s+)(\S)/s", array($this, '__pdfPreWhitespacesCallback'), $code); 1157d101cc1SGerry Weißbach 1167d101cc1SGerry Weißbach return "\n<pre" . $DATA[1] . ">\n" . $code . "\n</pre>\n"; 1177d101cc1SGerry Weißbach } 1187d101cc1SGerry Weißbach 1197d101cc1SGerry Weißbach private function __pdfPreWhitespacesCallback($DATA) { 12069bdc716SGerry Weißbach return $DATA[1] . "\n" . str_repeat(" ", strlen($DATA[2])-($DATA[2][0] == "\n" ? 1 : 0)) . $DATA[3]; 1217d101cc1SGerry Weißbach } 1227d101cc1SGerry Weißbach 1237d101cc1SGerry Weißbach private function __pdfHeaderCallback($DATA) { 124984a49eaSGerry Weißbach $contentText = htmlspecialchars_decode(preg_replace("/<\/?.*?>/s", '', $DATA[3]), ENT_NOQUOTES); // 2014-07-23 Do not encode again. or ä -> &auml; 125876523d1SGerry Weißbach return '<h' . $DATA[1] . $DATA[2] . '><tocentry content="' . $contentText . '" level="' . ($DATA[1]-1) . '" /><bookmark content="' . $contentText . '" level="' . ($DATA[1]-1) . '" />' . $DATA[3] . '</h' . $DATA[1] . '>'; 1267d101cc1SGerry Weißbach } 1277d101cc1SGerry Weißbach 1287d101cc1SGerry Weißbach private function __pdfHeaderCallbackPagebreak($DATA) { 1297d101cc1SGerry Weißbach return '</div>' . "\r\n" . '<pagebreak />' . "\r\n\r\n" . '<h' . $DATA[1] . $DATA[2] . '>'; 1307d101cc1SGerry Weißbach } 1317d101cc1SGerry Weißbach // thanks to Jared Ong 1327d101cc1SGerry Weißbach // Custom function for help in stripping span tags 1337d101cc1SGerry Weißbach private function strip_only($str, $tags) { 1347d101cc1SGerry Weißbach if (!is_array($tags)) { 1357d101cc1SGerry Weißbach $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 1367d101cc1SGerry Weißbach if (end($tags) == '') array_pop($tags); 1377d101cc1SGerry Weißbach } 1387d101cc1SGerry Weißbach 1397d101cc1SGerry Weißbach foreach ($tags as $tag) $str = preg_replace('#</?' . $tag . '[^>]*>#is', '', $str); 1407d101cc1SGerry Weißbach return $str; 1417d101cc1SGerry Weißbach } 1427d101cc1SGerry Weißbach // Custom function for help in stripping span tags 1437d101cc1SGerry Weißbach 1447d101cc1SGerry Weißbach // Custom function for help in replacing ' " > < & 1457d101cc1SGerry Weißbach private function strip_htmlencodedchars($str) { 1467d101cc1SGerry Weißbach $str = str_replace(''', '\'', $str); 1477d101cc1SGerry Weißbach return $str; 1487d101cc1SGerry Weißbach } 1497d101cc1SGerry Weißbach // Custom function for help in replacing ' " > < & 1507d101cc1SGerry Weißbach 1517d101cc1SGerry Weißbach /** 1527d101cc1SGerry Weißbach * return an de-obfuscated email address in line with $conf['mailguard'] setting 1537d101cc1SGerry Weißbach */ 1547d101cc1SGerry Weißbach private function deobfuscate($email) { 1557d101cc1SGerry Weißbach global $conf; 1567d101cc1SGerry Weißbach 1577d101cc1SGerry Weißbach switch ($conf['mailguard']) { 1587d101cc1SGerry Weißbach case 'visible' : 159a8c17ab5Si-net /// software return /** @scrutinizer ignore-call */ strtr($email, array(' [at] ' => '@', ' [dot] ' => '.', ' [dash] ' => '-')); 1607d101cc1SGerry Weißbach 1617d101cc1SGerry Weißbach case 'hex' : 1627d101cc1SGerry Weißbach $encode = ''; 1637d101cc1SGerry Weißbach $len = strlen($email); 1647d101cc1SGerry Weißbach for ($x = 0; $x < $len; $x += 6) { 165*892afd94SGerry Weißbach $encode .= chr((int)hexdec($email[$x+3] . $email[$x+4])); 1667d101cc1SGerry Weißbach } 1677d101cc1SGerry Weißbach return $encode; 1687d101cc1SGerry Weißbach 1697d101cc1SGerry Weißbach case 'none' : 1707d101cc1SGerry Weißbach default : 1717d101cc1SGerry Weißbach return $email; 1727d101cc1SGerry Weißbach } 1737d101cc1SGerry Weißbach } 1747d101cc1SGerry Weißbach } 1757d101cc1SGerry Weißbach} 176