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