xref: /template/mikio/mikio.php (revision 234272691481e9df4f77516f3cfc3d67c0890af1)
1<?php
2/** @noinspection DuplicatedCode */
3/** @noinspection SpellCheckingInspection */
4
5/**
6 * DokuWiki Mikio Template
7 *
8 * @link    http://dokuwiki.org/template:mikio
9 * @author  James Collins <james.collins@outlook.com.au>
10 * @license GPLv2 (http://www.gnu.org/licenses/gpl-2.0.html)
11 */
12
13namespace dokuwiki\template\mikio;
14
15use Doku_Event;
16use dokuwiki\Menu\PageMenu;
17use dokuwiki\Menu\SiteMenu;
18use dokuwiki\Menu\UserMenu;
19use ParensParser;
20use simple_html_dom;
21use DOMDocument;
22use DOMNode;
23
24if (defined('DOKU_INC') === false) {
25    die();
26}
27
28require_once('icons/icons.php');
29require_once('inc/simple_html_dom.php');
30require_once('inc/parens-parser.php');
31
32class mikio
33{
34    /**
35     * @var mikio|null Instance of the class.
36     */
37    private static $instance = null;
38
39    /**
40     * @var string Template directory path from local FS.
41     */
42    public $tplDir  = '';
43
44    /**
45     * @var string Template directory path from web.
46     */
47    public $baseDir = '';
48
49    /**
50     * @var array Array of Javascript files to include in footer.
51     */
52    public $footerScript = [];
53
54    /**
55     * @var string Notifications from included pages.
56     */
57    private $includedPageNotifications = '';
58
59    /**
60     * @var array Array of formatted template configuration values.
61     */
62    static private $formattedConfigValues = [];
63
64
65    /**
66     * Class constructor
67     */
68    public function __construct()
69    {
70        $this->tplDir  = tpl_incdir();
71        $this->baseDir = tpl_basedir();
72
73        $this->registerHooks();
74    }
75
76    /**
77     * Returns the instance of the class
78     *
79     * @return  self        class instance
80     */
81    public static function getInstance(): self
82    {
83        if (self::$instance === null) {
84            self::$instance = new self();
85        }
86
87        return self::$instance;
88    }
89
90    /**
91     * Prevent cloning of the instance
92     *
93     * @noinspection PhpUnusedPrivateMethodInspection
94     */
95    private function __clone() {}
96
97    /**
98     * Prevent unserialization of the instance
99     *
100     * @noinspection PhpUnusedPrivateMethodInspection
101     */
102    public function __wakeup() {}
103
104
105    /**
106     * Register the themes hooks into Dokuwiki
107     *
108     * @return void
109     * @noinspection PhpUnusedPrivateMethodInspection
110     */
111    private function registerHooks(): void
112    {
113        global $EVENT_HANDLER;
114
115        $events_dispatcher = [
116            'TPL_METAHEADER_OUTPUT'     => 'metaheadersHandler'
117        ];
118
119        foreach ($events_dispatcher as $event => $method) {
120            $EVENT_HANDLER->register_hook($event, 'BEFORE', $this, $method);
121        }
122    }
123
124
125    /**
126     * Meta handler hook for DokuWiki
127     *
128     * @param   Doku_Event $event DokuWiki Event.
129     * @return  void
130     */
131    public function metaHeadersHandler(Doku_Event $event): void
132    {
133        global $MIKIO_ICONS;
134        global $conf;
135
136        global $MIKIO_TEMPLATE;
137        $MIKIO_TEMPLATE = '123';    // TODO - is this set correctly?
138
139        $this->includePage('theme', false);
140
141        $stylesheets    = [];
142        $scripts        = [];
143
144        if (empty($this->getConf('customTheme')) === false) {
145            if (file_exists($this->tplDir . 'themes/' . $this->getConf('customTheme') . '/style.less') === true) {
146                $stylesheets[] = $this->baseDir . 'themes/' . $this->getConf('customTheme') . '/style.less';
147            } else {
148                if (file_exists($this->tplDir . 'themes/' . $this->getConf('customTheme') . '/style.css') === true) {
149                    $stylesheets[] = $this->baseDir . 'themes/' . $this->getConf('customTheme') . '/style.css';
150                }
151            }
152            if (file_exists($this->tplDir . 'themes/' . $this->getConf('customTheme') . '/script.js') === true) {
153                $scripts[] = $this->baseDir . 'themes/' . $this->getConf('customTheme') . '/script.js';
154            }
155        }
156
157        if (is_array($MIKIO_ICONS) === true && empty($this->getConf('iconTag', 'icon')) === false) {
158            $icons = [];
159            foreach ($MIKIO_ICONS as $icon) {
160                if (isset($icon['name']) === true && isset($icon['css']) === true && isset($icon['insert']) === true) {
161                    $icons[] = $icon;
162
163                    if (empty($icon['css']) === false) {
164                        if (strpos($icon['css'], '//') === false) {
165                            $stylesheets[] = $this->baseDir . 'icons/' . $icon['css'];
166                        } else {
167                            $stylesheets[] = $icon['css'];
168                        }
169                    }
170                }
171            }
172            $MIKIO_ICONS = $icons;
173        } else {
174            $MIKIO_ICONS = [];
175        }
176
177        $scripts[] = $this->baseDir . 'assets/mikio-typeahead.js';
178        $scripts[] = $this->baseDir . 'assets/mikio.js';
179
180        if ($this->getConf('useLESS') === true) {
181            $stylesheets[] = $this->baseDir . 'assets/mikio.less';
182        } else {
183            $stylesheets[] = $this->baseDir . 'assets/mikio.css';
184        }
185
186        /* MikioPlugin Support */
187        if (plugin_load('action', 'mikioplugin') !== null) {
188            if ($this->getConf('useLESS') === true) {
189                $stylesheets[] = $this->baseDir . 'assets/mikioplugin.less';
190            } else {
191                $stylesheets[] = $this->baseDir . 'assets/mikioplugin.css';
192            }
193        }
194
195        $set = [];
196        foreach ($stylesheets as $style) {
197            if (in_array($style, $set, true) === false) {
198                if ($this->getConf('useLESS') === true && strcasecmp(substr($style, -5), '.less') === 0) {
199                    $style = $this->baseDir . 'css.php?css=' . str_replace($this->baseDir, '', $style);
200                }
201
202                array_unshift($event->data['link'], [
203                    'type' => 'text/css',
204                    'rel'  => 'stylesheet',
205                    'href' => $style
206                ]);
207            }
208            $set[] = $style;
209        }
210
211        $set = [];
212        foreach ($scripts as $script) {
213            if (in_array($script, $set, true) === false) {
214                $script_params = [
215                    'type'  => 'text/javascript',
216                    '_data' => '',
217                    'src'   => $script
218                ];
219
220                // equal to or greator than hogfather
221                if ($this->getDokuWikiVersion() >= 20200729 || $this->getDokuWikiVersion() === 0) {
222                    // greator than hogfather - defer always on
223                    if ($this->getDokuWikiVersion() >= 20200729 || $this->getDokuWikiVersion() === 0) {
224                        $script_params += ['defer' => 'defer'];
225                    } else {
226                        // hogfather - defer always on unless $conf['defer_js'] is false
227                        if (array_key_exists('defer_js', $conf) === false || $conf['defer_js'] === true) {
228                            $script_params += ['defer' => 'defer'];
229                        }
230                    }
231                }
232
233                $event->data['script'][] = $script_params;
234            }//end if
235            $set[] = $script;
236        }//end foreach
237    }
238
239
240    /**
241     * Print or return the footer metadata
242     *
243     * @param   boolean $print Print the data to buffer.
244     * @return  string         HTML footer meta data
245     */
246    public function includeFooterMeta(bool $print = true): string
247    {
248        $html = '';
249
250        if (count($this->footerScript) > 0) {
251            $html .= '<script type="text/javascript">function mikioFooterRun() {';
252            foreach ($this->footerScript as $script) {
253                $html .= $script . ';';
254            }
255            $html .= '}</script>';
256        }
257
258
259        if ($print === true) {
260            echo $html;
261        }
262        return $html;
263    }
264
265    /**
266     * Retreive and parse theme configuration options
267     *
268     * @param   string $key     The configuration key to retreive.
269     * @param   mixed  $default If key doesn't exist, return this value.
270     * @return  mixed           parsed value of configuration
271     */
272    public function getConf(string $key, $default = false)
273    {
274        if(array_key_exists($key, self::$formattedConfigValues) === true) {
275            return self::$formattedConfigValues[$key];
276        }
277
278        $value = tpl_getConf($key, $default);
279
280        $data = [
281            ['keys' => ['navbarDWMenuType'],
282                'type' => 'choice',
283                'values' => [tpl_getLang('value_both'), tpl_getLang('value_icons'), tpl_getLang('value_text')]
284            ],
285            ['keys' => ['navbarDWMenuCombine'],
286                'type' => 'choice',
287                'values' => [tpl_getLang('value_combine'), tpl_getLang('value_separate'), tpl_getLang('value_dropdown')]
288            ],
289            ['keys' => ['navbarPosLeft', 'navbarPosMiddle', 'navbarPosRight'],
290                'type' => 'choice',
291                'values' => [tpl_getLang('value_none'), tpl_getLang('value_custom'), tpl_getLang('value_search'), tpl_getLang('value_dokuwiki')],
292                'default' => [
293                    'navbarPosLeft' => tpl_getLang('value_none'),
294                    'navbarPosMiddle' => tpl_getLang('value_search'),
295                    'navbarPosRight' => tpl_getLang('value_dokuwiki')
296                ]
297            ],
298            ['keys' => ['navbarItemShowCreate', 'navbarItemShowShow', 'navbarItemShowRevs', 'navbarItemShowBacklink',
299                'navbarItemShowRecent', 'navbarItemShowMedia', 'navbarItemShowIndex', 'navbarItemShowProfile',
300                'navbarItemShowAdmin'
301            ],
302                'type' => 'choice',
303                'values' => [tpl_getLang('value_always'), tpl_getLang('value_logged_in'), tpl_getLang('value_logged_out'), tpl_getLang('value_never')]
304            ],
305            ['keys' => ['navbarItemShowLogin', 'navbarItemShowLogout'],
306                'type' => 'choice',
307                'values' => [tpl_getLang('value_always'), tpl_getLang('value_never')]
308            ],
309            ['keys' => ['searchButton'],                    'type' => 'choice',
310                'values' => [tpl_getLang('value_icon'), tpl_getLang('value_text')]
311            ],
312            ['keys' => ['breadcrumbPosition', 'youareherePosition'],
313                'type' => 'choice',
314                'values' => [tpl_getLang('value_top'), tpl_getLang('value_hero'), tpl_getLang('value_page'), tpl_getLang('value_none')]
315            ],
316            ['keys' => ['youarehereHome'],                  'type' => 'choice',
317                'values' => [tpl_getLang('value_page_title'), tpl_getLang('value_home'), tpl_getLang('value_icon'), tpl_getLang('value_none')]
318            ],
319            ['keys' => ['sidebarLeftRow1', 'sidebarLeftRow2', 'sidebarLeftRow3', 'sidebarLeftRow4'],
320                'type' => 'choice',
321                'values' => [tpl_getLang('value_none'), tpl_getLang('value_logged in user'), tpl_getLang('value_search'), tpl_getLang('value_content'), tpl_getLang('value_tags')],
322                'default' => [
323                    'sidebarLeftRow1' => tpl_getLang('value_logged in user'),
324                    'sidebarLeftRow2' => tpl_getLang('value_search'),
325                    'sidebarLeftRow3' => tpl_getLang('value_content')
326                ]
327            ],
328            ['keys' => ['pageToolsFloating', 'pageToolsFooter'],
329                'type' => 'choice',
330                'values' => [tpl_getLang('value_always'), tpl_getLang('value_none'), tpl_getLang('value_page_editors')]
331            ],
332            ['keys' => ['pageToolsShowCreate', 'pageToolsShowEdit', 'pageToolsShowRevs', 'pageToolsShowBacklink',
333                'pageToolsShowTop'
334            ],
335                'type' => 'choice',
336                'values' => [tpl_getLang('value_always'), tpl_getLang('value_logged_in'), tpl_getLang('value_logged_out'), tpl_getLang('value_never')]
337            ],
338            ['keys' => ['showNotifications'],               'type' => 'choice',
339                'values' => [tpl_getLang('value_admin'), tpl_getLang('value_always'), tpl_getLang('value_none'), '', tpl_getLang('value_never')]
340            ],
341            ['keys' => ['licenseType'],                     'type' => 'choice',
342                'values' => [tpl_getLang('value_badge'), tpl_getLang('value_button'), tpl_getLang('value_none')]
343            ],
344            ['keys' => ['navbarUseTitleIcon'],              'type' => 'bool'],
345            ['keys' => ['navbarUseTitleText'],              'type' => 'bool'],
346            ['keys' => ['navbarUseTaglineText'],            'type' => 'bool'],
347            ['keys' => ['navbarShowSub'],                   'type' => 'bool'],
348            ['keys' => ['heroTitle'],                       'type' => 'bool'],
349            ['keys' => ['heroImagePropagation'],            'type' => 'bool'],
350            ['keys' => ['breadcrumbPrefix'],                'type' => 'bool'],
351            ['keys' => ['breadcrumbSep'],                   'type' => 'bool'],
352            ['keys' => ['youareherePrefix'],                'type' => 'bool'],
353            ['keys' => ['youarehereSep'],                   'type' => 'bool'],
354            ['keys' => ['sidebarShowLeft'],                 'type' => 'bool'],
355            ['keys' => ['sidebarShowRight'],                'type' => 'bool'],
356            ['keys' => ['tocFull'],                         'type' => 'bool'],
357            ['keys' => ['footerSearch'],                    'type' => 'bool'],
358            ['keys' => ['licenseImageOnly'],                'type' => 'bool'],
359            ['keys' => ['includePageUseACL'],               'type' => 'bool'],
360            ['keys' => ['includePagePropagate'],            'type' => 'bool'],
361            ['keys' => ['youarehereHideHome'],              'type' => 'bool'],
362            ['keys' => ['tagsConsolidate'],                 'type' => 'bool'],
363            ['keys' => ['tagsShowHero'],                    'type' => 'bool'],
364            ['keys' => ['footerInPage'],                    'type' => 'bool'],
365            ['keys' => ['sidebarMobileDefaultCollapse'],    'type' => 'bool'],
366            ['keys' => ['sidebarAlwaysShowLeft'],           'type' => 'bool'],
367            ['keys' => ['sidebarAlwaysShowRight'],          'type' => 'bool'],
368            ['keys' => ['searchUseTypeahead'],              'type' => 'bool'],
369            ['keys' => ['showLightDark'],                   'type' => 'bool'],
370            ['keys' => ['autoLightDark'],                   'type' => 'bool'],
371            ['keys' => ['youarehereShowLast'],              'type' => 'int'],
372
373            ['keys' => ['iconTag'],                         'type' => 'string'],
374            ['keys' => ['customTheme'],                     'type' => 'string'],
375            ['keys' => ['navbarCustomMenuText'],            'type' => 'string'],
376            ['keys' => ['breadcrumbPrefixText'],            'type' => 'string'],
377            ['keys' => ['breadcrumbSepText'],               'type' => 'string'],
378            ['keys' => ['youareherePrefixText'],            'type' => 'string'],
379            ['keys' => ['youarehereSepText'],               'type' => 'string'],
380            ['keys' => ['footerPageInfoText'],              'type' => 'string'],
381            ['keys' => ['footerCustomMenuText'],            'type' => 'string'],
382            ['keys' => ['brandURLGuest'],                   'type' => 'string'],
383            ['keys' => ['brandURLUser'],                    'type' => 'string'],
384
385            ['keys' => ['useLESS'],                         'type' => 'bool'],
386
387            ['keys' => ['stickyTopHeader'],                  'type' => 'bool'],
388            ['keys' => ['stickyNavbar'],                     'type' => 'bool'],
389            ['keys' => ['stickyHeader'],                     'type' => 'bool'],
390            ['keys' => ['stickyLeftSidebar'],                'type' => 'bool'],
391        ];
392
393        foreach ($data as $row) {
394            // does not check case....
395            if (in_array($key, $row['keys'], true) === true) {
396                if (array_key_exists('type', $row) === true) {
397                    switch ($row['type']) {
398                        case 'bool':
399                            return (bool) $value;
400                        case 'int':
401                            return (int) $value;
402                        case 'string':
403                            return $value;
404                    }//end switch
405                }//end if
406
407                if (in_array($value, $row['values'], true) === true) {
408                    return $value;
409                }
410
411                if (array_key_exists('default', $row) === true) {
412                    if (is_array($row['default']) === true) {
413                        if (array_key_exists($key, $row['default']) === true) {
414                            return $row['default'][$key];
415                        }
416                    } else {
417                        return $row['default'];
418                    }
419                }
420
421                return reset($row['values']);
422            }//end if
423        }//end foreach
424
425        self::$formattedConfigValues[$key] = $value;
426        return $value;
427    }
428
429
430    /**
431     * Check if a page exist in directory or namespace
432     *
433     * @param   string $page Page/namespace to search.
434     * @return  boolean      if page exists
435     */
436    public function pageExists(string $page): bool
437    {
438        ob_start();
439        tpl_includeFile($page . '.html');
440        $html = ob_get_clean();
441
442        if (empty($html) === false) {
443            return true;
444        }
445
446        $useACL = $this->getConf('includePageUseACL');
447        $propagate = $this->getConf('includePagePropagate');
448
449        if ($propagate === true) {
450            if (page_findnearest($page, $useACL) !== false) {
451                return true;
452            }
453        } elseif ($useACL === true && auth_quickaclcheck($page) !== AUTH_NONE) {
454            return true;
455        }
456
457        return false;
458    }
459
460
461    /**
462     * Print or return page from directory or namespace
463     *
464     * @param   string  $page         Page/namespace to include.
465     * @param   boolean $print        Print content.
466     * @param   boolean $parse        Parse content before printing/returning.
467     * @param   string  $classWrapper Wrap page in a div with class.
468     * @return  string                contents of page found
469     */
470    public function includePage(string $page, bool $print = true, bool $parse = true, string $classWrapper = ''): string
471    {
472        ob_start();
473        tpl_includeFile($page . '.html');
474        $html = ob_get_clean();
475
476        if (empty($html) === true) {
477            $useACL = $this->getConf('includePageUseACL');
478            $propagate = $this->getConf('includePagePropagate');
479
480            ob_start();
481            $html = tpl_include_page($page, false, $propagate, $useACL);
482            $this->includedPageNotifications .= ob_get_clean();
483        }
484
485        if (empty($html) === false && $parse === true) {
486            $html = $this->parseContent($html); // TODO - move to end of main.php
487        }
488
489        if (empty($classWrapper) === false && empty($html) === false) {
490            $html = '<div class="' . $classWrapper . '">' . $html . '</div>';
491        }
492
493        if ($print === true) {
494            echo $html;
495        }
496        return $html;
497    }
498
499
500    /**
501     * Print or return logged-in user information
502     *
503     * @param   boolean $print Print content.
504     * @return  string         user information
505     */
506    public function includeLoggedIn(bool $print = true): string
507    {
508        $html = '';
509
510        if (empty($_SERVER['REMOTE_USER']) === false) {
511            $html .= '<div class="mikio-user-info">';
512            ob_start();
513            tpl_userinfo();
514            $html .= ob_get_clean();
515            $html .= '</div>';
516        }
517
518        if ($print === true) {
519            echo $html;
520        }
521        return $html;
522    }
523
524
525    /**
526     * Print or return DokuWiki Menu
527     *
528     * @param   boolean $print Print content.
529     * @return  string         contents of the menu
530     */
531    public function includeDWMenu(bool $print = true): string
532    {
533        global $lang;
534        global $USERINFO;
535
536        $loggedIn = (is_array($USERINFO) === true && count($USERINFO) > 0);
537        $html = '<ul class="mikio-nav">';
538
539        $pageToolsMenu = [];
540        $siteToolsMenu = [];
541        $userToolsMenu = [];
542
543        $showIcons  = ($this->getConf('navbarDWMenuType') != tpl_getLang('value_text'));
544        $showText   = ($this->getConf('navbarDWMenuType') != tpl_getLang('value_icons'));
545        $isDropDown = ($this->getConf('navbarDWMenuCombine') != tpl_getLang('value_separate'));
546
547        $items = (new PageMenu())->getItems();
548        foreach ($items as $item) {
549            if ($item->getType() !== 'top') {
550                $itemHtml = '';
551
552                $showItem = $this->getConf('navbarItemShow' . ucfirst($item->getType()));
553                if (
554                    $showItem !== false && (strcasecmp($showItem, tpl_getLang('value_always')) === 0 ||
555                    (strcasecmp($showItem, tpl_getLang('value_logged_in')) === 0 && $loggedIn === true) ||
556                    (strcasecmp($showItem, tpl_getLang('value_logged_out')) === 0 && $loggedIn === false))
557                ) {
558                    $title = isset($attr['title']) && $attr['title'] !== 0 ? $attr['title'] : $item->getTitle();
559
560                    $itemHtml .= '<a class="mikio-nav-link ' . ($isDropDown === true ? 'mikio-dropdown-item' : '') .
561                        ' ' . $item->getType() . '" href="' . $item->getLink() . '" title="' . $title . '"' . (isset($attr['accesskey']) && $attr['accesskey'] !== '' ? ' accesskey="' . $attr['accesskey'] . '"' : '') . '>';
562                    if ($showIcons === true) {
563                        $itemHtml .= '<span class="mikio-icon">' . inlineSVG($item->getSvg()) . '</span>';
564                    }
565                    if ($showText === true || $isDropDown === true) {
566                        $itemHtml .= '<span>' . $item->getLabel() . '</span>';
567                    }
568                    $itemHtml .= '</a>';
569
570                    $pageToolsMenu[] = $itemHtml;
571                }
572            }//end if
573        }//end foreach
574
575        $items = (new SiteMenu())->getItems();
576        foreach ($items as $item) {
577            $itemHtml = '';
578
579            $showItem = $this->getConf('navbarItemShow' . ucfirst($item->getType()));
580            if (
581                $showItem !== false && (strcasecmp($showItem, tpl_getLang('value_always')) === 0 ||
582                (strcasecmp($showItem, tpl_getLang('value_logged_in')) === 0 && $loggedIn === true) ||
583                (strcasecmp($showItem, tpl_getLang('value_logged_out')) === 0 && $loggedIn === false))
584            ) {
585                $itemHtml .= '<a class="mikio-nav-link ' . ($isDropDown === true ? 'mikio-dropdown-item' : '') . ' ' .
586                    $item->getType() . '" href="' . $item->getLink() . '" title="' . $item->getTitle() . '">';
587                if ($showIcons === true) {
588                    $itemHtml .= '<span class="mikio-icon">' . inlineSVG($item->getSvg()) . '</span>';
589                }
590                if ($showText === true || $isDropDown === true) {
591                    $itemHtml .= '<span>' . $item->getLabel() . '</span>';
592                }
593                $itemHtml .= '</a>';
594
595                $siteToolsMenu[] = $itemHtml;
596            }
597        }//end foreach
598
599        $items = (new UserMenu())->getItems();
600        foreach ($items as $item) {
601            $itemHtml = '';
602
603            $showItem = $this->getConf('navbarItemShow' . ucfirst($item->getType()));
604            if (
605                $showItem !== false && (strcasecmp($showItem, 'always') === 0 ||
606                (strcasecmp($showItem, tpl_getLang('value_logged_in')) === 0 && $loggedIn === true) ||
607                (strcasecmp($showItem, tpl_getLang('value_logged_out')) === 0 && $loggedIn === false))
608            ) {
609                $itemHtml .= '<a class="mikio-nav-link' . ($isDropDown === true ? ' mikio-dropdown-item' : '') . ' ' .
610                $item->getType() . '" href="' . $item->getLink() . '" title="' . $item->getTitle() . '">';
611                if ($showIcons === true) {
612                    $itemHtml .= '<span class="mikio-icon">' . inlineSVG($item->getSvg()) . '</span>';
613                }
614                if ($showText === true || $isDropDown === true) {
615                    $itemHtml .= '<span>' . $item->getLabel() . '</span>';
616                }
617                $itemHtml .= '</a>';
618
619                $userToolsMenu[] = $itemHtml;
620            }
621        }//end foreach
622
623        $value_dropdown = tpl_getLang('value_dropdown');
624        $value_combine = tpl_getLang('value_combine');
625//        $value_separate = tpl_getLang('value_separate');
626
627        switch ($this->getConf('navbarDWMenuCombine')) {
628            case $value_dropdown:
629                if (count($pageToolsMenu) > 0 ) {
630                    $html .= '<li id="dokuwiki__pagetools" class="mikio-nav-dropdown">';
631                    $html .= '<a id="mikio_dropdown_pagetools" class="nav-link dropdown-toggle" href="#" role="button"
632    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' .
633                        ($showIcons === true ? $this->mikioInlineIcon('file') : '') .
634                        ($showText === true ? $lang['page_tools'] : '<span class="mikio-small-only">' . $lang['page_tools'] .
635                            '</span>') . '</a>';
636
637                    $html .= '<div class="mikio-dropdown closed">' . implode('', $pageToolsMenu);
638
639                    $html .= '</div>';
640                    $html .= '</li>';
641                }
642
643                if (count($siteToolsMenu) > 0 ) {
644                    $html .= '<li id="dokuwiki__sitetools" class="mikio-nav-dropdown">';
645                    $html .= '<a id="mikio_dropdown_sitetools" class="nav-link dropdown-toggle" href="#" role="button"
646    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' .
647                        ($showIcons === true ? $this->mikioInlineIcon('gear') : '') .
648                        ($showText === true ? $lang['site_tools'] : '<span class="mikio-small-only">' .
649                        $lang['site_tools'] . '</span>') . '</a>';
650
651                    $html .= '<div class="mikio-dropdown closed">' . implode('', $siteToolsMenu);
652
653                    $html .= '</div>';
654                    $html .= '</li>';
655                }
656
657                /** @var helper_plugin_do $do */
658                $do = plugin_load('helper', 'do');
659                if ($do) {
660                    $html .= $do->tpl_getUserTasksIconHTML();
661                }
662
663                if (count($userToolsMenu) > 0 ) {
664                    $html .= '<li id="dokuwiki__usertools" class="mikio-nav-dropdown">';
665                    $html .= '<a id="mikio_dropdown_usertools" class="nav-link dropdown-toggle" href="#" role="button"
666    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' .
667                        ($showIcons === true ? $this->mikioInlineIcon('user') : '') .
668                        ($showText === true ? $lang['user_tools'] : '<span class="mikio-small-only">' .
669                            $lang['user_tools'] . '</span>') . '</a>';
670
671                    $html .= '<div class="mikio-dropdown closed">' . implode('', $userToolsMenu);
672
673                    $html .= '</div>';
674                    $html .= '</li>';
675                }
676
677                break;
678
679            case $value_combine:
680                $html .= '<li class="mikio-nav-dropdown">';
681                $html .= '<a class="mikio-nav-link" href="#">' .
682                    ($showIcons === true ? $this->mikioInlineIcon('wrench') : '') .
683                    ($showText === true ? tpl_getLang('tools-menu') : '<span class="mikio-small-only">' .
684                    tpl_getLang('tools-menu') . '</span>') . '</a>';
685                $html .= '<div class="mikio-dropdown closed">';
686
687                if (count($pageToolsMenu) > 0) {
688                    $html .= '<h6 class="mikio-dropdown-header">' . $lang['page_tools'] . '</h6>';
689                    foreach ($pageToolsMenu as $item) {
690                        $html .= $item;
691                    }
692                }
693
694                if (count($siteToolsMenu) > 0) {
695                    $html .= '<div class="mikio-dropdown-divider"></div>';
696                    $html .= '<h6 class="mikio-dropdown-header">' . $lang['site_tools'] . '</h6>';
697                    foreach ($siteToolsMenu as $item) {
698                        $html .= $item;
699                    }
700                }
701
702                /** @var helper_plugin_do $do */
703                $do = plugin_load('helper', 'do');
704                if ($do) {
705                    $html .= $do->tpl_getUserTasksIconHTML();
706                }
707
708                if (count($userToolsMenu) > 0) {
709                    $html .= '<div class="mikio-dropdown-divider"></div>';
710                    $html .= '<h6 class="mikio-dropdown-header">' . $lang['user_tools'] . '</h6>';
711                    foreach ($userToolsMenu as $item) {
712                        $html .= $item;
713                    }
714                }
715
716                $html .= '</div>';
717                $html .= '</li>';
718                break;
719
720            default:    // separate
721                foreach ($siteToolsMenu as $item) {
722                    $html .= '<li class="mikio-nav-item">' . $item . '</li>';
723                }
724
725                foreach ($pageToolsMenu as $item) {
726                    $html .= '<li class="mikio-nav-item">' . $item . '</li>';
727                }
728
729                /** @var helper_plugin_do $do */
730                $do = plugin_load('helper', 'do');
731                if ($do) {
732                    $html .= $do->tpl_getUserTasksIconHTML();
733                }
734
735                foreach ($userToolsMenu as $item) {
736                    $html .= '<li class="mikio-nav-item">' . $item . '</li>';
737                }
738
739                break;
740        }//end switch
741
742        $vswitch = plugin_load('syntax', 'versionswitch');
743        if ($vswitch && method_exists($vswitch, 'versionSelector')) {
744            $versionData = $vswitch->versionSelector();
745            $links = [];
746            $currentLinkText = "NA";
747
748            // Regex to find all 'a' tags
749            $pattern = '/<a\s+[^>]*href="([^"]+)"[^>]*>.*?<\/a>/i';
750            preg_match_all($pattern, $versionData, $matches);
751
752            // Loop through matches to build the links array
753            foreach ($matches[0] as $match) {
754                $links[] = $match;
755            }
756
757            // Regex to find the 'a' tag within 'curid' class span
758            $currentPattern = '/<li[^>]*class="[^"]*current[^"]*"[^>]*>\s*<a\s+[^>]*href="([^"]+)"[^>]*>([^<]+)<\/a>/i';
759            preg_match($currentPattern, $versionData, $currentMatch);
760
761            if (!empty($currentMatch)) {
762                $currentLinkText = $currentMatch[2]; // This will capture the text inside the <a> tag
763            }
764
765            $html .= '<li id="mikio__versionswitch" class="mikio-nav-dropdown">';
766            $html .= '<a id="mikio_dropdown_translate" class="nav-link dropdown-toggle" href="#" role="button"
767data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . $currentLinkText . '</a>';
768            $html .= '<div class="mikio-dropdown closed">';
769
770            foreach($links as $link) {
771                $classPattern = '/class="[^"]*"/i';
772                $html .= preg_replace($classPattern, 'class="mikio-nav-link mikio-dropdown-item"', $link);
773            }
774
775            $html .= '</div>';
776            $html .= '</li>';
777        }
778
779        $translation = plugin_load('helper', 'translation');
780        if ($translation !== null && method_exists($translation, 'showTranslations')) {
781            $html .= '<li id="mikio__translate" class="mikio-nav-dropdown">';
782            $html .= '<a id="mikio_dropdown_translate" class="nav-link dropdown-toggle" href="#" role="button"
783data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' .
784                $this->mikioInlineIcon('language') .
785                 '</a>';
786            $html .= '<div class="mikio-dropdown closed">';
787
788                $html .= $translation->showTranslations();
789
790            $html .= '</div>';
791            $html .= '</li>';
792        }
793
794        if ($this->getConf('showLightDark') === true) {
795            $autoLightDark = $this->getConf('autoLightDark');
796            $html .= '<li class="mikio-darklight">
797<a href="#" class="mikio-control mikio-button mikio-darklight-button">' .
798            ($autoLightDark === true ? $this->mikioInlineIcon('sunmoon', 'mikio-darklight-auto') : '') .
799            $this->mikioInlineIcon('sun', 'mikio-darklight-light') .
800            $this->mikioInlineIcon('moon', 'mikio-darklight-dark') .
801            '</a></li>';
802        }
803
804        $html .= '</ul>';
805
806        if ($print === true) {
807            echo $html;
808        }
809        return $html;
810    }
811
812
813    /**
814     * Create a nav element from a string. <uri>|<title>;
815     *
816     * @param string $str String to generate nav.
817     * @return string     nav elements generated
818     */
819    public function stringToNav(string $str): string
820    {
821        $html = '';
822
823        if (empty($str) === false) {
824            $items = explode(';', $str);
825            if (count($items) > 0) {
826                $html .= '<ul class="mikio-nav">';
827                foreach ($items as $item) {
828                    $parts = explode('|', $item);
829                    if ($parts > 1) {
830                        $html .= '<li class="mikio-nav-item"><a class="mikio-nav-link" href="' .
831                            strip_tags($this->getLink(trim($parts[0]))) . '">' . strip_tags(trim($parts[1])) .
832                            '</a></li>';
833                    }
834                }
835                $html .= '</ul>';
836            }
837        }
838
839        return $html;
840    }
841
842    /**
843     * print or return the main navbar
844     *
845     * @param boolean $print   Print the navbar.
846     * @param boolean $showSub Include the sub navbar.
847     * @return string          generated content
848     */
849    public function includeNavbar(bool $print = true, bool $showSub = false): string
850    {
851        global $conf, $USERINFO;
852
853        $homeUrl = wl();
854
855        if (plugin_isdisabled('showpageafterlogin') === false) {
856            $p = plugin_load('action', 'showpageafterlogin');
857            if (empty($p) === false) {
858                if (is_array($USERINFO) === true && count($USERINFO) > 0) {
859                    $homeUrl = wl($p->getConf('page_after_login'));
860                }
861            }
862        } else {
863            if (is_array($USERINFO) === true && count($USERINFO) > 0) {
864                $url = $this->getConf('brandURLUser');
865            } else {
866                $url = $this->getConf('brandURLGuest');
867            }
868            if (strlen($url) > 0) {
869                $homeUrl = $url;
870            }
871        }
872
873        $html = '<nav class="mikio-navbar' . (($this->getConf('stickyNavbar') === true) ? ' mikio-sticky' : '') .
874            '">';
875        $html .= '<div class="mikio-container">';
876        $html .= '<a class="mikio-navbar-brand" href="' . $homeUrl . '" accesskey="h" title="Home [h]">';
877        if ($this->getConf('navbarUseTitleIcon') === true || $this->getConf('navbarUseTitleText') === true) {
878            // Brand image
879            if ($this->getConf('navbarUseTitleIcon') === true) {
880                $logo = $this->getMediaFile('logo', false);
881                if (empty($logo) === false) {
882                    $width = $this->getConf('navbarTitleIconWidth');
883                    $height = $this->getConf('navbarTitleIconHeight');
884                    $styles = '';
885
886                    if (strlen($width) > 0 || strlen($height) > 0) {
887                        if (ctype_digit($width) === true) {
888                            $styles .= 'max-width:' . intval($width) . 'px;';
889                        } elseif (preg_match('/^\d+(px|rem|em|%)$/', $width) === 1) {
890                            $styles .= 'max-width:' . $width . ';';
891                        } elseif (strcasecmp($width, 'none') === 0) {
892                            $styles .= 'max-width:none;';
893                        }
894
895                        if (ctype_digit($height) === true) {
896                            $styles .= 'max-height:' . intval($height) . 'px;';
897                        } elseif (preg_match('/^\d+(px|rem|em|%)$/', $height) === 1) {
898                            $styles .= 'max-height:' . $height . ';';
899                        } elseif (strcasecmp($height, 'none') === 0) {
900                            $styles .= 'max-height:none;';
901                        }
902
903                        if (strlen($styles) > 0) {
904                            $styles = ' style="' . $styles . '"';
905                        }
906                    }//end if
907
908                    $html .= '<img src="' . $logo . '" class="mikio-navbar-brand-image"' . $styles . '>';
909                }//end if
910            }//end if
911
912            // Brand title
913            if ($this->getConf('navbarUseTitleText') === true) {
914                $html .= '<div class="mikio-navbar-brand-title">';
915                $html .= '<h1 class="mikio-navbar-brand-title-text">' . $conf['title'] . '</h1>';
916                if ($this->getConf('navbarUseTaglineText') === true) {
917                    $html .= '<p class="claim mikio-navbar-brand-title-tagline">' . $conf['tagline'] . '</p>';
918                }
919                $html .= '</div>';
920            }
921        }//end if
922        $html .= '</a>';
923        $html .= '<div class="mikio-navbar-toggle"><span class="icon"></span></div>';
924
925        // Menus
926        $html .= '<div class="mikio-navbar-collapse">';
927
928        $menus = [$this->getConf('navbarPosLeft', tpl_getLang('value_none')), $this->getConf('navbarPosMiddle', tpl_getLang('value_none')),
929            $this->getConf('navbarPosRight', tpl_getLang('value_none'))
930        ];
931
932        $value_custom = tpl_getLang('value_custom');
933        $value_search = tpl_getLang('value_search');
934        $value_dokuwiki = tpl_getLang('value_dokuwiki');
935
936        foreach ($menus as $menuType) {
937            switch ($menuType) {
938                case $value_custom:
939                    $html .= $this->stringToNav($this->getConf('navbarCustomMenuText', ''));
940                    break;
941                case $value_search:
942                    $html .= '<div class="mikio-nav-item">';
943                    $html .= $this->includeSearch(false);
944                    $html .= '</div>';
945                    break;
946                case $value_dokuwiki:
947                    $html .= $this->includeDWMenu(false);
948                    break;
949            }
950        }
951
952        $html .= '</div>';
953        $html .= '</div>';
954        $html .= '</nav>';
955
956        // Sub Navbar
957        if ($showSub === true) {
958            $sub = $this->includePage('submenu', false);
959            if (empty($sub) === false) {
960                $html .= '<nav class="mikio-navbar mikio-sub-navbar">' . $sub . '</nav>';
961            }
962        }
963
964        if ($print === true) {
965            echo $html;
966        }
967        return $html;
968    }
969
970
971    /**
972     * Is there a sidebar
973     *
974     * @param   string $prefix Sidebar prefix to use when searching.
975     * @return  boolean        if sidebar exists
976     */
977    public function sidebarExists(string $prefix = ''): bool
978    {
979        global $conf;
980
981        if (strcasecmp($prefix, 'left') === 0) {
982            $prefix = '';
983        }
984
985        return $this->pageExists($conf['sidebar' . $prefix]);
986    }
987
988
989    /**
990     * Print or return the sidebar content
991     *
992     * @param   string  $prefix Sidebar prefix to use when searching.
993     * @param   boolean $print  Print the generated content to the output buffer.
994     * @param   boolean $parse  Parse the content.
995     * @return  string          generated content
996     */
997    public function includeSidebar(string $prefix = '', bool $print = true, bool $parse = true): string
998    {
999        global $conf, $ID;
1000
1001        $html = '';
1002        $confPrefix = preg_replace('/[^a-zA-Z0-9]/', '', ucwords($prefix));
1003        $prefix = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($prefix));
1004
1005        if (empty($confPrefix) === true) {
1006            $confPrefix = 'Left';
1007        }
1008        if (strcasecmp($prefix, 'left') === 0) {
1009            $prefix = '';
1010        }
1011
1012        $sidebarPage = empty($conf[$prefix . 'sidebar']) === true ? $prefix . 'sidebar' : $conf[$prefix . 'sidebar'];
1013
1014        if (
1015            $this->getConf('sidebarShow' . $confPrefix) === true && page_findnearest($sidebarPage) !== false &&
1016            p_get_metadata($ID, 'nosidebar', false) === null
1017        ) {
1018            $content = $this->includePage($sidebarPage . 'header', false);
1019            if (empty($content) === false) {
1020                $html .= '<div class="mikio-sidebar-header">' . $content . '</div>';
1021            }
1022
1023            if (empty($prefix) === true) {
1024                $rows = [$this->getConf('sidebarLeftRow1'), $this->getConf('sidebarLeftRow2'),
1025                    $this->getConf('sidebarLeftRow3'), $this->getConf('sidebarLeftRow4')
1026                ];
1027
1028                $value_search = tpl_getLang('value_search');
1029                $value_logged_in_user = tpl_getLang('value_logged_in_user');
1030                $value_content = tpl_getLang('value_content');
1031                $value_tags = tpl_getLang('value_tags');
1032
1033                foreach ($rows as $row) {
1034                    switch ($row) {
1035                        case $value_search:
1036                            $html .= $this->includeSearch(false);
1037                            break;
1038                        case $value_logged_in_user:
1039                            $html .= $this->includeLoggedIn(false);
1040                            break;
1041                        case $value_content:
1042                            $content = $this->includePage($sidebarPage, false);
1043                            if (empty($content) === false) {
1044                                $html .= '<div class="mikio-sidebar-content">' . $content . '</div>';
1045                            }
1046                            break;
1047                        case $value_tags:
1048                            $html .= '<div class="mikio-tags"></div>';
1049                    }
1050                }
1051            } else {
1052                $content = $this->includePage($sidebarPage, false);
1053                if (empty($content) === false) {
1054                    $html .= '<div class="mikio-sidebar-content">' . $content . '</div>';
1055                }
1056            }//end if
1057
1058            $content = $this->includePage($sidebarPage . 'footer', false);
1059            if (empty($content) === false) {
1060                $html .= '<div class="mikio-sidebar-footer">' . $content . '</div>';
1061            }
1062        }//end if
1063
1064        if (empty($html) === true) {
1065            if (empty($prefix) === true && $this->getConf('sidebarAlwaysShowLeft') === true) {
1066                $html = '&nbsp;';
1067            }
1068            if ($this->getConf('sidebarAlwaysShow' . ucfirst($prefix)) === true) {
1069                $html = '&nbsp;';
1070            }
1071        }
1072
1073        if (empty($html) === false) {
1074            $sidebarClasses = [
1075                'mikio-sidebar',
1076                'mikio-sidebar-' . (empty($prefix) === true ? 'left' : $prefix)
1077            ];
1078
1079            $collapseClasses = ['mikio-sidebar-collapse'];
1080
1081            if(empty($prefix) === true && $this->getConf('stickyLeftSidebar') === true) {
1082                $collapseClasses[] = 'mikio-sidebar-sticky';
1083            }
1084
1085            $html = '<aside class="' . implode(' ', $sidebarClasses) . '"><a class="mikio-sidebar-toggle' .
1086                ($this->getConf('sidebarMobileDefaultCollapse') === true ? ' closed' : '') . '" href="#">' .
1087                tpl_getLang('sidebar-title') . ' <span class="icon"></span></a><div class="' . implode(' ', $collapseClasses) . '">' .
1088                $html . '</div></aside>';
1089        }
1090
1091        if ($parse === true) {
1092            $html = $this->includeIcons($html);
1093        }
1094        if ($print === true) {
1095            echo $html;
1096        }
1097
1098        return $html;
1099    }
1100
1101
1102    /**
1103     * Print or return the page tools content
1104     *
1105     * @param   boolean $print     Print the generated content to the output buffer.
1106     * @param   boolean $includeId Include the dw__pagetools id in the element.
1107     * @return  string             generated content
1108     */
1109    public function includePageTools(bool $print = true, bool $includeId = false): string
1110    {
1111        global $USERINFO;
1112
1113        $loggedIn = (is_array($USERINFO) === true && count($USERINFO) > 0);
1114
1115        $html = '<nav' . ($includeId === true ? ' id="dw__pagetools"' : '') . ' class="hidden-print dw__pagetools">';
1116        $html .= '<ul class="tools">';
1117
1118        $items = (new PageMenu())->getItems();
1119        foreach ($items as $item) {
1120            $classes = [];
1121            $classes[] = $item->getType();
1122            $attr = $item->getLinkAttributes();
1123
1124            if (!empty($attr['class'])) {
1125                $classes += explode(' ', $attr['class']);
1126            }
1127
1128            $classes = array_unique($classes);
1129            $title = isset($attr['title']) && $attr['title'] !== 0 ? $attr['title'] : $item->getTitle();
1130
1131            $showItem = $this->getConf('pageToolsShow' . ucfirst($item->getType()), tpl_getLang('value_always'));
1132            if (
1133                $showItem !== false && (strcasecmp($showItem, 'always') === 0 ||
1134                (strcasecmp($showItem, 'logged in') === 0 && $loggedIn === true) ||
1135                (strcasecmp($showItem, 'logged out') === 0 && $loggedIn === true))
1136            ) {
1137                $html .= '<li class="' . implode(' ', $classes) . '">';
1138                $html .= '<a href="' . $item->getLink() . '" class="' . $item->getType() . '" title="' .
1139                    $title . '"' . (isset($attr['accesskey']) && $attr['accesskey'] !== '' ? ' accesskey="' . $attr['accesskey'] . '"' : '') . '><div class="icon">' . inlineSVG($item->getSvg()) .
1140                    '</div><span class="a11y">' . $item->getLabel() . '</span></a>';
1141                $html .= '</li>';
1142            }
1143        }//end foreach
1144
1145        $html .= '</ul>';
1146        $html .= '</nav>';
1147
1148        if ($print === true) {
1149            echo $html;
1150        }
1151        return $html;
1152    }
1153
1154
1155    /**
1156     * Print or return the search bar
1157     *
1158     * @param   boolean $print Print content.
1159     * @return  string         contents of the search bar
1160     */
1161    public function includeSearch(bool $print = true): string
1162    {
1163        $html = $this->parseHTML('tpl_searchform', function($dom) {
1164            $forms = $dom->getElementsByTagName('form');
1165            if (0 !== count($forms)) {
1166                foreach ($forms as $form) {
1167                    $currentClasses = $form->getAttribute('class');
1168                    $newClasses = trim($currentClasses . ' mikio-search');
1169                    $form->setAttribute('class', $newClasses);
1170                }
1171            }
1172
1173            if ($this->getConf('searchUseTypeahead') === true) {
1174                $inputs = $dom->getElementsByTagName('input');
1175                foreach ($inputs as $input) {
1176                    if ($input->getAttribute('name') === 'q') {
1177                        $inputClasses = $input->getAttribute('class');
1178                        $inputNewClasses = trim($inputClasses . ' search_typeahead');
1179                        $input->setAttribute('class', $inputNewClasses);
1180                    }
1181                }
1182            }
1183
1184            if (strcasecmp($this->getConf('searchButton'), tpl_getLang('value_icon')) === 0) {
1185                $buttons = $dom->getElementsByTagName('button');
1186                foreach($buttons as $button) {
1187                    if($button->getAttribute('type') === 'submit') {
1188                        $icon = $this->iconAsDomElement($dom, 'search');
1189                        $button->nodeValue = '';
1190                        $button->appendChild($icon);
1191                    }
1192                }
1193            }
1194        });
1195
1196        if ($print === true) {
1197            echo $html;
1198        }
1199        return $html;
1200    }
1201
1202
1203    /**
1204     * Print or return content
1205     *
1206     * @param   boolean $print Print content.
1207     * @return  string         contents
1208     */
1209    public function includeContent(bool $print = true): string
1210    {
1211        ob_start();
1212        tpl_content(false);
1213        $html = ob_get_clean();
1214
1215        $html = $this->includeIcons($html);
1216        $html = $this->parseContent($html);
1217
1218        $html .= '<div style="clear:both"></div>';
1219
1220        if ($this->getConf('heroTitle') === false && $this->getConf('tagsShowHero') === true) {
1221            $html = '<div class="mikio-tags"></div>' . $html;
1222        }
1223
1224        $html = '<div class="mikio-article-content">' . $html . '</div>';
1225
1226        if ($print === true) {
1227            echo $html;
1228        }
1229        return $html;
1230    }
1231
1232    private function custom_tpl_pageinfo($ret = false)
1233    {
1234        global $conf;
1235        global $lang;
1236        global $INFO;
1237        global $ID;
1238
1239        // return if we are not allowed to view the page
1240        if (!auth_quickaclcheck($ID)) {
1241            return false;
1242        }
1243
1244        if (isset($INFO['exists'])) {
1245            $file = $INFO['filepath'];
1246            if (!$conf['fullpath']) {
1247                if ($INFO['rev']) {
1248                    $file = str_replace($conf['olddir'] . '/', '', $file);
1249                } else {
1250                    $file = str_replace($conf['datadir'] . '/', '', $file);
1251                }
1252            }
1253            $file = utf8_decodeFN($file);
1254            $date = dformat($INFO['lastmod']);
1255
1256            $string = $this->getConf('footerPageInfoText', '');
1257
1258            // replace lang items
1259            $string = preg_replace_callback('/%([^%]+)%/', static function ($matches) use ($lang) {
1260                return $lang[$matches[1]] ?? '';
1261            }, $string);
1262
1263            $options = [
1264                'file' => '<bdi>' . $file . '</bdi>',
1265                'date' => $date,
1266                'user' => $INFO['editor'] ? '<bdi>' . editorinfo($INFO['editor']) . '</bdi>' : $lang['external_edit']
1267            ];
1268
1269            if (!empty($_SERVER['REMOTE_USER'])) {
1270                $options['loggedin'] = true;
1271            }
1272
1273            if ($INFO['locked']) {
1274                $options['locked'] = '<bdi>' . editorinfo($INFO['locked']) . '</bdi>';
1275            }
1276
1277            $parser = new ParensParser();
1278            $result = $parser->parse($string);
1279
1280            $parserIterate = function ($arr, $func) use ($options) {
1281                $str = '';
1282
1283                foreach ($arr as $value) {
1284                    if (is_array($value)) {
1285                        $str .= $func($value, $func);
1286                    } else {
1287                        if (preg_match('/^([a-zA-Z]+)=(.*)/', $value, $matches)) {
1288                            $key = strtolower($matches[1]); // Extract the key (a-zA-Z part)
1289
1290                            if (isset($options[$key])) {
1291                                $str .= $matches[2];
1292                            } else {
1293                                return $str;
1294                            }
1295                        } else {
1296                            $str .= $value;
1297                        }
1298                    }
1299                }//end foreach
1300
1301                return $str;
1302            };
1303
1304            $string = $parserIterate($result, $parserIterate);
1305
1306            $string = preg_replace_callback('/{([^}]+)}/', static function ($matches) use ($options) {
1307                $key = strtolower($matches[1]);
1308                return $options[$key] ?? '';
1309            }, $string);
1310
1311            if ($ret) {
1312                return $string;
1313            }
1314
1315            echo $string;
1316            return true;
1317        }//end if
1318
1319        return false;
1320    }
1321
1322    /**
1323     * Print or return footer
1324     *
1325     * @param   boolean $print Print footer.
1326     * @return  string         HTML string containing footer
1327     */
1328    public function includeFooter(bool $print = true): string
1329    {
1330        global $ACT;
1331
1332        $html = '<footer class="mikio-footer">';
1333        $html .= '<div class="doc">' . $this->custom_tpl_pageinfo(true) . '</div>';
1334        $html .= $this->includePage('footer', false);
1335
1336        $html .= $this->stringToNav($this->getConf('footerCustomMenuText'));
1337
1338        if ($this->getConf('footerSearch') === true) {
1339            $html .= '<div class="mikio-footer-search">';
1340            $html .= $this->includeSearch(false);
1341            $html .= '</div>';
1342        }
1343
1344        $showPageTools = $this->getConf('pageToolsFooter');
1345        if (
1346            !is_null($ACT) && !is_null($showPageTools) &&
1347            strcasecmp($ACT, 'show') === 0 && (strcasecmp($showPageTools, tpl_getLang('value_always')) === 0 ||
1348                ($this->userCanEdit() === true && strcasecmp($showPageTools, tpl_getLang('value_page_editors')) === 0))
1349        ) {
1350            $html .= $this->includePageTools(false);
1351        }
1352
1353        $meta['licenseType']            = ['multichoice', '_choices' => [tpl_getLang('value_none'), tpl_getLang('value_badge'), tpl_getLang('value_button')]];
1354        /** @noinspection PhpArrayWriteIsNotUsedInspection */
1355        $meta['licenseImageOnly']       = ['onoff'];
1356
1357        $licenseType = $this->getConf('licenseType');
1358        if ($licenseType !== 'none') {
1359            $html .= tpl_license($licenseType, $this->getConf('licenseImageOnly'), true);
1360        }
1361
1362        $html .= '</footer>';
1363
1364        if ($print === true) {
1365            echo $html;
1366        }
1367        return $html;
1368    }
1369
1370
1371    /**
1372     * Print or return breadcrumb trail
1373     *
1374     * @param   boolean $print Print out trail.
1375     * @param   boolean $parse Parse trail before printing.
1376     * @return  string         HTML string containing breadcrumbs
1377     */
1378    public function includeBreadcrumbs(bool $print = true, bool $parse = true): string
1379    {
1380        global $conf, $ID, $lang, $ACT;
1381
1382        if (
1383            ($this->getConf('breadcrumbHideHome') === true && strcasecmp($ID, 'start') === 0 &&
1384                strcasecmp($ACT, 'show') === 0) || strcasecmp($ACT, 'showtag') === 0 || $conf['breadcrumbs'] === 0
1385        ) {
1386            return '';
1387        }
1388
1389        $html = '<div class="mikio-breadcrumbs">';
1390        $html .= '<div class="mikio-container">';
1391        if (strcasecmp($ACT, 'show') === 0) {
1392            if ($this->getConf('breadcrumbPrefix') === false && $this->getConf('breadcrumbSep') === false) {
1393                ob_start();
1394                tpl_breadcrumbs();
1395                $html .= ob_get_clean();
1396            } else {
1397                $sep = '•';
1398                $prefix = $lang['breadcrumb'];
1399
1400                if ($this->getConf('breadcrumbSep') === true) {
1401                    $sep = $this->getConf('breadcrumbSepText');
1402                    $img = $this->getMediaFile('breadcrumb-sep', false);
1403
1404                    if ($img !== false) {
1405                        $sep = '<img src="' . $img . '">';
1406                    }
1407                }
1408
1409                if ($this->getConf('breadcrumbPrefix') === true) {
1410                    $prefix = $this->getConf('breadcrumbPrefixText');
1411                    $img = $this->getMediaFile('breadcrumb-prefix', false);
1412
1413                    if ($img !== false) {
1414                        $prefix = '<img src="' . $img . '">';
1415                    }
1416                }
1417
1418                $crumbs = breadcrumbs();
1419
1420                $html .= '<ul>';
1421                if (empty($prefix) === false) {
1422                    $html .= '<li class="prefix">' . $prefix . '</li>';
1423                }
1424
1425                $last = count($crumbs);
1426                $i    = 0;
1427                foreach ($crumbs as $id => $name) {
1428                    $i++;
1429                    if ($i !== 1) {
1430                        $html .= '<li class="sep">' . $sep . '</li>';
1431                    }
1432                    $html .= '<li' . ($i === $last ? ' class="curid"' : '') . '>';
1433                    $html .= tpl_pagelink($id, null, true);
1434                    $html .= '</li>';
1435                }
1436
1437                $html .= '</ul>';
1438            }//end if
1439        }//end if
1440
1441        $html .= '</div>';
1442        $html .= '</div>';
1443
1444        if ($parse === true) {
1445            $html = $this->includeIcons($html);
1446        }
1447        if ($print === true) {
1448            echo $html;
1449        }
1450        return $html;
1451    }
1452
1453    /**
1454     * Print or return you are here trail
1455     *
1456     * @param   boolean $print Print out trail.
1457     * @param   boolean $parse Parse trail before printing.
1458     * @return  string         HTML string containing breadcrumbs
1459     */
1460    public function includeYouAreHere(bool $print = true, bool $parse = true): string
1461    {
1462        global $conf, $ID, $lang, $ACT;
1463
1464        if (
1465            ($this->getConf('youarehereHideHome') === true && strcasecmp($ID, 'start') === 0 &&
1466                strcasecmp($ACT, 'show') === 0) || strcasecmp($ACT, 'showtag') === 0 || $conf['youarehere'] === 0
1467        ) {
1468            return '';
1469        }
1470
1471        $html = '<div class="mikio-youarehere">';
1472        $html .= '<div class="mikio-container">';
1473        if (strcasecmp($ACT, 'show') === 0) {
1474            if ($this->getConf('youareherePrefix') === false && $this->getConf('youarehereSep') === false) {
1475                $html .= '<div class="mikio-bcdw">';
1476                ob_start();
1477                tpl_youarehere();
1478                $html .= ob_get_clean();
1479                $html .= '</div>';
1480            } else {
1481                $sep = ' » ';
1482                $prefix = $lang['youarehere'];
1483
1484                if ($this->getConf('youarehereSep') === true) {
1485                    $sep = $this->getConf('youarehereSepText');
1486                    $img = $this->getMediaFile('youarehere-sep', false);
1487
1488                    if ($img !== false) {
1489                        $sep = '<img src="' . $img . '">';
1490                    }
1491                }
1492
1493                if ($this->getConf('youareherePrefix') === true) {
1494                    $prefix = $this->getConf('youareherePrefixText');
1495                    $img = $this->getMediaFile('youarehere-prefix', false);
1496
1497                    if ($img !== false) {
1498                        $prefix = '<img src="' . $img . '">';
1499                    }
1500                }
1501
1502                $html .= '<ul>';
1503                if (empty($prefix) === false) {
1504                    $html .= '<li class="prefix">' . $prefix . '</li>';
1505                }
1506                $html .= '<li>' . tpl_pagelink(':' . $conf['start'], null, true) . '</li>';
1507
1508                $parts = explode(':', $ID);
1509                $count = count($parts);
1510
1511                $part = '';
1512                for ($i = 0; $i < ($count - 1); $i++) {
1513                    $part .= $parts[$i] . ':';
1514                    $page = $part;
1515                    if ($page === $conf['start']) {
1516                        continue;
1517                    }
1518
1519                    $html .= '<li class="sep">' . $sep . '</li>';
1520                    $html .= '<li>' . tpl_pagelink($page, null, true) . '</li>';
1521                }
1522
1523                $page = '';
1524
1525                if ($this->getDokuWikiVersion() >= 20200729) {
1526                    $page = cleanID($page);
1527                } else {
1528                    $exists = false;
1529                    /** @noinspection PhpDeprecationInspection */
1530                    resolve_pageid('', $page, $exists);
1531                }
1532
1533                if ((isset($page) === true && $page === $part . $parts[$i]) === false) {
1534                    $page = $part . $parts[$i];
1535                    if ($page !== $conf['start']) {
1536                        $html .= '<li class="sep">' . $sep . '</li>';
1537                        $html .= '<li>' . tpl_pagelink($page, null, true) . '</li>';
1538                    }
1539                }
1540
1541                $html .= '</ul>';
1542            }//end if
1543
1544            $showLast = $this->getConf('youarehereShowLast');
1545            if ($showLast !== 0) {
1546                preg_match_all('/(<li[^>]*>.+?<\/li>)/', $html, $matches);
1547                if (count($matches) > 0 && count($matches[0]) > (($showLast * 2) + 2)) {
1548                    $count = count($matches[0]);
1549                    $list = '';
1550
1551                    // Show Home
1552                    $list .= $matches[0][0] . $matches[0][1];
1553
1554                    $list .= '<li>...</li>';
1555                    for ($i = ($count - ($showLast * 2)); $i <= $count; $i++) {
1556                        $list .= $matches[0][$i];
1557                    }
1558
1559                    $html = preg_replace('/<ul>.*<\/ul>/', '<ul>' . $list . '</ul>', $html);
1560                }
1561            }
1562
1563            $value_none = tpl_getLang('value_none');
1564            $value_home = tpl_getLang('value_home');
1565            $value_icon = tpl_getLang('value_icon');
1566
1567            switch ($this->getConf('youarehereHome')) {
1568                case $value_none:
1569                    $html = preg_replace('/<li[^>]*>.+?<\/li>/', '', $html, 2);
1570                    break;
1571                case $value_home:
1572                    $html = preg_replace('/(<a[^>]*>)(.+?)(<\/a>)/', '$1' . tpl_getlang('home') . '$3', $html, 1);
1573                    break;
1574                case $value_icon:
1575                    $html = preg_replace('/(<a[^>]*>)(.+?)(<\/a>)/', '$1' .
1576                        $this->mikioInlineIcon('home') . '$3', $html, 1);
1577                    break;
1578            }
1579        } else {
1580            $html .= '&#8810; ';
1581            if (isset($_GET['page']) === true) {
1582                $html .= '<a href="' . wl($ID, ['do' => $ACT]) . '">Back</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;';
1583            }
1584            $html .= '<a href="' . wl($ID) . '">View Page</a>';
1585        }//end if
1586
1587        $html .= '</div>';
1588        $html .= '</div>';
1589
1590        if ($parse === true) {
1591            $html = $this->includeIcons($html);
1592        }
1593        if ($print === true) {
1594            echo $html;
1595        }
1596        return $html;
1597    }
1598
1599    /**
1600     * Get Page Title
1601     *
1602     * @return string page title
1603     */
1604    public function parsePageTitle(): string
1605    {
1606        global $ID;
1607
1608        $title = p_get_first_heading($ID);
1609        if (strlen($title) <= 0) {
1610            $title = tpl_pagetitle(null, true);
1611        }
1612        return $this->includeIcons($title);
1613    }
1614
1615
1616    /**
1617     * Print or return hero block
1618     *
1619     * @param   boolean $print Print content.
1620     * @return  string         contents of hero
1621     */
1622    public function includeHero(bool $print = true): string
1623    {
1624        $html = '';
1625
1626        if ($this->getConf('heroTitle') === true) {
1627            $html .= '<div class="mikio-hero">';
1628            $html .= '<div class="mikio-container">';
1629            $html .= '<div class="mikio-hero-text">';
1630            if (strcasecmp($this->getConf('youareherePosition'), tpl_getLang('value_hero')) === 0) {
1631                $html .= $this->includeYouAreHere(false);
1632            }
1633            if (strcasecmp($this->getConf('breadcrumbPosition'), tpl_getLang('value_hero')) === 0) {
1634                $html .= $this->includeBreadcrumbs(false);
1635            }
1636
1637            $html .= '<h1 class="mikio-hero-title">';
1638            $html .= $this->parsePageTitle();    // No idea why this requires a blank space afterward to work?
1639            $html .= '</h1>';
1640            $html .= '<h2 class="mikio-hero-subtitle"></h2>';
1641            $html .= '</div>';
1642
1643            $hero_image = $this->getMediaFile('hero', true, $this->getConf('heroImagePropagation', true));
1644            $hero_image_resize_class = '';
1645            if (empty($hero_image) === false) {
1646                $hero_image = ' style="background-image:url(\'' . $hero_image . '\');"';
1647                $hero_image_resize_class = ' mikio-hero-image-resize';
1648            }
1649
1650            $html .= '<div class="mikio-hero-image' . $hero_image_resize_class . '"' . $hero_image .
1651                '>';
1652
1653            if($this->getConf('tagsShowHero') === true) {
1654                $html .= '<div class="mikio-tags"></div>';
1655            }
1656
1657            $html .= '</div>';
1658
1659            $html .= '</div>';
1660            $html .= '</div>';
1661        }//end if
1662
1663        if ($print === true) {
1664            echo $html;
1665        }
1666
1667        return $html;
1668    }
1669
1670
1671    /**
1672     * Print or return out TOC
1673     *
1674     * @param   boolean $print Print TOC.
1675     * @param   boolean $parse Parse icons.
1676     * @return  string         contents of TOC
1677     */
1678    public function includeTOC(bool $print = true, bool $parse = true): string
1679    {
1680        $html = '';
1681
1682        $tocHtml = tpl_toc(true);
1683
1684        if (empty($tocHtml) === false) {
1685            $tocHtml = preg_replace(
1686                '/(<h3.+?toggle.+?>)(.+?)<\/h3>/',
1687                '$1' .
1688                $this->mikioInlineIcon('hamburger', 'hamburger') . '$2' .
1689                $this->mikioInlineIcon('down-arrow', 'down-arrow') . '</h3>',
1690                $tocHtml
1691            );
1692            $tocHtml = preg_replace('/<li.*><div.*><a.*><\/a><\/div><\/li>\s*/', '', $tocHtml);
1693            $tocHtml = preg_replace('/<ul.*>\s*<\/ul>\s*/', '', $tocHtml);
1694
1695            $html .= '<div class="mikio-toc">';
1696            $html .= $tocHtml;
1697            $html .= '</div>';
1698        }
1699
1700        if ($parse === true) {
1701            $html = $this->includeIcons($html);
1702        }
1703
1704        if ($print === true) {
1705            echo $html;
1706        }
1707
1708        return $html;
1709    }
1710
1711
1712    /**
1713     * Parse the string and replace icon elements with included icon libraries
1714     *
1715     * @param   string $str Content to parse.
1716     * @return  string      parsed string
1717     */
1718    public function includeIcons(string $str): string
1719    {
1720        global $ACT, $MIKIO_ICONS;
1721
1722        $iconTag = $this->getConf('iconTag', 'icon');
1723        if (empty($iconTag) === true) {
1724            return $str;
1725        }
1726
1727        if (
1728            in_array($ACT, ['show', 'showtag', 'revisions', 'index', 'preview']) === true ||
1729            (strcasecmp($ACT, 'admin') === 0 && count($MIKIO_ICONS) > 0)
1730        ) {
1731            $content = $str;
1732            $preview = null;
1733
1734            $html = null;
1735            if (strcasecmp($ACT, 'preview') === 0) {
1736                $html = new simple_html_dom();
1737                $html->stripRNAttrValues = false;
1738                $html->load($str, true, false);
1739
1740                $preview = $html->find('div.preview');
1741                if (is_array($preview) === true && count($preview) > 0) {
1742                    $content = $preview[0]->innertext;
1743                }
1744            }
1745
1746            $page_regex = '/(.*)/';
1747            if (stripos($str, '<pre') !== false) {
1748                $page_regex = '/<(?!pre|\/).*?>(.*)[^<]*/';
1749            }
1750
1751            $content = preg_replace_callback($page_regex, function ($icons) {
1752                $iconTag = $this->getConf('iconTag', 'icon');
1753
1754                return preg_replace_callback(
1755                    '/&lt;' . $iconTag . ' ([\w\- #]*)&gt;(?=[^>]*(<|$))/',
1756                    function ($matches) {
1757                        global $MIKIO_ICONS;
1758
1759                        $s = $matches[0];
1760
1761                        if (count($MIKIO_ICONS) > 0) {
1762                            $icon = $MIKIO_ICONS[0];
1763
1764                            if (count($matches) > 1) {
1765                                $e = explode(' ', $matches[1]);
1766
1767                                if (count($e) > 1) {
1768                                    foreach ($MIKIO_ICONS as $iconItem) {
1769                                        if (strcasecmp($iconItem['name'], $e[0]) === 0) {
1770                                            $icon = $iconItem;
1771
1772                                            $s = $icon['insert'];
1773                                            for ($i = 1; $i < 9; $i++) {
1774                                                if (count($e) < $i || empty($e[$i]) === true) {
1775                                                    if (isset($icon['$' . $i]) === true) {
1776                                                        $s = str_replace('$' . $i, $icon['$' . $i], $s);
1777                                                    }
1778                                                } else {
1779                                                    $s = str_replace('$' . $i, $e[$i], $s);
1780                                                }
1781                                            }
1782
1783                                            $dir = '';
1784                                            if (isset($icon['dir']) === true) {
1785                                                $dir = $this->baseDir . 'icons/' . $icon['dir'] . '/';
1786                                            }
1787
1788                                            $s = str_replace('$0', $dir, $s);
1789
1790                                            break;
1791                                        }//end if
1792                                    }//end foreach
1793                                } else {
1794                                    $s = str_replace('$1', $matches[1], $icon['insert']);
1795                                }//end if
1796                            }//end if
1797                        }//end if
1798
1799                        $s = preg_replace('/(class=")(.*)"/', '$1mikio-icon $2"', $s, -1, $count);
1800                        if ($count === 0) {
1801                            $s = preg_replace('/(<\w* )/', '$1class="mikio-icon" ', $s);
1802                        }
1803
1804                        return $s;
1805                    },
1806                    $icons[0]
1807                );
1808            }, $content);
1809
1810            if (strcasecmp($ACT, 'preview') === 0) {
1811                if (is_array($preview) === true && count($preview) > 0) {
1812                    $preview[0]->innertext = $content;
1813                }
1814
1815                $str = $html->save();
1816                $html->clear();
1817                unset($html);
1818            } else {
1819                $str = $content;
1820            }
1821        }//end if
1822
1823        return $str;
1824    }
1825
1826    /**
1827     * Parse HTML for theme
1828     *
1829     * @param   string $content HTML content to parse.
1830     * @return  string          Parsed content
1831     */
1832    public function parseContent(string $content): string
1833    {
1834        global $INPUT, $ACT;
1835
1836        // Add Mikio Section titles
1837        if (strcasecmp($INPUT->str('page'), 'config') === 0) {
1838            $admin_sections = [
1839                // Section      Insert Before                 Icon
1840                'navbar'        => ['navbarUseTitleIcon',      ''],
1841                'search'        => ['searchButton',            ''],
1842                'hero'          => ['heroTitle',               ''],
1843                'tags'          => ['tagsConsolidate',         ''],
1844                'breadcrumb'    => ['breadcrumbHideHome',      ''],
1845                'youarehere'    => ['youarehereHideHome',      ''],
1846                'sidebar'       => ['sidebarShowLeft',         ''],
1847                'toc'           => ['tocFull',                 ''],
1848                'pagetools'     => ['pageToolsFloating',       ''],
1849                'footer'        => ['footerPageInfoText',      ''],
1850                'license'       => ['licenseType',             ''],
1851                'acl'           => ['includePageUseACL',       ''],
1852                'sticky'        => ['stickyTopHeader',         ''],
1853            ];
1854
1855            foreach ($admin_sections as $section => $items) {
1856                $search = $items[0];
1857                $icon   = $items[1];
1858
1859                $content = preg_replace(
1860                    '/<tr(.*)>\s*<td class="label">\s*<span class="outkey">(tpl»mikio»' . $search . ')<\/span>/',
1861                    '<tr$1><td class="mikio-config-table-header" colspan="2">' . $this->mikioInlineIcon($icon) .
1862                        tpl_getLang('config_' . $section) .
1863                        '</td></tr><tr class="default"><td class="label"><span class="outkey">tpl»mikio»' .
1864                        $search . '</span>',
1865                    $content
1866                );
1867            }
1868        } elseif (strcasecmp($INPUT->str('page'), 'styling') === 0) {
1869            $mikioPluginMissing = true;
1870            /* Hide plugin fields if not installed */
1871            if (plugin_load('action', 'mikioplugin') !== null) {
1872                $mikioPluginMissing = false;
1873            }
1874
1875            $style_headers = [
1876                ['title' => 'Base', 'starts_with' => '__text_'],
1877                ['title' => 'Code', 'starts_with' => '__code_'],
1878                ['title' => 'Controls', 'starts_with' => '__control_'],
1879                ['title' => 'Header', 'starts_with' => '__topheader_'],
1880                ['title' => 'Navbar', 'starts_with' => '__navbar_'],
1881                ['title' => 'Sub Navbar', 'starts_with' => '__subnavbar_'],
1882                ['title' => 'Tags', 'starts_with' => '__tag_background_color_'],
1883                ['title' => 'Breadcrumbs', 'starts_with' => '__breadcrumb_'],
1884                ['title' => 'Hero', 'starts_with' => '__hero_'],
1885                ['title' => 'Sidebar', 'starts_with' => '__sidebar_'],
1886                ['title' => 'Content', 'starts_with' => '__content_'],
1887                ['title' => 'TOC', 'starts_with' => '__toc_'],
1888                ['title' => 'Page Tools', 'starts_with' => '__pagetools_'],
1889                ['title' => 'Footer', 'starts_with' => '__footer_'],
1890                ['title' => 'Table', 'starts_with' => '__table_'],
1891                ['title' => 'Dropdown', 'starts_with' => '__dropdown_'],
1892                ['title' => 'Section Edit', 'starts_with' => '__section_edit_'],
1893                ['title' => 'Tree', 'starts_with' => '__tree_'],
1894                ['title' => 'Tabs', 'starts_with' => '__tab_'],
1895                ['title' => 'Mikio Plugin', 'starts_with' => '__plugin_', 'heading' => 'h2',
1896                    'hidden' => $mikioPluginMissing
1897                ],
1898                ['title' => 'Primary Colours', 'starts_with' => '__plugin_primary_', 'hidden' => $mikioPluginMissing],
1899                ['title' => 'Secondary Colours', 'starts_with' => '__plugin_secondary_',
1900                    'hidden' => $mikioPluginMissing
1901                ],
1902                ['title' => 'Success Colours', 'starts_with' => '__plugin_success_', 'hidden' => $mikioPluginMissing],
1903                ['title' => 'Danger Colours', 'starts_with' => '__plugin_danger_', 'hidden' => $mikioPluginMissing],
1904                ['title' => 'Warning Colours', 'starts_with' => '__plugin_warning_', 'hidden' => $mikioPluginMissing],
1905                ['title' => 'Info Colours', 'starts_with' => '__plugin_info_', 'hidden' => $mikioPluginMissing],
1906                ['title' => 'Light Colours', 'starts_with' => '__plugin_light_', 'hidden' => $mikioPluginMissing],
1907                ['title' => 'Dark Colours', 'starts_with' => '__plugin_dark_', 'hidden' => $mikioPluginMissing],
1908                ['title' => 'Link Colours', 'starts_with' => '__plugin_link_', 'hidden' => $mikioPluginMissing],
1909                ['title' => 'Carousel', 'starts_with' => '__plugin_carousel_', 'hidden' => $mikioPluginMissing],
1910                ['title' => 'Steps', 'starts_with' => '__plugin_steps_', 'hidden' => $mikioPluginMissing],
1911                ['title' => 'Tabgroup', 'starts_with' => '__plugin_tabgroup_', 'hidden' => $mikioPluginMissing],
1912                ['title' => 'Tooltip', 'starts_with' => '__plugin_tooltip_', 'hidden' => $mikioPluginMissing],
1913                ['title' => 'Dark Mode', 'starts_with' => '__darkmode_', 'heading' => 'h2'],
1914                ['title' => 'Base', 'starts_with' => '__darkmode_text_'],
1915                ['title' => 'Code', 'starts_with' => '__darkmode_code_'],
1916                ['title' => 'Controls', 'starts_with' => '__darkmode_control_'],
1917                ['title' => 'Header', 'starts_with' => '__darkmode_topheader_'],
1918                ['title' => 'Navbar', 'starts_with' => '__darkmode_navbar_'],
1919                ['title' => 'Sub Navbar', 'starts_with' => '__darkmode_subnavbar_'],
1920                ['title' => 'Tags', 'starts_with' => '__darkmode_tag_background_color_'],
1921                ['title' => 'Breadcrumbs', 'starts_with' => '__darkmode_breadcrumb_'],
1922                ['title' => 'Hero', 'starts_with' => '__darkmode_hero_'],
1923                ['title' => 'Sidebar', 'starts_with' => '__darkmode_sidebar_'],
1924                ['title' => 'Content', 'starts_with' => '__darkmode_content_'],
1925                ['title' => 'TOC', 'starts_with' => '__darkmode_toc_'],
1926                ['title' => 'Page Tools', 'starts_with' => '__darkmode_pagetools_'],
1927                ['title' => 'Footer', 'starts_with' => '__darkmode_footer_'],
1928                ['title' => 'Table', 'starts_with' => '__darkmode_table_'],
1929                ['title' => 'Dropdown', 'starts_with' => '__darkmode_dropdown_'],
1930                ['title' => 'Section Edit', 'starts_with' => '__darkmode_section_edit_'],
1931                ['title' => 'Tree', 'starts_with' => '__darkmode_tree_'],
1932                ['title' => 'Tabs', 'starts_with' => '__darkmode_tab_'],
1933                ['title' => 'Mikio Plugin (Dark mode)', 'starts_with' => '__plugin_darkmode_', 'heading' => 'h2',
1934                    'hidden' => $mikioPluginMissing
1935                ],
1936                ['title' => 'Primary Colours', 'starts_with' => '__plugin_darkmode_primary_',
1937                    'hidden' => $mikioPluginMissing
1938                ],
1939                ['title' => 'Secondary Colours', 'starts_with' => '__plugin_darkmode_secondary_',
1940                    'hidden' => $mikioPluginMissing
1941                ],
1942                ['title' => 'Success Colours', 'starts_with' => '__plugin_darkmode_success_',
1943                    'hidden' => $mikioPluginMissing
1944                ],
1945                ['title' => 'Danger Colours', 'starts_with' => '__plugin_darkmode_danger_',
1946                    'hidden' => $mikioPluginMissing
1947                ],
1948                ['title' => 'Warning Colours', 'starts_with' => '__plugin_darkmode_warning_',
1949                    'hidden' => $mikioPluginMissing
1950                ],
1951                ['title' => 'Info Colours', 'starts_with' => '__plugin_darkmode_info_',
1952                    'hidden' => $mikioPluginMissing
1953                ],
1954                ['title' => 'Light Colours', 'starts_with' => '__plugin_darkmode_light_',
1955                    'hidden' => $mikioPluginMissing
1956                ],
1957                ['title' => 'Dark Colours', 'starts_with' => '__plugin_darkmode_dark_',
1958                    'hidden' => $mikioPluginMissing
1959                ],
1960                ['title' => 'Link Colours', 'starts_with' => '__plugin_darkmode_link_',
1961                    'hidden' => $mikioPluginMissing
1962                ],
1963                ['title' => 'Carousel', 'starts_with' => '__plugin_darkmode_carousel_',
1964                    'hidden' => $mikioPluginMissing
1965                ],
1966                ['title' => 'Steps', 'starts_with' => '__plugin_darkmode_steps_', 'hidden' => $mikioPluginMissing],
1967                ['title' => 'Tabgroup', 'starts_with' => '__plugin_darkmode_tabgroup_',
1968                    'hidden' => $mikioPluginMissing
1969                ],
1970                ['title' => 'Tooltip', 'starts_with' => '__plugin_darkmode_tooltip_', 'hidden' => $mikioPluginMissing],
1971            ];
1972
1973            foreach ($style_headers as $header) {
1974                if (array_key_exists('heading', $header) === false) {
1975                    $header['heading'] = 'h3';
1976                }
1977
1978                if (array_key_exists('hidden', $header) === false) {
1979                    $header['hidden'] = false;
1980                }
1981
1982                $content = preg_replace(
1983                    '/(<tr>\s*<td>\s*<label for="tpl__' . $header['starts_with'] . '.+?<\/tr>)/s',
1984                    '</tbody></table><' . $header['heading'] . ' style="display:' .
1985                    ($header['hidden'] === true ? 'none' : 'block') . '">' .
1986                    $header['title'] . '</' . $header['heading'] . '>
1987                    <table style="display:' . ($header['hidden'] === true ? 'none' : 'table') . '"><tbody>$1',
1988                    $content,
1989                    1
1990                );
1991            }
1992
1993            $content = preg_replace_callback('/<input type="color"[^>]*>/', function ($match) {
1994                // Get the ID of the <input type="color"> element
1995                preg_match('/id="([^"]*)"/', $match[0]);
1996
1997                // Replace type with text and remove the id attribute
1998                $replacement = preg_replace(
1999                    ['/type="color"/', '/id="([^"]*)"/'],
2000                    ['type="text" class="mikio-color-text-input"', 'for="$1"'],
2001                    $match[0]
2002                );
2003
2004                return '<div class="mikio-color-picker">' . $replacement . $match[0] . '</div>';
2005            }, $content);
2006        }//end if
2007
2008        if (strcasecmp($ACT, 'admin') === 0 && isset($_GET['page']) === false) {
2009            $content = preg_replace('/(<ul.*?>.*?)<\/ul>.*?<ul.*?>(.*?<\/ul>)/s', '$1$2', $content);
2010        }
2011
2012        // Page Revisions - Table Fix
2013        if (strpos($content, 'id="page__revisions"') !== false) {
2014            $content = preg_replace(
2015                '/(<span class="sum">\s.*<\/span>\s.*<span class="user">\s.*<\/span>)/',
2016                '<span>$1</span>',
2017                $content
2018            );
2019        }
2020
2021        $html = new simple_html_dom();
2022        $html->stripRNAttrValues = false;
2023        $html->load($content, true, false);
2024
2025        /* Buttons */
2026        foreach ($html->find('#config__manager button') as $node) {
2027            $c = explode(' ', $node->class);
2028            if (in_array('mikio-button', $c) === false) {
2029                $c[] = 'mikio-button';
2030            }
2031            $node->class = implode(' ', $c);
2032        }
2033
2034
2035        /* Buttons - Primary */
2036        foreach ($html->find('#config__manager [type=submit]') as $node) {
2037            $c = explode(' ', $node->class);
2038            if (in_array('mikio-primary', $c) === false) {
2039                $c[] = 'mikio-primary';
2040            }
2041            $node->class = implode(' ', $c);
2042        }
2043
2044        /* Hide page title if hero is enabled */
2045        if ($this->getConf('heroTitle') === true && $ACT !== 'preview') {
2046            $pageTitle = $this->parsePageTitle();
2047
2048            foreach ($html->find('h1,h2,h3,h4') as $elm) {
2049                if ($elm->innertext === $pageTitle) {
2050                    // $elm->innertext = '';
2051                    $elm->setAttribute('style', 'display:none');
2052
2053                    break;
2054                }
2055            }
2056        }
2057
2058        /* Hero subtitle */
2059        foreach ($html->find('p') as $elm) {
2060            if (preg_match('/[~-]~hero-subtitle (.+?)~[~-]/ui', $elm->innertext, $matches) === 1) {
2061                $subtitle = $matches[1];
2062                $this->footerScript['hero-subtitle'] = 'mikio.setHeroSubTitle(\'' . $subtitle . '\')';
2063
2064                $elm->innertext = preg_replace('/[~-]~hero-subtitle (.+?)~[~-]/ui', '', $elm->innertext);
2065                break;
2066            }
2067        }
2068
2069        /* Hero image */
2070        foreach ($html->find('p') as $elm) {
2071            preg_match('/[~-]~hero-image (.+?)~[~-](?!.?")/ui', $elm->innertext, $matches);
2072            if (count($matches) > 0) {
2073                preg_match('/<img.*src="(.+?)"/ui', $matches[1], $imageTagMatches);
2074                if (count($imageTagMatches) > 0) {
2075                    $image = $imageTagMatches[1];
2076                } else {
2077                    preg_match('/<a.+?>(.+?)[~<]/ui', $matches[1], $imageTagMatches);
2078                    if (count($imageTagMatches) > 0) {
2079                        $image = $imageTagMatches[1];
2080                    } else {
2081                        $image = strip_tags($matches[1]);
2082                        if (stripos($image, ':') === false) {
2083                            $image = str_replace(['{', '}'], '', $image);
2084                            $i = stripos($image, '?');
2085                            if ($i !== false) {
2086                                $image = substr($image, 0, $i);
2087                            }
2088
2089                            $image = ml($image, '', true, '');
2090                        }
2091                    }
2092                }
2093
2094                $this->footerScript['hero-image'] = 'mikio.setHeroImage(\'' . $image . '\')';
2095
2096                $elm->innertext = preg_replace('/[~-]~hero-image (.+?)~[~-].*/ui', '', $elm->innertext);
2097            }//end if
2098        }//end foreach
2099
2100        /* Hero colors - ~~hero-colors [background-color] [hero-title-color] [hero-subtitle-color]
2101        [breadcrumb-text-color] [breadcrumb-hover-color] (use 'initial' for original color) */
2102        foreach ($html->find('p') as $elm) {
2103            if (preg_match('/[~-]~hero-colors (.+?)~[~-]/ui', $elm->innertext, $matches) === 1) {
2104                $subtitle = $matches[1];
2105                $this->footerScript['hero-colors'] = 'mikio.setHeroColor(\'' . $subtitle . '\')';
2106
2107                $elm->innertext = preg_replace('/[~-]~hero-colors (.+?)~[~-]/ui', '', $elm->innertext);
2108                break;
2109            }
2110        }
2111
2112        /* Hide parts - ~~hide-parts [parts]~~  */
2113        foreach ($html->find('p') as $elm) {
2114            if (preg_match('/[~-]~hide-parts (.+?)~[~-]/ui', $elm->innertext, $matches) === 1) {
2115                $parts = explode(' ', $matches[1]);
2116                $script = '';
2117
2118                foreach ($parts as $part) {
2119                    if (strlen($part) > 0) {
2120                        $script .= 'mikio.hidePart(\'' . $part . '\');';
2121                    }
2122                }
2123
2124                if (strlen($script) > 0) {
2125                    $this->footerScript['hide-parts'] = $script;
2126                }
2127
2128                $elm->innertext = preg_replace('/[~-]~hide-parts (.+?)~[~-]/ui', '', $elm->innertext);
2129                break;
2130            }
2131        }//end foreach
2132
2133
2134        /* Page Tags (tag plugin) */
2135        if ($this->getConf('tagsConsolidate') === true) {
2136            $tags = '';
2137            foreach ($html->find('div.tags a') as $elm) {
2138                $tags .= $elm->outertext;
2139            }
2140
2141            foreach ($html->find('div.tags') as $elm) {
2142                $elm->innertext = '';
2143                $elm->setAttribute('style', 'display:none');
2144            }
2145
2146            if (empty($tags) === false) {
2147                $this->footerScript['tags'] = 'mikio.setTags(\'' . $tags . '\')';
2148            }
2149        }
2150
2151        // Configuration Manager
2152        if (strcasecmp($INPUT->str('page'), 'config') === 0) {
2153            // Additional save buttons
2154            foreach ($html->find('#config__manager') as $cm) {
2155                $saveButtons = '';
2156
2157                foreach ($cm->find('p') as $elm) {
2158                    $saveButtons = $elm->outertext;
2159                    $saveButtons = str_replace('<p>', '<p style="text-align:right">', $saveButtons);
2160                    $elm->outertext = '';
2161                }
2162
2163                foreach ($cm->find('fieldset') as $elm) {
2164                    $elm->innertext .= $saveButtons;
2165                }
2166            }
2167        }
2168
2169        $content = $html->save();
2170        $html->clear();
2171        unset($html);
2172
2173        return $content;
2174    }
2175
2176
2177    /**
2178     * Get DokuWiki namespace/page/URI as link
2179     *
2180     * @param   string $str String to parse.
2181     * @return  string      parsed URI
2182     */
2183    public function getLink(string $str): string
2184    {
2185        $i = strpos($str, '://');
2186        if ($i !== false) {
2187            return $str;
2188        }
2189
2190        return wl($str);
2191    }
2192
2193
2194    /**
2195     * Check if the user can edit current namespace/page
2196     *
2197     * @return  boolean  user can edit
2198     */
2199    public function userCanEdit(): bool
2200    {
2201        global $INFO;
2202        global $ID;
2203
2204        $wiki_file = wikiFN($ID);
2205        if (@file_exists($wiki_file) === false) {
2206            return true;
2207        }
2208        if ($INFO['isadmin'] === true || $INFO['ismanager'] === true) {
2209            return true;
2210        }
2211        // $meta_file = metaFN($ID, '.meta');
2212        if ($INFO['meta']['user'] === false) {
2213            return true;
2214        }
2215        if ($INFO['client'] === $INFO['meta']['user']) {
2216            return true;
2217        }
2218
2219        return false;
2220    }
2221
2222
2223    /**
2224     * Search for and return the uri of a media file
2225     *
2226     * @param string  $image           Image name to search for (without extension).
2227     * @param boolean $searchCurrentNS Search the current namespace.
2228     * @param boolean $propagate       Propagate search through the namespace.
2229     * @return string                  URI of the found media file
2230     */
2231    public function getMediaFile(string $image, bool $searchCurrentNS = true, bool $propagate = true)
2232    {
2233        global $INFO;
2234
2235        $ext = ['png', 'jpg', 'gif', 'svg'];
2236
2237        if ($searchCurrentNS === true) {
2238            $prefix[] = ':' . $INFO['namespace'] . ':';
2239        }
2240        if ($propagate === true) {
2241            $prefix[] = ':';
2242            $prefix[] = ':wiki:';
2243        }
2244        $theme = $this->getConf('customTheme');
2245        if (empty($theme) === false) {
2246            $prefix[] = 'themes/' . $theme . '/images/';
2247        }
2248        $prefix[] = 'images/';
2249
2250        $search = [];
2251        foreach ($prefix as $pitem) {
2252            foreach ($ext as $eitem) {
2253                $search[] = $pitem . $image . '.' . $eitem;
2254            }
2255        }
2256
2257        $img = '';
2258        $ismedia = false;
2259        $found = false;
2260
2261        foreach ($search as $img) {
2262            if (strcasecmp(substr($img, 0, 1), ':') === 0) {
2263                $file    = mediaFN($img);
2264                $ismedia = true;
2265            } else {
2266                $file    = tpl_incdir() . $img;
2267                $ismedia = false;
2268            }
2269
2270            if (file_exists($file) === true) {
2271                $found = true;
2272                break;
2273            }
2274        }
2275
2276        if ($found === false) {
2277            return false;
2278        }
2279
2280        if ($ismedia === true) {
2281            $url = ml($img, '', true, '');
2282        } else {
2283            $url = tpl_basedir() . $img;
2284        }
2285
2286        return $url;
2287    }
2288
2289
2290    /**
2291     * Print or return the page title
2292     *
2293     * @param string $page Page id or empty string for current page.
2294     * @return string      generated content
2295     */
2296    public function getPageTitle(string $page = ''): string
2297    {
2298        global $ID, $conf;
2299
2300        if (empty($page) === true) {
2301            $page = $ID;
2302        }
2303
2304        $html = p_get_first_heading($page);
2305        if(empty($html) === true) {
2306            $html = $conf['title'];
2307        }
2308        $html = strip_tags($html);
2309        $html = preg_replace('/\s+/', ' ', $html);
2310        $html .= ' [' . strip_tags($conf['title']) . ']';
2311        return trim($html);
2312    }
2313
2314
2315    /**
2316     * Return inline theme icon
2317     *
2318     * @param   string $type  Icon to retreive.
2319     * @param   string $class Classname to insert.
2320     * @return  string        HTML icon content
2321     */
2322    public function mikioInlineIcon(string $type, string $class = ""): string
2323    {
2324        if (is_array($class) === true) {
2325            $class = implode(' ', $class);
2326        }
2327
2328        if (strlen($class) > 0) {
2329            $class = ' ' . $class;
2330        }
2331
2332        switch ($type) {
2333            case 'wrench':
2334                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792
23351792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,53.152542,1217.0847)"><path d="m 384,64 q 0,26 -19,45 -19,
233619 -45,19 -26,0 -45,-19 -19,-19 -19,-45 0,-26 19,-45 19,-19 45,-19 26,0 45,19 19,19 19,45 z m 644,420 -682,-682 q -37,
2337-37 -90,-37 -52,0 -91,37 L 59,-90 Q 21,-54 21,0 21,53 59,91 L 740,772 Q 779,674 854.5,598.5 930,523 1028,484 z m 634,
2338435 q 0,-39 -23,-106 Q 1592,679 1474.5,595.5 1357,512 1216,512 1031,512 899.5,643.5 768,775 768,960 q 0,185 131.5,316.5
2339131.5,131.5 316.5,131.5 58,0 121.5,-16.5 63.5,-16.5 107.5,-46.5 16,-11 16,-28 0,-17 -16,-28 L 1152,1120 V 896 l 193,
2340-107 q 5,3 79,48.5 74,45.5 135.5,81 61.5,35.5 70.5,35.5 15,0 23.5,-10 8.5,-10 8.5,-25 z"/></g></svg>';
2341            case 'file':
2342                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2343viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,235.38983,1277.8305)" id="g2991">
2344<path d="M 128,0 H 1152 V 768 H 736 q -40,0 -68,28 -28,28 -28,68 v 416 H 128 V 0 z m 640,896 h 299 L 768,1195 V 896 z M
23451280,768 V -32 q 0,-40 -28,-68 -28,-28 -68,-28 H 96 q -40,0 -68,28 -28,28 -28,68 v 1344 q 0,40 28,68 28,28 68,28 h 544
2346q 40,0 88,-20 48,-20 76,-48 l 408,-408 q 28,-28 48,-76 20,-48 20,-88 z" id="path2993" /></g></svg>';
2347            case 'gear':
2348                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2349viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,121.49153,1285.4237)" id="g3027">
2350<path d="m 1024,640 q 0,106 -75,181 -75,75 -181,75 -106,0 -181,-75 -75,-75 -75,-181 0,-106 75,-181 75,-75 181,-75 106,0
2351181,75 75,75 75,181 z m 512,109 V 527 q 0,-12 -8,-23 -8,-11 -20,-13 l -185,-28 q -19,-54 -39,-91 35,-50 107,-138 10,-12
235210,-25 0,-13 -9,-23 -27,-37 -99,-108 -72,-71 -94,-71 -12,0 -26,9 l -138,108 q -44,-23 -91,-38 -16,-136 -29,-186 -7,-28
2353-36,-28 H 657 q -14,0 -24.5,8.5 Q 622,-111 621,-98 L 593,86 q -49,16 -90,37 L 362,16 Q 352,7 337,7 323,7 312,18 186,132
2354147,186 q -7,10 -7,23 0,12 8,23 15,21 51,66.5 36,45.5 54,70.5 -27,50 -41,99 L 29,495 Q 16,497 8,507.5 0,518 0,531 v 222
2355q 0,12 8,23 8,11 19,13 l 186,28 q 14,46 39,92 -40,57 -107,138 -10,12 -10,24 0,10 9,23 26,36 98.5,107.5 72.5,71.5 94.5,
235671.5 13,0 26,-10 l 138,-107 q 44,23 91,38 16,136 29,186 7,28 36,28 h 222 q 14,0 24.5,-8.5 Q 914,1391 915,1378 l 28,-184
2357q 49,-16 90,-37 l 142,107 q 9,9 24,9 13,0 25,-10 129,-119 165,-170 7,-8 7,-22 0,-12 -8,-23 -15,-21 -51,-66.5 -36,-45.5
2358-54,-70.5 26,-50 41,-98 l 183,-28 q 13,-2 21,-12.5 8,-10.5 8,-23.5 z" id="path3029" />
2359</g></svg>';
2360            case 'user':
2361                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2362viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,197.42373,1300.6102)"><path d="M
23631408,131 Q 1408,11 1335,-58.5 1262,-128 1141,-128 H 267 Q 146,-128 73,-58.5 0,11 0,131 0,184 3.5,234.5 7,285 17.5,343.5
236428,402 44,452 q 16,50 43,97.5 27,47.5 62,81 35,33.5 85.5,53.5 50.5,20 111.5,20 9,0 42,-21.5 33,-21.5 74.5,-48 41.5,
2365-26.5 108,-48 Q 637,565 704,565 q 67,0 133.5,21.5 66.5,21.5 108,48 41.5,26.5 74.5,48 33,21.5 42,21.5 61,0 111.5,-20
236650.5,-20 85.5,-53.5 35,-33.5 62,-81 27,-47.5 43,-97.5 16,-50 26.5,-108.5 10.5,-58.5 14,-109 Q 1408,184 1408,131 z m
2367-320,893 Q 1088,865 975.5,752.5 863,640 704,640 545,640 432.5,752.5 320,865 320,1024 320,1183 432.5,1295.5 545,1408 704,
23681408 863,1408 975.5,1295.5 1088,1183 1088,1024 z"/></g></svg>';
2369            case 'search':
2370                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"
2371aria-hidden="true" style="fill:currentColor"><path d="M27 24.57l-5.647-5.648a8.895 8.895 0 0 0 1.522-4.984C22.875 9.01
237218.867 5 13.938 5 9.01 5 5 9.01 5 13.938c0 4.929 4.01 8.938 8.938 8.938a8.887 8.887 0 0 0 4.984-1.522L24.568 27 27
237324.57zm-13.062-4.445a6.194 6.194 0 0 1-6.188-6.188 6.195 6.195 0 0 1 6.188-6.188 6.195 6.195 0 0 1 6.188 6.188 6.195
23746.195 0 0 1-6.188 6.188z"/></svg>';
2375            case 'home':
2376                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2377viewBox="0 -256 1792 1792" aria-hidden="true" style="fill:currentColor"><g
2378transform="matrix(1,0,0,-1,68.338983,1285.4237)" id="g3015"><path d="M 1408,544 V 64 Q 1408,38 1389,19 1370,0 1344,0 H
2379960 V 384 H 704 V 0 H 320 q -26,0 -45,19 -19,19 -19,45 v 480 q 0,1 0.5,3 0.5,2 0.5,3 l 575,474 575,-474 q 1,-2 1,-6 z
2380m 223,69 -62,-74 q -8,-9 -21,-11 h -3 q -13,0 -21,7 L 832,1112 140,535 q -12,-8 -24,-7 -13,2 -21,11 l -62,74 q -8,10
2381-7,23.5 1,13.5 11,21.5 l 719,599 q 32,26 76,26 44,0 76,-26 l 244,-204 v 195 q 0,14 9,23 9,9 23,9 h 192 q 14,0 23,-9 9,
2382-9 9,-23 V 840 l 219,-182 q 10,-8 11,-21.5 1,-13.5 -7,-23.5 z" id="path3017" /></g></svg>';
2383            case 'sun':
2384                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2385style="fill:currentColor" viewBox="0 0 16 16"><path d="M8 11a3 3 0 1 1 0-6 3 3 0 0 1 0 6zm0 1a4 4 0 1 0 0-8 4 4 0 0 0
23860 8zm.5-9.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm0 11a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm5-5a.5.5 0 1 1 0-1 .5.5 0 0 1 0 1zm-11
23870a.5.5 0 1 1 0-1 .5.5 0 0 1 0 1zm9.743-4.036a.5.5 0 1 1-.707-.707.5.5 0 0 1 .707.707zm-7.779 7.779a.5.5 0 1
23881-.707-.707.5.5 0 0 1 .707.707zm7.072 0a.5.5 0 1 1 .707-.707.5.5 0 0 1-.707.707zM3.757 4.464a.5.5 0 1 1 .707-.707.5.5
23890 0 1-.707.707z" /></svg>';
2390            case 'moon':
2391                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2392style="fill:currentColor" viewBox="0 0 16 16"><path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0
23934.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0
23941 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278zM4.858 1.311A7.269 7.269 0 0 0
23951.025 7.71c0 4.02 3.279 7.276 7.319 7.276a7.316 7.316 0 0 0 5.205-2.162c-.337.042-.68.063-1.029.063-4.61
23960-8.343-3.714-8.343-8.29 0-1.167.242-2.278.681-3.286z" /></svg>';
2397            case 'sunmoon':
2398                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2399style="fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10"
2400viewBox="0 0 32 32"><line x1="16" y1="3" x2="16" y2="29"/><path d="M16,23c-3.87,0-7-3.13-7-7s3.13-7,7-7"/><line
2401x1="6.81" y1="6.81" x2="8.93" y2="8.93"/><line x1="3" y1="16" x2="6" y2="16"/><line x1="6.81" y1="25.19" x2="8.93"
2402y2="23.07"/><path d="M16,12.55C17.2,10.43,19.48,9,22.09,9c0.16,0,0.31,0.01,0.47,0.02c-1.67,0.88-2.8,2.63-2.8,4.64c0,2.9,
24032.35,5.25,5.25,5.25c1.6,0,3.03-0.72,3.99-1.85C28.48,20.43,25.59,23,22.09,23c-2.61,0-4.89-1.43-6.09-3.55"/></svg>';
2404            case 'hamburger':
2405                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"
2406style="fill:currentColor"><path d="M16 132h416c8.837 0 16-7.163 16-16V76c0-8.837-7.163-16-16-16H16C7.163 60 0 67.163 0
240776v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16
240816v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16
240916v40c0 8.837 7.163 16 16 16z"/></svg>';
2410            case 'down-arrow':
2411                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"
2412aria-hidden="true" style="fill:currentColor"><path d="M16.003 18.626l7.081-7.081L25 13.46l-8.997 8.998-9.003-9
24131.917-1.916z"/></svg>';
2414            case 'language':
2415                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" width="16"
2416height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M4.545 6.714 4.11
24178H3l1.862-5h1.284L8 8H6.833l-.435-1.286H4.545zm1.634-.736L5.5 3.956h-.049l-.679 2.022H6.18z"/><path d="M0 2a2 2 0 0 1
24182-2h7a2 2 0 0 1 2 2v3h3a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-3H2a2 2 0 0 1-2-2V2zm2-1a1 1 0 0 0-1 1v7a1 1 0 0
24190 1 1h7a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H2zm7.138 9.995c.193.301.402.583.63.846-.748.575-1.673 1.001-2.768
24201.292.178.217.451.635.555.867 1.125-.359 2.08-.844 2.886-1.494.777.665 1.739 1.165 2.93
24211.472.133-.254.414-.673.629-.89-1.125-.253-2.057-.694-2.82-1.284.681-.747 1.222-1.651
24221.621-2.757H14V8h-3v1.047h.765c-.318.844-.74 1.546-1.272 2.13a6.066 6.066 0 0 1-.415-.492 1.988 1.988 0 0 1-.94.31z"/>
2423</svg>';
2424        }//end switch
2425
2426        return '';
2427    }
2428
2429    /**
2430     * Show Messages
2431     *
2432     * @return void
2433     */
2434    public function showMessages()
2435    {
2436        global $ACT;
2437
2438        $show = $this->getConf('showNotifications');
2439        if (
2440            strlen($show) === 0 ||
2441            strcasecmp($show, tpl_getLang('value_always')) === 0 ||
2442            (strcasecmp($show, tpl_getLang('value_admin')) === 0 && strcasecmp($ACT, 'admin') === 0)
2443        ) {
2444            html_msgarea();
2445
2446            // global $MSG, $MSG_shown;
2447
2448            // if (isset($MSG) !== false) {
2449            //     if (isset($MSG_shown) === false) {
2450            //         $MSG_shown = [];
2451            //     }
2452
2453            //     foreach ($MSG as $msg) {
2454            //         $hash = md5($msg['msg']);
2455            //         if (isset($MSG_shown[$hash]) === true) {
2456            //             continue;
2457            //         }
2458            //         // skip double messages
2459
2460            //         if (info_msg_allowed($msg) === true) {
2461            //             echo '<div class="me ' . $msg['lvl'] . '">';
2462            //             echo $msg['msg'];
2463            //             echo '</div>';
2464            //         }
2465
2466            //         $MSG_shown[$hash] = true;
2467            //     }
2468
2469            //     unset($GLOBALS['MSG']);
2470            // }//end if
2471
2472            if (strlen($this->includedPageNotifications) > 0) {
2473                echo $this->includedPageNotifications;
2474            }
2475        }//end if
2476    }
2477
2478    /**
2479     * Dokuwiki version number
2480     *
2481     * @return  int        the dw version date converted to integer
2482     */
2483    public function getDokuWikiVersion(): int
2484    {
2485        if (function_exists('getVersionData') === true) {
2486            $version_data = getVersionData();
2487            if (is_array($version_data) === true && array_key_exists('date', $version_data) === true) {
2488                $version_items = explode(' ', $version_data['date']);
2489                if (count($version_items) >= 1) {
2490                    return (int)preg_replace('/\D+/', '', strtolower($version_items[0]));
2491                }
2492            }
2493        }
2494
2495        return 0;
2496    }
2497
2498    /**
2499     * Call a method and parse the HTML output
2500     *
2501     * @param callable $method The method to call and capture output
2502     * @param callable $parser The parser method which is passed a DOMDocument to manipulate
2503     * @return  string           The raw parsed HTML
2504     */
2505    protected function parseHTML(callable $method, callable $parser): string
2506    {
2507        if(!is_callable($method) || !is_callable($parser)) {
2508            return '';
2509        }
2510
2511        ob_start();
2512        $method();
2513        $content = ob_get_clean();
2514        if($content !== '') {
2515            $domDocument = new DOMDocument();
2516            $domContent = $domDocument->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES'));
2517            if (false === $domContent) {
2518                return $content;
2519            }
2520
2521            $parser($domDocument);
2522            return $domDocument->saveHTML();
2523        }
2524
2525        return $content;
2526    }
2527
2528
2529    /**
2530     * Get an icon as a DOM element
2531     *
2532     * @param DOMDocument $domDocument The DOMDocument to import the icon into
2533     * @param string $type The icon type
2534     * @param string $class The icon class
2535     * @return DOMNode The icon as a DOM element
2536     */
2537    protected function iconAsDomElement(DOMDocument $domDocument, string $type, string $class = ''): DOMNode
2538    {
2539        $svgDoc = new DOMDocument();
2540        $svgDoc->loadXML($this->mikioInlineIcon($type, $class));
2541        $svgElement = $svgDoc->documentElement;
2542        return $domDocument->importNode($svgElement, true);
2543    }
2544}
2545
2546global $TEMPLATE;
2547$TEMPLATE = mikio::getInstance();
2548// 2494
2549