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