1<?php
2/**
3 * DokuWiki Template Masa Functions
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Michael Klier <chi@chimeric.de>, Kirill Bezrukov <kirbez@mail.ru>
7 */
8
9if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
10
11// load sidebar contents
12$sbl   = explode(',',tpl_getConf('left_sidebar_content'));
13$sbpos = tpl_getConf('sidebar');
14
15// set notoc option and toolbar regarding the sitebar setup
16$notoc = (in_array('toc',$sbl)) ? true : false;
17$toolb = (in_array('toolbox',$sbl)) ? true : false;
18
19/**
20 * Prints the action menu
21 *
22 * @author Kirill Bezrukov <kirbez@mail.ru>
23 */
24function tpl_actionmenu() {
25    global $lang;
26    global $conf;
27
28		include_once (dirname(__FILE__) . '/lang/en/lang.php');
29		@include_once (dirname(__FILE__) . '/lang/' . $conf['lang'] . '/lang.php');
30
31    $am_order   = explode(',', tpl_getConf('actionmenu_order'));
32    $am_content = explode(',', tpl_getConf('actionmenu_content'));
33
34    // process contents by given order
35
36    print '<select name="actionmenu" class="edit" onchange=
37    			"if ((this.selectedIndex != 0) &&
38       					(this.options[this.selectedIndex].disabled == false)) {
39            				location.href = this.options[this.selectedIndex].value;
40      			}
41          this.selectedIndex = 0;" >';
42    print '  <option value="show">'.$lang['caption'].'</option>';
43
44    // process contents by given order
45    foreach($am_order as $am) {
46        if(in_array($am,$am_content) || $am == '---') {
47            $key = array_search($am,$am_content);
48            unset($am_content[$key]);
49            tpl_actionmenu_dispatch($am);
50        }
51    }
52
53    // check for left content not specified by order
54    if(is_array($am_content) && !empty($am_content) > 0) {
55        foreach($am_content as $am) {
56            tpl_actionmenu_dispatch($am);
57        }
58    }
59
60    print '</select>';
61}
62
63
64/**
65 * Dispatches the given sidebar type to return the right content
66 *
67 * @author Michael Klier <chi@chimeric.de>
68 */
69function tpl_actionmenu_dispatch($am) {
70    global $lang;
71    global $conf;
72
73		include_once (dirname(__FILE__) . '/lang/en/lang.php');
74		@include_once (dirname(__FILE__) . '/lang/' . $conf['lang'] . '/lang.php');
75
76
77    switch($am) {
78       case 'export_raw':
79            print '   <option value="?do=export_raw">'.$lang['export_raw'].'</option>';
80            break;
81       case 'export_html':
82            print '   <option value="?do=export_html">'.$lang['export_html'].'</option>';
83            break;
84       case 'export_htmlbody':
85            print '   <option value="?do=export_htmlbody">'.$lang['export_htmlbody'].'</option>';
86            break;
87       case 'export_odt':
88            print '   <option value="?do=export_odt">'.$lang['export_odt'].'</option>';
89            break;
90       case 'backlink':
91            print '   <option value="?do=backlink">'.$lang['backlink'].'</option>';
92            break;
93       case 'pagemove':
94            print '   <option value="?do=admin&page=pagemove">'.$lang['pagemove'].'</option>';
95            break;
96       case 'config':
97            print '   <option value="?do=admin&page=config">'.$lang['config'].'</option>';
98            break;
99       case 'purge':
100            print '   <option value="?purge=true">'.$lang['purge'].'</option>';
101            break;
102       case '---':
103            print '   <option value="show" disabled="disabled" class="disabled">------------------------</option>';
104            break;
105       default:
106            print '   <option value="?do='.$am.'">'.$am.'</option>';
107            break;
108    }
109
110}
111
112/**
113 * Prints the sidebars
114 *
115 * @author Kirill Bezrukov <kirbez@mail.ru>
116 */
117function tpl_sidebar() {
118
119    $sb_order   = explode(',', tpl_getConf('left_sidebar_order'));
120    $sb_content = explode(',', tpl_getConf('left_sidebar_content'));
121
122    // process contents by given order
123    foreach($sb_order as $sb) {
124        if(in_array($sb,$sb_content)) {
125            $key = array_search($sb,$sb_content);
126            unset($sb_content[$key]);
127            tpl_sidebar_dispatch($sb,$pos);
128        }
129    }
130
131    // check for left content not specified by order
132    if(is_array($sb_content) && !empty($sb_content) > 0) {
133        foreach($sb_content as $sb) {
134            tpl_sidebar_dispatch($sb,$pos);
135        }
136    }
137}
138
139
140/**
141 * Dispatches the given sidebar type to return the right content
142 *
143 * @author Michael Klier <chi@chimeric.de>
144 */
145function tpl_sidebar_dispatch($sb,$pos) {
146    global $lang;
147    global $conf;
148    global $ID;
149    global $REV;
150    global $INFO;
151
152    $svID  = $ID;   // save current ID
153    $svREV = $REV;  // save current REV
154
155    $pname   = tpl_getConf('pagename');
156
157    switch($sb) {
158
159        case 'main':
160            $main_sb = $pname;
161            if(@file_exists(wikiFN($main_sb)) && auth_quickaclcheck($main_sb) >= AUTH_READ) {
162                print '<div class="main_sidebar sidebar_box">' . DOKU_LF;
163                print p_sidebar_xhtml($main_sb,$pos) . DOKU_LF;
164                print '</div>' . DOKU_LF;
165            }
166            break;
167
168        case 'namespace':
169            $user_ns  = tpl_getConf('user_sidebar_namespace');
170            $group_ns = tpl_getConf('group_sidebar_namespace');
171            if(!preg_match("/^".$user_ns.":.*?$|^".$group_ns.":.*?$/", $svID)) { // skip group/user sidebars and current ID
172                $ns_sb = _getNsSb($svID);
173                if($ns_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
174                    print '<div class="namespace_sidebar sidebar_box">' . DOKU_LF;
175                    print p_sidebar_xhtml($ns_sb,$pos) . DOKU_LF;
176                    print '</div>' . DOKU_LF;
177                }
178            }
179            break;
180
181        case 'user':
182            $user_ns = tpl_getConf('user_sidebar_namespace');
183            if(isset($INFO['userinfo']['name'])) {
184                $user = $_SERVER['REMOTE_USER'];
185                $user_sb = $user_ns . ':' . $user . ':' . $pname;
186                if(@file_exists(wikiFN($user_sb))) {
187                    print '<div class="user_sidebar sidebar_box">' . DOKU_LF;
188                    print p_sidebar_xhtml($user_sb,$pos) . DOKU_LF;
189                    print '</div>';
190                }
191                // check for namespace sidebars in user namespace too
192                if(preg_match('/'.$user_ns.':'.$user.':.*/', $svID)) {
193                    $ns_sb = _getNsSb($svID);
194                    if($ns_sb && $ns_sb != $user_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
195                        print '<div class="namespace_sidebar sidebar_box">' . DOKU_LF;
196                        print p_sidebar_xhtml($ns_sb,$pos) . DOKU_LF;
197                        print '</div>' . DOKU_LF;
198                    }
199                }
200
201            }
202            break;
203
204        case 'group':
205            $group_ns = tpl_getConf('group_sidebar_namespace');
206            if(isset($INFO['userinfo']['name'], $INFO['userinfo']['grps'])) {
207                foreach($INFO['userinfo']['grps'] as $grp) {
208                    $group_sb = $group_ns.':'.$grp.':'.$pname;
209                    if(@file_exists(wikiFN($group_sb)) && auth_quickaclcheck(cleanID($group_sb)) >= AUTH_READ) {
210                        print '<div class="group_sidebar sidebar_box">' . DOKU_LF;
211                        print p_sidebar_xhtml($group_sb,$pos) . DOKU_LF;
212                        print '</div>' . DOKU_LF;
213                    }
214                }
215            }
216            break;
217
218        case 'index':
219            print '<div class="index_sidebar sidebar_box">' . DOKU_LF;
220            print '  ' . p_index_xhtml($svID,$pos) . DOKU_LF;
221            print '</div>' . DOKU_LF;
222            break;
223
224        case 'toc':
225            if(auth_quickaclcheck($svID) >= AUTH_READ) {
226                $instructions = p_cached_instructions(wikiFN($svID));
227                if(!empty($instructions)) {
228                    // FIXME - there's another way - read your todo list
229                    foreach($instructions as $instruction) {
230                        // ~~NOTOC~~ is set - do nothing
231                        if($instruction[0] == 'notoc') return;
232                    }
233                }
234                @require_once(DOKU_INC.'inc/parser/xhtml.php');
235                // replace ids to keep XHTML compliance
236                $meta = p_get_metadata($svID,'description tableofcontents');
237                if(!empty($meta)) {
238                    $toc = preg_replace('/id="(.*?)"/', 'id="sb__' . $pos . '__\1"', Doku_Renderer_xhtml::render_TOC($meta));
239                    if(!empty($toc)) {
240                        print '<div class="toc_sidebar sidebar_box">' . DOKU_LF;
241                        print ($toc);
242                        print '</div>' . DOKU_LF;
243                    }
244                }
245            }
246            break;
247
248        case 'toolbox':
249            $actions = array('admin', 'edit', 'history', 'recent', 'backlink', 'subscription', 'index', 'login', 'profile');
250
251            print '<div class="toolbox_sidebar sidebar_box">' . DOKU_LF;
252            print '  <div class="level1">' . DOKU_LF;
253            print '    <ul>' . DOKU_LF;
254
255            foreach($actions as $action) {
256                if(!actionOK($action)) continue;
257                // start output buffering
258                ob_start();
259                print '     <li><div class="li">';
260                if(tpl_actionlink($action)) {
261                    print '     </div></li>' . DOKU_LF;
262                    ob_end_flush();
263                } else {
264                    ob_end_clean();
265                }
266            }
267
268            print '    </ul>' . DOKU_LF;
269            print '  </div>' . DOKU_LF;
270            print '</div>' . DOKU_LF;
271            break;
272
273        case 'trace':
274            print '<div class="trace_sidebar sidebar_box">' . DOKU_LF;
275            print '  <h1>'.$lang['breadcrumb'].'</h1>' . DOKU_LF;
276            print '  <div class="breadcrumbs">' . DOKU_LF;
277            ($conf['youarehere'] != 1) ? tpl_breadcrumbs() : tpl_youarehere();
278            print '  </div>' . DOKU_LF;
279            print '</div>' . DOKU_LF;
280            break;
281
282        case 'extra':
283            print '<div class="extra_sidebar sidebar_box">' . DOKU_LF;
284            @include(dirname(__FILE__).'/' . $pos .'_sidebar.html');
285            print '</div>' . DOKU_LF;
286            break;
287
288        default:
289            // check for user defined sidebars
290            if(@file_exists(DOKU_TPLINC.'sidebars/'.$sb.'/sidebar.php')) {
291                print '<div class="'.$sb.'_sidebar sidebar_box">' . DOKU_LF;
292                @require_once(DOKU_TPLINC.'sidebars/'.$sb.'/sidebar.php');
293                print '</div>' . DOKU_LF;
294            }
295            break;
296    }
297
298    // restore ID and REV
299    $ID  = $svID;
300    $REV = $svREV;
301}
302
303/**
304 * Removes the TOC of the sidebar pages and
305 * shows a edit button if the user has enough rights
306 *
307 * TODO sidebar caching
308 *
309 * @author Michael Klier <chi@chimeric.de>
310 */
311function p_sidebar_xhtml($sb,$pos) {
312    $data = p_wiki_xhtml($sb,'',false);
313    if(auth_quickaclcheck($sb) >= AUTH_EDIT) {
314        $data .= '<div class="secedit">'.html_btn('secedit',$sb,'',array('do'=>'edit','rev'=>'','post')).'</div>';
315    }
316    // strip TOC
317    $data = preg_replace('/<div class="toc">.*?(<\/div>\n<\/div>)/s', '', $data);
318    // replace headline ids for XHTML compliance
319    $data = preg_replace('/(<h.*?><a.*?id=")(.*?)(">.*?<\/a><\/h.*?>)/','\1sb_'.$pos.'_\2\3', $data);
320    return ($data);
321}
322
323/**
324 * Renders the Index
325 *
326 * copy of html_index located in /inc/html.php
327 *
328 * TODO update to new AJAX index possible?
329 *
330 * @author Andreas Gohr <andi@splitbrain.org>
331 * @author Michael Klier <chi@chimeric.de>
332 */
333function p_index_xhtml($ns,$pos) {
334  require_once(DOKU_INC.'inc/search.php');
335  global $conf;
336  global $ID;
337  $dir = $conf['datadir'];
338  $ns  = cleanID($ns);
339  #fixme use appropriate function
340  if(empty($ns)){
341    $ns = dirname(str_replace(':','/',$ID));
342    if($ns == '.') $ns ='';
343  }
344  $ns  = utf8_encodeFN(str_replace(':','/',$ns));
345
346  // extract only the headline
347  preg_match('/<h1>.*?<\/h1>/', p_locale_xhtml('index'), $match);
348  print preg_replace('#<h1(.*?id=")(.*?)(".*?)h1>#', '<h1\1sidebar_'.$pos.'_\2\3h1>', $match[0]);
349
350  $data = array();
351  search($data,$conf['datadir'],'search_index',array('ns' => $ns));
352  print html_buildlist($data,'idx','_html_list_index','html_li_index');
353}
354
355/**
356 * Index item formatter
357 *
358 * User function for html_buildlist()
359 *
360 * @author Andreas Gohr <andi@splitbrain.org>
361 * @author Michael Klier <chi@chimeric.de>
362 */
363function _html_list_index($item){
364  global $ID;
365  global $conf;
366  $ret = '';
367  $base = ':'.$item['id'];
368  $base = substr($base,strrpos($base,':')+1);
369  if($item['type']=='d'){
370    if(@file_exists(wikiFN($item['id'].':'.$conf['start']))) {
371      $ret .= '<a href="'.wl($item['id'].':'.$conf['start']).'" class="idx_dir">';
372      $ret .= $base;
373      $ret .= '</a>';
374    } else {
375      $ret .= '<a href="'.wl($ID,'idx='.$item['id']).'" class="idx_dir">';
376      $ret .= $base;
377      $ret .= '</a>';
378    }
379  }else{
380    $ret .= html_wikilink(':'.$item['id']);
381  }
382  return $ret;
383}
384
385/**
386 * searches for namespace sidebars
387 *
388 * @author Michael Klier <chi@chimeric.de>
389 */
390function _getNsSb($id) {
391    $pname = tpl_getConf('pagename');
392    $ns_sb = '';
393    $path  = explode(':', $id);
394    $found = false;
395
396    while(count($path) > 0) {
397        $ns_sb = implode(':', $path).':'.$pname;
398        if(@file_exists(wikiFN($ns_sb))) return $ns_sb;
399        array_pop($path);
400    }
401
402    // nothing found
403    return false;
404}
405
406//Setup vim: ts=4 sw=4:
407?>