xref: /plugin/dw2pdf/action.php (revision 213fdb755af27347b94e094914e120c41a19beca)
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     */
25*213fdb75SGerrit Uitslag    protected $exportConfig = null;
2660e59de7SGerrit Uitslag    protected $tpl;
2760e59de7SGerrit Uitslag    protected $list = array();
281c14c879SAndreas Gohr
291c14c879SAndreas Gohr    /**
301c14c879SAndreas Gohr     * Constructor. Sets the correct template
311c14c879SAndreas Gohr     */
326be736bfSGerrit Uitslag    public function __construct() {
3302f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
341c14c879SAndreas Gohr    }
351c14c879SAndreas Gohr
36ee19bac3SLuigi Micco    /**
37ee19bac3SLuigi Micco     * Register the events
38ee19bac3SLuigi Micco     */
396be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
40ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
416be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
42ee19bac3SLuigi Micco    }
43ee19bac3SLuigi Micco
441c14c879SAndreas Gohr    /**
451c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
46737417c6SKlap-in     *
47737417c6SKlap-in     * @param Doku_Event $event
48737417c6SKlap-in     * @param array      $param
49737417c6SKlap-in     * @return bool
501c14c879SAndreas Gohr     */
516be736bfSGerrit Uitslag    public function convert(&$event, $param) {
52ee19bac3SLuigi Micco        global $ACT;
53ee19bac3SLuigi Micco        global $REV;
54ee19bac3SLuigi Micco        global $ID;
55ad18f4e1SGerrit Uitslag        global $INPUT, $conf;
56ee19bac3SLuigi Micco
571ef68647SAndreas Gohr        // our event?
58ad18f4e1SGerrit Uitslag        if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false;
59ee19bac3SLuigi Micco
601ef68647SAndreas Gohr        // check user's rights
611ef68647SAndreas Gohr        if(auth_quickaclcheck($ID) < AUTH_READ) return false;
621ef68647SAndreas Gohr
6387c86ddaSAndreas Gohr        // one or multiple pages?
6460e59de7SGerrit Uitslag        $this->list = array();
6528e636eaSGerrit Uitslag
6687c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
6760e59de7SGerrit Uitslag            $this->list[0] = $ID;
6815923cb9SGerrit Uitslag            $title = $INPUT->str('pdftitle');
6915923cb9SGerrit Uitslag            if(!$title) {
70737417c6SKlap-in                $title = p_get_first_heading($ID);
7115923cb9SGerrit Uitslag            }
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
13502f9a447SGerrit Uitslag        //some shortcuts to export settings
13602f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
13702f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
13802f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
1396ea88a05SAndreas Gohr
14087c86ddaSAndreas Gohr        // prepare cache
14102f9a447SGerrit Uitslag        $cachekey = join(',', $this->list)
14202f9a447SGerrit Uitslag                    . $REV
14302f9a447SGerrit Uitslag                    . $this->getExportConfig('template')
14402f9a447SGerrit Uitslag                    . $this->getExportConfig('pagesize')
14502f9a447SGerrit Uitslag                    . $this->getExportConfig('orientation')
146*213fdb75SGerrit Uitslag                    . $this->getExportConfig('doublesided')
14702f9a447SGerrit Uitslag                    . ($hasToC ? join('-', $levels) : '0')
14802f9a447SGerrit Uitslag                    . $title;
14902f9a447SGerrit Uitslag        $cache = new cache($cachekey, '.dw2.pdf');
15002f9a447SGerrit Uitslag
15160e59de7SGerrit Uitslag        $depends['files']   = array_map('wikiFN', $this->list);
15287c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
15387c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
15487c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
155df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
15687c86ddaSAndreas Gohr
157d01d2a42SAndreas Gohr        // hard work only when no cache available
15887c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)) {
159a2c33768SGerrit Uitslag            // debug enabled?
160a2c33768SGerrit Uitslag
1611ef68647SAndreas Gohr            // initialize PDF library
162cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__) . "/DokuPDF.class.php");
1636ea88a05SAndreas Gohr
16402f9a447SGerrit Uitslag            $mpdf = new DokuPDF($this->getExportConfig('pagesize'), $this->getExportConfig('orientation'));
165ee19bac3SLuigi Micco
166d62df65bSAndreas Gohr            // let mpdf fix local links
167d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
168d62df65bSAndreas Gohr            $url = $self['scheme'] . '://' . $self['host'];
16902f9a447SGerrit Uitslag            if($self['port']) {
17002f9a447SGerrit Uitslag                $url .= ':' . $self['port'];
17102f9a447SGerrit Uitslag            }
172d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
173d62df65bSAndreas Gohr
17456d13144SAndreas Gohr            // Set the title
17556d13144SAndreas Gohr            $mpdf->SetTitle($title);
17656d13144SAndreas Gohr
1771ef68647SAndreas Gohr            // some default settings
178*213fdb75SGerrit Uitslag            //double-sided document, starts at an odd page (first page is a right-hand side page)
179*213fdb75SGerrit Uitslag            //single-side document has only odd pages
180*213fdb75SGerrit Uitslag            $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
181*213fdb75SGerrit Uitslag            //duplicate of mirrorMargins
182*213fdb75SGerrit Uitslag            $mpdf->useOddEven    = $this->getExportConfig('doublesided');
183daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
184daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
18502f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
18602f9a447SGerrit Uitslag            if($hasToC) {
18702f9a447SGerrit Uitslag                $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off');//use italic pageno until ToC
18802f9a447SGerrit Uitslag                $mpdf->h2toc = $levels;
18902f9a447SGerrit Uitslag            } else {
19002f9a447SGerrit Uitslag                $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
19102f9a447SGerrit Uitslag            }
1922eedf77dSAndreas Gohr
19356d13144SAndreas Gohr            // load the template
1941c14c879SAndreas Gohr            $template = $this->load_template($title);
195ee19bac3SLuigi Micco
1961ef68647SAndreas Gohr            // prepare HTML header styles
197a2c33768SGerrit Uitslag            $html = '';
19802f9a447SGerrit Uitslag            if($isDebug) {
199a2c33768SGerrit Uitslag                $html .= '<html><head>';
200737417c6SKlap-in                $html .= '<style type="text/css">';
201a2c33768SGerrit Uitslag            }
202a2c33768SGerrit Uitslag            $styles = $this->load_css();
203a2c33768SGerrit Uitslag            $styles .= '@page { size:auto; ' . $template['page'] . '}';
204a2c33768SGerrit Uitslag            $styles .= '@page :first {' . $template['first'] . '}';
205a2c33768SGerrit Uitslag            $mpdf->WriteHTML($styles, 1);
206a2c33768SGerrit Uitslag
20702f9a447SGerrit Uitslag            if($isDebug) {
208a2c33768SGerrit Uitslag                $html .= $styles;
2091ef68647SAndreas Gohr                $html .= '</style>';
2101ef68647SAndreas Gohr                $html .= '</head><body>';
211a2c33768SGerrit Uitslag            }
212a2c33768SGerrit Uitslag
213a2c33768SGerrit Uitslag            $body_start = $template['html'];
214a2c33768SGerrit Uitslag            $body_start .= '<div class="dokuwiki">';
2152eedf77dSAndreas Gohr
2161e45476bSmnapp            // insert the cover page
217a2c33768SGerrit Uitslag            $body_start .= $template['cover'];
218a2c33768SGerrit Uitslag
219a2c33768SGerrit Uitslag            $mpdf->WriteHTML($body_start, 2, true, false); //start body html
22002f9a447SGerrit Uitslag            if($isDebug) {
221a2c33768SGerrit Uitslag                $html .= $body_start;
222a2c33768SGerrit Uitslag            }
22302f9a447SGerrit Uitslag            if($hasToC) {
22402f9a447SGerrit 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
22502f9a447SGerrit Uitslag                //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
22602f9a447SGerrit Uitslag                //      - there is no page numbering at the pages of the ToC
22702f9a447SGerrit Uitslag                $mpdf->TOCpagebreakByArray(
22802f9a447SGerrit Uitslag                    array(
22902f9a447SGerrit Uitslag                        'toc-preHTML' => '<h2>Table of contents</h2>',
23002f9a447SGerrit Uitslag                        'toc-bookmarkText'=> 'Table of Content',
23102f9a447SGerrit Uitslag                        'links' => true,
23202f9a447SGerrit Uitslag                        'outdent' => '1em',
23302f9a447SGerrit Uitslag                        'resetpagenum' => true, //start pagenumbering after ToC
23402f9a447SGerrit Uitslag                        'pagenumstyle' => '1'
23502f9a447SGerrit Uitslag                    )
23602f9a447SGerrit Uitslag                );
23702f9a447SGerrit Uitslag                $html .= '<tocpagebreak>';
23802f9a447SGerrit Uitslag            }
23902f9a447SGerrit Uitslag
240ed9f9952Smnapp
241c00eb13bSGerrit Uitslag            // store original pageid
242c00eb13bSGerrit Uitslag            $keep = $ID;
243c00eb13bSGerrit Uitslag
2441ef68647SAndreas Gohr            // loop over all pages
245a2c33768SGerrit Uitslag            $cnt = count($this->list);
2461ef68647SAndreas Gohr            for($n = 0; $n < $cnt; $n++) {
247a2c33768SGerrit Uitslag                $page = $this->list[$n];
248ee19bac3SLuigi Micco
249c00eb13bSGerrit Uitslag                // set global pageid to the rendered page
250c00eb13bSGerrit Uitslag                $ID   = $page;
251c00eb13bSGerrit Uitslag
252a2c33768SGerrit Uitslag                $pagehtml = p_cached_output(wikiFN($page, $REV), 'dw2pdf', $page);
253a2c33768SGerrit Uitslag                $pagehtml .= $this->page_depend_replacements($template['cite'], cleanID($page));
2541ef68647SAndreas Gohr                if($n < ($cnt - 1)) {
255a2c33768SGerrit Uitslag                    $pagehtml .= '<pagebreak />';
256a2c33768SGerrit Uitslag                }
257a2c33768SGerrit Uitslag
258a2c33768SGerrit Uitslag                $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
25902f9a447SGerrit Uitslag                if($isDebug) {
260a2c33768SGerrit Uitslag                    $html .= $pagehtml;
2611ef68647SAndreas Gohr                }
262ee19bac3SLuigi Micco            }
263c00eb13bSGerrit Uitslag            //restore ID
264c00eb13bSGerrit Uitslag            $ID = $keep;
265ee19bac3SLuigi Micco
26633c15297SGerrit Uitslag            // insert the back page
267a2c33768SGerrit Uitslag            $body_end = $template['back'];
26833c15297SGerrit Uitslag
269a2c33768SGerrit Uitslag            $body_end .= '</div>';
270a2c33768SGerrit Uitslag
271a2c33768SGerrit Uitslag            $mpdf->WriteHTML($body_end, 2, false, true); // end body html
27202f9a447SGerrit Uitslag            if($isDebug) {
273a2c33768SGerrit Uitslag                $html .= $body_end;
274eeb17e15SAndreas Gohr                $html .= '</body>';
275eeb17e15SAndreas Gohr                $html .= '</html>';
276a2c33768SGerrit Uitslag            }
277f765508eSGerrit Uitslag
278f765508eSGerrit Uitslag            //Return html for debugging
27902f9a447SGerrit Uitslag            if($isDebug) {
28002f9a447SGerrit Uitslag                if($INPUT->str('debughtml', 'text', true) == 'html') {
28126be4eceSGerrit Uitslag                    echo $html;
282a2c33768SGerrit Uitslag                } else {
283a2c33768SGerrit Uitslag                    header('Content-Type: text/plain; charset=utf-8');
284a2c33768SGerrit Uitslag                    echo $html;
285a2c33768SGerrit Uitslag                }
28626be4eceSGerrit Uitslag                exit();
28726be4eceSGerrit Uitslag            };
288f765508eSGerrit Uitslag
28987c86ddaSAndreas Gohr            // write to cache file
29087c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
29187c86ddaSAndreas Gohr        }
29287c86ddaSAndreas Gohr
29387c86ddaSAndreas Gohr        // deliver the file
29487c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
295b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
29687c86ddaSAndreas Gohr        header('Pragma: public');
29787c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
29887c86ddaSAndreas Gohr
2999a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
30087c86ddaSAndreas Gohr        if($this->getConf('output') == 'file') {
3019a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
30287c86ddaSAndreas Gohr        } else {
3039a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
30487c86ddaSAndreas Gohr        }
305ee19bac3SLuigi Micco
306e993da11SGerrit Uitslag        //try to send file, and exit if done
307e993da11SGerrit Uitslag        http_sendfile($cache->cache);
30887c86ddaSAndreas Gohr
30987c86ddaSAndreas Gohr        $fp = @fopen($cache->cache, "rb");
31087c86ddaSAndreas Gohr        if($fp) {
31187c86ddaSAndreas Gohr            http_rangeRequest($fp, filesize($cache->cache), 'application/pdf');
31287c86ddaSAndreas Gohr        } else {
31387c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
31487c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
31587c86ddaSAndreas Gohr        }
3161ef68647SAndreas Gohr        exit();
3171ef68647SAndreas Gohr    }
3181ef68647SAndreas Gohr
3196be736bfSGerrit Uitslag    /**
3206be736bfSGerrit Uitslag     * Add 'export pdf'-button to pagetools
3216be736bfSGerrit Uitslag     *
3226be736bfSGerrit Uitslag     * @param Doku_Event $event
3236be736bfSGerrit Uitslag     * @param mixed      $param not defined
3246be736bfSGerrit Uitslag     */
32502f9a447SGerrit Uitslag    public function addbutton(Doku_Event $event, $param) {
3262c53f619SAndreas Gohr        global $ID, $REV;
3276be736bfSGerrit Uitslag
3286be736bfSGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
3296be736bfSGerrit Uitslag            $params = array('do' => 'export_pdf');
33002f9a447SGerrit Uitslag            if($REV) {
33102f9a447SGerrit Uitslag                $params['rev'] = $REV;
33202f9a447SGerrit Uitslag            }
3336be736bfSGerrit Uitslag
3341e02f86cSAndreas Gohr            // insert button at position before last (up to top)
3351e02f86cSAndreas Gohr            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
3361e02f86cSAndreas Gohr                                    array('export_pdf' =>
3376be736bfSGerrit Uitslag                                          '<li>'
3386be736bfSGerrit Uitslag                                          . '<a href=' . wl($ID, $params) . '  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
3396be736bfSGerrit Uitslag                                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
3406be736bfSGerrit Uitslag                                          . '</a>'
3411e02f86cSAndreas Gohr                                          . '</li>'
3421e02f86cSAndreas Gohr                                    ) +
3431e02f86cSAndreas Gohr                                    array_slice($event->data['items'], -1, 1, true);
3446be736bfSGerrit Uitslag        }
3456be736bfSGerrit Uitslag    }
346d01d2a42SAndreas Gohr
347d01d2a42SAndreas Gohr    /**
3482eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
3491ef68647SAndreas Gohr     */
3501c14c879SAndreas Gohr    protected function load_template($title) {
3511ef68647SAndreas Gohr        global $ID;
3521ef68647SAndreas Gohr        global $conf;
3531ef68647SAndreas Gohr
3542eedf77dSAndreas Gohr        // this is what we'll return
3552eedf77dSAndreas Gohr        $output = array(
3561e45476bSmnapp            'cover' => '',
3572eedf77dSAndreas Gohr            'html'  => '',
3582eedf77dSAndreas Gohr            'page'  => '',
3592eedf77dSAndreas Gohr            'first' => '',
3602eedf77dSAndreas Gohr            'cite'  => '',
3612eedf77dSAndreas Gohr        );
3622eedf77dSAndreas Gohr
3632eedf77dSAndreas Gohr        // prepare header/footer elements
3642eedf77dSAndreas Gohr        $html = '';
36533c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
36633c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
36702f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
36833c15297SGerrit Uitslag                if(file_exists($file)) {
36933c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
37033c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
37133c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
3722eedf77dSAndreas Gohr
3732eedf77dSAndreas Gohr                    // register the needed pseudo CSS
37433c15297SGerrit Uitslag                    if($order == '_first') {
37533c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
37633c15297SGerrit Uitslag                    } elseif($order == '_even') {
37733c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
37833c15297SGerrit Uitslag                    } elseif($order == '_odd') {
37933c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
380daa70883SAndreas Gohr                    } else {
38133c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
3822eedf77dSAndreas Gohr                    }
3832eedf77dSAndreas Gohr                }
3842eedf77dSAndreas Gohr            }
3852eedf77dSAndreas Gohr        }
3862eedf77dSAndreas Gohr
3871ef68647SAndreas Gohr        // prepare replacements
3881ef68647SAndreas Gohr        $replace = array(
3891ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
39002f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
3912eedf77dSAndreas Gohr            '@TITLE@'   => hsc($title),
3921ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
3931ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
3941ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
3955d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
39602f9a447SGerrit Uitslag            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
3971ef68647SAndreas Gohr        );
3981ef68647SAndreas Gohr
3992eedf77dSAndreas Gohr        // set HTML element
400a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
401a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
402a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
4031ef68647SAndreas Gohr
4041e45476bSmnapp        // cover page
40502f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
40633c15297SGerrit Uitslag        if(file_exists($coverfile)) {
40733c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
4081e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
4096e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
4101e45476bSmnapp        }
4111e45476bSmnapp
41233c15297SGerrit Uitslag        // cover page
41302f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
41433c15297SGerrit Uitslag        if(file_exists($backfile)) {
41533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
41633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
41733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
41833c15297SGerrit Uitslag        }
41933c15297SGerrit Uitslag
4202eedf77dSAndreas Gohr        // citation box
42102f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
42233c15297SGerrit Uitslag        if(file_exists($citationfile)) {
42333c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
4242eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
4252eedf77dSAndreas Gohr        }
4261ef68647SAndreas Gohr
4272eedf77dSAndreas Gohr        return $output;
4281ef68647SAndreas Gohr    }
4291ef68647SAndreas Gohr
4301ef68647SAndreas Gohr    /**
431a180c973SKlap-in     * @param string $raw code with placeholders
432a180c973SKlap-in     * @param string $id  pageid
433a180c973SKlap-in     * @return string
434a180c973SKlap-in     */
435a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
436a180c973SKlap-in        global $REV;
437a180c973SKlap-in
438a180c973SKlap-in        // generate qr code for this page using google infographics api
439a180c973SKlap-in        $qr_code = '';
440a180c973SKlap-in        if($this->getConf('qrcodesize')) {
441a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
442a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' .
443a180c973SKlap-in                $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />';
444a180c973SKlap-in        }
445a180c973SKlap-in        // prepare replacements
446a180c973SKlap-in        $replace['@ID@']      = $id;
447a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
448a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&");
449a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
450a180c973SKlap-in
451a180c973SKlap-in        return str_replace(array_keys($replace), array_values($replace), $raw);
452a180c973SKlap-in    }
453a180c973SKlap-in
454a180c973SKlap-in    /**
4551c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
4561ef68647SAndreas Gohr     */
4571c14c879SAndreas Gohr    protected function load_css() {
458737417c6SKlap-in        global $conf;
4591c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
4601c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
4611c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
462ee19bac3SLuigi Micco
4631c14c879SAndreas Gohr        // prepare CSS files
4641c14c879SAndreas Gohr        $files = array_merge(
4651c14c879SAndreas Gohr            array(
4661c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
4671c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
4681c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
4691c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
4701c14c879SAndreas Gohr            ),
4711c14c879SAndreas Gohr            css_pluginstyles('all'),
47258e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
4731c14c879SAndreas Gohr            array(
4741c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
4751c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4761c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
4771c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
4781c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
4791c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
4801c14c879SAndreas Gohr            )
4811c14c879SAndreas Gohr        );
4821c14c879SAndreas Gohr        $css = '';
4831c14c879SAndreas Gohr        foreach($files as $file => $location) {
48428e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
48528e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
4861c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
4871ef68647SAndreas Gohr        }
4881ef68647SAndreas Gohr
48928e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
4901c14c879SAndreas Gohr            // apply pattern replacements
49128e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
49228e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
49328e636eaSGerrit Uitslag
49428e636eaSGerrit Uitslag            // parse less
49528e636eaSGerrit Uitslag            $css = css_parseless($css);
49628e636eaSGerrit Uitslag        } else {
49728e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
4981c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
49928e636eaSGerrit Uitslag        }
5001ef68647SAndreas Gohr
5011c14c879SAndreas Gohr        return $css;
502ee19bac3SLuigi Micco    }
5031c14c879SAndreas Gohr
50458e6409eSAndreas Gohr    /**
50558e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
50658e6409eSAndreas Gohr     *
50758e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
50858e6409eSAndreas Gohr     *
50958e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
51058e6409eSAndreas Gohr     */
5116be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
51258e6409eSAndreas Gohr        $list = array();
51358e6409eSAndreas Gohr        $plugins = plugin_list();
514f54b51f7SAndreas Gohr
515f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
51658e6409eSAndreas Gohr        foreach($plugins as $p) {
517f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
518f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
519f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
520f54b51f7SAndreas Gohr            }
521f54b51f7SAndreas Gohr
52258e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) {
52358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
52458e6409eSAndreas Gohr            } else {
52558e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
52658e6409eSAndreas Gohr            }
52758e6409eSAndreas Gohr        }
52858e6409eSAndreas Gohr        return $list;
52958e6409eSAndreas Gohr    }
530ad18f4e1SGerrit Uitslag
531ad18f4e1SGerrit Uitslag    /**
53260e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
53360e59de7SGerrit Uitslag     *
53460e59de7SGerrit Uitslag     * @return array
53560e59de7SGerrit Uitslag     */
53660e59de7SGerrit Uitslag    public function getExportedPages() {
53760e59de7SGerrit Uitslag        return $this->list;
53860e59de7SGerrit Uitslag    }
53960e59de7SGerrit Uitslag
54060e59de7SGerrit Uitslag    /**
541ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
542ad18f4e1SGerrit Uitslag     */
543ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
544ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
545ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
546ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
547ad18f4e1SGerrit Uitslag    }
548ad18f4e1SGerrit Uitslag
549ad18f4e1SGerrit Uitslag    /**
550ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
551ad18f4e1SGerrit Uitslag     */
552ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
553ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
554ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
555ad18f4e1SGerrit Uitslag        return 0;
556ad18f4e1SGerrit Uitslag    }
55726be4eceSGerrit Uitslag
55826be4eceSGerrit Uitslag    /**
55926be4eceSGerrit Uitslag     * Set error notification and reload page again
56026be4eceSGerrit Uitslag     *
56126be4eceSGerrit Uitslag     * @param Doku_Event $event
56226be4eceSGerrit Uitslag     * @param string     $msglangkey key of translation key
56326be4eceSGerrit Uitslag     */
56426be4eceSGerrit Uitslag    private function showPageWithErrorMsg(&$event, $msglangkey) {
56526be4eceSGerrit Uitslag        msg($this->getLang($msglangkey), -1);
56626be4eceSGerrit Uitslag
56726be4eceSGerrit Uitslag        $event->data = 'show';
56826be4eceSGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
56926be4eceSGerrit Uitslag    }
57002f9a447SGerrit Uitslag
57102f9a447SGerrit Uitslag    /**
57202f9a447SGerrit Uitslag     * Return settings read from:
57302f9a447SGerrit Uitslag     *   1. url parameters
57402f9a447SGerrit Uitslag     *   2. plugin config
57502f9a447SGerrit Uitslag     *   3. global config
57602f9a447SGerrit Uitslag     *
57702f9a447SGerrit Uitslag     * @return array
57802f9a447SGerrit Uitslag     */
57902f9a447SGerrit Uitslag    protected function loadExportConfig() {
58002f9a447SGerrit Uitslag        global $INPUT;
58102f9a447SGerrit Uitslag        global $conf;
58202f9a447SGerrit Uitslag
58302f9a447SGerrit Uitslag        $this->exportConfig = array();
58402f9a447SGerrit Uitslag
58502f9a447SGerrit Uitslag        // decide on the paper setup from param or config
58602f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
58702f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
58802f9a447SGerrit Uitslag
589*213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
590*213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
591*213fdb75SGerrit Uitslag
592*213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
59302f9a447SGerrit Uitslag        $levels = array();
59402f9a447SGerrit Uitslag        if($hasToC) {
59502f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
59602f9a447SGerrit Uitslag            list($top_input, $max_input) = explode('-', $toclevels, 2);
59702f9a447SGerrit Uitslag            list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2);
59802f9a447SGerrit Uitslag            $bounds_input = array(
59902f9a447SGerrit Uitslag                'top' => array(
60002f9a447SGerrit Uitslag                    (int) $top_input,
60102f9a447SGerrit Uitslag                    (int) $top_conf
60202f9a447SGerrit Uitslag                ),
60302f9a447SGerrit Uitslag                'max' => array(
60402f9a447SGerrit Uitslag                    (int) $max_input,
60502f9a447SGerrit Uitslag                    (int) $max_conf
60602f9a447SGerrit Uitslag                )
60702f9a447SGerrit Uitslag            );
60802f9a447SGerrit Uitslag            $bounds = array(
60902f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
61002f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
61102f9a447SGerrit Uitslag
61202f9a447SGerrit Uitslag            );
61302f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
61402f9a447SGerrit Uitslag                foreach($values as $value) {
61502f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
61602f9a447SGerrit Uitslag                        //stop at valid value and store
61702f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
61802f9a447SGerrit Uitslag                        break;
61902f9a447SGerrit Uitslag                    }
62002f9a447SGerrit Uitslag                }
62102f9a447SGerrit Uitslag            }
62202f9a447SGerrit Uitslag
62302f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
62402f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
62502f9a447SGerrit Uitslag            }
62602f9a447SGerrit Uitslag
62702f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
62802f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
62902f9a447SGerrit Uitslag            }
63002f9a447SGerrit Uitslag        }
63102f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
63202f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
63302f9a447SGerrit Uitslag
63402f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
63502f9a447SGerrit Uitslag
63602f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
63702f9a447SGerrit Uitslag        $tpl = $INPUT->str('template', $tplconf, true);
63802f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
63902f9a447SGerrit Uitslag            $tpl = $tplconf;
64002f9a447SGerrit Uitslag        }
64102f9a447SGerrit Uitslag        if(!$tpl){
64202f9a447SGerrit Uitslag            $tpl = 'default';
64302f9a447SGerrit Uitslag        }
64402f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
64502f9a447SGerrit Uitslag
64602f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
64702f9a447SGerrit Uitslag    }
64802f9a447SGerrit Uitslag
64902f9a447SGerrit Uitslag    /**
65002f9a447SGerrit Uitslag     * Returns requested config
65102f9a447SGerrit Uitslag     *
65202f9a447SGerrit Uitslag     * @param string $name
65302f9a447SGerrit Uitslag     * @param mixed  $notset
65402f9a447SGerrit Uitslag     * @return mixed|bool
65502f9a447SGerrit Uitslag     */
65602f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
65702f9a447SGerrit Uitslag        if ($this->exportConfig === null){
65802f9a447SGerrit Uitslag            $this->loadExportConfig();
65902f9a447SGerrit Uitslag        }
66002f9a447SGerrit Uitslag
66102f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
66202f9a447SGerrit Uitslag            return $this->exportConfig[$name];
66302f9a447SGerrit Uitslag        }else{
66402f9a447SGerrit Uitslag            return $notset;
66502f9a447SGerrit Uitslag        }
66602f9a447SGerrit Uitslag    }
667ee19bac3SLuigi Micco}
668