1<?php
2/**
3 * DokuWiki search functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9/**
10 * Recurse directory
11 *
12 * This function recurses into a given base directory
13 * and calls the supplied function for each file and directory
14 *
15 * @param   array    &$data The results of the search are stored here
16 * @param   string    $base Where to start the search
17 * @param   callback  $func Callback (function name or array with object,method)
18 * @param   array     $opts option array will be given to the Callback
19 * @param   string    $dir  Current directory beyond $base
20 * @param   int       $lvl  Recursion Level
21 * @param   mixed     $sort 'natural' to use natural order sorting (default);
22 *                          'date' to sort by filemtime; leave empty to skip sorting.
23 * @author  Andreas Gohr <andi@splitbrain.org>
24 */
25function search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
26    $dirs   = array();
27    $files  = array();
28    $filepaths = array();
29
30    // safeguard against runaways #1452
31    if($base == '' || $base == '/') {
32        throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug');
33    }
34
35    //read in directories and files
36    $dh = @opendir($base.'/'.$dir);
37    if(!$dh) return;
38    while(($file = readdir($dh)) !== false){
39        if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
40        if(is_dir($base.'/'.$dir.'/'.$file)){
41            $dirs[] = $dir.'/'.$file;
42            continue;
43        }
44        $files[] = $dir.'/'.$file;
45        $filepaths[] = $base.'/'.$dir.'/'.$file;
46    }
47    closedir($dh);
48    if (!empty($sort)) {
49        if ($sort == 'date') {
50            @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
51        } else /* natural */ {
52            natsort($files);
53        }
54        natsort($dirs);
55    }
56
57    //give directories to userfunction then recurse
58    foreach($dirs as $dir){
59        if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
60            search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
61        }
62    }
63    //now handle the files
64    foreach($files as $file){
65        call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
66    }
67}
68
69/**
70 * The following functions are userfunctions to use with the search
71 * function above. This function is called for every found file or
72 * directory. When a directory is given to the function it has to
73 * decide if this directory should be traversed (true) or not (false)
74 * The function has to accept the following parameters:
75 *
76 * array &$data  - Reference to the result data structure
77 * string $base  - Base usually $conf['datadir']
78 * string $file  - current file or directory relative to $base
79 * string $type  - Type either 'd' for directory or 'f' for file
80 * int    $lvl   - Current recursion depht
81 * array  $opts  - option array as given to search()
82 *
83 * return values for files are ignored
84 *
85 * All functions should check the ACL for document READ rights
86 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this
87 * would break the recursion (You can have an nonreadable dir over a readable
88 * one deeper nested) also make sure to check the file type (for example
89 * in case of lockfiles).
90 */
91
92/**
93 * Searches for pages beginning with the given query
94 *
95 * @author Andreas Gohr <andi@splitbrain.org>
96 *
97 * @param array $data
98 * @param string $base
99 * @param string $file
100 * @param string $type
101 * @param integer $lvl
102 * @param array $opts
103 *
104 * @return bool
105 */
106function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
107    $opts = array(
108            'idmatch'   => '(^|:)'.preg_quote($opts['query'],'/').'/',
109            'listfiles' => true,
110            'pagesonly' => true,
111            );
112    return search_universal($data,$base,$file,$type,$lvl,$opts);
113}
114
115/**
116 * Build the browsable index of pages
117 *
118 * $opts['ns'] is the currently viewed namespace
119 *
120 * @author  Andreas Gohr <andi@splitbrain.org>
121 *
122 * @param array $data
123 * @param string $base
124 * @param string $file
125 * @param string $type
126 * @param integer $lvl
127 * @param array $opts
128 *
129 * @return bool
130 */
131function search_index(&$data,$base,$file,$type,$lvl,$opts){
132    global $conf;
133    $opts = array(
134        'pagesonly' => true,
135        'listdirs' => true,
136        'listfiles' => empty($opts['nofiles']),
137        'sneakyacl' => $conf['sneaky_index'],
138        // Hacky, should rather use recmatch
139        'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1
140    );
141
142    return search_universal($data, $base, $file, $type, $lvl, $opts);
143}
144
145/**
146 * List all namespaces
147 *
148 * @author  Andreas Gohr <andi@splitbrain.org>
149 *
150 * @param array $data
151 * @param string $base
152 * @param string $file
153 * @param string $type
154 * @param integer $lvl
155 * @param array $opts
156 *
157 * @return bool
158 */
159function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
160    $opts = array(
161            'listdirs' => true,
162            );
163    return search_universal($data,$base,$file,$type,$lvl,$opts);
164}
165
166/**
167 * List all mediafiles in a namespace
168 *   $opts['depth']     recursion level, 0 for all
169 *   $opts['showmsg']   shows message if invalid media id is used
170 *   $opts['skipacl']   skip acl checking
171 *   $opts['pattern']   check given pattern
172 *   $opts['hash']      add hashes to result list
173 *
174 * @author  Andreas Gohr <andi@splitbrain.org>
175 *
176 * @param array $data
177 * @param string $base
178 * @param string $file
179 * @param string $type
180 * @param integer $lvl
181 * @param array $opts
182 *
183 * @return bool
184 */
185function search_media(&$data,$base,$file,$type,$lvl,$opts){
186
187    //we do nothing with directories
188    if($type == 'd') {
189        if(empty($opts['depth'])) return true; // recurse forever
190        $depth = substr_count($file,'/');
191        if($depth >= $opts['depth']) return false; // depth reached
192        return true;
193    }
194
195    $info         = array();
196    $info['id']   = pathID($file,true);
197    if($info['id'] != cleanID($info['id'])){
198        if($opts['showmsg'])
199            msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
200        return false; // skip non-valid files
201    }
202
203    //check ACL for namespace (we have no ACL for mediafiles)
204    $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
205    if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ){
206        return false;
207    }
208
209    //check pattern filter
210    if(!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])){
211        return false;
212    }
213
214    $info['file']     = \dokuwiki\Utf8\PhpString::basename($file);
215    $info['size']     = filesize($base.'/'.$file);
216    $info['mtime']    = filemtime($base.'/'.$file);
217    $info['writable'] = is_writable($base.'/'.$file);
218    if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
219        $info['isimg'] = true;
220        $info['meta']  = new JpegMeta($base.'/'.$file);
221    }else{
222        $info['isimg'] = false;
223    }
224    if(!empty($opts['hash'])){
225        $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
226    }
227
228    $data[] = $info;
229
230    return false;
231}
232
233/**
234 * This function just lists documents (for RSS namespace export)
235 *
236 * @author  Andreas Gohr <andi@splitbrain.org>
237 *
238 * @param array $data
239 * @param string $base
240 * @param string $file
241 * @param string $type
242 * @param integer $lvl
243 * @param array $opts
244 *
245 * @return bool
246 */
247function search_list(&$data,$base,$file,$type,$lvl,$opts){
248    //we do nothing with directories
249    if($type == 'd') return false;
250    //only search txt files
251    if(substr($file,-4) == '.txt'){
252        //check ACL
253        $id = pathID($file);
254        if(auth_quickaclcheck($id) < AUTH_READ){
255            return false;
256        }
257        $data[]['id'] = $id;
258    }
259    return false;
260}
261
262/**
263 * Quicksearch for searching matching pagenames
264 *
265 * $opts['query'] is the search query
266 *
267 * @author  Andreas Gohr <andi@splitbrain.org>
268 *
269 * @param array $data
270 * @param string $base
271 * @param string $file
272 * @param string $type
273 * @param integer $lvl
274 * @param array $opts
275 *
276 * @return bool
277 */
278function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
279    //we do nothing with directories
280    if($type == 'd') return true;
281    //only search txt files
282    if(substr($file,-4) != '.txt') return true;
283
284    //simple stringmatching
285    if (!empty($opts['query'])){
286        if(strpos($file,$opts['query']) !== false){
287            //check ACL
288            $id = pathID($file);
289            if(auth_quickaclcheck($id) < AUTH_READ){
290                return false;
291            }
292            $data[]['id'] = $id;
293        }
294    }
295    return true;
296}
297
298/**
299 * Just lists all documents
300 *
301 * $opts['depth']   recursion level, 0 for all
302 * $opts['hash']    do md5 sum of content?
303 * $opts['skipacl'] list everything regardless of ACL
304 *
305 * @author  Andreas Gohr <andi@splitbrain.org>
306 *
307 * @param array $data
308 * @param string $base
309 * @param string $file
310 * @param string $type
311 * @param integer $lvl
312 * @param array $opts
313 *
314 * @return bool
315 */
316function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
317    if(isset($opts['depth']) && $opts['depth']){
318        $parts = explode('/',ltrim($file,'/'));
319        if(($type == 'd' && count($parts) >= $opts['depth'])
320          || ($type != 'd' && count($parts) > $opts['depth'])){
321            return false; // depth reached
322        }
323    }
324
325    //we do nothing with directories
326    if($type == 'd'){
327        return true;
328    }
329
330    //only search txt files
331    if(substr($file,-4) != '.txt') return true;
332
333    $item = array();
334    $item['id']   = pathID($file);
335    if(empty($opts['skipacl']) && auth_quickaclcheck($item['id']) < AUTH_READ){
336        return false;
337    }
338
339    $item['rev']   = filemtime($base.'/'.$file);
340    $item['mtime'] = $item['rev'];
341    $item['size']  = filesize($base.'/'.$file);
342    if(!empty($opts['hash'])){
343        $item['hash'] = md5(trim(rawWiki($item['id'])));
344    }
345
346    $data[] = $item;
347    return true;
348}
349
350/* ------------- helper functions below -------------- */
351
352/**
353 * fulltext sort
354 *
355 * Callback sort function for use with usort to sort the data
356 * structure created by search_fulltext. Sorts descending by count
357 *
358 * @author  Andreas Gohr <andi@splitbrain.org>
359 *
360 * @param array $a
361 * @param array $b
362 *
363 * @return int
364 */
365function sort_search_fulltext($a,$b){
366    if($a['count'] > $b['count']){
367        return -1;
368    }elseif($a['count'] < $b['count']){
369        return 1;
370    }else{
371        return strcmp($a['id'],$b['id']);
372    }
373}
374
375/**
376 * translates a document path to an ID
377 *
378 * @author  Andreas Gohr <andi@splitbrain.org>
379 * @todo    move to pageutils
380 *
381 * @param string $path
382 * @param bool $keeptxt
383 *
384 * @return mixed|string
385 */
386function pathID($path,$keeptxt=false){
387    $id = utf8_decodeFN($path);
388    $id = str_replace('/',':',$id);
389    if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
390    $id = trim($id, ':');
391    return $id;
392}
393
394
395/**
396 * This is a very universal callback for the search() function, replacing
397 * many of the former individual functions at the cost of a more complex
398 * setup.
399 *
400 * How the function behaves, depends on the options passed in the $opts
401 * array, where the following settings can be used.
402 *
403 * depth      int     recursion depth. 0 for unlimited                       (default: 0)
404 * keeptxt    bool    keep .txt extension for IDs                            (default: false)
405 * listfiles  bool    include files in listing                               (default: false)
406 * listdirs   bool    include namespaces in listing                          (default: false)
407 * pagesonly  bool    restrict files to pages                                (default: false)
408 * skipacl    bool    do not check for READ permission                       (default: false)
409 * sneakyacl  bool    don't recurse into nonreadable dirs                    (default: false)
410 * hash       bool    create MD5 hash for files                              (default: false)
411 * meta       bool    return file metadata                                   (default: false)
412 * filematch  string  match files against this regexp                        (default: '', so accept everything)
413 * idmatch    string  match full ID against this regexp                      (default: '', so accept everything)
414 * dirmatch   string  match directory against this regexp when adding        (default: '', so accept everything)
415 * nsmatch    string  match namespace against this regexp when adding        (default: '', so accept everything)
416 * recmatch   string  match directory against this regexp when recursing     (default: '', so accept everything)
417 * showmsg    bool    warn about non-ID files                                (default: false)
418 * showhidden bool    show hidden files(e.g. by hidepages config) too        (default: false)
419 * firsthead  bool    return first heading for pages                         (default: false)
420 *
421 * @param array &$data  - Reference to the result data structure
422 * @param string $base  - Base usually $conf['datadir']
423 * @param string $file  - current file or directory relative to $base
424 * @param string $type  - Type either 'd' for directory or 'f' for file
425 * @param int    $lvl   - Current recursion depht
426 * @param array  $opts  - option array as given to search()
427 * @return bool if this directory should be traversed (true) or not (false)
428 *              return value is ignored for files
429 *
430 * @author Andreas Gohr <gohr@cosmocode.de>
431 */
432function search_universal(&$data,$base,$file,$type,$lvl,$opts){
433    $item   = array();
434    $return = true;
435
436    // get ID and check if it is a valid one
437    $item['id'] = pathID($file,($type == 'd' || !empty($opts['keeptxt'])));
438    if($item['id'] != cleanID($item['id'])){
439        if(!empty($opts['showmsg'])){
440            msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
441        }
442        return false; // skip non-valid files
443    }
444    $item['ns']  = getNS($item['id']);
445
446    if($type == 'd') {
447        // decide if to recursion into this directory is wanted
448        if(empty($opts['depth'])){
449            $return = true; // recurse forever
450        }else{
451            $depth = substr_count($file,'/');
452            if($depth >= $opts['depth']){
453                $return = false; // depth reached
454            }else{
455                $return = true;
456            }
457        }
458
459        if ($return) {
460            $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/',$file);
461            if (!$match) {
462                return false; // doesn't match
463            }
464        }
465    }
466
467    // check ACL
468    if(empty($opts['skipacl'])){
469        if($type == 'd'){
470            $item['perm'] = auth_quickaclcheck($item['id'].':*');
471        }else{
472            $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
473        }
474    }else{
475        $item['perm'] = AUTH_DELETE;
476    }
477
478    // are we done here maybe?
479    if($type == 'd'){
480        if(empty($opts['listdirs'])) return $return;
481        //neither list nor recurse forbidden items:
482        if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false;
483        if(!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
484        if(!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
485    }else{
486        if(empty($opts['listfiles'])) return $return;
487        if(empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return;
488        if(!empty($opts['pagesonly']) && (substr($file,-4) != '.txt')) return $return;
489        if(empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return;
490        if(!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
491        if(!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
492    }
493
494    // still here? prepare the item
495    $item['type']  = $type;
496    $item['level'] = $lvl;
497    $item['open']  = $return;
498
499    if(!empty($opts['meta'])){
500        $item['file']       = \dokuwiki\Utf8\PhpString::basename($file);
501        $item['size']       = filesize($base.'/'.$file);
502        $item['mtime']      = filemtime($base.'/'.$file);
503        $item['rev']        = $item['mtime'];
504        $item['writable']   = is_writable($base.'/'.$file);
505        $item['executable'] = is_executable($base.'/'.$file);
506    }
507
508    if($type == 'f'){
509        if(!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
510        if(!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
511    }
512
513    // finally add the item
514    $data[] = $item;
515    return $return;
516}
517
518//Setup VIM: ex: et ts=4 :
519