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