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