1<?php
2/**
3 * Template Functions
4 *
5 * This file provides template specific custom functions that are
6 * not provided by the DokuWiki core.
7 * It is common practice to start each function with an underscore
8 * to make sure it won't interfere with future core functions.
9 */
10
11// must be run from within DokuWiki
12if (!defined('DOKU_INC')) die();
13
14/**
15 * Generate the HTML for the Site-Branding Area
16 *
17 * @return string
18 */
19if (!function_exists('tpl_getSiteBranding')) {
20    function tpl_getSiteBranding()
21    {
22        // Retrieve the global variables
23        global $ID,$conf;
24
25        // Initialize the return value
26        $return = '';
27
28        // Initialize the site branding configurations
29        $logoSize = array();
30        $logoImages = array();
31        if(tpl_getConf('doLogoChangesByNamespace')){
32            $namespace = "";
33            $namespaces = array();
34            foreach(explode(':',getNS($ID)) as $ns){
35                $namespace .= "$ns:";
36                $namespaces[] = $namespace;
37            }
38            foreach(array_reverse($namespaces)  as $namespace){
39                $logoImages[] = ":".trim($namespace,":").":logo.png";
40            }
41        }
42        $logoImages[] = ':logo.png';
43        $logoImages[] = ':wiki:logo.png';
44        $logoImages[] = 'images/logo.png';
45        $logo = tpl_getMediaFile($logoImages, false, $logoSize);
46        $link = wl();
47        if(tpl_getConf('doLogoLinkChangesByNamespace')){
48            $link = str_replace(['/_media','logo.png'],['',$conf['start']],$logo);
49        }
50        $title = $conf['title'];
51        if(tpl_getConf('doTitleChangesByNamespace')){
52            $nstitle = tpl_include_page('nstitle', 0, 1);
53            if ($nstitle) {
54                $title = str_replace(['<p>','</p>',"\n"],'',$nstitle);
55            }
56        }
57        $tagline = $conf['tagline'];
58        if(tpl_getConf('doTaglineChangesByNamespace')){
59            $nstagline = tpl_include_page('nstagline', 0, 1);
60            if ($nstagline) {
61                $tagline = str_replace(['<p>','</p>',"\n"],'',$nstagline);
62            }
63        }
64
65        // Initialize the logo
66        $return .= '<a class="site-logo" href="'.$link.'" title="'.$title.'" rel="home" accesskey="h">';
67        $return .= '<img src="'.$logo.'" '.$logoSize[3].' class="no-grav header-image" />';
68        $return .= '</a>';
69
70        // Initialize the site branding
71        $return .= '<div class="site-branding">';
72        $return .= '<h1 class="site-title"><a href="'.$link.'" rel="home" accesskey="h" title="'.$title.'">'.$title.'</a></h1>';
73        if($tagline){
74            $return .= '<h2 class="site-description">'.$tagline.'</h2>';
75        }
76        $return .= '</div>';
77
78        return $return;
79    }
80}
81
82/**
83 * Generate the gravatar URL for a given email
84 *
85 * @return string
86 */
87if (!function_exists('tpl_getGravatarURL')) {
88    function tpl_getGravatarURL($email, $size = 96)
89    {
90        global $conf;
91        $email = trim(strtolower((string)$email));
92        $tplName = basename(__DIR__);
93        $variant = function_exists('tpl_getConf') ? tpl_getConf('profile_pic_variant') : 'white';
94
95        if ($variant === 'black') {
96            $fallback = DOKU_BASE.'lib/tpl/'.$tplName.'/images/profile_pic_black.png';
97        } else {
98            $fallback = DOKU_BASE.'lib/tpl/'.$tplName.'/images/profile_pic_white.png';
99        }
100
101        if (empty($email)) {
102            return $fallback;
103        }
104
105        $hash = md5(strtolower(trim($email)));
106        $testUrl = 'https://www.gravatar.com/avatar/'.$hash.'?s='.$size.'&d=404';
107        $headers = @get_headers($testUrl);
108        if ($headers && isset($headers[0]) && strpos($headers[0], '200') !== false) {
109            return 'https://www.gravatar.com/avatar/'.$hash.'?s='.$size;
110        }
111        return $fallback;
112    }
113}
114
115/**
116 * Generate the HTML for a menu
117 *
118 * @return string
119 */
120if (!function_exists('tpl_getMenu')) {
121    function tpl_getMenu($menu)
122    {
123        switch($menu){
124            case 'usermenu':
125                return tpl_getUserMenu();
126                break;
127            case 'sidebarmenu':
128                return tpl_getSidebarMenu();
129                break;
130        }
131    }
132}
133
134/**
135 * Generate the HTML for the user menu
136 *
137 * @return string
138 */
139if (!function_exists('tpl_getUserMenu')) {
140    function tpl_getUserMenu()
141    {
142        global $lang,$ID,$conf,$INFO;
143
144        $return = '';
145
146        $items = (new \dokuwiki\Menu\UserMenu())->getItems();
147
148        if(isset($INFO['userinfo'])){
149            $return .= '<div class="tools-menus">';
150                $return .= '<div class="dropdown user-tools">';
151                    $return .= '<a href="'.wl($ID).'" class="dropdown-toggle" title="'.$lang['user_tools'].'" data-target="#" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">';
152                        $return .= '<i class="bi bi-person"></i>'.$INFO['userinfo']['name'];
153                    $return .= '</a>';
154                    $return .= '<ul class="dropdown-menu" role="menu">';
155                        $return .= '<li>';
156                            $return .= '<p class="avatar">';
157                                $return .= '<img alt="'.$INFO['userinfo']['name'].'" src="'.tpl_getGravatarURL($INFO['userinfo']['mail']).'" />';
158                            $return .= '</p>';
159                        $return .= '</li>';
160                        foreach($items as $item) {
161                            $return .= '<li>'
162                                .'<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
163                                .'<i></i> '
164                                .$item->getLabel()
165                                .'</a></li>';
166                        }
167                    $return .= '</ul>';
168                $return .= '</div>';
169                if(tpl_getConf('enableStarredBookmark')){
170                    if (!plugin_isdisabled('sqlite')) {
171                        if (!plugin_isdisabled('starred')) {
172                            $return .= '<div class="dropdown bookmarks">';
173                                $return .= '<a href="#" class="dropdown-toggle starred" title="'.tpl_getLang('Bookmarks').'" data-target="#" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">';
174                                    $return .= '<i class="bi bi-star"></i>'.tpl_getLang('Bookmarks');
175                                $return .= '</a>';
176                                $return .= '<ul class="dropdown-menu" role="menu">';
177                                $instructions = p_get_instructions('{{starred>min}}');
178                                if(count($instructions) > 0) {
179                                    $render = p_render('xhtml',$instructions, $info);
180                                    // Sanitize the output
181                                    $render = preg_replace('/<div class="plugin_starred">/', '', $render);
182                                    $render = preg_replace('/<\/div>/', '', $render);
183                                    $render = preg_replace('/<div class="li">/', '', $render);
184                                    $render = preg_replace('/<\/div>/', '', $render);
185                                    $render = preg_replace('/<ul>/', '', $render);
186                                    $render = preg_replace('/<\/ul>/', '', $render);
187                                    $render = preg_replace('/<li class="level1">/', '<li>', $render);
188                                    $return .= $render;
189                                }
190                                $return .= '</ul>';
191                            $return .= '</div>';
192                        }
193                    }
194                }
195            $return .= '</div>';
196        } else {
197            $return .= '<div class="inline user-tools">';
198            foreach($items as $item) {
199                $return .= '<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
200                    .'<i></i> '
201                    .$item->getLabel()
202                    .'</a>';
203            }
204            $return .= '</div>';
205        }
206
207        return $return;
208    }
209}
210
211/**
212 * Generate the HTML for the sidebar menus
213 *
214 * @return string
215 */
216if (!function_exists('tpl_getSidebarMenu')) {
217    function tpl_getSidebarMenu()
218    {
219        global $lang,$ID,$conf,$INFO;
220
221        $userItems = (new \dokuwiki\Menu\UserMenu())->getItems();
222
223        $return = '';
224        $return .= '<div id="writr__sidebar__tools">';
225
226        if(isset($INFO['userinfo'])){
227            $return .= '<div class="user-tools">';
228                $return .= '<a href="'.wl($ID).'" class="" title="'.$lang['user_tools'].'" data-target="#user-tools-menu" data-toggle="collapse" role="button" aria-haspopup="true" aria-expanded="false">';
229                    $return .= '<img alt="'.$INFO['userinfo']['name'].'" src="'.tpl_getGravatarURL($INFO['userinfo']['mail'], 32).'" />';
230                    $return .= '<span>'.$INFO['userinfo']['name'].'</span>';
231                $return .= '</a>';
232                $return .= '<div id="user-tools-menu" class="collapsed">';
233                    $return .= '<ul class="menu" role="menu">';
234                        foreach($userItems as $item) {
235                            $return .= '<li>'
236                                .'<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
237                                .'<i></i> '
238                                .$item->getLabel()
239                                .'</a></li>';
240                        }
241                    $return .= '</ul>';
242                $return .= '</div>';
243            $return .= '</div>';
244        } else {
245            $return .= '<div class="user-tools">';
246            foreach($userItems as $item) {
247                $return .= '<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
248                    .'<i></i> '
249                    .$item->getLabel()
250                    .'</a>';
251            }
252            $return .= '</div>';
253        }
254
255        $return .= '</div>';
256
257        return $return;
258    }
259}
260