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