xref: /plugin/dw2pdf/action.php (revision 52896605837be7335616d9217f11ff395126a69b)
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
759bcbb4f0SGerrit Uitslag        // hard work only when no cache available or needed for debugging
769bcbb4f0SGerrit Uitslag        if(!$this->getConf('usecache') || $this->getExportConfig('isDebug') || !$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'])) {
162b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator*/
16326be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
164177a7d30SGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
165177a7d30SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
166177a7d30SGerrit Uitslag            if(empty($title)) {
16726be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
168737417c6SKlap-in                return false;
16926be4eceSGerrit Uitslag            } else {
170d63e7fe7SGerrit Uitslag                $list = explode("|", $_COOKIE['list-pagelist']);
17126be4eceSGerrit Uitslag            }
172ad18f4e1SGerrit Uitslag
173b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
174b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
175b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
176b3eed6e3SGerrit Uitslag//                http_status(403);
177b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
178b3eed6e3SGerrit Uitslag//                exit();
179b3eed6e3SGerrit Uitslag//            }
180b3eed6e3SGerrit Uitslag
181b3eed6e3SGerrit Uitslag            $json = new JSON(JSON_LOOSE_TYPE);
182b3eed6e3SGerrit Uitslag            $list = $json->decode($INPUT->post->str('selection', '', true));
183b3eed6e3SGerrit Uitslag            if(!is_array($list) || empty($list)) {
184b3eed6e3SGerrit Uitslag                http_status(400);
185b3eed6e3SGerrit Uitslag                print $this->getLang('empty');
186b3eed6e3SGerrit Uitslag                exit();
187b3eed6e3SGerrit Uitslag            }
188b3eed6e3SGerrit Uitslag
189b3eed6e3SGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
190b3eed6e3SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
191b3eed6e3SGerrit Uitslag            if(empty($title)) {
192b3eed6e3SGerrit Uitslag                http_status(400);
193b3eed6e3SGerrit Uitslag                print $this->getLang('needtitle');
194b3eed6e3SGerrit Uitslag                exit();
195b3eed6e3SGerrit Uitslag            }
196b3eed6e3SGerrit Uitslag
197737417c6SKlap-in        } else {
19826be4eceSGerrit Uitslag            //show empty bookcreator message
19926be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
200737417c6SKlap-in            return false;
201737417c6SKlap-in        }
202737417c6SKlap-in
203719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
204d63e7fe7SGerrit Uitslag        return array($title, $list);
205d63e7fe7SGerrit Uitslag    }
206d63e7fe7SGerrit Uitslag
207a58f45f0SGerrit Uitslag    /**
208a58f45f0SGerrit Uitslag     * Prepare cache
209a58f45f0SGerrit Uitslag     *
210a58f45f0SGerrit Uitslag     * @param string $title
211a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
212a58f45f0SGerrit Uitslag     * @return cache
213a58f45f0SGerrit Uitslag     */
214a58f45f0SGerrit Uitslag    protected function prepareCache($title, &$depends) {
215a58f45f0SGerrit Uitslag        global $REV;
216a58f45f0SGerrit Uitslag
217ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
218ee19bac3SLuigi Micco            . $REV
219ee19bac3SLuigi Micco            . $this->getExportConfig('template')
220ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
221ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
222ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
223ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
224ee19bac3SLuigi Micco            . $title;
225ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
226ee19bac3SLuigi Micco
227ee19bac3SLuigi Micco        $dependencies = array();
228ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
229ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
230ee19bac3SLuigi Micco
231ee19bac3SLuigi Micco            if(is_array($relations)) {
232ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
233ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
234ee19bac3SLuigi Micco                        if($exists) {
235ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
236ee19bac3SLuigi Micco                        }
237ee19bac3SLuigi Micco                    }
238ee19bac3SLuigi Micco                }
239ee19bac3SLuigi Micco
240ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
241ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
242ee19bac3SLuigi Micco                        if($exists) {
243ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
244ee19bac3SLuigi Micco                        }
245ee19bac3SLuigi Micco                    }
246ee19bac3SLuigi Micco                }
247ee19bac3SLuigi Micco            }
248ee19bac3SLuigi Micco
249ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
250ee19bac3SLuigi Micco        }
251ee19bac3SLuigi Micco
252ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
253ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
254ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
255ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
256ee19bac3SLuigi Micco        $depends['files'] = array_merge(
257ee19bac3SLuigi Micco            $depends['files'],
258ee19bac3SLuigi Micco            $dependencies,
259ee19bac3SLuigi Micco            getConfigFiles('main')
260ee19bac3SLuigi Micco        );
261a58f45f0SGerrit Uitslag        return $cache;
262ee19bac3SLuigi Micco    }
263ee19bac3SLuigi Micco
264d63e7fe7SGerrit Uitslag    /**
265d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
266d63e7fe7SGerrit Uitslag     *
267d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
268d63e7fe7SGerrit Uitslag     * @param string     $msglangkey key of translation key
269d63e7fe7SGerrit Uitslag     */
270d63e7fe7SGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey) {
271d63e7fe7SGerrit Uitslag        msg($this->getLang($msglangkey), -1);
272d63e7fe7SGerrit Uitslag
273d63e7fe7SGerrit Uitslag        $event->data = 'show';
274d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
275d63e7fe7SGerrit Uitslag    }
276d63e7fe7SGerrit Uitslag
277d63e7fe7SGerrit Uitslag    /**
278d63e7fe7SGerrit Uitslag     * Build a pdf from the html
279d63e7fe7SGerrit Uitslag     *
280d63e7fe7SGerrit Uitslag     * @param string $cachefile
281d63e7fe7SGerrit Uitslag     * @param string $title
282d63e7fe7SGerrit Uitslag     */
283d63e7fe7SGerrit Uitslag    protected function generatePDF($cachefile, $title) {
284d63e7fe7SGerrit Uitslag        global $ID;
285d63e7fe7SGerrit Uitslag        global $REV;
286d63e7fe7SGerrit Uitslag        global $INPUT;
28787c86ddaSAndreas Gohr
28802f9a447SGerrit Uitslag        //some shortcuts to export settings
28902f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
29002f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
29102f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
2926ea88a05SAndreas Gohr
2931ef68647SAndreas Gohr        // initialize PDF library
294cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
2956ea88a05SAndreas Gohr
2964870b378SLarsDW223        $mpdf = new DokuPDF($this->getExportConfig('pagesize'),
2974870b378SLarsDW223                            $this->getExportConfig('orientation'),
2984870b378SLarsDW223                            $this->getExportConfig('font-size'));
299ee19bac3SLuigi Micco
300d62df65bSAndreas Gohr        // let mpdf fix local links
301d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
302d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
30302f9a447SGerrit Uitslag        if($self['port']) {
30402f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
30502f9a447SGerrit Uitslag        }
306d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
307d62df65bSAndreas Gohr
30856d13144SAndreas Gohr        // Set the title
30956d13144SAndreas Gohr        $mpdf->SetTitle($title);
31056d13144SAndreas Gohr
311d63e7fe7SGerrit Uitslag        // some default document settings
312d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
313213fdb75SGerrit Uitslag        //      single-side document has only odd pages
314213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
315daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
316daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
31702f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
31802f9a447SGerrit Uitslag        if($hasToC) {
31902f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
32002f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
32102f9a447SGerrit Uitslag        } else {
32202f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
32302f9a447SGerrit Uitslag        }
3242eedf77dSAndreas Gohr
32556d13144SAndreas Gohr        // load the template
3261c14c879SAndreas Gohr        $template = $this->load_template($title);
327ee19bac3SLuigi Micco
3281ef68647SAndreas Gohr        // prepare HTML header styles
329a2c33768SGerrit Uitslag        $html = '';
33002f9a447SGerrit Uitslag        if($isDebug) {
331a2c33768SGerrit Uitslag            $html .= '<html><head>';
332737417c6SKlap-in            $html .= '<style type="text/css">';
333a2c33768SGerrit Uitslag        }
334a2c33768SGerrit Uitslag        $styles = $this->load_css();
335a2c33768SGerrit Uitslag        $styles .= '@page { size:auto; ' . $template['page'] . '}';
336a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
337254467c4SGerrit Uitslag
338254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
339254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
340254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
341254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
342254467c4SGerrit Uitslag
343a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
344a2c33768SGerrit Uitslag
34502f9a447SGerrit Uitslag        if($isDebug) {
346a2c33768SGerrit Uitslag            $html .= $styles;
3471ef68647SAndreas Gohr            $html .= '</style>';
3481ef68647SAndreas Gohr            $html .= '</head><body>';
349a2c33768SGerrit Uitslag        }
350a2c33768SGerrit Uitslag
351a2c33768SGerrit Uitslag        $body_start = $template['html'];
352a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3532eedf77dSAndreas Gohr
3541e45476bSmnapp        // insert the cover page
355a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
356a2c33768SGerrit Uitslag
357a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
35802f9a447SGerrit Uitslag        if($isDebug) {
359a2c33768SGerrit Uitslag            $html .= $body_start;
360a2c33768SGerrit Uitslag        }
36102f9a447SGerrit Uitslag        if($hasToC) {
36202f9a447SGerrit 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
36302f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
36402f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
36502f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
36602f9a447SGerrit Uitslag                array(
367230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
368230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
36902f9a447SGerrit Uitslag                    'links' => true,
37002f9a447SGerrit Uitslag                    'outdent' => '1em',
37102f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
37202f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
37302f9a447SGerrit Uitslag                )
37402f9a447SGerrit Uitslag            );
37502f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
37602f9a447SGerrit Uitslag        }
37702f9a447SGerrit Uitslag
378c00eb13bSGerrit Uitslag        // store original pageid
379c00eb13bSGerrit Uitslag        $keep = $ID;
380c00eb13bSGerrit Uitslag
3811ef68647SAndreas Gohr        // loop over all pages
382a2c33768SGerrit Uitslag        $cnt = count($this->list);
3831ef68647SAndreas Gohr        for($n = 0; $n < $cnt; $n++) {
384a2c33768SGerrit Uitslag            $page = $this->list[$n];
385b3eed6e3SGerrit Uitslag            $filename = wikiFN($page, $REV);
386b3eed6e3SGerrit Uitslag
387b3eed6e3SGerrit Uitslag            if(!file_exists($filename)) {
388b3eed6e3SGerrit Uitslag                continue;
389b3eed6e3SGerrit Uitslag            }
390ee19bac3SLuigi Micco
391c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
392c00eb13bSGerrit Uitslag            $ID = $page;
393c00eb13bSGerrit Uitslag
394b3eed6e3SGerrit Uitslag            $pagehtml = p_cached_output($filename, 'dw2pdf', $page);
395719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
3961ef68647SAndreas Gohr            if($n < ($cnt - 1)) {
397a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
398a2c33768SGerrit Uitslag            }
399a2c33768SGerrit Uitslag
400a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
40102f9a447SGerrit Uitslag            if($isDebug) {
402a2c33768SGerrit Uitslag                $html .= $pagehtml;
4031ef68647SAndreas Gohr            }
404ee19bac3SLuigi Micco        }
405c00eb13bSGerrit Uitslag        //restore ID
406c00eb13bSGerrit Uitslag        $ID = $keep;
407ee19bac3SLuigi Micco
40833c15297SGerrit Uitslag        // insert the back page
409a2c33768SGerrit Uitslag        $body_end = $template['back'];
41033c15297SGerrit Uitslag
411a2c33768SGerrit Uitslag        $body_end .= '</div>';
412a2c33768SGerrit Uitslag
413d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
41402f9a447SGerrit Uitslag        if($isDebug) {
415a2c33768SGerrit Uitslag            $html .= $body_end;
416eeb17e15SAndreas Gohr            $html .= '</body>';
417eeb17e15SAndreas Gohr            $html .= '</html>';
418a2c33768SGerrit Uitslag        }
419*52896605SGerrit Uitslag
420f765508eSGerrit Uitslag        //Return html for debugging
42102f9a447SGerrit Uitslag        if($isDebug) {
42202f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
42326be4eceSGerrit Uitslag                echo $html;
424a2c33768SGerrit Uitslag            } else {
425a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
426a2c33768SGerrit Uitslag                echo $html;
427a2c33768SGerrit Uitslag            }
42826be4eceSGerrit Uitslag            exit();
42926be4eceSGerrit Uitslag        };
430*52896605SGerrit Uitslag
43187c86ddaSAndreas Gohr        // write to cache file
432d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
43387c86ddaSAndreas Gohr    }
43487c86ddaSAndreas Gohr
435d63e7fe7SGerrit Uitslag    /**
436d63e7fe7SGerrit Uitslag     * @param string $cachefile
437d63e7fe7SGerrit Uitslag     * @param string $title
438d63e7fe7SGerrit Uitslag     */
439d63e7fe7SGerrit Uitslag    protected function sendPDFFile($cachefile, $title) {
44087c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
441b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
44287c86ddaSAndreas Gohr        header('Pragma: public');
443d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
44487c86ddaSAndreas Gohr
4459a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
44687c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4479a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
44887c86ddaSAndreas Gohr        } else {
4499a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
45087c86ddaSAndreas Gohr        }
451ee19bac3SLuigi Micco
452b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
453b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
454b3eed6e3SGerrit Uitslag
455e993da11SGerrit Uitslag        //try to send file, and exit if done
456d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
45787c86ddaSAndreas Gohr
458d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
45987c86ddaSAndreas Gohr        if($fp) {
460d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
46187c86ddaSAndreas Gohr        } else {
46287c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
46387c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
46487c86ddaSAndreas Gohr        }
4651ef68647SAndreas Gohr        exit();
4661ef68647SAndreas Gohr    }
4671ef68647SAndreas Gohr
4686be736bfSGerrit Uitslag    /**
4692eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
47044e8e8fbSGerrit Uitslag     *
47144e8e8fbSGerrit Uitslag     * @param string $title
47244e8e8fbSGerrit Uitslag     * @return array
4731ef68647SAndreas Gohr     */
4741c14c879SAndreas Gohr    protected function load_template($title) {
4751ef68647SAndreas Gohr        global $ID;
4761ef68647SAndreas Gohr        global $conf;
4771ef68647SAndreas Gohr
4782eedf77dSAndreas Gohr        // this is what we'll return
4792eedf77dSAndreas Gohr        $output = array(
4801e45476bSmnapp            'cover' => '',
4812eedf77dSAndreas Gohr            'html'  => '',
4822eedf77dSAndreas Gohr            'page'  => '',
4832eedf77dSAndreas Gohr            'first' => '',
4842eedf77dSAndreas Gohr            'cite'  => '',
4852eedf77dSAndreas Gohr        );
4862eedf77dSAndreas Gohr
4872eedf77dSAndreas Gohr        // prepare header/footer elements
4882eedf77dSAndreas Gohr        $html = '';
48933c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
49033c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
49102f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
49233c15297SGerrit Uitslag                if(file_exists($file)) {
49333c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
49433c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
49533c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
4962eedf77dSAndreas Gohr
4972eedf77dSAndreas Gohr                    // register the needed pseudo CSS
49833c15297SGerrit Uitslag                    if($order == '_first') {
49933c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
50033c15297SGerrit Uitslag                    } elseif($order == '_even') {
50133c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
50233c15297SGerrit Uitslag                    } elseif($order == '_odd') {
50333c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
504daa70883SAndreas Gohr                    } else {
50533c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5062eedf77dSAndreas Gohr                    }
5072eedf77dSAndreas Gohr                }
5082eedf77dSAndreas Gohr            }
5092eedf77dSAndreas Gohr        }
5102eedf77dSAndreas Gohr
5111ef68647SAndreas Gohr        // prepare replacements
5121ef68647SAndreas Gohr        $replace = array(
5131ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
51402f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
5152eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
5161ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5171ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5181ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5195d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
52002f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5211ef68647SAndreas Gohr        );
5221ef68647SAndreas Gohr
5232eedf77dSAndreas Gohr        // set HTML element
524a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
525a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
526a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5271ef68647SAndreas Gohr
5281e45476bSmnapp        // cover page
52902f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
53033c15297SGerrit Uitslag        if(file_exists($coverfile)) {
53133c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5321e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5339b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5346e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5351e45476bSmnapp        }
5361e45476bSmnapp
53733c15297SGerrit Uitslag        // cover page
53802f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
53933c15297SGerrit Uitslag        if(file_exists($backfile)) {
54033c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
54133c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
54233c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5439b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
54433c15297SGerrit Uitslag        }
54533c15297SGerrit Uitslag
5462eedf77dSAndreas Gohr        // citation box
54702f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
54833c15297SGerrit Uitslag        if(file_exists($citationfile)) {
54933c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5502eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5512eedf77dSAndreas Gohr        }
5521ef68647SAndreas Gohr
5532eedf77dSAndreas Gohr        return $output;
5541ef68647SAndreas Gohr    }
5551ef68647SAndreas Gohr
5561ef68647SAndreas Gohr    /**
557a180c973SKlap-in     * @param string $raw code with placeholders
558a180c973SKlap-in     * @param string $id  pageid
559a180c973SKlap-in     * @return string
560a180c973SKlap-in     */
561a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
562a180c973SKlap-in        global $REV;
563a180c973SKlap-in
564a180c973SKlap-in        // generate qr code for this page using google infographics api
565a180c973SKlap-in        $qr_code = '';
566a180c973SKlap-in        if($this->getConf('qrcodesize')) {
567a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
568a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
569a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
570a180c973SKlap-in        }
571a180c973SKlap-in        // prepare replacements
572a180c973SKlap-in        $replace['@ID@']      = $id;
573a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
574a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
575a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
576a180c973SKlap-in
577e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
578e3d68265SGerrit Uitslag
579e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
580e3d68265SGerrit Uitslag        $content = preg_replace_callback(
581e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
582e3d68265SGerrit Uitslag            array($this, 'replacedate'),
583e3d68265SGerrit Uitslag            $content
584e3d68265SGerrit Uitslag        );
585e3d68265SGerrit Uitslag
586e3d68265SGerrit Uitslag        return $content;
587a180c973SKlap-in    }
588a180c973SKlap-in
589e3d68265SGerrit Uitslag
590e3d68265SGerrit Uitslag    /**
591e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
592e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
593e3d68265SGerrit Uitslag     *
594e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
595e3d68265SGerrit Uitslag     * @return string
596e3d68265SGerrit Uitslag     */
597e3d68265SGerrit Uitslag    function replacedate($match) {
598e3d68265SGerrit Uitslag        global $conf;
599e3d68265SGerrit Uitslag        //no 2nd argument for default date format
600e3d68265SGerrit Uitslag        if($match[2] == null) {
601e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
602e3d68265SGerrit Uitslag        }
603e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
604e3d68265SGerrit Uitslag    }
605e3d68265SGerrit Uitslag
606e3d68265SGerrit Uitslag
607a180c973SKlap-in    /**
6081c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6091ef68647SAndreas Gohr     */
6101c14c879SAndreas Gohr    protected function load_css() {
611737417c6SKlap-in        global $conf;
6121c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6131c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6141c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
615ee19bac3SLuigi Micco
6161c14c879SAndreas Gohr        // prepare CSS files
6171c14c879SAndreas Gohr        $files = array_merge(
6181c14c879SAndreas Gohr            array(
6191c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6201c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6211c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6221c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6231c14c879SAndreas Gohr            ),
6241c14c879SAndreas Gohr            css_pluginstyles('all'),
62558e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6261c14c879SAndreas Gohr            array(
6271c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6281c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6291c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6301c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6311c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6321c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6331c14c879SAndreas Gohr            )
6341c14c879SAndreas Gohr        );
6351c14c879SAndreas Gohr        $css = '';
6361c14c879SAndreas Gohr        foreach($files as $file => $location) {
63728e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
63828e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6391c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6401ef68647SAndreas Gohr        }
6411ef68647SAndreas Gohr
64228e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6431c14c879SAndreas Gohr            // apply pattern replacements
64428e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
64528e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
64628e636eaSGerrit Uitslag
64728e636eaSGerrit Uitslag            // parse less
64828e636eaSGerrit Uitslag            $css = css_parseless($css);
64928e636eaSGerrit Uitslag        } else {
65028e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6511c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
65228e636eaSGerrit Uitslag        }
6531ef68647SAndreas Gohr
6541c14c879SAndreas Gohr        return $css;
655ee19bac3SLuigi Micco    }
6561c14c879SAndreas Gohr
65758e6409eSAndreas Gohr    /**
65858e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
65958e6409eSAndreas Gohr     *
66058e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
66158e6409eSAndreas Gohr     *
66258e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
66358e6409eSAndreas Gohr     */
6646be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
66558e6409eSAndreas Gohr        $list = array();
66658e6409eSAndreas Gohr        $plugins = plugin_list();
667f54b51f7SAndreas Gohr
668f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
66958e6409eSAndreas Gohr        foreach($plugins as $p) {
670f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
671f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
672f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
673f54b51f7SAndreas Gohr            }
674f54b51f7SAndreas Gohr
67558e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
67658e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
67758e6409eSAndreas Gohr            } else {
67858e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
67958e6409eSAndreas Gohr            }
68058e6409eSAndreas Gohr        }
68158e6409eSAndreas Gohr        return $list;
68258e6409eSAndreas Gohr    }
683ad18f4e1SGerrit Uitslag
684ad18f4e1SGerrit Uitslag    /**
68560e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
68660e59de7SGerrit Uitslag     *
68760e59de7SGerrit Uitslag     * @return array
68860e59de7SGerrit Uitslag     */
68960e59de7SGerrit Uitslag    public function getExportedPages() {
69060e59de7SGerrit Uitslag        return $this->list;
69160e59de7SGerrit Uitslag    }
69260e59de7SGerrit Uitslag
69360e59de7SGerrit Uitslag    /**
694ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
69544e8e8fbSGerrit Uitslag     *
69644e8e8fbSGerrit Uitslag     * @param array $a
69744e8e8fbSGerrit Uitslag     * @param array $b
69844e8e8fbSGerrit Uitslag     * @return int
699ad18f4e1SGerrit Uitslag     */
700ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
701ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
702ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
703ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
704ad18f4e1SGerrit Uitslag    }
705ad18f4e1SGerrit Uitslag
706ad18f4e1SGerrit Uitslag    /**
707ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
70844e8e8fbSGerrit Uitslag     * @param array $a
70944e8e8fbSGerrit Uitslag     * @param array $b
71044e8e8fbSGerrit Uitslag     * @return int
711ad18f4e1SGerrit Uitslag     */
712ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
713ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
714ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
715ad18f4e1SGerrit Uitslag        return 0;
716ad18f4e1SGerrit Uitslag    }
71726be4eceSGerrit Uitslag
71826be4eceSGerrit Uitslag    /**
71902f9a447SGerrit Uitslag     * Return settings read from:
72002f9a447SGerrit Uitslag     *   1. url parameters
72102f9a447SGerrit Uitslag     *   2. plugin config
72202f9a447SGerrit Uitslag     *   3. global config
72302f9a447SGerrit Uitslag     *
72402f9a447SGerrit Uitslag     * @return array
72502f9a447SGerrit Uitslag     */
72602f9a447SGerrit Uitslag    protected function loadExportConfig() {
72702f9a447SGerrit Uitslag        global $INPUT;
72802f9a447SGerrit Uitslag        global $conf;
72902f9a447SGerrit Uitslag
73002f9a447SGerrit Uitslag        $this->exportConfig = array();
73102f9a447SGerrit Uitslag
73202f9a447SGerrit Uitslag        // decide on the paper setup from param or config
73302f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
73402f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
73502f9a447SGerrit Uitslag
7364870b378SLarsDW223        // decide on the font-size from param or config
7374870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
7384870b378SLarsDW223
739213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
740213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
741213fdb75SGerrit Uitslag
742213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
74302f9a447SGerrit Uitslag        $levels = array();
74402f9a447SGerrit Uitslag        if($hasToC) {
74502f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
74602f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
74702f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
74802f9a447SGerrit Uitslag            $bounds_input = array(
74902f9a447SGerrit Uitslag                'top' => array(
75002f9a447SGerrit Uitslag                    (int) $top_input,
75102f9a447SGerrit Uitslag                    (int) $top_conf
75202f9a447SGerrit Uitslag                ),
75302f9a447SGerrit Uitslag                'max' => array(
75402f9a447SGerrit Uitslag                    (int) $max_input,
75502f9a447SGerrit Uitslag                    (int) $max_conf
75602f9a447SGerrit Uitslag                )
75702f9a447SGerrit Uitslag            );
75802f9a447SGerrit Uitslag            $bounds = array(
75902f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
76002f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
76102f9a447SGerrit Uitslag
76202f9a447SGerrit Uitslag            );
76302f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
76402f9a447SGerrit Uitslag                foreach($values as $value) {
76502f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
76602f9a447SGerrit Uitslag                        //stop at valid value and store
76702f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
76802f9a447SGerrit Uitslag                        break;
76902f9a447SGerrit Uitslag                    }
77002f9a447SGerrit Uitslag                }
77102f9a447SGerrit Uitslag            }
77202f9a447SGerrit Uitslag
77302f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
77402f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
77502f9a447SGerrit Uitslag            }
77602f9a447SGerrit Uitslag
77702f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
77802f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
77902f9a447SGerrit Uitslag            }
78002f9a447SGerrit Uitslag        }
78102f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
78202f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
78302f9a447SGerrit Uitslag
78402f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
78502f9a447SGerrit Uitslag
78602f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
78705d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
78802f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
78902f9a447SGerrit Uitslag            $tpl = $tplconf;
79002f9a447SGerrit Uitslag        }
79102f9a447SGerrit Uitslag        if(!$tpl){
79202f9a447SGerrit Uitslag            $tpl = 'default';
79302f9a447SGerrit Uitslag        }
79402f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
79502f9a447SGerrit Uitslag
79602f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
79702f9a447SGerrit Uitslag    }
79802f9a447SGerrit Uitslag
79902f9a447SGerrit Uitslag    /**
80002f9a447SGerrit Uitslag     * Returns requested config
80102f9a447SGerrit Uitslag     *
80202f9a447SGerrit Uitslag     * @param string $name
80302f9a447SGerrit Uitslag     * @param mixed  $notset
80402f9a447SGerrit Uitslag     * @return mixed|bool
80502f9a447SGerrit Uitslag     */
80602f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
80702f9a447SGerrit Uitslag        if ($this->exportConfig === null){
80802f9a447SGerrit Uitslag            $this->loadExportConfig();
80902f9a447SGerrit Uitslag        }
81002f9a447SGerrit Uitslag
81102f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
81202f9a447SGerrit Uitslag            return $this->exportConfig[$name];
81302f9a447SGerrit Uitslag        }else{
81402f9a447SGerrit Uitslag            return $notset;
81502f9a447SGerrit Uitslag        }
81602f9a447SGerrit Uitslag    }
817d63e7fe7SGerrit Uitslag
818d63e7fe7SGerrit Uitslag    /**
819d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
820d63e7fe7SGerrit Uitslag     *
821d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
822d63e7fe7SGerrit Uitslag     */
82344e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
824d63e7fe7SGerrit Uitslag        global $ID, $REV;
825d63e7fe7SGerrit Uitslag
826d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
827d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
828d63e7fe7SGerrit Uitslag            if($REV) {
829d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
830d63e7fe7SGerrit Uitslag            }
831d63e7fe7SGerrit Uitslag
832d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
833d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
834d63e7fe7SGerrit Uitslag                array('export_pdf' =>
835d63e7fe7SGerrit Uitslag                          '<li>'
83672cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
837d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
838d63e7fe7SGerrit Uitslag                          . '</a>'
839d63e7fe7SGerrit Uitslag                          . '</li>'
840d63e7fe7SGerrit Uitslag                ) +
841d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
842d63e7fe7SGerrit Uitslag        }
843d63e7fe7SGerrit Uitslag    }
844ee19bac3SLuigi Micco}
845