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 arctic_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    // process contents by given order
49    foreach($sb_order as $sb) {
50        if(in_array($sb,$sb_content)) {
51            $key = array_search($sb,$sb_content);
52            unset($sb_content[$key]);
53            arctic_tpl_sidebar_dispatch($sb,$pos);
54        }
55    }
56
57    // check for left content not specified by order
58    if(is_array($sb_content) && !empty($sb_content) > 0) {
59        foreach($sb_content as $sb) {
60            arctic_tpl_sidebar_dispatch($sb,$pos);
61        }
62    }
63}
64
65/**
66 * Dispatches the given sidebar type to return the right content
67 *
68 * @author Michael Klier <chi@chimeric.de>
69 */
70function arctic_tpl_sidebar_dispatch($sb,$pos) {
71    global $lang;
72    global $conf;
73    global $ID;
74    global $REV;
75    global $INFO;
76    global $TOC;
77
78    $svID  = $ID;   // save current ID
79    $svREV = $REV;  // save current REV
80    $svTOC = $TOC;  // save current TOC
81
82    $pname = tpl_getConf('pagename');
83
84    switch($sb) {
85
86        case 'main':
87            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
88            $main_sb = $pname;
89            if(@page_exists($main_sb) && auth_quickaclcheck($main_sb) >= AUTH_READ) {
90                $always = tpl_getConf('main_sidebar_always');
91                if($always or (!$always && !getNS($ID))) {
92                    print '<div class="main_sidebar sidebar_box">' . DOKU_LF;
93                    print p_sidebar_xhtml($main_sb,$pos) . DOKU_LF;
94                    print '</div>' . DOKU_LF;
95                }
96            } elseif(!@page_exists($main_sb) && auth_quickaclcheck($main_sb) >= AUTH_CREATE) {
97                if(@file_exists(DOKU_TPLINC.'lang/'. $conf['lang'].'/nosidebar.txt')) {
98                    $out = p_render('xhtml', p_get_instructions(io_readFile(DOKU_TPLINC.'lang/'.$conf['lang'].'/nosidebar.txt')), $info);
99                } else {
100                    $out = p_render('xhtml', p_get_instructions(io_readFile(DOKU_TPLINC.'lang/en/nosidebar.txt')), $info);
101                }
102                $link = '<a href="' . wl($pname) . '" class="wikilink2">' . $pname . '</a>' . DOKU_LF;
103                print '<div class="main_sidebar sidebar_box">' . DOKU_LF;
104                print str_replace('LINK', $link, $out);
105                print '</div>' . DOKU_LF;
106            }
107            break;
108
109        case 'namespace':
110            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
111            $user_ns  = tpl_getConf('user_sidebar_namespace');
112            $group_ns = tpl_getConf('group_sidebar_namespace');
113            if(!preg_match("/^".$user_ns.":.*?$|^".$group_ns.":.*?$/", $svID)) { // skip group/user sidebars and current ID
114                $ns_sb = _getNsSb($svID);
115                if($ns_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
116                    print '<div class="namespace_sidebar sidebar_box">' . DOKU_LF;
117                    print p_sidebar_xhtml($ns_sb,$pos) . DOKU_LF;
118                    print '</div>' . DOKU_LF;
119                }
120            }
121            break;
122
123        case 'user':
124            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
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(@page_exists($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            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
150            $group_ns = tpl_getConf('group_sidebar_namespace');
151            if(isset($INFO['userinfo']['name'], $INFO['userinfo']['grps'])) {
152                foreach($INFO['userinfo']['grps'] as $grp) {
153                    $group_sb = $group_ns.':'.$grp.':'.$pname;
154                    if(@page_exists($group_sb) && auth_quickaclcheck(cleanID($group_sb)) >= AUTH_READ) {
155                        $subst = array('pattern' => array('/@GROUP@/'), 'replace' => array($grp));
156                        print '<div class="group_sidebar sidebar_box">' . DOKU_LF;
157                        print p_sidebar_xhtml($group_sb,$pos,$subst) . DOKU_LF;
158                        print '</div>' . DOKU_LF;
159                    }
160                }
161            }
162            break;
163
164        case 'index':
165            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
166            print '<div class="index_sidebar sidebar_box">' . DOKU_LF;
167            print '  ' . p_index_xhtml($svID,$pos) . DOKU_LF;
168            print '</div>' . DOKU_LF;
169            break;
170
171        case 'toc':
172            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
173            if(auth_quickaclcheck($svID) >= AUTH_READ) {
174                $toc = tpl_toc(true);
175                // replace ids to keep XHTML compliance
176                if(!empty($toc)) {
177                    $toc = preg_replace('/id="(.*?)"/', 'id="sb__' . $pos . '__\1"', $toc);
178                    print '<div class="toc_sidebar sidebar_box">' . DOKU_LF;
179                    print ($toc);
180                    print '</div>' . DOKU_LF;
181                }
182            }
183            break;
184
185        case 'toolbox':
186
187            if(tpl_getConf('hideactions') && !isset($_SERVER['REMOTE_USER'])) return;
188
189            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) {
190                print '<div class="toolbox_sidebar sidebar_box">' . DOKU_LF;
191                print '  <div class="level1">' . DOKU_LF;
192                print '    <ul>' . DOKU_LF;
193                print '      <li><div class="li">';
194                tpl_actionlink('login');
195                print '      </div></li>' . DOKU_LF;
196                print '    </ul>' . DOKU_LF;
197                print '  </div>' . DOKU_LF;
198                print '</div>' . DOKU_LF;
199            } else {
200                $actions = array('admin',
201                                 'revert',
202                                 'edit',
203                                 'history',
204                                 'recent',
205                                 'backlink',
206                                 'media',
207                                 'subscription',
208                                 'index',
209                                 'login',
210                                 'profile',
211                                 'top');
212
213                print '<div class="toolbox_sidebar sidebar_box">' . DOKU_LF;
214                print '  <div class="level1">' . DOKU_LF;
215                print '    <ul>' . DOKU_LF;
216
217                foreach($actions as $action) {
218                    if(!actionOK($action)) continue;
219                    // start output buffering
220                    if($action == 'edit') {
221                        // check if new page button plugin is available
222                        if(!plugin_isdisabled('npd') && ($npd =& plugin_load('helper', 'npd'))) {
223                            $npb = $npd->html_new_page_button(true);
224                            if($npb) {
225                                print '    <li><div class="li">';
226                                print $npb;
227                                print '</div></li>' . DOKU_LF;
228                            }
229                        }
230                    }
231                    ob_start();
232                    print '     <li><div class="li">';
233                    if(tpl_actionlink($action)) {
234                        print '</div></li>' . DOKU_LF;
235                        ob_end_flush();
236                    } else {
237                        ob_end_clean();
238                    }
239                }
240
241                print '    </ul>' . DOKU_LF;
242                print '  </div>' . DOKU_LF;
243                print '</div>' . DOKU_LF;
244            }
245
246            break;
247
248        case 'trace':
249            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
250            print '<div class="trace_sidebar sidebar_box">' . DOKU_LF;
251            print '  <h1>'.$lang['breadcrumb'].'</h1>' . DOKU_LF;
252            print '  <div class="breadcrumbs">' . DOKU_LF;
253            ($conf['youarehere'] != 1) ? tpl_breadcrumbs() : tpl_youarehere();
254            print '  </div>' . DOKU_LF;
255            print '</div>' . DOKU_LF;
256            break;
257
258        case 'extra':
259            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
260            print '<div class="extra_sidebar sidebar_box">' . DOKU_LF;
261            @include(dirname(__FILE__).'/' . $pos .'_sidebar.html');
262            print '</div>' . DOKU_LF;
263            break;
264
265        default:
266            if(tpl_getConf('closedwiki') && !isset($_SERVER['REMOTE_USER'])) return;
267            // check for user defined sidebars
268            if(@file_exists(DOKU_TPLINC.'sidebars/'.$sb.'/sidebar.php')) {
269                print '<div class="'.$sb.'_sidebar sidebar_box">' . DOKU_LF;
270                @require_once(DOKU_TPLINC.'sidebars/'.$sb.'/sidebar.php');
271                print '</div>' . DOKU_LF;
272            }
273            break;
274    }
275
276    // restore ID, REV and TOC
277    $ID  = $svID;
278    $REV = $svREV;
279    $TOC = $svTOC;
280}
281
282/**
283 * Removes the TOC of the sidebar pages and
284 * shows a edit button if the user has enough rights
285 *
286 * TODO sidebar caching
287 *
288 * @author Michael Klier <chi@chimeric.de>
289 */
290function p_sidebar_xhtml($sb,$pos,$subst=array()) {
291    $data = p_wiki_xhtml($sb,'',false);
292    if(!empty($subst)) {
293        $data = preg_replace($subst['pattern'], $subst['replace'], $data);
294    }
295    if(auth_quickaclcheck($sb) >= AUTH_EDIT) {
296        $data .= '<div class="secedit">'.html_btn('secedit',$sb,'',array('do'=>'edit','rev'=>'','post')).'</div>';
297    }
298    // strip TOC
299    $data = preg_replace('/<div class="toc">.*?(<\/div>\n<\/div>)/s', '', $data);
300    // replace headline ids for XHTML compliance
301    $data = preg_replace('/(<h.*?><a.*?name=")(.*?)(".*?id=")(.*?)(">.*?<\/a><\/h.*?>)/','\1sb_'.$pos.'_\2\3sb_'.$pos.'_\4\5', $data);
302    return ($data);
303}
304
305/**
306 * Renders the Index
307 *
308 * copy of html_index located in /inc/html.php
309 *
310 * TODO update to new AJAX index possible?
311 *
312 * @author Andreas Gohr <andi@splitbrain.org>
313 * @author Michael Klier <chi@chimeric.de>
314 */
315function p_index_xhtml($ns,$pos) {
316  require_once(DOKU_INC.'inc/search.php');
317  global $conf;
318  global $ID;
319  $dir = $conf['datadir'];
320  $ns  = cleanID($ns);
321  #fixme use appropriate function
322  if(empty($ns)){
323    $ns = dirname(str_replace(':','/',$ID));
324    if($ns == '.') $ns ='';
325  }
326  $ns  = utf8_encodeFN(str_replace(':','/',$ns));
327
328  // extract only the headline
329  preg_match('/<h1>.*?<\/h1>/', p_locale_xhtml('index'), $match);
330  print preg_replace('#<h1(.*?id=")(.*?)(".*?)h1>#', '<h1\1sidebar_'.$pos.'_\2\3h1>', $match[0]);
331
332  $data = array();
333  search($data,$conf['datadir'],'search_index',array('ns' => $ns));
334
335  print '<div id="' . $pos . '__index__tree">' . DOKU_LF;
336  print html_buildlist($data,'idx','html_list_index','html_li_index');
337  print '</div>' . DOKU_LF;
338}
339
340/**
341 * searches for namespace sidebars
342 *
343 * @author Michael Klier <chi@chimeric.de>
344 */
345function _getNsSb($id) {
346    $pname = tpl_getConf('pagename');
347    $ns_sb = '';
348    $path  = explode(':', $id);
349    $found = false;
350
351    while(count($path) > 0) {
352        $ns_sb = implode(':', $path).':'.$pname;
353        if(@page_exists($ns_sb)) return $ns_sb;
354        array_pop($path);
355    }
356
357    // nothing found
358    return false;
359}
360
361/**
362 * Checks wether the sidebar should be hidden or not
363 *
364 * @author Michael Klier <chi@chimeric.de>
365 */
366function arctic_tpl_sidebar_hide() {
367    global $ACT;
368    $act_hide = array( 'admin', 'conflict', 'media' );
369    if(in_array($ACT, $act_hide)) {
370        return true;
371    } else {
372        return false;
373    }
374}
375
376// vim:ts=4:sw=4:et:enc=utf-8:
377?>
378