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