1<?php
2/*
3 * DokuWiki Template Wallpaper 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/* prints the menu */
15function _wp_tpl_mainmenu() {
16  require_once(DOKU_INC.'inc/search.php');
17  global $conf;
18  global $ID;
19
20/* options for search() function */
21  $opts = array(
22   'depth' => 0,
23   'listfiles' => true,
24   'listdirs'  => true,
25   'pagesonly' => true,
26   'firsthead' => true,
27   'sneakyacl' => true
28  );
29
30  $dir = $conf['datadir'];
31  $tpl = $conf['template'];
32  if(isset($conf['start'])) {
33    $start = $conf['start'];
34  } else {
35    $start = 'start';
36  }
37
38  $data = array();
39  $ff = TRUE;
40  if($conf['tpl'][$tpl]['menufilename']) {
41    $menufilename = $conf['tpl'][$tpl]['menufilename'];
42  } else {
43    $menufilename = 'menu';
44  }
45  if($conf['tpl'][$tpl]['usemenufile']) {
46    $filepath = wikiFN($menufilename);
47    if(!file_exists($filepath)) {
48      $ff = FALSE;
49    } else {
50      _wp_tpl_parsemenufile($data, $menufilename, $start);
51    }
52  }
53  $data2 = array();
54  if(!$conf['tpl'][$tpl]['usemenufile'] or ($conf['tpl'][$tpl]['usemenufile'] and !$ff)) {
55    search($data,$conf['datadir'],'search_universal',$opts);
56    $i = 0;
57    $cleanindexlist = array();
58    if($conf['tpl'][$tpl]['cleanindexlist']) {
59       $cleanindexlist = explode(',', $conf['tpl'][$tpl]['cleanindexlist']);
60       $i = 0;
61       foreach($cleanindexlist as $tmpitem) {
62         $cleanindexlist[$i] = trim($tmpitem);
63         $i++;
64       }
65    }
66    $first = true;
67    $i = 0;
68    $countroot = 0;
69    foreach($data as $item) {
70      if($conf['tpl'][$tpl]['hiderootlinks']) {
71        $item2 = array();
72        if($item['type'] == 'f' and !$item['ns'] and $item['id']) {
73          if($first) {
74            $item2['id'] = $start;
75            $item2['ns'] = 'root';
76            $item2['perm'] = 8;
77            $item2['type'] = 'd';
78            $item2['level'] = 1;
79            $item2['open'] = 1;
80            $item2['title'] = $conf['tpl'][$tpl]['rootmenutext'];
81            $data2[$i] = $item2;
82            $i++;
83            $first = false;
84          }
85          $item['ns'] = 'root';
86          $item['level'] = 2;
87          $countroot++;
88        }
89      }
90      if($conf['tpl'][$tpl]['cleanindex']) {
91        if(strpos($item['id'],'playground') !== false
92           or strpos($item['id'], 'wiki') !== false) {
93          continue;
94        }
95        if(count($cleanindexlist)) {
96          if(strpos($item['id'], ':')) {
97            list($tmpitem) = explode(':',$item['id']);
98          } else {
99            $tmpitem = $item['id'];
100          }
101          if(in_array($tmpitem, $cleanindexlist)) {
102            continue;
103          }
104        }
105      }
106      if($item['id'] == $menufilename) {
107        continue;
108      }
109      if($item['id'] == $start or preg_match('/:'.$start.'$/',$item['id'])
110         or preg_match('/(\w+):\1$/',$item['id'])) {
111        continue;
112      }
113      $data2[$i] = $item;
114      $i++;
115    }
116    if($countroot) {
117      $pos = $i - $countroot + 2;
118      $tmparr = array_splice($data2,$pos);
119      $data2 = array_merge($tmparr, $data2);
120    }
121//    ksort($data2);
122  } else {
123    $data2 = $data;
124  }
125  // print_r($data2);
126  echo html_buildlist($data2,'idx','_wp_tpl_list_index','_wp_tpl_html_li_index');
127}
128
129/* Index item formatter
130 * Callback function for html_buildlist()
131*/
132function _wp_tpl_list_index($item){
133  global $ID;
134  global $conf;
135  $ret = '';
136  if($item['type']=='d'){
137    if(@file_exists(wikiFN($item['id'].':'.$conf['start']))) {
138      $ret .= html_wikilink($item['id'].':'.$conf['start'], $item['title']);
139    } elseif(@file_exists(wikiFN($item['id'].':'.$item['id']))) {
140      $ret .= html_wikilink($item['id'].':'.$item['id'], $item['title']);
141    } else {
142      $ret .= html_wikilink($item['id'].':', $item['title']);
143    }
144  } else {
145    $ret .= html_wikilink(':'.$item['id'], $item['title']);
146  }
147  return $ret;
148}
149
150/**
151 * Index List item
152 *
153 * This user function is used in html_buildlist to build the
154 * <li> tags for namespaces when displaying the page index
155 * it gives different classes to opened or closed "folders"
156 *
157 * @author Andreas Gohr <andi@splitbrain.org>
158 *
159 * @param array $item
160 * @return string html
161 */
162function _wp_tpl_html_li_index($item){
163  global $INFO;
164
165  $class = '';
166  $id = '';
167
168  if($item['type'] == "f"){
169    // scroll to the current item
170    return '<li class="level'.$item['level'].$class.'" '.$id.'>';
171  } elseif($item['open']) {
172    return '<li class="open">';
173  } else {
174    return '<li class="closed">';
175  }
176}
177
178
179function _wp_tpl_parsemenufile(&$data, $filename, $start) {
180  $ret = TRUE;
181  $filepath = wikiFN($filename);
182  if(file_exists($filepath)) {
183    $lines = file($filepath);
184    $i = 0;
185    $lines2 = array();
186// read only lines formatted as wiki lists
187    foreach($lines as $line) {
188      if(preg_match('/^\s+\*/', $line)) {
189        $lines2[] = $line;
190      }
191      $i++;
192    }
193    $numlines = count($lines2);
194    $oldlevel = 0;
195// Array is read back to forth so pages with children can be found easier
196// you do not have to go back in the array if a child entry is found
197    for($i = $numlines - 1;$i >= 0; $i--) {
198      if(!$lines2[$i]) {
199        continue;
200      }
201      $tmparr = explode('*', $lines2[$i]);
202      $level = intval(strlen($tmparr[0]) / 2);
203      if($level > 3) {
204        $level = 3;
205      }
206// ignore lines without links
207      if(!preg_match('/\s*\[\[[^\]]+\]\]/', $tmparr[1])) {
208        continue;
209      }
210      $tmparr[1] = str_replace(array(']','['),'',trim($tmparr[1]));
211      list($id, $title) = explode('|', $tmparr[1]);
212// ignore links to non-existing pages
213      if(!file_exists(wikiFN($id))) {
214        continue;
215      }
216      if(!$title) {
217        $title = p_get_first_heading($id);
218      }
219      $data[$i]['id'] = $id;
220      $data[$i]['ns'] = '';
221      $data[$i]['perm'] = 8;
222      $data[$i]['type'] = 'f';
223      $data[$i]['level'] = $level;
224      $data[$i]['open'] = 1;
225      $data[$i]['title'] = $title;
226// namespaces must be tagged correctly (type = 'd') or they will not be found by the
227// html_wikilink function : means that they will marked as having subpages
228// even if there is no submenu
229      if(strpos($id,':') !== FALSE) {
230        $nsarray = explode(':', $id);
231        $pid = array_pop($nsarray);
232        $nspace = implode(':',$nsarray);
233        if($pid == $start) {
234          $data[$i]['id'] = $nspace;
235          $data[$i]['type'] = 'd';
236        } else {
237          $data[$i]['ns'] = $nspace;
238        }
239      }
240      if($oldlevel > $level) {
241        $data[$i]['type'] = 'd';
242      }
243      $oldlevel = $level;
244    }
245  } else {
246    $ret = FALSE;
247  }
248  ksort($data);
249  return $ret;
250}
251
252# wallpaper modified version of pageinfo
253function _wp_tpl_pageinfo(){
254  global $conf;
255  global $lang;
256  global $INFO;
257  global $REV;
258  global $ID;
259
260  // return if we are not allowed to view the page
261  if (!auth_quickaclcheck($ID)) { return; }
262
263  // prepare date
264  $date = dformat($INFO['lastmod']);
265
266  // echo it
267  if($INFO['exists']) {
268    echo '<span class="resp">', $lang['lastmod'], '</span> ', $date;
269    if($_SERVER['REMOTE_USER']) {
270      if($INFO['editor']) {
271        echo ' ',$lang['by'],' ', $INFO['editor'];
272      } else {
273        echo ' (',$lang['external_edit'],')';
274      }
275      if($INFO['locked']){
276        echo ' &middot; ', $lang['lockedby'], ': ', $INFO['locked'];
277      }
278    }
279    return true;
280  }
281  return false;
282}
283
284function _wp_tpl_bgimage() {
285/*
286 * Search for individual background images for pages or namespace
287 */
288
289  global $ID;
290  $nsarr = explode(':',$ID);
291  $i = count($nsarr);
292  for($j = $i; $j > 0; $j--) {
293    $ptarr = array_slice($nsarr, 0, $j);
294    if($j == $i) {
295      $sep = '';
296    } else {
297      $sep = '/'.$nsarr[($j-1)];
298    }
299    $pn = implode('/',$ptarr).$sep.'_bg.jpg';
300    if(file_exists(DOKU_INC.'data/media/'.$pn)) {
301      return ml(str_replace('/', ':', $pn));
302    }
303  }
304  return DOKU_TPL.'images/bg.jpg';
305}
306
307/*
308 * Hierarchical breadcrumbs
309 *
310 * This code was suggested as replacement for the usual breadcrumbs.
311 * It only makes sense with a deep site structure.
312 *
313 * @author Andreas Gohr <andi@splitbrain.org>
314 * @author Nigel McNie <oracle.shinoda@gmail.com>
315 * @author Sean Coates <sean@caedmon.net>
316 * @author <fredrik@averpil.com>
317 * @todo   May behave strangely in RTL languages
318 * @param string $sep Separator between entries
319 * @return bool
320 */
321function _wp_tpl_youarehere($sep = ' » ') {
322  global $conf;
323  global $ID;
324  global $lang;
325
326  $lspace = $_SESSION[DOKU_COOKIE]['translationlc'];
327
328  // check if enabled
329  if(!$conf['youarehere']) return false;
330
331  $parts = explode(':', $ID);
332  $count = count($parts);
333
334  echo '<span class="bchead">',$lang['youarehere'],' </span>';
335
336  // always print the startpage
337  if(!$lspace) tpl_pagelink(':'.$conf['start']);
338
339  // print intermediate namespace links
340  $part = '';
341  for($i = 0; $i < $count - 1; $i++) {
342    $part .= $parts[$i].':';
343    $page = $part;
344    if($page == $conf['start']) continue; // Skip startpage
345
346    // output
347    echo $sep;
348    tpl_pagelink($page);
349  }
350
351  // print current page, skipping start page, skipping for namespace index
352  resolve_pageid('', $page, $exists);
353  if(isset($page) && $page == $part.$parts[$i]) return true;
354  $page = $part.$parts[$i];
355  if($page == $conf['start']) return true;
356  echo $sep;
357  tpl_pagelink($page);
358  return true;
359}
360?>
361