xref: /plugin/dw2pdf/action.php (revision 60e59de7d6295c0b3cfb1268109a5e4457ac4705)
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 {
20ee19bac3SLuigi Micco
21*60e59de7SGerrit Uitslag    protected $tpl;
22*60e59de7SGerrit Uitslag    protected $list = array();
231c14c879SAndreas Gohr
241c14c879SAndreas Gohr    /**
251c14c879SAndreas Gohr     * Constructor. Sets the correct template
261c14c879SAndreas Gohr     */
276be736bfSGerrit Uitslag    public function __construct() {
28737417c6SKlap-in        $tpl = false;
291c14c879SAndreas Gohr        if(isset($_REQUEST['tpl'])) {
301c14c879SAndreas Gohr            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/', '', $_REQUEST['tpl']));
311c14c879SAndreas Gohr        }
321c14c879SAndreas Gohr        if(!$tpl) $tpl = $this->getConf('template');
331c14c879SAndreas Gohr        if(!$tpl) $tpl = 'default';
341c14c879SAndreas Gohr        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) $tpl = 'default';
351c14c879SAndreas Gohr
361c14c879SAndreas Gohr        $this->tpl = $tpl;
371c14c879SAndreas Gohr    }
381c14c879SAndreas Gohr
39ee19bac3SLuigi Micco    /**
40ee19bac3SLuigi Micco     * Register the events
41ee19bac3SLuigi Micco     */
426be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
43ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
446be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
45ee19bac3SLuigi Micco    }
46ee19bac3SLuigi Micco
471c14c879SAndreas Gohr    /**
481c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
49737417c6SKlap-in     *
50737417c6SKlap-in     * @param Doku_Event $event
51737417c6SKlap-in     * @param array      $param
52737417c6SKlap-in     * @return bool
531c14c879SAndreas Gohr     */
546be736bfSGerrit Uitslag    public function convert(&$event, $param) {
55ee19bac3SLuigi Micco        global $ACT;
56ee19bac3SLuigi Micco        global $REV;
57ee19bac3SLuigi Micco        global $ID;
58ad18f4e1SGerrit Uitslag        global $INPUT, $conf;
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
6687c86ddaSAndreas Gohr        // one or multiple pages?
67*60e59de7SGerrit Uitslag        $this->list = array();
6828e636eaSGerrit Uitslag
6987c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
70*60e59de7SGerrit Uitslag            $this->list[0] = $ID;
71737417c6SKlap-in            $title = p_get_first_heading($ID);
72ad18f4e1SGerrit Uitslag
73ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
74ad18f4e1SGerrit Uitslag            //check input for title and ns
7526be4eceSGerrit Uitslag            if(!$title = $INPUT->str('pdfns_title')) {
7626be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
77ad18f4e1SGerrit Uitslag                return false;
78ad18f4e1SGerrit Uitslag            }
7926be4eceSGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('pdfns_ns'));
80ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
8126be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
82ad18f4e1SGerrit Uitslag                return false;
83ad18f4e1SGerrit Uitslag            }
84ad18f4e1SGerrit Uitslag
8526be4eceSGerrit Uitslag            //sort order
86ad18f4e1SGerrit Uitslag            $order = $INPUT->str('pdfns_order', 'natural', true);
87ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
88ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
89ad18f4e1SGerrit Uitslag                $order = 'natural';
90ad18f4e1SGerrit Uitslag            }
91ad18f4e1SGerrit Uitslag
9226be4eceSGerrit Uitslag            //search depth
93ad18f4e1SGerrit Uitslag            $depth = $INPUT->int('pdfns_depth', 0);
94ad18f4e1SGerrit Uitslag            if($depth < 0) {
95ad18f4e1SGerrit Uitslag                $depth = 0;
96ad18f4e1SGerrit Uitslag            }
9726be4eceSGerrit Uitslag
98ad18f4e1SGerrit Uitslag            //page search
99ad18f4e1SGerrit Uitslag            $result = array();
100ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
101ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
102ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
103ad18f4e1SGerrit Uitslag
10426be4eceSGerrit Uitslag            //sorting
105ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
106ad18f4e1SGerrit Uitslag                if($order == 'date') {
107ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
108ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
109ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
110ad18f4e1SGerrit Uitslag                }
111ad18f4e1SGerrit Uitslag            }
112ad18f4e1SGerrit Uitslag
113ad18f4e1SGerrit Uitslag            foreach($result as $item) {
114*60e59de7SGerrit Uitslag                $this->list[] = $item['id'];
115ad18f4e1SGerrit Uitslag            }
116ad18f4e1SGerrit Uitslag
117737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
11826be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
11926be4eceSGerrit Uitslag            if(!$title = $INPUT->str('pdfbook_title')) { //TODO when title is changed, the cached file contains the old title
12026be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
121737417c6SKlap-in                return false;
12226be4eceSGerrit Uitslag            } else {
123*60e59de7SGerrit Uitslag                $this->list = explode("|", $_COOKIE['list-pagelist']);
12426be4eceSGerrit Uitslag            }
125ad18f4e1SGerrit Uitslag
126737417c6SKlap-in        } else {
12726be4eceSGerrit Uitslag            //show empty bookcreator message
12826be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
129737417c6SKlap-in            return false;
130737417c6SKlap-in        }
131737417c6SKlap-in
132737417c6SKlap-in        // it's ours, no one else's
133737417c6SKlap-in        $event->preventDefault();
13487c86ddaSAndreas Gohr
1356ea88a05SAndreas Gohr        // decide on the paper setup from param or config
1366ea88a05SAndreas Gohr        $pagesize    = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
1376ea88a05SAndreas Gohr        $orientation = $INPUT->str('orientation', $this->getConf('orientation'), true);
1386ea88a05SAndreas Gohr
13987c86ddaSAndreas Gohr        // prepare cache
140*60e59de7SGerrit Uitslag        $cache = new cache(join(',', $this->list) . $REV . $this->tpl . $pagesize . $orientation, '.dw2.pdf');
141*60e59de7SGerrit Uitslag        $depends['files']   = array_map('wikiFN', $this->list);
14287c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
14387c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
14487c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
145df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
14687c86ddaSAndreas Gohr
147d01d2a42SAndreas Gohr        // hard work only when no cache available
14887c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
1491ef68647SAndreas Gohr            // initialize PDF library
150cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__) . "/DokuPDF.class.php");
1516ea88a05SAndreas Gohr
1526ea88a05SAndreas Gohr            $mpdf = new DokuPDF($pagesize, $orientation);
153ee19bac3SLuigi Micco
154d62df65bSAndreas Gohr            // let mpdf fix local links
155d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
156d62df65bSAndreas Gohr            $url = $self['scheme'] . '://' . $self['host'];
157737417c6SKlap-in            if($self['port']) $url .= ':' . $self['port'];
158d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
159d62df65bSAndreas Gohr
16056d13144SAndreas Gohr            // Set the title
16156d13144SAndreas Gohr            $mpdf->SetTitle($title);
16256d13144SAndreas Gohr
1631ef68647SAndreas Gohr            // some default settings
164daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
165daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
166daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
167daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
1682eedf77dSAndreas Gohr
16956d13144SAndreas Gohr            // load the template
1701c14c879SAndreas Gohr            $template = $this->load_template($title);
171ee19bac3SLuigi Micco
1721ef68647SAndreas Gohr            // prepare HTML header styles
173ee19bac3SLuigi Micco            $html  = '<html><head>';
174737417c6SKlap-in            $html .= '<style type="text/css">';
1751c14c879SAndreas Gohr            $html .= $this->load_css();
176daa70883SAndreas Gohr            $html .= '@page { size:auto; ' . $template['page'] . '}';
1772eedf77dSAndreas Gohr            $html .= '@page :first {' . $template['first'] . '}';
1781ef68647SAndreas Gohr            $html .= '</style>';
1791ef68647SAndreas Gohr            $html .= '</head><body>';
1802eedf77dSAndreas Gohr            $html .= $template['html'];
1811c14c879SAndreas Gohr            $html .= '<div class="dokuwiki">';
1822eedf77dSAndreas Gohr
1831e45476bSmnapp            // insert the cover page
1846e2ec302SGerrit Uitslag            $html .= $template['cover'];
185ed9f9952Smnapp
186c00eb13bSGerrit Uitslag            // store original pageid
187c00eb13bSGerrit Uitslag            $keep = $ID;
188c00eb13bSGerrit Uitslag
1891ef68647SAndreas Gohr            // loop over all pages
1901ef68647SAndreas Gohr            $cnt = count($list);
1911ef68647SAndreas Gohr            for($n = 0; $n < $cnt; $n++) {
192ee19bac3SLuigi Micco                $page = $list[$n];
193ee19bac3SLuigi Micco
194c00eb13bSGerrit Uitslag                // set global pageid to the rendered page
195c00eb13bSGerrit Uitslag                $ID   = $page;
196c00eb13bSGerrit Uitslag
197a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page, $REV), 'dw2pdf', $page);
198a180c973SKlap-in                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
1991ef68647SAndreas Gohr                if($n < ($cnt - 1)) {
2001ef68647SAndreas Gohr                    $html .= '<pagebreak />';
2011ef68647SAndreas Gohr                }
202ee19bac3SLuigi Micco            }
203c00eb13bSGerrit Uitslag            //restore ID
204c00eb13bSGerrit Uitslag            $ID = $keep;
205ee19bac3SLuigi Micco
20633c15297SGerrit Uitslag            // insert the back page
20733c15297SGerrit Uitslag            $html .= $template['back'];
20833c15297SGerrit Uitslag
2091c14c879SAndreas Gohr            $html .= '</div>';
210eeb17e15SAndreas Gohr            $html .= '</body>';
211eeb17e15SAndreas Gohr            $html .= '</html>';
212f765508eSGerrit Uitslag
213f765508eSGerrit Uitslag            //Return html for debugging
21426be4eceSGerrit Uitslag            if($conf['allowdebug'] && $_GET['debughtml'] == 'html') {
21526be4eceSGerrit Uitslag                echo $html;
21626be4eceSGerrit Uitslag                exit();
21726be4eceSGerrit Uitslag            };
218f765508eSGerrit Uitslag
219ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
220a06728a6SAndreas Gohr
22187c86ddaSAndreas Gohr            // write to cache file
22287c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
22387c86ddaSAndreas Gohr        }
22487c86ddaSAndreas Gohr
22587c86ddaSAndreas Gohr        // deliver the file
22687c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
227b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
22887c86ddaSAndreas Gohr        header('Pragma: public');
22987c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
23087c86ddaSAndreas Gohr
2319a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
23287c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
2339a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
23487c86ddaSAndreas Gohr        } else {
2359a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
23687c86ddaSAndreas Gohr        }
237ee19bac3SLuigi Micco
238e993da11SGerrit Uitslag        //try to send file, and exit if done
239e993da11SGerrit Uitslag        http_sendfile($cache->cache);
24087c86ddaSAndreas Gohr
24187c86ddaSAndreas Gohr        $fp = @fopen($cache->cache, "rb");
24287c86ddaSAndreas Gohr        if($fp) {
24387c86ddaSAndreas Gohr            http_rangeRequest($fp, filesize($cache->cache), 'application/pdf');
24487c86ddaSAndreas Gohr        } else {
24587c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
24687c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
24787c86ddaSAndreas Gohr        }
2481ef68647SAndreas Gohr        exit();
2491ef68647SAndreas Gohr    }
2501ef68647SAndreas Gohr
2516be736bfSGerrit Uitslag    /**
2526be736bfSGerrit Uitslag     * Add 'export pdf'-button to pagetools
2536be736bfSGerrit Uitslag     *
2546be736bfSGerrit Uitslag     * @param Doku_Event $event
2556be736bfSGerrit Uitslag     * @param mixed      $param not defined
2566be736bfSGerrit Uitslag     */
2576be736bfSGerrit Uitslag    public function addbutton(&$event, $param) {
2582c53f619SAndreas Gohr        global $ID, $REV;
2596be736bfSGerrit Uitslag
2606be736bfSGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
2616be736bfSGerrit Uitslag            $params = array('do' => 'export_pdf');
2626be736bfSGerrit Uitslag            if($REV) $params['rev'] = $REV;
2636be736bfSGerrit Uitslag
2641e02f86cSAndreas Gohr            // insert button at position before last (up to top)
2651e02f86cSAndreas Gohr            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
2661e02f86cSAndreas Gohr                                    array('export_pdf' =>
2676be736bfSGerrit Uitslag                                          '<li>'
2686be736bfSGerrit Uitslag                                          . '<a href=' . wl($ID, $params) . '  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
2696be736bfSGerrit Uitslag                                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
2706be736bfSGerrit Uitslag                                          . '</a>'
2711e02f86cSAndreas Gohr                                          . '</li>'
2721e02f86cSAndreas Gohr                                    ) +
2731e02f86cSAndreas Gohr                                    array_slice($event->data['items'], -1, 1, true);
2746be736bfSGerrit Uitslag        }
2756be736bfSGerrit Uitslag    }
276d01d2a42SAndreas Gohr
277d01d2a42SAndreas Gohr    /**
2782eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
2791ef68647SAndreas Gohr     */
2801c14c879SAndreas Gohr    protected function load_template($title) {
2811ef68647SAndreas Gohr        global $ID;
2821ef68647SAndreas Gohr        global $conf;
2831c14c879SAndreas Gohr        $tpl = $this->tpl;
2841ef68647SAndreas Gohr
2852eedf77dSAndreas Gohr        // this is what we'll return
2862eedf77dSAndreas Gohr        $output = array(
2871e45476bSmnapp            'cover' => '',
2882eedf77dSAndreas Gohr            'html'  => '',
2892eedf77dSAndreas Gohr            'page'  => '',
2902eedf77dSAndreas Gohr            'first' => '',
2912eedf77dSAndreas Gohr            'cite'  => '',
2922eedf77dSAndreas Gohr        );
2932eedf77dSAndreas Gohr
2942eedf77dSAndreas Gohr        // prepare header/footer elements
2952eedf77dSAndreas Gohr        $html = '';
29633c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
29733c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
29833c15297SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/' . $section . $order . '.html';
29933c15297SGerrit Uitslag                if(file_exists($file)) {
30033c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
30133c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
30233c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
3032eedf77dSAndreas Gohr
3042eedf77dSAndreas Gohr                    // register the needed pseudo CSS
30533c15297SGerrit Uitslag                    if($order == '_first') {
30633c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
30733c15297SGerrit Uitslag                    } elseif($order == '_even') {
30833c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
30933c15297SGerrit Uitslag                    } elseif($order == '_odd') {
31033c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
311daa70883SAndreas Gohr                    } else {
31233c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
3132eedf77dSAndreas Gohr                    }
3142eedf77dSAndreas Gohr                }
3152eedf77dSAndreas Gohr            }
3162eedf77dSAndreas Gohr        }
3172eedf77dSAndreas Gohr
3181ef68647SAndreas Gohr        // prepare replacements
3191ef68647SAndreas Gohr        $replace = array(
3201ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
3211ef68647SAndreas Gohr            '@PAGES@'   => '{nb}',
3222eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
3231ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
3241ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
3251ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
3265d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
327a180c973SKlap-in            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $tpl . '/'
3281ef68647SAndreas Gohr        );
3291ef68647SAndreas Gohr
3302eedf77dSAndreas Gohr        // set HTML element
331a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
332a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
333a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
3341ef68647SAndreas Gohr
3351e45476bSmnapp        // cover page
33633c15297SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/cover.html';
33733c15297SGerrit Uitslag        if(file_exists($coverfile)) {
33833c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
3391e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
3406e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
3411e45476bSmnapp        }
3421e45476bSmnapp
34333c15297SGerrit Uitslag        // cover page
34433c15297SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/back.html';
34533c15297SGerrit Uitslag        if(file_exists($backfile)) {
34633c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
34733c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
34833c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
34933c15297SGerrit Uitslag        }
35033c15297SGerrit Uitslag
3512eedf77dSAndreas Gohr        // citation box
35233c15297SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/citation.html';
35333c15297SGerrit Uitslag        if(file_exists($citationfile)) {
35433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
3552eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
3562eedf77dSAndreas Gohr        }
3571ef68647SAndreas Gohr
3582eedf77dSAndreas Gohr        return $output;
3591ef68647SAndreas Gohr    }
3601ef68647SAndreas Gohr
3611ef68647SAndreas Gohr    /**
362a180c973SKlap-in     * @param string $raw code with placeholders
363a180c973SKlap-in     * @param string $id  pageid
364a180c973SKlap-in     * @return string
365a180c973SKlap-in     */
366a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
367a180c973SKlap-in        global $REV;
368a180c973SKlap-in
369a180c973SKlap-in        // generate qr code for this page using google infographics api
370a180c973SKlap-in        $qr_code = '';
371a180c973SKlap-in        if($this->getConf('qrcodesize')) {
372a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
373a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
374a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
375a180c973SKlap-in        }
376a180c973SKlap-in        // prepare replacements
377a180c973SKlap-in        $replace['@ID@']      = $id;
378a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
379a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
380a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
381a180c973SKlap-in
382a180c973SKlap-in        return str_replace(array_keys($replace), array_values($replace), $raw);
383a180c973SKlap-in    }
384a180c973SKlap-in
385a180c973SKlap-in    /**
3861c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
3871ef68647SAndreas Gohr     */
3881c14c879SAndreas Gohr    protected function load_css() {
389737417c6SKlap-in        global $conf;
3901c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
3911c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
3921c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
393ee19bac3SLuigi Micco
3941c14c879SAndreas Gohr        // prepare CSS files
3951c14c879SAndreas Gohr        $files = array_merge(
3961c14c879SAndreas Gohr            array(
3971c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
3981c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
3991c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
4001c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
4011c14c879SAndreas Gohr            ),
4021c14c879SAndreas Gohr            css_pluginstyles('all'),
40358e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
4041c14c879SAndreas Gohr            array(
4051c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
4061c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4071c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
4081c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
4091c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
4101c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4111c14c879SAndreas Gohr            )
4121c14c879SAndreas Gohr        );
4131c14c879SAndreas Gohr        $css = '';
4141c14c879SAndreas Gohr        foreach($files as $file => $location) {
41528e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
41628e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
4171c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
4181ef68647SAndreas Gohr        }
4191ef68647SAndreas Gohr
42028e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
4211c14c879SAndreas Gohr            // apply pattern replacements
42228e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
42328e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
42428e636eaSGerrit Uitslag
42528e636eaSGerrit Uitslag            // parse less
42628e636eaSGerrit Uitslag            $css = css_parseless($css);
42728e636eaSGerrit Uitslag        } else {
42828e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
4291c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
43028e636eaSGerrit Uitslag        }
4311ef68647SAndreas Gohr
4321c14c879SAndreas Gohr        return $css;
433ee19bac3SLuigi Micco    }
4341c14c879SAndreas Gohr
43558e6409eSAndreas Gohr    /**
43658e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
43758e6409eSAndreas Gohr     *
43858e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
43958e6409eSAndreas Gohr     *
44058e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
44158e6409eSAndreas Gohr     */
4426be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
44358e6409eSAndreas Gohr        $list = array();
44458e6409eSAndreas Gohr        $plugins = plugin_list();
445f54b51f7SAndreas Gohr
446f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
44758e6409eSAndreas Gohr        foreach($plugins as $p) {
448f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
449f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
450f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
451f54b51f7SAndreas Gohr            }
452f54b51f7SAndreas Gohr
45358e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
45458e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
45558e6409eSAndreas Gohr            } else {
45658e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
45758e6409eSAndreas Gohr            }
45858e6409eSAndreas Gohr        }
45958e6409eSAndreas Gohr        return $list;
46058e6409eSAndreas Gohr    }
461ad18f4e1SGerrit Uitslag
462ad18f4e1SGerrit Uitslag    /**
463*60e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
464*60e59de7SGerrit Uitslag     *
465*60e59de7SGerrit Uitslag     * @return array
466*60e59de7SGerrit Uitslag     */
467*60e59de7SGerrit Uitslag    public function getExportedPages() {
468*60e59de7SGerrit Uitslag        return $this->list;
469*60e59de7SGerrit Uitslag    }
470*60e59de7SGerrit Uitslag
471*60e59de7SGerrit Uitslag    /**
472ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
473ad18f4e1SGerrit Uitslag     */
474ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
475ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
476ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
477ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
478ad18f4e1SGerrit Uitslag    }
479ad18f4e1SGerrit Uitslag
480ad18f4e1SGerrit Uitslag    /**
481ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
482ad18f4e1SGerrit Uitslag     */
483ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
484ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
485ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
486ad18f4e1SGerrit Uitslag        return 0;
487ad18f4e1SGerrit Uitslag    }
48826be4eceSGerrit Uitslag
48926be4eceSGerrit Uitslag    /**
49026be4eceSGerrit Uitslag     * Set error notification and reload page again
49126be4eceSGerrit Uitslag     *
49226be4eceSGerrit Uitslag     * @param Doku_Event $event
49326be4eceSGerrit Uitslag     * @param string     $msglangkey key of translation key
49426be4eceSGerrit Uitslag     */
49526be4eceSGerrit Uitslag    private function showPageWithErrorMsg(&$event, $msglangkey) {
49626be4eceSGerrit Uitslag        msg($this->getLang($msglangkey), -1);
49726be4eceSGerrit Uitslag
49826be4eceSGerrit Uitslag        $event->data = 'show';
49926be4eceSGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
50026be4eceSGerrit Uitslag    }
501ee19bac3SLuigi Micco}
502