xref: /plugin/dw2pdf/action.php (revision 6be736bf1febe92502fa79f98c4f8f983a97fbc8)
1<?php
2 /**
3  * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Luigi Micco <l.micco@tiscali.it>
7  * @author     Andreas Gohr <andi@splitbrain.org>
8  */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
14
15    private $tpl;
16
17    /**
18     * Constructor. Sets the correct template
19     */
20    public function __construct(){
21        $tpl = false;
22        if(isset($_REQUEST['tpl'])){
23            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl']));
24        }
25        if(!$tpl) $tpl = $this->getConf('template');
26        if(!$tpl) $tpl = 'default';
27        if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default';
28
29        $this->tpl = $tpl;
30    }
31
32    /**
33     * Register the events
34     */
35    public function register(Doku_Event_Handler $controller) {
36        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
37        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
38    }
39
40    /**
41     * Do the HTML to PDF conversion work
42     *
43     * @param Doku_Event $event
44     * @param array      $param
45     * @return bool
46     */
47    public function convert(&$event, $param) {
48        global $ACT;
49        global $REV;
50        global $ID;
51
52        // our event?
53        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
54
55        // check user's rights
56        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
57
58        // one or multiple pages?
59        $list  = array();
60
61        if($ACT == 'export_pdf') {
62            $list[0] = $ID;
63            $title = p_get_first_heading($ID);
64        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
65            //is in Bookmanager of bookcreator plugin title given
66            if(!$title = $_GET['pdfbook_title']) {  //TODO when title is changed, the cached file contains the old title
67                /** @var $bookcreator action_plugin_bookcreator */
68                $bookcreator = plugin_load('action', 'bookcreator');
69                msg($bookcreator->getLang('needtitle'), -1);
70
71                $event->data               = 'show';
72                $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
73                return false;
74            }
75            $list = explode("|", $_COOKIE['list-pagelist']);
76        } else {
77            /** @var $bookcreator action_plugin_bookcreator */
78            $bookcreator = plugin_load('action', 'bookcreator');
79            msg($bookcreator->getLang('empty'), -1);
80
81            $event->data               = 'show';
82            $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
83            return false;
84        }
85
86        // it's ours, no one else's
87        $event->preventDefault();
88
89        // prepare cache
90        $cache = new cache(join(',',$list).$REV.$this->tpl,'.dw2.pdf');
91        $depends['files']   = array_map('wikiFN',$list);
92        $depends['files'][] = __FILE__;
93        $depends['files'][] = dirname(__FILE__).'/renderer.php';
94        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
95        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
96
97        // hard work only when no cache available
98        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
99            // initialize PDF library
100            require_once(dirname(__FILE__)."/DokuPDF.class.php");
101            $mpdf = new DokuPDF();
102
103            // let mpdf fix local links
104            $self = parse_url(DOKU_URL);
105            $url  = $self['scheme'].'://'.$self['host'];
106            if($self['port']) $url .= ':'.$self['port'];
107            $mpdf->setBasePath($url);
108
109            // Set the title
110            $mpdf->SetTitle($title);
111
112            // some default settings
113            $mpdf->mirrorMargins = 1;
114            $mpdf->useOddEven    = 1;
115            $mpdf->setAutoTopMargin = 'stretch';
116            $mpdf->setAutoBottomMargin = 'stretch';
117
118            // load the template
119            $template = $this->load_template($title);
120
121            // prepare HTML header styles
122            $html  = '<html><head>';
123            $html .= '<style type="text/css">';
124            $html .= $this->load_css();
125            $html .= '@page { size:auto; '.$template['page'].'}';
126            $html .= '@page :first {'.$template['first'].'}';
127            $html .= '</style>';
128            $html .= '</head><body>';
129            $html .= $template['html'];
130            $html .= '<div class="dokuwiki">';
131
132            // loop over all pages
133            $cnt = count($list);
134            for($n=0; $n<$cnt; $n++){
135                $page = $list[$n];
136
137                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
138                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
139                if ($n < ($cnt - 1)){
140                    $html .= '<pagebreak />';
141                }
142            }
143
144            $html .= '</div>';
145            $mpdf->WriteHTML($html);
146
147            // write to cache file
148            $mpdf->Output($cache->cache, 'F');
149        }
150
151        // deliver the file
152        header('Content-Type: application/pdf');
153        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
154        header('Pragma: public');
155        http_conditionalRequest(filemtime($cache->cache));
156
157        $filename = rawurlencode(cleanID(strtr($title, ':/;"','    ')));
158        if($this->getConf('output') == 'file'){
159            header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
160        }else{
161            header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
162        }
163
164        if (http_sendfile($cache->cache)) exit;
165
166        $fp = @fopen($cache->cache,"rb");
167        if($fp){
168            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
169        }else{
170            header("HTTP/1.0 500 Internal Server Error");
171            print "Could not read file - bad permissions?";
172        }
173        exit();
174    }
175
176    /**
177     * Add 'export pdf'-button to pagetools
178     *
179     * @param Doku_Event $event
180     * @param mixed      $param not defined
181     */
182    public function addbutton(&$event, $param) {
183        global $ID, $REV, $conf;
184
185        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
186            $params = array('do' => 'export_pdf');
187            if($REV) $params['rev'] = $REV;
188
189            switch($conf['template']) {
190                case 'dokuwiki':
191                    $event->data['items']['export_pdf'] =
192                        '<li>'
193                        .'<a href='.wl($ID, $params).'  class="action export_pdf" rel="nofollow" title="'.$this->getLang('export_pdf_button').'">'
194                        .'<span>'.$this->getLang('export_pdf_button').'</span>'
195                        .'</a>'
196                        .'</li>';
197                    break;
198            }
199        }
200    }
201
202    /**
203     * Load the various template files and prepare the HTML/CSS for insertion
204     */
205    protected function load_template($title){
206        global $ID;
207        global $conf;
208        $tpl = $this->tpl;
209
210        // this is what we'll return
211        $output = array(
212            'html'  => '',
213            'page'  => '',
214            'first' => '',
215            'cite'  => '',
216        );
217
218        // prepare header/footer elements
219        $html = '';
220        foreach(array('header','footer') as $t){
221            foreach(array('','_odd','_even','_first') as $h){
222                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
223                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
224                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
225                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
226
227                    // register the needed pseudo CSS
228                    if($h == '_first'){
229                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
230                    }elseif($h == '_even'){
231                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
232                    }elseif($h == '_odd'){
233                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
234                    }else{
235                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
236                    }
237                }
238            }
239        }
240
241        // prepare replacements
242        $replace = array(
243                '@PAGE@'    => '{PAGENO}',
244                '@PAGES@'   => '{nb}',
245                '@TITLE@'   => hsc($title),
246                '@WIKI@'    => $conf['title'],
247                '@WIKIURL@' => DOKU_URL,
248                '@DATE@'    => dformat(time()),
249                '@BASE@'    => DOKU_BASE,
250                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/'
251        );
252
253        // set HTML element
254        $html = str_replace(array_keys($replace), array_values($replace), $html);
255        //TODO For bookcreator $ID (= bookmanager page) makes no sense
256        $output['html'] = $this->page_depend_replacements($html, $ID);
257
258        // citation box
259        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
260            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
261            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
262        }
263
264        return $output;
265    }
266
267    /**
268     * @param string $raw code with placeholders
269     * @param string $id  pageid
270     * @return string
271     */
272    protected function page_depend_replacements($raw, $id){
273        global $REV;
274
275        // generate qr code for this page using google infographics api
276        $qr_code = '';
277        if ($this->getConf('qrcodesize')) {
278            $url = urlencode(wl($id,'','&',true));
279            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
280                $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
281        }
282        // prepare replacements
283        $replace['@ID@']      = $id;
284        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
285        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev'=> $REV) : false, true, "&");
286        $replace['@QRCODE@']  = $qr_code;
287
288        return str_replace(array_keys($replace), array_values($replace), $raw);
289    }
290
291    /**
292     * Load all the style sheets and apply the needed replacements
293     */
294    protected function load_css(){
295        global $conf;
296        //reusue the CSS dispatcher functions without triggering the main function
297        define('SIMPLE_TEST',1);
298        require_once(DOKU_INC.'lib/exe/css.php');
299
300        // prepare CSS files
301        $files = array_merge(
302                    array(
303                        DOKU_INC.'lib/styles/screen.css'
304                            => DOKU_BASE.'lib/styles/',
305                        DOKU_INC.'lib/styles/print.css'
306                            => DOKU_BASE.'lib/styles/',
307                    ),
308                    css_pluginstyles('all'),
309                    $this->css_pluginPDFstyles(),
310                    array(
311                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
312                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
313                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
314                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
315                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
316                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
317                    )
318                 );
319        $css = '';
320        foreach($files as $file => $location){
321            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
322            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
323            $css .= css_loadfile($file, $location);
324        }
325
326        if(function_exists('css_parseless')) {
327            // apply pattern replacements
328            $styleini = css_styleini($conf['template']);
329            $css = css_applystyle($css, $styleini['replacements']);
330
331            // parse less
332            $css = css_parseless($css);
333        } else {
334            // @deprecated 2013-12-19: fix backward compatibility
335            $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
336        }
337
338        return $css;
339    }
340
341    /**
342     * Returns a list of possible Plugin PDF Styles
343     *
344     * Checks for a pdf.css, falls back to print.css
345     *
346     * @author Andreas Gohr <andi@splitbrain.org>
347     */
348    protected function css_pluginPDFstyles(){
349        $list = array();
350        $plugins = plugin_list();
351
352        $usestyle = explode(',',$this->getConf('usestyles'));
353        foreach ($plugins as $p){
354            if(in_array($p,$usestyle)){
355                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
356                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
357            }
358
359            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
360                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
361            }else{
362                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
363            }
364        }
365        return $list;
366    }
367}