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
12 if (!defined('DOKU_INC')) die();
13 
14 /**
15  * Generate the HTML for the Site-Branding Area
16  *
17  * @return string
18  */
19 if (!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  */
87 if (!function_exists('tpl_getGravatarURL')) {
88     function tpl_getGravatarURL($email, $size = 96)
89     {
90         return 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($email))).'?s='.$size;
91     }
92 }
93 
94 
95 /**
96  * Generate the HTML for a menu
97  *
98  * @return string
99  */
100 if (!function_exists('tpl_getMenu')) {
101     function tpl_getMenu($menu)
102     {
103         switch($menu){
104             case 'usermenu':
105                 return tpl_getUserMenu();
106                 break;
107             case 'sidebarmenu':
108                 return tpl_getSidebarMenu();
109                 break;
110         }
111     }
112 }
113 
114 /**
115  * Generate the HTML for the user menu
116  *
117  * @return string
118  */
119 if (!function_exists('tpl_getUserMenu')) {
120     function tpl_getUserMenu()
121     {
122         global $lang,$ID,$conf,$INFO;
123 
124         $return = '';
125 
126         $items = (new \dokuwiki\Menu\UserMenu())->getItems();
127 
128         if(isset($INFO['userinfo'])){
129             $return .= '<div class="tools-menus">';
130                 $return .= '<div class="dropdown user-tools">';
131                     $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">';
132                         $return .= '<i class="bi bi-person"></i>'.$INFO['userinfo']['name'];
133                     $return .= '</a>';
134                     $return .= '<ul class="dropdown-menu" role="menu">';
135                         $return .= '<li>';
136                             $return .= '<p class="avatar">';
137                                 $return .= '<img alt="'.$INFO['userinfo']['name'].'" src="'.tpl_getGravatarURL($INFO['userinfo']['mail']).'" />';
138                             $return .= '</p>';
139                         $return .= '</li>';
140                         foreach($items as $item) {
141                             $return .= '<li>'
142                                 .'<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
143                                 .'<i></i> '
144                                 .$item->getLabel()
145                                 .'</a></li>';
146                         }
147                     $return .= '</ul>';
148                 $return .= '</div>';
149                 if(tpl_getConf('enableStarredBookmark')){
150                     if (!plugin_isdisabled('sqlite')) {
151                         if (!plugin_isdisabled('starred')) {
152                             $return .= '<div class="dropdown bookmarks">';
153                                 $return .= '<a href="#" class="dropdown-toggle starred" title="'.tpl_getLang('Bookmarks').'" data-target="#" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">';
154                                     $return .= '<i class="bi bi-star"></i>'.tpl_getLang('Bookmarks');
155                                 $return .= '</a>';
156                                 $return .= '<ul class="dropdown-menu" role="menu">';
157                                 $instructions = p_get_instructions('{{starred>min}}');
158                                 if(count($instructions) > 0) {
159                                     $render = p_render('xhtml',$instructions, $info);
160                                     // Sanitize the output
161                                     $render = preg_replace('/<div class="plugin_starred">/', '', $render);
162                                     $render = preg_replace('/<\/div>/', '', $render);
163                                     $render = preg_replace('/<div class="li">/', '', $render);
164                                     $render = preg_replace('/<\/div>/', '', $render);
165                                     $render = preg_replace('/<ul>/', '', $render);
166                                     $render = preg_replace('/<\/ul>/', '', $render);
167                                     $render = preg_replace('/<li class="level1">/', '<li>', $render);
168                                     $return .= $render;
169                                 }
170                                 $return .= '</ul>';
171                             $return .= '</div>';
172                         }
173                     }
174                 }
175             $return .= '</div>';
176         } else {
177             $return .= '<div class="inline user-tools">';
178             foreach($items as $item) {
179                 $return .= '<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
180                     .'<i></i> '
181                     .$item->getLabel()
182                     .'</a>';
183             }
184             $return .= '</div>';
185         }
186 
187         return $return;
188     }
189 }
190 
191 /**
192  * Generate the HTML for the sidebar menus
193  *
194  * @return string
195  */
196 if (!function_exists('tpl_getSidebarMenu')) {
197     function tpl_getSidebarMenu()
198     {
199         global $lang,$ID,$conf,$INFO;
200 
201         $userItems = (new \dokuwiki\Menu\UserMenu())->getItems();
202 
203         $return = '';
204         $return .= '<div id="writr__sidebar__tools">';
205 
206         if(isset($INFO['userinfo'])){
207             $return .= '<div class="user-tools">';
208                 $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">';
209                     $return .= '<img alt="'.$INFO['userinfo']['name'].'" src="'.tpl_getGravatarURL($INFO['userinfo']['mail'], 32).'" />';
210                     $return .= '<span>'.$INFO['userinfo']['name'].'</span>';
211                 $return .= '</a>';
212                 $return .= '<div id="user-tools-menu" class="collapsed">';
213                     $return .= '<ul class="menu" role="menu">';
214                         foreach($userItems as $item) {
215                             $return .= '<li>'
216                                 .'<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
217                                 .'<i></i> '
218                                 .$item->getLabel()
219                                 .'</a></li>';
220                         }
221                     $return .= '</ul>';
222                 $return .= '</div>';
223             $return .= '</div>';
224         } else {
225             $return .= '<div class="user-tools">';
226             foreach($userItems as $item) {
227                 $return .= '<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
228                     .'<i></i> '
229                     .$item->getLabel()
230                     .'</a>';
231             }
232             $return .= '</div>';
233         }
234 
235         $return .= '</div>';
236 
237         return $return;
238     }
239 }
240