xref: /template/mikio/mikio.php (revision 34d5f19a7c6e8434b6a1d63ab86a60d1b895282c)
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            $html .= '<li class="mikio-darklight">
709<a href="#" class="mikio-control mikio-button mikio-darklight-button">' .
710            ($this->getConf('autoLightDark') === true ? $this->mikioInlineIcon('sunmoon', 'mikio-darklight-auto') : '') .
711            $this->mikioInlineIcon('sun', 'mikio-darklight-light') .
712            $this->mikioInlineIcon('moon', 'mikio-darklight-dark') .
713            '</a></li>';
714        }
715
716        $html .= '</ul>';
717
718        if ($print === true) {
719            echo $html;
720        }
721        return $html;
722    }
723
724
725    /**
726     * Create a nav element from a string. <uri>|<title>;
727     *
728     * @param string $str String to generate nav.
729     * @return string     nav elements generated
730     */
731    public function stringToNav(string $str)
732    {
733        $html = '';
734
735        if (empty($str) === false) {
736            $items = explode(';', $str);
737            if (count($items) > 0) {
738                $html .= '<ul class="mikio-nav">';
739                foreach ($items as $item) {
740                    $parts = explode('|', $item);
741                    if ($parts > 1) {
742                        $html .= '<li class="mikio-nav-item"><a class="mikio-nav-link" href="' .
743                            strip_tags($this->getLink(trim($parts[0]))) . '">' . strip_tags(trim($parts[1])) .
744                            '</a></li>';
745                    }
746                }
747                $html .= '</ul>';
748            }
749        }
750
751        return $html;
752    }
753
754    /**
755     * print or return the main navbar
756     *
757     * @param boolean $print   Print the navbar.
758     * @param boolean $showSub Include the sub navbar.
759     * @return string          generated content
760     */
761    public function includeNavbar(bool $print = true, bool $showSub = false)
762    {
763        global $conf, $USERINFO;
764
765        $homeUrl = wl();
766
767        if (plugin_isdisabled('showpageafterlogin') === false) {
768            $p = &plugin_load('action', 'showpageafterlogin');
769            if (empty($p) === false) {
770                if (is_array($USERINFO) === true && count($USERINFO) > 0) {
771                    $homeUrl = wl($p->getConf('page_after_login'));
772                }
773            }
774        } else {
775            if (is_array($USERINFO) === true && count($USERINFO) > 0) {
776                $url = $this->getConf('brandURLUser');
777                if (strlen($url) > 0) {
778                    $homeUrl = $url;
779                }
780            } else {
781                $url = $this->getConf('brandURLGuest');
782                if (strlen($url) > 0) {
783                    $homeUrl = $url;
784                }
785            }
786        }
787
788        $html = '';
789
790        $html .= '<nav class="mikio-navbar'  . (($this->getConf('stickyNavbar') === true) ? ' mikio-sticky' : '') .
791            '">';
792        $html .= '<div class="mikio-container">';
793        $html .= '<a class="mikio-navbar-brand" href="' . $homeUrl . '">';
794        if ($this->getConf('navbarUseTitleIcon') === true || $this->getConf('navbarUseTitleText') === true) {
795            // Brand image
796            if ($this->getConf('navbarUseTitleIcon') === true) {
797                $logo = $this->getMediaFile('logo', false);
798                ;
799                if (empty($logo) === false) {
800                    $width = $this->getConf('navbarTitleIconWidth');
801                    $height = $this->getConf('navbarTitleIconHeight');
802                    $styles = '';
803
804                    if (strlen($width) > 0 || strlen($height) > 0) {
805                        if (ctype_digit($width) === true) {
806                            $styles .= 'max-width:' . intval($width) . 'px;';
807                        } elseif (preg_match('/^\d+(px|rem|em|%)$/', $width) === 1) {
808                            $styles .= 'max-width:' . $width . ';';
809                        } elseif (strcasecmp($width, 'none') === 0) {
810                            $styles .= 'max-width:none;';
811                        }
812
813                        if (ctype_digit($height) === true) {
814                            $styles .= 'max-height:' . intval($height) . 'px;';
815                        } elseif (preg_match('/^\d+(px|rem|em|%)$/', $height) === 1) {
816                            $styles .= 'max-height:' . $height . ';';
817                        } elseif (strcasecmp($height, 'none') === 0) {
818                            $styles .= 'max-height:none;';
819                        }
820
821                        if (strlen($styles) > 0) {
822                            $styles = ' style="' . $styles . '"';
823                        }
824                    }//end if
825
826                    $html .= '<img src="' . $logo . '" class="mikio-navbar-brand-image"' . $styles . '>';
827                }//end if
828            }//end if
829
830            // Brand title
831            if ($this->getConf('navbarUseTitleText') === true) {
832                $html .= '<div class="mikio-navbar-brand-title">';
833                $html .= '<h1 class="mikio-navbar-brand-title-text">' . $conf['title'] . '</h1>';
834                if ($this->getConf('navbarUseTaglineText') === true) {
835                    $html .= '<p class="claim mikio-navbar-brand-title-tagline">' . $conf['tagline'] . '</p>';
836                }
837                $html .= '</div>';
838            }
839        }//end if
840        $html .= '</a>';
841        $html .= '<div class="mikio-navbar-toggle"><span class="icon"></span></div>';
842
843        // Menus
844        $html .= '<div class="mikio-navbar-collapse">';
845
846        $menus = [$this->getConf('navbarPosLeft', 'none'), $this->getConf('navbarPosMiddle', 'none'),
847            $this->getConf('navbarPosRight', 'none')
848        ];
849        foreach ($menus as $menuType) {
850            switch ($menuType) {
851                case 'custom':
852                    $html .= $this->stringToNav($this->getConf('navbarCustomMenuText', ''));
853                    break;
854                case 'search':
855                    $html .= '<div class="mikio-nav-item">';
856                    $html .= $this->includeSearch(false);
857                    $html .= '</div>';
858                    break;
859                case 'dokuwiki':
860                    $html .= $this->includeDWMenu(false);
861                    break;
862            }
863        }
864
865        $html .= '</div>';
866        $html .= '</div>';
867        $html .= '</nav>';
868
869        // Sub Navbar
870        if ($showSub === true) {
871            $sub = $this->includePage('submenu', false);
872            if (empty($sub) === false) {
873                $html .= '<nav class="mikio-navbar mikio-sub-navbar">' . $sub . '</nav>';
874            }
875        }
876
877        if ($print === true) {
878            echo $html;
879        }
880        return $html;
881    }
882
883
884    /**
885     * Is there a sidebar
886     *
887     * @param   string $prefix Sidebar prefix to use when searching.
888     * @return  boolean        if sidebar exists
889     */
890    public function sidebarExists(string $prefix = '')
891    {
892        global $conf;
893
894        if (strcasecmp($prefix, 'left') === 0) {
895            $prefix = '';
896        }
897
898        return $this->pageExists($conf['sidebar' . $prefix]);
899    }
900
901
902    /**
903     * Print or return the sidebar content
904     *
905     * @param   string  $prefix Sidebar prefix to use when searching.
906     * @param   boolean $print  Print the generated content to the output buffer.
907     * @param   boolean $parse  Parse the content.
908     * @return  string          generated content
909     */
910    public function includeSidebar(string $prefix = '', bool $print = true, bool $parse = true)
911    {
912        global $conf, $ID;
913
914        $html = '';
915        $confPrefix = preg_replace('/[^a-zA-Z0-9]/', '', ucwords($prefix));
916        $prefix = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($prefix));
917
918        if (empty($confPrefix) === true) {
919            $confPrefix = 'Left';
920        }
921        if (strcasecmp($prefix, 'left') === 0) {
922            $prefix = '';
923        }
924
925        $sidebarPage = empty($conf[$prefix . 'sidebar']) === true ? $prefix . 'sidebar' : $conf[$prefix . 'sidebar'];
926
927        if (
928            $this->getConf('sidebarShow' . $confPrefix) === true && page_findnearest($sidebarPage) !== false &&
929            p_get_metadata($ID, 'nosidebar', false) === null
930        ) {
931            $content = $this->includePage($sidebarPage . 'header', false);
932            if (empty($content) === false) {
933                $html .= '<div class="mikio-sidebar-header">' . $content . '</div>';
934            }
935
936            if (empty($prefix) === true) {
937                $rows = [$this->getConf('sidebarLeftRow1'), $this->getConf('sidebarLeftRow2'),
938                    $this->getConf('sidebarLeftRow3'), $this->getConf('sidebarLeftRow4')
939                ];
940
941                foreach ($rows as $row) {
942                    switch ($row) {
943                        case 'search':
944                            $html .= $this->includeSearch(false);
945                            break;
946                        case 'logged in user':
947                            $html .= $this->includeLoggedIn(false);
948                            break;
949                        case 'content':
950                            $content = $this->includePage($sidebarPage, false);
951                            if (empty($content) === false) {
952                                $html .= '<div class="mikio-sidebar-content">' . $content . '</div>';
953                            }
954                            break;
955                        case 'tags':
956                            $html .= '<div class="mikio-tags"></div>';
957                    }
958                }
959            } else {
960                $content = $this->includePage($sidebarPage, false);
961                if (empty($content) === false) {
962                    $html .= '<div class="mikio-sidebar-content">' . $content . '</div>';
963                }
964            }//end if
965
966            $content = $this->includePage($sidebarPage . 'footer', false);
967            if (empty($content) === false) {
968                $html .= '<div class="mikio-sidebar-footer">' . $content . '</div>';
969            }
970        }//end if
971
972        if (empty($html) === true) {
973            if (empty($prefix) === true && $this->getConf('sidebarAlwaysShowLeft') === true) {
974                $html = '&nbsp;';
975            }
976            if ($this->getConf('sidebarAlwaysShow' . ucfirst($prefix)) === true) {
977                $html = '&nbsp;';
978            }
979        }
980
981        if (empty($html) === false) {
982            $html = '<aside class="mikio-sidebar mikio-sidebar-' . (empty($prefix) === true ? 'left' : $prefix) .
983                '"><a class="mikio-sidebar-toggle' .
984                ($this->getConf('sidebarMobileDefaultCollapse') === true ? ' closed' : '') . '" href="#">' .
985                tpl_getLang('sidebar-title') . ' <span class="icon"></span></a><div class="mikio-sidebar-collapse">' .
986                $html . '</div></aside>';
987        }
988
989        if ($parse === true) {
990            $html = $this->includeIcons($html);
991        }
992        if ($print === true) {
993            echo $html;
994        }
995        return $html;
996    }
997
998
999    /**
1000     * Print or return the page tools content
1001     *
1002     * @param   boolean $print     Print the generated content to the output buffer.
1003     * @param   boolean $includeId Include the dw__pagetools id in the element.
1004     * @return  string             generated content
1005     */
1006    public function includePageTools(bool $print = true, bool $includeId = false)
1007    {
1008        global $USERINFO;
1009
1010        $loggedIn = (is_array($USERINFO) === true && count($USERINFO) > 0);
1011        $html = '';
1012
1013        $html .= '<nav' . ($includeId === true ? ' id="dw__pagetools"' : '') . ' class="hidden-print dw__pagetools">';
1014        $html .= '<ul class="tools">';
1015
1016        $items = (new \dokuwiki\Menu\PageMenu())->getItems();
1017        foreach ($items as $item) {
1018            $classes = [];
1019            $classes[] = $item->getType();
1020            $attr = $item->getLinkAttributes();
1021
1022            if (empty($attr['class']) === false) {
1023                $classes = array_merge($classes, explode(' ', $attr['class']));
1024            }
1025
1026            $classes = array_unique($classes);
1027
1028            $showItem = $this->getConf('pageToolsShow' . ucfirst($item->getType()));
1029            if (
1030                $showItem !== false && (strcasecmp($showItem, 'always') === 0 ||
1031                (strcasecmp($showItem, 'logged in') === 0 && $loggedIn === true) ||
1032                (strcasecmp($showItem, 'logged out') === 0 && $loggedIn === true))
1033            ) {
1034                $html .= '<li class="' . implode(' ', $classes) . '">';
1035                $html .= '<a href="' . $item->getLink() . '" class="' . $item->getType() . '" title="' .
1036                    $item->getTitle() . '"><div class="icon">' . inlineSVG($item->getSvg()) .
1037                    '</div><span class="a11y">' . $item->getLabel() . '</span></a>';
1038                $html .= '</li>';
1039            }
1040        }//end foreach
1041
1042        $html .= '</ul>';
1043        $html .= '</nav>';
1044
1045        if ($print === true) {
1046            echo $html;
1047        }
1048        return $html;
1049    }
1050
1051
1052    /**
1053     * Print or return the search bar
1054     *
1055     * @param   boolean $print Print content.
1056     * @return  string         contents of the search bar
1057     */
1058    public function includeSearch(bool $print = true)
1059    {
1060        global $lang, $ID, $ACT, $QUERY;
1061        $html = '';
1062
1063        $html .= '<form class="mikio-search search" action="' . wl() .
1064            '" accept-charset="utf-8" method="get" role="search">';
1065        $html .= '<input type="hidden" name="do" value="search">';
1066        $html .= '<input type="hidden" name="id" value="' . $ID . '">';
1067        $html .= '<input name="q" ';
1068        if ($this->getConf('searchUseTypeahead') === true) {
1069            $html .= 'class="search_typeahead" ';
1070        }
1071        $html .= 'autocomplete="off" type="search" placeholder="' . $lang['btn_search'] . '" value="' .
1072            ((strcasecmp($ACT, 'search') === 0) ? htmlspecialchars($QUERY) : '') . '" accesskey="f" title="[F]" />';
1073        $html .= '<button type="submit" title="' .  $lang['btn_search'] . '">';
1074        if (strcasecmp($this->getConf('searchButton'), 'icon') === 0) {
1075            $html .= $this->mikioInlineIcon('search');
1076        } else {
1077            $html .= $lang['btn_search'];
1078        }
1079        $html .= '</button>';
1080        $html .= '</form>';
1081
1082        if ($print === true) {
1083            echo $html;
1084        }
1085        return $html;
1086    }
1087
1088
1089    /**
1090     * Print or return content
1091     *
1092     * @param   boolean $print Print content.
1093     * @return  string         contents
1094     */
1095    public function includeContent(bool $print = true)
1096    {
1097        ob_start();
1098        tpl_content(false);
1099        $html = ob_get_contents();
1100        ob_end_clean();
1101
1102        $html = $this->includeIcons($html);
1103        $html = $this->parseContent($html);
1104
1105        $html .= '<div style="clear:both"></div>';
1106
1107        if ($this->getConf('heroTitle') === false) {
1108            $html = '<div class="mikio-tags"></div>' . $html;
1109        }
1110
1111        $html = '<div class="mikio-article-content">' . $html . '</div>';
1112
1113        if ($print === true) {
1114            echo $html;
1115        }
1116        return $html;
1117    }
1118
1119    /**
1120     * Print or return footer
1121     *
1122     * @param   boolean $print Print footer.
1123     * @return  string         HTML string containing footer
1124     */
1125    public function includeFooter(bool $print = true)
1126    {
1127        global $ACT;
1128
1129        $html = '';
1130
1131        $html .= '<footer class="mikio-footer">';
1132        $html .= '<div class="doc">' . tpl_pageinfo(true) . '</div>';
1133        $html .= $this->includePage('footer', false);
1134
1135        $html .= $this->stringToNav($this->getConf('footerCustomMenuText'));
1136
1137        if ($this->getConf('footerSearch') === true) {
1138            $html .= '<div class="mikio-footer-search">';
1139            $html .= $this->includeSearch(false);
1140            $html .= '</div>';
1141        }
1142
1143        $showPageTools = $this->getConf('pageToolsFooter');
1144        if (
1145            strcasecmp($ACT, 'show') === 0 && (strcasecmp($showPageTools, 'always') === 0 ||
1146            $this->userCanEdit() === true && strcasecmp($showPageTools, 'page editors') === 0)
1147        ) {
1148            $html .= $this->includePageTools(false);
1149        }
1150
1151        $meta['licenseType']            = ['multichoice', '_choices' => ['none', 'badge', 'button']];
1152        $meta['licenseImageOnly']       = ['onoff'];
1153
1154        $licenseType = $this->getConf('licenseType');
1155        if ($licenseType !== 'none') {
1156            $html .= tpl_license($licenseType, $this->getConf('licenseImageOnly'), true, true);
1157        }
1158
1159        $html .= '</footer>';
1160
1161        if ($print === true) {
1162            echo $html;
1163        }
1164        return $html;
1165    }
1166
1167
1168    /**
1169     * Print or return breadcrumb trail
1170     *
1171     * @param   boolean $print Print out trail.
1172     * @param   boolean $parse Parse trail before printing.
1173     * @return  string         HTML string containing breadcrumbs
1174     */
1175    public function includeBreadcrumbs(bool $print = true, bool $parse = true)
1176    {
1177        global $conf, $ID, $lang, $ACT;
1178
1179        if (
1180            $this->getConf('breadcrumbHideHome') === true && strcasecmp($ID, 'start') === 0 &&
1181            strcasecmp($ACT, 'show') === 0 || strcasecmp($ACT, 'showtag') === 0 || $conf['breadcrumbs'] === 0
1182        ) {
1183            return '';
1184        }
1185
1186        $html = '<div class="mikio-breadcrumbs">';
1187        $html .= '<div class="mikio-container">';
1188        if (strcasecmp($ACT, 'show') === 0) {
1189            if ($conf['breadcrumbs'] !== 0) {
1190                if ($this->getConf('breadcrumbPrefix') === false && $this->getConf('breadcrumbSep') === false) {
1191                    ob_start();
1192                    tpl_breadcrumbs();
1193                    $html .= ob_get_contents();
1194                    ob_end_clean();
1195                } else {
1196                    $sep = '•';
1197                    $prefix = $lang['breadcrumb'];
1198
1199                    if ($this->getConf('breadcrumbSep') === true) {
1200                        $sep = $this->getConf('breadcrumbSepText');
1201                        $img = $this->getMediaFile('breadcrumb-sep', false);
1202
1203                        if ($img !== false) {
1204                            $sep = '<img src="' . $img . '">';
1205                        }
1206                    }
1207
1208                    if ($this->getConf('breadcrumbPrefix') === true) {
1209                        $prefix = $this->getConf('breadcrumbPrefixText');
1210                        $img = $this->getMediaFile('breadcrumb-prefix', false);
1211
1212                        if ($img !== false) {
1213                            $prefix = '<img src="' . $img . '">';
1214                        }
1215                    }
1216
1217                    $crumbs = breadcrumbs();
1218
1219                    $html .= '<ul>';
1220                    if (empty($prefix) === false) {
1221                        $html .= '<li class="prefix">' . $prefix . '</li>';
1222                    }
1223
1224                    $last = count($crumbs);
1225                    $i    = 0;
1226                    foreach ($crumbs as $id => $name) {
1227                        $i++;
1228                        if ($i !== 1) {
1229                            $html .= '<li class="sep">' . $sep . '</li>';
1230                        }
1231                        $html .= '<li' . ($i === $last ? ' class="curid"' : '') . '>';
1232                        $html .= tpl_pagelink($id, null, true);
1233                        $html .= '</li>';
1234                    }
1235
1236                    $html .= '</ul>';
1237                }//end if
1238            }//end if
1239        }//end if
1240
1241        $html .= '</div>';
1242        $html .= '</div>';
1243
1244        if ($parse === true) {
1245            $html = $this->includeIcons($html);
1246        }
1247        if ($print === true) {
1248            echo $html;
1249        }
1250        return $html;
1251    }
1252
1253    /**
1254     * Print or return you are here trail
1255     *
1256     * @param   boolean $print Print out trail.
1257     * @param   boolean $parse Parse trail before printing.
1258     * @return  string         HTML string containing breadcrumbs
1259     */
1260    public function includeYouAreHere(bool $print = true, bool $parse = true)
1261    {
1262        global $conf, $ID, $lang, $ACT;
1263
1264        if (
1265            $this->getConf('youarehereHideHome') === true && strcasecmp($ID, 'start') === 0 &&
1266            strcasecmp($ACT, 'show') === 0 || strcasecmp($ACT, 'showtag') === 0 || $conf['youarehere'] === 0
1267        ) {
1268            return '';
1269        }
1270
1271        $html = '<div class="mikio-youarehere">';
1272        $html .= '<div class="mikio-container">';
1273        if (strcasecmp($ACT, 'show') === 0) {
1274            if ($conf['youarehere'] !== 0) {
1275                if ($this->getConf('youareherePrefix') === false && $this->getConf('youarehereSep') === false) {
1276                    $html .= '<div class="mikio-bcdw">';
1277                    ob_start();
1278                    tpl_youarehere();
1279                    $html .= ob_get_contents();
1280                    ob_end_clean();
1281                    $html .= '</div>';
1282                } else {
1283                    $sep = ' » ';
1284                    $prefix = $lang['youarehere'];
1285
1286                    if ($this->getConf('youarehereSep') === true) {
1287                        $sep = $this->getConf('youarehereSepText');
1288                        $img = $this->getMediaFile('youarehere-sep', false);
1289
1290                        if ($img !== false) {
1291                            $sep = '<img src="' . $img . '">';
1292                        }
1293                    }
1294
1295                    if ($this->getConf('youareherePrefix') === true) {
1296                        $prefix = $this->getConf('youareherePrefixText');
1297                        $img = $this->getMediaFile('youarehere-prefix', false);
1298
1299                        if ($img !== false) {
1300                            $prefix = '<img src="' . $img . '">';
1301                        }
1302                    }
1303
1304                    $html .= '<ul>';
1305                    if (empty($prefix) === false) {
1306                        $html .= '<li class="prefix">' . $prefix . '</li>';
1307                    }
1308                    $html .= '<li>' . tpl_pagelink(':' . $conf['start'], null, true) . '</li>';
1309
1310                    $parts = explode(':', $ID);
1311                    $count = count($parts);
1312
1313                    $part = '';
1314                    for ($i = 0; $i < ($count - 1); $i++) {
1315                        $part .= $parts[$i] . ':';
1316                        $page = $part;
1317                        if ($page === $conf['start']) {
1318                            continue;
1319                        }
1320
1321                        $html .= '<li class="sep">' . $sep . '</li>';
1322                        $html .= '<li>' . tpl_pagelink($page, null, true) . '</li>';
1323                    }
1324
1325                    resolve_pageid('', $page, $exists);
1326                    if ((isset($page) === true && $page === $part . $parts[$i]) === false) {
1327                        $page = $part . $parts[$i];
1328                        if ($page !== $conf['start']) {
1329                            $html .= '<li class="sep">' . $sep . '</li>';
1330                            $html .= '<li>' . tpl_pagelink($page, null, true) . '</li>';
1331                        }
1332                    }
1333
1334                    $html .= '</ul>';
1335                }//end if
1336            }//end if
1337
1338            $showLast = $this->getConf('youarehereShowLast');
1339            if ($showLast !== 0) {
1340                preg_match_all('/(<li[^>]*>.+?<\/li>)/', $html, $matches);
1341                if (count($matches) > 0 && count($matches[0]) > (($showLast * 2) + 2)) {
1342                    $count = count($matches[0]);
1343                    $list = '';
1344
1345                    // Show Home
1346                    $list .= $matches[0][0] . $matches[0][1];
1347
1348                    $list .= '<li>...</li>';
1349                    for ($i = ($count - ($showLast * 2)); $i <= $count; $i++) {
1350                        $list .= $matches[0][$i];
1351                    }
1352
1353                    $html = preg_replace('/<ul>.*<\/ul>/', '<ul>' . $list . '</ul>', $html);
1354                }
1355            }
1356
1357            switch ($this->getConf('youarehereHome')) {
1358                case 'none':
1359                    $html = preg_replace('/<li[^>]*>.+?<\/li>/', '', $html, 2);
1360                    break;
1361                case 'home':
1362                    $html = preg_replace('/(<a[^>]*>)(.+?)(<\/a>)/', '$1' . tpl_getlang('home') . '$3', $html, 1);
1363                    break;
1364                case 'icon':
1365                    $html = preg_replace('/(<a[^>]*>)(.+?)(<\/a>)/', '$1' .
1366                        $this->mikioInlineIcon('home') . '$3', $html, 1);
1367                    break;
1368            }
1369        } else {
1370            $html .= '&#8810; ';
1371            if (isset($_GET['page']) === true) {
1372                $html .= '<a href="' . wl($ID, ['do' => $ACT]) . '">Back</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;';
1373            }
1374            $html .= '<a href="' . wl($ID) . '">View Page</a>';
1375        }//end if
1376
1377        $html .= '</div>';
1378        $html .= '</div>';
1379
1380        if ($parse === true) {
1381            $html = $this->includeIcons($html);
1382        }
1383        if ($print === true) {
1384            echo $html;
1385        }
1386        return $html;
1387    }
1388
1389    /**
1390     * Get Page Title
1391     *
1392     * @return string page title
1393     */
1394    public function parsePageTitle()
1395    {
1396        global $ID;
1397
1398        $title = p_get_first_heading($ID);
1399        if (strlen($title) <= 0) {
1400            $title = tpl_pagetitle(null, true);
1401        }
1402        $title = $this->includeIcons($title);
1403
1404        return $title;
1405    }
1406
1407
1408    /**
1409     * Print or return hero block
1410     *
1411     * @param   boolean $print Print content.
1412     * @return  string         contents of hero
1413     */
1414    public function includeHero(bool $print = true)
1415    {
1416        $html = '';
1417
1418        if ($this->getConf('heroTitle') === true) {
1419            $html .= '<div class="mikio-hero">';
1420            $html .= '<div class="mikio-container">';
1421            $html .= '<div class="mikio-hero-text">';
1422            if (strcasecmp($this->getConf('youareherePosition'), 'hero') === 0) {
1423                $html .= $this->includeYouAreHere(false);
1424            }
1425            if (strcasecmp($this->getConf('breadcrumbPosition'), 'hero') === 0) {
1426                $html .= $this->includeBreadcrumbs(false);
1427            }
1428
1429            $html .= '<h1 class="mikio-hero-title">';
1430            $html .= $this->parsePageTitle();    // No idea why this requires a blank space afterwards to work?
1431            $html .= '</h1>';
1432            $html .= '<h2 class="mikio-hero-subtitle"></h2>';
1433            $html .= '</div>';
1434
1435            $hero_image = $this->getMediaFile('hero', true, $this->getConf('heroImagePropagation', true));
1436            $hero_image_resize_class = '';
1437            if (empty($hero_image) === false) {
1438                $hero_image = ' style="background-image:url(\'' . $hero_image . '\');"';
1439                $hero_image_resize_class = ' mikio-hero-image-resize';
1440            }
1441
1442            $html .= '<div class="mikio-hero-image' . $hero_image_resize_class . '"' . $hero_image .
1443                '><div class="mikio-tags"></div></div>';
1444
1445            $html .= '</div>';
1446            $html .= '</div>';
1447        }//end if
1448
1449        if ($print === true) {
1450            echo $html;
1451        }
1452
1453        return $html;
1454    }
1455
1456
1457    /**
1458     * Print or return out TOC
1459     *
1460     * @param   boolean $print Print TOC.
1461     * @param   boolean $parse Parse icons.
1462     * @return  string         contents of TOC
1463     */
1464    public function includeTOC(bool $print = true, bool $parse = true)
1465    {
1466        $html = '';
1467
1468        $tocHtml = tpl_toc(true);
1469
1470        if (empty($tocHtml) === false) {
1471            $tocHtml = preg_replace(
1472                '/(<h3.+?toggle.+?>)(.+?)<\/h3>/',
1473                '$1' .
1474                $this->mikioInlineIcon('hamburger', 'hamburger') . '$2' .
1475                $this->mikioInlineIcon('down-arrow', 'down-arrow') . '</h3>',
1476                $tocHtml
1477            );
1478            $tocHtml = preg_replace('/<li.*><div.*><a.*><\/a><\/div><\/li>\s*/', '', $tocHtml);
1479            $tocHtml = preg_replace('/<ul.*>\s*<\/ul>\s*/', '', $tocHtml);
1480
1481            $html .= '<div class="mikio-toc">';
1482            $html .= $tocHtml;
1483            $html .= '</div>';
1484        }
1485
1486        if ($parse === true) {
1487            $html = $this->includeIcons($html);
1488        }
1489
1490        if ($print === true) {
1491            echo $html;
1492        }
1493
1494        return $html;
1495    }
1496
1497
1498    /**
1499     * Parse the string and replace icon elements with included icon libraries
1500     *
1501     * @param   string $str Content to parse.
1502     * @return  string      parsed string
1503     */
1504    public function includeIcons(string $str)
1505    {
1506        global $ACT, $MIKIO_ICONS;
1507
1508        $iconTag = $this->getConf('iconTag', 'icon');
1509        if (empty($iconTag) === true) {
1510            return $str;
1511        }
1512
1513        if (
1514            in_array($ACT, ['show', 'showtag', 'revisions', 'index', 'preview']) === true ||
1515            strcasecmp($ACT, 'admin') === 0 && count($MIKIO_ICONS) > 0
1516        ) {
1517            $content = $str;
1518            $preview = null;
1519
1520            if (strcasecmp($ACT, 'preview') === 0) {
1521                $html = new \simple_html_dom();
1522                $html->stripRNAttrValues = false;
1523                $html->load($str, true, false);
1524
1525                $preview = $html->find('div.preview');
1526                if (is_array($preview) === true && count($preview) > 0) {
1527                    $content = $preview[0]->innertext;
1528                }
1529            }
1530
1531            $page_regex = '/(.*)/';
1532            if (stripos($str, '<pre') !== false) {
1533                $page_regex = '/<(?!pre|\/).*?>(.*)[^<]*/';
1534            }
1535
1536            $content = preg_replace_callback($page_regex, function ($icons) {
1537                $iconTag = $this->getConf('iconTag', 'icon');
1538
1539                return preg_replace_callback(
1540                    '/&lt;' . $iconTag . ' ([\w\- #]*)&gt;(?=[^>]*(<|$))/',
1541                    function ($matches) {
1542                        global $MIKIO_ICONS;
1543
1544                        $s = $matches[0];
1545
1546                        if (count($MIKIO_ICONS) > 0) {
1547                            $icon = $MIKIO_ICONS[0];
1548
1549                            if (count($matches) > 1) {
1550                                $e = explode(' ', $matches[1]);
1551
1552                                if (count($e) > 1) {
1553                                    foreach ($MIKIO_ICONS as $iconItem) {
1554                                        if (strcasecmp($iconItem['name'], $e[0]) === 0) {
1555                                            $icon = $iconItem;
1556
1557                                            $s = $icon['insert'];
1558                                            for ($i = 1; $i < 9; $i++) {
1559                                                if (count($e) < $i || empty($e[$i]) === true) {
1560                                                    if (isset($icon['$' . $i]) === true) {
1561                                                        $s = str_replace('$' . $i, $icon['$' . $i], $s);
1562                                                    }
1563                                                } else {
1564                                                    $s = str_replace('$' . $i, $e[$i], $s);
1565                                                }
1566                                            }
1567
1568                                            $dir = '';
1569                                            if (isset($icon['dir']) === true) {
1570                                                $dir = $this->baseDir . 'icons/' . $icon['dir'] . '/';
1571                                            }
1572
1573                                            $s = str_replace('$0', $dir, $s);
1574
1575                                            break;
1576                                        }//end if
1577                                    }//end foreach
1578                                } else {
1579                                    $s = str_replace('$1', $matches[1], $icon['insert']);
1580                                }//end if
1581                            }//end if
1582                        }//end if
1583
1584                        $s = preg_replace('/(class=")(.*)"/', '$1mikio-icon $2"', $s, -1, $count);
1585                        if ($count === 0) {
1586                            $s = preg_replace('/(<\w* )/', '$1class="mikio-icon" ', $s);
1587                        }
1588
1589                        return $s;
1590                    },
1591                    $icons[0]
1592                );
1593            }, $content);
1594
1595            if (strcasecmp($ACT, 'preview') === 0) {
1596                if (is_array($preview) === true && count($preview) > 0) {
1597                    $preview[0]->innertext = $content;
1598                }
1599
1600                $str = $html->save();
1601                $html->clear();
1602                unset($html);
1603            } else {
1604                $str = $content;
1605            }
1606        }//end if
1607
1608        return $str;
1609    }
1610
1611    /**
1612     * Parse HTML for theme
1613     *
1614     * @param   string $content HTML content to parse.
1615     * @return  string          Parsed content
1616     */
1617    public function parseContent(string $content)
1618    {
1619        global $INPUT, $ACT;
1620
1621        // Add Mikio Section titles
1622        if (strcasecmp($INPUT->str('page'), 'config') === 0) {
1623            $admin_sections = [
1624                // Section      Insert Before                 Icon
1625                'navbar'        => ['navbarUseTitleIcon',      ''],
1626                'search'        => ['searchButton',            ''],
1627                'hero'          => ['heroTitle',               ''],
1628                'tags'          => ['tagsConsolidate',         ''],
1629                'breadcrumb'    => ['breadcrumbHideHome',      ''],
1630                'youarehere'    => ['youarehereHideHome',      ''],
1631                'sidebar'       => ['sidebarShowLeft',         ''],
1632                'toc'           => ['tocFull',                 ''],
1633                'pagetools'     => ['pageToolsFloating',       ''],
1634                'footer'        => ['footerCustomMenuText',    ''],
1635                'license'       => ['licenseType',             ''],
1636                'acl'           => ['includePageUseACL',       ''],
1637                'sticky'        => ['stickyTopHeader',         ''],
1638            ];
1639
1640            foreach ($admin_sections as $section => $items) {
1641                $search = $items[0];
1642                $icon   = $items[1];
1643
1644                $content = preg_replace(
1645                    '/<tr(.*)>\s*<td class="label">\s*<span class="outkey">(tpl»mikio»' . $search . ')<\/span>/',
1646                    '<tr$1><td class="mikio-config-table-header" colspan="2">' . $this->mikioInlineIcon($icon) .
1647                        tpl_getLang('config_' . $section) .
1648                        '</td></tr><tr class="default"><td class="label"><span class="outkey">tpl»mikio»' .
1649                        $search . '</span>',
1650                    $content
1651                );
1652            }
1653        } elseif (strcasecmp($INPUT->str('page'), 'styling') === 0) {
1654            $mikioPluginMissing = true;
1655            /* Hide plugin fields if not installed */
1656            if (plugin_load('action', 'mikioplugin') !== null) {
1657                $mikioPluginMissing = false;
1658            }
1659
1660            $style_headers = [
1661                ['title' => 'Base', 'starts_with' => '__text_'],
1662                ['title' => 'Code', 'starts_with' => '__code_'],
1663                ['title' => 'Controls', 'starts_with' => '__control_'],
1664                ['title' => 'Header', 'starts_with' => '__topheader_'],
1665                ['title' => 'Navbar', 'starts_with' => '__navbar_'],
1666                ['title' => 'Sub Navbar', 'starts_with' => '__subnavbar_'],
1667                ['title' => 'Tags', 'starts_with' => '__tag_background_color_'],
1668                ['title' => 'Breadcrumbs', 'starts_with' => '__breadcrumb_'],
1669                ['title' => 'Hero', 'starts_with' => '__hero_'],
1670                ['title' => 'Sidebar', 'starts_with' => '__sidebar_'],
1671                ['title' => 'Content', 'starts_with' => '__content_'],
1672                ['title' => 'TOC', 'starts_with' => '__toc_'],
1673                ['title' => 'Page Tools', 'starts_with' => '__pagetools_'],
1674                ['title' => 'Footer', 'starts_with' => '__footer_'],
1675                ['title' => 'Table', 'starts_with' => '__table_'],
1676                ['title' => 'Dropdown', 'starts_with' => '__dropdown_'],
1677                ['title' => 'Section Edit', 'starts_with' => '__section_edit_'],
1678                ['title' => 'Tree', 'starts_with' => '__tree_'],
1679                ['title' => 'Tabs', 'starts_with' => '__tab_'],
1680                ['title' => 'Mikio Plugin', 'starts_with' => '__plugin_', 'heading' => 'h2',
1681                    'hidden' => $mikioPluginMissing
1682                ],
1683                ['title' => 'Primary Colours', 'starts_with' => '__plugin_primary_', 'hidden' => $mikioPluginMissing],
1684                ['title' => 'Secondary Colours', 'starts_with' => '__plugin_secondary_',
1685                    'hidden' => $mikioPluginMissing
1686                ],
1687                ['title' => 'Success Colours', 'starts_with' => '__plugin_success_', 'hidden' => $mikioPluginMissing],
1688                ['title' => 'Danger Colours', 'starts_with' => '__plugin_danger_', 'hidden' => $mikioPluginMissing],
1689                ['title' => 'Warning Colours', 'starts_with' => '__plugin_warning_', 'hidden' => $mikioPluginMissing],
1690                ['title' => 'Info Colours', 'starts_with' => '__plugin_info_', 'hidden' => $mikioPluginMissing],
1691                ['title' => 'Light Colours', 'starts_with' => '__plugin_light_', 'hidden' => $mikioPluginMissing],
1692                ['title' => 'Dark Colours', 'starts_with' => '__plugin_dark_', 'hidden' => $mikioPluginMissing],
1693                ['title' => 'Link Colours', 'starts_with' => '__plugin_link_', 'hidden' => $mikioPluginMissing],
1694                ['title' => 'Carousel', 'starts_with' => '__plugin_carousel_', 'hidden' => $mikioPluginMissing],
1695                ['title' => 'Steps', 'starts_with' => '__plugin_steps_', 'hidden' => $mikioPluginMissing],
1696                ['title' => 'Tabgroup', 'starts_with' => '__plugin_tabgroup_', 'hidden' => $mikioPluginMissing],
1697                ['title' => 'Tooltip', 'starts_with' => '__plugin_tooltip_', 'hidden' => $mikioPluginMissing],
1698                ['title' => 'Dark Mode', 'starts_with' => '__darkmode_', 'heading' => 'h2'],
1699                ['title' => 'Base', 'starts_with' => '__darkmode_text_'],
1700                ['title' => 'Code', 'starts_with' => '__darkmode_code_'],
1701                ['title' => 'Controls', 'starts_with' => '__darkmode_control_'],
1702                ['title' => 'Header', 'starts_with' => '__darkmode_topheader_'],
1703                ['title' => 'Navbar', 'starts_with' => '__darkmode_navbar_'],
1704                ['title' => 'Sub Navbar', 'starts_with' => '__darkmode_subnavbar_'],
1705                ['title' => 'Tags', 'starts_with' => '__darkmode_tag_background_color_'],
1706                ['title' => 'Breadcrumbs', 'starts_with' => '__darkmode_breadcrumb_'],
1707                ['title' => 'Hero', 'starts_with' => '__darkmode_hero_'],
1708                ['title' => 'Sidebar', 'starts_with' => '__darkmode_sidebar_'],
1709                ['title' => 'Content', 'starts_with' => '__darkmode_content_'],
1710                ['title' => 'TOC', 'starts_with' => '__darkmode_toc_'],
1711                ['title' => 'Page Tools', 'starts_with' => '__darkmode_pagetools_'],
1712                ['title' => 'Footer', 'starts_with' => '__darkmode_footer_'],
1713                ['title' => 'Table', 'starts_with' => '__darkmode_table_'],
1714                ['title' => 'Dropdown', 'starts_with' => '__darkmode_dropdown_'],
1715                ['title' => 'Section Edit', 'starts_with' => '__darkmode_section_edit_'],
1716                ['title' => 'Tree', 'starts_with' => '__darkmode_tree_'],
1717                ['title' => 'Tabs', 'starts_with' => '__darkmode_tab_'],
1718                ['title' => 'Mikio Plugin (Dark mode)', 'starts_with' => '__plugin_darkmode_', 'heading' => 'h2',
1719                    'hidden' => $mikioPluginMissing
1720                ],
1721                ['title' => 'Primary Colours', 'starts_with' => '__plugin_darkmode_primary_',
1722                    'hidden' => $mikioPluginMissing
1723                ],
1724                ['title' => 'Secondary Colours', 'starts_with' => '__plugin_darkmode_secondary_',
1725                    'hidden' => $mikioPluginMissing
1726                ],
1727                ['title' => 'Success Colours', 'starts_with' => '__plugin_darkmode_success_',
1728                    'hidden' => $mikioPluginMissing
1729                ],
1730                ['title' => 'Danger Colours', 'starts_with' => '__plugin_darkmode_danger_',
1731                    'hidden' => $mikioPluginMissing
1732                ],
1733                ['title' => 'Warning Colours', 'starts_with' => '__plugin_darkmode_warning_',
1734                    'hidden' => $mikioPluginMissing
1735                ],
1736                ['title' => 'Info Colours', 'starts_with' => '__plugin_darkmode_info_',
1737                    'hidden' => $mikioPluginMissing
1738                ],
1739                ['title' => 'Light Colours', 'starts_with' => '__plugin_darkmode_light_',
1740                    'hidden' => $mikioPluginMissing
1741                ],
1742                ['title' => 'Dark Colours', 'starts_with' => '__plugin_darkmode_dark_',
1743                    'hidden' => $mikioPluginMissing
1744                ],
1745                ['title' => 'Link Colours', 'starts_with' => '__plugin_darkmode_link_',
1746                    'hidden' => $mikioPluginMissing
1747                ],
1748                ['title' => 'Carousel', 'starts_with' => '__plugin_darkmode_carousel_',
1749                    'hidden' => $mikioPluginMissing
1750                ],
1751                ['title' => 'Steps', 'starts_with' => '__plugin_darkmode_steps_', 'hidden' => $mikioPluginMissing],
1752                ['title' => 'Tabgroup', 'starts_with' => '__plugin_darkmode_tabgroup_',
1753                    'hidden' => $mikioPluginMissing
1754                ],
1755                ['title' => 'Tooltip', 'starts_with' => '__plugin_darkmode_tooltip_', 'hidden' => $mikioPluginMissing],
1756            ];
1757
1758            foreach ($style_headers as $header) {
1759                if (array_key_exists('heading', $header) === false) {
1760                    $header['heading'] = 'h3';
1761                }
1762
1763                if (array_key_exists('hidden', $header) === false) {
1764                    $header['hidden'] = false;
1765                }
1766
1767                $content = preg_replace(
1768                    '/(<tr>\s*<td>\s*<label for="tpl__' . $header['starts_with'] . '.+?<\/tr>)/s',
1769                    '</tbody></table><' . $header['heading'] . ' style="display:' .
1770                    ($header['hidden'] === true ? 'none' : 'block') . '">' .
1771                    $header['title'] . '</' . $header['heading'] . '>
1772                    <table style="display:' . ($header['hidden'] === true ? 'none' : 'table') . '"><tbody>$1',
1773                    $content,
1774                    1
1775                );
1776            }
1777
1778            $content = preg_replace('/type="color"/', 'type="text"', $content);
1779        }//end if
1780
1781        if (strcasecmp($ACT, 'admin') === 0 && isset($_GET['page']) === false) {
1782            $content = preg_replace('/(<ul.*?>.*?)<\/ul>.*?<ul.*?>(.*?<\/ul>)/s', '$1$2', $content);
1783        }
1784
1785        // Page Revisions - Table Fix
1786        if (strpos($content, 'id="page__revisions"') !== false) {
1787            $content = preg_replace(
1788                '/(<span class="sum">\s.*<\/span>\s.*<span class="user">\s.*<\/span>)/',
1789                '<span>$1</span>',
1790                $content
1791            );
1792        }
1793
1794        $html = new \simple_html_dom();
1795        $html->stripRNAttrValues = false;
1796        $html->load($content, true, false);
1797
1798        if ($html === false) {
1799            return $content;
1800        }
1801
1802        /* Buttons */
1803        foreach ($html->find('#config__manager button') as $node) {
1804            $c = explode(' ', $node->class);
1805            if (in_array('mikio-button', $c) === false) {
1806                $c[] = 'mikio-button';
1807            }
1808            $node->class = implode(' ', $c);
1809        }
1810
1811
1812        /* Buttons - Primary */
1813        foreach ($html->find('#config__manager [type=submit]') as $node) {
1814            $c = explode(' ', $node->class);
1815            if (in_array('mikio-primary', $c) === false) {
1816                $c[] = 'mikio-primary';
1817            }
1818            $node->class = implode(' ', $c);
1819        }
1820
1821        /* Hide page title if hero is enabled */
1822        if ($this->getConf('heroTitle') === true && $ACT !== 'preview') {
1823            $pageTitle = $this->parsePageTitle();
1824
1825            foreach ($html->find('h1,h2,h3,h4') as $elm) {
1826                if ($elm->innertext === $pageTitle) {
1827                    // $elm->innertext = '';
1828                    $elm->setAttribute('style', 'display:none');
1829
1830                    break;
1831                }
1832            }
1833        }
1834
1835        /* Hero subtitle */
1836        foreach ($html->find('p') as $elm) {
1837            $i = stripos($elm->innertext, '~~hero-subtitle');
1838            if ($i !== false) {
1839                $j = strpos($elm->innertext, '~~', ($i + 2));
1840                if ($j !== false) {
1841                    if ($j > ($i + 16)) {
1842                        $subtitle = substr($elm->innertext, ($i + 16), ($j - $i - 16));
1843                        $this->footerScript['hero-subtitle'] = 'mikio.setHeroSubTitle(\'' . $subtitle . '\')';
1844
1845                        // $elm->innertext = substr($elm->innertext, 0, $i + 2) . substr($elm->innertext, $j + 2);
1846                        $elm->innertext = preg_replace('/~~hero-subtitle (.+?)~~.*/ui', '', $elm->innertext);
1847                    }
1848
1849                    break;
1850                }
1851            }
1852        }
1853
1854        /* Hero image */
1855        foreach ($html->find('p') as $elm) {
1856            $image = '';
1857            preg_match('/~~hero-image (.+?)~~(?!.?")/ui', $elm->innertext, $matches);
1858            if (count($matches) > 0) {
1859                preg_match('/<img.*src="(.+?)"/ui', $matches[1], $imageTagMatches);
1860                if (count($imageTagMatches) > 0) {
1861                    $image = $imageTagMatches[1];
1862                } else {
1863                    preg_match('/<a.+?>(.+?)[~<]/ui', $matches[1], $imageTagMatches);
1864                    if (count($imageTagMatches) > 0) {
1865                        $image = $imageTagMatches[1];
1866                    } else {
1867                        $image = strip_tags($matches[1]);
1868                        if (stripos($image, ':') === false) {
1869                            $image = str_replace(['{', '}'], '', $image);
1870                            $i = stripos($image, '?');
1871                            if ($i !== false) {
1872                                $image = substr($image, 0, $i);
1873                            }
1874
1875                            $image = ml($image, '', true, '', false);
1876                        }
1877                    }
1878                }
1879
1880                $this->footerScript['hero-image'] = 'mikio.setHeroImage(\'' . $image . '\')';
1881
1882                $elm->innertext = preg_replace('/~~hero-image (.+?)~~.*/ui', '', $elm->innertext);
1883            }//end if
1884        }//end foreach
1885
1886        /* Hero colors - ~~hero-colors [background-color] [hero-title-color] [hero-subtitle-color]
1887        [breadcrumb-text-color] [breadcrumb-hover-color] (use 'initial' for original color) */
1888        foreach ($html->find('p') as $elm) {
1889            $i = stripos($elm->innertext, '~~hero-colors');
1890            if ($i !== false) {
1891                $j = strpos($elm->innertext, '~~', ($i + 2));
1892                if ($j !== false) {
1893                    if ($j > ($i + 14)) {
1894                        $color = substr($elm->innertext, ($i + 14), ($j - $i - 14));
1895                        $this->footerScript['hero-colors'] = 'mikio.setHeroColor(\'' . $color . '\')';
1896
1897                        $elm->innertext = preg_replace('/~~hero-colors (.+?)~~.*/ui', '', $elm->innertext);
1898                    }
1899
1900                    break;
1901                }
1902            }
1903        }
1904
1905        /* Hide parts - ~~hide-parts [parts]~~  */
1906        foreach ($html->find('p') as $elm) {
1907            $i = stripos($elm->innertext, '~~hide-parts');
1908            if ($i !== false) {
1909                $j = strpos($elm->innertext, '~~', ($i + 2));
1910                if ($j !== false) {
1911                    if ($j > ($i + 13)) {
1912                        $parts = explode(' ', substr($elm->innertext, ($i + 13), ($j - $i - 13)));
1913                        $script = '';
1914
1915                        foreach ($parts as $part) {
1916                            // $part = trim($part);
1917                            if (strlen($part) > 0) {
1918                                $script .= 'mikio.hidePart(\'' . $part . '\');';
1919                            }
1920                        }
1921
1922                        if (strlen($script) > 0) {
1923                            $this->footerScript['hide-parts'] = $script;
1924                        }
1925
1926                        $elm->innertext = preg_replace('/~~hide-parts (.+?)~~.*/ui', '', $elm->innertext);
1927                    }
1928
1929                    break;
1930                }//end if
1931            }//end if
1932        }//end foreach
1933
1934
1935        /* Page Tags (tag plugin) */
1936        if ($this->getConf('tagsConsolidate') === true) {
1937            $tags = '';
1938            foreach ($html->find('div.tags a') as $elm) {
1939                $tags .= $elm->outertext;
1940            }
1941
1942            foreach ($html->find('div.tags') as $elm) {
1943                $elm->innertext = '';
1944                $elm->setAttribute('style', 'display:none');
1945            }
1946
1947            if (empty($tags) === false) {
1948                $this->footerScript['tags'] = 'mikio.setTags(\'' . $tags . '\')';
1949            }
1950        }
1951
1952        // Configuration Manager
1953        if (strcasecmp($INPUT->str('page'), 'config') === 0) {
1954            // Additional save buttons
1955            foreach ($html->find('#config__manager') as $cm) {
1956                $saveButtons = '';
1957
1958                foreach ($cm->find('p') as $elm) {
1959                    $saveButtons = $elm->outertext;
1960                    $saveButtons = str_replace('<p>', '<p style="text-align:right">', $saveButtons);
1961                    $elm->outertext = '';
1962                }
1963
1964                foreach ($cm->find('fieldset') as $elm) {
1965                    $elm->innertext .= $saveButtons;
1966                }
1967            }
1968        }
1969
1970        $content = $html->save();
1971        $html->clear();
1972        unset($html);
1973
1974        return $content;
1975    }
1976
1977
1978    /**
1979     * Get DokuWiki namespace/page/URI as link
1980     *
1981     * @param   string $str String to parse.
1982     * @return  string      parsed URI
1983     */
1984    public function getLink(string $str)
1985    {
1986        $i = strpos($str, '://');
1987        if ($i !== false) {
1988            return $str;
1989        }
1990
1991        return wl($str);
1992    }
1993
1994
1995    /**
1996     * Check if the user can edit current namespace/page
1997     *
1998     * @return  boolean  user can edit
1999     */
2000    public function userCanEdit()
2001    {
2002        global $INFO;
2003        global $ID;
2004
2005        $wiki_file = wikiFN($ID);
2006        if (@file_exists($wiki_file) === false) {
2007            return true;
2008        }
2009        if ($INFO['isadmin'] === true || $INFO['ismanager'] === true) {
2010            return true;
2011        }
2012        // $meta_file = metaFN($ID, '.meta');
2013        if ($INFO['meta']['user'] === false) {
2014            return true;
2015        }
2016        if ($INFO['client'] === $INFO['meta']['user']) {
2017            return true;
2018        }
2019
2020        return false;
2021    }
2022
2023
2024    /**
2025     * Search for and return the uri of a media file
2026     *
2027     * @param string  $image           Image name to search for (without extension).
2028     * @param boolean $searchCurrentNS Search the current namespace.
2029     * @param boolean $propagate       Propagate search through the namespace.
2030     * @return string                  URI of the found media file
2031     */
2032    public function getMediaFile(string $image, bool $searchCurrentNS = true, bool $propagate = true)
2033    {
2034        global $INFO;
2035
2036        $ext = ['png', 'jpg', 'gif', 'svg'];
2037
2038        if ($searchCurrentNS === true) {
2039            $prefix[] = ':' . $INFO['namespace'] . ':';
2040        }
2041        if ($propagate === true) {
2042            $prefix[] = ':';
2043            $prefix[] = ':wiki:';
2044        }
2045        $theme = $this->getConf('customTheme');
2046        if (empty($theme) === false) {
2047            $prefix[] = 'themes/' . $theme . '/images/';
2048        }
2049        $prefix[] = 'images/';
2050
2051        $search = [];
2052        foreach ($prefix as $pitem) {
2053            foreach ($ext as $eitem) {
2054                $search[] = $pitem . $image . '.' . $eitem;
2055            }
2056        }
2057
2058        $img = '';
2059        $file = '';
2060        $url = '';
2061        $ismedia = false;
2062        $found = false;
2063
2064        foreach ($search as $img) {
2065            if (strcasecmp(substr($img, 0, 1), ':') === 0) {
2066                $file    = mediaFN($img);
2067                $ismedia = true;
2068            } else {
2069                $file    = tpl_incdir() . $img;
2070                $ismedia = false;
2071            }
2072
2073            if (file_exists($file) === true) {
2074                $found = true;
2075                break;
2076            }
2077        }
2078
2079        if ($found === false) {
2080            return false;
2081        }
2082
2083        if ($ismedia === true) {
2084            $url = ml($img, '', true, '', false);
2085        } else {
2086            $url = tpl_basedir() . $img;
2087        }
2088
2089        return $url;
2090    }
2091
2092
2093    /**
2094     * Print or return the page title
2095     *
2096     * @param string $page Page id or empty string for current page.
2097     * @return string      generated content
2098     */
2099    public function getPageTitle(string $page = '')
2100    {
2101        global $ID, $conf;
2102
2103        $html = '';
2104
2105        if (empty($page) === true) {
2106            $page = $ID;
2107        }
2108
2109        $html = p_get_first_heading($page);
2110        $html = strip_tags($html);
2111        $html = preg_replace('/\s+/', ' ', $html);
2112        $html .= ' [' . strip_tags($conf['title']) . ']';
2113        $html = trim($html);
2114
2115        return $html;
2116    }
2117
2118
2119    /**
2120     * Return inline theme icon
2121     *
2122     * @param   string $type  Icon to retreive.
2123     * @param   string $class Classname to insert.
2124     * @return  string        HTML icon content
2125     */
2126    public function mikioInlineIcon(string $type, string $class = "")
2127    {
2128        if (is_array($class) === true) {
2129            $class = explode(' ', $class);
2130        }
2131
2132        if (strlen($class) > 0) {
2133            $class = ' ' . $class;
2134        }
2135
2136        switch ($type) {
2137            case 'wrench':
2138                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792
21391792" 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,
214019 -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,
2141-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,
2142435 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
2143131.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,
2144-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>';
2145            case 'file':
2146                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2147viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,235.38983,1277.8305)" id="g2991">
2148<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
21491280,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
2150q 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"
2151xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" /></g></svg>';
2152            case 'gear':
2153                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2154viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,121.49153,1285.4237)" id="g3027">
2155<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
2156181,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
215710,-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
2158-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
2159147,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
2160q 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,
216171.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
2162q 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
2163-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" />
2164</g></svg>';
2165            case 'user':
2166                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2167viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,197.42373,1300.6102)"><path d="M
21681408,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
216928,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,
2170-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
217150.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
2172-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,
21731408 863,1408 975.5,1295.5 1088,1183 1088,1024 z"/></g></svg>';
2174            case 'search':
2175                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"
2176aria-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
217718.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
217824.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
21796.195 0 0 1-6.188 6.188z"/></svg>';
2180            case 'home':
2181                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2182viewBox="0 -256 1792 1792" aria-hidden="true" style="fill:currentColor"><g
2183transform="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
2184960 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
2185m 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
2186-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,
2187-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"
2188xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" /></g></svg>';
2189            case 'sun':
2190                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2191style="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
21920 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
21930a.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
21941-.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
21950 0 1-.707.707z" /></svg>';
2196            case 'moon':
2197                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg"
2198style="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
21994.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
22001 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
22011.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
22020-8.343-3.714-8.343-8.29 0-1.167.242-2.278.681-3.286z" /></svg>';
2203            case 'sunmoon':
2204                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" style="fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" viewBox="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 x1="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" y2="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,2.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>';
2205            case 'hamburger':
2206                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"
2207style="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
220876v40c0 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
220916v40c0 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
221016v40c0 8.837 7.163 16 16 16z"/></svg>';
2211            case 'down-arrow':
2212                return '<svg class="mikio-iicon' . $class . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"
2213aria-hidden="true" style="fill:currentColor"><path d="M16.003 18.626l7.081-7.081L25 13.46l-8.997 8.998-9.003-9
22141.917-1.916z"/></svg>';
2215        }//end switch
2216
2217        return '';
2218    }
2219
2220    /**
2221     * Finalize theme
2222     *
2223     * @return void
2224     */
2225    public function finalize()
2226    {
2227    }
2228
2229    /**
2230     * Show Messages
2231     *
2232     * @return void
2233     */
2234    public function showMessages()
2235    {
2236        global $ACT;
2237
2238        if ($this->lessIgnored === true) {
2239            msg(
2240                'useLESS is enabled on the Mikio template, however is not supported on this server',
2241                2,
2242                '',
2243                '',
2244                MSG_ADMINS_ONLY
2245            );
2246        }
2247
2248        $show = $this->getConf('showNotifications');
2249        if (
2250            strcasecmp($show, 'always') === 0 ||
2251            (strcasecmp($show, 'admin') === 0 && strcasecmp($ACT, 'admin') === 0)
2252        ) {
2253            global $MSG, $MSG_shown;
2254
2255            if (isset($MSG) === false) {
2256                return;
2257            }
2258
2259            if (isset($MSG_shown) === false) {
2260                $MSG_shown = [];
2261            }
2262
2263            foreach ($MSG as $msg) {
2264                $hash = md5($msg['msg']);
2265                if (isset($MSG_shown[$hash]) === true) {
2266                    continue;
2267                }
2268                // skip double messages
2269
2270                if (info_msg_allowed($msg) === true) {
2271                    echo '<div class="' . $msg['lvl'] . '">';
2272                    echo $msg['msg'];
2273                    echo '</div>';
2274                }
2275
2276                $MSG_shown[$hash] = true;
2277            }
2278
2279            unset($GLOBALS['MSG']);
2280        }//end if
2281    }
2282
2283    /**
2284     * Dokuwiki version
2285     *
2286     * @return  string        the dw version name
2287     */
2288    public function dwVersion()
2289    {
2290        if (function_exists('getVersionData') === true) {
2291            $version_data = getVersionData();
2292            if (is_array($version_data) === true && array_key_exists('date', $version_data) === true) {
2293                $version_items = explode(' ', $version_data['date']);
2294                if (count($version_items) >= 2) {
2295                    return preg_replace('/[^a-zA-Z0-9 ]+/', '', strtolower($version_items[1]));
2296                }
2297            }
2298        }
2299
2300        return 'unknown';
2301    }
2302
2303    /**
2304     * Dokuwiki version number
2305     *
2306     * @return  string        the dw version date converted to integer
2307     */
2308    public function dwVersionNumber()
2309    {
2310        if (function_exists('getVersionData') === true) {
2311            $version_data = getVersionData();
2312            if (is_array($version_data) === true && array_key_exists('date', $version_data) === true) {
2313                $version_items = explode(' ', $version_data['date']);
2314                if (count($version_items) >= 1) {
2315                    return intval(preg_replace('/[^0-9]+/', '', strtolower($version_items[0])));
2316                }
2317            }
2318        }
2319
2320        return 0;
2321    }
2322}
2323
2324global $TEMPLATE;
2325$TEMPLATE = \dokuwiki\template\mikio\Template::getInstance();
2326