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