xref: /dokuwiki/inc/search.php (revision 42ea7f447f39fbc2f79eaaec31f8c10ede59c5d0)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * DokuWiki search functions
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
8f3f0262cSandi
9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
10f3f0262cSandi
11f3f0262cSandi/**
12ce4301e3SGerrit Uitslag * Recurse directory
1315fae107Sandi *
14f3f0262cSandi * This function recurses into a given base directory
15f3f0262cSandi * and calls the supplied function for each file and directory
1615fae107Sandi *
1724998b31SGerrit Uitslag * @param   array    &$data The results of the search are stored here
1824baa045SAndreas Gohr * @param   string    $base Where to start the search
19fe82d751SChristopher Smith * @param   callback  $func Callback (function name or array with object,method)
2024998b31SGerrit Uitslag * @param   array     $opts option array will be given to the Callback
2124baa045SAndreas Gohr * @param   string    $dir  Current directory beyond $base
2224baa045SAndreas Gohr * @param   int       $lvl  Recursion Level
23ec24a2dfSPhilipp A. Hartmann * @param   mixed     $sort 'natural' to use natural order sorting (default); 'date' to sort by filemtime; leave empty to skip sorting.
2415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
25f3f0262cSandi */
26155e63c9SChristopher Smithfunction search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
27f3f0262cSandi    $dirs   = array();
28f3f0262cSandi    $files  = array();
29abc306f4SKate Arzamastseva    $filepaths = array();
30f3f0262cSandi
31f3f0262cSandi    //read in directories and files
32f3f0262cSandi    $dh = @opendir($base.'/'.$dir);
33f3f0262cSandi    if(!$dh) return;
34f3f0262cSandi    while(($file = readdir($dh)) !== false){
35de3dfc91Sandi        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
36f3f0262cSandi        if(is_dir($base.'/'.$dir.'/'.$file)){
37f3f0262cSandi            $dirs[] = $dir.'/'.$file;
38f3f0262cSandi            continue;
39f3f0262cSandi        }
40f3f0262cSandi        $files[] = $dir.'/'.$file;
41abc306f4SKate Arzamastseva        $filepaths[] = $base.'/'.$dir.'/'.$file;
42f3f0262cSandi    }
43f3f0262cSandi    closedir($dh);
44ec24a2dfSPhilipp A. Hartmann    if (!empty($sort)) {
45abc306f4SKate Arzamastseva        if ($sort == 'date') {
46d971ea8bSKate Arzamastseva            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
471dc5d48bSChristopher Smith        } else /* natural */ {
481dc5d48bSChristopher Smith            natsort($files);
49abc306f4SKate Arzamastseva        }
501dc5d48bSChristopher Smith        natsort($dirs);
51ec24a2dfSPhilipp A. Hartmann    }
52f3f0262cSandi
53f3f0262cSandi    //give directories to userfunction then recurse
54f3f0262cSandi    foreach($dirs as $dir){
55d8126df2SGina Haeussge        if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
565514a5a7SChristopher Smith            search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
57f3f0262cSandi        }
58f3f0262cSandi    }
59f3f0262cSandi    //now handle the files
60f3f0262cSandi    foreach($files as $file){
61d8126df2SGina Haeussge        call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
62f3f0262cSandi    }
63f3f0262cSandi}
64f3f0262cSandi
65f3f0262cSandi/**
66f3f0262cSandi * The following functions are userfunctions to use with the search
67f3f0262cSandi * function above. This function is called for every found file or
68f3f0262cSandi * directory. When a directory is given to the function it has to
69f3f0262cSandi * decide if this directory should be traversed (true) or not (false)
70f3f0262cSandi * The function has to accept the following parameters:
71f3f0262cSandi *
72ce4301e3SGerrit Uitslag * array &$data  - Reference to the result data structure
73ce4301e3SGerrit Uitslag * string $base  - Base usually $conf['datadir']
74ce4301e3SGerrit Uitslag * string $file  - current file or directory relative to $base
75ce4301e3SGerrit Uitslag * string $type  - Type either 'd' for directory or 'f' for file
76ce4301e3SGerrit Uitslag * int    $lvl   - Current recursion depht
77ce4301e3SGerrit Uitslag * array  $opts  - option array as given to search()
78f3f0262cSandi *
79f3f0262cSandi * return values for files are ignored
80f3f0262cSandi *
81f3f0262cSandi * All functions should check the ACL for document READ rights
82783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
83783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable
840e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example
850e1a261eSMichael Klier * in case of lockfiles).
86f3f0262cSandi */
87f3f0262cSandi
88f3f0262cSandi/**
8963f2400bSandi * Searches for pages beginning with the given query
9063f2400bSandi *
9163f2400bSandi * @author Andreas Gohr <andi@splitbrain.org>
9263f2400bSandi */
9363f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
948705cc81SAndreas Gohr    $opts = array(
958705cc81SAndreas Gohr            'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
968705cc81SAndreas Gohr            'listfiles' => true,
978705cc81SAndreas Gohr            'pagesonly' => true,
988705cc81SAndreas Gohr            );
998705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
10063f2400bSandi}
10163f2400bSandi
10263f2400bSandi/**
10315fae107Sandi * Build the browsable index of pages
104f3f0262cSandi *
105783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace
10615fae107Sandi *
10715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
108f3f0262cSandi */
109f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){
110d1c7b6ecSAndreas Gohr    global $conf;
111783d2e49SAdrian Lang    $opts = array(
112783d2e49SAdrian Lang        'pagesonly' => true,
113783d2e49SAdrian Lang        'listdirs' => true,
114443e135dSChristopher Smith        'listfiles' => empty($opts['nofiles']),
115783d2e49SAdrian Lang        'sneakyacl' => $conf['sneaky_index'],
116783d2e49SAdrian Lang        // Hacky, should rather use recmatch
1171c6c1c6cSMichael Hamann        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1
118783d2e49SAdrian Lang    );
119f3f0262cSandi
120783d2e49SAdrian Lang    return search_universal($data, $base, $file, $type, $lvl, $opts);
121f3f0262cSandi}
122f3f0262cSandi
123f3f0262cSandi/**
12415fae107Sandi * List all namespaces
12515fae107Sandi *
12615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
127f3f0262cSandi */
128f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
1298705cc81SAndreas Gohr    $opts = array(
1308705cc81SAndreas Gohr            'listdirs' => true,
1318705cc81SAndreas Gohr            );
1328705cc81SAndreas Gohr    return search_universal($data,$base,$file,$type,$lvl,$opts);
133f3f0262cSandi}
134f3f0262cSandi
135f3f0262cSandi/**
13615fae107Sandi * List all mediafiles in a namespace
137*42ea7f44SGerrit Uitslag *   $opts['depth']     recursion level, 0 for all
138*42ea7f44SGerrit Uitslag *   $opts['showmsg']   shows message if invalid media id is used
139*42ea7f44SGerrit Uitslag *   $opts['skipacl']   skip acl checking
140*42ea7f44SGerrit Uitslag *   $opts['pattern']   check given pattern
141*42ea7f44SGerrit Uitslag *   $opts['hash']      add hashes to result list
14215fae107Sandi *
14315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
144f3f0262cSandi */
145f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){
146b8219d2dSAndreas Gohr
147f3f0262cSandi    //we do nothing with directories
1481a49ac65SGina Haeussge    if($type == 'd') {
1490e80bb5eSChristopher Smith        if(empty($opts['depth'])) return true; // recurse forever
15078315408SAndreas Gohr        $depth = substr_count($file,'/');
151b8219d2dSAndreas Gohr        if($depth >= $opts['depth']) return false; // depth reached
152224122cfSAndreas Gohr        return true;
1531a49ac65SGina Haeussge    }
154f3f0262cSandi
155f3f0262cSandi    $info         = array();
156156a608cSandi    $info['id']   = pathID($file,true);
15764807c84SAndreas Gohr    if($info['id'] != cleanID($info['id'])){
15864807c84SAndreas Gohr        if($opts['showmsg'])
15964807c84SAndreas Gohr            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
16064807c84SAndreas Gohr        return false; // skip non-valid files
16164807c84SAndreas Gohr    }
162f3f0262cSandi
163f3f0262cSandi    //check ACL for namespace (we have no ACL for mediafiles)
164224122cfSAndreas Gohr    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
1650e80bb5eSChristopher Smith    if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ){
166224122cfSAndreas Gohr        return false;
167224122cfSAndreas Gohr    }
168224122cfSAndreas Gohr
169224122cfSAndreas Gohr    //check pattern filter
1700e80bb5eSChristopher Smith    if(!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])){
171f3f0262cSandi        return false;
172f3f0262cSandi    }
173f3f0262cSandi
1743009a773SAndreas Gohr    $info['file']     = utf8_basename($file);
175f3f0262cSandi    $info['size']     = filesize($base.'/'.$file);
1765e7fa82eSAndreas Gohr    $info['mtime']    = filemtime($base.'/'.$file);
1773df72098SAndreas Gohr    $info['writable'] = is_writable($base.'/'.$file);
178f3f0262cSandi    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
179f3f0262cSandi        $info['isimg'] = true;
18023a34783SAndreas Gohr        $info['meta']  = new JpegMeta($base.'/'.$file);
181f3f0262cSandi    }else{
182f3f0262cSandi        $info['isimg'] = false;
183f3f0262cSandi    }
1840e80bb5eSChristopher Smith    if(!empty($opts['hash'])){
185dfd343c4SAndreas Gohr        $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
186224122cfSAndreas Gohr    }
187224122cfSAndreas Gohr
188f3f0262cSandi    $data[] = $info;
189f3f0262cSandi
190f3f0262cSandi    return false;
191f3f0262cSandi}
192f3f0262cSandi
193f3f0262cSandi/**
194f3f0262cSandi * This function just lists documents (for RSS namespace export)
19515fae107Sandi *
19615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
197f3f0262cSandi */
198f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){
199f3f0262cSandi    //we do nothing with directories
200f3f0262cSandi    if($type == 'd') return false;
2010e1a261eSMichael Klier    //only search txt files
2020e1a261eSMichael Klier    if(substr($file,-4) == '.txt'){
203f3f0262cSandi        //check ACL
204f3f0262cSandi        $id = pathID($file);
205f3f0262cSandi        if(auth_quickaclcheck($id) < AUTH_READ){
206f3f0262cSandi            return false;
207f3f0262cSandi        }
2080e1a261eSMichael Klier        $data[]['id'] = $id;
209f3f0262cSandi    }
210f3f0262cSandi    return false;
211f3f0262cSandi}
212f3f0262cSandi
213f3f0262cSandi/**
214f3f0262cSandi * Quicksearch for searching matching pagenames
215f3f0262cSandi *
216f3f0262cSandi * $opts['query'] is the search query
21715fae107Sandi *
21815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
219f3f0262cSandi */
220f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){
221f3f0262cSandi    //we do nothing with directories
222f3f0262cSandi    if($type == 'd') return true;
223f3f0262cSandi    //only search txt files
2240e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
225f3f0262cSandi
226f3f0262cSandi    //simple stringmatching
227396b7edbSmatthiasgrimm    if (!empty($opts['query'])){
228f3f0262cSandi        if(strpos($file,$opts['query']) !== false){
229f3f0262cSandi            //check ACL
230f3f0262cSandi            $id = pathID($file);
231f3f0262cSandi            if(auth_quickaclcheck($id) < AUTH_READ){
232f3f0262cSandi                return false;
233f3f0262cSandi            }
234f3f0262cSandi            $data[]['id'] = $id;
235f3f0262cSandi        }
236396b7edbSmatthiasgrimm    }
237f3f0262cSandi    return true;
238f3f0262cSandi}
239f3f0262cSandi
240f3f0262cSandi/**
24158b6f612SAndreas Gohr * Just lists all documents
24258b6f612SAndreas Gohr *
2431fcfad4dSAndreas Gohr * $opts['depth']   recursion level, 0 for all
2441fcfad4dSAndreas Gohr * $opts['hash']    do md5 sum of content?
245224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL
2461fcfad4dSAndreas Gohr *
24758b6f612SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
24858b6f612SAndreas Gohr */
24958b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){
2508451f4adSGuillaume Turri    if(isset($opts['depth']) && $opts['depth']){
251c647387eSGuillaume Turri        $parts = explode('/',ltrim($file,'/'));
2525737a81eSMichael Hamann        if(($type == 'd' && count($parts) >= $opts['depth'])
2535737a81eSMichael Hamann          || ($type != 'd' && count($parts) > $opts['depth'])){
254c647387eSGuillaume Turri            return false; // depth reached
255c647387eSGuillaume Turri        }
256c647387eSGuillaume Turri    }
257c647387eSGuillaume Turri
25858b6f612SAndreas Gohr    //we do nothing with directories
2591fcfad4dSAndreas Gohr    if($type == 'd'){
2601fcfad4dSAndreas Gohr        return true;
2611fcfad4dSAndreas Gohr    }
2621fcfad4dSAndreas Gohr
26358b6f612SAndreas Gohr    //only search txt files
2640e1a261eSMichael Klier    if(substr($file,-4) != '.txt') return true;
26558b6f612SAndreas Gohr
26659bc3b48SGerrit Uitslag    $item = array();
2671fcfad4dSAndreas Gohr    $item['id']   = pathID($file);
268061df79cSAndreas Gohr    if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
2691fcfad4dSAndreas Gohr        return false;
2701fcfad4dSAndreas Gohr    }
2711fcfad4dSAndreas Gohr
2721fcfad4dSAndreas Gohr    $item['rev']   = filemtime($base.'/'.$file);
273224122cfSAndreas Gohr    $item['mtime'] = $item['rev'];
2741fcfad4dSAndreas Gohr    $item['size']  = filesize($base.'/'.$file);
2751fcfad4dSAndreas Gohr    if($opts['hash']){
2761fcfad4dSAndreas Gohr        $item['hash'] = md5(trim(rawWiki($item['id'])));
2771fcfad4dSAndreas Gohr    }
2781fcfad4dSAndreas Gohr
2791fcfad4dSAndreas Gohr    $data[] = $item;
28058b6f612SAndreas Gohr    return true;
28158b6f612SAndreas Gohr}
28258b6f612SAndreas Gohr
283b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */
284b59a406bSmatthiasgrimm
285b59a406bSmatthiasgrimm/**
28615fae107Sandi * fulltext sort
28715fae107Sandi *
288f3f0262cSandi * Callback sort function for use with usort to sort the data
289f3f0262cSandi * structure created by search_fulltext. Sorts descending by count
29015fae107Sandi *
29115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
292f3f0262cSandi */
293f3f0262cSandifunction sort_search_fulltext($a,$b){
294f3f0262cSandi    if($a['count'] > $b['count']){
295f3f0262cSandi        return -1;
296f3f0262cSandi    }elseif($a['count'] < $b['count']){
297f3f0262cSandi        return 1;
298f3f0262cSandi    }else{
299f3f0262cSandi        return strcmp($a['id'],$b['id']);
300f3f0262cSandi    }
301f3f0262cSandi}
302f3f0262cSandi
303f3f0262cSandi/**
304f3f0262cSandi * translates a document path to an ID
30515fae107Sandi *
30615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
30737e34a5eSandi * @todo    move to pageutils
308f3f0262cSandi */
309156a608cSandifunction pathID($path,$keeptxt=false){
31049c713a3Sandi    $id = utf8_decodeFN($path);
31149c713a3Sandi    $id = str_replace('/',':',$id);
312156a608cSandi    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
313709b1063SAdrian Lang    $id = trim($id, ':');
314f3f0262cSandi    return $id;
315f3f0262cSandi}
316f3f0262cSandi
317340756e4Sandi
3183abeade3SAndreas Gohr/**
3193abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing
3203abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex
3213abeade3SAndreas Gohr * setup.
3223abeade3SAndreas Gohr *
3233abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts
3243abeade3SAndreas Gohr * array, where the following settings can be used.
3253abeade3SAndreas Gohr *
326e14fe973SGerrit Uitslag * depth      int     recursion depth. 0 for unlimited                       (default: 0)
327e14fe973SGerrit Uitslag * keeptxt    bool    keep .txt extension for IDs                            (default: false)
328e14fe973SGerrit Uitslag * listfiles  bool    include files in listing                               (default: false)
329e14fe973SGerrit Uitslag * listdirs   bool    include namespaces in listing                          (default: false)
330e14fe973SGerrit Uitslag * pagesonly  bool    restrict files to pages                                (default: false)
331e14fe973SGerrit Uitslag * skipacl    bool    do not check for READ permission                       (default: false)
332e14fe973SGerrit Uitslag * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
333e14fe973SGerrit Uitslag * hash       bool    create MD5 hash for files                              (default: false)
334e14fe973SGerrit Uitslag * meta       bool    return file metadata                                   (default: false)
335e14fe973SGerrit Uitslag * filematch  string  match files against this regexp                        (default: '', so accept everything)
336e14fe973SGerrit Uitslag * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
337e14fe973SGerrit Uitslag * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
338e14fe973SGerrit Uitslag * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
339e14fe973SGerrit Uitslag * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
340e14fe973SGerrit Uitslag * showmsg    bool    warn about non-ID files                                (default: false)
341e14fe973SGerrit Uitslag * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
342e14fe973SGerrit Uitslag * firsthead  bool    return first heading for pages                         (default: false)
3433abeade3SAndreas Gohr *
344ce4301e3SGerrit Uitslag * @param array &$data  - Reference to the result data structure
345ce4301e3SGerrit Uitslag * @param string $base  - Base usually $conf['datadir']
346ce4301e3SGerrit Uitslag * @param string $file  - current file or directory relative to $base
347ce4301e3SGerrit Uitslag * @param string $type  - Type either 'd' for directory or 'f' for file
348ce4301e3SGerrit Uitslag * @param int    $lvl   - Current recursion depht
349ce4301e3SGerrit Uitslag * @param array  $opts  - option array as given to search()
350ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false)
351ce4301e3SGerrit Uitslag *              return value is ignored for files
352ce4301e3SGerrit Uitslag *
3533abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
3543abeade3SAndreas Gohr */
3553abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){
3563abeade3SAndreas Gohr    $item   = array();
3573abeade3SAndreas Gohr    $return = true;
3583abeade3SAndreas Gohr
3593abeade3SAndreas Gohr    // get ID and check if it is a valid one
360b7a3421aSChristopher Smith    $item['id'] = pathID($file,($type == 'd' || !empty($opts['keeptxt'])));
3618537abd1SAdrian Lang    if($item['id'] != cleanID($item['id'])){
36249f299d6SChristopher Smith        if(!empty($opts['showmsg'])){
3638537abd1SAdrian Lang            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
364b7a3421aSChristopher Smith        }
3653abeade3SAndreas Gohr        return false; // skip non-valid files
3663abeade3SAndreas Gohr    }
3678705cc81SAndreas Gohr    $item['ns']  = getNS($item['id']);
3683abeade3SAndreas Gohr
3693abeade3SAndreas Gohr    if($type == 'd') {
3703abeade3SAndreas Gohr        // decide if to recursion into this directory is wanted
3710e80bb5eSChristopher Smith        if(empty($opts['depth'])){
3723abeade3SAndreas Gohr            $return = true; // recurse forever
3733abeade3SAndreas Gohr        }else{
3743abeade3SAndreas Gohr            $depth = substr_count($file,'/');
3753abeade3SAndreas Gohr            if($depth >= $opts['depth']){
3763abeade3SAndreas Gohr                $return = false; // depth reached
3773abeade3SAndreas Gohr            }else{
3783abeade3SAndreas Gohr                $return = true;
3793abeade3SAndreas Gohr            }
3803abeade3SAndreas Gohr        }
3819b4337c6SChristopher Smith
3829b4337c6SChristopher Smith        if ($return) {
3839b4337c6SChristopher Smith            $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/',$file);
3849b4337c6SChristopher Smith            if (!$match) {
3859b4337c6SChristopher Smith                return false; // doesn't match
3869b4337c6SChristopher Smith            }
3873abeade3SAndreas Gohr        }
3883abeade3SAndreas Gohr    }
3893abeade3SAndreas Gohr
3903abeade3SAndreas Gohr    // check ACL
391443e135dSChristopher Smith    if(empty($opts['skipacl'])){
3923abeade3SAndreas Gohr        if($type == 'd'){
3933abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id'].':*');
3943abeade3SAndreas Gohr        }else{
3953abeade3SAndreas Gohr            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
3963abeade3SAndreas Gohr        }
3973abeade3SAndreas Gohr    }else{
3983abeade3SAndreas Gohr        $item['perm'] = AUTH_DELETE;
3993abeade3SAndreas Gohr    }
4003abeade3SAndreas Gohr
4013abeade3SAndreas Gohr    // are we done here maybe?
4023abeade3SAndreas Gohr    if($type == 'd'){
403443e135dSChristopher Smith        if(empty($opts['listdirs'])) return $return;
404443e135dSChristopher Smith        if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false; //neither list nor recurse
405443e135dSChristopher Smith        if(!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
406443e135dSChristopher Smith        if(!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
4073abeade3SAndreas Gohr    }else{
408443e135dSChristopher Smith        if(empty($opts['listfiles'])) return $return;
409443e135dSChristopher Smith        if(empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
410443e135dSChristopher Smith        if(!empty($opts['pagesonly']) && (substr($file,-4) != '.txt')) return $return;
411443e135dSChristopher Smith        if(empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
412443e135dSChristopher Smith        if(!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
413443e135dSChristopher Smith        if(!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
4143abeade3SAndreas Gohr    }
4153abeade3SAndreas Gohr
4163abeade3SAndreas Gohr    // still here? prepare the item
4173abeade3SAndreas Gohr    $item['type']  = $type;
41832d6093dSAndreas Gohr    $item['level'] = $lvl;
4193abeade3SAndreas Gohr    $item['open']  = $return;
4203abeade3SAndreas Gohr
4210e80bb5eSChristopher Smith    if(!empty($opts['meta'])){
4223009a773SAndreas Gohr        $item['file']       = utf8_basename($file);
4233abeade3SAndreas Gohr        $item['size']       = filesize($base.'/'.$file);
4243abeade3SAndreas Gohr        $item['mtime']      = filemtime($base.'/'.$file);
4253abeade3SAndreas Gohr        $item['rev']        = $item['mtime'];
4263abeade3SAndreas Gohr        $item['writable']   = is_writable($base.'/'.$file);
4273abeade3SAndreas Gohr        $item['executable'] = is_executable($base.'/'.$file);
4283abeade3SAndreas Gohr    }
4293abeade3SAndreas Gohr
4303abeade3SAndreas Gohr    if($type == 'f'){
4310e80bb5eSChristopher Smith        if(!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
4320e80bb5eSChristopher Smith        if(!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
4333abeade3SAndreas Gohr    }
4343abeade3SAndreas Gohr
4353abeade3SAndreas Gohr    // finally add the item
4363abeade3SAndreas Gohr    $data[] = $item;
4373abeade3SAndreas Gohr    return $return;
4383abeade3SAndreas Gohr}
4393abeade3SAndreas Gohr
440e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
441