xref: /plugin/dw2pdf/action.php (revision 9bcbb4f044d7fe60e5bb93616e366896646bdab0)
1ee19bac3SLuigi Micco<?php
2ee19bac3SLuigi Micco/**
3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4ee19bac3SLuigi Micco *
5ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
75db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8ee19bac3SLuigi Micco */
9ee19bac3SLuigi Micco
10ee19bac3SLuigi Micco// must be run within Dokuwiki
11ee19bac3SLuigi Miccoif(!defined('DOKU_INC')) die();
12ee19bac3SLuigi Micco
130639157eSGerrit Uitslag/**
140639157eSGerrit Uitslag * Class action_plugin_dw2pdf
150639157eSGerrit Uitslag *
160639157eSGerrit Uitslag * Export hmtl content to pdf, for different url parameter configurations
170639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html.
180639157eSGerrit Uitslag */
191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
2002f9a447SGerrit Uitslag    /**
2102f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2202f9a447SGerrit Uitslag     *
2302f9a447SGerrit Uitslag     * @var array
2402f9a447SGerrit Uitslag     */
25213fdb75SGerrit Uitslag    protected $exportConfig = null;
2660e59de7SGerrit Uitslag    protected $tpl;
2760e59de7SGerrit Uitslag    protected $list = array();
281c14c879SAndreas Gohr
291c14c879SAndreas Gohr    /**
301c14c879SAndreas Gohr     * Constructor. Sets the correct template
311c14c879SAndreas Gohr     */
326be736bfSGerrit Uitslag    public function __construct() {
3302f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
341c14c879SAndreas Gohr    }
351c14c879SAndreas Gohr
36ee19bac3SLuigi Micco    /**
37ee19bac3SLuigi Micco     * Register the events
38177a7d30SGerrit Uitslag     *
39177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
40ee19bac3SLuigi Micco     */
416be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
42ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
436be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
44ee19bac3SLuigi Micco    }
45ee19bac3SLuigi Micco
461c14c879SAndreas Gohr    /**
471c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
48737417c6SKlap-in     *
49737417c6SKlap-in     * @param Doku_Event $event
50737417c6SKlap-in     * @return bool
511c14c879SAndreas Gohr     */
5244e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
53ee19bac3SLuigi Micco        global $ACT;
54ee19bac3SLuigi Micco        global $ID;
55ee19bac3SLuigi Micco
561ef68647SAndreas Gohr        // our event?
57ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false;
58ee19bac3SLuigi Micco
591ef68647SAndreas Gohr        // check user's rights
601ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
611ef68647SAndreas Gohr
62d63e7fe7SGerrit Uitslag        if($data = $this->collectExportPages($event)) {
63d63e7fe7SGerrit Uitslag            list($title, $this->list) = $data;
64d63e7fe7SGerrit Uitslag        } else {
65d63e7fe7SGerrit Uitslag            return false;
66d63e7fe7SGerrit Uitslag        }
67d63e7fe7SGerrit Uitslag
68d63e7fe7SGerrit Uitslag        // it's ours, no one else's
69d63e7fe7SGerrit Uitslag        $event->preventDefault();
70d63e7fe7SGerrit Uitslag
71a58f45f0SGerrit Uitslag        // prepare cache and its dependencies
72a58f45f0SGerrit Uitslag        $depends = array();
73a58f45f0SGerrit Uitslag        $cache = $this->prepareCache($title, $depends);
74d63e7fe7SGerrit Uitslag
75*9bcbb4f0SGerrit Uitslag        // hard work only when no cache available or needed for debugging
76*9bcbb4f0SGerrit 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*9bcbb4f0SGerrit Uitslag          error_log('isDebug:'.($isDebug ? 'yes' : 'no'));
420f765508eSGerrit Uitslag        //Return html for debugging
42102f9a447SGerrit Uitslag        if($isDebug) {
422*9bcbb4f0SGerrit Uitslag            error_log('entered');
42302f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
424*9bcbb4f0SGerrit Uitslag                error_log('enteredhtml');
425*9bcbb4f0SGerrit Uitslag
42626be4eceSGerrit Uitslag                echo $html;
427a2c33768SGerrit Uitslag            } else {
428a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
429*9bcbb4f0SGerrit Uitslag                error_log('enteredtext');
430*9bcbb4f0SGerrit Uitslag
431a2c33768SGerrit Uitslag                echo $html;
432a2c33768SGerrit Uitslag            }
43326be4eceSGerrit Uitslag            exit();
43426be4eceSGerrit Uitslag        };
435*9bcbb4f0SGerrit Uitslag             error_log('to caching no exit..');
43687c86ddaSAndreas Gohr        // write to cache file
437d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
43887c86ddaSAndreas Gohr    }
43987c86ddaSAndreas Gohr
440d63e7fe7SGerrit Uitslag    /**
441d63e7fe7SGerrit Uitslag     * @param string $cachefile
442d63e7fe7SGerrit Uitslag     * @param string $title
443d63e7fe7SGerrit Uitslag     */
444d63e7fe7SGerrit Uitslag    protected function sendPDFFile($cachefile, $title) {
44587c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
446b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
44787c86ddaSAndreas Gohr        header('Pragma: public');
448d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
44987c86ddaSAndreas Gohr
4509a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
45187c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4529a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
45387c86ddaSAndreas Gohr        } else {
4549a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
45587c86ddaSAndreas Gohr        }
456ee19bac3SLuigi Micco
457b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
458b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
459b3eed6e3SGerrit Uitslag
460e993da11SGerrit Uitslag        //try to send file, and exit if done
461d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
46287c86ddaSAndreas Gohr
463d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
46487c86ddaSAndreas Gohr        if($fp) {
465d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
46687c86ddaSAndreas Gohr        } else {
46787c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
46887c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
46987c86ddaSAndreas Gohr        }
4701ef68647SAndreas Gohr        exit();
4711ef68647SAndreas Gohr    }
4721ef68647SAndreas Gohr
4736be736bfSGerrit Uitslag    /**
4742eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
47544e8e8fbSGerrit Uitslag     *
47644e8e8fbSGerrit Uitslag     * @param string $title
47744e8e8fbSGerrit Uitslag     * @return array
4781ef68647SAndreas Gohr     */
4791c14c879SAndreas Gohr    protected function load_template($title) {
4801ef68647SAndreas Gohr        global $ID;
4811ef68647SAndreas Gohr        global $conf;
4821ef68647SAndreas Gohr
4832eedf77dSAndreas Gohr        // this is what we'll return
4842eedf77dSAndreas Gohr        $output = array(
4851e45476bSmnapp            'cover' => '',
4862eedf77dSAndreas Gohr            'html'  => '',
4872eedf77dSAndreas Gohr            'page'  => '',
4882eedf77dSAndreas Gohr            'first' => '',
4892eedf77dSAndreas Gohr            'cite'  => '',
4902eedf77dSAndreas Gohr        );
4912eedf77dSAndreas Gohr
4922eedf77dSAndreas Gohr        // prepare header/footer elements
4932eedf77dSAndreas Gohr        $html = '';
49433c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
49533c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
49602f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
49733c15297SGerrit Uitslag                if(file_exists($file)) {
49833c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
49933c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
50033c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
5012eedf77dSAndreas Gohr
5022eedf77dSAndreas Gohr                    // register the needed pseudo CSS
50333c15297SGerrit Uitslag                    if($order == '_first') {
50433c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
50533c15297SGerrit Uitslag                    } elseif($order == '_even') {
50633c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
50733c15297SGerrit Uitslag                    } elseif($order == '_odd') {
50833c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
509daa70883SAndreas Gohr                    } else {
51033c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5112eedf77dSAndreas Gohr                    }
5122eedf77dSAndreas Gohr                }
5132eedf77dSAndreas Gohr            }
5142eedf77dSAndreas Gohr        }
5152eedf77dSAndreas Gohr
5161ef68647SAndreas Gohr        // prepare replacements
5171ef68647SAndreas Gohr        $replace = array(
5181ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
51902f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
5202eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
5211ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5221ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5231ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5245d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
52502f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5261ef68647SAndreas Gohr        );
5271ef68647SAndreas Gohr
5282eedf77dSAndreas Gohr        // set HTML element
529a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
530a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
531a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5321ef68647SAndreas Gohr
5331e45476bSmnapp        // cover page
53402f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
53533c15297SGerrit Uitslag        if(file_exists($coverfile)) {
53633c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5371e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5389b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5396e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5401e45476bSmnapp        }
5411e45476bSmnapp
54233c15297SGerrit Uitslag        // cover page
54302f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
54433c15297SGerrit Uitslag        if(file_exists($backfile)) {
54533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
54633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
54733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5489b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
54933c15297SGerrit Uitslag        }
55033c15297SGerrit Uitslag
5512eedf77dSAndreas Gohr        // citation box
55202f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
55333c15297SGerrit Uitslag        if(file_exists($citationfile)) {
55433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5552eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5562eedf77dSAndreas Gohr        }
5571ef68647SAndreas Gohr
5582eedf77dSAndreas Gohr        return $output;
5591ef68647SAndreas Gohr    }
5601ef68647SAndreas Gohr
5611ef68647SAndreas Gohr    /**
562a180c973SKlap-in     * @param string $raw code with placeholders
563a180c973SKlap-in     * @param string $id  pageid
564a180c973SKlap-in     * @return string
565a180c973SKlap-in     */
566a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
567a180c973SKlap-in        global $REV;
568a180c973SKlap-in
569a180c973SKlap-in        // generate qr code for this page using google infographics api
570a180c973SKlap-in        $qr_code = '';
571a180c973SKlap-in        if($this->getConf('qrcodesize')) {
572a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
573a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
574a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
575a180c973SKlap-in        }
576a180c973SKlap-in        // prepare replacements
577a180c973SKlap-in        $replace['@ID@']      = $id;
578a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
579a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
580a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
581a180c973SKlap-in
582e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
583e3d68265SGerrit Uitslag
584e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
585e3d68265SGerrit Uitslag        $content = preg_replace_callback(
586e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
587e3d68265SGerrit Uitslag            array($this, 'replacedate'),
588e3d68265SGerrit Uitslag            $content
589e3d68265SGerrit Uitslag        );
590e3d68265SGerrit Uitslag
591e3d68265SGerrit Uitslag        return $content;
592a180c973SKlap-in    }
593a180c973SKlap-in
594e3d68265SGerrit Uitslag
595e3d68265SGerrit Uitslag    /**
596e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
597e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
598e3d68265SGerrit Uitslag     *
599e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
600e3d68265SGerrit Uitslag     * @return string
601e3d68265SGerrit Uitslag     */
602e3d68265SGerrit Uitslag    function replacedate($match) {
603e3d68265SGerrit Uitslag        global $conf;
604e3d68265SGerrit Uitslag        //no 2nd argument for default date format
605e3d68265SGerrit Uitslag        if($match[2] == null) {
606e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
607e3d68265SGerrit Uitslag        }
608e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
609e3d68265SGerrit Uitslag    }
610e3d68265SGerrit Uitslag
611e3d68265SGerrit Uitslag
612a180c973SKlap-in    /**
6131c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6141ef68647SAndreas Gohr     */
6151c14c879SAndreas Gohr    protected function load_css() {
616737417c6SKlap-in        global $conf;
6171c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6181c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6191c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
620ee19bac3SLuigi Micco
6211c14c879SAndreas Gohr        // prepare CSS files
6221c14c879SAndreas Gohr        $files = array_merge(
6231c14c879SAndreas Gohr            array(
6241c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6251c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6261c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6271c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6281c14c879SAndreas Gohr            ),
6291c14c879SAndreas Gohr            css_pluginstyles('all'),
63058e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6311c14c879SAndreas Gohr            array(
6321c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6331c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6341c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6351c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6361c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6371c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6381c14c879SAndreas Gohr            )
6391c14c879SAndreas Gohr        );
6401c14c879SAndreas Gohr        $css = '';
6411c14c879SAndreas Gohr        foreach($files as $file => $location) {
64228e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
64328e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6441c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6451ef68647SAndreas Gohr        }
6461ef68647SAndreas Gohr
64728e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6481c14c879SAndreas Gohr            // apply pattern replacements
64928e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
65028e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
65128e636eaSGerrit Uitslag
65228e636eaSGerrit Uitslag            // parse less
65328e636eaSGerrit Uitslag            $css = css_parseless($css);
65428e636eaSGerrit Uitslag        } else {
65528e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6561c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
65728e636eaSGerrit Uitslag        }
6581ef68647SAndreas Gohr
6591c14c879SAndreas Gohr        return $css;
660ee19bac3SLuigi Micco    }
6611c14c879SAndreas Gohr
66258e6409eSAndreas Gohr    /**
66358e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
66458e6409eSAndreas Gohr     *
66558e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
66658e6409eSAndreas Gohr     *
66758e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
66858e6409eSAndreas Gohr     */
6696be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
67058e6409eSAndreas Gohr        $list = array();
67158e6409eSAndreas Gohr        $plugins = plugin_list();
672f54b51f7SAndreas Gohr
673f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
67458e6409eSAndreas Gohr        foreach($plugins as $p) {
675f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
676f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
677f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
678f54b51f7SAndreas Gohr            }
679f54b51f7SAndreas Gohr
68058e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
68158e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
68258e6409eSAndreas Gohr            } else {
68358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
68458e6409eSAndreas Gohr            }
68558e6409eSAndreas Gohr        }
68658e6409eSAndreas Gohr        return $list;
68758e6409eSAndreas Gohr    }
688ad18f4e1SGerrit Uitslag
689ad18f4e1SGerrit Uitslag    /**
69060e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
69160e59de7SGerrit Uitslag     *
69260e59de7SGerrit Uitslag     * @return array
69360e59de7SGerrit Uitslag     */
69460e59de7SGerrit Uitslag    public function getExportedPages() {
69560e59de7SGerrit Uitslag        return $this->list;
69660e59de7SGerrit Uitslag    }
69760e59de7SGerrit Uitslag
69860e59de7SGerrit Uitslag    /**
699ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
70044e8e8fbSGerrit Uitslag     *
70144e8e8fbSGerrit Uitslag     * @param array $a
70244e8e8fbSGerrit Uitslag     * @param array $b
70344e8e8fbSGerrit Uitslag     * @return int
704ad18f4e1SGerrit Uitslag     */
705ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
706ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
707ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
708ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
709ad18f4e1SGerrit Uitslag    }
710ad18f4e1SGerrit Uitslag
711ad18f4e1SGerrit Uitslag    /**
712ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
71344e8e8fbSGerrit Uitslag     * @param array $a
71444e8e8fbSGerrit Uitslag     * @param array $b
71544e8e8fbSGerrit Uitslag     * @return int
716ad18f4e1SGerrit Uitslag     */
717ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
718ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
719ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
720ad18f4e1SGerrit Uitslag        return 0;
721ad18f4e1SGerrit Uitslag    }
72226be4eceSGerrit Uitslag
72326be4eceSGerrit Uitslag    /**
72402f9a447SGerrit Uitslag     * Return settings read from:
72502f9a447SGerrit Uitslag     *   1. url parameters
72602f9a447SGerrit Uitslag     *   2. plugin config
72702f9a447SGerrit Uitslag     *   3. global config
72802f9a447SGerrit Uitslag     *
72902f9a447SGerrit Uitslag     * @return array
73002f9a447SGerrit Uitslag     */
73102f9a447SGerrit Uitslag    protected function loadExportConfig() {
73202f9a447SGerrit Uitslag        global $INPUT;
73302f9a447SGerrit Uitslag        global $conf;
73402f9a447SGerrit Uitslag
73502f9a447SGerrit Uitslag        $this->exportConfig = array();
73602f9a447SGerrit Uitslag
73702f9a447SGerrit Uitslag        // decide on the paper setup from param or config
73802f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
73902f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
74002f9a447SGerrit Uitslag
7414870b378SLarsDW223        // decide on the font-size from param or config
7424870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
7434870b378SLarsDW223
744213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
745213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
746213fdb75SGerrit Uitslag
747213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
74802f9a447SGerrit Uitslag        $levels = array();
74902f9a447SGerrit Uitslag        if($hasToC) {
75002f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
75102f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
75202f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
75302f9a447SGerrit Uitslag            $bounds_input = array(
75402f9a447SGerrit Uitslag                'top' => array(
75502f9a447SGerrit Uitslag                    (int) $top_input,
75602f9a447SGerrit Uitslag                    (int) $top_conf
75702f9a447SGerrit Uitslag                ),
75802f9a447SGerrit Uitslag                'max' => array(
75902f9a447SGerrit Uitslag                    (int) $max_input,
76002f9a447SGerrit Uitslag                    (int) $max_conf
76102f9a447SGerrit Uitslag                )
76202f9a447SGerrit Uitslag            );
76302f9a447SGerrit Uitslag            $bounds = array(
76402f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
76502f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
76602f9a447SGerrit Uitslag
76702f9a447SGerrit Uitslag            );
76802f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
76902f9a447SGerrit Uitslag                foreach($values as $value) {
77002f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
77102f9a447SGerrit Uitslag                        //stop at valid value and store
77202f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
77302f9a447SGerrit Uitslag                        break;
77402f9a447SGerrit Uitslag                    }
77502f9a447SGerrit Uitslag                }
77602f9a447SGerrit Uitslag            }
77702f9a447SGerrit Uitslag
77802f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
77902f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
78002f9a447SGerrit Uitslag            }
78102f9a447SGerrit Uitslag
78202f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
78302f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
78402f9a447SGerrit Uitslag            }
78502f9a447SGerrit Uitslag        }
78602f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
78702f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
78802f9a447SGerrit Uitslag
78902f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
79002f9a447SGerrit Uitslag
79102f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
79205d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
79302f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
79402f9a447SGerrit Uitslag            $tpl = $tplconf;
79502f9a447SGerrit Uitslag        }
79602f9a447SGerrit Uitslag        if(!$tpl){
79702f9a447SGerrit Uitslag            $tpl = 'default';
79802f9a447SGerrit Uitslag        }
79902f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
80002f9a447SGerrit Uitslag
80102f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
80202f9a447SGerrit Uitslag    }
80302f9a447SGerrit Uitslag
80402f9a447SGerrit Uitslag    /**
80502f9a447SGerrit Uitslag     * Returns requested config
80602f9a447SGerrit Uitslag     *
80702f9a447SGerrit Uitslag     * @param string $name
80802f9a447SGerrit Uitslag     * @param mixed  $notset
80902f9a447SGerrit Uitslag     * @return mixed|bool
81002f9a447SGerrit Uitslag     */
81102f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
81202f9a447SGerrit Uitslag        if ($this->exportConfig === null){
81302f9a447SGerrit Uitslag            $this->loadExportConfig();
81402f9a447SGerrit Uitslag        }
81502f9a447SGerrit Uitslag
81602f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
81702f9a447SGerrit Uitslag            return $this->exportConfig[$name];
81802f9a447SGerrit Uitslag        }else{
81902f9a447SGerrit Uitslag            return $notset;
82002f9a447SGerrit Uitslag        }
82102f9a447SGerrit Uitslag    }
822d63e7fe7SGerrit Uitslag
823d63e7fe7SGerrit Uitslag    /**
824d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
825d63e7fe7SGerrit Uitslag     *
826d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
827d63e7fe7SGerrit Uitslag     */
82844e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
829d63e7fe7SGerrit Uitslag        global $ID, $REV;
830d63e7fe7SGerrit Uitslag
831d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
832d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
833d63e7fe7SGerrit Uitslag            if($REV) {
834d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
835d63e7fe7SGerrit Uitslag            }
836d63e7fe7SGerrit Uitslag
837d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
838d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
839d63e7fe7SGerrit Uitslag                array('export_pdf' =>
840d63e7fe7SGerrit Uitslag                          '<li>'
84172cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
842d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
843d63e7fe7SGerrit Uitslag                          . '</a>'
844d63e7fe7SGerrit Uitslag                          . '</li>'
845d63e7fe7SGerrit Uitslag                ) +
846d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
847d63e7fe7SGerrit Uitslag        }
848d63e7fe7SGerrit Uitslag    }
849ee19bac3SLuigi Micco}
850