xref: /plugin/dw2pdf/action.php (revision 8003b4936e64346b06c4c548fcf5b590baec9dd8)
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;
2703352761SKirsten Roschanski    protected $title;
2860e59de7SGerrit Uitslag    protected $list = array();
291c14c879SAndreas Gohr
301c14c879SAndreas Gohr    /**
311c14c879SAndreas Gohr     * Constructor. Sets the correct template
3203352761SKirsten Roschanski     *
3303352761SKirsten Roschanski     * @param string $title
341c14c879SAndreas Gohr     */
3503352761SKirsten Roschanski    public function __construct($title=null) {
3602f9a447SGerrit Uitslag        $this->tpl   = $this->getExportConfig('template');
3703352761SKirsten Roschanski        $this->title = $title ? $title : '';
381c14c879SAndreas Gohr    }
391c14c879SAndreas Gohr
40ee19bac3SLuigi Micco    /**
41ee19bac3SLuigi Micco     * Register the events
42177a7d30SGerrit Uitslag     *
43177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
44ee19bac3SLuigi Micco     */
456be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
46ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
476be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
48026b594bSAndreas Gohr        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array());
49ee19bac3SLuigi Micco    }
50ee19bac3SLuigi Micco
511c14c879SAndreas Gohr    /**
521c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
53737417c6SKlap-in     *
54737417c6SKlap-in     * @param Doku_Event $event
55737417c6SKlap-in     * @return bool
561c14c879SAndreas Gohr     */
5744e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
58ee19bac3SLuigi Micco        global $ID;
59ee19bac3SLuigi Micco
601ef68647SAndreas Gohr        // our event?
6152738244SLarsDW223        if(($event->data != 'export_pdfbook') && ($event->data != 'export_pdf') && ($event->data != 'export_pdfns')) return false;
62ee19bac3SLuigi Micco
631ef68647SAndreas Gohr        // check user's rights
641ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
651ef68647SAndreas Gohr
66d63e7fe7SGerrit Uitslag        if($data = $this->collectExportPages($event)) {
6703352761SKirsten Roschanski            list($this->title, $this->list) = $data;
68d63e7fe7SGerrit Uitslag        } else {
69d63e7fe7SGerrit Uitslag            return false;
70d63e7fe7SGerrit Uitslag        }
71d63e7fe7SGerrit Uitslag
72d63e7fe7SGerrit Uitslag        // it's ours, no one else's
73d63e7fe7SGerrit Uitslag        $event->preventDefault();
74d63e7fe7SGerrit Uitslag
75a58f45f0SGerrit Uitslag        // prepare cache and its dependencies
76a58f45f0SGerrit Uitslag        $depends = array();
7703352761SKirsten Roschanski        $cache = $this->prepareCache($depends);
78d63e7fe7SGerrit Uitslag
79bd977188SGerrit Uitslag        // hard work only when no cache available or needed for debugging
809bcbb4f0SGerrit Uitslag        if(!$this->getConf('usecache') || $this->getExportConfig('isDebug') || !$cache->useCache($depends)) {
81e5f6c2cbSMichael Große            // generating the pdf may take a long time for larger wikis / namespaces with many pages
82e5f6c2cbSMichael Große            set_time_limit(0);
83e5f6c2cbSMichael Große
8403352761SKirsten Roschanski            $this->generatePDF($cache->cache);
85d63e7fe7SGerrit Uitslag        }
86d63e7fe7SGerrit Uitslag
87d63e7fe7SGerrit Uitslag        // deliver the file
8803352761SKirsten Roschanski        $this->sendPDFFile($cache->cache);
89d63e7fe7SGerrit Uitslag        return true;
90d63e7fe7SGerrit Uitslag    }
91d63e7fe7SGerrit Uitslag
92d63e7fe7SGerrit Uitslag
93d63e7fe7SGerrit Uitslag    /**
94d63e7fe7SGerrit Uitslag     * Obtain list of pages and title, based on url parameters
95d63e7fe7SGerrit Uitslag     *
96d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
97d63e7fe7SGerrit Uitslag     * @return string|bool
98d63e7fe7SGerrit Uitslag     */
99d63e7fe7SGerrit Uitslag    protected function collectExportPages(Doku_Event $event) {
100d63e7fe7SGerrit Uitslag        global $ID;
101d63e7fe7SGerrit Uitslag        global $INPUT;
102d63e7fe7SGerrit Uitslag        global $conf;
103d63e7fe7SGerrit Uitslag
104d63e7fe7SGerrit Uitslag        // list of one or multiple pages
105d63e7fe7SGerrit Uitslag        $list = array();
10628e636eaSGerrit Uitslag
1074b4cebc2SLarsDW223        if($event->data == 'export_pdf') {
108d63e7fe7SGerrit Uitslag            $list[0] = $ID;
10903352761SKirsten Roschanski            $this->title = $INPUT->str('pdftitle'); //DEPRECATED
11003352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
11103352761SKirsten Roschanski            if(empty($this->title)) {
11203352761SKirsten Roschanski                $this->title = p_get_first_heading($ID);
11315923cb9SGerrit Uitslag            }
114ad18f4e1SGerrit Uitslag
1154b4cebc2SLarsDW223        } elseif($event->data == 'export_pdfns') {
116ad18f4e1SGerrit Uitslag            //check input for title and ns
11703352761SKirsten Roschanski            if(!$this->title = $INPUT->str('book_title')) {
11826be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
119ad18f4e1SGerrit Uitslag                return false;
120ad18f4e1SGerrit Uitslag            }
121177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
122ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
12326be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
124ad18f4e1SGerrit Uitslag                return false;
125ad18f4e1SGerrit Uitslag            }
126ad18f4e1SGerrit Uitslag
12726be4eceSGerrit Uitslag            //sort order
128177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
129ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
130ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
131ad18f4e1SGerrit Uitslag                $order = 'natural';
132ad18f4e1SGerrit Uitslag            }
133ad18f4e1SGerrit Uitslag
13426be4eceSGerrit Uitslag            //search depth
135177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
136ad18f4e1SGerrit Uitslag            if($depth < 0) {
137ad18f4e1SGerrit Uitslag                $depth = 0;
138ad18f4e1SGerrit Uitslag            }
13926be4eceSGerrit Uitslag
140ad18f4e1SGerrit Uitslag            //page search
141ad18f4e1SGerrit Uitslag            $result = array();
142ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
143ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
144ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
145ad18f4e1SGerrit Uitslag
14626be4eceSGerrit Uitslag            //sorting
147ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
148ad18f4e1SGerrit Uitslag                if($order == 'date') {
149ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
150ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
151ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
152ad18f4e1SGerrit Uitslag                }
153ad18f4e1SGerrit Uitslag            }
154ad18f4e1SGerrit Uitslag
155ad18f4e1SGerrit Uitslag            foreach($result as $item) {
156d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
157ad18f4e1SGerrit Uitslag            }
158ad18f4e1SGerrit Uitslag
159baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
160baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
161baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
162baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
163baa31dc5SGerrit Uitslag                    }
164baa31dc5SGerrit Uitslag                }
165baa31dc5SGerrit Uitslag            }
166baa31dc5SGerrit Uitslag
167737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
168b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator*/
16926be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
17003352761SKirsten Roschanski            $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED
17103352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
17203352761SKirsten Roschanski            if(empty($this->title)) {
17326be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
174737417c6SKlap-in                return false;
17526be4eceSGerrit Uitslag            } else {
176d63e7fe7SGerrit Uitslag                $list = explode("|", $_COOKIE['list-pagelist']);
17726be4eceSGerrit Uitslag            }
178ad18f4e1SGerrit Uitslag
179b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
180b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
181b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
182b3eed6e3SGerrit Uitslag//                http_status(403);
183b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
184b3eed6e3SGerrit Uitslag//                exit();
185b3eed6e3SGerrit Uitslag//            }
186b3eed6e3SGerrit Uitslag
187b3eed6e3SGerrit Uitslag            $json = new JSON(JSON_LOOSE_TYPE);
188b3eed6e3SGerrit Uitslag            $list = $json->decode($INPUT->post->str('selection', '', true));
189b3eed6e3SGerrit Uitslag            if(!is_array($list) || empty($list)) {
190b3eed6e3SGerrit Uitslag                http_status(400);
191b3eed6e3SGerrit Uitslag                print $this->getLang('empty');
192b3eed6e3SGerrit Uitslag                exit();
193b3eed6e3SGerrit Uitslag            }
194b3eed6e3SGerrit Uitslag
19503352761SKirsten Roschanski            $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED
19603352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
19703352761SKirsten Roschanski            if(empty($this->title)) {
198b3eed6e3SGerrit Uitslag                http_status(400);
199b3eed6e3SGerrit Uitslag                print $this->getLang('needtitle');
200b3eed6e3SGerrit Uitslag                exit();
201b3eed6e3SGerrit Uitslag            }
202b3eed6e3SGerrit Uitslag
203737417c6SKlap-in        } else {
20426be4eceSGerrit Uitslag            //show empty bookcreator message
20526be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
206737417c6SKlap-in            return false;
207737417c6SKlap-in        }
208737417c6SKlap-in
209719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
210c7138b3fSGerrit Uitslag
211c7138b3fSGerrit Uitslag        $skippedpages = array();
212c7138b3fSGerrit Uitslag        foreach($list as $index => $pageid) {
213c7138b3fSGerrit Uitslag            if(auth_quickaclcheck($pageid) < AUTH_READ) {
214c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
215c7138b3fSGerrit Uitslag                unset($list[$index]);
216c7138b3fSGerrit Uitslag            }
217c7138b3fSGerrit Uitslag        }
218c7138b3fSGerrit Uitslag        $list = array_filter($list); //removes also pages mentioned '0'
219c7138b3fSGerrit Uitslag
220c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
221c7138b3fSGerrit Uitslag        if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) {
222c7138b3fSGerrit Uitslag            $msg = hsc(join(', ', $skippedpages));
223c7138b3fSGerrit Uitslag            if($INPUT->has('selection')) {
224c7138b3fSGerrit Uitslag                http_status(400);
225c7138b3fSGerrit Uitslag                print sprintf($this->getLang('forbidden'), $msg);
226c7138b3fSGerrit Uitslag                exit();
227c7138b3fSGerrit Uitslag            } else {
228c7138b3fSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'forbidden', $msg);
229c7138b3fSGerrit Uitslag                return false;
230c7138b3fSGerrit Uitslag            }
231c7138b3fSGerrit Uitslag
232c7138b3fSGerrit Uitslag        }
233c7138b3fSGerrit Uitslag
23403352761SKirsten Roschanski        return array($this->title, $list);
235d63e7fe7SGerrit Uitslag    }
236d63e7fe7SGerrit Uitslag
237a58f45f0SGerrit Uitslag    /**
238a58f45f0SGerrit Uitslag     * Prepare cache
239a58f45f0SGerrit Uitslag     *
240a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
241a58f45f0SGerrit Uitslag     * @return cache
242a58f45f0SGerrit Uitslag     */
24303352761SKirsten Roschanski    protected function prepareCache(&$depends) {
244a58f45f0SGerrit Uitslag        global $REV;
245a58f45f0SGerrit Uitslag
246ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
247ee19bac3SLuigi Micco            . $REV
248ee19bac3SLuigi Micco            . $this->getExportConfig('template')
249ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
250ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
251d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
252ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
253ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
25403352761SKirsten Roschanski            . $this->title;
255ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
256ee19bac3SLuigi Micco
257ee19bac3SLuigi Micco        $dependencies = array();
258ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
259ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
260ee19bac3SLuigi Micco
261ee19bac3SLuigi Micco            if(is_array($relations)) {
262ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
263ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
264ee19bac3SLuigi Micco                        if($exists) {
265ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
266ee19bac3SLuigi Micco                        }
267ee19bac3SLuigi Micco                    }
268ee19bac3SLuigi Micco                }
269ee19bac3SLuigi Micco
270ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
271ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
272ee19bac3SLuigi Micco                        if($exists) {
273ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
274ee19bac3SLuigi Micco                        }
275ee19bac3SLuigi Micco                    }
276ee19bac3SLuigi Micco                }
277ee19bac3SLuigi Micco            }
278ee19bac3SLuigi Micco
279ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
280ee19bac3SLuigi Micco        }
281ee19bac3SLuigi Micco
282ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
283ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
284ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
285ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
286ee19bac3SLuigi Micco        $depends['files'] = array_merge(
287ee19bac3SLuigi Micco            $depends['files'],
288ee19bac3SLuigi Micco            $dependencies,
289ee19bac3SLuigi Micco            getConfigFiles('main')
290ee19bac3SLuigi Micco        );
291a58f45f0SGerrit Uitslag        return $cache;
292ee19bac3SLuigi Micco    }
293ee19bac3SLuigi Micco
294d63e7fe7SGerrit Uitslag    /**
295d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
296d63e7fe7SGerrit Uitslag     *
297d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
298d63e7fe7SGerrit Uitslag     * @param string $msglangkey key of translation key
299c7138b3fSGerrit Uitslag     * @param string $replacement
300d63e7fe7SGerrit Uitslag     */
301c7138b3fSGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) {
302c7138b3fSGerrit Uitslag        if(empty($replacement)) {
303c7138b3fSGerrit Uitslag            $msg = $this->getLang($msglangkey);
304c7138b3fSGerrit Uitslag        } else {
305c7138b3fSGerrit Uitslag            $msg = sprintf($this->getLang($msglangkey), $replacement);
306c7138b3fSGerrit Uitslag        }
307c7138b3fSGerrit Uitslag        msg($msg, -1);
308d63e7fe7SGerrit Uitslag
309d63e7fe7SGerrit Uitslag        $event->data = 'show';
310d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
311d63e7fe7SGerrit Uitslag    }
312d63e7fe7SGerrit Uitslag
313d63e7fe7SGerrit Uitslag    /**
314d63e7fe7SGerrit Uitslag     * Build a pdf from the html
315d63e7fe7SGerrit Uitslag     *
316d63e7fe7SGerrit Uitslag     * @param string $cachefile
317d63e7fe7SGerrit Uitslag     */
31803352761SKirsten Roschanski    protected function generatePDF($cachefile) {
319d63e7fe7SGerrit Uitslag        global $ID;
320d63e7fe7SGerrit Uitslag        global $REV;
321d63e7fe7SGerrit Uitslag        global $INPUT;
32287c86ddaSAndreas Gohr
32302f9a447SGerrit Uitslag        //some shortcuts to export settings
32402f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
32502f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
32602f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
3276ea88a05SAndreas Gohr
3281ef68647SAndreas Gohr        // initialize PDF library
329cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
3306ea88a05SAndreas Gohr
3314870b378SLarsDW223        $mpdf = new DokuPDF($this->getExportConfig('pagesize'),
3324870b378SLarsDW223                            $this->getExportConfig('orientation'),
3334870b378SLarsDW223                            $this->getExportConfig('font-size'));
334ee19bac3SLuigi Micco
335d62df65bSAndreas Gohr        // let mpdf fix local links
336d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
337d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
33802f9a447SGerrit Uitslag        if($self['port']) {
33902f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
34002f9a447SGerrit Uitslag        }
341d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
342d62df65bSAndreas Gohr
34356d13144SAndreas Gohr        // Set the title
34403352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
34556d13144SAndreas Gohr
346d63e7fe7SGerrit Uitslag        // some default document settings
347d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
348213fdb75SGerrit Uitslag        //      single-side document has only odd pages
349213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
350daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
351daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
35202f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
35302f9a447SGerrit Uitslag        if($hasToC) {
35402f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
35502f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
35602f9a447SGerrit Uitslag        } else {
35702f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
35802f9a447SGerrit Uitslag        }
3592eedf77dSAndreas Gohr
36056d13144SAndreas Gohr        // load the template
36103352761SKirsten Roschanski        $template = $this->load_template();
362ee19bac3SLuigi Micco
3631ef68647SAndreas Gohr        // prepare HTML header styles
364a2c33768SGerrit Uitslag        $html = '';
36502f9a447SGerrit Uitslag        if($isDebug) {
366a2c33768SGerrit Uitslag            $html .= '<html><head>';
367737417c6SKlap-in            $html .= '<style type="text/css">';
368a2c33768SGerrit Uitslag        }
369db1aa1bfSKirsten Roschanski
370db1aa1bfSKirsten Roschanski        $styles = '@page { size:auto; ' . $template['page'] . '}';
371a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
372254467c4SGerrit Uitslag
373254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
374254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
375254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
376254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
377db1aa1bfSKirsten Roschanski        $styles .= $this->load_css();
378254467c4SGerrit Uitslag
379a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
380a2c33768SGerrit Uitslag
38102f9a447SGerrit Uitslag        if($isDebug) {
382a2c33768SGerrit Uitslag            $html .= $styles;
3831ef68647SAndreas Gohr            $html .= '</style>';
3841ef68647SAndreas Gohr            $html .= '</head><body>';
385a2c33768SGerrit Uitslag        }
386a2c33768SGerrit Uitslag
387a2c33768SGerrit Uitslag        $body_start = $template['html'];
388a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3892eedf77dSAndreas Gohr
3901e45476bSmnapp        // insert the cover page
391a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
392a2c33768SGerrit Uitslag
393a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
39402f9a447SGerrit Uitslag        if($isDebug) {
395a2c33768SGerrit Uitslag            $html .= $body_start;
396a2c33768SGerrit Uitslag        }
39702f9a447SGerrit Uitslag        if($hasToC) {
39802f9a447SGerrit 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
39902f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
40002f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
40102f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
40202f9a447SGerrit Uitslag                array(
403230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
404230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
40502f9a447SGerrit Uitslag                    'links' => true,
40602f9a447SGerrit Uitslag                    'outdent' => '1em',
40702f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
40802f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
40902f9a447SGerrit Uitslag                )
41002f9a447SGerrit Uitslag            );
41102f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
41202f9a447SGerrit Uitslag        }
41302f9a447SGerrit Uitslag
414c00eb13bSGerrit Uitslag        // store original pageid
415c00eb13bSGerrit Uitslag        $keep = $ID;
416c00eb13bSGerrit Uitslag
4171ef68647SAndreas Gohr        // loop over all pages
418c7138b3fSGerrit Uitslag        $counter = 0;
419c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
420c7138b3fSGerrit Uitslag        foreach($this->list as $page) {
421c7138b3fSGerrit Uitslag            $counter++;
422b3eed6e3SGerrit Uitslag            $filename = wikiFN($page, $REV);
423b3eed6e3SGerrit Uitslag
424b3eed6e3SGerrit Uitslag            if(!file_exists($filename)) {
425b3eed6e3SGerrit Uitslag                continue;
426b3eed6e3SGerrit Uitslag            }
427ee19bac3SLuigi Micco
428c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
429c00eb13bSGerrit Uitslag            $ID = $page;
430c00eb13bSGerrit Uitslag
431b3eed6e3SGerrit Uitslag            $pagehtml = p_cached_output($filename, 'dw2pdf', $page);
432719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
433c7138b3fSGerrit Uitslag            if($counter < $no_pages) {
434a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
435a2c33768SGerrit Uitslag            }
436a2c33768SGerrit Uitslag
437a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
43802f9a447SGerrit Uitslag            if($isDebug) {
439a2c33768SGerrit Uitslag                $html .= $pagehtml;
4401ef68647SAndreas Gohr            }
441ee19bac3SLuigi Micco        }
442c00eb13bSGerrit Uitslag        //restore ID
443c00eb13bSGerrit Uitslag        $ID = $keep;
444ee19bac3SLuigi Micco
44533c15297SGerrit Uitslag        // insert the back page
446a2c33768SGerrit Uitslag        $body_end = $template['back'];
44733c15297SGerrit Uitslag
448a2c33768SGerrit Uitslag        $body_end .= '</div>';
449a2c33768SGerrit Uitslag
450d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
45102f9a447SGerrit Uitslag        if($isDebug) {
452a2c33768SGerrit Uitslag            $html .= $body_end;
453eeb17e15SAndreas Gohr            $html .= '</body>';
454eeb17e15SAndreas Gohr            $html .= '</html>';
455a2c33768SGerrit Uitslag        }
456f765508eSGerrit Uitslag
457f765508eSGerrit Uitslag        //Return html for debugging
45802f9a447SGerrit Uitslag        if($isDebug) {
45902f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
46026be4eceSGerrit Uitslag                echo $html;
461a2c33768SGerrit Uitslag            } else {
462a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
463a2c33768SGerrit Uitslag                echo $html;
464a2c33768SGerrit Uitslag            }
46526be4eceSGerrit Uitslag            exit();
46626be4eceSGerrit Uitslag        };
467f765508eSGerrit Uitslag
46887c86ddaSAndreas Gohr        // write to cache file
469d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
47087c86ddaSAndreas Gohr    }
47187c86ddaSAndreas Gohr
472d63e7fe7SGerrit Uitslag    /**
473d63e7fe7SGerrit Uitslag     * @param string $cachefile
474d63e7fe7SGerrit Uitslag     */
47503352761SKirsten Roschanski    protected function sendPDFFile($cachefile) {
47687c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
477b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
47887c86ddaSAndreas Gohr        header('Pragma: public');
479d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
480c0956f69SMichael Große        global $INPUT;
481f7b24f48SMichael Große        $outputTarget = $INPUT->str('outputTarget', $this->getConf('output'));
48287c86ddaSAndreas Gohr
48303352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
484c0956f69SMichael Große        if($outputTarget === 'file') {
4859a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
48687c86ddaSAndreas Gohr        } else {
4879a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
48887c86ddaSAndreas Gohr        }
489ee19bac3SLuigi Micco
490b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
491b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
492b3eed6e3SGerrit Uitslag
493e993da11SGerrit Uitslag        //try to send file, and exit if done
494d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
49587c86ddaSAndreas Gohr
496d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
49787c86ddaSAndreas Gohr        if($fp) {
498d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
49987c86ddaSAndreas Gohr        } else {
50087c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
50187c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
50287c86ddaSAndreas Gohr        }
5031ef68647SAndreas Gohr        exit();
5041ef68647SAndreas Gohr    }
5051ef68647SAndreas Gohr
5066be736bfSGerrit Uitslag    /**
5072eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
50844e8e8fbSGerrit Uitslag     *
50944e8e8fbSGerrit Uitslag     * @return array
5101ef68647SAndreas Gohr     */
51103352761SKirsten Roschanski    protected function load_template() {
5121ef68647SAndreas Gohr        global $ID;
5131ef68647SAndreas Gohr        global $conf;
5141ef68647SAndreas Gohr
5152eedf77dSAndreas Gohr        // this is what we'll return
5162eedf77dSAndreas Gohr        $output = array(
5171e45476bSmnapp            'cover' => '',
5182eedf77dSAndreas Gohr            'html'  => '',
5192eedf77dSAndreas Gohr            'page'  => '',
5202eedf77dSAndreas Gohr            'first' => '',
5212eedf77dSAndreas Gohr            'cite'  => '',
5222eedf77dSAndreas Gohr        );
5232eedf77dSAndreas Gohr
5242eedf77dSAndreas Gohr        // prepare header/footer elements
5252eedf77dSAndreas Gohr        $html = '';
52633c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
52733c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
52802f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
52933c15297SGerrit Uitslag                if(file_exists($file)) {
53033c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
53133c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
53233c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
5332eedf77dSAndreas Gohr
5342eedf77dSAndreas Gohr                    // register the needed pseudo CSS
53533c15297SGerrit Uitslag                    if($order == '_first') {
53633c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
53733c15297SGerrit Uitslag                    } elseif($order == '_even') {
53833c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
53933c15297SGerrit Uitslag                    } elseif($order == '_odd') {
54033c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
541daa70883SAndreas Gohr                    } else {
54233c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5432eedf77dSAndreas Gohr                    }
5442eedf77dSAndreas Gohr                }
5452eedf77dSAndreas Gohr            }
5462eedf77dSAndreas Gohr        }
5472eedf77dSAndreas Gohr
5481ef68647SAndreas Gohr        // prepare replacements
5491ef68647SAndreas Gohr        $replace = array(
5501ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
55102f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
55203352761SKirsten Roschanski            '@TITLE@'   => hsc($this->title),
5531ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5541ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5551ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5565d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
557893987a2SAndreas Gohr            '@INC@'     => DOKU_INC,
558893987a2SAndreas Gohr            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
559893987a2SAndreas Gohr            '@TPLINC@'  => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5601ef68647SAndreas Gohr        );
5611ef68647SAndreas Gohr
5622eedf77dSAndreas Gohr        // set HTML element
563a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
564a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
565a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5661ef68647SAndreas Gohr
5671e45476bSmnapp        // cover page
56802f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
56933c15297SGerrit Uitslag        if(file_exists($coverfile)) {
57033c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5711e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5729b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5736e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5741e45476bSmnapp        }
5751e45476bSmnapp
57633c15297SGerrit Uitslag        // cover page
57702f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
57833c15297SGerrit Uitslag        if(file_exists($backfile)) {
57933c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
58033c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
58133c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5829b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
58333c15297SGerrit Uitslag        }
58433c15297SGerrit Uitslag
5852eedf77dSAndreas Gohr        // citation box
58602f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
58733c15297SGerrit Uitslag        if(file_exists($citationfile)) {
58833c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5892eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5902eedf77dSAndreas Gohr        }
5911ef68647SAndreas Gohr
5922eedf77dSAndreas Gohr        return $output;
5931ef68647SAndreas Gohr    }
5941ef68647SAndreas Gohr
5951ef68647SAndreas Gohr    /**
596a180c973SKlap-in     * @param string $raw code with placeholders
597a180c973SKlap-in     * @param string $id  pageid
598a180c973SKlap-in     * @return string
599a180c973SKlap-in     */
600a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
601a180c973SKlap-in        global $REV;
602a180c973SKlap-in
603a180c973SKlap-in        // generate qr code for this page using google infographics api
604a180c973SKlap-in        $qr_code = '';
605a180c973SKlap-in        if($this->getConf('qrcodesize')) {
606a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
607a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
608a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
609a180c973SKlap-in        }
610a180c973SKlap-in        // prepare replacements
611a180c973SKlap-in        $replace['@ID@']      = $id;
612a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
613a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
614a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
615a180c973SKlap-in
616e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
617e3d68265SGerrit Uitslag
618e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
619e3d68265SGerrit Uitslag        $content = preg_replace_callback(
620e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
621e3d68265SGerrit Uitslag            array($this, 'replacedate'),
622e3d68265SGerrit Uitslag            $content
623e3d68265SGerrit Uitslag        );
624e3d68265SGerrit Uitslag
625e3d68265SGerrit Uitslag        return $content;
626a180c973SKlap-in    }
627a180c973SKlap-in
628e3d68265SGerrit Uitslag
629e3d68265SGerrit Uitslag    /**
630e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
631e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
632e3d68265SGerrit Uitslag     *
633e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
634e3d68265SGerrit Uitslag     * @return string
635e3d68265SGerrit Uitslag     */
636e3d68265SGerrit Uitslag    function replacedate($match) {
637e3d68265SGerrit Uitslag        global $conf;
638e3d68265SGerrit Uitslag        //no 2nd argument for default date format
639e3d68265SGerrit Uitslag        if($match[2] == null) {
640e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
641e3d68265SGerrit Uitslag        }
642e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
643e3d68265SGerrit Uitslag    }
644e3d68265SGerrit Uitslag
645e3d68265SGerrit Uitslag
646a180c973SKlap-in    /**
6471c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6481ef68647SAndreas Gohr     */
6491c14c879SAndreas Gohr    protected function load_css() {
650737417c6SKlap-in        global $conf;
6511c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6521c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6531c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
654ee19bac3SLuigi Micco
6551c14c879SAndreas Gohr        // prepare CSS files
6561c14c879SAndreas Gohr        $files = array_merge(
6571c14c879SAndreas Gohr            array(
6581c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6591c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6601c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6611c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6621c14c879SAndreas Gohr            ),
66358e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6641c14c879SAndreas Gohr            array(
6651c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6661c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6671c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6681c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6691c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6701c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6711c14c879SAndreas Gohr            )
6721c14c879SAndreas Gohr        );
6731c14c879SAndreas Gohr        $css = '';
6741c14c879SAndreas Gohr        foreach($files as $file => $location) {
67528e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
67628e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6771c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6781ef68647SAndreas Gohr        }
6791ef68647SAndreas Gohr
68028e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6811c14c879SAndreas Gohr            // apply pattern replacements
68228e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
68328e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
68428e636eaSGerrit Uitslag
68528e636eaSGerrit Uitslag            // parse less
68628e636eaSGerrit Uitslag            $css = css_parseless($css);
68728e636eaSGerrit Uitslag        } else {
68828e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6891c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
69028e636eaSGerrit Uitslag        }
6911ef68647SAndreas Gohr
6921c14c879SAndreas Gohr        return $css;
693ee19bac3SLuigi Micco    }
6941c14c879SAndreas Gohr
69558e6409eSAndreas Gohr    /**
69658e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
69758e6409eSAndreas Gohr     *
69858e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
69958e6409eSAndreas Gohr     *
70058e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
70158e6409eSAndreas Gohr     */
7026be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
70358e6409eSAndreas Gohr        $list = array();
70458e6409eSAndreas Gohr        $plugins = plugin_list();
705f54b51f7SAndreas Gohr
706f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
70758e6409eSAndreas Gohr        foreach($plugins as $p) {
708f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
709f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
710*8003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
711*8003b493SGerrit Uitslag
712f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
713*8003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
714f54b51f7SAndreas Gohr            }
715f54b51f7SAndreas Gohr
716*8003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
717*8003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
718*8003b493SGerrit Uitslag
71958e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
72058e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
721*8003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
72258e6409eSAndreas Gohr            } else {
72358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
724*8003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
72558e6409eSAndreas Gohr            }
72658e6409eSAndreas Gohr        }
72758e6409eSAndreas Gohr        return $list;
72858e6409eSAndreas Gohr    }
729ad18f4e1SGerrit Uitslag
730ad18f4e1SGerrit Uitslag    /**
73160e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
73260e59de7SGerrit Uitslag     *
73360e59de7SGerrit Uitslag     * @return array
73460e59de7SGerrit Uitslag     */
73560e59de7SGerrit Uitslag    public function getExportedPages() {
73660e59de7SGerrit Uitslag        return $this->list;
73760e59de7SGerrit Uitslag    }
73860e59de7SGerrit Uitslag
73960e59de7SGerrit Uitslag    /**
740ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
74144e8e8fbSGerrit Uitslag     *
74244e8e8fbSGerrit Uitslag     * @param array $a
74344e8e8fbSGerrit Uitslag     * @param array $b
74444e8e8fbSGerrit Uitslag     * @return int
745ad18f4e1SGerrit Uitslag     */
746ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
747ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
748ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
749ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
750ad18f4e1SGerrit Uitslag    }
751ad18f4e1SGerrit Uitslag
752ad18f4e1SGerrit Uitslag    /**
753ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
75444e8e8fbSGerrit Uitslag     * @param array $a
75544e8e8fbSGerrit Uitslag     * @param array $b
75644e8e8fbSGerrit Uitslag     * @return int
757ad18f4e1SGerrit Uitslag     */
758ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
759ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
760ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
761ad18f4e1SGerrit Uitslag        return 0;
762ad18f4e1SGerrit Uitslag    }
76326be4eceSGerrit Uitslag
76426be4eceSGerrit Uitslag    /**
76502f9a447SGerrit Uitslag     * Return settings read from:
76602f9a447SGerrit Uitslag     *   1. url parameters
76702f9a447SGerrit Uitslag     *   2. plugin config
76802f9a447SGerrit Uitslag     *   3. global config
76902f9a447SGerrit Uitslag     *
77002f9a447SGerrit Uitslag     * @return array
77102f9a447SGerrit Uitslag     */
77202f9a447SGerrit Uitslag    protected function loadExportConfig() {
77302f9a447SGerrit Uitslag        global $INPUT;
77402f9a447SGerrit Uitslag        global $conf;
77502f9a447SGerrit Uitslag
77602f9a447SGerrit Uitslag        $this->exportConfig = array();
77702f9a447SGerrit Uitslag
77802f9a447SGerrit Uitslag        // decide on the paper setup from param or config
77902f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
78002f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
78102f9a447SGerrit Uitslag
7824870b378SLarsDW223        // decide on the font-size from param or config
7834870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
7844870b378SLarsDW223
785213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
786213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
787213fdb75SGerrit Uitslag
788213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
78902f9a447SGerrit Uitslag        $levels = array();
79002f9a447SGerrit Uitslag        if($hasToC) {
79102f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
79202f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
79302f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
79402f9a447SGerrit Uitslag            $bounds_input = array(
79502f9a447SGerrit Uitslag                'top' => array(
79602f9a447SGerrit Uitslag                    (int) $top_input,
79702f9a447SGerrit Uitslag                    (int) $top_conf
79802f9a447SGerrit Uitslag                ),
79902f9a447SGerrit Uitslag                'max' => array(
80002f9a447SGerrit Uitslag                    (int) $max_input,
80102f9a447SGerrit Uitslag                    (int) $max_conf
80202f9a447SGerrit Uitslag                )
80302f9a447SGerrit Uitslag            );
80402f9a447SGerrit Uitslag            $bounds = array(
80502f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
80602f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
80702f9a447SGerrit Uitslag
80802f9a447SGerrit Uitslag            );
80902f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
81002f9a447SGerrit Uitslag                foreach($values as $value) {
81102f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
81202f9a447SGerrit Uitslag                        //stop at valid value and store
81302f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
81402f9a447SGerrit Uitslag                        break;
81502f9a447SGerrit Uitslag                    }
81602f9a447SGerrit Uitslag                }
81702f9a447SGerrit Uitslag            }
81802f9a447SGerrit Uitslag
81902f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
82002f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
82102f9a447SGerrit Uitslag            }
82202f9a447SGerrit Uitslag
82302f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
82402f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
82502f9a447SGerrit Uitslag            }
82602f9a447SGerrit Uitslag        }
82702f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
82802f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
82902f9a447SGerrit Uitslag
83002f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
83102f9a447SGerrit Uitslag
83202f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
83305d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
83402f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
83502f9a447SGerrit Uitslag            $tpl = $tplconf;
83602f9a447SGerrit Uitslag        }
83702f9a447SGerrit Uitslag        if(!$tpl){
83802f9a447SGerrit Uitslag            $tpl = 'default';
83902f9a447SGerrit Uitslag        }
84002f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
84102f9a447SGerrit Uitslag
84202f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
84302f9a447SGerrit Uitslag    }
84402f9a447SGerrit Uitslag
84502f9a447SGerrit Uitslag    /**
84602f9a447SGerrit Uitslag     * Returns requested config
84702f9a447SGerrit Uitslag     *
84802f9a447SGerrit Uitslag     * @param string $name
84902f9a447SGerrit Uitslag     * @param mixed  $notset
85002f9a447SGerrit Uitslag     * @return mixed|bool
85102f9a447SGerrit Uitslag     */
85202f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
85302f9a447SGerrit Uitslag        if ($this->exportConfig === null){
85402f9a447SGerrit Uitslag            $this->loadExportConfig();
85502f9a447SGerrit Uitslag        }
85602f9a447SGerrit Uitslag
85702f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
85802f9a447SGerrit Uitslag            return $this->exportConfig[$name];
85902f9a447SGerrit Uitslag        }else{
86002f9a447SGerrit Uitslag            return $notset;
86102f9a447SGerrit Uitslag        }
86202f9a447SGerrit Uitslag    }
863d63e7fe7SGerrit Uitslag
864d63e7fe7SGerrit Uitslag    /**
865d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
866d63e7fe7SGerrit Uitslag     *
867d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
868d63e7fe7SGerrit Uitslag     */
86944e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
870d63e7fe7SGerrit Uitslag        global $ID, $REV;
871d63e7fe7SGerrit Uitslag
872d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
873d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
874d63e7fe7SGerrit Uitslag            if($REV) {
875d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
876d63e7fe7SGerrit Uitslag            }
877d63e7fe7SGerrit Uitslag
878d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
879d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
880d63e7fe7SGerrit Uitslag                array('export_pdf' =>
881d63e7fe7SGerrit Uitslag                          '<li>'
88272cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
883d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
884d63e7fe7SGerrit Uitslag                          . '</a>'
885d63e7fe7SGerrit Uitslag                          . '</li>'
886d63e7fe7SGerrit Uitslag                ) +
887d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
888d63e7fe7SGerrit Uitslag        }
889d63e7fe7SGerrit Uitslag    }
890026b594bSAndreas Gohr
891026b594bSAndreas Gohr    /**
892026b594bSAndreas Gohr     * Add 'export pdf' button to page tools, new SVG based mechanism
893026b594bSAndreas Gohr     *
894026b594bSAndreas Gohr     * @param Doku_Event $event
895026b594bSAndreas Gohr     */
896026b594bSAndreas Gohr    public function addsvgbutton(Doku_Event $event) {
897026b594bSAndreas Gohr        if($event->data['view'] != 'page') return;
898026b594bSAndreas Gohr        array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\dw2pdf\MenuItem()]);
899026b594bSAndreas Gohr    }
900ee19bac3SLuigi Micco}
901