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