xref: /plugin/dw2pdf/action.php (revision b34cb34e84202e2dff9433aad8b03bd96450396c)
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());
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)) {
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);
83*b34cb34eSSzymon Olewniczak            try {
8403352761SKirsten Roschanski                $this->generatePDF($cache->cache);
85*b34cb34eSSzymon Olewniczak            } catch (Mpdf\MpdfException $e) {
86*b34cb34eSSzymon Olewniczak                //prevent act_export()
87*b34cb34eSSzymon Olewniczak                $ACT = 'show';
88*b34cb34eSSzymon Olewniczak                msg($e->getMessage(), -1);
89*b34cb34eSSzymon Olewniczak                return false;
90*b34cb34eSSzymon Olewniczak            }
91*b34cb34eSSzymon Olewniczak
92d63e7fe7SGerrit Uitslag        }
93d63e7fe7SGerrit Uitslag
94d63e7fe7SGerrit Uitslag        // deliver the file
9503352761SKirsten Roschanski        $this->sendPDFFile($cache->cache);
96d63e7fe7SGerrit Uitslag        return true;
97d63e7fe7SGerrit Uitslag    }
98d63e7fe7SGerrit Uitslag
99d63e7fe7SGerrit Uitslag
100d63e7fe7SGerrit Uitslag    /**
101d63e7fe7SGerrit Uitslag     * Obtain list of pages and title, based on url parameters
102d63e7fe7SGerrit Uitslag     *
103d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
104d63e7fe7SGerrit Uitslag     * @return string|bool
105d63e7fe7SGerrit Uitslag     */
106d63e7fe7SGerrit Uitslag    protected function collectExportPages(Doku_Event $event) {
107d63e7fe7SGerrit Uitslag        global $ACT;
108d63e7fe7SGerrit Uitslag        global $ID;
109d63e7fe7SGerrit Uitslag        global $INPUT;
110d63e7fe7SGerrit Uitslag        global $conf;
111d63e7fe7SGerrit Uitslag
112d63e7fe7SGerrit Uitslag        // list of one or multiple pages
113d63e7fe7SGerrit Uitslag        $list = array();
11428e636eaSGerrit Uitslag
11587c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
116d63e7fe7SGerrit Uitslag            $list[0] = $ID;
11703352761SKirsten Roschanski            $this->title = $INPUT->str('pdftitle'); //DEPRECATED
11803352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
11903352761SKirsten Roschanski            if(empty($this->title)) {
12003352761SKirsten Roschanski                $this->title = p_get_first_heading($ID);
12115923cb9SGerrit Uitslag            }
122ad18f4e1SGerrit Uitslag
123ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
124ad18f4e1SGerrit Uitslag            //check input for title and ns
12503352761SKirsten Roschanski            if(!$this->title = $INPUT->str('book_title')) {
12626be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
127ad18f4e1SGerrit Uitslag                return false;
128ad18f4e1SGerrit Uitslag            }
129177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
130ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
13126be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
132ad18f4e1SGerrit Uitslag                return false;
133ad18f4e1SGerrit Uitslag            }
134ad18f4e1SGerrit Uitslag
13526be4eceSGerrit Uitslag            //sort order
136177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
137ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
138ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
139ad18f4e1SGerrit Uitslag                $order = 'natural';
140ad18f4e1SGerrit Uitslag            }
141ad18f4e1SGerrit Uitslag
14226be4eceSGerrit Uitslag            //search depth
143177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
144ad18f4e1SGerrit Uitslag            if($depth < 0) {
145ad18f4e1SGerrit Uitslag                $depth = 0;
146ad18f4e1SGerrit Uitslag            }
14726be4eceSGerrit Uitslag
148ad18f4e1SGerrit Uitslag            //page search
149ad18f4e1SGerrit Uitslag            $result = array();
150ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
151ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
152ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
153ad18f4e1SGerrit Uitslag
15426be4eceSGerrit Uitslag            //sorting
155ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
156ad18f4e1SGerrit Uitslag                if($order == 'date') {
157ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
158ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
159ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
160ad18f4e1SGerrit Uitslag                }
161ad18f4e1SGerrit Uitslag            }
162ad18f4e1SGerrit Uitslag
163ad18f4e1SGerrit Uitslag            foreach($result as $item) {
164d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
165ad18f4e1SGerrit Uitslag            }
166ad18f4e1SGerrit Uitslag
167baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
168baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
169baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
170baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
171baa31dc5SGerrit Uitslag                    }
172baa31dc5SGerrit Uitslag                }
173baa31dc5SGerrit Uitslag            }
174baa31dc5SGerrit Uitslag
175737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
176b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator*/
17726be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
17803352761SKirsten Roschanski            $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED
17903352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
18003352761SKirsten Roschanski            if(empty($this->title)) {
18126be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
182737417c6SKlap-in                return false;
18326be4eceSGerrit Uitslag            } else {
184d63e7fe7SGerrit Uitslag                $list = explode("|", $_COOKIE['list-pagelist']);
18526be4eceSGerrit Uitslag            }
186ad18f4e1SGerrit Uitslag
187b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
188b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
189b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
190b3eed6e3SGerrit Uitslag//                http_status(403);
191b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
192b3eed6e3SGerrit Uitslag//                exit();
193b3eed6e3SGerrit Uitslag//            }
194b3eed6e3SGerrit Uitslag
195b3eed6e3SGerrit Uitslag            $json = new JSON(JSON_LOOSE_TYPE);
196b3eed6e3SGerrit Uitslag            $list = $json->decode($INPUT->post->str('selection', '', true));
197b3eed6e3SGerrit Uitslag            if(!is_array($list) || empty($list)) {
198b3eed6e3SGerrit Uitslag                http_status(400);
199b3eed6e3SGerrit Uitslag                print $this->getLang('empty');
200b3eed6e3SGerrit Uitslag                exit();
201b3eed6e3SGerrit Uitslag            }
202b3eed6e3SGerrit Uitslag
20303352761SKirsten Roschanski            $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED
20403352761SKirsten Roschanski            $this->title = $INPUT->str('book_title', $this->title, true);
20503352761SKirsten Roschanski            if(empty($this->title)) {
206b3eed6e3SGerrit Uitslag                http_status(400);
207b3eed6e3SGerrit Uitslag                print $this->getLang('needtitle');
208b3eed6e3SGerrit Uitslag                exit();
209b3eed6e3SGerrit Uitslag            }
210b3eed6e3SGerrit Uitslag
211737417c6SKlap-in        } else {
21226be4eceSGerrit Uitslag            //show empty bookcreator message
21326be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
214737417c6SKlap-in            return false;
215737417c6SKlap-in        }
216737417c6SKlap-in
217719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
218c7138b3fSGerrit Uitslag
219c7138b3fSGerrit Uitslag        $skippedpages = array();
220c7138b3fSGerrit Uitslag        foreach($list as $index => $pageid) {
221c7138b3fSGerrit Uitslag            if(auth_quickaclcheck($pageid) < AUTH_READ) {
222c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
223c7138b3fSGerrit Uitslag                unset($list[$index]);
224c7138b3fSGerrit Uitslag            }
225c7138b3fSGerrit Uitslag        }
226c7138b3fSGerrit Uitslag        $list = array_filter($list); //removes also pages mentioned '0'
227c7138b3fSGerrit Uitslag
228c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
229c7138b3fSGerrit Uitslag        if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) {
230c7138b3fSGerrit Uitslag            $msg = hsc(join(', ', $skippedpages));
231c7138b3fSGerrit Uitslag            if($INPUT->has('selection')) {
232c7138b3fSGerrit Uitslag                http_status(400);
233c7138b3fSGerrit Uitslag                print sprintf($this->getLang('forbidden'), $msg);
234c7138b3fSGerrit Uitslag                exit();
235c7138b3fSGerrit Uitslag            } else {
236c7138b3fSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'forbidden', $msg);
237c7138b3fSGerrit Uitslag                return false;
238c7138b3fSGerrit Uitslag            }
239c7138b3fSGerrit Uitslag
240c7138b3fSGerrit Uitslag        }
241c7138b3fSGerrit Uitslag
24203352761SKirsten Roschanski        return array($this->title, $list);
243d63e7fe7SGerrit Uitslag    }
244d63e7fe7SGerrit Uitslag
245a58f45f0SGerrit Uitslag    /**
246a58f45f0SGerrit Uitslag     * Prepare cache
247a58f45f0SGerrit Uitslag     *
248a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
249a58f45f0SGerrit Uitslag     * @return cache
250a58f45f0SGerrit Uitslag     */
25103352761SKirsten Roschanski    protected function prepareCache(&$depends) {
252a58f45f0SGerrit Uitslag        global $REV;
253a58f45f0SGerrit Uitslag
254ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
255ee19bac3SLuigi Micco            . $REV
256ee19bac3SLuigi Micco            . $this->getExportConfig('template')
257ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
258ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
259d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
260ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
261ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
26203352761SKirsten Roschanski            . $this->title;
263ee19bac3SLuigi Micco        $cache = new cache($cachekey, '.dw2.pdf');
264ee19bac3SLuigi Micco
265ee19bac3SLuigi Micco        $dependencies = array();
266ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
267ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
268ee19bac3SLuigi Micco
269ee19bac3SLuigi Micco            if(is_array($relations)) {
270ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
271ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
272ee19bac3SLuigi Micco                        if($exists) {
273ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
274ee19bac3SLuigi Micco                        }
275ee19bac3SLuigi Micco                    }
276ee19bac3SLuigi Micco                }
277ee19bac3SLuigi Micco
278ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
279ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
280ee19bac3SLuigi Micco                        if($exists) {
281ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
282ee19bac3SLuigi Micco                        }
283ee19bac3SLuigi Micco                    }
284ee19bac3SLuigi Micco                }
285ee19bac3SLuigi Micco            }
286ee19bac3SLuigi Micco
287ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
288ee19bac3SLuigi Micco        }
289ee19bac3SLuigi Micco
290ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
291ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
292ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
293ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
294ee19bac3SLuigi Micco        $depends['files'] = array_merge(
295ee19bac3SLuigi Micco            $depends['files'],
296ee19bac3SLuigi Micco            $dependencies,
297ee19bac3SLuigi Micco            getConfigFiles('main')
298ee19bac3SLuigi Micco        );
299a58f45f0SGerrit Uitslag        return $cache;
300ee19bac3SLuigi Micco    }
301ee19bac3SLuigi Micco
302d63e7fe7SGerrit Uitslag    /**
303d63e7fe7SGerrit Uitslag     * Set error notification and reload page again
304d63e7fe7SGerrit Uitslag     *
305d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
306d63e7fe7SGerrit Uitslag     * @param string $msglangkey key of translation key
307c7138b3fSGerrit Uitslag     * @param string $replacement
308d63e7fe7SGerrit Uitslag     */
309c7138b3fSGerrit Uitslag    private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) {
310c7138b3fSGerrit Uitslag        if(empty($replacement)) {
311c7138b3fSGerrit Uitslag            $msg = $this->getLang($msglangkey);
312c7138b3fSGerrit Uitslag        } else {
313c7138b3fSGerrit Uitslag            $msg = sprintf($this->getLang($msglangkey), $replacement);
314c7138b3fSGerrit Uitslag        }
315c7138b3fSGerrit Uitslag        msg($msg, -1);
316d63e7fe7SGerrit Uitslag
317d63e7fe7SGerrit Uitslag        $event->data = 'show';
318d63e7fe7SGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
319d63e7fe7SGerrit Uitslag    }
320d63e7fe7SGerrit Uitslag
321d63e7fe7SGerrit Uitslag    /**
322d63e7fe7SGerrit Uitslag     * Build a pdf from the html
323d63e7fe7SGerrit Uitslag     *
324d63e7fe7SGerrit Uitslag     * @param string $cachefile
325d63e7fe7SGerrit Uitslag     */
32603352761SKirsten Roschanski    protected function generatePDF($cachefile) {
327d63e7fe7SGerrit Uitslag        global $ID;
328d63e7fe7SGerrit Uitslag        global $REV;
329d63e7fe7SGerrit Uitslag        global $INPUT;
33087c86ddaSAndreas Gohr
33102f9a447SGerrit Uitslag        //some shortcuts to export settings
33202f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
33302f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
33402f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
3356ea88a05SAndreas Gohr
3361ef68647SAndreas Gohr        // initialize PDF library
337cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
3386ea88a05SAndreas Gohr
3394870b378SLarsDW223        $mpdf = new DokuPDF($this->getExportConfig('pagesize'),
3404870b378SLarsDW223                            $this->getExportConfig('orientation'),
3414870b378SLarsDW223                            $this->getExportConfig('font-size'));
342ee19bac3SLuigi Micco
343d62df65bSAndreas Gohr        // let mpdf fix local links
344d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
345d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
34602f9a447SGerrit Uitslag        if($self['port']) {
34702f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
34802f9a447SGerrit Uitslag        }
349d62df65bSAndreas Gohr        $mpdf->setBasePath($url);
350d62df65bSAndreas Gohr
35156d13144SAndreas Gohr        // Set the title
35203352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
35356d13144SAndreas Gohr
354d63e7fe7SGerrit Uitslag        // some default document settings
355d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
356213fdb75SGerrit Uitslag        //      single-side document has only odd pages
357213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
358daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
359daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
36002f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
36102f9a447SGerrit Uitslag        if($hasToC) {
36202f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
36302f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
36402f9a447SGerrit Uitslag        } else {
36502f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
36602f9a447SGerrit Uitslag        }
3672eedf77dSAndreas Gohr
36856d13144SAndreas Gohr        // load the template
36903352761SKirsten Roschanski        $template = $this->load_template();
370ee19bac3SLuigi Micco
3711ef68647SAndreas Gohr        // prepare HTML header styles
372a2c33768SGerrit Uitslag        $html = '';
37302f9a447SGerrit Uitslag        if($isDebug) {
374a2c33768SGerrit Uitslag            $html .= '<html><head>';
375737417c6SKlap-in            $html .= '<style type="text/css">';
376a2c33768SGerrit Uitslag        }
377db1aa1bfSKirsten Roschanski
378db1aa1bfSKirsten Roschanski        $styles = '@page { size:auto; ' . $template['page'] . '}';
379a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
380254467c4SGerrit Uitslag
381254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
382254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
383254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
384254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
385db1aa1bfSKirsten Roschanski        $styles .= $this->load_css();
386254467c4SGerrit Uitslag
387a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
388a2c33768SGerrit Uitslag
38902f9a447SGerrit Uitslag        if($isDebug) {
390a2c33768SGerrit Uitslag            $html .= $styles;
3911ef68647SAndreas Gohr            $html .= '</style>';
3921ef68647SAndreas Gohr            $html .= '</head><body>';
393a2c33768SGerrit Uitslag        }
394a2c33768SGerrit Uitslag
395a2c33768SGerrit Uitslag        $body_start = $template['html'];
396a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
3972eedf77dSAndreas Gohr
3981e45476bSmnapp        // insert the cover page
399a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
400a2c33768SGerrit Uitslag
401a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
40202f9a447SGerrit Uitslag        if($isDebug) {
403a2c33768SGerrit Uitslag            $html .= $body_start;
404a2c33768SGerrit Uitslag        }
40502f9a447SGerrit Uitslag        if($hasToC) {
40602f9a447SGerrit 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
40702f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
40802f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
40902f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
41002f9a447SGerrit Uitslag                array(
411230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
412230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
41302f9a447SGerrit Uitslag                    'links' => true,
41402f9a447SGerrit Uitslag                    'outdent' => '1em',
41502f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
41602f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
41702f9a447SGerrit Uitslag                )
41802f9a447SGerrit Uitslag            );
41902f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
42002f9a447SGerrit Uitslag        }
42102f9a447SGerrit Uitslag
422c00eb13bSGerrit Uitslag        // store original pageid
423c00eb13bSGerrit Uitslag        $keep = $ID;
424c00eb13bSGerrit Uitslag
4251ef68647SAndreas Gohr        // loop over all pages
426c7138b3fSGerrit Uitslag        $counter = 0;
427c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
428c7138b3fSGerrit Uitslag        foreach($this->list as $page) {
429c7138b3fSGerrit Uitslag            $counter++;
430b3eed6e3SGerrit Uitslag            $filename = wikiFN($page, $REV);
431b3eed6e3SGerrit Uitslag
432b3eed6e3SGerrit Uitslag            if(!file_exists($filename)) {
433b3eed6e3SGerrit Uitslag                continue;
434b3eed6e3SGerrit Uitslag            }
435ee19bac3SLuigi Micco
436c00eb13bSGerrit Uitslag            // set global pageid to the rendered page
437c00eb13bSGerrit Uitslag            $ID = $page;
438c00eb13bSGerrit Uitslag
439b3eed6e3SGerrit Uitslag            $pagehtml = p_cached_output($filename, 'dw2pdf', $page);
440719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
441c7138b3fSGerrit Uitslag            if($counter < $no_pages) {
442a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
443a2c33768SGerrit Uitslag            }
444a2c33768SGerrit Uitslag
445a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
44602f9a447SGerrit Uitslag            if($isDebug) {
447a2c33768SGerrit Uitslag                $html .= $pagehtml;
4481ef68647SAndreas Gohr            }
449ee19bac3SLuigi Micco        }
450c00eb13bSGerrit Uitslag        //restore ID
451c00eb13bSGerrit Uitslag        $ID = $keep;
452ee19bac3SLuigi Micco
45333c15297SGerrit Uitslag        // insert the back page
454a2c33768SGerrit Uitslag        $body_end = $template['back'];
45533c15297SGerrit Uitslag
456a2c33768SGerrit Uitslag        $body_end .= '</div>';
457a2c33768SGerrit Uitslag
458d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
45902f9a447SGerrit Uitslag        if($isDebug) {
460a2c33768SGerrit Uitslag            $html .= $body_end;
461eeb17e15SAndreas Gohr            $html .= '</body>';
462eeb17e15SAndreas Gohr            $html .= '</html>';
463a2c33768SGerrit Uitslag        }
464f765508eSGerrit Uitslag
465f765508eSGerrit Uitslag        //Return html for debugging
46602f9a447SGerrit Uitslag        if($isDebug) {
46702f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
46826be4eceSGerrit Uitslag                echo $html;
469a2c33768SGerrit Uitslag            } else {
470a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
471a2c33768SGerrit Uitslag                echo $html;
472a2c33768SGerrit Uitslag            }
47326be4eceSGerrit Uitslag            exit();
47426be4eceSGerrit Uitslag        };
475f765508eSGerrit Uitslag
47687c86ddaSAndreas Gohr        // write to cache file
477d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
47887c86ddaSAndreas Gohr    }
47987c86ddaSAndreas Gohr
480d63e7fe7SGerrit Uitslag    /**
481d63e7fe7SGerrit Uitslag     * @param string $cachefile
482d63e7fe7SGerrit Uitslag     */
48303352761SKirsten Roschanski    protected function sendPDFFile($cachefile) {
48487c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
485b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
48687c86ddaSAndreas Gohr        header('Pragma: public');
487d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
48887c86ddaSAndreas Gohr
48903352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
49087c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
4919a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
49287c86ddaSAndreas Gohr        } else {
4939a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
49487c86ddaSAndreas Gohr        }
495ee19bac3SLuigi Micco
496b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
497b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
498b3eed6e3SGerrit Uitslag
499e993da11SGerrit Uitslag        //try to send file, and exit if done
500d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
50187c86ddaSAndreas Gohr
502d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
50387c86ddaSAndreas Gohr        if($fp) {
504d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
50587c86ddaSAndreas Gohr        } else {
50687c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
50787c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
50887c86ddaSAndreas Gohr        }
5091ef68647SAndreas Gohr        exit();
5101ef68647SAndreas Gohr    }
5111ef68647SAndreas Gohr
5126be736bfSGerrit Uitslag    /**
5132eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
51444e8e8fbSGerrit Uitslag     *
51544e8e8fbSGerrit Uitslag     * @return array
5161ef68647SAndreas Gohr     */
51703352761SKirsten Roschanski    protected function load_template() {
5181ef68647SAndreas Gohr        global $ID;
5191ef68647SAndreas Gohr        global $conf;
5201ef68647SAndreas Gohr
5212eedf77dSAndreas Gohr        // this is what we'll return
5222eedf77dSAndreas Gohr        $output = array(
5231e45476bSmnapp            'cover' => '',
5242eedf77dSAndreas Gohr            'html'  => '',
5252eedf77dSAndreas Gohr            'page'  => '',
5262eedf77dSAndreas Gohr            'first' => '',
5272eedf77dSAndreas Gohr            'cite'  => '',
5282eedf77dSAndreas Gohr        );
5292eedf77dSAndreas Gohr
5302eedf77dSAndreas Gohr        // prepare header/footer elements
5312eedf77dSAndreas Gohr        $html = '';
53233c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
53333c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
53402f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
53533c15297SGerrit Uitslag                if(file_exists($file)) {
53633c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
53733c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
53833c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
5392eedf77dSAndreas Gohr
5402eedf77dSAndreas Gohr                    // register the needed pseudo CSS
54133c15297SGerrit Uitslag                    if($order == '_first') {
54233c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
54333c15297SGerrit Uitslag                    } elseif($order == '_even') {
54433c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
54533c15297SGerrit Uitslag                    } elseif($order == '_odd') {
54633c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
547daa70883SAndreas Gohr                    } else {
54833c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
5492eedf77dSAndreas Gohr                    }
5502eedf77dSAndreas Gohr                }
5512eedf77dSAndreas Gohr            }
5522eedf77dSAndreas Gohr        }
5532eedf77dSAndreas Gohr
5541ef68647SAndreas Gohr        // prepare replacements
5551ef68647SAndreas Gohr        $replace = array(
5561ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
55702f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
55803352761SKirsten Roschanski            '@TITLE@'   => hsc($this->title),
5591ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
5601ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
5611ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
5625d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
56302f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
5641ef68647SAndreas Gohr        );
5651ef68647SAndreas Gohr
5662eedf77dSAndreas Gohr        // set HTML element
567a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
568a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
569a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
5701ef68647SAndreas Gohr
5711e45476bSmnapp        // cover page
57202f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
57333c15297SGerrit Uitslag        if(file_exists($coverfile)) {
57433c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
5751e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
5769b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
5776e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
5781e45476bSmnapp        }
5791e45476bSmnapp
58033c15297SGerrit Uitslag        // cover page
58102f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
58233c15297SGerrit Uitslag        if(file_exists($backfile)) {
58333c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
58433c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
58533c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
5869b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
58733c15297SGerrit Uitslag        }
58833c15297SGerrit Uitslag
5892eedf77dSAndreas Gohr        // citation box
59002f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
59133c15297SGerrit Uitslag        if(file_exists($citationfile)) {
59233c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
5932eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
5942eedf77dSAndreas Gohr        }
5951ef68647SAndreas Gohr
5962eedf77dSAndreas Gohr        return $output;
5971ef68647SAndreas Gohr    }
5981ef68647SAndreas Gohr
5991ef68647SAndreas Gohr    /**
600a180c973SKlap-in     * @param string $raw code with placeholders
601a180c973SKlap-in     * @param string $id  pageid
602a180c973SKlap-in     * @return string
603a180c973SKlap-in     */
604a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
605a180c973SKlap-in        global $REV;
606a180c973SKlap-in
607a180c973SKlap-in        // generate qr code for this page using google infographics api
608a180c973SKlap-in        $qr_code = '';
609a180c973SKlap-in        if($this->getConf('qrcodesize')) {
610a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
611a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
612a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
613a180c973SKlap-in        }
614a180c973SKlap-in        // prepare replacements
615a180c973SKlap-in        $replace['@ID@']      = $id;
616a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
617a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
618a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
619a180c973SKlap-in
620e3d68265SGerrit Uitslag        $content = str_replace(array_keys($replace), array_values($replace), $raw);
621e3d68265SGerrit Uitslag
622e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
623e3d68265SGerrit Uitslag        $content = preg_replace_callback(
624e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
625e3d68265SGerrit Uitslag            array($this, 'replacedate'),
626e3d68265SGerrit Uitslag            $content
627e3d68265SGerrit Uitslag        );
628e3d68265SGerrit Uitslag
629e3d68265SGerrit Uitslag        return $content;
630a180c973SKlap-in    }
631a180c973SKlap-in
632e3d68265SGerrit Uitslag
633e3d68265SGerrit Uitslag    /**
634e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
635e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
636e3d68265SGerrit Uitslag     *
637e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
638e3d68265SGerrit Uitslag     * @return string
639e3d68265SGerrit Uitslag     */
640e3d68265SGerrit Uitslag    function replacedate($match) {
641e3d68265SGerrit Uitslag        global $conf;
642e3d68265SGerrit Uitslag        //no 2nd argument for default date format
643e3d68265SGerrit Uitslag        if($match[2] == null) {
644e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
645e3d68265SGerrit Uitslag        }
646e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
647e3d68265SGerrit Uitslag    }
648e3d68265SGerrit Uitslag
649e3d68265SGerrit Uitslag
650a180c973SKlap-in    /**
6511c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
6521ef68647SAndreas Gohr     */
6531c14c879SAndreas Gohr    protected function load_css() {
654737417c6SKlap-in        global $conf;
6551c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
6561c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
6571c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
658ee19bac3SLuigi Micco
6591c14c879SAndreas Gohr        // prepare CSS files
6601c14c879SAndreas Gohr        $files = array_merge(
6611c14c879SAndreas Gohr            array(
6621c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
6631c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6641c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
6651c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
6661c14c879SAndreas Gohr            ),
6671c14c879SAndreas Gohr            css_pluginstyles('all'),
66858e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
6691c14c879SAndreas Gohr            array(
6701c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
6711c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6721c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
6731c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
6741c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
6751c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
6761c14c879SAndreas Gohr            )
6771c14c879SAndreas Gohr        );
6781c14c879SAndreas Gohr        $css = '';
6791c14c879SAndreas Gohr        foreach($files as $file => $location) {
68028e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
68128e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
6821c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
6831ef68647SAndreas Gohr        }
6841ef68647SAndreas Gohr
68528e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
6861c14c879SAndreas Gohr            // apply pattern replacements
68728e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
68828e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
68928e636eaSGerrit Uitslag
69028e636eaSGerrit Uitslag            // parse less
69128e636eaSGerrit Uitslag            $css = css_parseless($css);
69228e636eaSGerrit Uitslag        } else {
69328e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
6941c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
69528e636eaSGerrit Uitslag        }
6961ef68647SAndreas Gohr
6971c14c879SAndreas Gohr        return $css;
698ee19bac3SLuigi Micco    }
6991c14c879SAndreas Gohr
70058e6409eSAndreas Gohr    /**
70158e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
70258e6409eSAndreas Gohr     *
70358e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
70458e6409eSAndreas Gohr     *
70558e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
70658e6409eSAndreas Gohr     */
7076be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
70858e6409eSAndreas Gohr        $list = array();
70958e6409eSAndreas Gohr        $plugins = plugin_list();
710f54b51f7SAndreas Gohr
711f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
71258e6409eSAndreas Gohr        foreach($plugins as $p) {
713f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
714f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
715f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
716f54b51f7SAndreas Gohr            }
717f54b51f7SAndreas Gohr
71858e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
71958e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
72058e6409eSAndreas Gohr            } else {
72158e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
72258e6409eSAndreas Gohr            }
72358e6409eSAndreas Gohr        }
72458e6409eSAndreas Gohr        return $list;
72558e6409eSAndreas Gohr    }
726ad18f4e1SGerrit Uitslag
727ad18f4e1SGerrit Uitslag    /**
72860e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
72960e59de7SGerrit Uitslag     *
73060e59de7SGerrit Uitslag     * @return array
73160e59de7SGerrit Uitslag     */
73260e59de7SGerrit Uitslag    public function getExportedPages() {
73360e59de7SGerrit Uitslag        return $this->list;
73460e59de7SGerrit Uitslag    }
73560e59de7SGerrit Uitslag
73660e59de7SGerrit Uitslag    /**
737ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
73844e8e8fbSGerrit Uitslag     *
73944e8e8fbSGerrit Uitslag     * @param array $a
74044e8e8fbSGerrit Uitslag     * @param array $b
74144e8e8fbSGerrit Uitslag     * @return int
742ad18f4e1SGerrit Uitslag     */
743ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
744ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
745ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
746ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
747ad18f4e1SGerrit Uitslag    }
748ad18f4e1SGerrit Uitslag
749ad18f4e1SGerrit Uitslag    /**
750ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
75144e8e8fbSGerrit Uitslag     * @param array $a
75244e8e8fbSGerrit Uitslag     * @param array $b
75344e8e8fbSGerrit Uitslag     * @return int
754ad18f4e1SGerrit Uitslag     */
755ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
756ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
757ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
758ad18f4e1SGerrit Uitslag        return 0;
759ad18f4e1SGerrit Uitslag    }
76026be4eceSGerrit Uitslag
76126be4eceSGerrit Uitslag    /**
76202f9a447SGerrit Uitslag     * Return settings read from:
76302f9a447SGerrit Uitslag     *   1. url parameters
76402f9a447SGerrit Uitslag     *   2. plugin config
76502f9a447SGerrit Uitslag     *   3. global config
76602f9a447SGerrit Uitslag     *
76702f9a447SGerrit Uitslag     * @return array
76802f9a447SGerrit Uitslag     */
76902f9a447SGerrit Uitslag    protected function loadExportConfig() {
77002f9a447SGerrit Uitslag        global $INPUT;
77102f9a447SGerrit Uitslag        global $conf;
77202f9a447SGerrit Uitslag
77302f9a447SGerrit Uitslag        $this->exportConfig = array();
77402f9a447SGerrit Uitslag
77502f9a447SGerrit Uitslag        // decide on the paper setup from param or config
77602f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
77702f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
77802f9a447SGerrit Uitslag
7794870b378SLarsDW223        // decide on the font-size from param or config
7804870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
7814870b378SLarsDW223
782213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
783213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
784213fdb75SGerrit Uitslag
785213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
78602f9a447SGerrit Uitslag        $levels = array();
78702f9a447SGerrit Uitslag        if($hasToC) {
78802f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
78902f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
79002f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
79102f9a447SGerrit Uitslag            $bounds_input = array(
79202f9a447SGerrit Uitslag                'top' => array(
79302f9a447SGerrit Uitslag                    (int) $top_input,
79402f9a447SGerrit Uitslag                    (int) $top_conf
79502f9a447SGerrit Uitslag                ),
79602f9a447SGerrit Uitslag                'max' => array(
79702f9a447SGerrit Uitslag                    (int) $max_input,
79802f9a447SGerrit Uitslag                    (int) $max_conf
79902f9a447SGerrit Uitslag                )
80002f9a447SGerrit Uitslag            );
80102f9a447SGerrit Uitslag            $bounds = array(
80202f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
80302f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
80402f9a447SGerrit Uitslag
80502f9a447SGerrit Uitslag            );
80602f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
80702f9a447SGerrit Uitslag                foreach($values as $value) {
80802f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
80902f9a447SGerrit Uitslag                        //stop at valid value and store
81002f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
81102f9a447SGerrit Uitslag                        break;
81202f9a447SGerrit Uitslag                    }
81302f9a447SGerrit Uitslag                }
81402f9a447SGerrit Uitslag            }
81502f9a447SGerrit Uitslag
81602f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
81702f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
81802f9a447SGerrit Uitslag            }
81902f9a447SGerrit Uitslag
82002f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
82102f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
82202f9a447SGerrit Uitslag            }
82302f9a447SGerrit Uitslag        }
82402f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
82502f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
82602f9a447SGerrit Uitslag
82702f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
82802f9a447SGerrit Uitslag
82902f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
83005d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
83102f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
83202f9a447SGerrit Uitslag            $tpl = $tplconf;
83302f9a447SGerrit Uitslag        }
83402f9a447SGerrit Uitslag        if(!$tpl){
83502f9a447SGerrit Uitslag            $tpl = 'default';
83602f9a447SGerrit Uitslag        }
83702f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
83802f9a447SGerrit Uitslag
83902f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
84002f9a447SGerrit Uitslag    }
84102f9a447SGerrit Uitslag
84202f9a447SGerrit Uitslag    /**
84302f9a447SGerrit Uitslag     * Returns requested config
84402f9a447SGerrit Uitslag     *
84502f9a447SGerrit Uitslag     * @param string $name
84602f9a447SGerrit Uitslag     * @param mixed  $notset
84702f9a447SGerrit Uitslag     * @return mixed|bool
84802f9a447SGerrit Uitslag     */
84902f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
85002f9a447SGerrit Uitslag        if ($this->exportConfig === null){
85102f9a447SGerrit Uitslag            $this->loadExportConfig();
85202f9a447SGerrit Uitslag        }
85302f9a447SGerrit Uitslag
85402f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
85502f9a447SGerrit Uitslag            return $this->exportConfig[$name];
85602f9a447SGerrit Uitslag        }else{
85702f9a447SGerrit Uitslag            return $notset;
85802f9a447SGerrit Uitslag        }
85902f9a447SGerrit Uitslag    }
860d63e7fe7SGerrit Uitslag
861d63e7fe7SGerrit Uitslag    /**
862d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
863d63e7fe7SGerrit Uitslag     *
864d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
865d63e7fe7SGerrit Uitslag     */
86644e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
867d63e7fe7SGerrit Uitslag        global $ID, $REV;
868d63e7fe7SGerrit Uitslag
869d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
870d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
871d63e7fe7SGerrit Uitslag            if($REV) {
872d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
873d63e7fe7SGerrit Uitslag            }
874d63e7fe7SGerrit Uitslag
875d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
876d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
877d63e7fe7SGerrit Uitslag                array('export_pdf' =>
878d63e7fe7SGerrit Uitslag                          '<li>'
87972cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
880d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
881d63e7fe7SGerrit Uitslag                          . '</a>'
882d63e7fe7SGerrit Uitslag                          . '</li>'
883d63e7fe7SGerrit Uitslag                ) +
884d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
885d63e7fe7SGerrit Uitslag        }
886d63e7fe7SGerrit Uitslag    }
887ee19bac3SLuigi Micco}
888