xref: /plugin/dw2pdf/action.php (revision 03352761e51b7cdc034c745818baaa9b931f2797)
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;
27*03352761SKirsten Roschanski    protected $title;
2860e59de7SGerrit Uitslag    protected $list = array();
291c14c879SAndreas Gohr
301c14c879SAndreas Gohr    /**
311c14c879SAndreas Gohr     * Constructor. Sets the correct template
32*03352761SKirsten Roschanski     *
33*03352761SKirsten Roschanski     * @param string $title
341c14c879SAndreas Gohr     */
35*03352761SKirsten Roschanski    public function __construct($title=null) {
3602f9a447SGerrit Uitslag        $this->tpl   = $this->getExportConfig('template');
37*03352761SKirsten 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());
48ee19bac3SLuigi Micco    }
49ee19bac3SLuigi Micco
501c14c879SAndreas Gohr    /**
511c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
52737417c6SKlap-in     *
53737417c6SKlap-in     * @param Doku_Event $event
54737417c6SKlap-in     * @return bool
551c14c879SAndreas Gohr     */
5644e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
57ee19bac3SLuigi Micco        global $ACT;
58ee19bac3SLuigi Micco        global $ID;
59ee19bac3SLuigi Micco
601ef68647SAndreas Gohr        // our event?
61ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != '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)) {
67*03352761SKirsten 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();
77*03352761SKirsten 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
84*03352761SKirsten Roschanski            $this->generatePDF($cache->cache);
85d63e7fe7SGerrit Uitslag        }
86d63e7fe7SGerrit Uitslag
87d63e7fe7SGerrit Uitslag        // deliver the file
88*03352761SKirsten 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 $ACT;
101d63e7fe7SGerrit Uitslag        global $ID;
102d63e7fe7SGerrit Uitslag        global $INPUT;
103d63e7fe7SGerrit Uitslag        global $conf;
104d63e7fe7SGerrit Uitslag
105d63e7fe7SGerrit Uitslag        // list of one or multiple pages
106d63e7fe7SGerrit Uitslag        $list = array();
10728e636eaSGerrit Uitslag
10887c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
109d63e7fe7SGerrit Uitslag            $list[0] = $ID;
110*03352761SKirsten Roschanski            $this->title = $INPUT->str('pdftitle'); //DEPRECATED
111*03352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
112*03352761SKirsten Roschanski            if(empty($this->title)) {
113*03352761SKirsten Roschanski                $this->title = p_get_first_heading($ID);
11415923cb9SGerrit Uitslag            }
115ad18f4e1SGerrit Uitslag
116ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
117ad18f4e1SGerrit Uitslag            //check input for title and ns
118*03352761SKirsten Roschanski            if(!$this->title = $INPUT->str('book_title')) {
11926be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
120ad18f4e1SGerrit Uitslag                return false;
121ad18f4e1SGerrit Uitslag            }
122177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
123ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
12426be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
125ad18f4e1SGerrit Uitslag                return false;
126ad18f4e1SGerrit Uitslag            }
127ad18f4e1SGerrit Uitslag
12826be4eceSGerrit Uitslag            //sort order
129177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
130ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
131ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
132ad18f4e1SGerrit Uitslag                $order = 'natural';
133ad18f4e1SGerrit Uitslag            }
134ad18f4e1SGerrit Uitslag
13526be4eceSGerrit Uitslag            //search depth
136177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
137ad18f4e1SGerrit Uitslag            if($depth < 0) {
138ad18f4e1SGerrit Uitslag                $depth = 0;
139ad18f4e1SGerrit Uitslag            }
14026be4eceSGerrit Uitslag
141ad18f4e1SGerrit Uitslag            //page search
142ad18f4e1SGerrit Uitslag            $result = array();
143ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
144ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
145ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
146ad18f4e1SGerrit Uitslag
14726be4eceSGerrit Uitslag            //sorting
148ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
149ad18f4e1SGerrit Uitslag                if($order == 'date') {
150ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
151ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
152ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
153ad18f4e1SGerrit Uitslag                }
154ad18f4e1SGerrit Uitslag            }
155ad18f4e1SGerrit Uitslag
156ad18f4e1SGerrit Uitslag            foreach($result as $item) {
157d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
158ad18f4e1SGerrit Uitslag            }
159ad18f4e1SGerrit Uitslag
160baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
161baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
162baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
163baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
164baa31dc5SGerrit Uitslag                    }
165baa31dc5SGerrit Uitslag                }
166baa31dc5SGerrit Uitslag            }
167baa31dc5SGerrit Uitslag
168737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
169b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator*/
17026be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
171*03352761SKirsten Roschanski            $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED
172*03352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
173*03352761SKirsten Roschanski            if(empty($this->title)) {
17426be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
175737417c6SKlap-in                return false;
17626be4eceSGerrit Uitslag            } else {
177d63e7fe7SGerrit Uitslag                $list = explode("|", $_COOKIE['list-pagelist']);
17826be4eceSGerrit Uitslag            }
179ad18f4e1SGerrit Uitslag
180b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
181b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
182b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
183b3eed6e3SGerrit Uitslag//                http_status(403);
184b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
185b3eed6e3SGerrit Uitslag//                exit();
186b3eed6e3SGerrit Uitslag//            }
187b3eed6e3SGerrit Uitslag
188b3eed6e3SGerrit Uitslag            $json = new JSON(JSON_LOOSE_TYPE);
189b3eed6e3SGerrit Uitslag            $list = $json->decode($INPUT->post->str('selection', '', true));
190b3eed6e3SGerrit Uitslag            if(!is_array($list) || empty($list)) {
191b3eed6e3SGerrit Uitslag                http_status(400);
192b3eed6e3SGerrit Uitslag                print $this->getLang('empty');
193b3eed6e3SGerrit Uitslag                exit();
194b3eed6e3SGerrit Uitslag            }
195b3eed6e3SGerrit Uitslag
196*03352761SKirsten Roschanski            $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED
197*03352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
198*03352761SKirsten Roschanski            if(empty($this->title)) {
199b3eed6e3SGerrit Uitslag                http_status(400);
200b3eed6e3SGerrit Uitslag                print $this->getLang('needtitle');
201b3eed6e3SGerrit Uitslag                exit();
202b3eed6e3SGerrit Uitslag            }
203b3eed6e3SGerrit Uitslag
204737417c6SKlap-in        } else {
20526be4eceSGerrit Uitslag            //show empty bookcreator message
20626be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
207737417c6SKlap-in            return false;
208737417c6SKlap-in        }
209737417c6SKlap-in
210719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
211c7138b3fSGerrit Uitslag
212c7138b3fSGerrit Uitslag        $skippedpages = array();
213c7138b3fSGerrit Uitslag        foreach($list as $index => $pageid) {
214c7138b3fSGerrit Uitslag            if(auth_quickaclcheck($pageid) < AUTH_READ) {
215c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
216c7138b3fSGerrit Uitslag                unset($list[$index]);
217c7138b3fSGerrit Uitslag            }
218c7138b3fSGerrit Uitslag        }
219c7138b3fSGerrit Uitslag        $list = array_filter($list); //removes also pages mentioned '0'
220c7138b3fSGerrit Uitslag
221c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
222c7138b3fSGerrit Uitslag        if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) {
223c7138b3fSGerrit Uitslag            $msg = hsc(join(', ', $skippedpages));
224c7138b3fSGerrit Uitslag            if($INPUT->has('selection')) {
225c7138b3fSGerrit Uitslag                http_status(400);
226c7138b3fSGerrit Uitslag                print sprintf($this->getLang('forbidden'), $msg);
227c7138b3fSGerrit Uitslag                exit();
228c7138b3fSGerrit Uitslag            } else {
229c7138b3fSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'forbidden', $msg);
230c7138b3fSGerrit Uitslag                return false;
231c7138b3fSGerrit Uitslag            }
232c7138b3fSGerrit Uitslag
233c7138b3fSGerrit Uitslag        }
234c7138b3fSGerrit Uitslag
235*03352761SKirsten Roschanski        return array($this->title, $list);
236d63e7fe7SGerrit Uitslag    }
237d63e7fe7SGerrit Uitslag
238a58f45f0SGerrit Uitslag    /**
239a58f45f0SGerrit Uitslag     * Prepare cache
240a58f45f0SGerrit Uitslag     *
241a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
242a58f45f0SGerrit Uitslag     * @return cache
243a58f45f0SGerrit Uitslag     */
244*03352761SKirsten Roschanski    protected function prepareCache(&$depends) {
245a58f45f0SGerrit Uitslag        global $REV;
246a58f45f0SGerrit Uitslag
247ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
248ee19bac3SLuigi Micco            . $REV
249ee19bac3SLuigi Micco            . $this->getExportConfig('template')
250ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
251ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
252d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
253ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
254ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
255*03352761SKirsten Roschanski            . $this->title;
256ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
257ee19bac3SLuigi Micco
258ee19bac3SLuigi Micco        $dependencies = array();
259ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
260ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
261ee19bac3SLuigi Micco
262ee19bac3SLuigi Micco            if(is_array($relations)) {
263ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
264ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
265ee19bac3SLuigi Micco                        if($exists) {
266ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
267ee19bac3SLuigi Micco                        }
268ee19bac3SLuigi Micco                    }
269ee19bac3SLuigi Micco                }
270ee19bac3SLuigi Micco
271ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
272ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
273ee19bac3SLuigi Micco                        if($exists) {
274ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
275ee19bac3SLuigi Micco                        }
276ee19bac3SLuigi Micco                    }
277ee19bac3SLuigi Micco                }
278ee19bac3SLuigi Micco            }
279ee19bac3SLuigi Micco
280ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
281ee19bac3SLuigi Micco        }
282ee19bac3SLuigi Micco
283ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
284ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
285ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
286ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
287ee19bac3SLuigi Micco        $depends['files'] = array_merge(
288ee19bac3SLuigi Micco            $depends['files'],
289ee19bac3SLuigi Micco            $dependencies,
290ee19bac3SLuigi Micco            getConfigFiles('main')
291ee19bac3SLuigi Micco        );
292a58f45f0SGerrit Uitslag        return $cache;
293ee19bac3SLuigi Micco    }
294ee19bac3SLuigi Micco
295d63e7fe7SGerrit Uitslag    /**
296d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
297d63e7fe7SGerrit Uitslag     *
298d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
299d63e7fe7SGerrit Uitslag     * @param string $msglangkey key of translation key
300c7138b3fSGerrit Uitslag     * @param string $replacement
301d63e7fe7SGerrit Uitslag     */
302c7138b3fSGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) {
303c7138b3fSGerrit Uitslag        if(empty($replacement)) {
304c7138b3fSGerrit Uitslag            $msg = $this->getLang($msglangkey);
305c7138b3fSGerrit Uitslag        } else {
306c7138b3fSGerrit Uitslag            $msg = sprintf($this->getLang($msglangkey), $replacement);
307c7138b3fSGerrit Uitslag        }
308c7138b3fSGerrit Uitslag        msg($msg, -1);
309d63e7fe7SGerrit Uitslag
310d63e7fe7SGerrit Uitslag        $event->data = 'show';
311d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
312d63e7fe7SGerrit Uitslag    }
313d63e7fe7SGerrit Uitslag
314d63e7fe7SGerrit Uitslag    /**
315d63e7fe7SGerrit Uitslag     * Build a pdf from the html
316d63e7fe7SGerrit Uitslag     *
317d63e7fe7SGerrit Uitslag     * @param string $cachefile
318d63e7fe7SGerrit Uitslag     */
319*03352761SKirsten Roschanski    protected function generatePDF($cachefile) {
320d63e7fe7SGerrit Uitslag        global $ID;
321d63e7fe7SGerrit Uitslag        global $REV;
322d63e7fe7SGerrit Uitslag        global $INPUT;
32387c86ddaSAndreas Gohr
32402f9a447SGerrit Uitslag        //some shortcuts to export settings
32502f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
32602f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
32702f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
3286ea88a05SAndreas Gohr
3291ef68647SAndreas Gohr        // initialize PDF library
330cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
3316ea88a05SAndreas Gohr
3324870b378SLarsDW223        $mpdf = new DokuPDF($this->getExportConfig('pagesize'),
3334870b378SLarsDW223                            $this->getExportConfig('orientation'),
3344870b378SLarsDW223                            $this->getExportConfig('font-size'));
335ee19bac3SLuigi Micco
336d62df65bSAndreas Gohr        // let mpdf fix local links
337d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
338d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
33902f9a447SGerrit Uitslag        if($self['port']) {
34002f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
34102f9a447SGerrit Uitslag        }
342d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
343d62df65bSAndreas Gohr
34456d13144SAndreas Gohr        // Set the title
345*03352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
34656d13144SAndreas Gohr
347d63e7fe7SGerrit Uitslag        // some default document settings
348d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
349213fdb75SGerrit Uitslag        //      single-side document has only odd pages
350213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
351daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
352daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
35302f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
35402f9a447SGerrit Uitslag        if($hasToC) {
35502f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
35602f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
35702f9a447SGerrit Uitslag        } else {
35802f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
35902f9a447SGerrit Uitslag        }
3602eedf77dSAndreas Gohr
36156d13144SAndreas Gohr        // load the template
362*03352761SKirsten Roschanski        $template = $this->load_template();
363ee19bac3SLuigi Micco
3641ef68647SAndreas Gohr        // prepare HTML header styles
365a2c33768SGerrit Uitslag        $html = '';
36602f9a447SGerrit Uitslag        if($isDebug) {
367a2c33768SGerrit Uitslag            $html .= '<html><head>';
368737417c6SKlap-in            $html .= '<style type="text/css">';
369a2c33768SGerrit Uitslag        }
370a2c33768SGerrit Uitslag        $styles = $this->load_css();
371a2c33768SGerrit Uitslag        $styles .= '@page { size:auto; ' . $template['page'] . '}';
372a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
373254467c4SGerrit Uitslag
374254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
375254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
376254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
377254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
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     */
475*03352761SKirsten 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));
48087c86ddaSAndreas Gohr
481*03352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
48287c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4839a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
48487c86ddaSAndreas Gohr        } else {
4859a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
48687c86ddaSAndreas Gohr        }
487ee19bac3SLuigi Micco
488b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
489b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
490b3eed6e3SGerrit Uitslag
491e993da11SGerrit Uitslag        //try to send file, and exit if done
492d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
49387c86ddaSAndreas Gohr
494d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
49587c86ddaSAndreas Gohr        if($fp) {
496d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
49787c86ddaSAndreas Gohr        } else {
49887c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
49987c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
50087c86ddaSAndreas Gohr        }
5011ef68647SAndreas Gohr        exit();
5021ef68647SAndreas Gohr    }
5031ef68647SAndreas Gohr
5046be736bfSGerrit Uitslag    /**
5052eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
50644e8e8fbSGerrit Uitslag     *
50744e8e8fbSGerrit Uitslag     * @return array
5081ef68647SAndreas Gohr     */
509*03352761SKirsten Roschanski    protected function load_template() {
5101ef68647SAndreas Gohr        global $ID;
5111ef68647SAndreas Gohr        global $conf;
5121ef68647SAndreas Gohr
5132eedf77dSAndreas Gohr        // this is what we'll return
5142eedf77dSAndreas Gohr        $output = array(
5151e45476bSmnapp            'cover' => '',
5162eedf77dSAndreas Gohr            'html'  => '',
5172eedf77dSAndreas Gohr            'page'  => '',
5182eedf77dSAndreas Gohr            'first' => '',
5192eedf77dSAndreas Gohr            'cite'  => '',
5202eedf77dSAndreas Gohr        );
5212eedf77dSAndreas Gohr
5222eedf77dSAndreas Gohr        // prepare header/footer elements
5232eedf77dSAndreas Gohr        $html = '';
52433c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
52533c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
52602f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
52733c15297SGerrit Uitslag                if(file_exists($file)) {
52833c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
52933c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
53033c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
5312eedf77dSAndreas Gohr
5322eedf77dSAndreas Gohr                    // register the needed pseudo CSS
53333c15297SGerrit Uitslag                    if($order == '_first') {
53433c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
53533c15297SGerrit Uitslag                    } elseif($order == '_even') {
53633c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
53733c15297SGerrit Uitslag                    } elseif($order == '_odd') {
53833c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
539daa70883SAndreas Gohr                    } else {
54033c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5412eedf77dSAndreas Gohr                    }
5422eedf77dSAndreas Gohr                }
5432eedf77dSAndreas Gohr            }
5442eedf77dSAndreas Gohr        }
5452eedf77dSAndreas Gohr
5461ef68647SAndreas Gohr        // prepare replacements
5471ef68647SAndreas Gohr        $replace = array(
5481ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
54902f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
550*03352761SKirsten Roschanski            '@TITLE@'   => hsc($this->title),
5511ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5521ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5531ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5545d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
55502f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5561ef68647SAndreas Gohr        );
5571ef68647SAndreas Gohr
5582eedf77dSAndreas Gohr        // set HTML element
559a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
560a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
561a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5621ef68647SAndreas Gohr
5631e45476bSmnapp        // cover page
56402f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
56533c15297SGerrit Uitslag        if(file_exists($coverfile)) {
56633c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5671e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5689b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5696e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5701e45476bSmnapp        }
5711e45476bSmnapp
57233c15297SGerrit Uitslag        // cover page
57302f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
57433c15297SGerrit Uitslag        if(file_exists($backfile)) {
57533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
57633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
57733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5789b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
57933c15297SGerrit Uitslag        }
58033c15297SGerrit Uitslag
5812eedf77dSAndreas Gohr        // citation box
58202f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
58333c15297SGerrit Uitslag        if(file_exists($citationfile)) {
58433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5852eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5862eedf77dSAndreas Gohr        }
5871ef68647SAndreas Gohr
5882eedf77dSAndreas Gohr        return $output;
5891ef68647SAndreas Gohr    }
5901ef68647SAndreas Gohr
5911ef68647SAndreas Gohr    /**
592a180c973SKlap-in     * @param string $raw code with placeholders
593a180c973SKlap-in     * @param string $id  pageid
594a180c973SKlap-in     * @return string
595a180c973SKlap-in     */
596a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
597a180c973SKlap-in        global $REV;
598a180c973SKlap-in
599a180c973SKlap-in        // generate qr code for this page using google infographics api
600a180c973SKlap-in        $qr_code = '';
601a180c973SKlap-in        if($this->getConf('qrcodesize')) {
602a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
603a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
604a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
605a180c973SKlap-in        }
606a180c973SKlap-in        // prepare replacements
607a180c973SKlap-in        $replace['@ID@']      = $id;
608a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
609a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
610a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
611a180c973SKlap-in
612e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
613e3d68265SGerrit Uitslag
614e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
615e3d68265SGerrit Uitslag        $content = preg_replace_callback(
616e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
617e3d68265SGerrit Uitslag            array($this, 'replacedate'),
618e3d68265SGerrit Uitslag            $content
619e3d68265SGerrit Uitslag        );
620e3d68265SGerrit Uitslag
621e3d68265SGerrit Uitslag        return $content;
622a180c973SKlap-in    }
623a180c973SKlap-in
624e3d68265SGerrit Uitslag
625e3d68265SGerrit Uitslag    /**
626e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
627e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
628e3d68265SGerrit Uitslag     *
629e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
630e3d68265SGerrit Uitslag     * @return string
631e3d68265SGerrit Uitslag     */
632e3d68265SGerrit Uitslag    function replacedate($match) {
633e3d68265SGerrit Uitslag        global $conf;
634e3d68265SGerrit Uitslag        //no 2nd argument for default date format
635e3d68265SGerrit Uitslag        if($match[2] == null) {
636e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
637e3d68265SGerrit Uitslag        }
638e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
639e3d68265SGerrit Uitslag    }
640e3d68265SGerrit Uitslag
641e3d68265SGerrit Uitslag
642a180c973SKlap-in    /**
6431c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6441ef68647SAndreas Gohr     */
6451c14c879SAndreas Gohr    protected function load_css() {
646737417c6SKlap-in        global $conf;
6471c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6481c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6491c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
650ee19bac3SLuigi Micco
6511c14c879SAndreas Gohr        // prepare CSS files
6521c14c879SAndreas Gohr        $files = array_merge(
6531c14c879SAndreas Gohr            array(
6541c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6551c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6561c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6571c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6581c14c879SAndreas Gohr            ),
6591c14c879SAndreas Gohr            css_pluginstyles('all'),
66058e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6611c14c879SAndreas Gohr            array(
6621c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6631c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6641c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6651c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6661c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6671c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6681c14c879SAndreas Gohr            )
6691c14c879SAndreas Gohr        );
6701c14c879SAndreas Gohr        $css = '';
6711c14c879SAndreas Gohr        foreach($files as $file => $location) {
67228e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
67328e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6741c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6751ef68647SAndreas Gohr        }
6761ef68647SAndreas Gohr
67728e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6781c14c879SAndreas Gohr            // apply pattern replacements
67928e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
68028e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
68128e636eaSGerrit Uitslag
68228e636eaSGerrit Uitslag            // parse less
68328e636eaSGerrit Uitslag            $css = css_parseless($css);
68428e636eaSGerrit Uitslag        } else {
68528e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6861c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
68728e636eaSGerrit Uitslag        }
6881ef68647SAndreas Gohr
6891c14c879SAndreas Gohr        return $css;
690ee19bac3SLuigi Micco    }
6911c14c879SAndreas Gohr
69258e6409eSAndreas Gohr    /**
69358e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
69458e6409eSAndreas Gohr     *
69558e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
69658e6409eSAndreas Gohr     *
69758e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
69858e6409eSAndreas Gohr     */
6996be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
70058e6409eSAndreas Gohr        $list = array();
70158e6409eSAndreas Gohr        $plugins = plugin_list();
702f54b51f7SAndreas Gohr
703f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
70458e6409eSAndreas Gohr        foreach($plugins as $p) {
705f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
706f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
707f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
708f54b51f7SAndreas Gohr            }
709f54b51f7SAndreas Gohr
71058e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
71158e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
71258e6409eSAndreas Gohr            } else {
71358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
71458e6409eSAndreas Gohr            }
71558e6409eSAndreas Gohr        }
71658e6409eSAndreas Gohr        return $list;
71758e6409eSAndreas Gohr    }
718ad18f4e1SGerrit Uitslag
719ad18f4e1SGerrit Uitslag    /**
72060e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
72160e59de7SGerrit Uitslag     *
72260e59de7SGerrit Uitslag     * @return array
72360e59de7SGerrit Uitslag     */
72460e59de7SGerrit Uitslag    public function getExportedPages() {
72560e59de7SGerrit Uitslag        return $this->list;
72660e59de7SGerrit Uitslag    }
72760e59de7SGerrit Uitslag
72860e59de7SGerrit Uitslag    /**
729ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
73044e8e8fbSGerrit Uitslag     *
73144e8e8fbSGerrit Uitslag     * @param array $a
73244e8e8fbSGerrit Uitslag     * @param array $b
73344e8e8fbSGerrit Uitslag     * @return int
734ad18f4e1SGerrit Uitslag     */
735ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
736ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
737ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
738ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
739ad18f4e1SGerrit Uitslag    }
740ad18f4e1SGerrit Uitslag
741ad18f4e1SGerrit Uitslag    /**
742ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
74344e8e8fbSGerrit Uitslag     * @param array $a
74444e8e8fbSGerrit Uitslag     * @param array $b
74544e8e8fbSGerrit Uitslag     * @return int
746ad18f4e1SGerrit Uitslag     */
747ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
748ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
749ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
750ad18f4e1SGerrit Uitslag        return 0;
751ad18f4e1SGerrit Uitslag    }
75226be4eceSGerrit Uitslag
75326be4eceSGerrit Uitslag    /**
75402f9a447SGerrit Uitslag     * Return settings read from:
75502f9a447SGerrit Uitslag     *   1. url parameters
75602f9a447SGerrit Uitslag     *   2. plugin config
75702f9a447SGerrit Uitslag     *   3. global config
75802f9a447SGerrit Uitslag     *
75902f9a447SGerrit Uitslag     * @return array
76002f9a447SGerrit Uitslag     */
76102f9a447SGerrit Uitslag    protected function loadExportConfig() {
76202f9a447SGerrit Uitslag        global $INPUT;
76302f9a447SGerrit Uitslag        global $conf;
76402f9a447SGerrit Uitslag
76502f9a447SGerrit Uitslag        $this->exportConfig = array();
76602f9a447SGerrit Uitslag
76702f9a447SGerrit Uitslag        // decide on the paper setup from param or config
76802f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
76902f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
77002f9a447SGerrit Uitslag
7714870b378SLarsDW223        // decide on the font-size from param or config
7724870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
7734870b378SLarsDW223
774213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
775213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
776213fdb75SGerrit Uitslag
777213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
77802f9a447SGerrit Uitslag        $levels = array();
77902f9a447SGerrit Uitslag        if($hasToC) {
78002f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
78102f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
78202f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
78302f9a447SGerrit Uitslag            $bounds_input = array(
78402f9a447SGerrit Uitslag                'top' => array(
78502f9a447SGerrit Uitslag                    (int) $top_input,
78602f9a447SGerrit Uitslag                    (int) $top_conf
78702f9a447SGerrit Uitslag                ),
78802f9a447SGerrit Uitslag                'max' => array(
78902f9a447SGerrit Uitslag                    (int) $max_input,
79002f9a447SGerrit Uitslag                    (int) $max_conf
79102f9a447SGerrit Uitslag                )
79202f9a447SGerrit Uitslag            );
79302f9a447SGerrit Uitslag            $bounds = array(
79402f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
79502f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
79602f9a447SGerrit Uitslag
79702f9a447SGerrit Uitslag            );
79802f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
79902f9a447SGerrit Uitslag                foreach($values as $value) {
80002f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
80102f9a447SGerrit Uitslag                        //stop at valid value and store
80202f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
80302f9a447SGerrit Uitslag                        break;
80402f9a447SGerrit Uitslag                    }
80502f9a447SGerrit Uitslag                }
80602f9a447SGerrit Uitslag            }
80702f9a447SGerrit Uitslag
80802f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
80902f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
81002f9a447SGerrit Uitslag            }
81102f9a447SGerrit Uitslag
81202f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
81302f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
81402f9a447SGerrit Uitslag            }
81502f9a447SGerrit Uitslag        }
81602f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
81702f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
81802f9a447SGerrit Uitslag
81902f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
82002f9a447SGerrit Uitslag
82102f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
82205d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
82302f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
82402f9a447SGerrit Uitslag            $tpl = $tplconf;
82502f9a447SGerrit Uitslag        }
82602f9a447SGerrit Uitslag        if(!$tpl){
82702f9a447SGerrit Uitslag            $tpl = 'default';
82802f9a447SGerrit Uitslag        }
82902f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
83002f9a447SGerrit Uitslag
83102f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
83202f9a447SGerrit Uitslag    }
83302f9a447SGerrit Uitslag
83402f9a447SGerrit Uitslag    /**
83502f9a447SGerrit Uitslag     * Returns requested config
83602f9a447SGerrit Uitslag     *
83702f9a447SGerrit Uitslag     * @param string $name
83802f9a447SGerrit Uitslag     * @param mixed  $notset
83902f9a447SGerrit Uitslag     * @return mixed|bool
84002f9a447SGerrit Uitslag     */
84102f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
84202f9a447SGerrit Uitslag        if ($this->exportConfig === null){
84302f9a447SGerrit Uitslag            $this->loadExportConfig();
84402f9a447SGerrit Uitslag        }
84502f9a447SGerrit Uitslag
84602f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
84702f9a447SGerrit Uitslag            return $this->exportConfig[$name];
84802f9a447SGerrit Uitslag        }else{
84902f9a447SGerrit Uitslag            return $notset;
85002f9a447SGerrit Uitslag        }
85102f9a447SGerrit Uitslag    }
852d63e7fe7SGerrit Uitslag
853d63e7fe7SGerrit Uitslag    /**
854d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
855d63e7fe7SGerrit Uitslag     *
856d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
857d63e7fe7SGerrit Uitslag     */
85844e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
859d63e7fe7SGerrit Uitslag        global $ID, $REV;
860d63e7fe7SGerrit Uitslag
861d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
862d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
863d63e7fe7SGerrit Uitslag            if($REV) {
864d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
865d63e7fe7SGerrit Uitslag            }
866d63e7fe7SGerrit Uitslag
867d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
868d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
869d63e7fe7SGerrit Uitslag                array('export_pdf' =>
870d63e7fe7SGerrit Uitslag                          '<li>'
87172cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
872d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
873d63e7fe7SGerrit Uitslag                          . '</a>'
874d63e7fe7SGerrit Uitslag                          . '</li>'
875d63e7fe7SGerrit Uitslag                ) +
876d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
877d63e7fe7SGerrit Uitslag        }
878d63e7fe7SGerrit Uitslag    }
879ee19bac3SLuigi Micco}
880