xref: /plugin/dw2pdf/action.php (revision e5f6c2cbb60d31f39f1afb5fb6ccbe48678833a8)
1ee19bac3SLuigi Micco<?php
2ee19bac3SLuigi Micco/**
3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4ee19bac3SLuigi Micco *
5ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
75db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8ee19bac3SLuigi Micco */
9ee19bac3SLuigi Micco
10ee19bac3SLuigi Micco// must be run within Dokuwiki
11ee19bac3SLuigi Miccoif(!defined('DOKU_INC')) die();
12ee19bac3SLuigi Micco
130639157eSGerrit Uitslag/**
140639157eSGerrit Uitslag * Class action_plugin_dw2pdf
150639157eSGerrit Uitslag *
160639157eSGerrit Uitslag * Export hmtl content to pdf, for different url parameter configurations
170639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html.
180639157eSGerrit Uitslag */
191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
2002f9a447SGerrit Uitslag    /**
2102f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2202f9a447SGerrit Uitslag     *
2302f9a447SGerrit Uitslag     * @var array
2402f9a447SGerrit Uitslag     */
25213fdb75SGerrit Uitslag    protected $exportConfig = null;
2660e59de7SGerrit Uitslag    protected $tpl;
2760e59de7SGerrit Uitslag    protected $list = array();
281c14c879SAndreas Gohr
291c14c879SAndreas Gohr    /**
301c14c879SAndreas Gohr     * Constructor. Sets the correct template
311c14c879SAndreas Gohr     */
326be736bfSGerrit Uitslag    public function __construct() {
3302f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
341c14c879SAndreas Gohr    }
351c14c879SAndreas Gohr
36ee19bac3SLuigi Micco    /**
37ee19bac3SLuigi Micco     * Register the events
38177a7d30SGerrit Uitslag     *
39177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
40ee19bac3SLuigi Micco     */
416be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
42ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
436be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
44ee19bac3SLuigi Micco    }
45ee19bac3SLuigi Micco
461c14c879SAndreas Gohr    /**
471c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
48737417c6SKlap-in     *
49737417c6SKlap-in     * @param Doku_Event $event
50737417c6SKlap-in     * @return bool
511c14c879SAndreas Gohr     */
5244e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
53ee19bac3SLuigi Micco        global $ACT;
54ee19bac3SLuigi Micco        global $ID;
55ee19bac3SLuigi Micco
561ef68647SAndreas Gohr        // our event?
57ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false;
58ee19bac3SLuigi Micco
591ef68647SAndreas Gohr        // check user's rights
601ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
611ef68647SAndreas Gohr
62d63e7fe7SGerrit Uitslag        if($data = $this->collectExportPages($event)) {
63d63e7fe7SGerrit Uitslag            list($title, $this->list) = $data;
64d63e7fe7SGerrit Uitslag        } else {
65d63e7fe7SGerrit Uitslag            return false;
66d63e7fe7SGerrit Uitslag        }
67d63e7fe7SGerrit Uitslag
68d63e7fe7SGerrit Uitslag        // it's ours, no one else's
69d63e7fe7SGerrit Uitslag        $event->preventDefault();
70d63e7fe7SGerrit Uitslag
71a58f45f0SGerrit Uitslag        // prepare cache and its dependencies
72a58f45f0SGerrit Uitslag        $depends = array();
73a58f45f0SGerrit Uitslag        $cache = $this->prepareCache($title, $depends);
74d63e7fe7SGerrit Uitslag
75d63e7fe7SGerrit Uitslag        // hard work only when no cache available
76d63e7fe7SGerrit Uitslag        if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
77*e5f6c2cbSMichael Große            // generating the pdf may take a long time for larger wikis / namespaces with many pages
78*e5f6c2cbSMichael Große            set_time_limit(0);
79*e5f6c2cbSMichael 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);
207d63e7fe7SGerrit Uitslag        return array($title, $list);
208d63e7fe7SGerrit Uitslag    }
209d63e7fe7SGerrit Uitslag
210a58f45f0SGerrit Uitslag    /**
211a58f45f0SGerrit Uitslag     * Prepare cache
212a58f45f0SGerrit Uitslag     *
213a58f45f0SGerrit Uitslag     * @param string $title
214a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
215a58f45f0SGerrit Uitslag     * @return cache
216a58f45f0SGerrit Uitslag     */
217a58f45f0SGerrit Uitslag    protected function prepareCache($title, &$depends) {
218a58f45f0SGerrit Uitslag        global $REV;
219a58f45f0SGerrit Uitslag
220ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
221ee19bac3SLuigi Micco            . $REV
222ee19bac3SLuigi Micco            . $this->getExportConfig('template')
223ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
224ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
225ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
226ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
227ee19bac3SLuigi Micco            . $title;
228ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
229ee19bac3SLuigi Micco
230ee19bac3SLuigi Micco        $dependencies = array();
231ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
232ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
233ee19bac3SLuigi Micco
234ee19bac3SLuigi Micco            if(is_array($relations)) {
235ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
236ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
237ee19bac3SLuigi Micco                        if($exists) {
238ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
239ee19bac3SLuigi Micco                        }
240ee19bac3SLuigi Micco                    }
241ee19bac3SLuigi Micco                }
242ee19bac3SLuigi Micco
243ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
244ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
245ee19bac3SLuigi Micco                        if($exists) {
246ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
247ee19bac3SLuigi Micco                        }
248ee19bac3SLuigi Micco                    }
249ee19bac3SLuigi Micco                }
250ee19bac3SLuigi Micco            }
251ee19bac3SLuigi Micco
252ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
253ee19bac3SLuigi Micco        }
254ee19bac3SLuigi Micco
255ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
256ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
257ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
258ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
259ee19bac3SLuigi Micco        $depends['files'] = array_merge(
260ee19bac3SLuigi Micco            $depends['files'],
261ee19bac3SLuigi Micco            $dependencies,
262ee19bac3SLuigi Micco            getConfigFiles('main')
263ee19bac3SLuigi Micco        );
264a58f45f0SGerrit Uitslag        return $cache;
265ee19bac3SLuigi Micco    }
266ee19bac3SLuigi Micco
267d63e7fe7SGerrit Uitslag    /**
268d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
269d63e7fe7SGerrit Uitslag     *
270d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
271d63e7fe7SGerrit Uitslag     * @param string     $msglangkey key of translation key
272d63e7fe7SGerrit Uitslag     */
273d63e7fe7SGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey) {
274d63e7fe7SGerrit Uitslag        msg($this->getLang($msglangkey), -1);
275d63e7fe7SGerrit Uitslag
276d63e7fe7SGerrit Uitslag        $event->data = 'show';
277d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
278d63e7fe7SGerrit Uitslag    }
279d63e7fe7SGerrit Uitslag
280d63e7fe7SGerrit Uitslag    /**
281d63e7fe7SGerrit Uitslag     * Build a pdf from the html
282d63e7fe7SGerrit Uitslag     *
283d63e7fe7SGerrit Uitslag     * @param string $cachefile
284d63e7fe7SGerrit Uitslag     * @param string $title
285d63e7fe7SGerrit Uitslag     */
286d63e7fe7SGerrit Uitslag    protected function generatePDF($cachefile, $title) {
287d63e7fe7SGerrit Uitslag        global $ID;
288d63e7fe7SGerrit Uitslag        global $REV;
289d63e7fe7SGerrit Uitslag        global $INPUT;
29087c86ddaSAndreas Gohr
29102f9a447SGerrit Uitslag        //some shortcuts to export settings
29202f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
29302f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
29402f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
2956ea88a05SAndreas Gohr
2961ef68647SAndreas Gohr        // initialize PDF library
297cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
2986ea88a05SAndreas Gohr
29902f9a447SGerrit Uitslag        $mpdf = new DokuPDF($this->getExportConfig('pagesize'), $this->getExportConfig('orientation'));
300ee19bac3SLuigi Micco
301d62df65bSAndreas Gohr        // let mpdf fix local links
302d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
303d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
30402f9a447SGerrit Uitslag        if($self['port']) {
30502f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
30602f9a447SGerrit Uitslag        }
307d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
308d62df65bSAndreas Gohr
30956d13144SAndreas Gohr        // Set the title
31056d13144SAndreas Gohr        $mpdf->SetTitle($title);
31156d13144SAndreas Gohr
312d63e7fe7SGerrit Uitslag        // some default document settings
313d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
314213fdb75SGerrit Uitslag        //      single-side document has only odd pages
315213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
316daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
317daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
31802f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
31902f9a447SGerrit Uitslag        if($hasToC) {
32002f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
32102f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
32202f9a447SGerrit Uitslag        } else {
32302f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
32402f9a447SGerrit Uitslag        }
3252eedf77dSAndreas Gohr
32656d13144SAndreas Gohr        // load the template
3271c14c879SAndreas Gohr        $template = $this->load_template($title);
328ee19bac3SLuigi Micco
3291ef68647SAndreas Gohr        // prepare HTML header styles
330a2c33768SGerrit Uitslag        $html = '';
33102f9a447SGerrit Uitslag        if($isDebug) {
332a2c33768SGerrit Uitslag            $html .= '<html><head>';
333737417c6SKlap-in            $html .= '<style type="text/css">';
334a2c33768SGerrit Uitslag        }
335a2c33768SGerrit Uitslag        $styles = $this->load_css();
336a2c33768SGerrit Uitslag        $styles .= '@page { size:auto; ' . $template['page'] . '}';
337a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
338254467c4SGerrit Uitslag
339254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
340254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
341254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
342254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
343254467c4SGerrit Uitslag
344a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
345a2c33768SGerrit Uitslag
34602f9a447SGerrit Uitslag        if($isDebug) {
347a2c33768SGerrit Uitslag            $html .= $styles;
3481ef68647SAndreas Gohr            $html .= '</style>';
3491ef68647SAndreas Gohr            $html .= '</head><body>';
350a2c33768SGerrit Uitslag        }
351a2c33768SGerrit Uitslag
352a2c33768SGerrit Uitslag        $body_start = $template['html'];
353a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3542eedf77dSAndreas Gohr
3551e45476bSmnapp        // insert the cover page
356a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
357a2c33768SGerrit Uitslag
358a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
35902f9a447SGerrit Uitslag        if($isDebug) {
360a2c33768SGerrit Uitslag            $html .= $body_start;
361a2c33768SGerrit Uitslag        }
36202f9a447SGerrit Uitslag        if($hasToC) {
36302f9a447SGerrit 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
36402f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
36502f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
36602f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
36702f9a447SGerrit Uitslag                array(
368230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
369230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
37002f9a447SGerrit Uitslag                    'links' => true,
37102f9a447SGerrit Uitslag                    'outdent' => '1em',
37202f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
37302f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
37402f9a447SGerrit Uitslag                )
37502f9a447SGerrit Uitslag            );
37602f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
37702f9a447SGerrit Uitslag        }
37802f9a447SGerrit Uitslag
379c00eb13bSGerrit Uitslag        // store original pageid
380c00eb13bSGerrit Uitslag        $keep = $ID;
381c00eb13bSGerrit Uitslag
3821ef68647SAndreas Gohr        // loop over all pages
383a2c33768SGerrit Uitslag        $cnt = count($this->list);
3841ef68647SAndreas Gohr        for($n = 0; $n < $cnt; $n++) {
385a2c33768SGerrit Uitslag            $page = $this->list[$n];
386b3eed6e3SGerrit Uitslag            $filename = wikiFN($page, $REV);
387b3eed6e3SGerrit Uitslag
388b3eed6e3SGerrit Uitslag            if(!file_exists($filename)) {
389b3eed6e3SGerrit Uitslag                continue;
390b3eed6e3SGerrit Uitslag            }
391ee19bac3SLuigi Micco
392c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
393c00eb13bSGerrit Uitslag            $ID = $page;
394c00eb13bSGerrit Uitslag
395b3eed6e3SGerrit Uitslag            $pagehtml = p_cached_output($filename, 'dw2pdf', $page);
396719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
3971ef68647SAndreas Gohr            if($n < ($cnt - 1)) {
398a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
399a2c33768SGerrit Uitslag            }
400a2c33768SGerrit Uitslag
401a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
40202f9a447SGerrit Uitslag            if($isDebug) {
403a2c33768SGerrit Uitslag                $html .= $pagehtml;
4041ef68647SAndreas Gohr            }
405ee19bac3SLuigi Micco        }
406c00eb13bSGerrit Uitslag        //restore ID
407c00eb13bSGerrit Uitslag        $ID = $keep;
408ee19bac3SLuigi Micco
40933c15297SGerrit Uitslag        // insert the back page
410a2c33768SGerrit Uitslag        $body_end = $template['back'];
41133c15297SGerrit Uitslag
412a2c33768SGerrit Uitslag        $body_end .= '</div>';
413a2c33768SGerrit Uitslag
414d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
41502f9a447SGerrit Uitslag        if($isDebug) {
416a2c33768SGerrit Uitslag            $html .= $body_end;
417eeb17e15SAndreas Gohr            $html .= '</body>';
418eeb17e15SAndreas Gohr            $html .= '</html>';
419a2c33768SGerrit Uitslag        }
420f765508eSGerrit Uitslag
421f765508eSGerrit Uitslag        //Return html for debugging
42202f9a447SGerrit Uitslag        if($isDebug) {
42302f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
42426be4eceSGerrit Uitslag                echo $html;
425a2c33768SGerrit Uitslag            } else {
426a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
427a2c33768SGerrit Uitslag                echo $html;
428a2c33768SGerrit Uitslag            }
42926be4eceSGerrit Uitslag            exit();
43026be4eceSGerrit Uitslag        };
431f765508eSGerrit Uitslag
43287c86ddaSAndreas Gohr        // write to cache file
433d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
43487c86ddaSAndreas Gohr    }
43587c86ddaSAndreas Gohr
436d63e7fe7SGerrit Uitslag    /**
437d63e7fe7SGerrit Uitslag     * @param string $cachefile
438d63e7fe7SGerrit Uitslag     * @param string $title
439d63e7fe7SGerrit Uitslag     */
440d63e7fe7SGerrit Uitslag    protected function sendPDFFile($cachefile, $title) {
44187c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
442b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
44387c86ddaSAndreas Gohr        header('Pragma: public');
444d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
44587c86ddaSAndreas Gohr
4469a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
44787c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4489a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
44987c86ddaSAndreas Gohr        } else {
4509a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
45187c86ddaSAndreas Gohr        }
452ee19bac3SLuigi Micco
453b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
454b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
455b3eed6e3SGerrit Uitslag
456e993da11SGerrit Uitslag        //try to send file, and exit if done
457d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
45887c86ddaSAndreas Gohr
459d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
46087c86ddaSAndreas Gohr        if($fp) {
461d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
46287c86ddaSAndreas Gohr        } else {
46387c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
46487c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
46587c86ddaSAndreas Gohr        }
4661ef68647SAndreas Gohr        exit();
4671ef68647SAndreas Gohr    }
4681ef68647SAndreas Gohr
4696be736bfSGerrit Uitslag    /**
4702eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
47144e8e8fbSGerrit Uitslag     *
47244e8e8fbSGerrit Uitslag     * @param string $title
47344e8e8fbSGerrit Uitslag     * @return array
4741ef68647SAndreas Gohr     */
4751c14c879SAndreas Gohr    protected function load_template($title) {
4761ef68647SAndreas Gohr        global $ID;
4771ef68647SAndreas Gohr        global $conf;
4781ef68647SAndreas Gohr
4792eedf77dSAndreas Gohr        // this is what we'll return
4802eedf77dSAndreas Gohr        $output = array(
4811e45476bSmnapp            'cover' => '',
4822eedf77dSAndreas Gohr            'html'  => '',
4832eedf77dSAndreas Gohr            'page'  => '',
4842eedf77dSAndreas Gohr            'first' => '',
4852eedf77dSAndreas Gohr            'cite'  => '',
4862eedf77dSAndreas Gohr        );
4872eedf77dSAndreas Gohr
4882eedf77dSAndreas Gohr        // prepare header/footer elements
4892eedf77dSAndreas Gohr        $html = '';
49033c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
49133c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
49202f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
49333c15297SGerrit Uitslag                if(file_exists($file)) {
49433c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
49533c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
49633c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
4972eedf77dSAndreas Gohr
4982eedf77dSAndreas Gohr                    // register the needed pseudo CSS
49933c15297SGerrit Uitslag                    if($order == '_first') {
50033c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
50133c15297SGerrit Uitslag                    } elseif($order == '_even') {
50233c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
50333c15297SGerrit Uitslag                    } elseif($order == '_odd') {
50433c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
505daa70883SAndreas Gohr                    } else {
50633c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5072eedf77dSAndreas Gohr                    }
5082eedf77dSAndreas Gohr                }
5092eedf77dSAndreas Gohr            }
5102eedf77dSAndreas Gohr        }
5112eedf77dSAndreas Gohr
5121ef68647SAndreas Gohr        // prepare replacements
5131ef68647SAndreas Gohr        $replace = array(
5141ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
51502f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
5162eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
5171ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5181ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5191ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5205d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
52102f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5221ef68647SAndreas Gohr        );
5231ef68647SAndreas Gohr
5242eedf77dSAndreas Gohr        // set HTML element
525a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
526a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
527a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5281ef68647SAndreas Gohr
5291e45476bSmnapp        // cover page
53002f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
53133c15297SGerrit Uitslag        if(file_exists($coverfile)) {
53233c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5331e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5349b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5356e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5361e45476bSmnapp        }
5371e45476bSmnapp
53833c15297SGerrit Uitslag        // cover page
53902f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
54033c15297SGerrit Uitslag        if(file_exists($backfile)) {
54133c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
54233c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
54333c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5449b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
54533c15297SGerrit Uitslag        }
54633c15297SGerrit Uitslag
5472eedf77dSAndreas Gohr        // citation box
54802f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
54933c15297SGerrit Uitslag        if(file_exists($citationfile)) {
55033c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5512eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5522eedf77dSAndreas Gohr        }
5531ef68647SAndreas Gohr
5542eedf77dSAndreas Gohr        return $output;
5551ef68647SAndreas Gohr    }
5561ef68647SAndreas Gohr
5571ef68647SAndreas Gohr    /**
558a180c973SKlap-in     * @param string $raw code with placeholders
559a180c973SKlap-in     * @param string $id  pageid
560a180c973SKlap-in     * @return string
561a180c973SKlap-in     */
562a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
563a180c973SKlap-in        global $REV;
564a180c973SKlap-in
565a180c973SKlap-in        // generate qr code for this page using google infographics api
566a180c973SKlap-in        $qr_code = '';
567a180c973SKlap-in        if($this->getConf('qrcodesize')) {
568a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
569a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
570a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
571a180c973SKlap-in        }
572a180c973SKlap-in        // prepare replacements
573a180c973SKlap-in        $replace['@ID@']      = $id;
574a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
575a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
576a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
577a180c973SKlap-in
578e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
579e3d68265SGerrit Uitslag
580e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
581e3d68265SGerrit Uitslag        $content = preg_replace_callback(
582e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
583e3d68265SGerrit Uitslag            array($this, 'replacedate'),
584e3d68265SGerrit Uitslag            $content
585e3d68265SGerrit Uitslag        );
586e3d68265SGerrit Uitslag
587e3d68265SGerrit Uitslag        return $content;
588a180c973SKlap-in    }
589a180c973SKlap-in
590e3d68265SGerrit Uitslag
591e3d68265SGerrit Uitslag    /**
592e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
593e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
594e3d68265SGerrit Uitslag     *
595e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
596e3d68265SGerrit Uitslag     * @return string
597e3d68265SGerrit Uitslag     */
598e3d68265SGerrit Uitslag    function replacedate($match) {
599e3d68265SGerrit Uitslag        global $conf;
600e3d68265SGerrit Uitslag        //no 2nd argument for default date format
601e3d68265SGerrit Uitslag        if($match[2] == null) {
602e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
603e3d68265SGerrit Uitslag        }
604e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
605e3d68265SGerrit Uitslag    }
606e3d68265SGerrit Uitslag
607e3d68265SGerrit Uitslag
608a180c973SKlap-in    /**
6091c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6101ef68647SAndreas Gohr     */
6111c14c879SAndreas Gohr    protected function load_css() {
612737417c6SKlap-in        global $conf;
6131c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6141c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6151c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
616ee19bac3SLuigi Micco
6171c14c879SAndreas Gohr        // prepare CSS files
6181c14c879SAndreas Gohr        $files = array_merge(
6191c14c879SAndreas Gohr            array(
6201c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6211c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6221c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6231c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6241c14c879SAndreas Gohr            ),
6251c14c879SAndreas Gohr            css_pluginstyles('all'),
62658e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6271c14c879SAndreas Gohr            array(
6281c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6291c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6301c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6311c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6321c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6331c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6341c14c879SAndreas Gohr            )
6351c14c879SAndreas Gohr        );
6361c14c879SAndreas Gohr        $css = '';
6371c14c879SAndreas Gohr        foreach($files as $file => $location) {
63828e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
63928e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6401c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6411ef68647SAndreas Gohr        }
6421ef68647SAndreas Gohr
64328e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6441c14c879SAndreas Gohr            // apply pattern replacements
64528e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
64628e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
64728e636eaSGerrit Uitslag
64828e636eaSGerrit Uitslag            // parse less
64928e636eaSGerrit Uitslag            $css = css_parseless($css);
65028e636eaSGerrit Uitslag        } else {
65128e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6521c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
65328e636eaSGerrit Uitslag        }
6541ef68647SAndreas Gohr
6551c14c879SAndreas Gohr        return $css;
656ee19bac3SLuigi Micco    }
6571c14c879SAndreas Gohr
65858e6409eSAndreas Gohr    /**
65958e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
66058e6409eSAndreas Gohr     *
66158e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
66258e6409eSAndreas Gohr     *
66358e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
66458e6409eSAndreas Gohr     */
6656be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
66658e6409eSAndreas Gohr        $list = array();
66758e6409eSAndreas Gohr        $plugins = plugin_list();
668f54b51f7SAndreas Gohr
669f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
67058e6409eSAndreas Gohr        foreach($plugins as $p) {
671f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
672f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
673f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
674f54b51f7SAndreas Gohr            }
675f54b51f7SAndreas Gohr
67658e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
67758e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
67858e6409eSAndreas Gohr            } else {
67958e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
68058e6409eSAndreas Gohr            }
68158e6409eSAndreas Gohr        }
68258e6409eSAndreas Gohr        return $list;
68358e6409eSAndreas Gohr    }
684ad18f4e1SGerrit Uitslag
685ad18f4e1SGerrit Uitslag    /**
68660e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
68760e59de7SGerrit Uitslag     *
68860e59de7SGerrit Uitslag     * @return array
68960e59de7SGerrit Uitslag     */
69060e59de7SGerrit Uitslag    public function getExportedPages() {
69160e59de7SGerrit Uitslag        return $this->list;
69260e59de7SGerrit Uitslag    }
69360e59de7SGerrit Uitslag
69460e59de7SGerrit Uitslag    /**
695ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
69644e8e8fbSGerrit Uitslag     *
69744e8e8fbSGerrit Uitslag     * @param array $a
69844e8e8fbSGerrit Uitslag     * @param array $b
69944e8e8fbSGerrit Uitslag     * @return int
700ad18f4e1SGerrit Uitslag     */
701ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
702ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
703ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
704ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
705ad18f4e1SGerrit Uitslag    }
706ad18f4e1SGerrit Uitslag
707ad18f4e1SGerrit Uitslag    /**
708ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
70944e8e8fbSGerrit Uitslag     * @param array $a
71044e8e8fbSGerrit Uitslag     * @param array $b
71144e8e8fbSGerrit Uitslag     * @return int
712ad18f4e1SGerrit Uitslag     */
713ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
714ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
715ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
716ad18f4e1SGerrit Uitslag        return 0;
717ad18f4e1SGerrit Uitslag    }
71826be4eceSGerrit Uitslag
71926be4eceSGerrit Uitslag    /**
72002f9a447SGerrit Uitslag     * Return settings read from:
72102f9a447SGerrit Uitslag     *   1. url parameters
72202f9a447SGerrit Uitslag     *   2. plugin config
72302f9a447SGerrit Uitslag     *   3. global config
72402f9a447SGerrit Uitslag     *
72502f9a447SGerrit Uitslag     * @return array
72602f9a447SGerrit Uitslag     */
72702f9a447SGerrit Uitslag    protected function loadExportConfig() {
72802f9a447SGerrit Uitslag        global $INPUT;
72902f9a447SGerrit Uitslag        global $conf;
73002f9a447SGerrit Uitslag
73102f9a447SGerrit Uitslag        $this->exportConfig = array();
73202f9a447SGerrit Uitslag
73302f9a447SGerrit Uitslag        // decide on the paper setup from param or config
73402f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
73502f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
73602f9a447SGerrit Uitslag
737213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
738213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
739213fdb75SGerrit Uitslag
740213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
74102f9a447SGerrit Uitslag        $levels = array();
74202f9a447SGerrit Uitslag        if($hasToC) {
74302f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
74402f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
74502f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
74602f9a447SGerrit Uitslag            $bounds_input = array(
74702f9a447SGerrit Uitslag                'top' => array(
74802f9a447SGerrit Uitslag                    (int) $top_input,
74902f9a447SGerrit Uitslag                    (int) $top_conf
75002f9a447SGerrit Uitslag                ),
75102f9a447SGerrit Uitslag                'max' => array(
75202f9a447SGerrit Uitslag                    (int) $max_input,
75302f9a447SGerrit Uitslag                    (int) $max_conf
75402f9a447SGerrit Uitslag                )
75502f9a447SGerrit Uitslag            );
75602f9a447SGerrit Uitslag            $bounds = array(
75702f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
75802f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
75902f9a447SGerrit Uitslag
76002f9a447SGerrit Uitslag            );
76102f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
76202f9a447SGerrit Uitslag                foreach($values as $value) {
76302f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
76402f9a447SGerrit Uitslag                        //stop at valid value and store
76502f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
76602f9a447SGerrit Uitslag                        break;
76702f9a447SGerrit Uitslag                    }
76802f9a447SGerrit Uitslag                }
76902f9a447SGerrit Uitslag            }
77002f9a447SGerrit Uitslag
77102f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
77202f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
77302f9a447SGerrit Uitslag            }
77402f9a447SGerrit Uitslag
77502f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
77602f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
77702f9a447SGerrit Uitslag            }
77802f9a447SGerrit Uitslag        }
77902f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
78002f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
78102f9a447SGerrit Uitslag
78202f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
78302f9a447SGerrit Uitslag
78402f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
78505d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
78602f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
78702f9a447SGerrit Uitslag            $tpl = $tplconf;
78802f9a447SGerrit Uitslag        }
78902f9a447SGerrit Uitslag        if(!$tpl){
79002f9a447SGerrit Uitslag            $tpl = 'default';
79102f9a447SGerrit Uitslag        }
79202f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
79302f9a447SGerrit Uitslag
79402f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
79502f9a447SGerrit Uitslag    }
79602f9a447SGerrit Uitslag
79702f9a447SGerrit Uitslag    /**
79802f9a447SGerrit Uitslag     * Returns requested config
79902f9a447SGerrit Uitslag     *
80002f9a447SGerrit Uitslag     * @param string $name
80102f9a447SGerrit Uitslag     * @param mixed  $notset
80202f9a447SGerrit Uitslag     * @return mixed|bool
80302f9a447SGerrit Uitslag     */
80402f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
80502f9a447SGerrit Uitslag        if ($this->exportConfig === null){
80602f9a447SGerrit Uitslag            $this->loadExportConfig();
80702f9a447SGerrit Uitslag        }
80802f9a447SGerrit Uitslag
80902f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
81002f9a447SGerrit Uitslag            return $this->exportConfig[$name];
81102f9a447SGerrit Uitslag        }else{
81202f9a447SGerrit Uitslag            return $notset;
81302f9a447SGerrit Uitslag        }
81402f9a447SGerrit Uitslag    }
815d63e7fe7SGerrit Uitslag
816d63e7fe7SGerrit Uitslag    /**
817d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
818d63e7fe7SGerrit Uitslag     *
819d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
820d63e7fe7SGerrit Uitslag     */
82144e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
822d63e7fe7SGerrit Uitslag        global $ID, $REV;
823d63e7fe7SGerrit Uitslag
824d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
825d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
826d63e7fe7SGerrit Uitslag            if($REV) {
827d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
828d63e7fe7SGerrit Uitslag            }
829d63e7fe7SGerrit Uitslag
830d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
831d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
832d63e7fe7SGerrit Uitslag                array('export_pdf' =>
833d63e7fe7SGerrit Uitslag                          '<li>'
83472cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
835d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
836d63e7fe7SGerrit Uitslag                          . '</a>'
837d63e7fe7SGerrit Uitslag                          . '</li>'
838d63e7fe7SGerrit Uitslag                ) +
839d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
840d63e7fe7SGerrit Uitslag        }
841d63e7fe7SGerrit Uitslag    }
842ee19bac3SLuigi Micco}
843