xref: /plugin/dw2pdf/action.php (revision 9b071da5cb9ac78212200ae10d9f88834bb713d3)
1ee19bac3SLuigi Micco<?php
2ee19bac3SLuigi Micco/**
3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4ee19bac3SLuigi Micco *
5ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
75db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8ee19bac3SLuigi Micco */
9ee19bac3SLuigi Micco
10ee19bac3SLuigi Micco// must be run within Dokuwiki
11ee19bac3SLuigi Miccoif(!defined('DOKU_INC')) die();
12ee19bac3SLuigi Micco
130639157eSGerrit Uitslag/**
140639157eSGerrit Uitslag * Class action_plugin_dw2pdf
150639157eSGerrit Uitslag *
160639157eSGerrit Uitslag * Export hmtl content to pdf, for different url parameter configurations
170639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html.
180639157eSGerrit Uitslag */
191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
2002f9a447SGerrit Uitslag    /**
2102f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2202f9a447SGerrit Uitslag     *
2302f9a447SGerrit Uitslag     * @var array
2402f9a447SGerrit Uitslag     */
25213fdb75SGerrit Uitslag    protected $exportConfig = null;
2660e59de7SGerrit Uitslag    protected $tpl;
2760e59de7SGerrit Uitslag    protected $list = array();
281c14c879SAndreas Gohr
291c14c879SAndreas Gohr    /**
301c14c879SAndreas Gohr     * Constructor. Sets the correct template
311c14c879SAndreas Gohr     */
326be736bfSGerrit Uitslag    public function __construct() {
3302f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
341c14c879SAndreas Gohr    }
351c14c879SAndreas Gohr
36ee19bac3SLuigi Micco    /**
37ee19bac3SLuigi Micco     * Register the events
38177a7d30SGerrit Uitslag     *
39177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
40ee19bac3SLuigi Micco     */
416be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
42ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
436be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
44ee19bac3SLuigi Micco    }
45ee19bac3SLuigi Micco
461c14c879SAndreas Gohr    /**
471c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
48737417c6SKlap-in     *
49737417c6SKlap-in     * @param Doku_Event $event
50737417c6SKlap-in     * @return bool
511c14c879SAndreas Gohr     */
5244e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
53ee19bac3SLuigi Micco        global $ACT;
54ee19bac3SLuigi Micco        global $ID;
55ee19bac3SLuigi Micco
561ef68647SAndreas Gohr        // our event?
57ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false;
58ee19bac3SLuigi Micco
591ef68647SAndreas Gohr        // check user's rights
601ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
611ef68647SAndreas Gohr
62d63e7fe7SGerrit Uitslag        if($data = $this->collectExportPages($event)) {
63d63e7fe7SGerrit Uitslag            list($title, $this->list) = $data;
64d63e7fe7SGerrit Uitslag        } else {
65d63e7fe7SGerrit Uitslag            return false;
66d63e7fe7SGerrit Uitslag        }
67d63e7fe7SGerrit Uitslag
68d63e7fe7SGerrit Uitslag        // it's ours, no one else's
69d63e7fe7SGerrit Uitslag        $event->preventDefault();
70d63e7fe7SGerrit Uitslag
71a58f45f0SGerrit Uitslag        // prepare cache and its dependencies
72a58f45f0SGerrit Uitslag        $depends = array();
73a58f45f0SGerrit Uitslag        $cache = $this->prepareCache($title, $depends);
74d63e7fe7SGerrit Uitslag
75d63e7fe7SGerrit Uitslag        // hard work only when no cache available
76d63e7fe7SGerrit Uitslag        if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
77d63e7fe7SGerrit Uitslag            $this->generatePDF($cache->cache, $title);
78d63e7fe7SGerrit Uitslag        }
79d63e7fe7SGerrit Uitslag
80d63e7fe7SGerrit Uitslag        // deliver the file
81d63e7fe7SGerrit Uitslag        $this->sendPDFFile($cache->cache, $title);
82d63e7fe7SGerrit Uitslag        return true;
83d63e7fe7SGerrit Uitslag    }
84d63e7fe7SGerrit Uitslag
85d63e7fe7SGerrit Uitslag
86d63e7fe7SGerrit Uitslag    /**
87d63e7fe7SGerrit Uitslag     * Obtain list of pages and title, based on url parameters
88d63e7fe7SGerrit Uitslag     *
89d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
90d63e7fe7SGerrit Uitslag     * @return string|bool
91d63e7fe7SGerrit Uitslag     */
92d63e7fe7SGerrit Uitslag    protected function collectExportPages(Doku_Event $event) {
93d63e7fe7SGerrit Uitslag        global $ACT;
94d63e7fe7SGerrit Uitslag        global $ID;
95d63e7fe7SGerrit Uitslag        global $INPUT;
96d63e7fe7SGerrit Uitslag        global $conf;
97d63e7fe7SGerrit Uitslag
98d63e7fe7SGerrit Uitslag        // list of one or multiple pages
99d63e7fe7SGerrit Uitslag        $list = array();
10028e636eaSGerrit Uitslag
10187c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
102d63e7fe7SGerrit Uitslag            $list[0] = $ID;
103177a7d30SGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
104177a7d30SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
105177a7d30SGerrit Uitslag            if(empty($title)) {
106737417c6SKlap-in                $title = p_get_first_heading($ID);
10715923cb9SGerrit Uitslag            }
108ad18f4e1SGerrit Uitslag
109ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
110ad18f4e1SGerrit Uitslag            //check input for title and ns
111177a7d30SGerrit Uitslag            if(!$title = $INPUT->str('book_title')) {
11226be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
113ad18f4e1SGerrit Uitslag                return false;
114ad18f4e1SGerrit Uitslag            }
115177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
116ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
11726be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
118ad18f4e1SGerrit Uitslag                return false;
119ad18f4e1SGerrit Uitslag            }
120ad18f4e1SGerrit Uitslag
12126be4eceSGerrit Uitslag            //sort order
122177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
123ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
124ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
125ad18f4e1SGerrit Uitslag                $order = 'natural';
126ad18f4e1SGerrit Uitslag            }
127ad18f4e1SGerrit Uitslag
12826be4eceSGerrit Uitslag            //search depth
129177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
130ad18f4e1SGerrit Uitslag            if($depth < 0) {
131ad18f4e1SGerrit Uitslag                $depth = 0;
132ad18f4e1SGerrit Uitslag            }
13326be4eceSGerrit Uitslag
134ad18f4e1SGerrit Uitslag            //page search
135ad18f4e1SGerrit Uitslag            $result = array();
136ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
137ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
138ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
139ad18f4e1SGerrit Uitslag
14026be4eceSGerrit Uitslag            //sorting
141ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
142ad18f4e1SGerrit Uitslag                if($order == 'date') {
143ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
144ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
145ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
146ad18f4e1SGerrit Uitslag                }
147ad18f4e1SGerrit Uitslag            }
148ad18f4e1SGerrit Uitslag
149ad18f4e1SGerrit Uitslag            foreach($result as $item) {
150d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
151ad18f4e1SGerrit Uitslag            }
152ad18f4e1SGerrit Uitslag
153baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
154baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
155baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
156baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
157baa31dc5SGerrit Uitslag                    }
158baa31dc5SGerrit Uitslag                }
159baa31dc5SGerrit Uitslag            }
160baa31dc5SGerrit Uitslag
161737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
16226be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
163177a7d30SGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
164177a7d30SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
165177a7d30SGerrit Uitslag            if(empty($title)) {
16626be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
167737417c6SKlap-in                return false;
16826be4eceSGerrit Uitslag            } else {
169d63e7fe7SGerrit Uitslag                $list = explode("|", $_COOKIE['list-pagelist']);
17026be4eceSGerrit Uitslag            }
171ad18f4e1SGerrit Uitslag
172737417c6SKlap-in        } else {
17326be4eceSGerrit Uitslag            //show empty bookcreator message
17426be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
175737417c6SKlap-in            return false;
176737417c6SKlap-in        }
177737417c6SKlap-in
178719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
179d63e7fe7SGerrit Uitslag        return array($title, $list);
180d63e7fe7SGerrit Uitslag    }
181d63e7fe7SGerrit Uitslag
182a58f45f0SGerrit Uitslag    /**
183a58f45f0SGerrit Uitslag     * Prepare cache
184a58f45f0SGerrit Uitslag     *
185a58f45f0SGerrit Uitslag     * @param string $title
186a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
187a58f45f0SGerrit Uitslag     * @return cache
188a58f45f0SGerrit Uitslag     */
189a58f45f0SGerrit Uitslag    protected function prepareCache($title, &$depends) {
190a58f45f0SGerrit Uitslag        global $REV;
191a58f45f0SGerrit Uitslag
192ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
193ee19bac3SLuigi Micco            . $REV
194ee19bac3SLuigi Micco            . $this->getExportConfig('template')
195ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
196ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
197ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
198ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
199ee19bac3SLuigi Micco            . $title;
200ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
201ee19bac3SLuigi Micco
202ee19bac3SLuigi Micco        $dependencies = array();
203ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
204ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
205ee19bac3SLuigi Micco
206ee19bac3SLuigi Micco            if(is_array($relations)) {
207ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
208ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
209ee19bac3SLuigi Micco                        if($exists) {
210ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
211ee19bac3SLuigi Micco                        }
212ee19bac3SLuigi Micco                    }
213ee19bac3SLuigi Micco                }
214ee19bac3SLuigi Micco
215ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
216ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
217ee19bac3SLuigi Micco                        if($exists) {
218ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
219ee19bac3SLuigi Micco                        }
220ee19bac3SLuigi Micco                    }
221ee19bac3SLuigi Micco                }
222ee19bac3SLuigi Micco            }
223ee19bac3SLuigi Micco
224ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
225ee19bac3SLuigi Micco        }
226ee19bac3SLuigi Micco
227ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
228ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
229ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
230ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
231ee19bac3SLuigi Micco        $depends['files'] = array_merge(
232ee19bac3SLuigi Micco            $depends['files'],
233ee19bac3SLuigi Micco            $dependencies,
234ee19bac3SLuigi Micco            getConfigFiles('main')
235ee19bac3SLuigi Micco        );
236a58f45f0SGerrit Uitslag        return $cache;
237ee19bac3SLuigi Micco    }
238ee19bac3SLuigi Micco
239d63e7fe7SGerrit Uitslag    /**
240d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
241d63e7fe7SGerrit Uitslag     *
242d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
243d63e7fe7SGerrit Uitslag     * @param string     $msglangkey key of translation key
244d63e7fe7SGerrit Uitslag     */
245d63e7fe7SGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey) {
246d63e7fe7SGerrit Uitslag        msg($this->getLang($msglangkey), -1);
247d63e7fe7SGerrit Uitslag
248d63e7fe7SGerrit Uitslag        $event->data = 'show';
249d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
250d63e7fe7SGerrit Uitslag    }
251d63e7fe7SGerrit Uitslag
252d63e7fe7SGerrit Uitslag    /**
253d63e7fe7SGerrit Uitslag     * Build a pdf from the html
254d63e7fe7SGerrit Uitslag     *
255d63e7fe7SGerrit Uitslag     * @param string $cachefile
256d63e7fe7SGerrit Uitslag     * @param string $title
257d63e7fe7SGerrit Uitslag     */
258d63e7fe7SGerrit Uitslag    protected function generatePDF($cachefile, $title) {
259d63e7fe7SGerrit Uitslag        global $ID;
260d63e7fe7SGerrit Uitslag        global $REV;
261d63e7fe7SGerrit Uitslag        global $INPUT;
26287c86ddaSAndreas Gohr
26302f9a447SGerrit Uitslag        //some shortcuts to export settings
26402f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
26502f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
26602f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
2676ea88a05SAndreas Gohr
2681ef68647SAndreas Gohr        // initialize PDF library
269cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
2706ea88a05SAndreas Gohr
27102f9a447SGerrit Uitslag        $mpdf = new DokuPDF($this->getExportConfig('pagesize'), $this->getExportConfig('orientation'));
272ee19bac3SLuigi Micco
273d62df65bSAndreas Gohr        // let mpdf fix local links
274d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
275d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
27602f9a447SGerrit Uitslag        if($self['port']) {
27702f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
27802f9a447SGerrit Uitslag        }
279d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
280d62df65bSAndreas Gohr
28156d13144SAndreas Gohr        // Set the title
28256d13144SAndreas Gohr        $mpdf->SetTitle($title);
28356d13144SAndreas Gohr
284d63e7fe7SGerrit Uitslag        // some default document settings
285d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
286213fdb75SGerrit Uitslag        //      single-side document has only odd pages
287213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
288daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
289daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
29002f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
29102f9a447SGerrit Uitslag        if($hasToC) {
29202f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
29302f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
29402f9a447SGerrit Uitslag        } else {
29502f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
29602f9a447SGerrit Uitslag        }
2972eedf77dSAndreas Gohr
29856d13144SAndreas Gohr        // load the template
2991c14c879SAndreas Gohr        $template = $this->load_template($title);
300ee19bac3SLuigi Micco
3011ef68647SAndreas Gohr        // prepare HTML header styles
302a2c33768SGerrit Uitslag        $html = '';
30302f9a447SGerrit Uitslag        if($isDebug) {
304a2c33768SGerrit Uitslag            $html .= '<html><head>';
305737417c6SKlap-in            $html .= '<style type="text/css">';
306a2c33768SGerrit Uitslag        }
307a2c33768SGerrit Uitslag        $styles = $this->load_css();
308a2c33768SGerrit Uitslag        $styles .= '@page { size:auto; ' . $template['page'] . '}';
309a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
310254467c4SGerrit Uitslag
311254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
312254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
313254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
314254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
315254467c4SGerrit Uitslag
316a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
317a2c33768SGerrit Uitslag
31802f9a447SGerrit Uitslag        if($isDebug) {
319a2c33768SGerrit Uitslag            $html .= $styles;
3201ef68647SAndreas Gohr            $html .= '</style>';
3211ef68647SAndreas Gohr            $html .= '</head><body>';
322a2c33768SGerrit Uitslag        }
323a2c33768SGerrit Uitslag
324a2c33768SGerrit Uitslag        $body_start = $template['html'];
325a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3262eedf77dSAndreas Gohr
3271e45476bSmnapp        // insert the cover page
328a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
329a2c33768SGerrit Uitslag
330a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
33102f9a447SGerrit Uitslag        if($isDebug) {
332a2c33768SGerrit Uitslag            $html .= $body_start;
333a2c33768SGerrit Uitslag        }
33402f9a447SGerrit Uitslag        if($hasToC) {
33502f9a447SGerrit Uitslag            //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
33602f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
33702f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
33802f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
33902f9a447SGerrit Uitslag                array(
340230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
341230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
34202f9a447SGerrit Uitslag                    'links' => true,
34302f9a447SGerrit Uitslag                    'outdent' => '1em',
34402f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
34502f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
34602f9a447SGerrit Uitslag                )
34702f9a447SGerrit Uitslag            );
34802f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
34902f9a447SGerrit Uitslag        }
35002f9a447SGerrit Uitslag
351c00eb13bSGerrit Uitslag        // store original pageid
352c00eb13bSGerrit Uitslag        $keep = $ID;
353c00eb13bSGerrit Uitslag
3541ef68647SAndreas Gohr        // loop over all pages
355a2c33768SGerrit Uitslag        $cnt = count($this->list);
3561ef68647SAndreas Gohr        for($n = 0; $n < $cnt; $n++) {
357a2c33768SGerrit Uitslag            $page = $this->list[$n];
358ee19bac3SLuigi Micco
359c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
360c00eb13bSGerrit Uitslag            $ID = $page;
361c00eb13bSGerrit Uitslag
362a2c33768SGerrit Uitslag            $pagehtml = p_cached_output(wikiFN($page, $REV), 'dw2pdf', $page);
363719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
3641ef68647SAndreas Gohr            if($n < ($cnt - 1)) {
365a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
366a2c33768SGerrit Uitslag            }
367a2c33768SGerrit Uitslag
368a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
36902f9a447SGerrit Uitslag            if($isDebug) {
370a2c33768SGerrit Uitslag                $html .= $pagehtml;
3711ef68647SAndreas Gohr            }
372ee19bac3SLuigi Micco        }
373c00eb13bSGerrit Uitslag        //restore ID
374c00eb13bSGerrit Uitslag        $ID = $keep;
375ee19bac3SLuigi Micco
37633c15297SGerrit Uitslag        // insert the back page
377a2c33768SGerrit Uitslag        $body_end = $template['back'];
37833c15297SGerrit Uitslag
379a2c33768SGerrit Uitslag        $body_end .= '</div>';
380a2c33768SGerrit Uitslag
381d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
38202f9a447SGerrit Uitslag        if($isDebug) {
383a2c33768SGerrit Uitslag            $html .= $body_end;
384eeb17e15SAndreas Gohr            $html .= '</body>';
385eeb17e15SAndreas Gohr            $html .= '</html>';
386a2c33768SGerrit Uitslag        }
387f765508eSGerrit Uitslag
388f765508eSGerrit Uitslag        //Return html for debugging
38902f9a447SGerrit Uitslag        if($isDebug) {
39002f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
39126be4eceSGerrit Uitslag                echo $html;
392a2c33768SGerrit Uitslag            } else {
393a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
394a2c33768SGerrit Uitslag                echo $html;
395a2c33768SGerrit Uitslag            }
39626be4eceSGerrit Uitslag            exit();
39726be4eceSGerrit Uitslag        };
398f765508eSGerrit Uitslag
39987c86ddaSAndreas Gohr        // write to cache file
400d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
40187c86ddaSAndreas Gohr    }
40287c86ddaSAndreas Gohr
403d63e7fe7SGerrit Uitslag    /**
404d63e7fe7SGerrit Uitslag     * @param string $cachefile
405d63e7fe7SGerrit Uitslag     * @param string $title
406d63e7fe7SGerrit Uitslag     */
407d63e7fe7SGerrit Uitslag    protected function sendPDFFile($cachefile, $title) {
40887c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
409b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
41087c86ddaSAndreas Gohr        header('Pragma: public');
411d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
41287c86ddaSAndreas Gohr
4139a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
41487c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4159a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
41687c86ddaSAndreas Gohr        } else {
4179a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
41887c86ddaSAndreas Gohr        }
419ee19bac3SLuigi Micco
420e993da11SGerrit Uitslag        //try to send file, and exit if done
421d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
42287c86ddaSAndreas Gohr
423d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
42487c86ddaSAndreas Gohr        if($fp) {
425d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
42687c86ddaSAndreas Gohr        } else {
42787c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
42887c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
42987c86ddaSAndreas Gohr        }
4301ef68647SAndreas Gohr        exit();
4311ef68647SAndreas Gohr    }
4321ef68647SAndreas Gohr
4336be736bfSGerrit Uitslag    /**
4342eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
43544e8e8fbSGerrit Uitslag     *
43644e8e8fbSGerrit Uitslag     * @param string $title
43744e8e8fbSGerrit Uitslag     * @return array
4381ef68647SAndreas Gohr     */
4391c14c879SAndreas Gohr    protected function load_template($title) {
4401ef68647SAndreas Gohr        global $ID;
4411ef68647SAndreas Gohr        global $conf;
4421ef68647SAndreas Gohr
4432eedf77dSAndreas Gohr        // this is what we'll return
4442eedf77dSAndreas Gohr        $output = array(
4451e45476bSmnapp            'cover' => '',
4462eedf77dSAndreas Gohr            'html'  => '',
4472eedf77dSAndreas Gohr            'page'  => '',
4482eedf77dSAndreas Gohr            'first' => '',
4492eedf77dSAndreas Gohr            'cite'  => '',
4502eedf77dSAndreas Gohr        );
4512eedf77dSAndreas Gohr
4522eedf77dSAndreas Gohr        // prepare header/footer elements
4532eedf77dSAndreas Gohr        $html = '';
45433c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
45533c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
45602f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
45733c15297SGerrit Uitslag                if(file_exists($file)) {
45833c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
45933c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
46033c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
4612eedf77dSAndreas Gohr
4622eedf77dSAndreas Gohr                    // register the needed pseudo CSS
46333c15297SGerrit Uitslag                    if($order == '_first') {
46433c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
46533c15297SGerrit Uitslag                    } elseif($order == '_even') {
46633c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
46733c15297SGerrit Uitslag                    } elseif($order == '_odd') {
46833c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
469daa70883SAndreas Gohr                    } else {
47033c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
4712eedf77dSAndreas Gohr                    }
4722eedf77dSAndreas Gohr                }
4732eedf77dSAndreas Gohr            }
4742eedf77dSAndreas Gohr        }
4752eedf77dSAndreas Gohr
4761ef68647SAndreas Gohr        // prepare replacements
4771ef68647SAndreas Gohr        $replace = array(
4781ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
47902f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
4802eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
4811ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
4821ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
4831ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
4845d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
48502f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
4861ef68647SAndreas Gohr        );
4871ef68647SAndreas Gohr
4882eedf77dSAndreas Gohr        // set HTML element
489a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
490a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
491a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
4921ef68647SAndreas Gohr
4931e45476bSmnapp        // cover page
49402f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
49533c15297SGerrit Uitslag        if(file_exists($coverfile)) {
49633c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
4971e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
498*9b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
4996e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5001e45476bSmnapp        }
5011e45476bSmnapp
50233c15297SGerrit Uitslag        // cover page
50302f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
50433c15297SGerrit Uitslag        if(file_exists($backfile)) {
50533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
50633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
50733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
508*9b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
50933c15297SGerrit Uitslag        }
51033c15297SGerrit Uitslag
5112eedf77dSAndreas Gohr        // citation box
51202f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
51333c15297SGerrit Uitslag        if(file_exists($citationfile)) {
51433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5152eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5162eedf77dSAndreas Gohr        }
5171ef68647SAndreas Gohr
5182eedf77dSAndreas Gohr        return $output;
5191ef68647SAndreas Gohr    }
5201ef68647SAndreas Gohr
5211ef68647SAndreas Gohr    /**
522a180c973SKlap-in     * @param string $raw code with placeholders
523a180c973SKlap-in     * @param string $id  pageid
524a180c973SKlap-in     * @return string
525a180c973SKlap-in     */
526a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
527a180c973SKlap-in        global $REV;
528a180c973SKlap-in
529a180c973SKlap-in        // generate qr code for this page using google infographics api
530a180c973SKlap-in        $qr_code = '';
531a180c973SKlap-in        if($this->getConf('qrcodesize')) {
532a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
533a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
534a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
535a180c973SKlap-in        }
536a180c973SKlap-in        // prepare replacements
537a180c973SKlap-in        $replace['@ID@']      = $id;
538a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
539a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
540a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
541a180c973SKlap-in
542e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
543e3d68265SGerrit Uitslag
544e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
545e3d68265SGerrit Uitslag        $content = preg_replace_callback(
546e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
547e3d68265SGerrit Uitslag            array($this, 'replacedate'),
548e3d68265SGerrit Uitslag            $content
549e3d68265SGerrit Uitslag        );
550e3d68265SGerrit Uitslag
551e3d68265SGerrit Uitslag        return $content;
552a180c973SKlap-in    }
553a180c973SKlap-in
554e3d68265SGerrit Uitslag
555e3d68265SGerrit Uitslag    /**
556e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
557e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
558e3d68265SGerrit Uitslag     *
559e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
560e3d68265SGerrit Uitslag     * @return string
561e3d68265SGerrit Uitslag     */
562e3d68265SGerrit Uitslag    function replacedate($match) {
563e3d68265SGerrit Uitslag        global $conf;
564e3d68265SGerrit Uitslag        //no 2nd argument for default date format
565e3d68265SGerrit Uitslag        if($match[2] == null) {
566e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
567e3d68265SGerrit Uitslag        }
568e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
569e3d68265SGerrit Uitslag    }
570e3d68265SGerrit Uitslag
571e3d68265SGerrit Uitslag
572a180c973SKlap-in    /**
5731c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
5741ef68647SAndreas Gohr     */
5751c14c879SAndreas Gohr    protected function load_css() {
576737417c6SKlap-in        global $conf;
5771c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
5781c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
5791c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
580ee19bac3SLuigi Micco
5811c14c879SAndreas Gohr        // prepare CSS files
5821c14c879SAndreas Gohr        $files = array_merge(
5831c14c879SAndreas Gohr            array(
5841c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
5851c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
5861c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
5871c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
5881c14c879SAndreas Gohr            ),
5891c14c879SAndreas Gohr            css_pluginstyles('all'),
59058e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
5911c14c879SAndreas Gohr            array(
5921c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
5931c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
5941c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
5951c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
5961c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
5971c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
5981c14c879SAndreas Gohr            )
5991c14c879SAndreas Gohr        );
6001c14c879SAndreas Gohr        $css = '';
6011c14c879SAndreas Gohr        foreach($files as $file => $location) {
60228e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
60328e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6041c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6051ef68647SAndreas Gohr        }
6061ef68647SAndreas Gohr
60728e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6081c14c879SAndreas Gohr            // apply pattern replacements
60928e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
61028e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
61128e636eaSGerrit Uitslag
61228e636eaSGerrit Uitslag            // parse less
61328e636eaSGerrit Uitslag            $css = css_parseless($css);
61428e636eaSGerrit Uitslag        } else {
61528e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6161c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
61728e636eaSGerrit Uitslag        }
6181ef68647SAndreas Gohr
6191c14c879SAndreas Gohr        return $css;
620ee19bac3SLuigi Micco    }
6211c14c879SAndreas Gohr
62258e6409eSAndreas Gohr    /**
62358e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
62458e6409eSAndreas Gohr     *
62558e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
62658e6409eSAndreas Gohr     *
62758e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
62858e6409eSAndreas Gohr     */
6296be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
63058e6409eSAndreas Gohr        $list = array();
63158e6409eSAndreas Gohr        $plugins = plugin_list();
632f54b51f7SAndreas Gohr
633f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
63458e6409eSAndreas Gohr        foreach($plugins as $p) {
635f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
636f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
637f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
638f54b51f7SAndreas Gohr            }
639f54b51f7SAndreas Gohr
64058e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
64158e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
64258e6409eSAndreas Gohr            } else {
64358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
64458e6409eSAndreas Gohr            }
64558e6409eSAndreas Gohr        }
64658e6409eSAndreas Gohr        return $list;
64758e6409eSAndreas Gohr    }
648ad18f4e1SGerrit Uitslag
649ad18f4e1SGerrit Uitslag    /**
65060e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
65160e59de7SGerrit Uitslag     *
65260e59de7SGerrit Uitslag     * @return array
65360e59de7SGerrit Uitslag     */
65460e59de7SGerrit Uitslag    public function getExportedPages() {
65560e59de7SGerrit Uitslag        return $this->list;
65660e59de7SGerrit Uitslag    }
65760e59de7SGerrit Uitslag
65860e59de7SGerrit Uitslag    /**
659ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
66044e8e8fbSGerrit Uitslag     *
66144e8e8fbSGerrit Uitslag     * @param array $a
66244e8e8fbSGerrit Uitslag     * @param array $b
66344e8e8fbSGerrit Uitslag     * @return int
664ad18f4e1SGerrit Uitslag     */
665ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
666ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
667ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
668ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
669ad18f4e1SGerrit Uitslag    }
670ad18f4e1SGerrit Uitslag
671ad18f4e1SGerrit Uitslag    /**
672ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
67344e8e8fbSGerrit Uitslag     * @param array $a
67444e8e8fbSGerrit Uitslag     * @param array $b
67544e8e8fbSGerrit Uitslag     * @return int
676ad18f4e1SGerrit Uitslag     */
677ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
678ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
679ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
680ad18f4e1SGerrit Uitslag        return 0;
681ad18f4e1SGerrit Uitslag    }
68226be4eceSGerrit Uitslag
68326be4eceSGerrit Uitslag    /**
68402f9a447SGerrit Uitslag     * Return settings read from:
68502f9a447SGerrit Uitslag     *   1. url parameters
68602f9a447SGerrit Uitslag     *   2. plugin config
68702f9a447SGerrit Uitslag     *   3. global config
68802f9a447SGerrit Uitslag     *
68902f9a447SGerrit Uitslag     * @return array
69002f9a447SGerrit Uitslag     */
69102f9a447SGerrit Uitslag    protected function loadExportConfig() {
69202f9a447SGerrit Uitslag        global $INPUT;
69302f9a447SGerrit Uitslag        global $conf;
69402f9a447SGerrit Uitslag
69502f9a447SGerrit Uitslag        $this->exportConfig = array();
69602f9a447SGerrit Uitslag
69702f9a447SGerrit Uitslag        // decide on the paper setup from param or config
69802f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
69902f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
70002f9a447SGerrit Uitslag
701213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
702213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
703213fdb75SGerrit Uitslag
704213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
70502f9a447SGerrit Uitslag        $levels = array();
70602f9a447SGerrit Uitslag        if($hasToC) {
70702f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
70802f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
70902f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
71002f9a447SGerrit Uitslag            $bounds_input = array(
71102f9a447SGerrit Uitslag                'top' => array(
71202f9a447SGerrit Uitslag                    (int) $top_input,
71302f9a447SGerrit Uitslag                    (int) $top_conf
71402f9a447SGerrit Uitslag                ),
71502f9a447SGerrit Uitslag                'max' => array(
71602f9a447SGerrit Uitslag                    (int) $max_input,
71702f9a447SGerrit Uitslag                    (int) $max_conf
71802f9a447SGerrit Uitslag                )
71902f9a447SGerrit Uitslag            );
72002f9a447SGerrit Uitslag            $bounds = array(
72102f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
72202f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
72302f9a447SGerrit Uitslag
72402f9a447SGerrit Uitslag            );
72502f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
72602f9a447SGerrit Uitslag                foreach($values as $value) {
72702f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
72802f9a447SGerrit Uitslag                        //stop at valid value and store
72902f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
73002f9a447SGerrit Uitslag                        break;
73102f9a447SGerrit Uitslag                    }
73202f9a447SGerrit Uitslag                }
73302f9a447SGerrit Uitslag            }
73402f9a447SGerrit Uitslag
73502f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
73602f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
73702f9a447SGerrit Uitslag            }
73802f9a447SGerrit Uitslag
73902f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
74002f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
74102f9a447SGerrit Uitslag            }
74202f9a447SGerrit Uitslag        }
74302f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
74402f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
74502f9a447SGerrit Uitslag
74602f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
74702f9a447SGerrit Uitslag
74802f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
74905d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
75002f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
75102f9a447SGerrit Uitslag            $tpl = $tplconf;
75202f9a447SGerrit Uitslag        }
75302f9a447SGerrit Uitslag        if(!$tpl){
75402f9a447SGerrit Uitslag            $tpl = 'default';
75502f9a447SGerrit Uitslag        }
75602f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
75702f9a447SGerrit Uitslag
75802f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
75902f9a447SGerrit Uitslag    }
76002f9a447SGerrit Uitslag
76102f9a447SGerrit Uitslag    /**
76202f9a447SGerrit Uitslag     * Returns requested config
76302f9a447SGerrit Uitslag     *
76402f9a447SGerrit Uitslag     * @param string $name
76502f9a447SGerrit Uitslag     * @param mixed  $notset
76602f9a447SGerrit Uitslag     * @return mixed|bool
76702f9a447SGerrit Uitslag     */
76802f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
76902f9a447SGerrit Uitslag        if ($this->exportConfig === null){
77002f9a447SGerrit Uitslag            $this->loadExportConfig();
77102f9a447SGerrit Uitslag        }
77202f9a447SGerrit Uitslag
77302f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
77402f9a447SGerrit Uitslag            return $this->exportConfig[$name];
77502f9a447SGerrit Uitslag        }else{
77602f9a447SGerrit Uitslag            return $notset;
77702f9a447SGerrit Uitslag        }
77802f9a447SGerrit Uitslag    }
779d63e7fe7SGerrit Uitslag
780d63e7fe7SGerrit Uitslag    /**
781d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
782d63e7fe7SGerrit Uitslag     *
783d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
784d63e7fe7SGerrit Uitslag     */
78544e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
786d63e7fe7SGerrit Uitslag        global $ID, $REV;
787d63e7fe7SGerrit Uitslag
788d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
789d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
790d63e7fe7SGerrit Uitslag            if($REV) {
791d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
792d63e7fe7SGerrit Uitslag            }
793d63e7fe7SGerrit Uitslag
794d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
795d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
796d63e7fe7SGerrit Uitslag                array('export_pdf' =>
797d63e7fe7SGerrit Uitslag                          '<li>'
79872cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
799d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
800d63e7fe7SGerrit Uitslag                          . '</a>'
801d63e7fe7SGerrit Uitslag                          . '</li>'
802d63e7fe7SGerrit Uitslag                ) +
803d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
804d63e7fe7SGerrit Uitslag        }
805d63e7fe7SGerrit Uitslag    }
806ee19bac3SLuigi Micco}
807