xref: /plugin/dw2pdf/action.php (revision 2c53f619c72428fd39c97befc9b195dc537fd568)
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;
51ad18f4e1SGerrit Uitslag        global $INPUT, $conf;
52ee19bac3SLuigi Micco
531ef68647SAndreas Gohr        // our event?
54ad18f4e1SGerrit 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);
65ad18f4e1SGerrit Uitslag
66ad18f4e1SGerrit Uitslag        } elseif($ACT == 'export_pdfns') {
67ad18f4e1SGerrit Uitslag            //check input for title and ns
6826be4eceSGerrit Uitslag            if(!$title = $INPUT->str('pdfns_title')) {
6926be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
70ad18f4e1SGerrit Uitslag                return false;
71ad18f4e1SGerrit Uitslag            }
7226be4eceSGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('pdfns_ns'));
73ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
7426be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needns');
75ad18f4e1SGerrit Uitslag                return false;
76ad18f4e1SGerrit Uitslag            }
77ad18f4e1SGerrit Uitslag
7826be4eceSGerrit Uitslag            //sort order
79ad18f4e1SGerrit Uitslag            $order = $INPUT->str('pdfns_order', 'natural', true);
80ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
81ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
82ad18f4e1SGerrit Uitslag                $order = 'natural';
83ad18f4e1SGerrit Uitslag            }
84ad18f4e1SGerrit Uitslag
8526be4eceSGerrit Uitslag            //search depth
86ad18f4e1SGerrit Uitslag            $depth = $INPUT->int('pdfns_depth', 0);
87ad18f4e1SGerrit Uitslag            if($depth < 0) {
88ad18f4e1SGerrit Uitslag                $depth = 0;
89ad18f4e1SGerrit Uitslag            }
9026be4eceSGerrit Uitslag
91ad18f4e1SGerrit Uitslag            //page search
92ad18f4e1SGerrit Uitslag            $result = array();
93ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
94ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
95ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
96ad18f4e1SGerrit Uitslag
9726be4eceSGerrit Uitslag            //sorting
98ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
99ad18f4e1SGerrit Uitslag                if($order == 'date') {
100ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
101ad18f4e1SGerrit Uitslag                } elseif($order == 'pagename') {
102ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
103ad18f4e1SGerrit Uitslag                }
104ad18f4e1SGerrit Uitslag            }
105ad18f4e1SGerrit Uitslag
106ad18f4e1SGerrit Uitslag            foreach($result as $item) {
107ad18f4e1SGerrit Uitslag                $list[] = $item['id'];
108ad18f4e1SGerrit Uitslag            }
109ad18f4e1SGerrit Uitslag
110737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
11126be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
11226be4eceSGerrit Uitslag            if(!$title = $INPUT->str('pdfbook_title')) {  //TODO when title is changed, the cached file contains the old title
11326be4eceSGerrit Uitslag                $this->showPageWithErrorMsg($event, 'needtitle');
114737417c6SKlap-in                return false;
11526be4eceSGerrit Uitslag            } else {
116737417c6SKlap-in                $list = explode("|", $_COOKIE['list-pagelist']);
11726be4eceSGerrit Uitslag            }
118ad18f4e1SGerrit Uitslag
119737417c6SKlap-in        } else {
12026be4eceSGerrit Uitslag            //show empty bookcreator message
12126be4eceSGerrit Uitslag            $this->showPageWithErrorMsg($event, 'empty');
122737417c6SKlap-in            return false;
123737417c6SKlap-in        }
124737417c6SKlap-in
125737417c6SKlap-in        // it's ours, no one else's
126737417c6SKlap-in        $event->preventDefault();
12787c86ddaSAndreas Gohr
1286ea88a05SAndreas Gohr        // decide on the paper setup from param or config
1296ea88a05SAndreas Gohr        $pagesize    = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
1306ea88a05SAndreas Gohr        $orientation = $INPUT->str('orientation', $this->getConf('orientation'), true);
1316ea88a05SAndreas Gohr
13287c86ddaSAndreas Gohr        // prepare cache
1336ea88a05SAndreas Gohr        $cache = new cache(join(',',$list).$REV.$this->tpl.$pagesize.$orientation,'.dw2.pdf');
13487c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN',$list);
13587c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
13687c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/renderer.php';
13787c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
138df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
13987c86ddaSAndreas Gohr
140d01d2a42SAndreas Gohr        // hard work only when no cache available
14187c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
1421ef68647SAndreas Gohr            // initialize PDF library
143cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__)."/DokuPDF.class.php");
1446ea88a05SAndreas Gohr
1456ea88a05SAndreas Gohr            $mpdf = new DokuPDF($pagesize, $orientation);
146ee19bac3SLuigi Micco
147d62df65bSAndreas Gohr            // let mpdf fix local links
148d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
149d62df65bSAndreas Gohr            $url  = $self['scheme'].'://'.$self['host'];
150737417c6SKlap-in            if($self['port']) $url .= ':'.$self['port'];
151d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
152d62df65bSAndreas Gohr
15356d13144SAndreas Gohr            // Set the title
15456d13144SAndreas Gohr            $mpdf->SetTitle($title);
15556d13144SAndreas Gohr
1561ef68647SAndreas Gohr            // some default settings
157daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
158daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
159daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
160daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
1612eedf77dSAndreas Gohr
16256d13144SAndreas Gohr            // load the template
1631c14c879SAndreas Gohr            $template = $this->load_template($title);
164ee19bac3SLuigi Micco
1651ef68647SAndreas Gohr            // prepare HTML header styles
166ee19bac3SLuigi Micco            $html  = '<html><head>';
167737417c6SKlap-in            $html .= '<style type="text/css">';
1681c14c879SAndreas Gohr            $html .= $this->load_css();
169daa70883SAndreas Gohr            $html .= '@page { size:auto; '.$template['page'].'}';
1702eedf77dSAndreas Gohr            $html .= '@page :first {'.$template['first'].'}';
1711ef68647SAndreas Gohr            $html .= '</style>';
1721ef68647SAndreas Gohr            $html .= '</head><body>';
1732eedf77dSAndreas Gohr            $html .= $template['html'];
1741c14c879SAndreas Gohr            $html .= '<div class="dokuwiki">';
1752eedf77dSAndreas Gohr
1761ef68647SAndreas Gohr            // loop over all pages
1771ef68647SAndreas Gohr            $cnt = count($list);
1781ef68647SAndreas Gohr            for($n=0; $n<$cnt; $n++){
179ee19bac3SLuigi Micco                $page = $list[$n];
180ee19bac3SLuigi Micco
181a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
182a180c973SKlap-in                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
1831ef68647SAndreas Gohr                if ($n < ($cnt - 1)){
1841ef68647SAndreas Gohr                    $html .= '<pagebreak />';
1851ef68647SAndreas Gohr                }
186ee19bac3SLuigi Micco            }
187ee19bac3SLuigi Micco
1881c14c879SAndreas Gohr            $html .= '</div>';
189f765508eSGerrit Uitslag
190f765508eSGerrit Uitslag            //Return html for debugging
19126be4eceSGerrit Uitslag            if($conf['allowdebug'] && $_GET['debughtml'] == 'html') {
19226be4eceSGerrit Uitslag                echo $html;
19326be4eceSGerrit Uitslag                exit();
19426be4eceSGerrit Uitslag            };
195f765508eSGerrit Uitslag
196ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
197a06728a6SAndreas Gohr
19887c86ddaSAndreas Gohr            // write to cache file
19987c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
20087c86ddaSAndreas Gohr        }
20187c86ddaSAndreas Gohr
20287c86ddaSAndreas Gohr        // deliver the file
20387c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
204b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
20587c86ddaSAndreas Gohr        header('Pragma: public');
20687c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
20787c86ddaSAndreas Gohr
2089a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"','    ')));
20987c86ddaSAndreas Gohr        if($this->getConf('output') == 'file'){
2109a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
21187c86ddaSAndreas Gohr        }else{
2129a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
21387c86ddaSAndreas Gohr        }
214ee19bac3SLuigi Micco
215e993da11SGerrit Uitslag        //try to send file, and exit if done
216e993da11SGerrit Uitslag        http_sendfile($cache->cache);
21787c86ddaSAndreas Gohr
21887c86ddaSAndreas Gohr        $fp = @fopen($cache->cache,"rb");
21987c86ddaSAndreas Gohr        if($fp){
22087c86ddaSAndreas Gohr            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
22187c86ddaSAndreas Gohr        }else{
22287c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
22387c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
22487c86ddaSAndreas Gohr        }
2251ef68647SAndreas Gohr        exit();
2261ef68647SAndreas Gohr    }
2271ef68647SAndreas Gohr
2286be736bfSGerrit Uitslag    /**
2296be736bfSGerrit Uitslag     * Add 'export pdf'-button to pagetools
2306be736bfSGerrit Uitslag     *
2316be736bfSGerrit Uitslag     * @param Doku_Event $event
2326be736bfSGerrit Uitslag     * @param mixed      $param not defined
2336be736bfSGerrit Uitslag     */
2346be736bfSGerrit Uitslag    public function addbutton(&$event, $param) {
235*2c53f619SAndreas Gohr        global $ID, $REV;
2366be736bfSGerrit Uitslag
2376be736bfSGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
2386be736bfSGerrit Uitslag            $params = array('do' => 'export_pdf');
2396be736bfSGerrit Uitslag            if($REV) $params['rev'] = $REV;
2406be736bfSGerrit Uitslag
2416be736bfSGerrit Uitslag            $event->data['items']['export_pdf'] =
2426be736bfSGerrit Uitslag                '<li>'
2436be736bfSGerrit Uitslag                .'<a href='.wl($ID, $params).'  class="action export_pdf" rel="nofollow" title="'.$this->getLang('export_pdf_button').'">'
2446be736bfSGerrit Uitslag                .'<span>'.$this->getLang('export_pdf_button').'</span>'
2456be736bfSGerrit Uitslag                .'</a>'
2466be736bfSGerrit Uitslag                .'</li>';
2476be736bfSGerrit Uitslag        }
2486be736bfSGerrit Uitslag    }
249d01d2a42SAndreas Gohr
250d01d2a42SAndreas Gohr    /**
2512eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
2521ef68647SAndreas Gohr     */
2531c14c879SAndreas Gohr    protected function load_template($title){
2541ef68647SAndreas Gohr        global $ID;
2551ef68647SAndreas Gohr        global $conf;
2561c14c879SAndreas Gohr        $tpl = $this->tpl;
2571ef68647SAndreas Gohr
2582eedf77dSAndreas Gohr        // this is what we'll return
2592eedf77dSAndreas Gohr        $output = array(
2602eedf77dSAndreas Gohr            'html'  => '',
2612eedf77dSAndreas Gohr            'page'  => '',
2622eedf77dSAndreas Gohr            'first' => '',
2632eedf77dSAndreas Gohr            'cite'  => '',
2642eedf77dSAndreas Gohr        );
2652eedf77dSAndreas Gohr
2662eedf77dSAndreas Gohr        // prepare header/footer elements
2672eedf77dSAndreas Gohr        $html = '';
2682eedf77dSAndreas Gohr        foreach(array('header','footer') as $t){
2699e6e41f4SAndreas Gohr            foreach(array('','_odd','_even','_first') as $h){
2702eedf77dSAndreas Gohr                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
2712eedf77dSAndreas Gohr                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
2722eedf77dSAndreas Gohr                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
2732eedf77dSAndreas Gohr                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
2742eedf77dSAndreas Gohr
2752eedf77dSAndreas Gohr                    // register the needed pseudo CSS
2769e6e41f4SAndreas Gohr                    if($h == '_first'){
2772eedf77dSAndreas Gohr                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
2782eedf77dSAndreas Gohr                    }elseif($h == '_even'){
2792eedf77dSAndreas Gohr                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
280daa70883SAndreas Gohr                    }elseif($h == '_odd'){
2812eedf77dSAndreas Gohr                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
282daa70883SAndreas Gohr                    }else{
283daa70883SAndreas Gohr                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
2842eedf77dSAndreas Gohr                    }
2852eedf77dSAndreas Gohr                }
2862eedf77dSAndreas Gohr            }
2872eedf77dSAndreas Gohr        }
2882eedf77dSAndreas Gohr
2891ef68647SAndreas Gohr        // prepare replacements
2901ef68647SAndreas Gohr        $replace = array(
2911ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
2921ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
2932eedf77dSAndreas Gohr                '@TITLE@'   => hsc($title),
2941ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
2951ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
2961ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
2975d6fbaeaSAndreas Gohr                '@BASE@'    => DOKU_BASE,
298a180c973SKlap-in                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/'
2991ef68647SAndreas Gohr        );
3001ef68647SAndreas Gohr
3012eedf77dSAndreas Gohr        // set HTML element
302a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
303a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
304a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
3051ef68647SAndreas Gohr
3062eedf77dSAndreas Gohr        // citation box
3072eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
3082eedf77dSAndreas Gohr            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
3092eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
3102eedf77dSAndreas Gohr        }
3111ef68647SAndreas Gohr
3122eedf77dSAndreas Gohr        return $output;
3131ef68647SAndreas Gohr    }
3141ef68647SAndreas Gohr
3151ef68647SAndreas Gohr    /**
316a180c973SKlap-in     * @param string $raw code with placeholders
317a180c973SKlap-in     * @param string $id  pageid
318a180c973SKlap-in     * @return string
319a180c973SKlap-in     */
320a180c973SKlap-in    protected function page_depend_replacements($raw, $id){
321a180c973SKlap-in        global $REV;
322a180c973SKlap-in
323a180c973SKlap-in        // generate qr code for this page using google infographics api
324a180c973SKlap-in        $qr_code = '';
325a180c973SKlap-in        if ($this->getConf('qrcodesize')) {
326a180c973SKlap-in            $url = urlencode(wl($id,'','&',true));
327a180c973SKlap-in            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
328a180c973SKlap-in                $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
329a180c973SKlap-in        }
330a180c973SKlap-in        // prepare replacements
331a180c973SKlap-in        $replace['@ID@']      = $id;
332a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
333a180c973SKlap-in        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev'=> $REV) : false, true, "&");
334a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
335a180c973SKlap-in
336a180c973SKlap-in        return str_replace(array_keys($replace), array_values($replace), $raw);
337a180c973SKlap-in    }
338a180c973SKlap-in
339a180c973SKlap-in    /**
3401c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
3411ef68647SAndreas Gohr     */
3421c14c879SAndreas Gohr    protected function load_css(){
343737417c6SKlap-in        global $conf;
3441c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
3451c14c879SAndreas Gohr        define('SIMPLE_TEST',1);
3461c14c879SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
347ee19bac3SLuigi Micco
3481c14c879SAndreas Gohr        // prepare CSS files
3491c14c879SAndreas Gohr        $files = array_merge(
3501c14c879SAndreas Gohr                    array(
3511c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/screen.css'
3521c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
3531c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/print.css'
3541c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
3551c14c879SAndreas Gohr                    ),
3561c14c879SAndreas Gohr                    css_pluginstyles('all'),
35758e6409eSAndreas Gohr                    $this->css_pluginPDFstyles(),
3581c14c879SAndreas Gohr                    array(
3591c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
3601c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
3611c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
3621c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
3631c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
3641c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
3651c14c879SAndreas Gohr                    )
3661c14c879SAndreas Gohr                 );
3671c14c879SAndreas Gohr        $css = '';
3681c14c879SAndreas Gohr        foreach($files as $file => $location){
36928e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
37028e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
3711c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
3721ef68647SAndreas Gohr        }
3731ef68647SAndreas Gohr
37428e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
3751c14c879SAndreas Gohr            // apply pattern replacements
37628e636eaSGerrit Uitslag            $styleini = css_styleini($conf['template']);
37728e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
37828e636eaSGerrit Uitslag
37928e636eaSGerrit Uitslag            // parse less
38028e636eaSGerrit Uitslag            $css = css_parseless($css);
38128e636eaSGerrit Uitslag        } else {
38228e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
3831c14c879SAndreas Gohr            $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
38428e636eaSGerrit Uitslag        }
3851ef68647SAndreas Gohr
3861c14c879SAndreas Gohr        return $css;
387ee19bac3SLuigi Micco    }
3881c14c879SAndreas Gohr
38958e6409eSAndreas Gohr    /**
39058e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
39158e6409eSAndreas Gohr     *
39258e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
39358e6409eSAndreas Gohr     *
39458e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
39558e6409eSAndreas Gohr     */
3966be736bfSGerrit Uitslag    protected function css_pluginPDFstyles(){
39758e6409eSAndreas Gohr        $list = array();
39858e6409eSAndreas Gohr        $plugins = plugin_list();
399f54b51f7SAndreas Gohr
400f54b51f7SAndreas Gohr        $usestyle = explode(',',$this->getConf('usestyles'));
40158e6409eSAndreas Gohr        foreach ($plugins as $p){
402f54b51f7SAndreas Gohr            if(in_array($p,$usestyle)){
403f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
404f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
405f54b51f7SAndreas Gohr            }
406f54b51f7SAndreas Gohr
40758e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
40858e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
40958e6409eSAndreas Gohr            }else{
41058e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
41158e6409eSAndreas Gohr            }
41258e6409eSAndreas Gohr        }
41358e6409eSAndreas Gohr        return $list;
41458e6409eSAndreas Gohr    }
415ad18f4e1SGerrit Uitslag
416ad18f4e1SGerrit Uitslag    /**
417ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
418ad18f4e1SGerrit Uitslag     */
419ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
420ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
421ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
422ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
423ad18f4e1SGerrit Uitslag    }
424ad18f4e1SGerrit Uitslag
425ad18f4e1SGerrit Uitslag    /**
426ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
427ad18f4e1SGerrit Uitslag     */
428ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
429ad18f4e1SGerrit Uitslag        if($a['id'] <= $b['id']) return -1;
430ad18f4e1SGerrit Uitslag        if($a['id'] > $b['id']) return 1;
431ad18f4e1SGerrit Uitslag        return 0;
432ad18f4e1SGerrit Uitslag    }
43326be4eceSGerrit Uitslag
43426be4eceSGerrit Uitslag    /**
43526be4eceSGerrit Uitslag     * Set error notification and reload page again
43626be4eceSGerrit Uitslag     *
43726be4eceSGerrit Uitslag     * @param Doku_Event $event
43826be4eceSGerrit Uitslag     * @param string     $msglangkey key of translation key
43926be4eceSGerrit Uitslag     */
44026be4eceSGerrit Uitslag    private function showPageWithErrorMsg(&$event, $msglangkey) {
44126be4eceSGerrit Uitslag        msg($this->getLang($msglangkey), -1);
44226be4eceSGerrit Uitslag
44326be4eceSGerrit Uitslag        $event->data = 'show';
44426be4eceSGerrit Uitslag        $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
44526be4eceSGerrit Uitslag    }
446ee19bac3SLuigi Micco}
447