1<?php
2if (!defined('DOKU_INC')) die();
3
4// parse url defined in menu
5function menulink($id) {
6    if(strpos($id, 'http') === 0) {
7        return $id;
8    } elseif(strpos($id, 'www.') === 0) {
9        return $id;
10    }
11    if(strpos($id, "#")){
12        $anchor = substr($id, strpos($id, "#"));
13        $id = substr($id, 0, strpos($id, "#"));
14    } else {
15        $anchor = "";
16    }
17    if(strpos($id, "?")){
18        parse_str(substr($id, strpos($id, "?")+1), $urlParameters);
19        $id = substr($id, 0, strpos($id, "?"));
20    } else {
21        $urlParameters = '';
22    }
23    return wl($id, $urlParameters) . $anchor;
24}
25
26// generate menu-array either from json or from index
27function get_menu() {
28    global $conf;
29
30    $defaultmenu = json_decode('
31    {
32    "level1": {":wiki": "Start", ":error": "ERROR: Could not parse JSON menufile"},
33    "level2": {
34        ":wiki": {":wiki:welcome" : "Wiki", ":playground" : "Playground"}
35    },
36    "level3": {
37        ":wiki:welcome": {":wiki:welcome": "Welcome", ":wiki:dokuwiki": "DokuWiki", ":wiki:syntax": "Syntax"}
38    }
39    }
40    ', true);
41    $menu = $defaultmenu;
42
43    if(tpl_getConf('menu') == 'index') {
44        $menu = array("level1" => array(), "level2" => array(), "level3" => array());
45        $data = idx_get_indexer()->getPages();
46		$cleanindexlist = array_merge(
47                            explode(',', tpl_getConf('cleanindexlist')),
48                            array('sidebar')
49                        );
50        foreach($data as $id){
51            if(isHiddenPage($id)) continue;
52            if(auth_quickaclcheck($id) < AUTH_READ) continue;
53			$path = '';
54			if(in_array($id, $cleanindexlist)) continue;
55			foreach(explode(':', $id) as $lvl => $ns) {
56			    if(in_array($ns, $cleanindexlist)) continue;
57				$pathpre = $path;
58				$path .= ($lvl>0) ? ':' . $ns : $ns;
59            	$title = p_get_first_heading($path);
60            	$title = is_null($title) ? $ns : $title;
61				if($lvl == 0 && !array_key_exists($path, $menu['level1'])) $menu['level1'][$path] = $title;
62				if($lvl > 0) {
63					if(is_array($menu['level'. ($lvl+1)][$pathpre])) {
64						$menu['level'.($lvl+1)][$pathpre][$path] = $title;
65					} else {
66						$menu['level'.($lvl+1)][$pathpre] = array($path => $title);
67					}
68				}
69			}
70        }
71        return $menu;
72    }
73    $menufile = tpl_getConf('menusite');
74    if(@file_exists(wikiFN($menufile)) && auth_quickaclcheck($menufile) >= AUTH_READ) {
75        $content = utf8_encode(io_readWikiPage(wikiFN($menufile), $menufile));
76        preg_match('/<MENU>(.*?)<\/MENU>/s', $content, $menu);
77        $menu = ((count($menu) > 0)) ? json_decode($menu[1], true) : $defaultmenu;
78        if(is_null($menu)) $menu = $defaultmenu;
79    }
80    return $menu;
81}
82
83//render drop-down html menu from menu-array
84function dropdown_menu() {
85    global $ID;
86$menu = get_menu();
87echo '<ul class="navigation-l1"> ';
88foreach($menu['level1'] as $url => $title){
89    $active = (strpos(trim($ID,':'), trim($url,':')) === 0) ? "active": "";
90    if(array_key_exists($url, $menu['level2'])){
91    	echo '<li class="flyout ', $active,'">
92              <a href="'.menulink($url).'">', $title, '</a>
93    	      <div class="dropdown">
94    		  <ul class="navigation-l2">';
95    	foreach($menu['level2'][$url] as $url2 => $title2) {
96    		if(array_key_exists($url2, $menu['level3'])){
97                echo '<li class="has-submenu">
98                      <a href="', menulink($url2), '">', $title2, '</a>
99                      <div class="submenu">
100                      <ul class="navigation-l3">
101                      ';
102                foreach($menu['level3'][$url2] as $url3 => $title3) {
103    			    echo '<li class=""><a href="', menulink($url3), '">', $title3, '</a></li>';
104                }
105                echo '</ul></div></li>';
106    		} else {
107                echo '<li class=""><a href="', menulink($url2), '">', $title2, '</a></li>';
108    		}
109    	}
110    	echo '</ul></div></li>';
111    } else {
112    	echo '<li class="',$active,'"><a href="', menulink($url), '">', $title, '</a></li>';
113    }
114}
115if($_SERVER['REMOTE_USER'] && tpl_getConf('menu') == 'file' && (auth_quickaclcheck(tpl_getConf('menusite')) >= AUTH_EDIT)) {
116    	echo '<li class=""><small><a href="',wl(tpl_getConf('menusite'), array('do'=>'edit')), '">Edit</a></small></li>';
117}
118echo '</ul>';
119}
120
121//render breadcrump-like bar from menu-array
122function trace() {
123    global $ID, $conf;
124    $trace = explode(':', $ID);
125    $num = count($trace);
126    if($num == 1 && $trace[0] == $conf['start']) return ;
127    $menu = get_menu();
128    echo '
129    <div class="list">
130        <a href="', wl($conf['start']),'">', tpl_getConf("institute_".$conf['lang']), '</a>
131        <ul>';
132        foreach($menu['level1'] as $url => $title) {
133            echo '<li><a href="', wl($url) ,'">', ucfirst($title) ,'</a></li>';
134        }
135        echo '</ul>
136    </div>';
137    $path = '';
138    foreach($trace as $n => $ns) {
139    	$path .= ($n>0) ? ':' . $ns : $ns;
140    	$title = p_get_first_heading($path);
141    	$title = is_null($title) ? $ns : $title;
142        if($n == $num-1) {
143            echo '<div class="list last">';
144        } else {
145            echo '<div class="list">';
146        }
147        echo '<a href="', wl($path) ,'">', ucfirst($title) ,'</a>';
148        if(array_key_exists($path, $menu['level'.($n+2)])){
149            echo '<ul>';
150            foreach($menu['level'.($n+2)][$path] as $u => $t){
151                echo '<li><a href="', wl($u) ,'">', ucfirst($t) ,'</a></li>';
152            }
153            echo '</ul>';
154        }
155        echo '</div>';
156    }
157}
158
159?>
160