xref: /plugin/dw2pdf/action.php (revision ad18f4e1047f87e7d65311de94c571d10022bf2e)
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
131ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
14ee19bac3SLuigi Micco
151c14c879SAndreas Gohr    private $tpl;
161c14c879SAndreas Gohr
171c14c879SAndreas Gohr    /**
181c14c879SAndreas Gohr     * Constructor. Sets the correct template
191c14c879SAndreas Gohr     */
206be736bfSGerrit Uitslag    public function __construct(){
21737417c6SKlap-in        $tpl = false;
221c14c879SAndreas Gohr        if(isset($_REQUEST['tpl'])){
231c14c879SAndreas Gohr            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl']));
241c14c879SAndreas Gohr        }
251c14c879SAndreas Gohr        if(!$tpl) $tpl = $this->getConf('template');
261c14c879SAndreas Gohr        if(!$tpl) $tpl = 'default';
271c14c879SAndreas Gohr        if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default';
281c14c879SAndreas Gohr
291c14c879SAndreas Gohr        $this->tpl = $tpl;
301c14c879SAndreas Gohr    }
311c14c879SAndreas Gohr
32ee19bac3SLuigi Micco    /**
33ee19bac3SLuigi Micco     * Register the events
34ee19bac3SLuigi Micco     */
356be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
36ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
376be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
38ee19bac3SLuigi Micco    }
39ee19bac3SLuigi Micco
401c14c879SAndreas Gohr    /**
411c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
42737417c6SKlap-in     *
43737417c6SKlap-in     * @param Doku_Event $event
44737417c6SKlap-in     * @param array      $param
45737417c6SKlap-in     * @return bool
461c14c879SAndreas Gohr     */
476be736bfSGerrit Uitslag    public function convert(&$event, $param) {
48ee19bac3SLuigi Micco        global $ACT;
49ee19bac3SLuigi Micco        global $REV;
50ee19bac3SLuigi Micco        global $ID;
51*ad18f4e1SGerrit Uitslag        global $INPUT, $conf;
52ee19bac3SLuigi Micco
531ef68647SAndreas Gohr        // our event?
54*ad18f4e1SGerrit Uitslag        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' ) && ( $ACT != 'export_pdfns' )) return false;
55ee19bac3SLuigi Micco
561ef68647SAndreas Gohr        // check user's rights
571ef68647SAndreas Gohr        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
581ef68647SAndreas Gohr
5987c86ddaSAndreas Gohr        // one or multiple pages?
6087c86ddaSAndreas Gohr        $list  = array();
6128e636eaSGerrit Uitslag
6287c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
6387c86ddaSAndreas Gohr            $list[0] = $ID;
64737417c6SKlap-in            $title = p_get_first_heading($ID);
65*ad18f4e1SGerrit Uitslag
66*ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
67*ad18f4e1SGerrit Uitslag            //check input for title and ns
68*ad18f4e1SGerrit Uitslag            if(!$title = $INPUT->str('pdfns_title', '')) {
69*ad18f4e1SGerrit Uitslag                $bookcreator = plugin_load('action', 'bookcreator');
70*ad18f4e1SGerrit Uitslag                msg($bookcreator->getLang('needtitle'), -1);
71*ad18f4e1SGerrit Uitslag                $event->data               = 'show';
72*ad18f4e1SGerrit Uitslag                $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
73*ad18f4e1SGerrit Uitslag                return false;
74*ad18f4e1SGerrit Uitslag            }
75*ad18f4e1SGerrit Uitslag
76*ad18f4e1SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('pdfns_ns', ''));
77*ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
78*ad18f4e1SGerrit Uitslag                $bookcreator = plugin_load('action', 'bookcreator');
79*ad18f4e1SGerrit Uitslag                msg($bookcreator->getLang('needtitle').' missing ns', -1);
80*ad18f4e1SGerrit Uitslag                $event->data               = 'show';
81*ad18f4e1SGerrit Uitslag                $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
82*ad18f4e1SGerrit Uitslag                return false;
83*ad18f4e1SGerrit Uitslag            }
84*ad18f4e1SGerrit Uitslag
85*ad18f4e1SGerrit Uitslag            $order = $INPUT->str('pdfns_order', 'natural', true);
86*ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
87*ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
88*ad18f4e1SGerrit Uitslag                $order = 'natural';
89*ad18f4e1SGerrit Uitslag            }
90*ad18f4e1SGerrit Uitslag
91*ad18f4e1SGerrit Uitslag            $depth = $INPUT->int('pdfns_depth', 0);
92*ad18f4e1SGerrit Uitslag            if($depth < 0) {
93*ad18f4e1SGerrit Uitslag                $depth = 0;
94*ad18f4e1SGerrit Uitslag            }
95*ad18f4e1SGerrit Uitslag            //page search
96*ad18f4e1SGerrit Uitslag            $result = array();
97*ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
98*ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
99*ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
100*ad18f4e1SGerrit Uitslag
101*ad18f4e1SGerrit Uitslag            //sort
102*ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
103*ad18f4e1SGerrit Uitslag                if($order == 'date') {
104*ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
105*ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
106*ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
107*ad18f4e1SGerrit Uitslag                }
108*ad18f4e1SGerrit Uitslag            }
109*ad18f4e1SGerrit Uitslag
110*ad18f4e1SGerrit Uitslag            foreach($result as $item) {
111*ad18f4e1SGerrit Uitslag                $list[] = $item['id'];
112*ad18f4e1SGerrit Uitslag            }
113*ad18f4e1SGerrit Uitslag
114737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
115737417c6SKlap-in            //is in Bookmanager of bookcreator plugin title given
116737417c6SKlap-in            if(!$title = $_GET['pdfbook_title']) {  //TODO when title is changed, the cached file contains the old title
117737417c6SKlap-in                /** @var $bookcreator action_plugin_bookcreator */
11828e636eaSGerrit Uitslag                $bookcreator = plugin_load('action', 'bookcreator');
119737417c6SKlap-in                msg($bookcreator->getLang('needtitle'), -1);
120737417c6SKlap-in
121737417c6SKlap-in                $event->data               = 'show';
122737417c6SKlap-in                $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
123737417c6SKlap-in                return false;
12487c86ddaSAndreas Gohr            }
125737417c6SKlap-in            $list = explode("|", $_COOKIE['list-pagelist']);
126*ad18f4e1SGerrit Uitslag
127737417c6SKlap-in        } else {
128737417c6SKlap-in            /** @var $bookcreator action_plugin_bookcreator */
12928e636eaSGerrit Uitslag            $bookcreator = plugin_load('action', 'bookcreator');
130737417c6SKlap-in            msg($bookcreator->getLang('empty'), -1);
131737417c6SKlap-in
132737417c6SKlap-in            $event->data               = 'show';
133737417c6SKlap-in            $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
134737417c6SKlap-in            return false;
135737417c6SKlap-in        }
136737417c6SKlap-in
137737417c6SKlap-in        // it's ours, no one else's
138737417c6SKlap-in        $event->preventDefault();
13987c86ddaSAndreas Gohr
1406ea88a05SAndreas Gohr        // decide on the paper setup from param or config
1416ea88a05SAndreas Gohr        $pagesize    = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
1426ea88a05SAndreas Gohr        $orientation = $INPUT->str('orientation', $this->getConf('orientation'), true);
1436ea88a05SAndreas Gohr
14487c86ddaSAndreas Gohr        // prepare cache
1456ea88a05SAndreas Gohr        $cache = new cache(join(',',$list).$REV.$this->tpl.$pagesize.$orientation,'.dw2.pdf');
14687c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN',$list);
14787c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
14887c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/renderer.php';
14987c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
150df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
15187c86ddaSAndreas Gohr
152d01d2a42SAndreas Gohr        // hard work only when no cache available
15387c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
1541ef68647SAndreas Gohr            // initialize PDF library
155cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__)."/DokuPDF.class.php");
1566ea88a05SAndreas Gohr
1576ea88a05SAndreas Gohr            $mpdf = new DokuPDF($pagesize, $orientation);
158ee19bac3SLuigi Micco
159d62df65bSAndreas Gohr            // let mpdf fix local links
160d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
161d62df65bSAndreas Gohr            $url  = $self['scheme'].'://'.$self['host'];
162737417c6SKlap-in            if($self['port']) $url .= ':'.$self['port'];
163d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
164d62df65bSAndreas Gohr
16556d13144SAndreas Gohr            // Set the title
16656d13144SAndreas Gohr            $mpdf->SetTitle($title);
16756d13144SAndreas Gohr
1681ef68647SAndreas Gohr            // some default settings
169daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
170daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
171daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
172daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
1732eedf77dSAndreas Gohr
17456d13144SAndreas Gohr            // load the template
1751c14c879SAndreas Gohr            $template = $this->load_template($title);
176ee19bac3SLuigi Micco
1771ef68647SAndreas Gohr            // prepare HTML header styles
178ee19bac3SLuigi Micco            $html  = '<html><head>';
179737417c6SKlap-in            $html .= '<style type="text/css">';
1801c14c879SAndreas Gohr            $html .= $this->load_css();
181daa70883SAndreas Gohr            $html .= '@page { size:auto; '.$template['page'].'}';
1822eedf77dSAndreas Gohr            $html .= '@page :first {'.$template['first'].'}';
1831ef68647SAndreas Gohr            $html .= '</style>';
1841ef68647SAndreas Gohr            $html .= '</head><body>';
1852eedf77dSAndreas Gohr            $html .= $template['html'];
1861c14c879SAndreas Gohr            $html .= '<div class="dokuwiki">';
1872eedf77dSAndreas Gohr
1881ef68647SAndreas Gohr            // loop over all pages
1891ef68647SAndreas Gohr            $cnt = count($list);
1901ef68647SAndreas Gohr            for($n=0; $n<$cnt; $n++){
191ee19bac3SLuigi Micco                $page = $list[$n];
192ee19bac3SLuigi Micco
193a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
194a180c973SKlap-in                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
1951ef68647SAndreas Gohr                if ($n < ($cnt - 1)){
1961ef68647SAndreas Gohr                    $html .= '<pagebreak />';
1971ef68647SAndreas Gohr                }
198ee19bac3SLuigi Micco            }
199ee19bac3SLuigi Micco
2001c14c879SAndreas Gohr            $html .= '</div>';
201f765508eSGerrit Uitslag
202f765508eSGerrit Uitslag            //Return html for debugging
203f765508eSGerrit Uitslag            if($_GET['debughtml'] == 'html') die($html);
204f765508eSGerrit Uitslag
205ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
206a06728a6SAndreas Gohr
20787c86ddaSAndreas Gohr            // write to cache file
20887c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
20987c86ddaSAndreas Gohr        }
21087c86ddaSAndreas Gohr
21187c86ddaSAndreas Gohr        // deliver the file
21287c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
213b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
21487c86ddaSAndreas Gohr        header('Pragma: public');
21587c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
21687c86ddaSAndreas Gohr
2179a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"','    ')));
21887c86ddaSAndreas Gohr        if($this->getConf('output') == 'file'){
2199a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
22087c86ddaSAndreas Gohr        }else{
2219a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
22287c86ddaSAndreas Gohr        }
223ee19bac3SLuigi Micco
224e993da11SGerrit Uitslag        //try to send file, and exit if done
225e993da11SGerrit Uitslag        http_sendfile($cache->cache);
22687c86ddaSAndreas Gohr
22787c86ddaSAndreas Gohr        $fp = @fopen($cache->cache,"rb");
22887c86ddaSAndreas Gohr        if($fp){
22987c86ddaSAndreas Gohr            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
23087c86ddaSAndreas Gohr        }else{
23187c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
23287c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
23387c86ddaSAndreas Gohr        }
2341ef68647SAndreas Gohr        exit();
2351ef68647SAndreas Gohr    }
2361ef68647SAndreas Gohr
2376be736bfSGerrit Uitslag    /**
2386be736bfSGerrit Uitslag     * Add 'export pdf'-button to pagetools
2396be736bfSGerrit Uitslag     *
2406be736bfSGerrit Uitslag     * @param Doku_Event $event
2416be736bfSGerrit Uitslag     * @param mixed      $param not defined
2426be736bfSGerrit Uitslag     */
2436be736bfSGerrit Uitslag    public function addbutton(&$event, $param) {
2446be736bfSGerrit Uitslag        global $ID, $REV, $conf;
2456be736bfSGerrit Uitslag
2466be736bfSGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
2476be736bfSGerrit Uitslag            $params = array('do' => 'export_pdf');
2486be736bfSGerrit Uitslag            if($REV) $params['rev'] = $REV;
2496be736bfSGerrit Uitslag
2506be736bfSGerrit Uitslag            switch($conf['template']) {
2516be736bfSGerrit Uitslag                case 'dokuwiki':
2523cc6f95eSGerrit Uitslag                case 'arago':
253fc56eef8SGerrit Uitslag                case 'wikilu': // a private template
2546be736bfSGerrit Uitslag                    $event->data['items']['export_pdf'] =
2556be736bfSGerrit Uitslag                        '<li>'
2566be736bfSGerrit Uitslag                        .'<a href='.wl($ID, $params).'  class="action export_pdf" rel="nofollow" title="'.$this->getLang('export_pdf_button').'">'
2576be736bfSGerrit Uitslag                        .'<span>'.$this->getLang('export_pdf_button').'</span>'
2586be736bfSGerrit Uitslag                        .'</a>'
2596be736bfSGerrit Uitslag                        .'</li>';
2606be736bfSGerrit Uitslag                    break;
2616be736bfSGerrit Uitslag            }
2626be736bfSGerrit Uitslag        }
2636be736bfSGerrit Uitslag    }
264d01d2a42SAndreas Gohr
265d01d2a42SAndreas Gohr    /**
2662eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
2671ef68647SAndreas Gohr     */
2681c14c879SAndreas Gohr    protected function load_template($title){
2691ef68647SAndreas Gohr        global $ID;
2701ef68647SAndreas Gohr        global $conf;
2711c14c879SAndreas Gohr        $tpl = $this->tpl;
2721ef68647SAndreas Gohr
2732eedf77dSAndreas Gohr        // this is what we'll return
2742eedf77dSAndreas Gohr        $output = array(
2752eedf77dSAndreas Gohr            'html'  => '',
2762eedf77dSAndreas Gohr            'page'  => '',
2772eedf77dSAndreas Gohr            'first' => '',
2782eedf77dSAndreas Gohr            'cite'  => '',
2792eedf77dSAndreas Gohr        );
2802eedf77dSAndreas Gohr
2812eedf77dSAndreas Gohr        // prepare header/footer elements
2822eedf77dSAndreas Gohr        $html = '';
2832eedf77dSAndreas Gohr        foreach(array('header','footer') as $t){
2849e6e41f4SAndreas Gohr            foreach(array('','_odd','_even','_first') as $h){
2852eedf77dSAndreas Gohr                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
2862eedf77dSAndreas Gohr                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
2872eedf77dSAndreas Gohr                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
2882eedf77dSAndreas Gohr                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
2892eedf77dSAndreas Gohr
2902eedf77dSAndreas Gohr                    // register the needed pseudo CSS
2919e6e41f4SAndreas Gohr                    if($h == '_first'){
2922eedf77dSAndreas Gohr                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
2932eedf77dSAndreas Gohr                    }elseif($h == '_even'){
2942eedf77dSAndreas Gohr                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
295daa70883SAndreas Gohr                    }elseif($h == '_odd'){
2962eedf77dSAndreas Gohr                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
297daa70883SAndreas Gohr                    }else{
298daa70883SAndreas Gohr                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
2992eedf77dSAndreas Gohr                    }
3002eedf77dSAndreas Gohr                }
3012eedf77dSAndreas Gohr            }
3022eedf77dSAndreas Gohr        }
3032eedf77dSAndreas Gohr
3041ef68647SAndreas Gohr        // prepare replacements
3051ef68647SAndreas Gohr        $replace = array(
3061ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
3071ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
3082eedf77dSAndreas Gohr                '@TITLE@'   => hsc($title),
3091ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
3101ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
3111ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
3125d6fbaeaSAndreas Gohr                '@BASE@'    => DOKU_BASE,
313a180c973SKlap-in                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/'
3141ef68647SAndreas Gohr        );
3151ef68647SAndreas Gohr
3162eedf77dSAndreas Gohr        // set HTML element
317a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
318a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
319a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
3201ef68647SAndreas Gohr
3212eedf77dSAndreas Gohr        // citation box
3222eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
3232eedf77dSAndreas Gohr            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
3242eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
3252eedf77dSAndreas Gohr        }
3261ef68647SAndreas Gohr
3272eedf77dSAndreas Gohr        return $output;
3281ef68647SAndreas Gohr    }
3291ef68647SAndreas Gohr
3301ef68647SAndreas Gohr    /**
331a180c973SKlap-in     * @param string $raw code with placeholders
332a180c973SKlap-in     * @param string $id  pageid
333a180c973SKlap-in     * @return string
334a180c973SKlap-in     */
335a180c973SKlap-in    protected function page_depend_replacements($raw, $id){
336a180c973SKlap-in        global $REV;
337a180c973SKlap-in
338a180c973SKlap-in        // generate qr code for this page using google infographics api
339a180c973SKlap-in        $qr_code = '';
340a180c973SKlap-in        if ($this->getConf('qrcodesize')) {
341a180c973SKlap-in            $url = urlencode(wl($id,'','&',true));
342a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
343a180c973SKlap-in                $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
344a180c973SKlap-in        }
345a180c973SKlap-in        // prepare replacements
346a180c973SKlap-in        $replace['@ID@']      = $id;
347a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
348a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev'=> $REV) : false, true, "&");
349a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
350a180c973SKlap-in
351a180c973SKlap-in        return str_replace(array_keys($replace), array_values($replace), $raw);
352a180c973SKlap-in    }
353a180c973SKlap-in
354a180c973SKlap-in    /**
3551c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
3561ef68647SAndreas Gohr     */
3571c14c879SAndreas Gohr    protected function load_css(){
358737417c6SKlap-in        global $conf;
3591c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
3601c14c879SAndreas Gohr        define('SIMPLE_TEST',1);
3611c14c879SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
362ee19bac3SLuigi Micco
3631c14c879SAndreas Gohr        // prepare CSS files
3641c14c879SAndreas Gohr        $files = array_merge(
3651c14c879SAndreas Gohr                    array(
3661c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/screen.css'
3671c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
3681c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/print.css'
3691c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
3701c14c879SAndreas Gohr                    ),
3711c14c879SAndreas Gohr                    css_pluginstyles('all'),
37258e6409eSAndreas Gohr                    $this->css_pluginPDFstyles(),
3731c14c879SAndreas Gohr                    array(
3741c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
3751c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
3761c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
3771c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
3781c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
3791c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
3801c14c879SAndreas Gohr                    )
3811c14c879SAndreas Gohr                 );
3821c14c879SAndreas Gohr        $css = '';
3831c14c879SAndreas Gohr        foreach($files as $file => $location){
38428e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
38528e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
3861c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
3871ef68647SAndreas Gohr        }
3881ef68647SAndreas Gohr
38928e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
3901c14c879SAndreas Gohr            // apply pattern replacements
39128e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
39228e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
39328e636eaSGerrit Uitslag
39428e636eaSGerrit Uitslag            // parse less
39528e636eaSGerrit Uitslag            $css = css_parseless($css);
39628e636eaSGerrit Uitslag        } else {
39728e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
3981c14c879SAndreas Gohr            $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
39928e636eaSGerrit Uitslag        }
4001ef68647SAndreas Gohr
4011c14c879SAndreas Gohr        return $css;
402ee19bac3SLuigi Micco    }
4031c14c879SAndreas Gohr
40458e6409eSAndreas Gohr    /**
40558e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
40658e6409eSAndreas Gohr     *
40758e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
40858e6409eSAndreas Gohr     *
40958e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
41058e6409eSAndreas Gohr     */
4116be736bfSGerrit Uitslag    protected function css_pluginPDFstyles(){
41258e6409eSAndreas Gohr        $list = array();
41358e6409eSAndreas Gohr        $plugins = plugin_list();
414f54b51f7SAndreas Gohr
415f54b51f7SAndreas Gohr        $usestyle = explode(',',$this->getConf('usestyles'));
41658e6409eSAndreas Gohr        foreach ($plugins as $p){
417f54b51f7SAndreas Gohr            if(in_array($p,$usestyle)){
418f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
419f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
420f54b51f7SAndreas Gohr            }
421f54b51f7SAndreas Gohr
42258e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
42358e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
42458e6409eSAndreas Gohr            }else{
42558e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
42658e6409eSAndreas Gohr            }
42758e6409eSAndreas Gohr        }
42858e6409eSAndreas Gohr        return $list;
42958e6409eSAndreas Gohr    }
430*ad18f4e1SGerrit Uitslag
431*ad18f4e1SGerrit Uitslag    /**
432*ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
433*ad18f4e1SGerrit Uitslag     */
434*ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
435*ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
436*ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
437*ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
438*ad18f4e1SGerrit Uitslag    }
439*ad18f4e1SGerrit Uitslag
440*ad18f4e1SGerrit Uitslag    /**
441*ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
442*ad18f4e1SGerrit Uitslag     */
443*ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
444*ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
445*ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
446*ad18f4e1SGerrit Uitslag        return 0;
447*ad18f4e1SGerrit Uitslag    }
448ee19bac3SLuigi Micco}
449