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