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