xref: /plugin/dw2pdf/action.php (revision c00eb13b77c20cf2823cd5de29202547484893fc)
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
211c14c879SAndreas Gohr    private $tpl;
221c14c879SAndreas Gohr
231c14c879SAndreas Gohr    /**
241c14c879SAndreas Gohr     * Constructor. Sets the correct template
251c14c879SAndreas Gohr     */
266be736bfSGerrit Uitslag    public function __construct() {
27737417c6SKlap-in        $tpl = false;
281c14c879SAndreas Gohr        if(isset($_REQUEST['tpl'])) {
291c14c879SAndreas Gohr            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/', '', $_REQUEST['tpl']));
301c14c879SAndreas Gohr        }
311c14c879SAndreas Gohr        if(!$tpl) $tpl = $this->getConf('template');
321c14c879SAndreas Gohr        if(!$tpl) $tpl = 'default';
331c14c879SAndreas Gohr        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) $tpl = 'default';
341c14c879SAndreas Gohr
351c14c879SAndreas Gohr        $this->tpl = $tpl;
361c14c879SAndreas Gohr    }
371c14c879SAndreas Gohr
38ee19bac3SLuigi Micco    /**
39ee19bac3SLuigi Micco     * Register the events
40ee19bac3SLuigi Micco     */
416be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
42ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
436be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
44ee19bac3SLuigi Micco    }
45ee19bac3SLuigi Micco
461c14c879SAndreas Gohr    /**
471c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
48737417c6SKlap-in     *
49737417c6SKlap-in     * @param Doku_Event $event
50737417c6SKlap-in     * @param array      $param
51737417c6SKlap-in     * @return bool
521c14c879SAndreas Gohr     */
536be736bfSGerrit Uitslag    public function convert(&$event, $param) {
54ee19bac3SLuigi Micco        global $ACT;
55ee19bac3SLuigi Micco        global $REV;
56ee19bac3SLuigi Micco        global $ID;
57ad18f4e1SGerrit Uitslag        global $INPUT, $conf;
58ee19bac3SLuigi Micco
591ef68647SAndreas Gohr        // our event?
60ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false;
61ee19bac3SLuigi Micco
621ef68647SAndreas Gohr        // check user's rights
631ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
641ef68647SAndreas Gohr
6587c86ddaSAndreas Gohr        // one or multiple pages?
6687c86ddaSAndreas Gohr        $list = array();
6728e636eaSGerrit Uitslag
6887c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
6987c86ddaSAndreas Gohr            $list[0] = $ID;
70737417c6SKlap-in            $title = p_get_first_heading($ID);
71ad18f4e1SGerrit Uitslag
72ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
73ad18f4e1SGerrit Uitslag            //check input for title and ns
7426be4eceSGerrit Uitslag            if(!$title = $INPUT->str('pdfns_title')) {
7526be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
76ad18f4e1SGerrit Uitslag                return false;
77ad18f4e1SGerrit Uitslag            }
7826be4eceSGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('pdfns_ns'));
79ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
8026be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
81ad18f4e1SGerrit Uitslag                return false;
82ad18f4e1SGerrit Uitslag            }
83ad18f4e1SGerrit Uitslag
8426be4eceSGerrit Uitslag            //sort order
85ad18f4e1SGerrit Uitslag            $order = $INPUT->str('pdfns_order', 'natural', true);
86ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
87ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
88ad18f4e1SGerrit Uitslag                $order = 'natural';
89ad18f4e1SGerrit Uitslag            }
90ad18f4e1SGerrit Uitslag
9126be4eceSGerrit Uitslag            //search depth
92ad18f4e1SGerrit Uitslag            $depth = $INPUT->int('pdfns_depth', 0);
93ad18f4e1SGerrit Uitslag            if($depth < 0) {
94ad18f4e1SGerrit Uitslag                $depth = 0;
95ad18f4e1SGerrit Uitslag            }
9626be4eceSGerrit Uitslag
97ad18f4e1SGerrit Uitslag            //page search
98ad18f4e1SGerrit Uitslag            $result = array();
99ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
100ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
101ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
102ad18f4e1SGerrit Uitslag
10326be4eceSGerrit Uitslag            //sorting
104ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
105ad18f4e1SGerrit Uitslag                if($order == 'date') {
106ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
107ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
108ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
109ad18f4e1SGerrit Uitslag                }
110ad18f4e1SGerrit Uitslag            }
111ad18f4e1SGerrit Uitslag
112ad18f4e1SGerrit Uitslag            foreach($result as $item) {
113ad18f4e1SGerrit Uitslag                $list[] = $item['id'];
114ad18f4e1SGerrit Uitslag            }
115ad18f4e1SGerrit Uitslag
116737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
11726be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
11826be4eceSGerrit Uitslag            if(!$title = $INPUT->str('pdfbook_title')) { //TODO when title is changed, the cached file contains the old title
11926be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
120737417c6SKlap-in                return false;
12126be4eceSGerrit Uitslag            } else {
122737417c6SKlap-in                $list = explode("|", $_COOKIE['list-pagelist']);
12326be4eceSGerrit Uitslag            }
124ad18f4e1SGerrit Uitslag
125737417c6SKlap-in        } else {
12626be4eceSGerrit Uitslag            //show empty bookcreator message
12726be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
128737417c6SKlap-in            return false;
129737417c6SKlap-in        }
130737417c6SKlap-in
131737417c6SKlap-in        // it's ours, no one else's
132737417c6SKlap-in        $event->preventDefault();
13387c86ddaSAndreas Gohr
1346ea88a05SAndreas Gohr        // decide on the paper setup from param or config
1356ea88a05SAndreas Gohr        $pagesize    = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
1366ea88a05SAndreas Gohr        $orientation = $INPUT->str('orientation', $this->getConf('orientation'), true);
1376ea88a05SAndreas Gohr
13887c86ddaSAndreas Gohr        // prepare cache
1396ea88a05SAndreas Gohr        $cache = new cache(join(',', $list) . $REV . $this->tpl . $pagesize . $orientation, '.dw2.pdf');
14087c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN', $list);
14187c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
14287c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
14387c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
144df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
14587c86ddaSAndreas Gohr
146d01d2a42SAndreas Gohr        // hard work only when no cache available
14787c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
1481ef68647SAndreas Gohr            // initialize PDF library
149cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__) . "/DokuPDF.class.php");
1506ea88a05SAndreas Gohr
1516ea88a05SAndreas Gohr            $mpdf = new DokuPDF($pagesize, $orientation);
152ee19bac3SLuigi Micco
153d62df65bSAndreas Gohr            // let mpdf fix local links
154d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
155d62df65bSAndreas Gohr            $url = $self['scheme'] . '://' . $self['host'];
156737417c6SKlap-in            if($self['port']) $url .= ':' . $self['port'];
157d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
158d62df65bSAndreas Gohr
15956d13144SAndreas Gohr            // Set the title
16056d13144SAndreas Gohr            $mpdf->SetTitle($title);
16156d13144SAndreas Gohr
1621ef68647SAndreas Gohr            // some default settings
163daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
164daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
165daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
166daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
1672eedf77dSAndreas Gohr
16856d13144SAndreas Gohr            // load the template
1691c14c879SAndreas Gohr            $template = $this->load_template($title);
170ee19bac3SLuigi Micco
1711ef68647SAndreas Gohr            // prepare HTML header styles
172ee19bac3SLuigi Micco            $html  = '<html><head>';
173737417c6SKlap-in            $html .= '<style type="text/css">';
1741c14c879SAndreas Gohr            $html .= $this->load_css();
175daa70883SAndreas Gohr            $html .= '@page { size:auto; ' . $template['page'] . '}';
1762eedf77dSAndreas Gohr            $html .= '@page :first {' . $template['first'] . '}';
1771ef68647SAndreas Gohr            $html .= '</style>';
1781ef68647SAndreas Gohr            $html .= '</head><body>';
1792eedf77dSAndreas Gohr            $html .= $template['html'];
1801c14c879SAndreas Gohr            $html .= '<div class="dokuwiki">';
1812eedf77dSAndreas Gohr
1821e45476bSmnapp            // insert the cover page
1836e2ec302SGerrit Uitslag            $html .= $template['cover'];
184ed9f9952Smnapp
185*c00eb13bSGerrit Uitslag            // store original pageid
186*c00eb13bSGerrit Uitslag            $keep = $ID;
187*c00eb13bSGerrit Uitslag
1881ef68647SAndreas Gohr            // loop over all pages
1891ef68647SAndreas Gohr            $cnt = count($list);
1901ef68647SAndreas Gohr            for($n = 0; $n < $cnt; $n++) {
191ee19bac3SLuigi Micco                $page = $list[$n];
192ee19bac3SLuigi Micco
193*c00eb13bSGerrit Uitslag                // set global pageid to the rendered page
194*c00eb13bSGerrit Uitslag                $ID   = $page;
195*c00eb13bSGerrit Uitslag
196a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page, $REV), 'dw2pdf', $page);
197a180c973SKlap-in                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
1981ef68647SAndreas Gohr                if($n < ($cnt - 1)) {
1991ef68647SAndreas Gohr                    $html .= '<pagebreak />';
2001ef68647SAndreas Gohr                }
201ee19bac3SLuigi Micco            }
202*c00eb13bSGerrit Uitslag            //restore ID
203*c00eb13bSGerrit Uitslag            $ID = $keep;
204ee19bac3SLuigi Micco
20533c15297SGerrit Uitslag            // insert the back page
20633c15297SGerrit Uitslag            $html .= $template['back'];
20733c15297SGerrit Uitslag
2081c14c879SAndreas Gohr            $html .= '</div>';
209eeb17e15SAndreas Gohr            $html .= '</body>';
210eeb17e15SAndreas Gohr            $html .= '</html>';
211f765508eSGerrit Uitslag
212f765508eSGerrit Uitslag            //Return html for debugging
21326be4eceSGerrit Uitslag            if($conf['allowdebug'] && $_GET['debughtml'] == 'html') {
21426be4eceSGerrit Uitslag                echo $html;
21526be4eceSGerrit Uitslag                exit();
21626be4eceSGerrit Uitslag            };
217f765508eSGerrit Uitslag
218ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
219a06728a6SAndreas Gohr
22087c86ddaSAndreas Gohr            // write to cache file
22187c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
22287c86ddaSAndreas Gohr        }
22387c86ddaSAndreas Gohr
22487c86ddaSAndreas Gohr        // deliver the file
22587c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
226b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
22787c86ddaSAndreas Gohr        header('Pragma: public');
22887c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
22987c86ddaSAndreas Gohr
2309a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
23187c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
2329a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
23387c86ddaSAndreas Gohr        } else {
2349a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
23587c86ddaSAndreas Gohr        }
236ee19bac3SLuigi Micco
237e993da11SGerrit Uitslag        //try to send file, and exit if done
238e993da11SGerrit Uitslag        http_sendfile($cache->cache);
23987c86ddaSAndreas Gohr
24087c86ddaSAndreas Gohr        $fp = @fopen($cache->cache, "rb");
24187c86ddaSAndreas Gohr        if($fp) {
24287c86ddaSAndreas Gohr            http_rangeRequest($fp, filesize($cache->cache), 'application/pdf');
24387c86ddaSAndreas Gohr        } else {
24487c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
24587c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
24687c86ddaSAndreas Gohr        }
2471ef68647SAndreas Gohr        exit();
2481ef68647SAndreas Gohr    }
2491ef68647SAndreas Gohr
2506be736bfSGerrit Uitslag    /**
2516be736bfSGerrit Uitslag     * Add 'export pdf'-button to pagetools
2526be736bfSGerrit Uitslag     *
2536be736bfSGerrit Uitslag     * @param Doku_Event $event
2546be736bfSGerrit Uitslag     * @param mixed      $param not defined
2556be736bfSGerrit Uitslag     */
2566be736bfSGerrit Uitslag    public function addbutton(&$event, $param) {
2572c53f619SAndreas Gohr        global $ID, $REV;
2586be736bfSGerrit Uitslag
2596be736bfSGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
2606be736bfSGerrit Uitslag            $params = array('do' => 'export_pdf');
2616be736bfSGerrit Uitslag            if($REV) $params['rev'] = $REV;
2626be736bfSGerrit Uitslag
2631e02f86cSAndreas Gohr            // insert button at position before last (up to top)
2641e02f86cSAndreas Gohr            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
2651e02f86cSAndreas Gohr                                    array('export_pdf' =>
2666be736bfSGerrit Uitslag                                          '<li>'
2676be736bfSGerrit Uitslag                                          . '<a href=' . wl($ID, $params) . '  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
2686be736bfSGerrit Uitslag                                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
2696be736bfSGerrit Uitslag                                          . '</a>'
2701e02f86cSAndreas Gohr                                          . '</li>'
2711e02f86cSAndreas Gohr                                    ) +
2721e02f86cSAndreas Gohr                                    array_slice($event->data['items'], -1, 1, true);
2736be736bfSGerrit Uitslag        }
2746be736bfSGerrit Uitslag    }
275d01d2a42SAndreas Gohr
276d01d2a42SAndreas Gohr    /**
2772eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
2781ef68647SAndreas Gohr     */
2791c14c879SAndreas Gohr    protected function load_template($title) {
2801ef68647SAndreas Gohr        global $ID;
2811ef68647SAndreas Gohr        global $conf;
2821c14c879SAndreas Gohr        $tpl = $this->tpl;
2831ef68647SAndreas Gohr
2842eedf77dSAndreas Gohr        // this is what we'll return
2852eedf77dSAndreas Gohr        $output = array(
2861e45476bSmnapp            'cover' => '',
2872eedf77dSAndreas Gohr            'html'  => '',
2882eedf77dSAndreas Gohr            'page'  => '',
2892eedf77dSAndreas Gohr            'first' => '',
2902eedf77dSAndreas Gohr            'cite'  => '',
2912eedf77dSAndreas Gohr        );
2922eedf77dSAndreas Gohr
2932eedf77dSAndreas Gohr        // prepare header/footer elements
2942eedf77dSAndreas Gohr        $html = '';
29533c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
29633c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
29733c15297SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/' . $section . $order . '.html';
29833c15297SGerrit Uitslag                if(file_exists($file)) {
29933c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
30033c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
30133c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
3022eedf77dSAndreas Gohr
3032eedf77dSAndreas Gohr                    // register the needed pseudo CSS
30433c15297SGerrit Uitslag                    if($order == '_first') {
30533c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
30633c15297SGerrit Uitslag                    } elseif($order == '_even') {
30733c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
30833c15297SGerrit Uitslag                    } elseif($order == '_odd') {
30933c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
310daa70883SAndreas Gohr                    } else {
31133c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
3122eedf77dSAndreas Gohr                    }
3132eedf77dSAndreas Gohr                }
3142eedf77dSAndreas Gohr            }
3152eedf77dSAndreas Gohr        }
3162eedf77dSAndreas Gohr
3171ef68647SAndreas Gohr        // prepare replacements
3181ef68647SAndreas Gohr        $replace = array(
3191ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
3201ef68647SAndreas Gohr            '@PAGES@'   => '{nb}',
3212eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
3221ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
3231ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
3241ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
3255d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
326a180c973SKlap-in            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $tpl . '/'
3271ef68647SAndreas Gohr        );
3281ef68647SAndreas Gohr
3292eedf77dSAndreas Gohr        // set HTML element
330a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
331a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
332a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
3331ef68647SAndreas Gohr
3341e45476bSmnapp        // cover page
33533c15297SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/cover.html';
33633c15297SGerrit Uitslag        if(file_exists($coverfile)) {
33733c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
3381e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
3396e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
3401e45476bSmnapp        }
3411e45476bSmnapp
34233c15297SGerrit Uitslag        // cover page
34333c15297SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/back.html';
34433c15297SGerrit Uitslag        if(file_exists($backfile)) {
34533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
34633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
34733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
34833c15297SGerrit Uitslag        }
34933c15297SGerrit Uitslag
3502eedf77dSAndreas Gohr        // citation box
35133c15297SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/citation.html';
35233c15297SGerrit Uitslag        if(file_exists($citationfile)) {
35333c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
3542eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
3552eedf77dSAndreas Gohr        }
3561ef68647SAndreas Gohr
3572eedf77dSAndreas Gohr        return $output;
3581ef68647SAndreas Gohr    }
3591ef68647SAndreas Gohr
3601ef68647SAndreas Gohr    /**
361a180c973SKlap-in     * @param string $raw code with placeholders
362a180c973SKlap-in     * @param string $id  pageid
363a180c973SKlap-in     * @return string
364a180c973SKlap-in     */
365a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
366a180c973SKlap-in        global $REV;
367a180c973SKlap-in
368a180c973SKlap-in        // generate qr code for this page using google infographics api
369a180c973SKlap-in        $qr_code = '';
370a180c973SKlap-in        if($this->getConf('qrcodesize')) {
371a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
372a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
373a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
374a180c973SKlap-in        }
375a180c973SKlap-in        // prepare replacements
376a180c973SKlap-in        $replace['@ID@']      = $id;
377a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
378a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
379a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
380a180c973SKlap-in
381a180c973SKlap-in        return str_replace(array_keys($replace), array_values($replace), $raw);
382a180c973SKlap-in    }
383a180c973SKlap-in
384a180c973SKlap-in    /**
3851c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
3861ef68647SAndreas Gohr     */
3871c14c879SAndreas Gohr    protected function load_css() {
388737417c6SKlap-in        global $conf;
3891c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
3901c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
3911c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
392ee19bac3SLuigi Micco
3931c14c879SAndreas Gohr        // prepare CSS files
3941c14c879SAndreas Gohr        $files = array_merge(
3951c14c879SAndreas Gohr            array(
3961c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
3971c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
3981c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
3991c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
4001c14c879SAndreas Gohr            ),
4011c14c879SAndreas Gohr            css_pluginstyles('all'),
40258e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
4031c14c879SAndreas Gohr            array(
4041c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
4051c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4061c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
4071c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
4081c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
4091c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4101c14c879SAndreas Gohr            )
4111c14c879SAndreas Gohr        );
4121c14c879SAndreas Gohr        $css = '';
4131c14c879SAndreas Gohr        foreach($files as $file => $location) {
41428e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
41528e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
4161c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
4171ef68647SAndreas Gohr        }
4181ef68647SAndreas Gohr
41928e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
4201c14c879SAndreas Gohr            // apply pattern replacements
42128e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
42228e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
42328e636eaSGerrit Uitslag
42428e636eaSGerrit Uitslag            // parse less
42528e636eaSGerrit Uitslag            $css = css_parseless($css);
42628e636eaSGerrit Uitslag        } else {
42728e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
4281c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
42928e636eaSGerrit Uitslag        }
4301ef68647SAndreas Gohr
4311c14c879SAndreas Gohr        return $css;
432ee19bac3SLuigi Micco    }
4331c14c879SAndreas Gohr
43458e6409eSAndreas Gohr    /**
43558e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
43658e6409eSAndreas Gohr     *
43758e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
43858e6409eSAndreas Gohr     *
43958e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
44058e6409eSAndreas Gohr     */
4416be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
44258e6409eSAndreas Gohr        $list = array();
44358e6409eSAndreas Gohr        $plugins = plugin_list();
444f54b51f7SAndreas Gohr
445f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
44658e6409eSAndreas Gohr        foreach($plugins as $p) {
447f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
448f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
449f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
450f54b51f7SAndreas Gohr            }
451f54b51f7SAndreas Gohr
45258e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
45358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
45458e6409eSAndreas Gohr            } else {
45558e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
45658e6409eSAndreas Gohr            }
45758e6409eSAndreas Gohr        }
45858e6409eSAndreas Gohr        return $list;
45958e6409eSAndreas Gohr    }
460ad18f4e1SGerrit Uitslag
461ad18f4e1SGerrit Uitslag    /**
462ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
463ad18f4e1SGerrit Uitslag     */
464ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
465ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
466ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
467ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
468ad18f4e1SGerrit Uitslag    }
469ad18f4e1SGerrit Uitslag
470ad18f4e1SGerrit Uitslag    /**
471ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
472ad18f4e1SGerrit Uitslag     */
473ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
474ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
475ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
476ad18f4e1SGerrit Uitslag        return 0;
477ad18f4e1SGerrit Uitslag    }
47826be4eceSGerrit Uitslag
47926be4eceSGerrit Uitslag    /**
48026be4eceSGerrit Uitslag     * Set error notification and reload page again
48126be4eceSGerrit Uitslag     *
48226be4eceSGerrit Uitslag     * @param Doku_Event $event
48326be4eceSGerrit Uitslag     * @param string     $msglangkey key of translation key
48426be4eceSGerrit Uitslag     */
48526be4eceSGerrit Uitslag    private function showPageWithErrorMsg(&$event, $msglangkey) {
48626be4eceSGerrit Uitslag        msg($this->getLang($msglangkey), -1);
48726be4eceSGerrit Uitslag
48826be4eceSGerrit Uitslag        $event->data = 'show';
48926be4eceSGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
49026be4eceSGerrit Uitslag    }
491ee19bac3SLuigi Micco}
492