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