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