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[] = $baseDir . 'assets/variables.css';
76
77        $stylesheets = array_unique($stylesheets);
78
79        $tpl_supported = false;
80        if($conf['template'] === 'mikio' && file_exists(tpl_incdir() . 'template.info.txt')) {
81            $tpl_info = [];
82            $tpl_data = file_get_contents(tpl_incdir() . 'template.info.txt');
83            foreach(preg_split("/(\r\n|\n|\r)/", $tpl_data) as $line){
84                if(preg_match("/([a-z]*)\s+(.*)/", $line, $matches)) {
85                    $tpl_info[$matches[1]] = $matches[2];
86                }
87            }
88
89            if(array_key_exists('date', $tpl_info)) {
90                $date = array_map('intval', explode('-', $tpl_info['date']));
91                if(count($date) === 3) {
92                    // Date of mikio template is > 2022-10-12
93                    if($date[0] > 2022 || ($date[0] == 2022 && ($date[1] > 10 || ($date[1] == 10 && $date[2] > 12)))) {
94                        $tpl_supported = true;
95                    }
96                }
97            }
98        }
99
100        if($tpl_supported == false) {
101            array_unshift($stylesheets, $baseDir . 'assets/variables.css');
102        }
103
104        array_unshift($stylesheets, $baseDir . 'assets/styles.less');
105
106        // css
107        foreach ($stylesheets as $style) {
108            if (strtolower(substr($style, -5)) == '.less') {
109                $less[] = $style;
110            } else {
111                array_unshift(
112                    $event->data['link'], array(
113                    'type' => 'text/css',
114                    'rel'  => 'stylesheet',
115                    'href' => $style
116                    )
117                );
118            }
119        }
120
121        $lessPath = implode(',', $less);
122
123        if(strlen($lessPath) > 0) {
124            array_unshift(
125                $event->data['link'], array(
126                'type' => 'text/css',
127                'rel'  => 'stylesheet',
128                'href' => $baseDir . 'css.php?css=' . str_replace($baseDir, '', $lessPath)
129                )
130            );
131        }
132
133        // js
134        foreach ($scripts as $script) {
135            $event->data['script'][] = array(
136            'type'  => 'text/javascript',
137            '_data' => '',
138            'src'   => $script
139            );
140        }
141    }
142}
143