xref: /plugin/dw2pdf/action.php (revision bd9771889f8d3889fb6a97057300178bef235410)
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
75*bd977188SGerrit Uitslag        // hard work only when no cache available or needed for debugging
769bcbb4f0SGerrit Uitslag        if(!$this->getConf('usecache') || $this->getExportConfig('isDebug') || !$cache->useCache($depends)) {
77e5f6c2cbSMichael Große            // generating the pdf may take a long time for larger wikis / namespaces with many pages
78e5f6c2cbSMichael Große            set_time_limit(0);
79e5f6c2cbSMichael Große
80d63e7fe7SGerrit Uitslag            $this->generatePDF($cache->cache, $title);
81d63e7fe7SGerrit Uitslag        }
82d63e7fe7SGerrit Uitslag
83d63e7fe7SGerrit Uitslag        // deliver the file
84d63e7fe7SGerrit Uitslag        $this->sendPDFFile($cache->cache, $title);
85d63e7fe7SGerrit Uitslag        return true;
86d63e7fe7SGerrit Uitslag    }
87d63e7fe7SGerrit Uitslag
88d63e7fe7SGerrit Uitslag
89d63e7fe7SGerrit Uitslag    /**
90d63e7fe7SGerrit Uitslag     * Obtain list of pages and title, based on url parameters
91d63e7fe7SGerrit Uitslag     *
92d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
93d63e7fe7SGerrit Uitslag     * @return string|bool
94d63e7fe7SGerrit Uitslag     */
95d63e7fe7SGerrit Uitslag    protected function collectExportPages(Doku_Event $event) {
96d63e7fe7SGerrit Uitslag        global $ACT;
97d63e7fe7SGerrit Uitslag        global $ID;
98d63e7fe7SGerrit Uitslag        global $INPUT;
99d63e7fe7SGerrit Uitslag        global $conf;
100d63e7fe7SGerrit Uitslag
101d63e7fe7SGerrit Uitslag        // list of one or multiple pages
102d63e7fe7SGerrit Uitslag        $list = array();
10328e636eaSGerrit Uitslag
10487c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
105d63e7fe7SGerrit Uitslag            $list[0] = $ID;
106177a7d30SGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
107177a7d30SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
108177a7d30SGerrit Uitslag            if(empty($title)) {
109737417c6SKlap-in                $title = p_get_first_heading($ID);
11015923cb9SGerrit Uitslag            }
111ad18f4e1SGerrit Uitslag
112ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
113ad18f4e1SGerrit Uitslag            //check input for title and ns
114177a7d30SGerrit Uitslag            if(!$title = $INPUT->str('book_title')) {
11526be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
116ad18f4e1SGerrit Uitslag                return false;
117ad18f4e1SGerrit Uitslag            }
118177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
119ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
12026be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
121ad18f4e1SGerrit Uitslag                return false;
122ad18f4e1SGerrit Uitslag            }
123ad18f4e1SGerrit Uitslag
12426be4eceSGerrit Uitslag            //sort order
125177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
126ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
127ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
128ad18f4e1SGerrit Uitslag                $order = 'natural';
129ad18f4e1SGerrit Uitslag            }
130ad18f4e1SGerrit Uitslag
13126be4eceSGerrit Uitslag            //search depth
132177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
133ad18f4e1SGerrit Uitslag            if($depth < 0) {
134ad18f4e1SGerrit Uitslag                $depth = 0;
135ad18f4e1SGerrit Uitslag            }
13626be4eceSGerrit Uitslag
137ad18f4e1SGerrit Uitslag            //page search
138ad18f4e1SGerrit Uitslag            $result = array();
139ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
140ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
141ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
142ad18f4e1SGerrit Uitslag
14326be4eceSGerrit Uitslag            //sorting
144ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
145ad18f4e1SGerrit Uitslag                if($order == 'date') {
146ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
147ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
148ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
149ad18f4e1SGerrit Uitslag                }
150ad18f4e1SGerrit Uitslag            }
151ad18f4e1SGerrit Uitslag
152ad18f4e1SGerrit Uitslag            foreach($result as $item) {
153d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
154ad18f4e1SGerrit Uitslag            }
155ad18f4e1SGerrit Uitslag
156baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
157baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
158baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
159baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
160baa31dc5SGerrit Uitslag                    }
161baa31dc5SGerrit Uitslag                }
162baa31dc5SGerrit Uitslag            }
163baa31dc5SGerrit Uitslag
164737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
165b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator*/
16626be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
167177a7d30SGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
168177a7d30SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
169177a7d30SGerrit Uitslag            if(empty($title)) {
17026be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
171737417c6SKlap-in                return false;
17226be4eceSGerrit Uitslag            } else {
173d63e7fe7SGerrit Uitslag                $list = explode("|", $_COOKIE['list-pagelist']);
17426be4eceSGerrit Uitslag            }
175ad18f4e1SGerrit Uitslag
176b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
177b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
178b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
179b3eed6e3SGerrit Uitslag//                http_status(403);
180b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
181b3eed6e3SGerrit Uitslag//                exit();
182b3eed6e3SGerrit Uitslag//            }
183b3eed6e3SGerrit Uitslag
184b3eed6e3SGerrit Uitslag            $json = new JSON(JSON_LOOSE_TYPE);
185b3eed6e3SGerrit Uitslag            $list = $json->decode($INPUT->post->str('selection', '', true));
186b3eed6e3SGerrit Uitslag            if(!is_array($list) || empty($list)) {
187b3eed6e3SGerrit Uitslag                http_status(400);
188b3eed6e3SGerrit Uitslag                print $this->getLang('empty');
189b3eed6e3SGerrit Uitslag                exit();
190b3eed6e3SGerrit Uitslag            }
191b3eed6e3SGerrit Uitslag
192b3eed6e3SGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
193b3eed6e3SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
194b3eed6e3SGerrit Uitslag            if(empty($title)) {
195b3eed6e3SGerrit Uitslag                http_status(400);
196b3eed6e3SGerrit Uitslag                print $this->getLang('needtitle');
197b3eed6e3SGerrit Uitslag                exit();
198b3eed6e3SGerrit Uitslag            }
199b3eed6e3SGerrit Uitslag
200737417c6SKlap-in        } else {
20126be4eceSGerrit Uitslag            //show empty bookcreator message
20226be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
203737417c6SKlap-in            return false;
204737417c6SKlap-in        }
205737417c6SKlap-in
206719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
207c7138b3fSGerrit Uitslag
208c7138b3fSGerrit Uitslag        $skippedpages = array();
209c7138b3fSGerrit Uitslag        foreach($list as $index => $pageid) {
210c7138b3fSGerrit Uitslag            if(auth_quickaclcheck($pageid) < AUTH_READ) {
211c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
212c7138b3fSGerrit Uitslag                unset($list[$index]);
213c7138b3fSGerrit Uitslag            }
214c7138b3fSGerrit Uitslag        }
215c7138b3fSGerrit Uitslag        $list = array_filter($list); //removes also pages mentioned '0'
216c7138b3fSGerrit Uitslag
217c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
218c7138b3fSGerrit Uitslag        if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) {
219c7138b3fSGerrit Uitslag            $msg = hsc(join(', ', $skippedpages));
220c7138b3fSGerrit Uitslag            if($INPUT->has('selection')) {
221c7138b3fSGerrit Uitslag                http_status(400);
222c7138b3fSGerrit Uitslag                print sprintf($this->getLang('forbidden'), $msg);
223c7138b3fSGerrit Uitslag                exit();
224c7138b3fSGerrit Uitslag            } else {
225c7138b3fSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'forbidden', $msg);
226c7138b3fSGerrit Uitslag                return false;
227c7138b3fSGerrit Uitslag            }
228c7138b3fSGerrit Uitslag
229c7138b3fSGerrit Uitslag        }
230c7138b3fSGerrit Uitslag
231d63e7fe7SGerrit Uitslag        return array($title, $list);
232d63e7fe7SGerrit Uitslag    }
233d63e7fe7SGerrit Uitslag
234a58f45f0SGerrit Uitslag    /**
235a58f45f0SGerrit Uitslag     * Prepare cache
236a58f45f0SGerrit Uitslag     *
237a58f45f0SGerrit Uitslag     * @param string $title
238a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
239a58f45f0SGerrit Uitslag     * @return cache
240a58f45f0SGerrit Uitslag     */
241a58f45f0SGerrit Uitslag    protected function prepareCache($title, &$depends) {
242a58f45f0SGerrit Uitslag        global $REV;
243a58f45f0SGerrit Uitslag
244ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
245ee19bac3SLuigi Micco            . $REV
246ee19bac3SLuigi Micco            . $this->getExportConfig('template')
247ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
248ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
249d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
250ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
251ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
252ee19bac3SLuigi Micco            . $title;
253ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
254ee19bac3SLuigi Micco
255ee19bac3SLuigi Micco        $dependencies = array();
256ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
257ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
258ee19bac3SLuigi Micco
259ee19bac3SLuigi Micco            if(is_array($relations)) {
260ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
261ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
262ee19bac3SLuigi Micco                        if($exists) {
263ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
264ee19bac3SLuigi Micco                        }
265ee19bac3SLuigi Micco                    }
266ee19bac3SLuigi Micco                }
267ee19bac3SLuigi Micco
268ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
269ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
270ee19bac3SLuigi Micco                        if($exists) {
271ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
272ee19bac3SLuigi Micco                        }
273ee19bac3SLuigi Micco                    }
274ee19bac3SLuigi Micco                }
275ee19bac3SLuigi Micco            }
276ee19bac3SLuigi Micco
277ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
278ee19bac3SLuigi Micco        }
279ee19bac3SLuigi Micco
280ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
281ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
282ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
283ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
284ee19bac3SLuigi Micco        $depends['files'] = array_merge(
285ee19bac3SLuigi Micco            $depends['files'],
286ee19bac3SLuigi Micco            $dependencies,
287ee19bac3SLuigi Micco            getConfigFiles('main')
288ee19bac3SLuigi Micco        );
289a58f45f0SGerrit Uitslag        return $cache;
290ee19bac3SLuigi Micco    }
291ee19bac3SLuigi Micco
292d63e7fe7SGerrit Uitslag    /**
293d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
294d63e7fe7SGerrit Uitslag     *
295d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
296d63e7fe7SGerrit Uitslag     * @param string $msglangkey key of translation key
297c7138b3fSGerrit Uitslag     * @param string $replacement
298d63e7fe7SGerrit Uitslag     */
299c7138b3fSGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) {
300c7138b3fSGerrit Uitslag        if(empty($replacement)) {
301c7138b3fSGerrit Uitslag            $msg = $this->getLang($msglangkey);
302c7138b3fSGerrit Uitslag        } else {
303c7138b3fSGerrit Uitslag            $msg = sprintf($this->getLang($msglangkey), $replacement);
304c7138b3fSGerrit Uitslag        }
305c7138b3fSGerrit Uitslag        msg($msg, -1);
306d63e7fe7SGerrit Uitslag
307d63e7fe7SGerrit Uitslag        $event->data = 'show';
308d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
309d63e7fe7SGerrit Uitslag    }
310d63e7fe7SGerrit Uitslag
311d63e7fe7SGerrit Uitslag    /**
312d63e7fe7SGerrit Uitslag     * Build a pdf from the html
313d63e7fe7SGerrit Uitslag     *
314d63e7fe7SGerrit Uitslag     * @param string $cachefile
315d63e7fe7SGerrit Uitslag     * @param string $title
316d63e7fe7SGerrit Uitslag     */
317d63e7fe7SGerrit Uitslag    protected function generatePDF($cachefile, $title) {
318d63e7fe7SGerrit Uitslag        global $ID;
319d63e7fe7SGerrit Uitslag        global $REV;
320d63e7fe7SGerrit Uitslag        global $INPUT;
32187c86ddaSAndreas Gohr
32202f9a447SGerrit Uitslag        //some shortcuts to export settings
32302f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
32402f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
32502f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
3266ea88a05SAndreas Gohr
3271ef68647SAndreas Gohr        // initialize PDF library
328cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
3296ea88a05SAndreas Gohr
3304870b378SLarsDW223        $mpdf = new DokuPDF($this->getExportConfig('pagesize'),
3314870b378SLarsDW223                            $this->getExportConfig('orientation'),
3324870b378SLarsDW223                            $this->getExportConfig('font-size'));
333ee19bac3SLuigi Micco
334d62df65bSAndreas Gohr        // let mpdf fix local links
335d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
336d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
33702f9a447SGerrit Uitslag        if($self['port']) {
33802f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
33902f9a447SGerrit Uitslag        }
340d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
341d62df65bSAndreas Gohr
34256d13144SAndreas Gohr        // Set the title
34356d13144SAndreas Gohr        $mpdf->SetTitle($title);
34456d13144SAndreas Gohr
345d63e7fe7SGerrit Uitslag        // some default document settings
346d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
347213fdb75SGerrit Uitslag        //      single-side document has only odd pages
348213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
349daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
350daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
35102f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
35202f9a447SGerrit Uitslag        if($hasToC) {
35302f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
35402f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
35502f9a447SGerrit Uitslag        } else {
35602f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
35702f9a447SGerrit Uitslag        }
3582eedf77dSAndreas Gohr
35956d13144SAndreas Gohr        // load the template
3601c14c879SAndreas Gohr        $template = $this->load_template($title);
361ee19bac3SLuigi Micco
3621ef68647SAndreas Gohr        // prepare HTML header styles
363a2c33768SGerrit Uitslag        $html = '';
36402f9a447SGerrit Uitslag        if($isDebug) {
365a2c33768SGerrit Uitslag            $html .= '<html><head>';
366737417c6SKlap-in            $html .= '<style type="text/css">';
367a2c33768SGerrit Uitslag        }
368a2c33768SGerrit Uitslag        $styles = $this->load_css();
369a2c33768SGerrit Uitslag        $styles .= '@page { size:auto; ' . $template['page'] . '}';
370a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
371254467c4SGerrit Uitslag
372254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
373254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
374254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
375254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
376254467c4SGerrit Uitslag
377a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
378a2c33768SGerrit Uitslag
37902f9a447SGerrit Uitslag        if($isDebug) {
380a2c33768SGerrit Uitslag            $html .= $styles;
3811ef68647SAndreas Gohr            $html .= '</style>';
3821ef68647SAndreas Gohr            $html .= '</head><body>';
383a2c33768SGerrit Uitslag        }
384a2c33768SGerrit Uitslag
385a2c33768SGerrit Uitslag        $body_start = $template['html'];
386a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3872eedf77dSAndreas Gohr
3881e45476bSmnapp        // insert the cover page
389a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
390a2c33768SGerrit Uitslag
391a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
39202f9a447SGerrit Uitslag        if($isDebug) {
393a2c33768SGerrit Uitslag            $html .= $body_start;
394a2c33768SGerrit Uitslag        }
39502f9a447SGerrit Uitslag        if($hasToC) {
39602f9a447SGerrit 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
39702f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
39802f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
39902f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
40002f9a447SGerrit Uitslag                array(
401230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
402230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
40302f9a447SGerrit Uitslag                    'links' => true,
40402f9a447SGerrit Uitslag                    'outdent' => '1em',
40502f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
40602f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
40702f9a447SGerrit Uitslag                )
40802f9a447SGerrit Uitslag            );
40902f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
41002f9a447SGerrit Uitslag        }
41102f9a447SGerrit Uitslag
412c00eb13bSGerrit Uitslag        // store original pageid
413c00eb13bSGerrit Uitslag        $keep = $ID;
414c00eb13bSGerrit Uitslag
4151ef68647SAndreas Gohr        // loop over all pages
416c7138b3fSGerrit Uitslag        $counter = 0;
417c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
418c7138b3fSGerrit Uitslag        foreach($this->list as $page) {
419c7138b3fSGerrit Uitslag            $counter++;
420b3eed6e3SGerrit Uitslag            $filename = wikiFN($page, $REV);
421b3eed6e3SGerrit Uitslag
422b3eed6e3SGerrit Uitslag            if(!file_exists($filename)) {
423b3eed6e3SGerrit Uitslag                continue;
424b3eed6e3SGerrit Uitslag            }
425ee19bac3SLuigi Micco
426c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
427c00eb13bSGerrit Uitslag            $ID = $page;
428c00eb13bSGerrit Uitslag
429b3eed6e3SGerrit Uitslag            $pagehtml = p_cached_output($filename, 'dw2pdf', $page);
430719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
431c7138b3fSGerrit Uitslag            if($counter < $no_pages) {
432a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
433a2c33768SGerrit Uitslag            }
434a2c33768SGerrit Uitslag
435a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
43602f9a447SGerrit Uitslag            if($isDebug) {
437a2c33768SGerrit Uitslag                $html .= $pagehtml;
4381ef68647SAndreas Gohr            }
439ee19bac3SLuigi Micco        }
440c00eb13bSGerrit Uitslag        //restore ID
441c00eb13bSGerrit Uitslag        $ID = $keep;
442ee19bac3SLuigi Micco
44333c15297SGerrit Uitslag        // insert the back page
444a2c33768SGerrit Uitslag        $body_end = $template['back'];
44533c15297SGerrit Uitslag
446a2c33768SGerrit Uitslag        $body_end .= '</div>';
447a2c33768SGerrit Uitslag
448d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
44902f9a447SGerrit Uitslag        if($isDebug) {
450a2c33768SGerrit Uitslag            $html .= $body_end;
451eeb17e15SAndreas Gohr            $html .= '</body>';
452eeb17e15SAndreas Gohr            $html .= '</html>';
453a2c33768SGerrit Uitslag        }
454f765508eSGerrit Uitslag
455f765508eSGerrit Uitslag        //Return html for debugging
45602f9a447SGerrit Uitslag        if($isDebug) {
45702f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
45826be4eceSGerrit Uitslag                echo $html;
459a2c33768SGerrit Uitslag            } else {
460a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
461a2c33768SGerrit Uitslag                echo $html;
462a2c33768SGerrit Uitslag            }
46326be4eceSGerrit Uitslag            exit();
46426be4eceSGerrit Uitslag        };
465f765508eSGerrit Uitslag
46687c86ddaSAndreas Gohr        // write to cache file
467d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
46887c86ddaSAndreas Gohr    }
46987c86ddaSAndreas Gohr
470d63e7fe7SGerrit Uitslag    /**
471d63e7fe7SGerrit Uitslag     * @param string $cachefile
472d63e7fe7SGerrit Uitslag     * @param string $title
473d63e7fe7SGerrit Uitslag     */
474d63e7fe7SGerrit Uitslag    protected function sendPDFFile($cachefile, $title) {
47587c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
476b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
47787c86ddaSAndreas Gohr        header('Pragma: public');
478d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
47987c86ddaSAndreas Gohr
4809a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
48187c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4829a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
48387c86ddaSAndreas Gohr        } else {
4849a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
48587c86ddaSAndreas Gohr        }
486ee19bac3SLuigi Micco
487b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
488b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
489b3eed6e3SGerrit Uitslag
490e993da11SGerrit Uitslag        //try to send file, and exit if done
491d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
49287c86ddaSAndreas Gohr
493d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
49487c86ddaSAndreas Gohr        if($fp) {
495d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
49687c86ddaSAndreas Gohr        } else {
49787c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
49887c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
49987c86ddaSAndreas Gohr        }
5001ef68647SAndreas Gohr        exit();
5011ef68647SAndreas Gohr    }
5021ef68647SAndreas Gohr
5036be736bfSGerrit Uitslag    /**
5042eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
50544e8e8fbSGerrit Uitslag     *
50644e8e8fbSGerrit Uitslag     * @param string $title
50744e8e8fbSGerrit Uitslag     * @return array
5081ef68647SAndreas Gohr     */
5091c14c879SAndreas Gohr    protected function load_template($title) {
5101ef68647SAndreas Gohr        global $ID;
5111ef68647SAndreas Gohr        global $conf;
5121ef68647SAndreas Gohr
5132eedf77dSAndreas Gohr        // this is what we'll return
5142eedf77dSAndreas Gohr        $output = array(
5151e45476bSmnapp            'cover' => '',
5162eedf77dSAndreas Gohr            'html'  => '',
5172eedf77dSAndreas Gohr            'page'  => '',
5182eedf77dSAndreas Gohr            'first' => '',
5192eedf77dSAndreas Gohr            'cite'  => '',
5202eedf77dSAndreas Gohr        );
5212eedf77dSAndreas Gohr
5222eedf77dSAndreas Gohr        // prepare header/footer elements
5232eedf77dSAndreas Gohr        $html = '';
52433c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
52533c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
52602f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
52733c15297SGerrit Uitslag                if(file_exists($file)) {
52833c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
52933c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
53033c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
5312eedf77dSAndreas Gohr
5322eedf77dSAndreas Gohr                    // register the needed pseudo CSS
53333c15297SGerrit Uitslag                    if($order == '_first') {
53433c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
53533c15297SGerrit Uitslag                    } elseif($order == '_even') {
53633c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
53733c15297SGerrit Uitslag                    } elseif($order == '_odd') {
53833c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
539daa70883SAndreas Gohr                    } else {
54033c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5412eedf77dSAndreas Gohr                    }
5422eedf77dSAndreas Gohr                }
5432eedf77dSAndreas Gohr            }
5442eedf77dSAndreas Gohr        }
5452eedf77dSAndreas Gohr
5461ef68647SAndreas Gohr        // prepare replacements
5471ef68647SAndreas Gohr        $replace = array(
5481ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
54902f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
5502eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
5511ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5521ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5531ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5545d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
55502f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5561ef68647SAndreas Gohr        );
5571ef68647SAndreas Gohr
5582eedf77dSAndreas Gohr        // set HTML element
559a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
560a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
561a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5621ef68647SAndreas Gohr
5631e45476bSmnapp        // cover page
56402f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
56533c15297SGerrit Uitslag        if(file_exists($coverfile)) {
56633c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5671e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5689b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5696e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5701e45476bSmnapp        }
5711e45476bSmnapp
57233c15297SGerrit Uitslag        // cover page
57302f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
57433c15297SGerrit Uitslag        if(file_exists($backfile)) {
57533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
57633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
57733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5789b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
57933c15297SGerrit Uitslag        }
58033c15297SGerrit Uitslag
5812eedf77dSAndreas Gohr        // citation box
58202f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
58333c15297SGerrit Uitslag        if(file_exists($citationfile)) {
58433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5852eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5862eedf77dSAndreas Gohr        }
5871ef68647SAndreas Gohr
5882eedf77dSAndreas Gohr        return $output;
5891ef68647SAndreas Gohr    }
5901ef68647SAndreas Gohr
5911ef68647SAndreas Gohr    /**
592a180c973SKlap-in     * @param string $raw code with placeholders
593a180c973SKlap-in     * @param string $id  pageid
594a180c973SKlap-in     * @return string
595a180c973SKlap-in     */
596a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
597a180c973SKlap-in        global $REV;
598a180c973SKlap-in
599a180c973SKlap-in        // generate qr code for this page using google infographics api
600a180c973SKlap-in        $qr_code = '';
601a180c973SKlap-in        if($this->getConf('qrcodesize')) {
602a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
603a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
604a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
605a180c973SKlap-in        }
606a180c973SKlap-in        // prepare replacements
607a180c973SKlap-in        $replace['@ID@']      = $id;
608a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
609a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
610a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
611a180c973SKlap-in
612e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
613e3d68265SGerrit Uitslag
614e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
615e3d68265SGerrit Uitslag        $content = preg_replace_callback(
616e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
617e3d68265SGerrit Uitslag            array($this, 'replacedate'),
618e3d68265SGerrit Uitslag            $content
619e3d68265SGerrit Uitslag        );
620e3d68265SGerrit Uitslag
621e3d68265SGerrit Uitslag        return $content;
622a180c973SKlap-in    }
623a180c973SKlap-in
624e3d68265SGerrit Uitslag
625e3d68265SGerrit Uitslag    /**
626e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
627e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
628e3d68265SGerrit Uitslag     *
629e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
630e3d68265SGerrit Uitslag     * @return string
631e3d68265SGerrit Uitslag     */
632e3d68265SGerrit Uitslag    function replacedate($match) {
633e3d68265SGerrit Uitslag        global $conf;
634e3d68265SGerrit Uitslag        //no 2nd argument for default date format
635e3d68265SGerrit Uitslag        if($match[2] == null) {
636e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
637e3d68265SGerrit Uitslag        }
638e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
639e3d68265SGerrit Uitslag    }
640e3d68265SGerrit Uitslag
641e3d68265SGerrit Uitslag
642a180c973SKlap-in    /**
6431c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6441ef68647SAndreas Gohr     */
6451c14c879SAndreas Gohr    protected function load_css() {
646737417c6SKlap-in        global $conf;
6471c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6481c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6491c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
650ee19bac3SLuigi Micco
6511c14c879SAndreas Gohr        // prepare CSS files
6521c14c879SAndreas Gohr        $files = array_merge(
6531c14c879SAndreas Gohr            array(
6541c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6551c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6561c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6571c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6581c14c879SAndreas Gohr            ),
6591c14c879SAndreas Gohr            css_pluginstyles('all'),
66058e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6611c14c879SAndreas Gohr            array(
6621c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6631c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6641c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6651c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6661c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6671c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6681c14c879SAndreas Gohr            )
6691c14c879SAndreas Gohr        );
6701c14c879SAndreas Gohr        $css = '';
6711c14c879SAndreas Gohr        foreach($files as $file => $location) {
67228e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
67328e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6741c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6751ef68647SAndreas Gohr        }
6761ef68647SAndreas Gohr
67728e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6781c14c879SAndreas Gohr            // apply pattern replacements
67928e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
68028e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
68128e636eaSGerrit Uitslag
68228e636eaSGerrit Uitslag            // parse less
68328e636eaSGerrit Uitslag            $css = css_parseless($css);
68428e636eaSGerrit Uitslag        } else {
68528e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6861c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
68728e636eaSGerrit Uitslag        }
6881ef68647SAndreas Gohr
6891c14c879SAndreas Gohr        return $css;
690ee19bac3SLuigi Micco    }
6911c14c879SAndreas Gohr
69258e6409eSAndreas Gohr    /**
69358e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
69458e6409eSAndreas Gohr     *
69558e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
69658e6409eSAndreas Gohr     *
69758e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
69858e6409eSAndreas Gohr     */
6996be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
70058e6409eSAndreas Gohr        $list = array();
70158e6409eSAndreas Gohr        $plugins = plugin_list();
702f54b51f7SAndreas Gohr
703f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
70458e6409eSAndreas Gohr        foreach($plugins as $p) {
705f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
706f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
707f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
708f54b51f7SAndreas Gohr            }
709f54b51f7SAndreas Gohr
71058e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
71158e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
71258e6409eSAndreas Gohr            } else {
71358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
71458e6409eSAndreas Gohr            }
71558e6409eSAndreas Gohr        }
71658e6409eSAndreas Gohr        return $list;
71758e6409eSAndreas Gohr    }
718ad18f4e1SGerrit Uitslag
719ad18f4e1SGerrit Uitslag    /**
72060e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
72160e59de7SGerrit Uitslag     *
72260e59de7SGerrit Uitslag     * @return array
72360e59de7SGerrit Uitslag     */
72460e59de7SGerrit Uitslag    public function getExportedPages() {
72560e59de7SGerrit Uitslag        return $this->list;
72660e59de7SGerrit Uitslag    }
72760e59de7SGerrit Uitslag
72860e59de7SGerrit Uitslag    /**
729ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
73044e8e8fbSGerrit Uitslag     *
73144e8e8fbSGerrit Uitslag     * @param array $a
73244e8e8fbSGerrit Uitslag     * @param array $b
73344e8e8fbSGerrit Uitslag     * @return int
734ad18f4e1SGerrit Uitslag     */
735ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
736ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
737ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
738ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
739ad18f4e1SGerrit Uitslag    }
740ad18f4e1SGerrit Uitslag
741ad18f4e1SGerrit Uitslag    /**
742ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
74344e8e8fbSGerrit Uitslag     * @param array $a
74444e8e8fbSGerrit Uitslag     * @param array $b
74544e8e8fbSGerrit Uitslag     * @return int
746ad18f4e1SGerrit Uitslag     */
747ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
748ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
749ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
750ad18f4e1SGerrit Uitslag        return 0;
751ad18f4e1SGerrit Uitslag    }
75226be4eceSGerrit Uitslag
75326be4eceSGerrit Uitslag    /**
75402f9a447SGerrit Uitslag     * Return settings read from:
75502f9a447SGerrit Uitslag     *   1. url parameters
75602f9a447SGerrit Uitslag     *   2. plugin config
75702f9a447SGerrit Uitslag     *   3. global config
75802f9a447SGerrit Uitslag     *
75902f9a447SGerrit Uitslag     * @return array
76002f9a447SGerrit Uitslag     */
76102f9a447SGerrit Uitslag    protected function loadExportConfig() {
76202f9a447SGerrit Uitslag        global $INPUT;
76302f9a447SGerrit Uitslag        global $conf;
76402f9a447SGerrit Uitslag
76502f9a447SGerrit Uitslag        $this->exportConfig = array();
76602f9a447SGerrit Uitslag
76702f9a447SGerrit Uitslag        // decide on the paper setup from param or config
76802f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
76902f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
77002f9a447SGerrit Uitslag
7714870b378SLarsDW223        // decide on the font-size from param or config
7724870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
7734870b378SLarsDW223
774213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
775213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
776213fdb75SGerrit Uitslag
777213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
77802f9a447SGerrit Uitslag        $levels = array();
77902f9a447SGerrit Uitslag        if($hasToC) {
78002f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
78102f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
78202f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
78302f9a447SGerrit Uitslag            $bounds_input = array(
78402f9a447SGerrit Uitslag                'top' => array(
78502f9a447SGerrit Uitslag                    (int) $top_input,
78602f9a447SGerrit Uitslag                    (int) $top_conf
78702f9a447SGerrit Uitslag                ),
78802f9a447SGerrit Uitslag                'max' => array(
78902f9a447SGerrit Uitslag                    (int) $max_input,
79002f9a447SGerrit Uitslag                    (int) $max_conf
79102f9a447SGerrit Uitslag                )
79202f9a447SGerrit Uitslag            );
79302f9a447SGerrit Uitslag            $bounds = array(
79402f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
79502f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
79602f9a447SGerrit Uitslag
79702f9a447SGerrit Uitslag            );
79802f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
79902f9a447SGerrit Uitslag                foreach($values as $value) {
80002f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
80102f9a447SGerrit Uitslag                        //stop at valid value and store
80202f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
80302f9a447SGerrit Uitslag                        break;
80402f9a447SGerrit Uitslag                    }
80502f9a447SGerrit Uitslag                }
80602f9a447SGerrit Uitslag            }
80702f9a447SGerrit Uitslag
80802f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
80902f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
81002f9a447SGerrit Uitslag            }
81102f9a447SGerrit Uitslag
81202f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
81302f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
81402f9a447SGerrit Uitslag            }
81502f9a447SGerrit Uitslag        }
81602f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
81702f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
81802f9a447SGerrit Uitslag
81902f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
82002f9a447SGerrit Uitslag
82102f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
82205d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
82302f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
82402f9a447SGerrit Uitslag            $tpl = $tplconf;
82502f9a447SGerrit Uitslag        }
82602f9a447SGerrit Uitslag        if(!$tpl){
82702f9a447SGerrit Uitslag            $tpl = 'default';
82802f9a447SGerrit Uitslag        }
82902f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
83002f9a447SGerrit Uitslag
83102f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
83202f9a447SGerrit Uitslag    }
83302f9a447SGerrit Uitslag
83402f9a447SGerrit Uitslag    /**
83502f9a447SGerrit Uitslag     * Returns requested config
83602f9a447SGerrit Uitslag     *
83702f9a447SGerrit Uitslag     * @param string $name
83802f9a447SGerrit Uitslag     * @param mixed  $notset
83902f9a447SGerrit Uitslag     * @return mixed|bool
84002f9a447SGerrit Uitslag     */
84102f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
84202f9a447SGerrit Uitslag        if ($this->exportConfig === null){
84302f9a447SGerrit Uitslag            $this->loadExportConfig();
84402f9a447SGerrit Uitslag        }
84502f9a447SGerrit Uitslag
84602f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
84702f9a447SGerrit Uitslag            return $this->exportConfig[$name];
84802f9a447SGerrit Uitslag        }else{
84902f9a447SGerrit Uitslag            return $notset;
85002f9a447SGerrit Uitslag        }
85102f9a447SGerrit Uitslag    }
852d63e7fe7SGerrit Uitslag
853d63e7fe7SGerrit Uitslag    /**
854d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
855d63e7fe7SGerrit Uitslag     *
856d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
857d63e7fe7SGerrit Uitslag     */
85844e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
859d63e7fe7SGerrit Uitslag        global $ID, $REV;
860d63e7fe7SGerrit Uitslag
861d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
862d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
863d63e7fe7SGerrit Uitslag            if($REV) {
864d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
865d63e7fe7SGerrit Uitslag            }
866d63e7fe7SGerrit Uitslag
867d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
868d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
869d63e7fe7SGerrit Uitslag                array('export_pdf' =>
870d63e7fe7SGerrit Uitslag                          '<li>'
87172cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
872d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
873d63e7fe7SGerrit Uitslag                          . '</a>'
874d63e7fe7SGerrit Uitslag                          . '</li>'
875d63e7fe7SGerrit Uitslag                ) +
876d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
877d63e7fe7SGerrit Uitslag        }
878d63e7fe7SGerrit Uitslag    }
879ee19bac3SLuigi Micco}
880