xref: /plugin/siteexport/action/pdfstyles.php (revision 55c869b6a74bad0804df55a3b9eeaa77077dbe4d)
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, $conf;
58
59        $conf['cssdatauri'] = false;
60
61        switch( $event->data['mediatype'] ) {
62
63            case 'print':
64            case 'all':
65                // Filter for user styles
66                $allowed = array_filter( array_keys($event->data['files']), array($this, 'filter_css') );
67                $event->data['files'] = array_intersect_key($event->data['files'], array_flip($allowed));
68                break;
69
70            case 'screen':
71            case 'speech':
72            case 'DW_DEFAULT':
73                $event->preventDefault();
74                break;
75        }
76    }
77
78    /**
79     * A simple filter function to check the input string against a list of path-parts that are allowed
80     *
81     * @param string    $str   the script file to check against the list
82     * @param mixed     $list  the list of path parts to test
83     * @return boolean
84     */
85    private function includeFilter( $str, $list ) {
86
87        foreach( $list as $entry ) {
88            if ( strpos( $str, $entry ) ) return true;
89        }
90
91        return false;
92    }
93
94    /**
95     * Filters scripts that are intended for admins only
96     *
97     * @param string    $script   the script file to check against the list
98     * @return boolean
99     */
100    private function filter_css( $script ) {
101        return $this->includeFilter( $script, array(
102            '/lib/tpl/',
103        ));
104    }
105}
106
107// vim:ts=4:sw=4:et:
108