1<?php
2/**
3 * DokuWiki Template DokuCMS Functions - adapted from arctic 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 */
10
11// must be run from within DokuWiki
12if (!defined('DOKU_INC')) die();
13
14/**
15 * Prints the sidebars
16 *
17 */
18function _tpl_sidebar() {
19    global $lang;
20    global $conf;
21    global $ID;
22
23    if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
24    $pname = 'sidebar';
25
26    $tpl = $conf['template'];
27
28    if($conf['tpl'][$tpl]['sidebar']== 'file')  {
29        $ns_sb = _tpl_getNsSb($ID);
30        if($ns_sb && auth_quickaclcheck($ns_sb) >= AUTH_READ) {
31            echo '<div class="sidebar_box">', DOKU_LF;
32            echo _tpl_sidebar_xhtml($ns_sb), DOKU_LF;
33            echo '</div>', DOKU_LF;
34         } elseif(@file_exists(wikiFN($pname)) && auth_quickaclcheck($pname) >= AUTH_READ) {
35            echo '<div class="sidebar_box">', DOKU_LF;
36            echo _tpl_sidebar_xhtml($pname), DOKU_LF;
37            echo '</div>', DOKU_LF;
38        } else {
39            echo '<div class="sidebar_box">', DOKU_LF;
40            echo '&nbsp;', DOKU_LF;
41            echo '</div>', DOKU_LF;
42			   }
43   } else {
44    			echo '<div class="sidebar_box">', DOKU_LF;
45    			echo '  ', _tpl_index_xhtml($ID), DOKU_LF;
46    			echo '</div>', DOKU_LF;
47	 }
48}
49
50/**
51 * searches for namespace sidebars
52 */
53function _tpl_getNsSb($id) {
54    $pname = 'sidebar';
55    $ns_sb = '';
56    $path  = explode(':', $id);
57
58    while(count($path) > 0) {
59        $ns_sb = implode(':', $path).':'.$pname;
60        if(@file_exists(wikiFN($ns_sb))) return $ns_sb;
61        array_pop($path);
62    }
63
64    // nothing found
65    return false;
66}
67
68/**
69 * Removes the TOC of the sidebar pages and
70 * shows a edit button if the user has enough rights
71 *
72 */
73function _tpl_sidebar_xhtml($sb) {
74  global $conf;
75  $tpl = $conf['template'];
76  $data = p_wiki_xhtml($sb,'',false);
77  if(auth_quickaclcheck($sb) >= AUTH_EDIT and $conf['tpl'][$tpl]['sidebaredit']) {
78    $data .= '<div class="secedit">'.html_btn('secedit',$sb,'',array('do'=>'edit','rev'=>'','post')).'</div>';
79  }
80  // strip TOC
81  $data = preg_replace('/<div class="toc">.*?(<\/div>\n<\/div>)/s', '', $data);
82  // replace headline ids for XHTML compliance
83  $data = preg_replace('/(<h.*?><a.*?id=")(.*?)(">.*?<\/a><\/h.*?>)/','\1sb_left_\2\3', $data);
84  return ($data);
85}
86
87/**
88 * Renders the Index
89 *
90 */
91function _tpl_index_xhtml($ns) {
92  require_once(DOKU_INC.'inc/search.php');
93  global $conf;
94  global $ID;
95  $dir = $conf['datadir'];
96  $tpl = $conf['template'];
97  if(isset($conf['start'])) {
98    $start = $conf['start'];
99  } else {
100    $start = 'start';
101  }
102
103  $ns  = cleanID($ns);
104# fixme use appropriate function
105  if(empty($ns)){
106    $ns = dirname(str_replace(':','/',$ID));
107    if($ns == '.') $ns ='';
108  }
109  $ns  = utf8_encodeFN(str_replace(':','/',$ns));
110
111  $data = array();
112  $data2 = array();
113  search($data,$conf['datadir'],'search_index',array('ns' => $ns));
114  $i = 0;
115  $cleanindexlist = array();
116  if($conf['tpl'][$tpl]['cleanindexlist']) {
117   	$cleanindexlist = explode(',', $conf['tpl'][$tpl]['cleanindexlist']);
118   	$i = 0;
119   	foreach($cleanindexlist as $tmpitem) {
120   		$cleanindexlist[$i] = trim($tmpitem);
121   		$i++;
122   	}
123	}
124  $i = 0;
125	foreach($data as $item) {
126    if($conf['tpl'][$tpl]['cleanindex']) {
127      if($item['id'] == 'playground' or $item['id'] == 'wiki') {
128        continue;
129      }
130      if(count($cleanindexlist)) {
131      	if(strpos($item['id'], ':')) {
132      		list($tmpitem) = explode(':',$item['id']);
133      	} else {
134      		$tmpitem = $item['id'];
135      	}
136	      if(in_array($tmpitem, $cleanindexlist)) {
137	        continue;
138	      }
139			}
140    }
141    if($item['id'] == 'sidebar' or $item['id'] == $start
142       or preg_match('/:'.$start.'$/',$item['id'])
143       or preg_match('/(\w+):\1$/',$item['id'])) {
144      continue;
145    }
146    if($item['type'] == 'f' and array_key_exists($item['id'], $data2)) {
147      $data2[$item['id']]['type'] = 'd';
148      $data2[$item['id']]['ns'] = $item['id'];
149      continue;
150    }
151    $data2[$item['id']] = $item;
152  }
153# echo index with empty items removed
154  echo html_buildlist($data2,'idx','_tpl_html_list_index','_tpl_html_li_index');
155}
156
157/**
158 * Index item formatter
159 *
160 * User function for html_buildlist()
161 *
162 */
163function _tpl_html_list_index($item){
164  global $conf;
165  $ret = '';
166  if($item['type']=='d'){
167    if(@file_exists(wikiFN($item['id'].':'.$conf['start']))) {
168      $ret .= html_wikilink($item['id'].':'.$conf['start'], $item['title']);
169    } elseif(@file_exists(wikiFN($item['id'].':'.$item['id']))) {
170      $ret .= html_wikilink($item['id'].':'.$item['id'], $item['title']);
171    } else {
172      $ret .= html_wikilink($item['id'].':', $item['title']);
173    }
174  } else {
175    $ret .= html_wikilink(':'.$item['id'], $item['title']);
176  }
177  return $ret;
178}
179/**
180 * Index List item
181 *
182 * This user function is used in html_buildlist to build the
183 * <li> tags for namespaces when displaying the page index
184 * it gives different classes to opened or closed "folders"
185 *
186 * @author Andreas Gohr <andi@splitbrain.org>
187 *
188 * @param array $item
189 * @return string html
190 */
191function _tpl_html_li_index($item){
192  global $INFO;
193
194  $class = '';
195  $id = '';
196
197  if($item['type'] == "f"){
198    // scroll to the current item
199    return '<li class="level'.$item['level'].$class.'" '.$id.'>';
200  } elseif($item['open']) {
201    return '<li class="open">';
202  } else {
203    return '<li class="closed">';
204  }
205}
206
207
208# dokucms modified version of pageinfo
209function _tpl_pageinfo(){
210  global $conf;
211  global $lang;
212  global $INFO;
213  global $ID;
214
215  // return if we are not allowed to view the page
216  if (!auth_quickaclcheck($ID)) { return; }
217
218  // prepare date and path
219  $date = dformat($INFO['lastmod']);
220
221  // echo it
222  if($INFO['exists']){
223    echo $lang['lastmod'], ': ', $date;
224    if($_SERVER['REMOTE_USER']) {
225      if($INFO['editor']) {
226        echo ' ', $lang['by'], ' ', $INFO['editor'];
227      } else {
228        echo ' (', $lang['external_edit'], ')';
229      }
230      if($INFO['locked']){
231        echo ' &middot; ', $lang['lockedby'], ': ', $INFO['locked'];
232      }
233    }
234    return true;
235  }
236  return false;
237}
238?>
239