xref: /plugin/mikioplugin/action.php (revision b007ca42f5dcac2e4da8e8ce8426ac9f91496844)
1<?php
2
3/**
4 * Mikio Plugin
5 *
6 * @version 1.0
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  James Collins <james.collins@outlook.com.au>
9 */
10
11if (!defined('DOKU_INC')) { die();
12}
13
14require_once 'icons/icons.php';
15require_once dirname(__FILE__) . '/inc/polyfill-array-key-first.php';
16
17if (!function_exists('glob_recursive')) {
18    function glob_recursive($pattern, $flags = 0)
19    {
20        $files = glob($pattern, $flags);
21        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
22            $files = array_merge($files, glob_recursive($dir . '/' . basename($pattern), $flags));
23        }
24        return $files;
25    }
26}
27
28class action_plugin_mikioplugin extends DokuWiki_Action_Plugin
29{
30    public function register(Doku_Event_Handler $controller)
31    {
32        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_load');
33    }
34
35    public function _load(Doku_Event $event, $param)
36    {
37        global $conf;
38        global $MIKIO_ICONS;
39
40        $baseDir = str_replace('\\', '/', DOKU_BASE . 'lib/plugins' . str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)) . '/');
41        $stylesheets = [];
42        $less = [];
43        $scripts = [];
44
45        if (is_array($MIKIO_ICONS)) {
46            $icons = array();
47            foreach ($MIKIO_ICONS as $icon) {
48                if (isset($icon['name']) && isset($icon['css']) && isset($icon['insert'])) {
49                    $icons[] = $icon;
50
51                    if ($icon['css'] != '') {
52                        if (strpos($icon['css'], '//') === false) {
53                            $stylesheets[] = $baseDir . 'icons/' . $icon['css'];
54                        } else {
55                            $stylesheets[] = $icon['css'];
56                        }
57                    }
58                }
59            }
60            $MIKIO_ICONS = $icons;
61        } else {
62            $MIKIO_ICONS = [];
63        }
64
65        $stylesList = glob_recursive(str_replace('\\', '/', 'lib/plugins' . str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)) . '/styles/*'));
66        if ($stylesList !== false) {
67            foreach ($stylesList as $value) {
68                $filename = strtolower($value);
69                if (substr($filename, -5) == '.less' || substr($filename, -4) == '.css') {
70                    $stylesheets[] = DOKU_BASE . $filename;
71                }
72            }
73        }
74
75        $stylesheets = array_unique($stylesheets);
76
77        $tpl_supported = false;
78        if($conf['template'] === 'mikio' && file_exists(tpl_incdir() . 'template.info.txt')) {
79            $tpl_info = [];
80            $tpl_data = file_get_contents(tpl_incdir() . 'template.info.txt');
81            foreach(preg_split("/(\r\n|\n|\r)/", $tpl_data) as $line){
82                if(preg_match("/([a-z]*)\s+(.*)/", $line, $matches)) {
83                    $tpl_info[$matches[1]] = $matches[2];
84                }
85            }
86
87            if(array_key_exists('date', $tpl_info)) {
88                $date = array_map('intval', explode('-', $tpl_info['date']));
89                if(count($date) === 3) {
90                    // Date of mikio template is > 2022-10-12
91                    if($date[0] > 2022 || ($date[0] == 2022 && ($date[1] > 10 || ($date[1] == 10 && $date[2] > 12)))) {
92                        $tpl_supported = true;
93                    }
94                }
95            }
96        }
97
98        if($tpl_supported == false) {
99            array_unshift($stylesheets, $baseDir . 'assets/variables.css');
100        }
101
102        array_unshift($stylesheets, $baseDir . 'assets/styles.less');
103
104        // css
105        foreach ($stylesheets as $style) {
106            if (strtolower(substr($style, -5)) == '.less') {
107                $less[] = $style;
108            } else {
109                array_unshift(
110                    $event->data['link'], array(
111                    'type' => 'text/css',
112                    'rel'  => 'stylesheet',
113                    'href' => $style
114                    )
115                );
116            }
117        }
118
119        $lessPath = implode(',', $less);
120
121        if(strlen($lessPath) > 0) {
122            array_unshift(
123                $event->data['link'], array(
124                'type' => 'text/css',
125                'rel'  => 'stylesheet',
126                'href' => $baseDir . 'css.php?css=' . str_replace($baseDir, '', $lessPath)
127                )
128            );
129        }
130
131        // js
132        foreach ($scripts as $script) {
133            $event->data['script'][] = array(
134            'type'  => 'text/javascript',
135            '_data' => '',
136            'src'   => $script
137            );
138        }
139    }
140}
141