1<?php
2/**
3 * DokuWiki Template Arctic Functions
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Michael Klier <chi@chimeric.de>
7 */
8
9// must be run from within DokuWiki
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_LF')) define('DOKU_LF',"\n");
12
13// load sidebar contents
14$sbl   = explode(',',tpl_getConf('left_sidebar_content'));
15$sbr   = explode(',',tpl_getConf('right_sidebar_content'));
16$sbpos = tpl_getConf('sidebar');
17
18// set notoc option and toolbar regarding the sitebar setup
19switch($sbpos) {
20  case 'both':
21    $notoc = (in_array('toc',$sbl) || in_array('toc',$sbr)) ? true : false;
22    $toolb = (in_array('toolbox',$sbl) || in_array('toolbox',$sbr)) ? true : false;
23    break;
24  case 'left':
25    $notoc = (in_array('toc',$sbl)) ? true : false;
26    $toolb = (in_array('toolbox',$sbl)) ? true : false;
27    break;
28  case 'right':
29    $notoc = (in_array('toc',$sbr)) ? true : false;
30    $toolb = (in_array('toolbox',$sbr)) ? true : false;
31    break;
32  case 'none':
33    $notoc = false;
34    $toolb = false;
35    break;
36}
37
38/**
39 * Prints the sidebars
40 *
41 * @author Michael Klier <chi@chimeric.de>
42 */
43function tpl_sidebar($pos) {
44
45    $sb_order   = ($pos == 'left') ? explode(',', tpl_getConf('left_sidebar_order'))   : explode(',', tpl_getConf('right_sidebar_order'));
46    $sb_content = ($pos == 'left') ? explode(',', tpl_getConf('left_sidebar_content')) : explode(',', tpl_getConf('right_sidebar_content'));
47
48    if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) {
49        if(in_array('toolbox', $sb_content)) {
50            print '<div class="toolbox_sidebar sidebar_box">' . DOKU_LF;
51            print '  <div class="level1">' . DOKU_LF;
52            print '    <ul>' . DOKU_LF;
53            print '      <li><div class="li">';
54            tpl_actionlink('login');
55            print '      </div></li>' . DOKU_LF;
56            print '    </ul>' . DOKU_LF;
57            print '  </div>' . DOKU_LF;
58            print '</div>' . DOKU_LF;
59        }
60        return;
61    }
62
63    // process contents by given order
64    foreach($sb_order as $sb) {
65        if(in_array($sb,$sb_content)) {
66            $key = array_search($sb,$sb_content);
67            unset($sb_content[$key]);
68            tpl_sidebar_dispatch($sb,$pos);
69        }
70    }
71
72    // check for left content not specified by order
73    if(is_array($sb_content) && !empty($sb_content) > 0) {
74        foreach($sb_content as $sb) {
75            tpl_sidebar_dispatch($sb,$pos);
76        }
77    }
78}
79
80/**
81 * Dispatches the given sidebar type to return the right content
82 *
83 * @author Michael Klier <chi@chimeric.de>
84 */
85function tpl_sidebar_dispatch($sb,$pos) {
86    global $lang;
87    global $conf;
88    global $ID;
89    global $REV;
90    global $INFO;
91
92    $svID  = $ID;   // save current ID
93    $svREV = $REV;  // save current REV
94
95    $pname = tpl_getConf('pagename');
96
97    switch($sb) {
98
99        case 'main':
100            $main_sb = $pname;
101            if(@file_exists(wikiFN($main_sb)) && auth_quickaclcheck($main_sb) >= AUTH_READ) {
102                $always = tpl_getConf('main_sidebar_always');
103                if($always or (!$always && !getNS($ID))) {
104                    print '<div class="main_sidebar sidebar_box">' . DOKU_LF;
105                    print p_sidebar_xhtml($main_sb,$pos) . DOKU_LF;
106                    print '</div>' . DOKU_LF;
107                }
108            }
109            break;
110
111        case 'namespace':
112            $user_ns  = tpl_getConf('user_sidebar_namespace');
113            $group_ns = tpl_getConf('group_sidebar_namespace');
114            if(!preg_match("/^".$user_ns.":.*?$|^".$group_ns.":.*?$/", $svID)) { // skip group/user sidebars and current ID
115                $ns_sb = _getNsSb($svID);
116                if($ns_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
117                    print '<div class="namespace_sidebar sidebar_box">' . DOKU_LF;
118                    print p_sidebar_xhtml($ns_sb,$pos) . DOKU_LF;
119                    print '</div>' . DOKU_LF;
120                }
121            }
122            break;
123
124        case 'user':
125            $user_ns = tpl_getConf('user_sidebar_namespace');
126            if(isset($INFO['userinfo']['name'])) {
127                $user = $_SERVER['REMOTE_USER'];
128                $user_sb = $user_ns . ':' . $user . ':' . $pname;
129                if(@file_exists(wikiFN($user_sb))) {
130                    $subst = array('pattern' => array('/@USER@/'), 'replace' => array($user));
131                    print '<div class="user_sidebar sidebar_box">' . DOKU_LF;
132                    print p_sidebar_xhtml($user_sb,$pos,$subst) . DOKU_LF;
133                    print '</div>';
134                }
135                // check for namespace sidebars in user namespace too
136                if(preg_match('/'.$user_ns.':'.$user.':.*/', $svID)) {
137                    $ns_sb = _getNsSb($svID);
138                    if($ns_sb && $ns_sb != $user_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
139                        print '<div class="namespace_sidebar sidebar_box">' . DOKU_LF;
140                        print p_sidebar_xhtml($ns_sb,$pos) . DOKU_LF;
141                        print '</div>' . DOKU_LF;
142                    }
143                }
144
145            }
146            break;
147
148        case 'group':
149            $group_ns = tpl_getConf('group_sidebar_namespace');
150            if(isset($INFO['userinfo']['name'], $INFO['userinfo']['grps'])) {
151                foreach($INFO['userinfo']['grps'] as $grp) {
152                    $group_sb = $group_ns.':'.$grp.':'.$pname;
153                    if(@file_exists(wikiFN($group_sb)) && auth_quickaclcheck(cleanID($group_sb)) >= AUTH_READ) {
154                        $subst = array('pattern' => array('/@GROUP@/'), 'replace' => array($grp));
155                        print '<div class="group_sidebar sidebar_box">' . DOKU_LF;
156                        print p_sidebar_xhtml($group_sb,$pos,$subst) . DOKU_LF;
157                        print '</div>' . DOKU_LF;
158                    }
159                }
160            }
161            break;
162
163        case 'index':
164            print '<div class="index_sidebar sidebar_box">' . DOKU_LF;
165            print '  ' . p_index_xhtml($svID,$pos) . DOKU_LF;
166            print '</div>' . DOKU_LF;
167            break;
168
169        case 'toc':
170            if(auth_quickaclcheck($svID) >= AUTH_READ) {
171                $toc = tpl_toc(true);
172                // replace ids to keep XHTML compliance
173                if(!empty($toc)) {
174                    $toc = preg_replace('/id="(.*?)"/', 'id="sb__' . $pos . '__\1"', $toc);
175                    print '<div class="toc_sidebar sidebar_box">' . DOKU_LF;
176                    print ($toc);
177                    print '</div>' . DOKU_LF;
178                }
179            }
180            break;
181
182        case 'toolbox':
183            $actions = array('admin', 'edit', 'history', 'recent', 'backlink', 'subscribe', 'subscribens', 'index', 'login', 'profile');
184
185            print '<div class="toolbox_sidebar sidebar_box">' . DOKU_LF;
186            print '  <div class="level1">' . DOKU_LF;
187            print '    <ul>' . DOKU_LF;
188
189            foreach($actions as $action) {
190                if(!actionOK($action)) continue;
191                // start output buffering
192                if($action == 'edit') {
193                    // check if new page button plugin is available
194                    if(!plugin_isdisabled('npd') && ($npd =& plugin_load('helper', 'npd'))) {
195                        $npb = $npd->html_new_page_button(true);
196                        if($npb) {
197                            print '    <li><div class="li">';
198                            print $npb;
199                            print '</div></li>' . DOKU_LF;
200                        }
201                    }
202                }
203                ob_start();
204                print '     <li><div class="li">';
205                if(tpl_actionlink($action)) {
206                    print '</div></li>' . DOKU_LF;
207                    ob_end_flush();
208                } else {
209                    ob_end_clean();
210                }
211            }
212
213            print '    </ul>' . DOKU_LF;
214            print '  </div>' . DOKU_LF;
215            print '</div>' . DOKU_LF;
216            break;
217
218        case 'trace':
219            print '<div class="trace_sidebar sidebar_box">' . DOKU_LF;
220            print '  <h1>'.$lang['breadcrumb'].'</h1>' . DOKU_LF;
221            print '  <div class="breadcrumbs">' . DOKU_LF;
222            ($conf['youarehere'] != 1) ? tpl_breadcrumbs() : tpl_youarehere();
223            print '  </div>' . DOKU_LF;
224            print '</div>' . DOKU_LF;
225            break;
226
227        case 'extra':
228            print '<div class="extra_sidebar sidebar_box">' . DOKU_LF;
229            @include(dirname(__FILE__).'/' . $pos .'_sidebar.html');
230            print '</div>' . DOKU_LF;
231            break;
232
233        default:
234            // check for user defined sidebars
235            if(@file_exists(DOKU_TPLINC.'sidebars/'.$sb.'/sidebar.php')) {
236                print '<div class="'.$sb.'_sidebar sidebar_box">' . DOKU_LF;
237                @require_once(DOKU_TPLINC.'sidebars/'.$sb.'/sidebar.php');
238                print '</div>' . DOKU_LF;
239            }
240            break;
241    }
242
243    // restore ID and REV
244    $ID  = $svID;
245    $REV = $svREV;
246}
247
248/**
249 * Removes the TOC of the sidebar pages and
250 * shows a edit button if the user has enough rights
251 *
252 * TODO sidebar caching
253 *
254 * @author Michael Klier <chi@chimeric.de>
255 */
256function p_sidebar_xhtml($sb,$pos,$subst=array()) {
257    $data = p_wiki_xhtml($sb,'',false);
258    if(!empty($subst)) {
259        $data = preg_replace($subst['pattern'], $subst['replace'], $data);
260    }
261    if(auth_quickaclcheck($sb) >= AUTH_EDIT) {
262        $data .= '<div class="secedit">'.html_btn('secedit',$sb,'',array('do'=>'edit','rev'=>'','post')).'</div>';
263    }
264    // strip TOC
265    $data = preg_replace('/<div class="toc">.*?(<\/div>\n<\/div>)/s', '', $data);
266    // replace headline ids for XHTML compliance
267    $data = preg_replace('/(<h.*?><a.*?name=")(.*?)(".*?id=")(.*?)(">.*?<\/a><\/h.*?>)/','\1sb_'.$pos.'_\2\3sb_'.$pos.'_\4\5', $data);
268    return ($data);
269}
270
271/**
272 * Renders the Index
273 *
274 * copy of html_index located in /inc/html.php
275 *
276 * TODO update to new AJAX index possible?
277 *
278 * @author Andreas Gohr <andi@splitbrain.org>
279 * @author Michael Klier <chi@chimeric.de>
280 */
281function p_index_xhtml($ns,$pos) {
282  require_once(DOKU_INC.'inc/search.php');
283  global $conf;
284  global $ID;
285  $dir = $conf['datadir'];
286  $ns  = cleanID($ns);
287  #fixme use appropriate function
288  if(empty($ns)){
289    $ns = dirname(str_replace(':','/',$ID));
290    if($ns == '.') $ns ='';
291  }
292  $ns  = utf8_encodeFN(str_replace(':','/',$ns));
293
294  // extract only the headline
295  preg_match('/<h1>.*?<\/h1>/', p_locale_xhtml('index'), $match);
296  print preg_replace('#<h1(.*?id=")(.*?)(".*?)h1>#', '<h1\1sidebar_'.$pos.'_\2\3h1>', $match[0]);
297
298  $data = array();
299  search($data,$conf['datadir'],'search_index',array('ns' => $ns));
300
301  print '<div id="' . $pos . '__index__tree">' . DOKU_LF;
302  print html_buildlist($data,'idx','html_list_index','html_li_index');
303  print '</div>' . DOKU_LF;
304}
305
306/**
307 * searches for namespace sidebars
308 *
309 * @author Michael Klier <chi@chimeric.de>
310 */
311function _getNsSb($id) {
312    $pname = tpl_getConf('pagename');
313    $ns_sb = '';
314    $path  = explode(':', $id);
315    $found = false;
316
317    while(count($path) > 0) {
318        $ns_sb = implode(':', $path).':'.$pname;
319        if(@file_exists(wikiFN($ns_sb))) return $ns_sb;
320        array_pop($path);
321    }
322
323    // nothing found
324    return false;
325}
326
327/**
328 * Checks wether the sidebar should be hidden or not
329 *
330 * @author Michael Klier <chi@chimeric.de>
331 */
332function tpl_sidebar_hide() {
333    global $ACT;
334    $act_hide = array( 'edit', 'diff', 'preview', 'admin', 'conflict', 'draft', 'recover');
335    if(in_array($ACT, $act_hide)) {
336        return true;
337    } else {
338        return false;
339    }
340}
341
342// vim:ts=4:sw=4:et:enc=utf-8:
343?>
344