xref: /plugin/siteexport/action/pdfstyles.php (revision 8b157782c87209a5933bf869562aea1c049554c8)
1<?php
2/**
3 * DokuWiki Plugin inetmodifications (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  i-net /// software <tools@inetsoftware.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_siteexport_pdfstyles extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21        global $INPUT;
22        if ( !strrpos( $_SERVER['SCRIPT_FILENAME'], 'css.php', -7 ) ) { return; }
23        if ( !$INPUT->has('pdfExport') ) { return true; }
24
25        $controller->register_hook('CSS_STYLES_INCLUDED', 'BEFORE', $this, 'handle_css_styles');
26        $controller->register_hook('CSS_CACHE_USE', 'BEFORE', $this, 'handle_use_cache');
27    }
28
29    /**
30     * This function serves debugging purposes and has to be enabled in the register phase
31     *
32     * @param Doku_Event $event  event object by reference
33     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
34     *                           handler was registered]
35     * @return void
36     */
37    public function handle_use_cache(Doku_Event &$event, $param) {
38        global $INPUT;
39
40        // We need different keys for each style sheet.
41        $event->data->key .= $INPUT->str('pdfExport', '0');
42        $event->data->cache = getCacheName( $event->data->key, $event->data->ext );
43
44        return true;
45    }
46
47    /**
48     * Finally, handle the JS script list. The script would be fit to do even more stuff / types
49     * but handles only admin and default currently.
50     *
51     * @param Doku_Event $event  event object by reference
52     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
53     *                           handler was registered]
54     * @return void
55     */
56    public function handle_css_styles(Doku_Event &$event, $param) {
57        global $INPUT;
58
59        switch( $event->data['mediatype'] ) {
60
61            case 'print':
62            case 'all':
63                // Filter for user styles
64                $allowed = array_filter( array_keys($event->data['files']), array($this, 'filter_css') );
65                $event->data['files'] = array_intersect_key($event->data['files'], array_flip($allowed));
66                break;
67
68            case 'screen':
69            case 'speech':
70            case 'DW_DEFAULT':
71                $event->preventDefault();
72                break;
73        }
74    }
75
76    /**
77     * A simple filter function to check the input string against a list of path-parts that are allowed
78     *
79     * @param string    $str   the script file to check against the list
80     * @param mixed     $list  the list of path parts to test
81     * @return boolean
82     */
83    private function includeFilter( $str, $list ) {
84
85        foreach( $list as $entry ) {
86            if ( strpos( $str, $entry ) ) return true;
87        }
88
89        return false;
90    }
91
92    /**
93     * Filters scripts that are intended for admins only
94     *
95     * @param string    $script   the script file to check against the list
96     * @return boolean
97     */
98    private function filter_css( $script ) {
99        return $this->includeFilter( $script, array(
100            '/lib/tpl/',
101        ));
102    }
103}
104
105// vim:ts=4:sw=4:et:
106