xref: /plugin/dw2pdf/action.php (revision a2c33768b561d90d55e3eb15c7ff04b8b70b3d2b)
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
2160e59de7SGerrit Uitslag    protected $tpl;
2260e59de7SGerrit 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?
6760e59de7SGerrit Uitslag        $this->list = array();
6828e636eaSGerrit Uitslag
6987c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
7060e59de7SGerrit 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) {
11460e59de7SGerrit 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 {
12360e59de7SGerrit 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
14060e59de7SGerrit Uitslag        $cache = new cache(join(',', $this->list) . $REV . $this->tpl . $pagesize . $orientation, '.dw2.pdf');
14160e59de7SGerrit 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)) {
149*a2c33768SGerrit Uitslag            // debug enabled?
150*a2c33768SGerrit Uitslag            $debug = $conf['allowdebug'] && isset($_GET['debughtml']);
151*a2c33768SGerrit Uitslag
1521ef68647SAndreas Gohr            // initialize PDF library
153cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__) . "/DokuPDF.class.php");
1546ea88a05SAndreas Gohr
1556ea88a05SAndreas Gohr            $mpdf = new DokuPDF($pagesize, $orientation);
156ee19bac3SLuigi Micco
157d62df65bSAndreas Gohr            // let mpdf fix local links
158d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
159d62df65bSAndreas Gohr            $url = $self['scheme'] . '://' . $self['host'];
160737417c6SKlap-in            if($self['port']) $url .= ':' . $self['port'];
161d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
162d62df65bSAndreas Gohr
16356d13144SAndreas Gohr            // Set the title
16456d13144SAndreas Gohr            $mpdf->SetTitle($title);
16556d13144SAndreas Gohr
1661ef68647SAndreas Gohr            // some default settings
167daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
168daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
169daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
170daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
1712eedf77dSAndreas Gohr
17256d13144SAndreas Gohr            // load the template
1731c14c879SAndreas Gohr            $template = $this->load_template($title);
174ee19bac3SLuigi Micco
1751ef68647SAndreas Gohr            // prepare HTML header styles
176*a2c33768SGerrit Uitslag            $html = '';
177*a2c33768SGerrit Uitslag            if($debug) {
178*a2c33768SGerrit Uitslag                $html .= '<html><head>';
179737417c6SKlap-in                $html .= '<style type="text/css">';
180*a2c33768SGerrit Uitslag            }
181*a2c33768SGerrit Uitslag            $styles = $this->load_css();
182*a2c33768SGerrit Uitslag            $styles .= '@page { size:auto; ' . $template['page'] . '}';
183*a2c33768SGerrit Uitslag            $styles .= '@page :first {' . $template['first'] . '}';
184*a2c33768SGerrit Uitslag            $mpdf->WriteHTML($styles, 1);
185*a2c33768SGerrit Uitslag
186*a2c33768SGerrit Uitslag            if($debug) {
187*a2c33768SGerrit Uitslag                $html .= $styles;
1881ef68647SAndreas Gohr                $html .= '</style>';
1891ef68647SAndreas Gohr                $html .= '</head><body>';
190*a2c33768SGerrit Uitslag            }
191*a2c33768SGerrit Uitslag
192*a2c33768SGerrit Uitslag            $body_start = $template['html'];
193*a2c33768SGerrit Uitslag            $body_start .= '<div class="dokuwiki">';
1942eedf77dSAndreas Gohr
1951e45476bSmnapp            // insert the cover page
196*a2c33768SGerrit Uitslag            $body_start .= $template['cover'];
197*a2c33768SGerrit Uitslag
198*a2c33768SGerrit Uitslag            $mpdf->WriteHTML($body_start, 2, true, false); //start body html
199*a2c33768SGerrit Uitslag            if($debug) {
200*a2c33768SGerrit Uitslag                $html .= $body_start;
201*a2c33768SGerrit Uitslag            }
202ed9f9952Smnapp
203c00eb13bSGerrit Uitslag            // store original pageid
204c00eb13bSGerrit Uitslag            $keep = $ID;
205c00eb13bSGerrit Uitslag
2061ef68647SAndreas Gohr            // loop over all pages
207*a2c33768SGerrit Uitslag            $cnt = count($this->list);
2081ef68647SAndreas Gohr            for($n = 0; $n < $cnt; $n++) {
209*a2c33768SGerrit Uitslag                $page = $this->list[$n];
210ee19bac3SLuigi Micco
211c00eb13bSGerrit Uitslag                // set global pageid to the rendered page
212c00eb13bSGerrit Uitslag                $ID   = $page;
213c00eb13bSGerrit Uitslag
214*a2c33768SGerrit Uitslag                $pagehtml = p_cached_output(wikiFN($page, $REV), 'dw2pdf', $page);
215*a2c33768SGerrit Uitslag                $pagehtml .= $this->page_depend_replacements($template['cite'], cleanID($page));
2161ef68647SAndreas Gohr                if($n < ($cnt - 1)) {
217*a2c33768SGerrit Uitslag                    $pagehtml .= '<pagebreak />';
218*a2c33768SGerrit Uitslag                }
219*a2c33768SGerrit Uitslag
220*a2c33768SGerrit Uitslag                $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
221*a2c33768SGerrit Uitslag                if($debug) {
222*a2c33768SGerrit Uitslag                    $html .= $pagehtml;
2231ef68647SAndreas Gohr                }
224ee19bac3SLuigi Micco            }
225c00eb13bSGerrit Uitslag            //restore ID
226c00eb13bSGerrit Uitslag            $ID = $keep;
227ee19bac3SLuigi Micco
22833c15297SGerrit Uitslag            // insert the back page
229*a2c33768SGerrit Uitslag            $body_end = $template['back'];
23033c15297SGerrit Uitslag
231*a2c33768SGerrit Uitslag            $body_end .= '</div>';
232*a2c33768SGerrit Uitslag
233*a2c33768SGerrit Uitslag            $mpdf->WriteHTML($body_end, 2, false, true); // end body html
234*a2c33768SGerrit Uitslag            if($debug) {
235*a2c33768SGerrit Uitslag                $html .= $body_end;
236eeb17e15SAndreas Gohr                $html .= '</body>';
237eeb17e15SAndreas Gohr                $html .= '</html>';
238*a2c33768SGerrit Uitslag            }
239f765508eSGerrit Uitslag
240f765508eSGerrit Uitslag            //Return html for debugging
241*a2c33768SGerrit Uitslag            if($debug) {
242*a2c33768SGerrit Uitslag                if($_GET['debughtml'] == 'html') {
24326be4eceSGerrit Uitslag                    echo $html;
244*a2c33768SGerrit Uitslag                } else {
245*a2c33768SGerrit Uitslag                    header('Content-Type: text/plain; charset=utf-8');
246*a2c33768SGerrit Uitslag                    echo $html;
247*a2c33768SGerrit Uitslag                }
24826be4eceSGerrit Uitslag                exit();
24926be4eceSGerrit Uitslag            };
250f765508eSGerrit Uitslag
25187c86ddaSAndreas Gohr            // write to cache file
25287c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
25387c86ddaSAndreas Gohr        }
25487c86ddaSAndreas Gohr
25587c86ddaSAndreas Gohr        // deliver the file
25687c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
257b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
25887c86ddaSAndreas Gohr        header('Pragma: public');
25987c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
26087c86ddaSAndreas Gohr
2619a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
26287c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
2639a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
26487c86ddaSAndreas Gohr        } else {
2659a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
26687c86ddaSAndreas Gohr        }
267ee19bac3SLuigi Micco
268e993da11SGerrit Uitslag        //try to send file, and exit if done
269e993da11SGerrit Uitslag        http_sendfile($cache->cache);
27087c86ddaSAndreas Gohr
27187c86ddaSAndreas Gohr        $fp = @fopen($cache->cache, "rb");
27287c86ddaSAndreas Gohr        if($fp) {
27387c86ddaSAndreas Gohr            http_rangeRequest($fp, filesize($cache->cache), 'application/pdf');
27487c86ddaSAndreas Gohr        } else {
27587c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
27687c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
27787c86ddaSAndreas Gohr        }
2781ef68647SAndreas Gohr        exit();
2791ef68647SAndreas Gohr    }
2801ef68647SAndreas Gohr
2816be736bfSGerrit Uitslag    /**
2826be736bfSGerrit Uitslag     * Add 'export pdf'-button to pagetools
2836be736bfSGerrit Uitslag     *
2846be736bfSGerrit Uitslag     * @param Doku_Event $event
2856be736bfSGerrit Uitslag     * @param mixed      $param not defined
2866be736bfSGerrit Uitslag     */
2876be736bfSGerrit Uitslag    public function addbutton(&$event, $param) {
2882c53f619SAndreas Gohr        global $ID, $REV;
2896be736bfSGerrit Uitslag
2906be736bfSGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
2916be736bfSGerrit Uitslag            $params = array('do' => 'export_pdf');
2926be736bfSGerrit Uitslag            if($REV) $params['rev'] = $REV;
2936be736bfSGerrit Uitslag
2941e02f86cSAndreas Gohr            // insert button at position before last (up to top)
2951e02f86cSAndreas Gohr            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
2961e02f86cSAndreas Gohr                                    array('export_pdf' =>
2976be736bfSGerrit Uitslag                                          '<li>'
2986be736bfSGerrit Uitslag                                          . '<a href=' . wl($ID, $params) . '  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
2996be736bfSGerrit Uitslag                                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
3006be736bfSGerrit Uitslag                                          . '</a>'
3011e02f86cSAndreas Gohr                                          . '</li>'
3021e02f86cSAndreas Gohr                                    ) +
3031e02f86cSAndreas Gohr                                    array_slice($event->data['items'], -1, 1, true);
3046be736bfSGerrit Uitslag        }
3056be736bfSGerrit Uitslag    }
306d01d2a42SAndreas Gohr
307d01d2a42SAndreas Gohr    /**
3082eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
3091ef68647SAndreas Gohr     */
3101c14c879SAndreas Gohr    protected function load_template($title) {
3111ef68647SAndreas Gohr        global $ID;
3121ef68647SAndreas Gohr        global $conf;
3131c14c879SAndreas Gohr        $tpl = $this->tpl;
3141ef68647SAndreas Gohr
3152eedf77dSAndreas Gohr        // this is what we'll return
3162eedf77dSAndreas Gohr        $output = array(
3171e45476bSmnapp            'cover' => '',
3182eedf77dSAndreas Gohr            'html'  => '',
3192eedf77dSAndreas Gohr            'page'  => '',
3202eedf77dSAndreas Gohr            'first' => '',
3212eedf77dSAndreas Gohr            'cite'  => '',
3222eedf77dSAndreas Gohr        );
3232eedf77dSAndreas Gohr
3242eedf77dSAndreas Gohr        // prepare header/footer elements
3252eedf77dSAndreas Gohr        $html = '';
32633c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
32733c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
32833c15297SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/' . $section . $order . '.html';
32933c15297SGerrit Uitslag                if(file_exists($file)) {
33033c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
33133c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
33233c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
3332eedf77dSAndreas Gohr
3342eedf77dSAndreas Gohr                    // register the needed pseudo CSS
33533c15297SGerrit Uitslag                    if($order == '_first') {
33633c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
33733c15297SGerrit Uitslag                    } elseif($order == '_even') {
33833c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
33933c15297SGerrit Uitslag                    } elseif($order == '_odd') {
34033c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
341daa70883SAndreas Gohr                    } else {
34233c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
3432eedf77dSAndreas Gohr                    }
3442eedf77dSAndreas Gohr                }
3452eedf77dSAndreas Gohr            }
3462eedf77dSAndreas Gohr        }
3472eedf77dSAndreas Gohr
3481ef68647SAndreas Gohr        // prepare replacements
3491ef68647SAndreas Gohr        $replace = array(
3501ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
3511ef68647SAndreas Gohr            '@PAGES@'   => '{nb}',
3522eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
3531ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
3541ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
3551ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
3565d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
357a180c973SKlap-in            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $tpl . '/'
3581ef68647SAndreas Gohr        );
3591ef68647SAndreas Gohr
3602eedf77dSAndreas Gohr        // set HTML element
361a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
362a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
363a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
3641ef68647SAndreas Gohr
3651e45476bSmnapp        // cover page
36633c15297SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/cover.html';
36733c15297SGerrit Uitslag        if(file_exists($coverfile)) {
36833c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
3691e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
3706e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
3711e45476bSmnapp        }
3721e45476bSmnapp
37333c15297SGerrit Uitslag        // cover page
37433c15297SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/back.html';
37533c15297SGerrit Uitslag        if(file_exists($backfile)) {
37633c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
37733c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
37833c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
37933c15297SGerrit Uitslag        }
38033c15297SGerrit Uitslag
3812eedf77dSAndreas Gohr        // citation box
38233c15297SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl . '/citation.html';
38333c15297SGerrit Uitslag        if(file_exists($citationfile)) {
38433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
3852eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
3862eedf77dSAndreas Gohr        }
3871ef68647SAndreas Gohr
3882eedf77dSAndreas Gohr        return $output;
3891ef68647SAndreas Gohr    }
3901ef68647SAndreas Gohr
3911ef68647SAndreas Gohr    /**
392a180c973SKlap-in     * @param string $raw code with placeholders
393a180c973SKlap-in     * @param string $id  pageid
394a180c973SKlap-in     * @return string
395a180c973SKlap-in     */
396a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
397a180c973SKlap-in        global $REV;
398a180c973SKlap-in
399a180c973SKlap-in        // generate qr code for this page using google infographics api
400a180c973SKlap-in        $qr_code = '';
401a180c973SKlap-in        if($this->getConf('qrcodesize')) {
402a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
403a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
404a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
405a180c973SKlap-in        }
406a180c973SKlap-in        // prepare replacements
407a180c973SKlap-in        $replace['@ID@']      = $id;
408a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
409a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
410a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
411a180c973SKlap-in
412a180c973SKlap-in        return str_replace(array_keys($replace), array_values($replace), $raw);
413a180c973SKlap-in    }
414a180c973SKlap-in
415a180c973SKlap-in    /**
4161c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
4171ef68647SAndreas Gohr     */
4181c14c879SAndreas Gohr    protected function load_css() {
419737417c6SKlap-in        global $conf;
4201c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
4211c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
4221c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
423ee19bac3SLuigi Micco
4241c14c879SAndreas Gohr        // prepare CSS files
4251c14c879SAndreas Gohr        $files = array_merge(
4261c14c879SAndreas Gohr            array(
4271c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
4281c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
4291c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
4301c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
4311c14c879SAndreas Gohr            ),
4321c14c879SAndreas Gohr            css_pluginstyles('all'),
43358e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
4341c14c879SAndreas Gohr            array(
4351c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
4361c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4371c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
4381c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
4391c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
4401c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4411c14c879SAndreas Gohr            )
4421c14c879SAndreas Gohr        );
4431c14c879SAndreas Gohr        $css = '';
4441c14c879SAndreas Gohr        foreach($files as $file => $location) {
44528e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
44628e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
4471c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
4481ef68647SAndreas Gohr        }
4491ef68647SAndreas Gohr
45028e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
4511c14c879SAndreas Gohr            // apply pattern replacements
45228e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
45328e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
45428e636eaSGerrit Uitslag
45528e636eaSGerrit Uitslag            // parse less
45628e636eaSGerrit Uitslag            $css = css_parseless($css);
45728e636eaSGerrit Uitslag        } else {
45828e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
4591c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
46028e636eaSGerrit Uitslag        }
4611ef68647SAndreas Gohr
4621c14c879SAndreas Gohr        return $css;
463ee19bac3SLuigi Micco    }
4641c14c879SAndreas Gohr
46558e6409eSAndreas Gohr    /**
46658e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
46758e6409eSAndreas Gohr     *
46858e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
46958e6409eSAndreas Gohr     *
47058e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
47158e6409eSAndreas Gohr     */
4726be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
47358e6409eSAndreas Gohr        $list = array();
47458e6409eSAndreas Gohr        $plugins = plugin_list();
475f54b51f7SAndreas Gohr
476f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
47758e6409eSAndreas Gohr        foreach($plugins as $p) {
478f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
479f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
480f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
481f54b51f7SAndreas Gohr            }
482f54b51f7SAndreas Gohr
48358e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
48458e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
48558e6409eSAndreas Gohr            } else {
48658e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
48758e6409eSAndreas Gohr            }
48858e6409eSAndreas Gohr        }
48958e6409eSAndreas Gohr        return $list;
49058e6409eSAndreas Gohr    }
491ad18f4e1SGerrit Uitslag
492ad18f4e1SGerrit Uitslag    /**
49360e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
49460e59de7SGerrit Uitslag     *
49560e59de7SGerrit Uitslag     * @return array
49660e59de7SGerrit Uitslag     */
49760e59de7SGerrit Uitslag    public function getExportedPages() {
49860e59de7SGerrit Uitslag        return $this->list;
49960e59de7SGerrit Uitslag    }
50060e59de7SGerrit Uitslag
50160e59de7SGerrit Uitslag    /**
502ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
503ad18f4e1SGerrit Uitslag     */
504ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
505ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
506ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
507ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
508ad18f4e1SGerrit Uitslag    }
509ad18f4e1SGerrit Uitslag
510ad18f4e1SGerrit Uitslag    /**
511ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
512ad18f4e1SGerrit Uitslag     */
513ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
514ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
515ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
516ad18f4e1SGerrit Uitslag        return 0;
517ad18f4e1SGerrit Uitslag    }
51826be4eceSGerrit Uitslag
51926be4eceSGerrit Uitslag    /**
52026be4eceSGerrit Uitslag     * Set error notification and reload page again
52126be4eceSGerrit Uitslag     *
52226be4eceSGerrit Uitslag     * @param Doku_Event $event
52326be4eceSGerrit Uitslag     * @param string     $msglangkey key of translation key
52426be4eceSGerrit Uitslag     */
52526be4eceSGerrit Uitslag    private function showPageWithErrorMsg(&$event, $msglangkey) {
52626be4eceSGerrit Uitslag        msg($this->getLang($msglangkey), -1);
52726be4eceSGerrit Uitslag
52826be4eceSGerrit Uitslag        $event->data = 'show';
52926be4eceSGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
53026be4eceSGerrit Uitslag    }
531ee19bac3SLuigi Micco}
532