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