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