xref: /plugin/dw2pdf/action.php (revision b3eed6e35c3fe530fdb75257055de18979116e6b)
1ee19bac3SLuigi Micco<?php
2ee19bac3SLuigi Micco/**
3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4ee19bac3SLuigi Micco *
5ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
75db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8ee19bac3SLuigi Micco */
9ee19bac3SLuigi Micco
10ee19bac3SLuigi Micco// must be run within Dokuwiki
11ee19bac3SLuigi Miccoif(!defined('DOKU_INC')) die();
12ee19bac3SLuigi Micco
130639157eSGerrit Uitslag/**
140639157eSGerrit Uitslag * Class action_plugin_dw2pdf
150639157eSGerrit Uitslag *
160639157eSGerrit Uitslag * Export hmtl content to pdf, for different url parameter configurations
170639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html.
180639157eSGerrit Uitslag */
191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
2002f9a447SGerrit Uitslag    /**
2102f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2202f9a447SGerrit Uitslag     *
2302f9a447SGerrit Uitslag     * @var array
2402f9a447SGerrit Uitslag     */
25213fdb75SGerrit Uitslag    protected $exportConfig = null;
2660e59de7SGerrit Uitslag    protected $tpl;
2760e59de7SGerrit Uitslag    protected $list = array();
281c14c879SAndreas Gohr
291c14c879SAndreas Gohr    /**
301c14c879SAndreas Gohr     * Constructor. Sets the correct template
311c14c879SAndreas Gohr     */
326be736bfSGerrit Uitslag    public function __construct() {
3302f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
341c14c879SAndreas Gohr    }
351c14c879SAndreas Gohr
36ee19bac3SLuigi Micco    /**
37ee19bac3SLuigi Micco     * Register the events
38177a7d30SGerrit Uitslag     *
39177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
40ee19bac3SLuigi Micco     */
416be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
42ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
436be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
44ee19bac3SLuigi Micco    }
45ee19bac3SLuigi Micco
461c14c879SAndreas Gohr    /**
471c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
48737417c6SKlap-in     *
49737417c6SKlap-in     * @param Doku_Event $event
50737417c6SKlap-in     * @return bool
511c14c879SAndreas Gohr     */
5244e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
53ee19bac3SLuigi Micco        global $ACT;
54ee19bac3SLuigi Micco        global $ID;
55ee19bac3SLuigi Micco
561ef68647SAndreas Gohr        // our event?
57ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false;
58ee19bac3SLuigi Micco
591ef68647SAndreas Gohr        // check user's rights
601ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
611ef68647SAndreas Gohr
62d63e7fe7SGerrit Uitslag        if($data = $this->collectExportPages($event)) {
63d63e7fe7SGerrit Uitslag            list($title, $this->list) = $data;
64d63e7fe7SGerrit Uitslag        } else {
65d63e7fe7SGerrit Uitslag            return false;
66d63e7fe7SGerrit Uitslag        }
67d63e7fe7SGerrit Uitslag
68d63e7fe7SGerrit Uitslag        // it's ours, no one else's
69d63e7fe7SGerrit Uitslag        $event->preventDefault();
70d63e7fe7SGerrit Uitslag
71a58f45f0SGerrit Uitslag        // prepare cache and its dependencies
72a58f45f0SGerrit Uitslag        $depends = array();
73a58f45f0SGerrit Uitslag        $cache = $this->prepareCache($title, $depends);
74d63e7fe7SGerrit Uitslag
75d63e7fe7SGerrit Uitslag        // hard work only when no cache available
76d63e7fe7SGerrit Uitslag        if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
77d63e7fe7SGerrit Uitslag            $this->generatePDF($cache->cache, $title);
78d63e7fe7SGerrit Uitslag        }
79d63e7fe7SGerrit Uitslag
80d63e7fe7SGerrit Uitslag        // deliver the file
81d63e7fe7SGerrit Uitslag        $this->sendPDFFile($cache->cache, $title);
82d63e7fe7SGerrit Uitslag        return true;
83d63e7fe7SGerrit Uitslag    }
84d63e7fe7SGerrit Uitslag
85d63e7fe7SGerrit Uitslag
86d63e7fe7SGerrit Uitslag    /**
87d63e7fe7SGerrit Uitslag     * Obtain list of pages and title, based on url parameters
88d63e7fe7SGerrit Uitslag     *
89d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
90d63e7fe7SGerrit Uitslag     * @return string|bool
91d63e7fe7SGerrit Uitslag     */
92d63e7fe7SGerrit Uitslag    protected function collectExportPages(Doku_Event $event) {
93d63e7fe7SGerrit Uitslag        global $ACT;
94d63e7fe7SGerrit Uitslag        global $ID;
95d63e7fe7SGerrit Uitslag        global $INPUT;
96d63e7fe7SGerrit Uitslag        global $conf;
97d63e7fe7SGerrit Uitslag
98d63e7fe7SGerrit Uitslag        // list of one or multiple pages
99d63e7fe7SGerrit Uitslag        $list = array();
10028e636eaSGerrit Uitslag
10187c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
102d63e7fe7SGerrit Uitslag            $list[0] = $ID;
103177a7d30SGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
104177a7d30SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
105177a7d30SGerrit Uitslag            if(empty($title)) {
106737417c6SKlap-in                $title = p_get_first_heading($ID);
10715923cb9SGerrit Uitslag            }
108ad18f4e1SGerrit Uitslag
109ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
110ad18f4e1SGerrit Uitslag            //check input for title and ns
111177a7d30SGerrit Uitslag            if(!$title = $INPUT->str('book_title')) {
11226be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
113ad18f4e1SGerrit Uitslag                return false;
114ad18f4e1SGerrit Uitslag            }
115177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
116ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
11726be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
118ad18f4e1SGerrit Uitslag                return false;
119ad18f4e1SGerrit Uitslag            }
120ad18f4e1SGerrit Uitslag
12126be4eceSGerrit Uitslag            //sort order
122177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
123ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
124ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
125ad18f4e1SGerrit Uitslag                $order = 'natural';
126ad18f4e1SGerrit Uitslag            }
127ad18f4e1SGerrit Uitslag
12826be4eceSGerrit Uitslag            //search depth
129177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
130ad18f4e1SGerrit Uitslag            if($depth < 0) {
131ad18f4e1SGerrit Uitslag                $depth = 0;
132ad18f4e1SGerrit Uitslag            }
13326be4eceSGerrit Uitslag
134ad18f4e1SGerrit Uitslag            //page search
135ad18f4e1SGerrit Uitslag            $result = array();
136ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
137ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
138ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
139ad18f4e1SGerrit Uitslag
14026be4eceSGerrit Uitslag            //sorting
141ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
142ad18f4e1SGerrit Uitslag                if($order == 'date') {
143ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
144ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
145ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
146ad18f4e1SGerrit Uitslag                }
147ad18f4e1SGerrit Uitslag            }
148ad18f4e1SGerrit Uitslag
149ad18f4e1SGerrit Uitslag            foreach($result as $item) {
150d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
151ad18f4e1SGerrit Uitslag            }
152ad18f4e1SGerrit Uitslag
153baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
154baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
155baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
156baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
157baa31dc5SGerrit Uitslag                    }
158baa31dc5SGerrit Uitslag                }
159baa31dc5SGerrit Uitslag            }
160baa31dc5SGerrit Uitslag
161737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
162*b3eed6e3SGerrit 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
173*b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
174*b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
175*b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
176*b3eed6e3SGerrit Uitslag//                http_status(403);
177*b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
178*b3eed6e3SGerrit Uitslag//                exit();
179*b3eed6e3SGerrit Uitslag//            }
180*b3eed6e3SGerrit Uitslag
181*b3eed6e3SGerrit Uitslag            $json = new JSON(JSON_LOOSE_TYPE);
182*b3eed6e3SGerrit Uitslag            $list = $json->decode($INPUT->post->str('selection', '', true));
183*b3eed6e3SGerrit Uitslag            if(!is_array($list) || empty($list)) {
184*b3eed6e3SGerrit Uitslag                http_status(400);
185*b3eed6e3SGerrit Uitslag                print $this->getLang('empty');
186*b3eed6e3SGerrit Uitslag                exit();
187*b3eed6e3SGerrit Uitslag            }
188*b3eed6e3SGerrit Uitslag
189*b3eed6e3SGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
190*b3eed6e3SGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
191*b3eed6e3SGerrit Uitslag            if(empty($title)) {
192*b3eed6e3SGerrit Uitslag                http_status(400);
193*b3eed6e3SGerrit Uitslag                print $this->getLang('needtitle');
194*b3eed6e3SGerrit Uitslag                exit();
195*b3eed6e3SGerrit Uitslag            }
196*b3eed6e3SGerrit 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
29602f9a447SGerrit Uitslag        $mpdf = new DokuPDF($this->getExportConfig('pagesize'), $this->getExportConfig('orientation'));
297ee19bac3SLuigi Micco
298d62df65bSAndreas Gohr        // let mpdf fix local links
299d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
300d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
30102f9a447SGerrit Uitslag        if($self['port']) {
30202f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
30302f9a447SGerrit Uitslag        }
304d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
305d62df65bSAndreas Gohr
30656d13144SAndreas Gohr        // Set the title
30756d13144SAndreas Gohr        $mpdf->SetTitle($title);
30856d13144SAndreas Gohr
309d63e7fe7SGerrit Uitslag        // some default document settings
310d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
311213fdb75SGerrit Uitslag        //      single-side document has only odd pages
312213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
313daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
314daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
31502f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
31602f9a447SGerrit Uitslag        if($hasToC) {
31702f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
31802f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
31902f9a447SGerrit Uitslag        } else {
32002f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
32102f9a447SGerrit Uitslag        }
3222eedf77dSAndreas Gohr
32356d13144SAndreas Gohr        // load the template
3241c14c879SAndreas Gohr        $template = $this->load_template($title);
325ee19bac3SLuigi Micco
3261ef68647SAndreas Gohr        // prepare HTML header styles
327a2c33768SGerrit Uitslag        $html = '';
32802f9a447SGerrit Uitslag        if($isDebug) {
329a2c33768SGerrit Uitslag            $html .= '<html><head>';
330737417c6SKlap-in            $html .= '<style type="text/css">';
331a2c33768SGerrit Uitslag        }
332a2c33768SGerrit Uitslag        $styles = $this->load_css();
333a2c33768SGerrit Uitslag        $styles .= '@page { size:auto; ' . $template['page'] . '}';
334a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
335254467c4SGerrit Uitslag
336254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
337254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
338254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
339254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
340254467c4SGerrit Uitslag
341a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
342a2c33768SGerrit Uitslag
34302f9a447SGerrit Uitslag        if($isDebug) {
344a2c33768SGerrit Uitslag            $html .= $styles;
3451ef68647SAndreas Gohr            $html .= '</style>';
3461ef68647SAndreas Gohr            $html .= '</head><body>';
347a2c33768SGerrit Uitslag        }
348a2c33768SGerrit Uitslag
349a2c33768SGerrit Uitslag        $body_start = $template['html'];
350a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3512eedf77dSAndreas Gohr
3521e45476bSmnapp        // insert the cover page
353a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
354a2c33768SGerrit Uitslag
355a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
35602f9a447SGerrit Uitslag        if($isDebug) {
357a2c33768SGerrit Uitslag            $html .= $body_start;
358a2c33768SGerrit Uitslag        }
35902f9a447SGerrit Uitslag        if($hasToC) {
36002f9a447SGerrit 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
36102f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
36202f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
36302f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
36402f9a447SGerrit Uitslag                array(
365230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
366230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
36702f9a447SGerrit Uitslag                    'links' => true,
36802f9a447SGerrit Uitslag                    'outdent' => '1em',
36902f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
37002f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
37102f9a447SGerrit Uitslag                )
37202f9a447SGerrit Uitslag            );
37302f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
37402f9a447SGerrit Uitslag        }
37502f9a447SGerrit Uitslag
376c00eb13bSGerrit Uitslag        // store original pageid
377c00eb13bSGerrit Uitslag        $keep = $ID;
378c00eb13bSGerrit Uitslag
3791ef68647SAndreas Gohr        // loop over all pages
380a2c33768SGerrit Uitslag        $cnt = count($this->list);
3811ef68647SAndreas Gohr        for($n = 0; $n < $cnt; $n++) {
382a2c33768SGerrit Uitslag            $page = $this->list[$n];
383*b3eed6e3SGerrit Uitslag            $filename = wikiFN($page, $REV);
384*b3eed6e3SGerrit Uitslag
385*b3eed6e3SGerrit Uitslag            if(!file_exists($filename)) {
386*b3eed6e3SGerrit Uitslag                continue;
387*b3eed6e3SGerrit Uitslag            }
388ee19bac3SLuigi Micco
389c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
390c00eb13bSGerrit Uitslag            $ID = $page;
391c00eb13bSGerrit Uitslag
392*b3eed6e3SGerrit Uitslag            $pagehtml = p_cached_output($filename, 'dw2pdf', $page);
393719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
3941ef68647SAndreas Gohr            if($n < ($cnt - 1)) {
395a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
396a2c33768SGerrit Uitslag            }
397a2c33768SGerrit Uitslag
398a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
39902f9a447SGerrit Uitslag            if($isDebug) {
400a2c33768SGerrit Uitslag                $html .= $pagehtml;
4011ef68647SAndreas Gohr            }
402ee19bac3SLuigi Micco        }
403c00eb13bSGerrit Uitslag        //restore ID
404c00eb13bSGerrit Uitslag        $ID = $keep;
405ee19bac3SLuigi Micco
40633c15297SGerrit Uitslag        // insert the back page
407a2c33768SGerrit Uitslag        $body_end = $template['back'];
40833c15297SGerrit Uitslag
409a2c33768SGerrit Uitslag        $body_end .= '</div>';
410a2c33768SGerrit Uitslag
411d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
41202f9a447SGerrit Uitslag        if($isDebug) {
413a2c33768SGerrit Uitslag            $html .= $body_end;
414eeb17e15SAndreas Gohr            $html .= '</body>';
415eeb17e15SAndreas Gohr            $html .= '</html>';
416a2c33768SGerrit Uitslag        }
417f765508eSGerrit Uitslag
418f765508eSGerrit Uitslag        //Return html for debugging
41902f9a447SGerrit Uitslag        if($isDebug) {
42002f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
42126be4eceSGerrit Uitslag                echo $html;
422a2c33768SGerrit Uitslag            } else {
423a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
424a2c33768SGerrit Uitslag                echo $html;
425a2c33768SGerrit Uitslag            }
42626be4eceSGerrit Uitslag            exit();
42726be4eceSGerrit Uitslag        };
428f765508eSGerrit Uitslag
42987c86ddaSAndreas Gohr        // write to cache file
430d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
43187c86ddaSAndreas Gohr    }
43287c86ddaSAndreas Gohr
433d63e7fe7SGerrit Uitslag    /**
434d63e7fe7SGerrit Uitslag     * @param string $cachefile
435d63e7fe7SGerrit Uitslag     * @param string $title
436d63e7fe7SGerrit Uitslag     */
437d63e7fe7SGerrit Uitslag    protected function sendPDFFile($cachefile, $title) {
43887c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
439b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
44087c86ddaSAndreas Gohr        header('Pragma: public');
441d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
44287c86ddaSAndreas Gohr
4439a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
44487c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4459a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
44687c86ddaSAndreas Gohr        } else {
4479a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
44887c86ddaSAndreas Gohr        }
449ee19bac3SLuigi Micco
450*b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
451*b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
452*b3eed6e3SGerrit Uitslag
453e993da11SGerrit Uitslag        //try to send file, and exit if done
454d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
45587c86ddaSAndreas Gohr
456d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
45787c86ddaSAndreas Gohr        if($fp) {
458d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
45987c86ddaSAndreas Gohr        } else {
46087c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
46187c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
46287c86ddaSAndreas Gohr        }
4631ef68647SAndreas Gohr        exit();
4641ef68647SAndreas Gohr    }
4651ef68647SAndreas Gohr
4666be736bfSGerrit Uitslag    /**
4672eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
46844e8e8fbSGerrit Uitslag     *
46944e8e8fbSGerrit Uitslag     * @param string $title
47044e8e8fbSGerrit Uitslag     * @return array
4711ef68647SAndreas Gohr     */
4721c14c879SAndreas Gohr    protected function load_template($title) {
4731ef68647SAndreas Gohr        global $ID;
4741ef68647SAndreas Gohr        global $conf;
4751ef68647SAndreas Gohr
4762eedf77dSAndreas Gohr        // this is what we'll return
4772eedf77dSAndreas Gohr        $output = array(
4781e45476bSmnapp            'cover' => '',
4792eedf77dSAndreas Gohr            'html'  => '',
4802eedf77dSAndreas Gohr            'page'  => '',
4812eedf77dSAndreas Gohr            'first' => '',
4822eedf77dSAndreas Gohr            'cite'  => '',
4832eedf77dSAndreas Gohr        );
4842eedf77dSAndreas Gohr
4852eedf77dSAndreas Gohr        // prepare header/footer elements
4862eedf77dSAndreas Gohr        $html = '';
48733c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
48833c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
48902f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
49033c15297SGerrit Uitslag                if(file_exists($file)) {
49133c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
49233c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
49333c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
4942eedf77dSAndreas Gohr
4952eedf77dSAndreas Gohr                    // register the needed pseudo CSS
49633c15297SGerrit Uitslag                    if($order == '_first') {
49733c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
49833c15297SGerrit Uitslag                    } elseif($order == '_even') {
49933c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
50033c15297SGerrit Uitslag                    } elseif($order == '_odd') {
50133c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
502daa70883SAndreas Gohr                    } else {
50333c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5042eedf77dSAndreas Gohr                    }
5052eedf77dSAndreas Gohr                }
5062eedf77dSAndreas Gohr            }
5072eedf77dSAndreas Gohr        }
5082eedf77dSAndreas Gohr
5091ef68647SAndreas Gohr        // prepare replacements
5101ef68647SAndreas Gohr        $replace = array(
5111ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
51202f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
5132eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
5141ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5151ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5161ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5175d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
51802f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5191ef68647SAndreas Gohr        );
5201ef68647SAndreas Gohr
5212eedf77dSAndreas Gohr        // set HTML element
522a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
523a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
524a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5251ef68647SAndreas Gohr
5261e45476bSmnapp        // cover page
52702f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
52833c15297SGerrit Uitslag        if(file_exists($coverfile)) {
52933c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5301e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5319b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5326e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5331e45476bSmnapp        }
5341e45476bSmnapp
53533c15297SGerrit Uitslag        // cover page
53602f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
53733c15297SGerrit Uitslag        if(file_exists($backfile)) {
53833c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
53933c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
54033c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5419b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
54233c15297SGerrit Uitslag        }
54333c15297SGerrit Uitslag
5442eedf77dSAndreas Gohr        // citation box
54502f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
54633c15297SGerrit Uitslag        if(file_exists($citationfile)) {
54733c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5482eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5492eedf77dSAndreas Gohr        }
5501ef68647SAndreas Gohr
5512eedf77dSAndreas Gohr        return $output;
5521ef68647SAndreas Gohr    }
5531ef68647SAndreas Gohr
5541ef68647SAndreas Gohr    /**
555a180c973SKlap-in     * @param string $raw code with placeholders
556a180c973SKlap-in     * @param string $id  pageid
557a180c973SKlap-in     * @return string
558a180c973SKlap-in     */
559a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
560a180c973SKlap-in        global $REV;
561a180c973SKlap-in
562a180c973SKlap-in        // generate qr code for this page using google infographics api
563a180c973SKlap-in        $qr_code = '';
564a180c973SKlap-in        if($this->getConf('qrcodesize')) {
565a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
566a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
567a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
568a180c973SKlap-in        }
569a180c973SKlap-in        // prepare replacements
570a180c973SKlap-in        $replace['@ID@']      = $id;
571a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
572a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
573a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
574a180c973SKlap-in
575e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
576e3d68265SGerrit Uitslag
577e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
578e3d68265SGerrit Uitslag        $content = preg_replace_callback(
579e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
580e3d68265SGerrit Uitslag            array($this, 'replacedate'),
581e3d68265SGerrit Uitslag            $content
582e3d68265SGerrit Uitslag        );
583e3d68265SGerrit Uitslag
584e3d68265SGerrit Uitslag        return $content;
585a180c973SKlap-in    }
586a180c973SKlap-in
587e3d68265SGerrit Uitslag
588e3d68265SGerrit Uitslag    /**
589e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
590e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
591e3d68265SGerrit Uitslag     *
592e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
593e3d68265SGerrit Uitslag     * @return string
594e3d68265SGerrit Uitslag     */
595e3d68265SGerrit Uitslag    function replacedate($match) {
596e3d68265SGerrit Uitslag        global $conf;
597e3d68265SGerrit Uitslag        //no 2nd argument for default date format
598e3d68265SGerrit Uitslag        if($match[2] == null) {
599e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
600e3d68265SGerrit Uitslag        }
601e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
602e3d68265SGerrit Uitslag    }
603e3d68265SGerrit Uitslag
604e3d68265SGerrit Uitslag
605a180c973SKlap-in    /**
6061c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6071ef68647SAndreas Gohr     */
6081c14c879SAndreas Gohr    protected function load_css() {
609737417c6SKlap-in        global $conf;
6101c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6111c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6121c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
613ee19bac3SLuigi Micco
6141c14c879SAndreas Gohr        // prepare CSS files
6151c14c879SAndreas Gohr        $files = array_merge(
6161c14c879SAndreas Gohr            array(
6171c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6181c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6191c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6201c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6211c14c879SAndreas Gohr            ),
6221c14c879SAndreas Gohr            css_pluginstyles('all'),
62358e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6241c14c879SAndreas Gohr            array(
6251c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6261c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6271c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6281c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6291c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6301c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6311c14c879SAndreas Gohr            )
6321c14c879SAndreas Gohr        );
6331c14c879SAndreas Gohr        $css = '';
6341c14c879SAndreas Gohr        foreach($files as $file => $location) {
63528e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
63628e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6371c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6381ef68647SAndreas Gohr        }
6391ef68647SAndreas Gohr
64028e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6411c14c879SAndreas Gohr            // apply pattern replacements
64228e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
64328e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
64428e636eaSGerrit Uitslag
64528e636eaSGerrit Uitslag            // parse less
64628e636eaSGerrit Uitslag            $css = css_parseless($css);
64728e636eaSGerrit Uitslag        } else {
64828e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6491c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
65028e636eaSGerrit Uitslag        }
6511ef68647SAndreas Gohr
6521c14c879SAndreas Gohr        return $css;
653ee19bac3SLuigi Micco    }
6541c14c879SAndreas Gohr
65558e6409eSAndreas Gohr    /**
65658e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
65758e6409eSAndreas Gohr     *
65858e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
65958e6409eSAndreas Gohr     *
66058e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
66158e6409eSAndreas Gohr     */
6626be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
66358e6409eSAndreas Gohr        $list = array();
66458e6409eSAndreas Gohr        $plugins = plugin_list();
665f54b51f7SAndreas Gohr
666f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
66758e6409eSAndreas Gohr        foreach($plugins as $p) {
668f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
669f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
670f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
671f54b51f7SAndreas Gohr            }
672f54b51f7SAndreas Gohr
67358e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
67458e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
67558e6409eSAndreas Gohr            } else {
67658e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
67758e6409eSAndreas Gohr            }
67858e6409eSAndreas Gohr        }
67958e6409eSAndreas Gohr        return $list;
68058e6409eSAndreas Gohr    }
681ad18f4e1SGerrit Uitslag
682ad18f4e1SGerrit Uitslag    /**
68360e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
68460e59de7SGerrit Uitslag     *
68560e59de7SGerrit Uitslag     * @return array
68660e59de7SGerrit Uitslag     */
68760e59de7SGerrit Uitslag    public function getExportedPages() {
68860e59de7SGerrit Uitslag        return $this->list;
68960e59de7SGerrit Uitslag    }
69060e59de7SGerrit Uitslag
69160e59de7SGerrit Uitslag    /**
692ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
69344e8e8fbSGerrit Uitslag     *
69444e8e8fbSGerrit Uitslag     * @param array $a
69544e8e8fbSGerrit Uitslag     * @param array $b
69644e8e8fbSGerrit Uitslag     * @return int
697ad18f4e1SGerrit Uitslag     */
698ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
699ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
700ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
701ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
702ad18f4e1SGerrit Uitslag    }
703ad18f4e1SGerrit Uitslag
704ad18f4e1SGerrit Uitslag    /**
705ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
70644e8e8fbSGerrit Uitslag     * @param array $a
70744e8e8fbSGerrit Uitslag     * @param array $b
70844e8e8fbSGerrit Uitslag     * @return int
709ad18f4e1SGerrit Uitslag     */
710ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
711ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
712ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
713ad18f4e1SGerrit Uitslag        return 0;
714ad18f4e1SGerrit Uitslag    }
71526be4eceSGerrit Uitslag
71626be4eceSGerrit Uitslag    /**
71702f9a447SGerrit Uitslag     * Return settings read from:
71802f9a447SGerrit Uitslag     *   1. url parameters
71902f9a447SGerrit Uitslag     *   2. plugin config
72002f9a447SGerrit Uitslag     *   3. global config
72102f9a447SGerrit Uitslag     *
72202f9a447SGerrit Uitslag     * @return array
72302f9a447SGerrit Uitslag     */
72402f9a447SGerrit Uitslag    protected function loadExportConfig() {
72502f9a447SGerrit Uitslag        global $INPUT;
72602f9a447SGerrit Uitslag        global $conf;
72702f9a447SGerrit Uitslag
72802f9a447SGerrit Uitslag        $this->exportConfig = array();
72902f9a447SGerrit Uitslag
73002f9a447SGerrit Uitslag        // decide on the paper setup from param or config
73102f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
73202f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
73302f9a447SGerrit Uitslag
734213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
735213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
736213fdb75SGerrit Uitslag
737213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
73802f9a447SGerrit Uitslag        $levels = array();
73902f9a447SGerrit Uitslag        if($hasToC) {
74002f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
74102f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
74202f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
74302f9a447SGerrit Uitslag            $bounds_input = array(
74402f9a447SGerrit Uitslag                'top' => array(
74502f9a447SGerrit Uitslag                    (int) $top_input,
74602f9a447SGerrit Uitslag                    (int) $top_conf
74702f9a447SGerrit Uitslag                ),
74802f9a447SGerrit Uitslag                'max' => array(
74902f9a447SGerrit Uitslag                    (int) $max_input,
75002f9a447SGerrit Uitslag                    (int) $max_conf
75102f9a447SGerrit Uitslag                )
75202f9a447SGerrit Uitslag            );
75302f9a447SGerrit Uitslag            $bounds = array(
75402f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
75502f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
75602f9a447SGerrit Uitslag
75702f9a447SGerrit Uitslag            );
75802f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
75902f9a447SGerrit Uitslag                foreach($values as $value) {
76002f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
76102f9a447SGerrit Uitslag                        //stop at valid value and store
76202f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
76302f9a447SGerrit Uitslag                        break;
76402f9a447SGerrit Uitslag                    }
76502f9a447SGerrit Uitslag                }
76602f9a447SGerrit Uitslag            }
76702f9a447SGerrit Uitslag
76802f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
76902f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
77002f9a447SGerrit Uitslag            }
77102f9a447SGerrit Uitslag
77202f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
77302f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
77402f9a447SGerrit Uitslag            }
77502f9a447SGerrit Uitslag        }
77602f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
77702f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
77802f9a447SGerrit Uitslag
77902f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
78002f9a447SGerrit Uitslag
78102f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
78205d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
78302f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
78402f9a447SGerrit Uitslag            $tpl = $tplconf;
78502f9a447SGerrit Uitslag        }
78602f9a447SGerrit Uitslag        if(!$tpl){
78702f9a447SGerrit Uitslag            $tpl = 'default';
78802f9a447SGerrit Uitslag        }
78902f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
79002f9a447SGerrit Uitslag
79102f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
79202f9a447SGerrit Uitslag    }
79302f9a447SGerrit Uitslag
79402f9a447SGerrit Uitslag    /**
79502f9a447SGerrit Uitslag     * Returns requested config
79602f9a447SGerrit Uitslag     *
79702f9a447SGerrit Uitslag     * @param string $name
79802f9a447SGerrit Uitslag     * @param mixed  $notset
79902f9a447SGerrit Uitslag     * @return mixed|bool
80002f9a447SGerrit Uitslag     */
80102f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
80202f9a447SGerrit Uitslag        if ($this->exportConfig === null){
80302f9a447SGerrit Uitslag            $this->loadExportConfig();
80402f9a447SGerrit Uitslag        }
80502f9a447SGerrit Uitslag
80602f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
80702f9a447SGerrit Uitslag            return $this->exportConfig[$name];
80802f9a447SGerrit Uitslag        }else{
80902f9a447SGerrit Uitslag            return $notset;
81002f9a447SGerrit Uitslag        }
81102f9a447SGerrit Uitslag    }
812d63e7fe7SGerrit Uitslag
813d63e7fe7SGerrit Uitslag    /**
814d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
815d63e7fe7SGerrit Uitslag     *
816d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
817d63e7fe7SGerrit Uitslag     */
81844e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
819d63e7fe7SGerrit Uitslag        global $ID, $REV;
820d63e7fe7SGerrit Uitslag
821d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
822d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
823d63e7fe7SGerrit Uitslag            if($REV) {
824d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
825d63e7fe7SGerrit Uitslag            }
826d63e7fe7SGerrit Uitslag
827d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
828d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
829d63e7fe7SGerrit Uitslag                array('export_pdf' =>
830d63e7fe7SGerrit Uitslag                          '<li>'
83172cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
832d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
833d63e7fe7SGerrit Uitslag                          . '</a>'
834d63e7fe7SGerrit Uitslag                          . '</li>'
835d63e7fe7SGerrit Uitslag                ) +
836d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
837d63e7fe7SGerrit Uitslag        }
838d63e7fe7SGerrit Uitslag    }
839ee19bac3SLuigi Micco}
840