xref: /plugin/dw2pdf/action.php (revision 3cc6f95e78d5745f3b2321091cb86d93b2af4022)
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                case 'arago':
192                    $event->data['items']['export_pdf'] =
193                        '<li>'
194                        .'<a href='.wl($ID, $params).'  class="action export_pdf" rel="nofollow" title="'.$this->getLang('export_pdf_button').'">'
195                        .'<span>'.$this->getLang('export_pdf_button').'</span>'
196                        .'</a>'
197                        .'</li>';
198                    break;
199            }
200        }
201    }
202
203    /**
204     * Load the various template files and prepare the HTML/CSS for insertion
205     */
206    protected function load_template($title){
207        global $ID;
208        global $conf;
209        $tpl = $this->tpl;
210
211        // this is what we'll return
212        $output = array(
213            'html'  => '',
214            'page'  => '',
215            'first' => '',
216            'cite'  => '',
217        );
218
219        // prepare header/footer elements
220        $html = '';
221        foreach(array('header','footer') as $t){
222            foreach(array('','_odd','_even','_first') as $h){
223                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
224                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
225                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
226                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
227
228                    // register the needed pseudo CSS
229                    if($h == '_first'){
230                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
231                    }elseif($h == '_even'){
232                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
233                    }elseif($h == '_odd'){
234                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
235                    }else{
236                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
237                    }
238                }
239            }
240        }
241
242        // prepare replacements
243        $replace = array(
244                '@PAGE@'    => '{PAGENO}',
245                '@PAGES@'   => '{nb}',
246                '@TITLE@'   => hsc($title),
247                '@WIKI@'    => $conf['title'],
248                '@WIKIURL@' => DOKU_URL,
249                '@DATE@'    => dformat(time()),
250                '@BASE@'    => DOKU_BASE,
251                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/'
252        );
253
254        // set HTML element
255        $html = str_replace(array_keys($replace), array_values($replace), $html);
256        //TODO For bookcreator $ID (= bookmanager page) makes no sense
257        $output['html'] = $this->page_depend_replacements($html, $ID);
258
259        // citation box
260        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
261            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
262            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
263        }
264
265        return $output;
266    }
267
268    /**
269     * @param string $raw code with placeholders
270     * @param string $id  pageid
271     * @return string
272     */
273    protected function page_depend_replacements($raw, $id){
274        global $REV;
275
276        // generate qr code for this page using google infographics api
277        $qr_code = '';
278        if ($this->getConf('qrcodesize')) {
279            $url = urlencode(wl($id,'','&',true));
280            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
281                $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
282        }
283        // prepare replacements
284        $replace['@ID@']      = $id;
285        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
286        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev'=> $REV) : false, true, "&");
287        $replace['@QRCODE@']  = $qr_code;
288
289        return str_replace(array_keys($replace), array_values($replace), $raw);
290    }
291
292    /**
293     * Load all the style sheets and apply the needed replacements
294     */
295    protected function load_css(){
296        global $conf;
297        //reusue the CSS dispatcher functions without triggering the main function
298        define('SIMPLE_TEST',1);
299        require_once(DOKU_INC.'lib/exe/css.php');
300
301        // prepare CSS files
302        $files = array_merge(
303                    array(
304                        DOKU_INC.'lib/styles/screen.css'
305                            => DOKU_BASE.'lib/styles/',
306                        DOKU_INC.'lib/styles/print.css'
307                            => DOKU_BASE.'lib/styles/',
308                    ),
309                    css_pluginstyles('all'),
310                    $this->css_pluginPDFstyles(),
311                    array(
312                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
313                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
314                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
315                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
316                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
317                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
318                    )
319                 );
320        $css = '';
321        foreach($files as $file => $location){
322            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
323            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
324            $css .= css_loadfile($file, $location);
325        }
326
327        if(function_exists('css_parseless')) {
328            // apply pattern replacements
329            $styleini = css_styleini($conf['template']);
330            $css = css_applystyle($css, $styleini['replacements']);
331
332            // parse less
333            $css = css_parseless($css);
334        } else {
335            // @deprecated 2013-12-19: fix backward compatibility
336            $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
337        }
338
339        return $css;
340    }
341
342    /**
343     * Returns a list of possible Plugin PDF Styles
344     *
345     * Checks for a pdf.css, falls back to print.css
346     *
347     * @author Andreas Gohr <andi@splitbrain.org>
348     */
349    protected function css_pluginPDFstyles(){
350        $list = array();
351        $plugins = plugin_list();
352
353        $usestyle = explode(',',$this->getConf('usestyles'));
354        foreach ($plugins as $p){
355            if(in_array($p,$usestyle)){
356                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
357                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
358            }
359
360            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
361                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
362            }else{
363                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
364            }
365        }
366        return $list;
367    }
368}