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