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