1<?php
2/**
3 * Display page index
4 *
5 * @author Andreas Gohr <andi@splitbrain.org>
6 * @author Otto Vainio <plugins@valjakko.net>
7 */
8
9
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'../../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',realpath(dirname(__FILE__).'../').'/');
12if(!defined('NL')) define('NL',"\n");
13
14require_once(DOKU_INC.'inc/html.php');
15require_once(DOKU_INC.'inc/search.php');
16
17$thislang=array();
18setupLocale();
19
20function getPluginName() {
21    $basename = preg_replace( '/^.+[\\\\\\/]/', '', dirname(__FILE__));
22    return $basename;
23}
24
25function myhtml_index(){
26  global $NS;
27  global $conf;
28  global $ID;
29  $dir = $conf['datadir'];
30  $ns  = cleanID($NS);
31  #fixme use appropriate function
32  if(empty($ns)){
33    $ns = dirname(str_replace(':','/',$ID));
34    if($ns == '.') $ns ='';
35  }
36  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
37
38  print p_locale_xhtml('index');
39
40  $data = array();
41  search($data,$conf['datadir'],'linkpage_search_index',array('ns' => $ns),$dir);
42  print html_buildlist($data,'idx','html_list_index','html_li_index');
43}
44
45function linkpage_filelist($ns,$auth=null,$jump=''){
46    global $conf;
47    global $lang;
48    global $thislang;
49    $ns = cleanID($ns);
50
51    // check auth our self if not given (needed for ajax calls)
52    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
53
54    echo '<h1 id="linkpage__ns">:'.hsc($ns).'</h1>'.NL;
55
56    if($auth < AUTH_READ){
57        // FIXME: print permission warning here instead?
58        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
59        return;
60    }
61
62
63    $dir = utf8_encodeFN(str_replace(':','/',$ns));
64    $data = array();
65    search($data,$conf['datadir'],'linkpage_search_index',array('ns' => $ns),$dir);
66
67    if(!count($data)){
68        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
69        return;
70    }
71    linkpage_newpage($ns);
72    echo $thislang['clicktosort'].NL;
73    echo '<table class="sortable" id="linkpage__sortable"><thead><tr>';
74    echo '<th>'.$thislang['id'].'</th><th>'.$thislang['title'].'</th>';
75    echo '</tr></thead><tbody>';
76    foreach($data as $item){
77        linkpage_printfile($item,$auth,$jump);
78    }
79    echo '</tbody></table>';
80}
81
82
83function linkpage_fileContent(){
84  global $AUTH;
85  global $NS;
86  global $JUMPTO;
87
88  ptln('<div id="linkpage__content">');
89  linkpage_filelist($NS,$AUTH,$JUMPTO);
90  ptln('</div>');
91}
92
93
94/**
95 * Formats and prints one file in the list
96 */
97function linkpage_printfile($item,$auth,$jump){
98    if ($item['type']=='d') return;
99    global $lang;
100    // Prepare zebra coloring
101    // I always wanted to use this variable name :-D
102    static $twibble = 1;
103    $twibble *= -1;
104    $zebra = ($twibble == -1) ? 'odd' : 'even';
105
106    // Automatically jump to recent action
107    if($jump == $item['id']) {
108        $jump = ' id="scroll__here" ';
109    }else{
110        $jump = '';
111    }
112
113    // Prepare fileicons
114    list($ext,$mime) = mimetype($item['file']);
115    $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
116    $class = 'select linkpagefile mf_'.$class;
117
118    // Prepare filename
119    $file = utf8_decodeFN($item['id']);
120    $title = $item['title'];
121    $pgsplt=explode(':',$item['id']);
122    $pagename = array_pop($pgsplt);
123    if (!$title) $title=$pagename;
124
125    // Prepare info
126    $info = '';
127
128    // output
129    echo '<tr><td>';
130    echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
131    echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$pagename.'</a>';
132    echo '</div></td>'.NL;
133    echo '<td>';
134    echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
135    echo $title;
136    echo '<div class="example" id="ex_'.$item['id'].'">';
137    echo $lang['mediausage'].' <code>[[:'.$item['id'].']]</code>';
138    echo '</div>';
139    echo '</div>';
140    echo '</td></tr>';
141}
142
143function linkpage_mediaTree(){
144  global $NS;
145
146  ptln('<div id="linkpage__tree">');
147  linkpage_nstree($NS);
148  ptln('</div>');
149}
150
151
152/**
153 * Build a tree outline of available media namespaces
154 *
155 * @author Andreas Gohr <andi@splitbrain.org>
156 * @author  Otto Vainio <plugins@valjakko.net>
157 */
158function linkpage_nstree($ns){
159    global $conf;
160    global $lang;
161
162    // currently selected namespace
163    $ns  = cleanID($ns);
164    if(empty($ns)){
165        $ns = dirname(str_replace(':','/',$ID));
166        if($ns == '.') $ns ='';
167    }
168    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
169
170    $data = array();
171    search($data,$conf['datadir'],'linkpage_search_index',array('ns' => $ns, 'nofiles' => true));
172
173    // wrap a list with the root level around the other namespaces
174    $item = array( 'level' => 0, 'id' => '',
175                   'open' =>'true', 'label' => '['.$lang['mediaroot'].']');
176
177    echo '<ul class="idx">';
178    echo linkpage_nstree_li($item);
179    echo linkpage_nstree_item($item);
180    echo html_buildlist($data,'idx','linkpage_nstree_item','linkpage_nstree_li');
181    echo '</li>';
182    echo '</ul>';
183}
184
185/**
186 * Userfunction for html_buildlist
187 *
188 * Prints a linkpage namespace tree item
189 *
190 * @author Andreas Gohr <andi@splitbrain.org>
191 * @author  Otto Vainio <plugins@valjakko.net>
192 */
193function linkpage_nstree_item($item){
194    $pos   = strrpos($item['id'], ':');
195    $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
196    if(!$item['label']) $item['label'] = $label;
197
198    $ret  = '';
199    $ret .= '<a href="'.DOKU_BASE.'lib/plugins/'.getPluginName().'/exe/filemanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
200    $ret .= $item['label'];
201    $ret .= '</a>';
202    return $ret;
203}
204
205/**
206 * Userfunction for html_buildlist
207 *
208 * Prints a linkpage namespace tree item opener
209 *
210 * @author Andreas Gohr <andi@splitbrain.org>
211 * @author  Otto Vainio <plugins@valjakko.net>
212 */
213function linkpage_nstree_li($item){
214    $class='linkpage level'.$item['level'];
215    if($item['open']){
216        $class .= ' open';
217        $img   = DOKU_BASE.'lib/images/minus.gif';
218        $alt   = '&minus;';
219    }else{
220        $class .= ' closed';
221        $img   = DOKU_BASE.'lib/images/plus.gif';
222        $alt   = '+';
223    }
224    return '<li class="'.$class.'">'.
225           '<img src="'.$img.'" alt="'.$alt.'" />';
226}
227/**
228 * Build the browsable index of pages
229 *
230 * $opts['ns'] is the current namespace
231 *
232 * @author  Andreas Gohr <andi@splitbrain.org>
233 * @author  Otto Vainio <plugins@valjakko.net>
234 */
235function linkpage_search_index(&$data,$base,$file,$type,$lvl,$opts){
236  $return = true;
237
238  $item = array();
239  if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
240    //add but don't recurse
241    $return = false;
242  }elseif($type == 'f' && ($opts['nofiles'] || !preg_match('#\.txt$#',$file))){
243    //don't add
244    return false;
245  }
246
247  $id = pathID($file);
248  $title = p_get_metadata($id, 'title');
249
250  //check hidden
251  if(isHiddenPage($id)){
252    return false;
253  }
254
255  //check ACL
256  if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
257    return false;
258  }
259
260  $data[]=array( 'id'    => $id,
261                 'type'  => $type,
262                 'level' => $lvl,
263                 'title' => $title,
264                 'open'  => $return );
265  return $return;
266}
267
268
269/**
270 * Print the Add new page form
271 *
272 */
273function linkpage_newpage($ns){
274    global $lang;
275    global $thislang;
276    global $conf;
277    $sepa='0';
278    if ($conf['plugin']['linkmanager']['usetitleseparator']) {$sepa='1';}
279    ?>
280    <form action="" method="post">
281        <input type="hidden" name="newpage__ns" id="newpage__ns" value="<?php echo hsc($ns)?>" />
282        <input type="hidden" name="conf__use_sepa" id="conf__use_sepa" value="<?php echo $sepa ?>" />
283        <p>
284          <label for="newpage__name"><?php echo $thislang['txt_newpage']?>:</label>
285          <span class="nowrap">
286          <input type="text" name="newpage__name" class="newpage__name" id="newpage__name" />
287          <a name="newpage" class="newpage__submit">submit</a>
288          </span>
289        </p>
290    </form>
291    <?php
292}
293
294/**
295 *  setupLocale()
296 *  reads all the plugins language dependent strings into $this->lang
297 *  this function is automatically called by getLang()
298 */
299function setupLocale() {
300  if ($localised) return;
301  global $thislang;
302
303  global $conf;            // definitely don't invoke "global $lang"
304  $path = DOKU_PLUGIN.getPluginName().'/lang/';
305//  echo "polku:$path";
306
307  $lang = array();
308
309  // don't include once, in case several plugin components require the same language file
310  @include($path.'en/lang.php');
311  if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php');
312
313  $thislang = $lang;
314  $localised = true;
315}
316?>
317