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