1<?php 2/** 3 * dw2Pdf Plugin: Conversion from dokuwiki content to pdf. 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Luigi Micco <l.micco@tiscali.it> 7 * @author Andreas Gohr <andi@splitbrain.org> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12 13/** 14 * Class action_plugin_dw2pdf 15 * 16 * Export hmtl content to pdf, for different url parameter configurations 17 * DokuPDF which extends mPDF is used for generating the pdf from html. 18 */ 19class action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 20 /** 21 * Settings for current export, collected from url param, plugin config, global config 22 * 23 * @var array 24 */ 25 protected $exportConfig = null; 26 protected $tpl; 27 protected $list = array(); 28 29 /** 30 * Constructor. Sets the correct template 31 */ 32 public function __construct() { 33 $this->tpl = $this->getExportConfig('template'); 34 } 35 36 /** 37 * Register the events 38 * 39 * @param Doku_Event_Handler $controller 40 */ 41 public function register(Doku_Event_Handler $controller) { 42 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 43 $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 44 } 45 46 /** 47 * Do the HTML to PDF conversion work 48 * 49 * @param Doku_Event $event 50 * @return bool 51 */ 52 public function convert(Doku_Event $event) { 53 global $ACT; 54 global $ID; 55 56 // our event? 57 if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false; 58 59 // check user's rights 60 if(auth_quickaclcheck($ID) < AUTH_READ) return false; 61 62 if($data = $this->collectExportPages($event)) { 63 list($title, $this->list) = $data; 64 } else { 65 return false; 66 } 67 68 // it's ours, no one else's 69 $event->preventDefault(); 70 71 // prepare cache and its dependencies 72 $depends = array(); 73 $cache = $this->prepareCache($title, $depends); 74 75 // hard work only when no cache available 76 if(!$this->getConf('usecache') || !$cache->useCache($depends)) { 77 // generating the pdf may take a long time for larger wikis / namespaces with many pages 78 set_time_limit(0); 79 80 $this->generatePDF($cache->cache, $title); 81 } 82 83 // deliver the file 84 $this->sendPDFFile($cache->cache, $title); 85 return true; 86 } 87 88 89 /** 90 * Obtain list of pages and title, based on url parameters 91 * 92 * @param Doku_Event $event 93 * @return string|bool 94 */ 95 protected function collectExportPages(Doku_Event $event) { 96 global $ACT; 97 global $ID; 98 global $INPUT; 99 global $conf; 100 101 // list of one or multiple pages 102 $list = array(); 103 104 if($ACT == 'export_pdf') { 105 $list[0] = $ID; 106 $title = $INPUT->str('pdftitle'); //DEPRECATED 107 $title = $INPUT->str('book_title', $title, true); 108 if(empty($title)) { 109 $title = p_get_first_heading($ID); 110 } 111 112 } elseif($ACT == 'export_pdfns') { 113 //check input for title and ns 114 if(!$title = $INPUT->str('book_title')) { 115 $this->showPageWithErrorMsg($event, 'needtitle'); 116 return false; 117 } 118 $pdfnamespace = cleanID($INPUT->str('book_ns')); 119 if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 120 $this->showPageWithErrorMsg($event, 'needns'); 121 return false; 122 } 123 124 //sort order 125 $order = $INPUT->str('book_order', 'natural', true); 126 $sortoptions = array('pagename', 'date', 'natural'); 127 if(!in_array($order, $sortoptions)) { 128 $order = 'natural'; 129 } 130 131 //search depth 132 $depth = $INPUT->int('book_nsdepth', 0); 133 if($depth < 0) { 134 $depth = 0; 135 } 136 137 //page search 138 $result = array(); 139 $opts = array('depth' => $depth); //recursive all levels 140 $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 141 search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 142 143 //sorting 144 if(count($result) > 0) { 145 if($order == 'date') { 146 usort($result, array($this, '_datesort')); 147 } elseif($order == 'pagename') { 148 usort($result, array($this, '_pagenamesort')); 149 } 150 } 151 152 foreach($result as $item) { 153 $list[] = $item['id']; 154 } 155 156 if ($pdfnamespace !== '') { 157 if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 158 if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 159 array_unshift($list,rtrim($pdfnamespace,':')); 160 } 161 } 162 } 163 164 } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 165 /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 166 //is in Bookmanager of bookcreator plugin a title given? 167 $title = $INPUT->str('pdfbook_title'); //DEPRECATED 168 $title = $INPUT->str('book_title', $title, true); 169 if(empty($title)) { 170 $this->showPageWithErrorMsg($event, 'needtitle'); 171 return false; 172 } else { 173 $list = explode("|", $_COOKIE['list-pagelist']); 174 } 175 176 } elseif($INPUT->has('selection')) { 177 //handle Bookcreator requests based at localStorage 178// if(!checkSecurityToken()) { 179// http_status(403); 180// print $this->getLang('empty'); 181// exit(); 182// } 183 184 $json = new JSON(JSON_LOOSE_TYPE); 185 $list = $json->decode($INPUT->post->str('selection', '', true)); 186 if(!is_array($list) || empty($list)) { 187 http_status(400); 188 print $this->getLang('empty'); 189 exit(); 190 } 191 192 $title = $INPUT->str('pdfbook_title'); //DEPRECATED 193 $title = $INPUT->str('book_title', $title, true); 194 if(empty($title)) { 195 http_status(400); 196 print $this->getLang('needtitle'); 197 exit(); 198 } 199 200 } else { 201 //show empty bookcreator message 202 $this->showPageWithErrorMsg($event, 'empty'); 203 return false; 204 } 205 206 $list = array_map('cleanID', $list); 207 return array($title, $list); 208 } 209 210 /** 211 * Prepare cache 212 * 213 * @param string $title 214 * @param array $depends (reference) array with dependencies 215 * @return cache 216 */ 217 protected function prepareCache($title, &$depends) { 218 global $REV; 219 220 $cachekey = join(',', $this->list) 221 . $REV 222 . $this->getExportConfig('template') 223 . $this->getExportConfig('pagesize') 224 . $this->getExportConfig('orientation') 225 . $this->getExportConfig('doublesided') 226 . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 227 . $title; 228 $cache = new cache($cachekey, '.dw2.pdf'); 229 230 $dependencies = array(); 231 foreach($this->list as $pageid) { 232 $relations = p_get_metadata($pageid, 'relation'); 233 234 if(is_array($relations)) { 235 if(array_key_exists('media', $relations) && is_array($relations['media'])) { 236 foreach($relations['media'] as $mediaid => $exists) { 237 if($exists) { 238 $dependencies[] = mediaFN($mediaid); 239 } 240 } 241 } 242 243 if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 244 foreach($relations['haspart'] as $part_pageid => $exists) { 245 if($exists) { 246 $dependencies[] = wikiFN($part_pageid); 247 } 248 } 249 } 250 } 251 252 $dependencies[] = metaFN($pageid, '.meta'); 253 } 254 255 $depends['files'] = array_map('wikiFN', $this->list); 256 $depends['files'][] = __FILE__; 257 $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 258 $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 259 $depends['files'] = array_merge( 260 $depends['files'], 261 $dependencies, 262 getConfigFiles('main') 263 ); 264 return $cache; 265 } 266 267 /** 268 * Set error notification and reload page again 269 * 270 * @param Doku_Event $event 271 * @param string $msglangkey key of translation key 272 */ 273 private function showPageWithErrorMsg(Doku_Event $event, $msglangkey) { 274 msg($this->getLang($msglangkey), -1); 275 276 $event->data = 'show'; 277 $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 278 } 279 280 /** 281 * Build a pdf from the html 282 * 283 * @param string $cachefile 284 * @param string $title 285 */ 286 protected function generatePDF($cachefile, $title) { 287 global $ID; 288 global $REV; 289 global $INPUT; 290 291 //some shortcuts to export settings 292 $hasToC = $this->getExportConfig('hasToC'); 293 $levels = $this->getExportConfig('levels'); 294 $isDebug = $this->getExportConfig('isDebug'); 295 296 // initialize PDF library 297 require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 298 299 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), $this->getExportConfig('orientation')); 300 301 // let mpdf fix local links 302 $self = parse_url(DOKU_URL); 303 $url = $self['scheme'] . '://' . $self['host']; 304 if($self['port']) { 305 $url .= ':' . $self['port']; 306 } 307 $mpdf->setBasePath($url); 308 309 // Set the title 310 $mpdf->SetTitle($title); 311 312 // some default document settings 313 //note: double-sided document, starts at an odd page (first page is a right-hand side page) 314 // single-side document has only odd pages 315 $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 316 $mpdf->setAutoTopMargin = 'stretch'; 317 $mpdf->setAutoBottomMargin = 'stretch'; 318// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 319 if($hasToC) { 320 $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 321 $mpdf->h2toc = $levels; 322 } else { 323 $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 324 } 325 326 // load the template 327 $template = $this->load_template($title); 328 329 // prepare HTML header styles 330 $html = ''; 331 if($isDebug) { 332 $html .= '<html><head>'; 333 $html .= '<style type="text/css">'; 334 } 335 $styles = $this->load_css(); 336 $styles .= '@page { size:auto; ' . $template['page'] . '}'; 337 $styles .= '@page :first {' . $template['first'] . '}'; 338 339 $styles .= '@page landscape-page { size:landscape }'; 340 $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 341 $styles .= '@page portrait-page { size:portrait }'; 342 $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 343 344 $mpdf->WriteHTML($styles, 1); 345 346 if($isDebug) { 347 $html .= $styles; 348 $html .= '</style>'; 349 $html .= '</head><body>'; 350 } 351 352 $body_start = $template['html']; 353 $body_start .= '<div class="dokuwiki">'; 354 355 // insert the cover page 356 $body_start .= $template['cover']; 357 358 $mpdf->WriteHTML($body_start, 2, true, false); //start body html 359 if($isDebug) { 360 $html .= $body_start; 361 } 362 if($hasToC) { 363 //Note: - for double-sided document the ToC is always on an even number of pages, so that the following content is on a correct odd/even page 364 // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 365 // - there is no page numbering at the pages of the ToC 366 $mpdf->TOCpagebreakByArray( 367 array( 368 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 369 'toc-bookmarkText' => $this->getLang('tocheader'), 370 'links' => true, 371 'outdent' => '1em', 372 'resetpagenum' => true, //start pagenumbering after ToC 373 'pagenumstyle' => '1' 374 ) 375 ); 376 $html .= '<tocpagebreak>'; 377 } 378 379 // store original pageid 380 $keep = $ID; 381 382 // loop over all pages 383 $cnt = count($this->list); 384 for($n = 0; $n < $cnt; $n++) { 385 $page = $this->list[$n]; 386 $filename = wikiFN($page, $REV); 387 388 if(!file_exists($filename)) { 389 continue; 390 } 391 392 // set global pageid to the rendered page 393 $ID = $page; 394 395 $pagehtml = p_cached_output($filename, 'dw2pdf', $page); 396 $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 397 if($n < ($cnt - 1)) { 398 $pagehtml .= '<pagebreak />'; 399 } 400 401 $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 402 if($isDebug) { 403 $html .= $pagehtml; 404 } 405 } 406 //restore ID 407 $ID = $keep; 408 409 // insert the back page 410 $body_end = $template['back']; 411 412 $body_end .= '</div>'; 413 414 $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 415 if($isDebug) { 416 $html .= $body_end; 417 $html .= '</body>'; 418 $html .= '</html>'; 419 } 420 421 //Return html for debugging 422 if($isDebug) { 423 if($INPUT->str('debughtml', 'text', true) == 'html') { 424 echo $html; 425 } else { 426 header('Content-Type: text/plain; charset=utf-8'); 427 echo $html; 428 } 429 exit(); 430 }; 431 432 // write to cache file 433 $mpdf->Output($cachefile, 'F'); 434 } 435 436 /** 437 * @param string $cachefile 438 * @param string $title 439 */ 440 protected function sendPDFFile($cachefile, $title) { 441 header('Content-Type: application/pdf'); 442 header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 443 header('Pragma: public'); 444 http_conditionalRequest(filemtime($cachefile)); 445 446 $filename = rawurlencode(cleanID(strtr($title, ':/;"', ' '))); 447 if($this->getConf('output') == 'file') { 448 header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 449 } else { 450 header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 451 } 452 453 //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 454 header('Set-Cookie: fileDownload=true; path=/'); 455 456 //try to send file, and exit if done 457 http_sendfile($cachefile); 458 459 $fp = @fopen($cachefile, "rb"); 460 if($fp) { 461 http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 462 } else { 463 header("HTTP/1.0 500 Internal Server Error"); 464 print "Could not read file - bad permissions?"; 465 } 466 exit(); 467 } 468 469 /** 470 * Load the various template files and prepare the HTML/CSS for insertion 471 * 472 * @param string $title 473 * @return array 474 */ 475 protected function load_template($title) { 476 global $ID; 477 global $conf; 478 479 // this is what we'll return 480 $output = array( 481 'cover' => '', 482 'html' => '', 483 'page' => '', 484 'first' => '', 485 'cite' => '', 486 ); 487 488 // prepare header/footer elements 489 $html = ''; 490 foreach(array('header', 'footer') as $section) { 491 foreach(array('', '_odd', '_even', '_first') as $order) { 492 $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 493 if(file_exists($file)) { 494 $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 495 $html .= file_get_contents($file) . DOKU_LF; 496 $html .= '</htmlpage' . $section . '>' . DOKU_LF; 497 498 // register the needed pseudo CSS 499 if($order == '_first') { 500 $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 501 } elseif($order == '_even') { 502 $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 503 } elseif($order == '_odd') { 504 $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 505 } else { 506 $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 507 } 508 } 509 } 510 } 511 512 // prepare replacements 513 $replace = array( 514 '@PAGE@' => '{PAGENO}', 515 '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 516 '@TITLE@' => hsc($title), 517 '@WIKI@' => $conf['title'], 518 '@WIKIURL@' => DOKU_URL, 519 '@DATE@' => dformat(time()), 520 '@BASE@' => DOKU_BASE, 521 '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 522 ); 523 524 // set HTML element 525 $html = str_replace(array_keys($replace), array_values($replace), $html); 526 //TODO For bookcreator $ID (= bookmanager page) makes no sense 527 $output['html'] = $this->page_depend_replacements($html, $ID); 528 529 // cover page 530 $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 531 if(file_exists($coverfile)) { 532 $output['cover'] = file_get_contents($coverfile); 533 $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 534 $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 535 $output['cover'] .= '<pagebreak />'; 536 } 537 538 // cover page 539 $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 540 if(file_exists($backfile)) { 541 $output['back'] = '<pagebreak />'; 542 $output['back'] .= file_get_contents($backfile); 543 $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 544 $output['back'] = $this->page_depend_replacements($output['back'], $ID); 545 } 546 547 // citation box 548 $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 549 if(file_exists($citationfile)) { 550 $output['cite'] = file_get_contents($citationfile); 551 $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 552 } 553 554 return $output; 555 } 556 557 /** 558 * @param string $raw code with placeholders 559 * @param string $id pageid 560 * @return string 561 */ 562 protected function page_depend_replacements($raw, $id) { 563 global $REV; 564 565 // generate qr code for this page using google infographics api 566 $qr_code = ''; 567 if($this->getConf('qrcodesize')) { 568 $url = urlencode(wl($id, '', '&', true)); 569 $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' . 570 $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />'; 571 } 572 // prepare replacements 573 $replace['@ID@'] = $id; 574 $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 575 $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&"); 576 $replace['@QRCODE@'] = $qr_code; 577 578 $content = str_replace(array_keys($replace), array_values($replace), $raw); 579 580 // @DATE(<date>[, <format>])@ 581 $content = preg_replace_callback( 582 '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 583 array($this, 'replacedate'), 584 $content 585 ); 586 587 return $content; 588 } 589 590 591 /** 592 * (callback) Replace date by request datestring 593 * e.g. '%m(30-11-1975)' is replaced by '11' 594 * 595 * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 596 * @return string 597 */ 598 function replacedate($match) { 599 global $conf; 600 //no 2nd argument for default date format 601 if($match[2] == null) { 602 $match[2] = $conf['dformat']; 603 } 604 return strftime($match[2], strtotime($match[1])); 605 } 606 607 608 /** 609 * Load all the style sheets and apply the needed replacements 610 */ 611 protected function load_css() { 612 global $conf; 613 //reusue the CSS dispatcher functions without triggering the main function 614 define('SIMPLE_TEST', 1); 615 require_once(DOKU_INC . 'lib/exe/css.php'); 616 617 // prepare CSS files 618 $files = array_merge( 619 array( 620 DOKU_INC . 'lib/styles/screen.css' 621 => DOKU_BASE . 'lib/styles/', 622 DOKU_INC . 'lib/styles/print.css' 623 => DOKU_BASE . 'lib/styles/', 624 ), 625 css_pluginstyles('all'), 626 $this->css_pluginPDFstyles(), 627 array( 628 DOKU_PLUGIN . 'dw2pdf/conf/style.css' 629 => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 630 DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 631 => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 632 DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 633 => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 634 ) 635 ); 636 $css = ''; 637 foreach($files as $file => $location) { 638 $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 639 $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 640 $css .= css_loadfile($file, $location); 641 } 642 643 if(function_exists('css_parseless')) { 644 // apply pattern replacements 645 $styleini = css_styleini($conf['template']); 646 $css = css_applystyle($css, $styleini['replacements']); 647 648 // parse less 649 $css = css_parseless($css); 650 } else { 651 // @deprecated 2013-12-19: fix backward compatibility 652 $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 653 } 654 655 return $css; 656 } 657 658 /** 659 * Returns a list of possible Plugin PDF Styles 660 * 661 * Checks for a pdf.css, falls back to print.css 662 * 663 * @author Andreas Gohr <andi@splitbrain.org> 664 */ 665 protected function css_pluginPDFstyles() { 666 $list = array(); 667 $plugins = plugin_list(); 668 669 $usestyle = explode(',', $this->getConf('usestyles')); 670 foreach($plugins as $p) { 671 if(in_array($p, $usestyle)) { 672 $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 673 $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 674 } 675 676 if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) { 677 $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 678 } else { 679 $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 680 } 681 } 682 return $list; 683 } 684 685 /** 686 * Returns array of pages which will be included in the exported pdf 687 * 688 * @return array 689 */ 690 public function getExportedPages() { 691 return $this->list; 692 } 693 694 /** 695 * usort callback to sort by file lastmodified time 696 * 697 * @param array $a 698 * @param array $b 699 * @return int 700 */ 701 public function _datesort($a, $b) { 702 if($b['rev'] < $a['rev']) return -1; 703 if($b['rev'] > $a['rev']) return 1; 704 return strcmp($b['id'], $a['id']); 705 } 706 707 /** 708 * usort callback to sort by page id 709 * @param array $a 710 * @param array $b 711 * @return int 712 */ 713 public function _pagenamesort($a, $b) { 714 if($a['id'] <= $b['id']) return -1; 715 if($a['id'] > $b['id']) return 1; 716 return 0; 717 } 718 719 /** 720 * Return settings read from: 721 * 1. url parameters 722 * 2. plugin config 723 * 3. global config 724 * 725 * @return array 726 */ 727 protected function loadExportConfig() { 728 global $INPUT; 729 global $conf; 730 731 $this->exportConfig = array(); 732 733 // decide on the paper setup from param or config 734 $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 735 $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 736 737 $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 738 $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 739 740 $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 741 $levels = array(); 742 if($hasToC) { 743 $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 744 list($top_input, $max_input) = explode('-', $toclevels, 2); 745 list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 746 $bounds_input = array( 747 'top' => array( 748 (int) $top_input, 749 (int) $top_conf 750 ), 751 'max' => array( 752 (int) $max_input, 753 (int) $max_conf 754 ) 755 ); 756 $bounds = array( 757 'top' => $conf['toptoclevel'], 758 'max' => $conf['maxtoclevel'] 759 760 ); 761 foreach($bounds_input as $bound => $values) { 762 foreach($values as $value) { 763 if($value > 0 && $value <= 5) { 764 //stop at valid value and store 765 $bounds[$bound] = $value; 766 break; 767 } 768 } 769 } 770 771 if($bounds['max'] < $bounds['top']) { 772 $bounds['max'] = $bounds['top']; 773 } 774 775 for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 776 $levels["H$level"] = $level - 1; 777 } 778 } 779 $this->exportConfig['hasToC'] = $hasToC; 780 $this->exportConfig['levels'] = $levels; 781 782 $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 783 784 $tplconf = $this->getConf('template'); 785 $tpl = $INPUT->str('tpl', $tplconf, true); 786 if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 787 $tpl = $tplconf; 788 } 789 if(!$tpl){ 790 $tpl = 'default'; 791 } 792 $this->exportConfig['template'] = $tpl; 793 794 $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 795 } 796 797 /** 798 * Returns requested config 799 * 800 * @param string $name 801 * @param mixed $notset 802 * @return mixed|bool 803 */ 804 public function getExportConfig($name, $notset = false) { 805 if ($this->exportConfig === null){ 806 $this->loadExportConfig(); 807 } 808 809 if(isset($this->exportConfig[$name])){ 810 return $this->exportConfig[$name]; 811 }else{ 812 return $notset; 813 } 814 } 815 816 /** 817 * Add 'export pdf'-button to pagetools 818 * 819 * @param Doku_Event $event 820 */ 821 public function addbutton(Doku_Event $event) { 822 global $ID, $REV; 823 824 if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 825 $params = array('do' => 'export_pdf'); 826 if($REV) { 827 $params['rev'] = $REV; 828 } 829 830 // insert button at position before last (up to top) 831 $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 832 array('export_pdf' => 833 '<li>' 834 . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 835 . '<span>' . $this->getLang('export_pdf_button') . '</span>' 836 . '</a>' 837 . '</li>' 838 ) + 839 array_slice($event->data['items'], -1, 1, true); 840 } 841 } 842} 843