xref: /template/mikio/mikio.php (revision b032613882034e7d2f046db5321b30e03179c541)
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')) die();
14
15require_once('icons/icons.php');
16require_once('inc/simple_html_dom.php');
17
18class Template
19{
20    public $tplDir  = '';
21    public $baseDir = '';
22    public $footerScript = array();
23    public $lessIgnored = false;
24
25
26    /**
27     * Class constructor
28     */
29    public function __construct()
30    {
31        $this->tplDir  = tpl_incdir();
32        $this->baseDir = tpl_basedir();
33
34        $this->_registerHooks();
35    }
36
37
38    /**
39     * Returns the instance of the class
40     *
41     * @return  Template        class instance
42     */
43    public static function getInstance()
44    {
45        static $instance = null;
46
47        if ($instance === null) {
48            $instance = new Template();
49        }
50
51        return $instance;
52    }
53
54
55    /**
56     * Register the themes hooks into Dokuwiki
57     */
58    private function _registerHooks()
59    {
60        global $EVENT_HANDLER;
61
62        $events_dispatcher = array(
63            'TPL_METAHEADER_OUTPUT'     => 'metaheadersHandler'
64        );
65
66        foreach ($events_dispatcher as $event => $method) {
67            $EVENT_HANDLER->register_hook($event, 'BEFORE', $this, $method);
68        }
69    }
70
71
72    /**
73     * Meta handler hook for DokuWiki
74     *
75     * @param   Doku_Event  $event
76     */
77    public function metaHeadersHandler(\Doku_Event $event)
78    {
79        global $MIKIO_ICONS;
80        global $conf;
81
82        $this->includePage('theme', FALSE, TRUE);
83
84        $stylesheets    = array();
85        $scripts        = array();
86
87        if ($this->getConf('customTheme') != '') {
88            if (file_exists($this->tplDir . 'themes/' . $this->getConf('customTheme') . '/style.less')) {
89                $stylesheets[] = $this->baseDir . 'themes/' . $this->getConf('customTheme') . '/style.less';
90            } else {
91                if (file_exists($this->tplDir . 'themes/' . $this->getConf('customTheme') . '/style.css')) {
92                    $stylesheets[] = $this->baseDir . 'themes/' . $this->getConf('customTheme') . '/style.css';
93                }
94            }
95            if (file_exists($this->tplDir . 'themes/' . $this->getConf('customTheme') . '/script.js')) {
96                $scripts[] = $this->baseDir . 'themes/' . $this->getConf('customTheme') . '/script.js';
97            }
98        }
99
100        if (is_array($MIKIO_ICONS) && $this->getConf('iconTag', 'icon') != '') {
101            $icons = array();
102            foreach ($MIKIO_ICONS as $icon) {
103                if (isset($icon['name']) && isset($icon['css']) && isset($icon['insert'])) {
104                    $icons[] = $icon;
105
106                    if ($icon['css'] != '') {
107                        if (strpos($icon['css'], '//') === FALSE) {
108                            $stylesheets[] = $this->baseDir . 'icons/' . $icon['css'];
109                        } else {
110                            $stylesheets[] = $icon['css'];
111                        }
112                    }
113                }
114            }
115            $MIKIO_ICONS = $icons;
116        } else {
117            $MIKIO_ICONS = [];
118        }
119
120        $scripts[] = $this->baseDir . 'assets/mikio-typeahead.js';
121        $scripts[] = $this->baseDir . 'assets/mikio.js';
122
123        if ($this->getConf('useLESS')) {
124            $stylesheets[] = $this->baseDir . 'assets/mikio.less';
125        } else {
126            $stylesheets[] = $this->baseDir . 'assets/mikio.css';
127        }
128
129
130        $set = [];
131        foreach ($stylesheets as $style) {
132            if (in_array($style, $set) == FALSE) {
133                if (strtolower(substr($style, -5)) == '.less' && $this->getConf('useLESS')) {
134                    $style = $this->baseDir . 'css.php?css=' . str_replace($this->baseDir, '', $style);
135                }
136
137                array_unshift($event->data['link'], array(
138                    'type' => 'text/css',
139                    'rel'  => 'stylesheet',
140                    'href' => $style
141                ));
142            }
143            $set[] = $style;
144        }
145
146        $set = [];
147        foreach ($scripts as $script) {
148            if (in_array($script, $set) == FALSE) {
149                $script_params = array(
150                    'type'  => 'text/javascript',
151                    '_data' => '',
152                    'src'   => $script
153                );
154
155                // equal to or greator than hogfather
156                if($this->dwVersionNumber() >= 20200729) {
157                    // greator than hogfather - defer always on
158                    if($this->dwVersionNumber() >= 20200729) {
159                        $script_params += ['defer' => 'defer'];
160                    } else {
161                        // hogfather - defer always on unless $conf['defer_js'] is false
162                        if(!array_key_exists('defer_js', $conf) || $conf['defer_js']) {
163                            $script_params += ['defer' => 'defer'];
164                        }
165                    }
166                }
167
168                $event->data['script'][] = $script_params;
169            }
170            $set[] = $script;
171        }
172    }
173
174
175    /**
176     * Print or return the footer meta data
177     *
178     * @param   boolean $print      print the data to buffer
179     */
180    public function includeFooterMeta($print = TRUE)
181    {
182        $html = '';
183
184        if (count($this->footerScript) > 0) {
185            $html .= '<script type="text/javascript">function mikioFooterRun() {';
186            foreach ($this->footerScript as $script) {
187                $html .= $script . ';';
188            }
189            $html .= '}</script>';
190        }
191
192
193        if ($print) echo $html;
194        return $html;
195    }
196
197    /**
198     * Retreive and parse theme configuration options
199     *
200     * @param   string  $key        the configuration key to retreive
201     * @param   mixed   $default    if key doesn't exist, return this value
202     * @return  mixed               parsed value of configuration
203     */
204    public function getConf($key, $default = FALSE)
205    {
206        $value = tpl_getConf($key, $default);
207
208        switch ($key) {
209            case 'navbarDWMenuType':
210                $value = strtolower($value);
211                if ($value != 'icons' && $value != 'text' && $value != 'both') $value = 'both';
212                break;
213            case 'navbarDWMenuCombine':
214                $value = strtolower($value);
215                if ($value != 'seperate' && $value != 'dropdown' && $value != 'combine') $value = 'combine';
216                break;
217            case 'navbarPosLeft':
218            case 'navbarPosMiddle':
219            case 'navbarPosRight':
220                $value = strtolower($value);
221                if ($value != 'none' && $value != 'custom' && $value != 'search' && $value != 'dokuwiki') {
222                    if ($key == 'navbarPosLeft') $value = 'none';
223                    if ($key == 'navbarPosMiddle') $value = 'search';
224                    if ($key == 'navbarPosRight') $value = 'dokuwiki';
225                }
226                break;
227            case 'navbarItemShowCreate':
228            case 'navbarItemShowShow':
229            case 'navbarItemShowRevs':
230            case 'navbarItemShowBacklink':
231            case 'navbarItemShowRecent':
232            case 'navbarItemShowMedia':
233            case 'navbarItemShowIndex':
234            case 'navbarItemShowProfile':
235            case 'navbarItemShowAdmin':
236                $value = strtolower($value);
237                if ($value != 'always' && $value != 'logged in' && $value != 'logged out' && $value != 'never') {
238                    $value = 'always';
239                }
240                break;
241            case 'navbarItemShowLogin':
242            case 'navbarItemShowLogout':
243                $value = strtolower($value);
244                if ($value != 'always' && $value != 'never') {
245                    $value = 'always';
246                }
247                break;
248            case 'searchButton':
249                $value = strtolower($value);
250                if ($value != 'icon' && $value != 'text') $value = 'icon';
251                break;
252            case 'searchButton':
253                $value = strtolower($value);
254                if ($value != 'icon' && $value != 'text') $value = 'icon';
255                break;
256            case 'breadcrumbPosition':
257                $value = strtolower($value);
258                if ($value != 'none' && $value != 'top' && $value != 'hero' && $value != 'page') $value = 'top';
259                break;
260            case 'youareherePosition':
261                $value = strtolower($value);
262                if ($value != 'none' && $value != 'top' && $value != 'hero' && $value != 'page') $value = 'top';
263                break;
264            case 'youarehereHome':
265                $value = strtolower($value);
266                if ($value != 'none' && $value != 'page title' && $value != 'home' && $value != 'icon') $value = 'page title';
267                break;
268            case 'sidebarLeftRow1':
269            case 'sidebarLeftRow2':
270            case 'sidebarLeftRow3':
271            case 'sidebarLeftRow4':
272                $value = strtolower($value);
273                if ($value != 'none' && $value != 'logged in user' && $value != 'search' && $value != 'content' && $value != 'tags') {
274                    if ($key == 'sidebarLeftRow1') $value = 'logged in user';
275                    if ($key == 'sidebarLeftRow2') $value = 'search';
276                    if ($key == 'sidebarLeftRow3') $value = 'content';
277                    if ($key == 'sidebarLeftRow4') $value = 'none';
278                }
279                break;
280            case 'pageToolsFloating':
281            case 'pageToolsFooter':
282                $value = strtolower($value);
283                if ($value != 'none' && $value != 'page editors' && $value != 'always') {
284                    if ($key == 'pageToolsFloating') $value = 'always';
285                    if ($key == 'pageToolsFooter') $value = 'always';
286                }
287                break;
288            case 'pageToolsShowCreate':
289            case 'pageToolsShowEdit':
290            case 'pageToolsShowRevs':
291            case 'pageToolsShowBacklink':
292            case 'pageToolsShowTop':
293                $value = strtolower($value);
294                if ($value != 'always' && $value != 'logged in' && $value != 'logged out' && $value != 'never') {
295                    $value = 'always';
296                }
297                break;
298            case 'showNotifications':
299                $value = strtolower($value);
300                if ($value != 'none' && $value != 'admin' && $value != 'always') $value = 'admin';
301                break;
302            case 'licenseType':
303                $value = strtolower($value);
304                if ($value != 'none' && $value != 'badge' && $value != 'buttom') $value = 'badge';
305                break;
306            case 'navbarUseTitleIcon':
307            case 'navbarUseTitleText':
308            case 'navbarUseTaglineText':
309            case 'navbarShowSub':
310            case 'heroTitle':
311            case 'heroImagePropagation':
312            case 'breadcrumbPrefix':
313            case 'breadcrumbSep':
314            case 'youareherePrefix':
315            case 'youarehereSep':
316            case 'sidebarShowLeft':
317            case 'sidebarShowRight':
318            case 'tocFull':
319            case 'footerSearch':
320            case 'licenseImageOnly':
321            case 'includePageUseACL':
322            case 'includePagePropagate':
323            case 'youarehereHideHome':
324            case 'tagsConsolidate':
325            case 'footerInPage':
326            case 'sidebarMobileDefaultCollapse':
327            case 'sidebarAlwaysShowLeft':
328            case 'sidebarAlwaysShowRight':
329            case 'searchUseTypeahead':
330                $value = (bool)$value;
331                break;
332            case 'youarehereShowLast':
333                $value = (int)$value;
334                break;
335            case 'iconTag':
336            case 'customTheme':
337            case 'navbarCustomMenuText':
338            case 'breadcrumbPrefixText':
339            case 'breadcrumbSepText':
340            case 'youareherePrefixText':
341            case 'youarehereSepText':
342            case 'footerCustomMenuText':
343                break;
344            case 'useLESS':
345                $value = (bool)$value;
346                $lessAvailable = true;
347
348                // check for less library
349                $lesscLib = '../../../vendor/marcusschwarz/lesserphp/lessc.inc.php';
350                if (!file_exists($lesscLib))
351                    $lesscLib = $_SERVER['DOCUMENT_ROOT'] . '/vendor/marcusschwarz/lesserphp/lessc.inc.php';
352                if (!file_exists($lesscLib))
353                    $lesscLib = '../../../../../app/dokuwiki/vendor/marcusschwarz/lesserphp/lessc.inc.php';
354                if (!file_exists($lesscLib))
355                    $lesscLib = $_SERVER['DOCUMENT_ROOT'] . '/app/dokuwiki/vendor/marcusschwarz/lesserphp/lessc.inc.php';
356                if (!file_exists($lesscLib)) {
357                    $lessAvailable = false;
358                }
359
360                // check for ctype extensions
361                if (!function_exists('ctype_digit')) {
362                    $lessAvailable = false;
363                }
364
365                if ($value && !$lessAvailable) {
366                    $this->lessIgnored = true;
367                    $value = false;
368                }
369                break;
370        }
371
372        return $value;
373    }
374
375
376    /**
377     * Check if a page exist in directory or namespace
378     *
379     * @param   string  $page   page/namespace to search
380     * @return  boolean         if page exists
381     */
382    public function pageExists($page)
383    {
384        ob_start();
385        tpl_includeFile($page . '.html');
386        $html = ob_get_contents();
387        ob_end_clean();
388
389        if ($html != '') return TRUE;
390
391        $useACL = $this->getConf('includePageUseACL');
392        $propagate = $this->getConf('includePagePropagate');
393
394        if ($propagate) {
395            if (page_findnearest($page, $useACL)) return TRUE;
396        } elseif ($useACL && auth_quickaclcheck($page) != AUTH_NONE) {
397            return TRUE;
398        }
399
400        return FALSE;
401    }
402
403
404    /**
405     * Print or return page from directory or namespace
406     *
407     * @param   string  $page           page/namespace to include
408     * @param   boolean $print          print content
409     * @param   boolean $parse          parse content before printing/returning
410     * @param   string  $classWrapper   wrap page in a div with class
411     * @return  string                  contents of page found
412     */
413    public function includePage($page, $print = TRUE, $parse = TRUE, $classWrapper = '')
414    {
415        ob_start();
416        tpl_includeFile($page . '.html');
417        $html = ob_get_contents();
418        ob_end_clean();
419
420        if ($html == '') {
421            $useACL = $this->getConf('includePageUseACL');
422            $propagate = $this->getConf('includePagePropagate');
423            $html = '';
424
425            $html = tpl_include_page($page, false, $propagate, $useACL);
426        }
427
428        if ($html != '' && $parse) {
429            $html = $this->parseContent($html);
430        }
431
432        if ($classWrapper != '' && $html != '') $html = '<div class="' . $classWrapper . '">' . $html . '</div>';
433
434        if ($print) echo $html;
435        return $html;
436    }
437
438
439    /**
440     * Print or return logged in user information
441     *
442     * @param   boolean $print          print content
443     * @return  string                  user information
444     */
445    public function includeLoggedIn($print = TRUE)
446    {
447        $html = '';
448
449        if (!empty($_SERVER['REMOTE_USER'])) {
450            $html .= '<div class="mikio-user-info">';
451            ob_start();
452            tpl_userinfo();
453            $html .= ob_get_contents();
454            ob_end_clean();
455            $html .= '</div>';
456        }
457
458        if ($print) echo $html;
459        return $html;
460    }
461
462
463    /**
464     * Print or return DokuWiki Menu
465     *
466     * @param   boolean $print          print content
467     * @return  string                  contents of the menu
468     */
469    public function includeDWMenu($print = TRUE)
470    {
471        global $lang;
472        global $USERINFO;
473
474        $loggedIn = (is_array($USERINFO) && count($USERINFO) > 0);
475        $html = '<ul class="mikio-nav">';
476
477        $pageToolsMenu = [];
478        $siteToolsMenu = [];
479        $userToolsMenu = [];
480
481        $showIcons  = ($this->getConf('navbarDWMenuType') != 'text');
482        $showText   = ($this->getConf('navbarDWMenuType') != 'icons');
483        $isDropDown = ($this->getConf('navbarDWMenuCombine') != 'seperate');
484
485        $items = (new \dokuwiki\Menu\PageMenu())->getItems();
486        foreach ($items as $item) {
487            if ($item->getType() != 'top') {
488                $itemHtml = '';
489
490                $showItem = $this->getConf('navbarItemShow' . ucfirst($item->getType()));
491                if ($showItem !== false && ($showItem == 'always' || ($showItem == 'logged in' && $loggedIn) || ($showItem == 'logged out' && !$loggedIn))) {
492                    $itemHtml .= '<a class="mikio-nav-link ' . ($isDropDown ? 'mikio-dropdown-item' : '') . ' ' . $item->getType() . '" href="' . $item->getLink() . '" title="' . $item->getTitle() . '">';
493                    if ($showIcons) $itemHtml .= '<span class="mikio-icon">' . inlineSVG($item->getSvg()) . '</span>';
494                    if ($showText || $isDropDown) $itemHtml .= '<span>' . $item->getLabel() . '</span>';
495                    $itemHtml .= '</a>';
496
497                    $pageToolsMenu[] = $itemHtml;
498                }
499            }
500        }
501
502        $items = (new \dokuwiki\Menu\SiteMenu())->getItems('action');
503        foreach ($items as $item) {
504            $itemHtml = '';
505
506            $showItem = $this->getConf('navbarItemShow' . ucfirst($item->getType()));
507            if ($showItem !== false && ($showItem == 'always' || ($showItem == 'logged in' && $loggedIn) || ($showItem == 'logged out' && !$loggedIn))) {
508                $itemHtml .= '<a class="mikio-nav-link ' . ($isDropDown ? 'mikio-dropdown-item' : '') . ' ' . $item->getType() . '" href="' . $item->getLink() . '" title="' . $item->getTitle() . '">';
509                if ($showIcons) $itemHtml .= '<span class="mikio-icon">' . inlineSVG($item->getSvg()) . '</span>';
510                if ($showText || $isDropDown) $itemHtml .= '<span>' . $item->getLabel() . '</span>';
511                $itemHtml .= '</a>';
512
513                $siteToolsMenu[] = $itemHtml;
514            }
515        }
516
517        $items = (new \dokuwiki\Menu\UserMenu())->getItems('action');
518        foreach ($items as $item) {
519            $itemHtml = '';
520
521            $showItem = $this->getConf('navbarItemShow' . ucfirst($item->getType()));
522            if ($showItem !== false && ($showItem == 'always' || ($showItem == 'logged in' && $loggedIn) || ($showItem == 'logged out' && !$loggedIn))) {
523                $itemHtml .= '<a class="mikio-nav-link' . ($isDropDown ? ' mikio-dropdown-item' : '') . ' ' . $item->getType() . '" href="' . $item->getLink() . '" title="' . $item->getTitle() . '">';
524                if ($showIcons) $itemHtml .= '<span class="mikio-icon">' . inlineSVG($item->getSvg()) . '</span>';
525                if ($showText || $isDropDown) $itemHtml .= '<span>' . $item->getLabel() . '</span>';
526                $itemHtml .= '</a>';
527
528                $userToolsMenu[] = $itemHtml;
529            }
530        }
531
532
533        switch ($this->getConf('navbarDWMenuCombine')) {
534            case 'dropdown':
535                $html .= '<li id="dokuwiki__pagetools" class="mikio-nav-dropdown">';
536                $html .= '<a id="mikio_dropdown_pagetools" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . ($showIcons ? $this->mikioInlineIcon('file') : '') . ($showText ? $lang['page_tools'] : '<span class="mikio-small-only">' . $lang['page_tools'] . '</span>') . '</a>';
537                $html .= '<div class="mikio-dropdown closed">';
538
539                foreach ($pageToolsMenu as $item) {
540                    $html .= $item;
541                }
542
543                $html .= '</div>';
544                $html .= '</li>';
545
546                $html .= '<li id="dokuwiki__sitetools" class="mikio-nav-dropdown">';
547                $html .= '<a id="mikio_dropdown_sitetools" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . ($showIcons ? $this->mikioInlineIcon('gear') : '') . ($showText ? $lang['site_tools'] : '<span class="mikio-small-only">' . $lang['site_tools'] . '</span>') . '</a>';
548                $html .= '<div class="mikio-dropdown closed">';
549
550                foreach ($siteToolsMenu as $item) {
551                    $html .= $item;
552                }
553
554                $html .= '</div>';
555                $html .= '</li>';
556
557                $html .= '<li id="dokuwiki__usertools" class="mikio-nav-dropdown">';
558                $html .= '<a id="mikio_dropdown_usertools" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . ($showIcons ? $this->mikioInlineIcon('user') : '') . ($showText ? $lang['user_tools'] : '<span class="mikio-small-only">' . $lang['user_tools'] . '</span>') . '</a>';
559                $html .= '<div class="mikio-dropdown closed">';
560
561                foreach ($userToolsMenu as $item) {
562                    $html .= $item;
563                }
564
565                $html .= '</div>';
566                $html .= '</li>';
567
568                break;
569
570            case 'combine':
571                $html .= '<li class="mikio-nav-dropdown">';
572                $html .= '<a class="mikio-nav-link" href="#">' . ($showIcons ? $this->mikioInlineIcon('wrench') : '') . ($showText ? tpl_getLang('tools-menu') : '<span class="mikio-small-only">' . tpl_getLang('tools-menu') . '</span>') . '</a>';   // TODO change $lang
573                $html .= '<div class="mikio-dropdown closed">';
574
575                $html .= '<h6 class="mikio-dropdown-header">' . $lang['page_tools'] . '</h6>';
576                foreach ($pageToolsMenu as $item) {
577                    $html .= $item;
578                }
579
580                $html .= '<div class="mikio-dropdown-divider"></div>';
581                $html .= '<h6 class="mikio-dropdown-header">' . $lang['site_tools'] . '</h6>';
582                foreach ($siteToolsMenu as $item) {
583                    $html .= $item;
584                }
585
586                $html .= '<div class="mikio-dropdown-divider"></div>';
587                $html .= '<h6 class="mikio-dropdown-header">' . $lang['user_tools'] . '</h6>';
588                foreach ($userToolsMenu as $item) {
589                    $html .= $item;
590                }
591
592                $html .= '</div>';
593                $html .= '</li>';
594                break;
595
596            default:    // seperate
597                foreach ($siteToolsMenu as $item) {
598                    $html .= '<li class="mikio-nav-item">' . $item . '</li>';
599                }
600
601                foreach ($pageToolsMenu as $item) {
602                    $html .= '<li class="mikio-nav-item">' . $item . '</li>';
603                }
604
605                foreach ($userToolsMenu as $item) {
606                    $html .= '<li class="mikio-nav-item">' . $item . '</li>';
607                }
608
609                break;
610        }
611
612        $html .= '</ul>';
613
614        if ($print) echo $html;
615        return $html;
616    }
617
618
619    /**
620     * Create a nav element from a string. <uri>|<title>;
621     *
622     * @param string   $str     string to generate nav
623     * @return string           nav elements generated
624     */
625    public function stringToNav($str)
626    {
627        $html = '';
628
629        if ($str != '') {
630            $items = explode(';', $str);
631            if (count($items) > 0) {
632                $html .= '<ul class="mikio-nav">';
633                foreach ($items as $item) {
634                    $parts = explode('|', $item);
635                    if ($parts > 1) {
636                        $html .= '<li class="mikio-nav-item"><a class="mikio-nav-link" href="' . strip_tags($this->getLink(trim($parts[0]))) . '">' . strip_tags(trim($parts[1])) . '</a></li>';
637                    }
638                }
639                $html .= '</ul>';
640            }
641        }
642
643        return $html;
644    }
645
646    /**
647     * print or return the main navbar
648     *
649     * @param boolean   $print      print the navbar
650     * @param boolean   $showSub    include the sub navbar
651     * @return string               generated content
652     */
653    public function includeNavbar($print = TRUE, $showSub = FALSE)
654    {
655        global $conf;
656
657        $homeUrl = wl();
658
659        if (!plugin_isdisabled('showpageafterlogin')) {
660            $p = &plugin_load('action', 'showpageafterlogin');
661            if ($p) {
662                global $USERINFO;
663
664                if (is_array($USERINFO) && count($USERINFO) > 0) {
665                    $homeUrl = wl($p->getConf('page_after_login'));
666                }
667            }
668        }
669
670
671        $html = '';
672
673        $html .= '<nav class="mikio-navbar'  . (($this->getConf('stickyNavbar')) ? ' mikio-sticky' : '') . '">';
674        $html .= '<div class="mikio-container">';
675        $html .= '<a class="mikio-navbar-brand" href="' . $homeUrl . '">';
676        if ($this->getConf('navbarUseTitleIcon') || $this->getConf('navbarUseTitleText')) {
677
678            // Brand image
679            if ($this->getConf('navbarUseTitleIcon')) {
680                $logo = $this->getMediaFile('logo', FALSE);;
681                if ($logo != '') {
682                    $html .= '<img src="' . $logo . '" class="mikio-navbar-brand-image">';
683                }
684            }
685
686            // Brand title
687            if ($this->getConf('navbarUseTitleText')) {
688                $html .= '<div class="mikio-navbar-brand-title">';
689                $html .= '<h1 class="mikio-navbar-brand-title-text">' . $conf['title'] . '</h1>';
690                if ($this->getConf('navbarUseTaglineText')) {
691                    $html .= '<p class="claim mikio-navbar-brand-title-tagline">' . $conf['tagline'] . '</p>';
692                }
693                $html .= '</div>';
694            }
695        }
696        $html .= '</a>';
697        $html .= '<div class="mikio-navbar-toggle"><span class="icon"></span></div>';
698
699        // Menus
700        $html .= '<div class="mikio-navbar-collapse">';
701
702        $menus = array($this->getConf('navbarPosLeft', 'none'), $this->getConf('navbarPosMiddle', 'none'), $this->getConf('navbarPosRight', 'none'));
703        foreach ($menus as $menuType) {
704            switch ($menuType) {
705                case 'custom':
706                    $html .= $this->stringToNav($this->getConf('navbarCustomMenuText', ''));
707                    break;
708                case 'search':
709                    $html .= '<div class="mikio-nav-item">';
710                    $html .= $this->includeSearch(false);
711                    $html .= '</div>';
712                    break;
713                case 'dokuwiki':
714                    $html .= $this->includeDWMenu(FALSE);
715                    break;
716            }
717        }
718
719        $html .= '</div>';
720        $html .= '</div>';
721        $html .= '</nav>';
722
723        // Sub Navbar
724        if ($showSub) {
725            $sub = $this->includePage('submenu', FALSE);
726            if ($sub != '') $html .= '<nav class="mikio-navbar mikio-sub-navbar">' . $sub . '</nav>';
727        }
728
729        if ($print) echo $html;
730        return $html;
731    }
732
733
734    /**
735     * Is there a sidebar
736     *
737     * @param   string  $prefix     sidebar prefix to use when searching
738     * @return  boolean             if sidebar exists
739     */
740    public function sidebarExists($prefix = '')
741    {
742        global $conf;
743
744        if ($prefix == 'left') $prefix = '';
745
746        return $this->pageExists($conf['sidebar' . $prefix]);
747    }
748
749
750    /**
751     * Print or return the sidebar content
752     *
753     * @param   string  $prefix     sidebar prefix to use when searching
754     * @param   boolean $print      print the generated content to the output buffer
755     * @param   boolean $parse      parse the content
756     * @return  string              generated content
757     */
758    public function includeSidebar($prefix = '', $print = TRUE, $parse = TRUE)
759    {
760        global $conf, $ID;
761
762        $html = '';
763        $confPrefix = preg_replace('/[^a-zA-Z0-9]/', '', ucwords($prefix));
764        $prefix = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($prefix));
765
766        if ($confPrefix == '') $confPrefix = 'Left';
767        if ($prefix == 'Left') $prefix = '';
768
769        $sidebarPage = $conf[$prefix . 'sidebar'] == '' ? $prefix . 'sidebar' : $conf[$prefix . 'sidebar'];
770
771        if ($this->getConf('sidebarShow' . $confPrefix) && page_findnearest($sidebarPage) != FALSE && p_get_metadata($ID, 'nosidebar', FALSE) == FALSE) {
772            $content = $this->includePage($sidebarPage . 'header', FALSE);
773            if ($content != '') $html .= '<div class="mikio-sidebar-header">' . $content . '</div>';
774
775            if ($prefix == '') {
776                $rows = array($this->getConf('sidebarLeftRow1'), $this->getConf('sidebarLeftRow2'), $this->getConf('sidebarLeftRow3'), $this->getConf('sidebarLeftRow4'));
777
778                foreach ($rows as $row) {
779                    switch ($row) {
780                        case 'search':
781                            $html .= $this->includeSearch(FALSE);
782                            break;
783                        case 'logged in user':
784                            $html .= $this->includeLoggedIn(FALSE);
785                            break;
786                        case 'content':
787                            $content = $this->includePage($sidebarPage, FALSE);
788                            if ($content != '') $html .= '<div class="mikio-sidebar-content">' . $content . '</div>';
789                            break;
790                        case 'tags':
791                            $html .= '<div class="mikio-tags"></div>';
792                    }
793                }
794            } else {
795                $content = $this->includePage($sidebarPage, FALSE);
796                if ($content != '') $html .= '<div class="mikio-sidebar-content">' . $content . '</div>';
797            }
798
799            $content = $this->includePage($sidebarPage . 'footer', FALSE);
800            if ($content != '') $html .= '<div class="mikio-sidebar-footer">' . $content . '</div>';
801        }
802
803        if ($html == '') {
804            if ($prefix == '' && $this->getConf('sidebarAlwaysShowLeft')) $html = '&nbsp;';
805            if ($this->getConf('sidebarAlwaysShow' . ucfirst($prefix))) $html = '&nbsp;';
806        }
807
808        if ($html != '') {
809            $html = '<aside class="mikio-sidebar mikio-sidebar-' . ($prefix == '' ? 'left' : $prefix) . '"><a class="mikio-sidebar-toggle' . ($this->getConf('sidebarMobileDefaultCollapse') ? ' closed' : '') . '" href="#">' . tpl_getLang('sidebar-title') . ' <span class="icon"></span></a><div class="mikio-sidebar-collapse">' . $html . '</div></aside>';
810        }
811
812        if ($parse) $html = $this->includeIcons($html);
813        if ($print) echo $html;
814        return $html;
815    }
816
817
818    /**
819     * Print or return the page tools content
820     *
821     * @param   boolean $print      print the generated content to the output buffer
822     * @param   boolean $includeId  include the dw__pagetools id in the element
823     * @return  string              generated content
824     */
825    public function includePageTools($print = TRUE, $includeId = FALSE)
826    {
827        global $USERINFO;
828
829        $loggedIn = (is_array($USERINFO) && count($USERINFO) > 0);
830        $html = '';
831
832        $html .= '<nav' . ($includeId ? ' id="dw__pagetools"' : '') . ' class="hidden-print dw__pagetools">';
833        $html .= '<ul class="tools">';
834
835        $items = (new \dokuwiki\Menu\PageMenu())->getItems();
836        foreach ($items as $item) {
837            $classes = array();
838            $classes[] = $item->getType();
839            $attr = $item->getLinkAttributes();
840
841            if (!empty($attr['class'])) {
842                $classes = array_merge($classes, explode(' ', $attr['class']));
843            }
844
845            $classes = array_unique($classes);
846
847            $showItem = $this->getConf('pageToolsShow' . ucfirst($item->getType()));
848            if ($showItem !== false && ($showItem == 'always' || ($showItem == 'logged in' && $loggedIn) || ($showItem == 'logged out' && !$loggedIn))) {
849                $html .= '<li class="' . implode(' ', $classes) . '">';
850                $html .= '<a href="' . $item->getLink() . '" class="' . $item->getType() . '" title="' . $item->getTitle() . '"><div class="icon">' . inlineSVG($item->getSvg()) . '</div><span class="a11y">' . $item->getLabel() . '</span></a>';
851                $html .= '</li>';
852            }
853        }
854
855        $html .= '</ul>';
856        $html .= '</nav>';
857
858        if ($print) echo $html;
859        return $html;
860    }
861
862
863    /**
864     * Print or return the search bar
865     *
866     * @param   boolean $print          print content
867     * @return  string                  contents of the search bar
868     */
869    public function includeSearch($print = TRUE)
870    {
871        global $lang, $ID;
872        $html = '';
873
874        $html .= '<form class="mikio-search search" action="' . wl() . '" accept-charset="utf-8" method="get" role="search">';
875        $html .= '<input type="hidden" name="do" value="search">';
876        $html .= '<input type="hidden" name="id" value="' . $ID . '">';
877        $html .= '<input name="q" ';
878        if ($this->getConf('searchUseTypeahead')) {
879            $html .= 'class="search_typeahead" ';
880        }
881        $html .= 'autocomplete="off" type="search" placeholder="' . $lang['btn_search'] . '" value="' . (($ACT == 'search') ? htmlspecialchars($QUERY) : '') . '" accesskey="f" title="[F]" />';
882        $html .= '<button type="submit" title="' .  $lang['btn_search'] . '">';
883        if ($this->getConf('searchButton') == 'icon') {
884            $html .= $this->mikioInlineIcon('search');
885        } else {
886            $html .= $lang['btn_search'];
887        }
888        $html .= '</button>';
889        $html .= '</form>';
890
891
892
893        if ($print) print $html;
894        return $html;
895    }
896
897
898    /**
899     * Print or return content
900     *
901     * @param   boolean $print          print content
902     * @return  string                  contents
903     */
904    public function includeContent($print = TRUE)
905    {
906        ob_start();
907        tpl_content(FALSE);
908        $html = ob_get_contents();
909        ob_end_clean();
910
911        $html = $this->includeIcons($html);
912        $html = $this->parseContent($html);
913
914        $html .= '<div style="clear:both"></div>';
915
916        if (!$this->getConf('heroTitle')) $html = '<div class="mikio-tags"></div>' . $html;
917
918        $html = '<div class="mikio-article-content">' . $html . '</div>';
919
920        if ($print) echo $html;
921        return $html;
922    }
923
924    /**
925     * Print or return footer
926     *
927     * @param   boolean  $print     print footer
928     * @return  string              html string containing footer
929     */
930    public function includeFooter($print = TRUE)
931    {
932        global $ACT;
933
934        $html = '';
935
936        $html .= '<footer class="mikio-footer">';
937        $html .= '<div class="doc">' . tpl_pageinfo(TRUE) . '</div>';
938        $html .= $this->includePage('footer', FALSE);
939
940        $html .= $this->stringToNav($this->getConf('footerCustomMenuText'));
941
942        if ($this->getConf('footerSearch')) {
943            $html .= '<div class="mikio-footer-search">';
944            $html .= $this->includeSearch(FALSE);
945            $html .= '</div>';
946        }
947
948        $showPageTools = $this->getConf('pageToolsFooter');
949        if ($ACT == 'show' && ($showPageTools == 'always' || $this->userCanEdit() && $showPageTools == 'page editors')) $html .= $this->includePageTools(FALSE);
950
951        $meta['licenseType']            = array('multichoice', '_choices' => array('none', 'badge', 'button'));
952        $meta['licenseImageOnly']       = array('onoff');
953
954        $licenseType = $this->getConf('licenseType');
955        if ($licenseType != 'none') {
956            $html .= tpl_license($licenseType, $this->getConf('licenseImageOnly'), TRUE, TRUE);
957        }
958
959        $html .= '</footer>';
960
961        if ($print) echo $html;
962        return $html;
963    }
964
965
966    /**
967     * Print or return breadcrumb trail
968     *
969     * @param   boolean  $print     print out trail
970     * @param   boolean  $parse     parse trail before printing
971     * @return  string              html string containing breadcrumbs
972     */
973    public function includeBreadcrumbs($print = TRUE, $parse = TRUE)
974    {
975        global $conf, $ID, $lang, $ACT;
976
977        if ($this->getConf('breadcrumbHideHome') && $ID == 'start' && $ACT == 'show' || $ACT == 'showtag') return '';
978
979        $html = '<div class="mikio-breadcrumbs">';
980        $html .= '<div class="mikio-container">';
981        if ($ACT == 'show') {
982            if ($conf['breadcrumbs']) {
983                if (!$this->getConf('breadcrumbPrefix') && !$this->getConf('breadcrumbSep')) {
984                    ob_start();
985                    tpl_breadcrumbs();
986                    $html .= ob_get_contents();
987                    ob_end_clean();
988                } else {
989                    $sep = '•';
990                    $prefix = $lang['breadcrumb'];
991
992                    if ($this->getConf('breadcrumbSep')) {
993                        $sep = $this->getConf('breadcrumbSepText');
994                        $img = $this->getMediaFile('breadcrumb-sep', FALSE);
995
996                        if ($img !== FALSE) {
997                            $sep = '<img src="' . $img . '">';
998                        }
999                    }
1000
1001                    if ($this->getConf('breadcrumbPrefix')) {
1002                        $prefix = $this->getConf('breadcrumbPrefixText');
1003                        $img = $this->getMediaFile('breadcrumb-prefix', FALSE);
1004
1005                        if ($img !== FALSE) {
1006                            $prefix = '<img src="' . $img . '">';
1007                        }
1008                    }
1009
1010                    $crumbs = breadcrumbs();
1011
1012                    $html .= '<ul>';
1013                    if ($prefix != '') $html .= '<li class="prefix">' . $prefix . '</li>';
1014
1015                    $last = count($crumbs);
1016                    $i    = 0;
1017                    foreach ($crumbs as $id => $name) {
1018                        $i++;
1019                        $html .= '<li class="sep">' . $sep . '</li>';
1020                        $html .= '<li' . ($i == $last ? ' class="curid"' : '') . '>';
1021                        $html .= tpl_pagelink($id, NULL, TRUE);
1022                        $html .= '</li>';
1023                    }
1024
1025                    $html .= '</ul>';
1026                }
1027            }
1028        }
1029
1030        $html .= '</div>';
1031        $html .= '</div>';
1032
1033        if ($parse) $html = $this->includeIcons($html);
1034        if ($print) echo $html;
1035        return $html;
1036    }
1037
1038    /**
1039     * Print or return you are here trail
1040     *
1041     * @param   boolean  $print     print out trail
1042     * @param   boolean  $parse     parse trail before printing
1043     * @return  string              html string containing breadcrumbs
1044     */
1045    public function includeYouAreHere($print = TRUE, $parse = TRUE)
1046    {
1047        global $conf, $ID, $lang, $ACT;
1048
1049        if ($this->getConf('youarehereHideHome') && $ID == 'start' && $ACT == 'show' || $ACT == 'showtag') return '';
1050
1051        $html = '<div class="mikio-youarehere">';
1052        $html .= '<div class="mikio-container">';
1053        if ($ACT == 'show') {
1054            if ($conf['youarehere']) {
1055                if (!$this->getConf('youareherePrefix') && !$this->getConf('youarehereSep')) {
1056                    ob_start();
1057                    tpl_youarehere();
1058                    $html .= ob_get_contents();
1059                    ob_end_clean();
1060                } else {
1061                    $sep = ' » ';
1062                    $prefix = $lang['youarehere'];
1063
1064                    if ($this->getConf('youarehereSep')) {
1065                        $sep = $this->getConf('youarehereSepText');
1066                        $img = $this->getMediaFile('youarehere-sep', FALSE);
1067
1068                        if ($img !== FALSE) {
1069                            $sep = '<img src="' . $img . '">';
1070                        }
1071                    }
1072
1073                    if ($this->getConf('youareherePrefix')) {
1074                        $prefix = $this->getConf('youareherePrefixText');
1075                        $img = $this->getMediaFile('youarehere-prefix', FALSE);
1076
1077                        if ($img !== FALSE) {
1078                            $prefix = '<img src="' . $img . '">';
1079                        }
1080                    }
1081
1082                    $html .= '<ul>';
1083                    if ($prefix != '') $html .= '<li class="prefix">' . $prefix . '</li>';
1084                    $html .= '<li>' . tpl_pagelink(':' . $conf['start'], NULL, TRUE) . '</li>';
1085
1086                    $parts = explode(':', $ID);
1087                    $count = count($parts);
1088
1089                    $part = '';
1090                    for ($i = 0; $i < $count - 1; $i++) {
1091                        $part .= $parts[$i] . ':';
1092                        $page = $part;
1093                        if ($page == $conf['start']) continue;
1094
1095                        $html .= '<li class="sep">' . $sep . '</li>';
1096                        $html .= '<li>' . tpl_pagelink($page, NULL, TRUE) . '</li>';
1097                    }
1098
1099                    resolve_pageid('', $page, $exists);
1100                    if (!(isset($page) && $page == $part . $parts[$i])) {
1101                        $page = $part . $parts[$i];
1102                        if ($page != $conf['start']) {
1103                            $html .= '<li class="sep">' . $sep . '</li>';
1104                            $html .= '<li>' . tpl_pagelink($page, NULL, TRUE) . '</li>';
1105                        }
1106                    }
1107
1108                    $html .= '</ul>';
1109                }
1110            }
1111
1112            $showLast = $this->getConf('youarehereShowLast');
1113            if ($showLast != 0) {
1114                preg_match_all('/(<li[^>]*>.+?<\/li>)/', $html, $matches);
1115                if (count($matches) > 0 && count($matches[0]) > ($showLast * 2) + 2) {
1116                    $count = count($matches[0]);
1117                    $list = '';
1118
1119                    // Show Home
1120                    $list .= $matches[0][0] . $matches[0][1];
1121
1122                    $list .= '<li>...</li>';
1123                    for ($i = $count - ($showLast * 2); $i <= $count; $i++) {
1124                        $list .= $matches[0][$i];
1125                    }
1126
1127                    $html = preg_replace('/<ul>.*<\/ul>/', '<ul>' . $list . '</ul>', $html);
1128                }
1129            }
1130
1131            switch ($this->getConf('youarehereHome')) {
1132                case 'none':
1133                    $html = preg_replace('/<li[^>]*>.+?<\/li>/', '', $html, 2);
1134                    break;
1135                case 'home':
1136                    $html = preg_replace('/(<a[^>]*>)(.+?)(<\/a>)/', '$1' . tpl_getlang('home') . '$3', $html, 1);
1137                    break;
1138                case 'icon':
1139                    $html = preg_replace('/(<a[^>]*>)(.+?)(<\/a>)/', '$1' . $this->mikioInlineIcon('home') . '$3', $html, 1);
1140                    break;
1141            }
1142        } else {
1143            $html .= '&#8810; ';
1144            if (isset($_GET['page'])) {
1145                $html .= '<a href="' . wl($ID, array('do' => $ACT)) . '">Back</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;';
1146            }
1147            $html .= '<a href="' . wl($ID) . '">View Page</a>';
1148        }
1149
1150        $html .= '</div>';
1151        $html .= '</div>';
1152
1153        if ($parse) $html = $this->includeIcons($html);
1154        if ($print) echo $html;
1155        return $html;
1156    }
1157
1158    /**
1159     * Get Page Title
1160     */
1161    public function parsePageTitle()
1162    {
1163        global $ID;
1164
1165        $title = p_get_first_heading($ID);
1166        if (strlen($title) <= 0) $title = tpl_pagetitle(null, TRUE);
1167        $title = $this->includeIcons($title);
1168
1169        return $title;
1170    }
1171
1172
1173    /**
1174     * Print or return hero block
1175     *
1176     * @param   boolean $print          print content
1177     * @return  string                  contents of hero
1178     */
1179    public function includeHero($print = TRUE)
1180    {
1181        $html = '';
1182
1183        if ($this->getConf('heroTitle')) {
1184            $html .= '<div class="mikio-hero">';
1185            $html .= '<div class="mikio-container">';
1186            $html .= '<div class="mikio-hero-text">';
1187            if ($this->getConf('youareherePosition') == 'hero') $html .= $this->includeYouAreHere(FALSE);
1188            if ($this->getConf('breadcrumbPosition') == 'hero') $html .= $this->includeBreadcrumbs(FALSE);
1189
1190            $html .= '<h1 class="mikio-hero-title">';
1191            $html .= $this->parsePageTitle();    // No idea why this requires a blank space afterwards to work?
1192            $html .= '</h1>';
1193            $html .= '<h2 class="mikio-hero-subtitle"></h2>';
1194            $html .= '</div>';
1195
1196            $hero_image = $this->getMediaFile('hero', TRUE, $this->getConf('heroImagePropagation', TRUE));
1197            $hero_image_resize_class = '';
1198            if ($hero_image != '') {
1199                $hero_image = ' style="background-image:url(\'' . $hero_image . '\');"';
1200                $hero_image_resize_class = ' mikio-hero-image-resize';
1201            }
1202
1203            $html .= '<div class="mikio-hero-image' . $hero_image_resize_class . '"' . $hero_image . '><div class="mikio-tags"></div></div>';
1204
1205            $html .= '</div>';
1206            $html .= '</div>';
1207        }
1208
1209        if ($print) echo $html;
1210
1211        return $html;
1212    }
1213
1214
1215    /**
1216     * Print or return out TOC
1217     *
1218     * @param   boolean $print          print TOC
1219     * @return  string                  contents of TOC
1220     */
1221    public function includeTOC($parse = TRUE)
1222    {
1223        $html = '';
1224
1225        $tocHtml = tpl_toc(true);
1226
1227        if ($tocHtml != '') {
1228            $tocHtml = preg_replace('/<li.*><div.*><a.*><\/a><\/div><\/li>\s*/', '', $tocHtml);
1229            $tocHtml = preg_replace('/<ul.*>\s*<\/ul>\s*/', '', $tocHtml);
1230
1231            $html .= '<div class="mikio-toc">';
1232            $html .= $tocHtml;
1233            $html .= '</div>';
1234        }
1235
1236        if ($parse) $html = $this->includeIcons($html);
1237        echo $html;
1238    }
1239
1240
1241    /**
1242     * Parse the string and replace icon elements with included icon libraries
1243     *
1244     * @param   string  $str        content to parse
1245     * @return  string              parsed string
1246     */
1247    public function includeIcons($str)
1248    {
1249        global $ACT, $MIKIO_ICONS;
1250
1251        $iconTag = $this->getConf('iconTag', 'icon');
1252        if ($iconTag == '') return $str;
1253
1254        if ($ACT == 'show' || $ACT == 'admin' && count($MIKIO_ICONS) > 0 || $ACT == 'showtag' || $ACT == 'revisions' || $ACT == 'index' || $ACT == 'preview') {
1255            $content = $str;
1256            $preview = null;
1257
1258            if ($ACT == 'preview') {
1259                $html = new \simple_html_dom;
1260                $html->stripRNAttrValues = false;
1261                $html->load($str, true, false);
1262
1263                $preview = $html->find('div.preview');
1264                if (is_array($preview) && count($preview) > 0) {
1265                    $content = $preview[0]->innertext;
1266                }
1267            }
1268
1269            $page_regex = '/(.*)/';
1270            if (stripos($str, '<pre')) {
1271                $page_regex = '/<(?!pre|\/).*?>(.*)[^<]*/';
1272            }
1273
1274            $content = preg_replace_callback($page_regex, function ($icons) {
1275                $iconTag = $this->getConf('iconTag', 'icon');
1276
1277                return preg_replace_callback(
1278                    '/&lt;' . $iconTag . ' ([\w\- #]*)&gt;(?=[^>]*(<|$))/',
1279                    function ($matches) {
1280                        global $MIKIO_ICONS;
1281
1282                        $s = $matches[0];
1283
1284                        if (count($MIKIO_ICONS) > 0) {
1285                            $icon = $MIKIO_ICONS[0];
1286
1287                            if (count($matches) > 1) {
1288                                $e = explode(' ', $matches[1]);
1289
1290                                if (count($e) > 1) {
1291                                    foreach ($MIKIO_ICONS as $iconItem) {
1292                                        if (strcasecmp($iconItem['name'], $e[0]) == 0) {
1293                                            $icon = $iconItem;
1294
1295                                            $s = $icon['insert'];
1296                                            for ($i = 1; $i < 9; $i++) {
1297                                                if (count($e) < $i || $e[$i] == '') {
1298                                                    if (isset($icon['$' . $i])) {
1299                                                        $s = str_replace('$' . $i, $icon['$' . $i], $s);
1300                                                    }
1301                                                } else {
1302                                                    $s = str_replace('$' . $i, $e[$i], $s);
1303                                                }
1304                                            }
1305
1306                                            $dir = '';
1307                                            if (isset($icon['dir'])) $dir = $this->baseDir . 'icons/' . $icon['dir'] . '/';
1308
1309                                            $s = str_replace('$0', $dir, $s);
1310
1311                                            break;
1312                                        }
1313                                    }
1314                                } else {
1315                                    $s = str_replace('$1', $matches[1], $icon['insert']);
1316                                }
1317                            }
1318                        }
1319
1320                        $s = preg_replace('/(class=")(.*)"/', '$1mikio-icon $2"', $s, -1, $count);
1321                        if ($count == 0) {
1322                            $s = preg_replace('/(<\w* )/', '$1class="mikio-icon" ', $s);
1323                        }
1324
1325                        return $s;
1326                    },
1327                    $icons[0]
1328                );
1329            }, $content);
1330
1331            if ($ACT == 'preview') {
1332                if (is_array($preview) && count($preview) > 0) {
1333                    $preview[0]->innertext = $content;
1334                }
1335
1336                $str = $html->save();
1337                $html->clear();
1338                unset($html);
1339            } else {
1340                $str = $content;
1341            }
1342        }
1343
1344        return $str;
1345    }
1346
1347    /**
1348     * Parse HTML for theme
1349     *
1350     * @param   string  $content    HTML content to parse
1351     * @return  string              Parsed content
1352     */
1353    public function parseContent($content)
1354    {
1355        global $INPUT, $ACT;
1356
1357        // Add Mikio Section titles
1358        if ($INPUT->str('page') == 'config') {
1359            $admin_sections = array(
1360                // Section      Insert Before                       Icon
1361                'navbar'        => array('navbarUseTitleIcon',      ''),
1362                'search'        => array('searchButton',            ''),
1363                'hero'          => array('heroTitle',               ''),
1364                'tags'          => array('tagsConsolidate',         ''),
1365                'breadcrumb'    => array('breadcrumbHideHome',      ''),
1366                'youarehere'    => array('youareherePosition',      ''),
1367                'sidebar'       => array('sidebarShowLeft',         ''),
1368                'toc'           => array('tocFull',                 ''),
1369                'pagetools'     => array('pageToolsFloating',       ''),
1370                'footer'        => array('footerCustomMenuText',    ''),
1371                'license'       => array('licenseType',             ''),
1372                'acl'           => array('includePageUseACL',       ''),
1373                'sticky'        => array('stickyTopHeader',         ''),
1374            );
1375
1376            foreach ($admin_sections as $section => $items) {
1377                $search = $items[0];
1378                $icon   = $items[1];
1379
1380                // $content = preg_replace('/<tr(.*)>\s+<td(.*)>\s+<span(.*)>(tpl»mikio»' . $search . ')<\/span>/',
1381                // '<tr class="default"><td class="mikio-config-table-header" colspan="2">' . $this->mikioInlineIcon($icon) . tpl_getLang('config_' . $section) . '</td></tr><tr$1><td$2><span$3>$4</span>', $content);
1382
1383                $content = preg_replace(
1384                    '/<tr(.*)>\s*<td class="label">\s*<span class="outkey">(tpl»mikio»' . $search . ')<\/span>/',
1385                    '<tr$1><td class="mikio-config-table-header" colspan="2">' . $this->mikioInlineIcon($icon) . tpl_getLang('config_' . $section) . '</td></tr><tr class="default"><td class="label"><span class="outkey">tpl»mikio»' . $search . '</span>',
1386                    $content
1387                );
1388            }
1389        }
1390
1391        if ($ACT == 'admin' && !isset($_GET['page'])) {
1392            $content = preg_replace('/(<ul.*?>.*?)<\/ul>.*?<ul.*?>(.*?<\/ul>)/s', '$1$2', $content);
1393        }
1394
1395        // Page Revisions - Table Fix
1396        if (strpos($content, 'id="page__revisions"') !== FALSE) {
1397            $content = preg_replace('/(<span class="sum">\s.*<\/span>\s.*<span class="user">\s.*<\/span>)/', '<span>$1</span>', $content);
1398        }
1399
1400        $html = new \simple_html_dom;
1401        $html->stripRNAttrValues = false;
1402        $html->load($content, true, false);
1403
1404        if (!$html) return $content;
1405
1406        /* Buttons */
1407        foreach ($html->find('#config__manager button') as $node) {
1408            $c = explode(' ', $node->class);
1409            if (!in_array('mikio-button', $c)) $c[] = 'mikio-button';
1410            $node->class = implode(' ', $c);
1411        }
1412
1413
1414        /* Buttons - Primary */
1415        foreach ($html->find('#config__manager [type=submit]') as $node) {
1416            $c = explode(' ', $node->class);
1417            if (!in_array('mikio-primary', $c)) $c[] = 'mikio-primary';
1418            $node->class = implode(' ', $c);
1419        }
1420
1421        /* Hide page title if hero is enabled */
1422        if ($this->getConf('heroTitle') && $ACT != 'preview') {
1423            $pageTitle = $this->parsePageTitle();
1424
1425            foreach ($html->find('h1,h2,h3,h4') as $elm) {
1426                if ($elm->innertext == $pageTitle) {
1427                    // $elm->innertext = '';
1428                    $elm->setAttribute('style', 'display:none');
1429
1430                    break;
1431                }
1432            }
1433        }
1434
1435        /* Hero subtitle */
1436        foreach ($html->find('p') as $elm) {
1437            $i = stripos($elm->innertext, '~~hero-subtitle');
1438            if ($i !== false) {
1439                $j = strpos($elm->innertext, '~~', $i + 2);
1440                if ($j !== false) {
1441                    if ($j > $i + 16) {
1442                        $subtitle = substr($elm->innertext, $i + 16, $j - $i - 16);
1443                        $this->footerScript['hero-subtitle'] = 'mikio.setHeroSubTitle(\'' . $subtitle . '\')';
1444
1445                        // $elm->innertext = substr($elm->innertext, 0, $i + 2) . substr($elm->innertext, $j + 2);
1446                        $elm->innertext = preg_replace('/~~hero-subtitle (.+?)~~.*/ui', '', $elm->innertext);
1447                    }
1448
1449                    break;
1450                }
1451            }
1452        }
1453
1454        /* Hero image */
1455        foreach ($html->find('p') as $elm) {
1456            $image = '';
1457            preg_match('/~~hero-image (.+?)~~(?!.?")/ui', $elm->innertext, $matches);
1458            if (count($matches) > 0) {
1459                preg_match('/<img.*src="(.+?)"/ui', $matches[1], $imageTagMatches);
1460                if (count($imageTagMatches) > 0) {
1461                    $image = $imageTagMatches[1];
1462                } else {
1463                    preg_match('/<a.+?>(.+?)[~<]/ui', $matches[1], $imageTagMatches);
1464                    if (count($imageTagMatches) > 0) {
1465                        $image = $imageTagMatches[1];
1466                    } else {
1467                        $image = strip_tags($matches[1]);
1468                        if (stripos($image, ':') === FALSE) {
1469                            $image = str_replace(array('{', '}'), '', $image);
1470                            $i = stripos($image, '?');
1471                            if ($i !== FALSE) {
1472                                $image = substr($image, 0, $i);
1473                            }
1474
1475                            $image = ml($image, '', true, '', false);
1476                        }
1477                    }
1478                }
1479
1480                $this->footerScript['hero-image'] = 'mikio.setHeroImage(\'' . $image . '\')';
1481
1482                $elm->innertext = preg_replace('/~~hero-image (.+?)~~.*/ui', '', $elm->innertext);
1483            }
1484        }
1485
1486        /* Hero colors - ~~hero-colors [background-color] [hero-title-color] [hero-subtitle-color] [breadcrumb-text-color] [breadcrumb-hover-color] (use 'initial' for original color) */
1487        foreach ($html->find('p') as $elm) {
1488            $i = stripos($elm->innertext, '~~hero-colors');
1489            if ($i !== false) {
1490                $j = strpos($elm->innertext, '~~', $i + 2);
1491                if ($j !== false) {
1492                    if ($j > $i + 14) {
1493                        $color = substr($elm->innertext, $i + 14, $j - $i - 14);
1494                        $this->footerScript['hero-colors'] = 'mikio.setHeroColor(\'' . $color . '\')';
1495
1496                        $elm->innertext = preg_replace('/~~hero-colors (.+?)~~.*/ui', '', $elm->innertext);
1497                    }
1498
1499                    break;
1500                }
1501            }
1502        }
1503
1504        /* Hide parts - ~~hide-parts [parts]~~  */
1505        foreach ($html->find('p') as $elm) {
1506            $i = stripos($elm->innertext, '~~hide-parts');
1507            if ($i !== false) {
1508                $j = strpos($elm->innertext, '~~', $i + 2);
1509                if ($j !== false) {
1510                    if ($j > $i + 13) {
1511                        $parts = explode(' ', substr($elm->innertext, $i + 13, $j - $i - 13));
1512                        $script = '';
1513
1514                        foreach ($parts as $part) {
1515                            // $part = trim($part);
1516                            if (strlen($part) > 0) {
1517                                $script .= 'mikio.hidePart(\'' . $part . '\');';
1518                            }
1519                        }
1520
1521                        if (strlen($script) > 0) {
1522                            $this->footerScript['hide-parts'] = $script;
1523                        }
1524
1525                        $elm->innertext = preg_replace('/~~hide-parts (.+?)~~.*/ui', '', $elm->innertext);
1526                    }
1527
1528                    break;
1529                }
1530            }
1531        }
1532
1533
1534        /* Page Tags (tag plugin) */
1535        if ($this->getConf('tagsConsolidate')) {
1536            $tags = '';
1537            foreach ($html->find('div.tags a') as $elm) {
1538                $tags .= $elm->outertext;
1539            }
1540
1541            foreach ($html->find('div.tags') as $elm) {
1542                $elm->innertext = '';
1543                $elm->setAttribute('style', 'display:none');
1544            }
1545
1546            if ($tags != '') {
1547                $this->footerScript['tags'] = 'mikio.setTags(\'' . $tags . '\')';
1548            }
1549        }
1550
1551        // Configuration Manager
1552        if ($INPUT->str('page') == 'config') {
1553
1554            // Additional save buttons
1555            foreach ($html->find('#config__manager') as $cm) {
1556                $saveButtons = '';
1557
1558                foreach ($cm->find('p') as $elm) {
1559                    $saveButtons = $elm->outertext;
1560                    $saveButtons = str_replace('<p>', '<p style="text-align:right">', $saveButtons);
1561                    $elm->outertext = '';
1562                }
1563
1564                foreach ($cm->find('fieldset') as $elm) {
1565                    $elm->innertext .= $saveButtons;
1566                }
1567            }
1568        }
1569
1570        $content = $html->save();
1571        $html->clear();
1572        unset($html);
1573
1574        return $content;
1575    }
1576
1577
1578    /**
1579     * Get DokuWiki namespace/page/URI as link
1580     *
1581     * @param   string  $str            string to parse
1582     * @return  string                  parsed uri
1583     */
1584    public function getLink($str)
1585    {
1586        $i = strpos($str, '://');
1587        if ($i !== false) return $str;
1588
1589        return wl($str);
1590    }
1591
1592
1593    /**
1594     * Check if the user can edit current namespace/page
1595     *
1596     * @return  boolean                  user can edit
1597     */
1598    public function userCanEdit()
1599    {
1600        global $INFO;
1601        global $ID;
1602
1603        $wiki_file = wikiFN($ID);
1604        if (@!file_exists($wiki_file)) return true;
1605        if ($INFO['isadmin'] || $INFO['ismanager']) return true;
1606        // $meta_file = metaFN($ID, '.meta');
1607        if (!$INFO['meta']['user']) return true;
1608        if ($INFO['client'] ==  $INFO['meta']['user']) return true;
1609
1610        return false;
1611    }
1612
1613
1614    /**
1615     * Search for and return the uri of a media file
1616     *
1617     * @param string    $image              image name to search for (without extension)
1618     * @param bool      $searchCurrentNS    search the current namespace
1619     * @return string                       uri of the found media file
1620     */
1621    public function getMediaFile($image, $searchCurrentNS = TRUE, $propagate = TRUE)
1622    {
1623        global $INFO;
1624
1625        $ext = array('png', 'jpg', 'gif', 'svg');
1626
1627        if ($searchCurrentNS) $prefix[] = ':' . $INFO['namespace'] . ':';
1628        if ($propagate) {
1629            $prefix[] = ':';
1630            $prefix[] = ':wiki:';
1631        }
1632        $theme = $this->getConf('customTheme');
1633        if ($theme != '') $prefix[] = $this->tplDir . 'themes/' . $theme . '/images/';
1634        $prefix[] = 'images/';
1635
1636        $search = array();
1637        foreach ($prefix as $pitem) {
1638            foreach ($ext as $eitem) {
1639                $search[] = $pitem . $image . '.' . $eitem;
1640            }
1641        }
1642
1643        $img = '';
1644        $file = '';
1645        $url = '';
1646        $ismedia = false;
1647        $found = false;
1648
1649        foreach ($search as $img) {
1650            if (substr($img, 0, 1) == ':') {
1651                $file    = mediaFN($img);
1652                $ismedia = true;
1653            } else {
1654                $file    = tpl_incdir() . $img;
1655                $ismedia = false;
1656            }
1657
1658            if (file_exists($file)) {
1659                $found = true;
1660                break;
1661            }
1662        }
1663
1664        if (!$found) return false;
1665
1666        if ($ismedia) {
1667            $url = ml($img, '', true, '', false);
1668        } else {
1669            $url = tpl_basedir() . $img;
1670        }
1671
1672        return $url;
1673    }
1674
1675
1676    /**
1677     * Print or return the page title
1678     *
1679     * @param string    $page       page id or empty string for current page
1680     * @return string               generated content
1681     */
1682    public function getPageTitle($page = '')
1683    {
1684        global $ID, $conf;
1685
1686        $html = '';
1687
1688        if ($page == '') $page = $ID;
1689
1690        $html = p_get_first_heading($page);
1691        $html = strip_tags($html);
1692        $html = preg_replace('/\s+/', ' ', $html);
1693        $html .= ' [' . strip_tags($conf['title']) . ']';
1694        $html = trim($html);
1695
1696        return $html;
1697    }
1698
1699
1700    /**
1701     * Return inline theme icon
1702     *
1703     * @param   string  $type           icon to retreive
1704     * @return  string                  html icon content
1705     */
1706    public function mikioInlineIcon($type)
1707    {
1708        switch ($type) {
1709            case 'wrench':
1710                return '<svg class="mikio-iicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792 1792" 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,19 -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,-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,435 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 131.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,-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>';
1711            case 'file':
1712                return '<svg class="mikio-iicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,235.38983,1277.8305)" id="g2991"><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 1280,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 q 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" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" /></g></svg>';
1713            case 'gear':
1714                return '<svg class="mikio-iicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,121.49153,1285.4237)" id="g3027"><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 181,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 10,-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 -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 147,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 q 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,71.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 q 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 -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" /></g></svg>';
1715            case 'user':
1716                return '<svg class="mikio-iicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792 1792" style="fill:currentColor"><g transform="matrix(1,0,0,-1,197.42373,1300.6102)"><path d="M 1408,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 28,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,-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 50.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 -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,1408 863,1408 975.5,1295.5 1088,1183 1088,1024 z"/></g></svg>';
1717            case 'search':
1718                return '<svg class="mikio-iicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" aria-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 18.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 24.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 6.195 0 0 1-6.188 6.188z"/></svg>';
1719            case 'home':
1720                return '<svg class="mikio-iicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 -256 1792 1792" aria-hidden="true" style="fill:currentColor"><g transform="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 960 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 m 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 -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,-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" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" /></g></svg>';
1721        }
1722
1723        return '';
1724    }
1725
1726    /**
1727     * Finalize theme
1728     */
1729    public function finalize()
1730    {
1731    }
1732
1733    /**
1734     * Show Messages
1735     */
1736    public function showMessages()
1737    {
1738        global $ACT;
1739
1740        if ($this->lessIgnored) {
1741            msg('useLESS is enabled on the Mikio template, however is not supported on this server', 2, '', '', MSG_ADMINS_ONLY);
1742        }
1743
1744        $show = $this->getConf('showNotifications');
1745        if ($show == 'always' || ($show == 'admin' && $ACT == 'admin')) {
1746            global $MSG, $MSG_shown;
1747
1748            if (!isset($MSG)) {
1749                return;
1750            }
1751
1752            if (!isset($MSG_shown)) {
1753                $MSG_shown = array();
1754            }
1755
1756            foreach ($MSG as $msg) {
1757
1758                $hash = md5($msg['msg']);
1759                if (isset($MSG_shown[$hash])) {
1760                    continue;
1761                }
1762                // skip double messages
1763
1764                if (info_msg_allowed($msg)) {
1765
1766                    print '<div class="' . $msg['lvl'] . '">';
1767                    print $msg['msg'];
1768                    print '</div>';
1769                }
1770
1771                $MSG_shown[$hash] = true;
1772            }
1773
1774            unset($GLOBALS['MSG']);
1775        }
1776    }
1777
1778    /**
1779     * Dokuwiki version
1780     *
1781     * @return  string        the dw version name
1782     */
1783    public function dwVersion() {
1784        if(function_exists('getVersionData')) {
1785            $version_data = getVersionData();
1786            if(is_array($version_data) && array_key_exists('date', $version_data)) {
1787                $version_items = explode(' ', $version_data['date']);
1788                if(count($version_items) >= 2) {
1789                    return preg_replace('/[^a-zA-Z0-9 ]+/', '', strtolower($version_items[1]));
1790                }
1791            }
1792        }
1793
1794        return 'unknown';
1795    }
1796
1797    /**
1798     * Dokuwiki version number
1799     *
1800     * @return  string        the dw version date converted to integer
1801     */
1802    public function dwVersionNumber() {
1803        if(function_exists('getVersionData')) {
1804            $version_data = getVersionData();
1805            if(is_array($version_data) && array_key_exists('date', $version_data)) {
1806                $version_items = explode(' ', $version_data['date']);
1807                if(count($version_items) >= 1) {
1808                    return intval(preg_replace('/[^0-9]+/', '', strtolower($version_items[0])));
1809                }
1810            }
1811        }
1812
1813        return 0;
1814    }
1815}
1816
1817global $TEMPLATE;
1818$TEMPLATE = \dokuwiki\template\mikio\Template::getInstance();
1819