1<?php
2/**
3 * DokuWiki Template DokuKIT Functions - adapted from DokuCMS template
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 * @author  Michael Klier <chi@chimeric.de>
8 * @author Klaus Vormweg <klaus.vormweg@gmx.de>
9 * @author Robin Roth <robin.roth@kit.edu>
10 * @author Martin Gabelmann <martin@gabelmann.biz>
11 */
12
13// must be run from within DokuWiki
14if (!defined('DOKU_INC')) die();
15
16/**
17 * Prints the sidebars
18 *
19 */
20function _tpl_sidebar() {
21    global $lang;
22    global $conf;
23    global $ID;
24    if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
25    $pname = 'sidebar';
26
27    // print static sidebar from wiki page(s)
28    if(tpl_getConf('sidebar')== 'file')  {
29        // search for sidebar files recursively starting in the current namespace
30        $ns_sb = _getNsSb($ID);
31        if($ns_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
32            echo '<div class="sidebar_box file">', DOKU_LF;
33            echo p_sidebar_xhtml($ns_sb), DOKU_LF;
34            echo '</div>', DOKU_LF;
35        } else {
36            echo '<div class="sidebar_box">', DOKU_LF;
37            echo '&nbsp;', DOKU_LF;
38            echo '</div>', DOKU_LF;
39        }
40    // load sidebar from index
41	} else {
42        echo '<div class="sidebar_box dynamic">', DOKU_LF;
43        echo '  ', p_index_xhtml($ID), DOKU_LF;
44        echo '</div>', DOKU_LF;
45	 }
46
47    // load wiki sidebar file containing external links/additional content
48    if(
49        tpl_getConf('showextlinks')== '1' &&
50        @file_exists(wikiFN(tpl_getConf('extlinks'))) &&
51        auth_quickaclcheck(tpl_getConf('extlinks')) >= AUTH_READ
52      )
53    {
54        echo '<div class="sidebar_box external_sidebar">', DOKU_LF;
55        echo p_sidebar_xhtml(tpl_getConf('extlinks')), DOKU_LF;
56        echo '</div>', DOKU_LF;
57    }
58
59}
60
61/**
62 * right info boxes
63 */
64function _tpl_infobox() {
65    global $lang;
66    global $conf;
67    global $ID;
68
69    if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
70    $boxfiles = explode(',', tpl_getConf('boxfiles'));
71    $infoboxes = array();
72
73    if($ID !=  $conf['start'] && $ID != $conf['lang'].':'.$conf['start'] || !tpl_getConf('showboxes')) unset($boxfiles);
74
75    if(count($boxfiles)>0){
76        while (list(, $pname) = each($boxfiles)) {
77            $ns_sb = _getNsSb($ID, $pname);
78            if($ns_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
79                $infoboxes[] = '<div class="infobox"><div class="infobox-inner">'.p_sidebar_xhtml($ns_sb).'</div></div>';
80            } elseif(@file_exists(wikiFN($pname)) && auth_quickaclcheck($pname) >= AUTH_READ) {
81                $infoboxes[] = '<div class="infobox"><div class="infobox-inner">'.p_sidebar_xhtml($pname).'</div></div>';
82            }
83        }
84    }
85
86    if(count($infoboxes)>0){
87        echo '<div id="right-row">', DOKU_LF;
88        while (list(, $infobox) = each($infoboxes)) echo $infobox, DOKU_LF;
89        echo '</div>', DOKU_LF;
90    } else {
91        echo '<style type="text/css">#middle-row, .tabelle3, .ptabelle, .ptabelleblank, .datentabelle { width:760px !important; }  </style>', DOKU_LF;
92    }
93}
94
95
96/**
97 * searches for namespace sidebars
98 */
99function _getNsSb($id, $pname = 'sidebar') {
100    $ns_sb = '';
101    $path  = explode(':', $id);
102
103    while(count($path) > 0) {
104        $ns_sb = implode(':', $path).':'.$pname;
105        if(@file_exists(wikiFN($ns_sb))) return $ns_sb;
106        array_pop($path);
107    }
108    if(@file_exists(wikiFN($pname))) return $pname;
109
110    // nothing found
111    return false;
112}
113
114/**
115 * Removes the TOC of the sidebar pages and
116 * shows a edit button if the user has enough rights
117 *
118 */
119function p_sidebar_xhtml($sb) {
120    global $conf;
121    $tpl = $conf['template'];
122    $data = p_wiki_xhtml($sb,'',false);
123
124    if(auth_quickaclcheck($sb) >= AUTH_EDIT and tpl_getConf('sidebaredit')) {
125        $data .= '<div class="secedit">'.html_btn('secedit',$sb,'',array('do'=>'edit','rev'=>'','post')).'</div>';
126    }
127    // strip TOC
128    $data = preg_replace('/<div class="toc">.*?(<\/div>\n<\/div>)/s', '', $data);
129    // remove curid - its placed via javascript (caching compatible)
130    $data = preg_replace('/<span class="curid">(.*)<\/span>/', '\1', $data);
131    // replace headline ids for XHTML compliance
132    $data = preg_replace('/(<h.*?><a.*?id=")(.*?)(">.*?<\/a><\/h.*?>)/','\1sb_left_\2\3', $data);
133    return ($data);
134}
135
136/**
137 * Renders the Index
138 *
139 */
140function p_index_xhtml($ns) {
141    require_once(DOKU_INC.'inc/search.php');
142    global $conf;
143    global $ID;
144    $dir = $conf['datadir'];
145    $tpl = $conf['template'];
146    $start = isset($conf['start']) ? $conf['start'] : 'start';
147
148    $ns  = getNS($ns);
149
150    $data = array();
151    search($data,$conf['datadir'],'search_index',array('ns' => $ns));
152    $i = 0;
153    $cleanindexlist = array();
154    if(tpl_getConf('cleanindexlist')) {
155        $cleanindexlist = explode(',', tpl_getConf('cleanindexlist'));
156     	$i = 0;
157     	foreach($cleanindexlist as $tmpitem) {
158     	    $cleanindexlist[$i] = trim($tmpitem);
159     		$i++;
160     	}
161    }
162    $i = 0;
163    foreach($data as $item) {
164        if(tpl_getConf('cleanindex')) {
165            if(count($cleanindexlist)) {
166                if(strpos($item['id'], ':')) {
167                    list($tmpitem) = explode(':',$item['id']);
168                } else {
169                	$tmpitem = $item['id'];
170                }
171                if(in_array($tmpitem, $cleanindexlist)) {
172                  unset($data[$i]);
173                }
174            }
175        }
176        if(
177            $item['id'] == 'sidebar' or
178            $item['id'] == $start or
179            preg_match('/:'.$start.'$/',$item['id']) or
180            !empty($conf['hidepages']) and preg_match('/'.$conf['hidepages'].'$/',$item['id']) or
181            $item['id'] == tpl_getConf('extlinks')
182        ) {
183            unset($data[$i]);
184          }
185          $i++;
186
187    }
188    # echo index with empty items removed
189    echo html_buildlist($data,'idx','_html_list_index','html_li_index');
190}
191
192/**
193 * Index item formatter
194 *
195 * User function for html_buildlist()
196 *
197 */
198function _html_list_index($item){
199    global $ID;
200    global $conf;
201    $ret = '';
202
203    if($item['type']=='d'){
204        if(@file_exists(wikiFN($item['id'].':'.$conf['start']))) {
205            $ret .= html_wikilink($item['id'].':'.$conf['start']);
206        } else {
207            $ret .= html_wikilink($item['id'].':');
208        }
209    } else {
210        $ret .= html_wikilink(':'.$item['id']);
211    }
212    return $ret;
213}
214
215# dokucms modified version of pageinfo
216function _tpl_pageinfo(){
217    global $conf;
218    global $lang;
219    global $INFO;
220    global $ID;
221
222    // return if we are not allowed to view the page
223    if (!auth_quickaclcheck($ID)) { return; }
224
225    // prepare date and path
226    $date = dformat($INFO['lastmod']);
227
228    // echo it
229    if($INFO['exists']){
230        echo $lang['lastmod'], ' ', $date;
231        if($_SERVER['REMOTE_USER']) {
232            if($INFO['editor']) {
233                echo ' ', $lang['by'], ' ', $INFO['editor'];
234            } else {
235                echo ' (', $lang['external_edit'], ')';
236            }
237            if($INFO['locked']){
238                echo ' &middot; ', $lang['lockedby'], ': ', $INFO['locked'];
239            }
240        }
241        return true;
242    }
243    return false;
244}
245
246?>
247