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 9f3f0262cSandi/** 10ce4301e3SGerrit Uitslag * Recurse directory 1115fae107Sandi * 12f3f0262cSandi * This function recurses into a given base directory 13f3f0262cSandi * and calls the supplied function for each file and directory 1415fae107Sandi * 1524998b31SGerrit Uitslag * @param array &$data The results of the search are stored here 1624baa045SAndreas Gohr * @param string $base Where to start the search 17fe82d751SChristopher Smith * @param callback $func Callback (function name or array with object,method) 1824998b31SGerrit Uitslag * @param array $opts option array will be given to the Callback 1924baa045SAndreas Gohr * @param string $dir Current directory beyond $base 2024baa045SAndreas Gohr * @param int $lvl Recursion Level 21*64159a61SAndreas Gohr * @param mixed $sort 'natural' to use natural order sorting (default); 22*64159a61SAndreas Gohr * 'date' to sort by filemtime; leave empty to skip sorting. 2315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 24f3f0262cSandi */ 25155e63c9SChristopher Smithfunction search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){ 26f3f0262cSandi $dirs = array(); 27f3f0262cSandi $files = array(); 28abc306f4SKate Arzamastseva $filepaths = array(); 29f3f0262cSandi 30e0b6aadeSAndreas Gohr // safeguard against runaways #1452 31e0b6aadeSAndreas Gohr if($base == '' || $base == '/') { 32e0b6aadeSAndreas Gohr throw new RuntimeException('No valid $base passed to search() - possible misconfiguration or bug'); 33e0b6aadeSAndreas Gohr } 34e0b6aadeSAndreas Gohr 35f3f0262cSandi //read in directories and files 36f3f0262cSandi $dh = @opendir($base.'/'.$dir); 37f3f0262cSandi if(!$dh) return; 38f3f0262cSandi while(($file = readdir($dh)) !== false){ 39de3dfc91Sandi if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs 40f3f0262cSandi if(is_dir($base.'/'.$dir.'/'.$file)){ 41f3f0262cSandi $dirs[] = $dir.'/'.$file; 42f3f0262cSandi continue; 43f3f0262cSandi } 44f3f0262cSandi $files[] = $dir.'/'.$file; 45abc306f4SKate Arzamastseva $filepaths[] = $base.'/'.$dir.'/'.$file; 46f3f0262cSandi } 47f3f0262cSandi closedir($dh); 48ec24a2dfSPhilipp A. Hartmann if (!empty($sort)) { 49abc306f4SKate Arzamastseva if ($sort == 'date') { 50d971ea8bSKate Arzamastseva @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files); 511dc5d48bSChristopher Smith } else /* natural */ { 521dc5d48bSChristopher Smith natsort($files); 53abc306f4SKate Arzamastseva } 541dc5d48bSChristopher Smith natsort($dirs); 55ec24a2dfSPhilipp A. Hartmann } 56f3f0262cSandi 57f3f0262cSandi //give directories to userfunction then recurse 58f3f0262cSandi foreach($dirs as $dir){ 59d8126df2SGina Haeussge if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){ 605514a5a7SChristopher Smith search($data,$base,$func,$opts,$dir,$lvl+1,$sort); 61f3f0262cSandi } 62f3f0262cSandi } 63f3f0262cSandi //now handle the files 64f3f0262cSandi foreach($files as $file){ 65d8126df2SGina Haeussge call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts)); 66f3f0262cSandi } 67f3f0262cSandi} 68f3f0262cSandi 69f3f0262cSandi/** 70f3f0262cSandi * The following functions are userfunctions to use with the search 71f3f0262cSandi * function above. This function is called for every found file or 72f3f0262cSandi * directory. When a directory is given to the function it has to 73f3f0262cSandi * decide if this directory should be traversed (true) or not (false) 74f3f0262cSandi * The function has to accept the following parameters: 75f3f0262cSandi * 76ce4301e3SGerrit Uitslag * array &$data - Reference to the result data structure 77ce4301e3SGerrit Uitslag * string $base - Base usually $conf['datadir'] 78ce4301e3SGerrit Uitslag * string $file - current file or directory relative to $base 79ce4301e3SGerrit Uitslag * string $type - Type either 'd' for directory or 'f' for file 80ce4301e3SGerrit Uitslag * int $lvl - Current recursion depht 81ce4301e3SGerrit Uitslag * array $opts - option array as given to search() 82f3f0262cSandi * 83f3f0262cSandi * return values for files are ignored 84f3f0262cSandi * 85f3f0262cSandi * All functions should check the ACL for document READ rights 86783d2e49SAdrian Lang * namespaces (directories) are NOT checked (when sneaky_index is 0) as this 87783d2e49SAdrian Lang * would break the recursion (You can have an nonreadable dir over a readable 880e1a261eSMichael Klier * one deeper nested) also make sure to check the file type (for example 890e1a261eSMichael Klier * in case of lockfiles). 90f3f0262cSandi */ 91f3f0262cSandi 92f3f0262cSandi/** 9363f2400bSandi * Searches for pages beginning with the given query 9463f2400bSandi * 9563f2400bSandi * @author Andreas Gohr <andi@splitbrain.org> 96f50a239bSTakamura * 97f50a239bSTakamura * @param array $data 98f50a239bSTakamura * @param string $base 99f50a239bSTakamura * @param string $file 100f50a239bSTakamura * @param string $type 101f50a239bSTakamura * @param integer $lvl 102f50a239bSTakamura * @param array $opts 103f50a239bSTakamura * 104f50a239bSTakamura * @return bool 10563f2400bSandi */ 10663f2400bSandifunction search_qsearch(&$data,$base,$file,$type,$lvl,$opts){ 1078705cc81SAndreas Gohr $opts = array( 1088705cc81SAndreas Gohr 'idmatch' => '(^|:)'.preg_quote($opts['query'],'/').'/', 1098705cc81SAndreas Gohr 'listfiles' => true, 1108705cc81SAndreas Gohr 'pagesonly' => true, 1118705cc81SAndreas Gohr ); 1128705cc81SAndreas Gohr return search_universal($data,$base,$file,$type,$lvl,$opts); 11363f2400bSandi} 11463f2400bSandi 11563f2400bSandi/** 11615fae107Sandi * Build the browsable index of pages 117f3f0262cSandi * 118783d2e49SAdrian Lang * $opts['ns'] is the currently viewed namespace 11915fae107Sandi * 12015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 121f50a239bSTakamura * 122f50a239bSTakamura * @param array $data 123f50a239bSTakamura * @param string $base 124f50a239bSTakamura * @param string $file 125f50a239bSTakamura * @param string $type 126f50a239bSTakamura * @param integer $lvl 127f50a239bSTakamura * @param array $opts 128f50a239bSTakamura * 129f50a239bSTakamura * @return bool 130f3f0262cSandi */ 131f3f0262cSandifunction search_index(&$data,$base,$file,$type,$lvl,$opts){ 132d1c7b6ecSAndreas Gohr global $conf; 133783d2e49SAdrian Lang $opts = array( 134783d2e49SAdrian Lang 'pagesonly' => true, 135783d2e49SAdrian Lang 'listdirs' => true, 136443e135dSChristopher Smith 'listfiles' => empty($opts['nofiles']), 137783d2e49SAdrian Lang 'sneakyacl' => $conf['sneaky_index'], 138783d2e49SAdrian Lang // Hacky, should rather use recmatch 1391c6c1c6cSMichael Hamann 'depth' => preg_match('#^'.preg_quote($file, '#').'(/|$)#','/'.$opts['ns']) ? 0 : -1 140783d2e49SAdrian Lang ); 141f3f0262cSandi 142783d2e49SAdrian Lang return search_universal($data, $base, $file, $type, $lvl, $opts); 143f3f0262cSandi} 144f3f0262cSandi 145f3f0262cSandi/** 14615fae107Sandi * List all namespaces 14715fae107Sandi * 14815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 149f50a239bSTakamura * 150f50a239bSTakamura * @param array $data 151f50a239bSTakamura * @param string $base 152f50a239bSTakamura * @param string $file 153f50a239bSTakamura * @param string $type 154f50a239bSTakamura * @param integer $lvl 155f50a239bSTakamura * @param array $opts 156f50a239bSTakamura * 157f50a239bSTakamura * @return bool 158f3f0262cSandi */ 159f3f0262cSandifunction search_namespaces(&$data,$base,$file,$type,$lvl,$opts){ 1608705cc81SAndreas Gohr $opts = array( 1618705cc81SAndreas Gohr 'listdirs' => true, 1628705cc81SAndreas Gohr ); 1638705cc81SAndreas Gohr return search_universal($data,$base,$file,$type,$lvl,$opts); 164f3f0262cSandi} 165f3f0262cSandi 166f3f0262cSandi/** 16715fae107Sandi * List all mediafiles in a namespace 16842ea7f44SGerrit Uitslag * $opts['depth'] recursion level, 0 for all 16942ea7f44SGerrit Uitslag * $opts['showmsg'] shows message if invalid media id is used 17042ea7f44SGerrit Uitslag * $opts['skipacl'] skip acl checking 17142ea7f44SGerrit Uitslag * $opts['pattern'] check given pattern 17242ea7f44SGerrit Uitslag * $opts['hash'] add hashes to result list 17315fae107Sandi * 17415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 175f50a239bSTakamura * 176f50a239bSTakamura * @param array $data 177f50a239bSTakamura * @param string $base 178f50a239bSTakamura * @param string $file 179f50a239bSTakamura * @param string $type 180f50a239bSTakamura * @param integer $lvl 181f50a239bSTakamura * @param array $opts 182f50a239bSTakamura * 183f50a239bSTakamura * @return bool 184f3f0262cSandi */ 185f3f0262cSandifunction search_media(&$data,$base,$file,$type,$lvl,$opts){ 186b8219d2dSAndreas Gohr 187f3f0262cSandi //we do nothing with directories 1881a49ac65SGina Haeussge if($type == 'd') { 1890e80bb5eSChristopher Smith if(empty($opts['depth'])) return true; // recurse forever 19078315408SAndreas Gohr $depth = substr_count($file,'/'); 191b8219d2dSAndreas Gohr if($depth >= $opts['depth']) return false; // depth reached 192224122cfSAndreas Gohr return true; 1931a49ac65SGina Haeussge } 194f3f0262cSandi 195f3f0262cSandi $info = array(); 196156a608cSandi $info['id'] = pathID($file,true); 19764807c84SAndreas Gohr if($info['id'] != cleanID($info['id'])){ 19864807c84SAndreas Gohr if($opts['showmsg']) 19964807c84SAndreas Gohr msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1); 20064807c84SAndreas Gohr return false; // skip non-valid files 20164807c84SAndreas Gohr } 202f3f0262cSandi 203f3f0262cSandi //check ACL for namespace (we have no ACL for mediafiles) 204224122cfSAndreas Gohr $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*'); 2050e80bb5eSChristopher Smith if(empty($opts['skipacl']) && $info['perm'] < AUTH_READ){ 206224122cfSAndreas Gohr return false; 207224122cfSAndreas Gohr } 208224122cfSAndreas Gohr 209224122cfSAndreas Gohr //check pattern filter 2100e80bb5eSChristopher Smith if(!empty($opts['pattern']) && !@preg_match($opts['pattern'], $info['id'])){ 211f3f0262cSandi return false; 212f3f0262cSandi } 213f3f0262cSandi 2143009a773SAndreas Gohr $info['file'] = utf8_basename($file); 215f3f0262cSandi $info['size'] = filesize($base.'/'.$file); 2165e7fa82eSAndreas Gohr $info['mtime'] = filemtime($base.'/'.$file); 2173df72098SAndreas Gohr $info['writable'] = is_writable($base.'/'.$file); 218f3f0262cSandi if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 219f3f0262cSandi $info['isimg'] = true; 22023a34783SAndreas Gohr $info['meta'] = new JpegMeta($base.'/'.$file); 221f3f0262cSandi }else{ 222f3f0262cSandi $info['isimg'] = false; 223f3f0262cSandi } 2240e80bb5eSChristopher Smith if(!empty($opts['hash'])){ 225dfd343c4SAndreas Gohr $info['hash'] = md5(io_readFile(mediaFN($info['id']),false)); 226224122cfSAndreas Gohr } 227224122cfSAndreas Gohr 228f3f0262cSandi $data[] = $info; 229f3f0262cSandi 230f3f0262cSandi return false; 231f3f0262cSandi} 232f3f0262cSandi 233f3f0262cSandi/** 234f3f0262cSandi * This function just lists documents (for RSS namespace export) 23515fae107Sandi * 23615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 237f50a239bSTakamura * 238f50a239bSTakamura * @param array $data 239f50a239bSTakamura * @param string $base 240f50a239bSTakamura * @param string $file 241f50a239bSTakamura * @param string $type 242f50a239bSTakamura * @param integer $lvl 243f50a239bSTakamura * @param array $opts 244f50a239bSTakamura * 245f50a239bSTakamura * @return bool 246f3f0262cSandi */ 247f3f0262cSandifunction search_list(&$data,$base,$file,$type,$lvl,$opts){ 248f3f0262cSandi //we do nothing with directories 249f3f0262cSandi if($type == 'd') return false; 2500e1a261eSMichael Klier //only search txt files 2510e1a261eSMichael Klier if(substr($file,-4) == '.txt'){ 252f3f0262cSandi //check ACL 253f3f0262cSandi $id = pathID($file); 254f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 255f3f0262cSandi return false; 256f3f0262cSandi } 2570e1a261eSMichael Klier $data[]['id'] = $id; 258f3f0262cSandi } 259f3f0262cSandi return false; 260f3f0262cSandi} 261f3f0262cSandi 262f3f0262cSandi/** 263f3f0262cSandi * Quicksearch for searching matching pagenames 264f3f0262cSandi * 265f3f0262cSandi * $opts['query'] is the search query 26615fae107Sandi * 26715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 268f50a239bSTakamura * 269f50a239bSTakamura * @param array $data 270f50a239bSTakamura * @param string $base 271f50a239bSTakamura * @param string $file 272f50a239bSTakamura * @param string $type 273f50a239bSTakamura * @param integer $lvl 274f50a239bSTakamura * @param array $opts 275f50a239bSTakamura * 276f50a239bSTakamura * @return bool 277f3f0262cSandi */ 278f3f0262cSandifunction search_pagename(&$data,$base,$file,$type,$lvl,$opts){ 279f3f0262cSandi //we do nothing with directories 280f3f0262cSandi if($type == 'd') return true; 281f3f0262cSandi //only search txt files 2820e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 283f3f0262cSandi 284f3f0262cSandi //simple stringmatching 285396b7edbSmatthiasgrimm if (!empty($opts['query'])){ 286f3f0262cSandi if(strpos($file,$opts['query']) !== false){ 287f3f0262cSandi //check ACL 288f3f0262cSandi $id = pathID($file); 289f3f0262cSandi if(auth_quickaclcheck($id) < AUTH_READ){ 290f3f0262cSandi return false; 291f3f0262cSandi } 292f3f0262cSandi $data[]['id'] = $id; 293f3f0262cSandi } 294396b7edbSmatthiasgrimm } 295f3f0262cSandi return true; 296f3f0262cSandi} 297f3f0262cSandi 298f3f0262cSandi/** 29958b6f612SAndreas Gohr * Just lists all documents 30058b6f612SAndreas Gohr * 3011fcfad4dSAndreas Gohr * $opts['depth'] recursion level, 0 for all 3021fcfad4dSAndreas Gohr * $opts['hash'] do md5 sum of content? 303224122cfSAndreas Gohr * $opts['skipacl'] list everything regardless of ACL 3041fcfad4dSAndreas Gohr * 30558b6f612SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 306f50a239bSTakamura * 307f50a239bSTakamura * @param array $data 308f50a239bSTakamura * @param string $base 309f50a239bSTakamura * @param string $file 310f50a239bSTakamura * @param string $type 311f50a239bSTakamura * @param integer $lvl 312f50a239bSTakamura * @param array $opts 313f50a239bSTakamura * 314f50a239bSTakamura * @return bool 31558b6f612SAndreas Gohr */ 31658b6f612SAndreas Gohrfunction search_allpages(&$data,$base,$file,$type,$lvl,$opts){ 3178451f4adSGuillaume Turri if(isset($opts['depth']) && $opts['depth']){ 318c647387eSGuillaume Turri $parts = explode('/',ltrim($file,'/')); 3195737a81eSMichael Hamann if(($type == 'd' && count($parts) >= $opts['depth']) 3205737a81eSMichael Hamann || ($type != 'd' && count($parts) > $opts['depth'])){ 321c647387eSGuillaume Turri return false; // depth reached 322c647387eSGuillaume Turri } 323c647387eSGuillaume Turri } 324c647387eSGuillaume Turri 32558b6f612SAndreas Gohr //we do nothing with directories 3261fcfad4dSAndreas Gohr if($type == 'd'){ 3271fcfad4dSAndreas Gohr return true; 3281fcfad4dSAndreas Gohr } 3291fcfad4dSAndreas Gohr 33058b6f612SAndreas Gohr //only search txt files 3310e1a261eSMichael Klier if(substr($file,-4) != '.txt') return true; 33258b6f612SAndreas Gohr 33359bc3b48SGerrit Uitslag $item = array(); 3341fcfad4dSAndreas Gohr $item['id'] = pathID($file); 3358f34cf3dSMichael Große if(isset($opts['skipacl']) && !$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){ 3361fcfad4dSAndreas Gohr return false; 3371fcfad4dSAndreas Gohr } 3381fcfad4dSAndreas Gohr 3391fcfad4dSAndreas Gohr $item['rev'] = filemtime($base.'/'.$file); 340224122cfSAndreas Gohr $item['mtime'] = $item['rev']; 3411fcfad4dSAndreas Gohr $item['size'] = filesize($base.'/'.$file); 3428f34cf3dSMichael Große if(!empty($opts['hash'])){ 3431fcfad4dSAndreas Gohr $item['hash'] = md5(trim(rawWiki($item['id']))); 3441fcfad4dSAndreas Gohr } 3451fcfad4dSAndreas Gohr 3461fcfad4dSAndreas Gohr $data[] = $item; 34758b6f612SAndreas Gohr return true; 34858b6f612SAndreas Gohr} 34958b6f612SAndreas Gohr 350b59a406bSmatthiasgrimm/* ------------- helper functions below -------------- */ 351b59a406bSmatthiasgrimm 352b59a406bSmatthiasgrimm/** 35315fae107Sandi * fulltext sort 35415fae107Sandi * 355f3f0262cSandi * Callback sort function for use with usort to sort the data 356f3f0262cSandi * structure created by search_fulltext. Sorts descending by count 35715fae107Sandi * 35815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 359f50a239bSTakamura * 360f50a239bSTakamura * @param array $a 361f50a239bSTakamura * @param array $b 362f50a239bSTakamura * 363f50a239bSTakamura * @return int 364f3f0262cSandi */ 365f3f0262cSandifunction sort_search_fulltext($a,$b){ 366f3f0262cSandi if($a['count'] > $b['count']){ 367f3f0262cSandi return -1; 368f3f0262cSandi }elseif($a['count'] < $b['count']){ 369f3f0262cSandi return 1; 370f3f0262cSandi }else{ 371f3f0262cSandi return strcmp($a['id'],$b['id']); 372f3f0262cSandi } 373f3f0262cSandi} 374f3f0262cSandi 375f3f0262cSandi/** 376f3f0262cSandi * translates a document path to an ID 37715fae107Sandi * 37815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 37937e34a5eSandi * @todo move to pageutils 380f50a239bSTakamura * 381f50a239bSTakamura * @param string $path 382f50a239bSTakamura * @param bool $keeptxt 383f50a239bSTakamura * 384f50a239bSTakamura * @return mixed|string 385f3f0262cSandi */ 386156a608cSandifunction pathID($path,$keeptxt=false){ 38749c713a3Sandi $id = utf8_decodeFN($path); 38849c713a3Sandi $id = str_replace('/',':',$id); 389156a608cSandi if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id); 390709b1063SAdrian Lang $id = trim($id, ':'); 391f3f0262cSandi return $id; 392f3f0262cSandi} 393f3f0262cSandi 394340756e4Sandi 3953abeade3SAndreas Gohr/** 3963abeade3SAndreas Gohr * This is a very universal callback for the search() function, replacing 3973abeade3SAndreas Gohr * many of the former individual functions at the cost of a more complex 3983abeade3SAndreas Gohr * setup. 3993abeade3SAndreas Gohr * 4003abeade3SAndreas Gohr * How the function behaves, depends on the options passed in the $opts 4013abeade3SAndreas Gohr * array, where the following settings can be used. 4023abeade3SAndreas Gohr * 403e14fe973SGerrit Uitslag * depth int recursion depth. 0 for unlimited (default: 0) 404e14fe973SGerrit Uitslag * keeptxt bool keep .txt extension for IDs (default: false) 405e14fe973SGerrit Uitslag * listfiles bool include files in listing (default: false) 406e14fe973SGerrit Uitslag * listdirs bool include namespaces in listing (default: false) 407e14fe973SGerrit Uitslag * pagesonly bool restrict files to pages (default: false) 408e14fe973SGerrit Uitslag * skipacl bool do not check for READ permission (default: false) 409e14fe973SGerrit Uitslag * sneakyacl bool don't recurse into nonreadable dirs (default: false) 410e14fe973SGerrit Uitslag * hash bool create MD5 hash for files (default: false) 411e14fe973SGerrit Uitslag * meta bool return file metadata (default: false) 412e14fe973SGerrit Uitslag * filematch string match files against this regexp (default: '', so accept everything) 413e14fe973SGerrit Uitslag * idmatch string match full ID against this regexp (default: '', so accept everything) 414e14fe973SGerrit Uitslag * dirmatch string match directory against this regexp when adding (default: '', so accept everything) 415e14fe973SGerrit Uitslag * nsmatch string match namespace against this regexp when adding (default: '', so accept everything) 416e14fe973SGerrit Uitslag * recmatch string match directory against this regexp when recursing (default: '', so accept everything) 417e14fe973SGerrit Uitslag * showmsg bool warn about non-ID files (default: false) 418e14fe973SGerrit Uitslag * showhidden bool show hidden files(e.g. by hidepages config) too (default: false) 419e14fe973SGerrit Uitslag * firsthead bool return first heading for pages (default: false) 4203abeade3SAndreas Gohr * 421ce4301e3SGerrit Uitslag * @param array &$data - Reference to the result data structure 422ce4301e3SGerrit Uitslag * @param string $base - Base usually $conf['datadir'] 423ce4301e3SGerrit Uitslag * @param string $file - current file or directory relative to $base 424ce4301e3SGerrit Uitslag * @param string $type - Type either 'd' for directory or 'f' for file 425ce4301e3SGerrit Uitslag * @param int $lvl - Current recursion depht 426ce4301e3SGerrit Uitslag * @param array $opts - option array as given to search() 427ce4301e3SGerrit Uitslag * @return bool if this directory should be traversed (true) or not (false) 428ce4301e3SGerrit Uitslag * return value is ignored for files 429ce4301e3SGerrit Uitslag * 4303abeade3SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 4313abeade3SAndreas Gohr */ 4323abeade3SAndreas Gohrfunction search_universal(&$data,$base,$file,$type,$lvl,$opts){ 4333abeade3SAndreas Gohr $item = array(); 4343abeade3SAndreas Gohr $return = true; 4353abeade3SAndreas Gohr 4363abeade3SAndreas Gohr // get ID and check if it is a valid one 437b7a3421aSChristopher Smith $item['id'] = pathID($file,($type == 'd' || !empty($opts['keeptxt']))); 4388537abd1SAdrian Lang if($item['id'] != cleanID($item['id'])){ 43949f299d6SChristopher Smith if(!empty($opts['showmsg'])){ 4408537abd1SAdrian Lang msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1); 441b7a3421aSChristopher Smith } 4423abeade3SAndreas Gohr return false; // skip non-valid files 4433abeade3SAndreas Gohr } 4448705cc81SAndreas Gohr $item['ns'] = getNS($item['id']); 4453abeade3SAndreas Gohr 4463abeade3SAndreas Gohr if($type == 'd') { 4473abeade3SAndreas Gohr // decide if to recursion into this directory is wanted 4480e80bb5eSChristopher Smith if(empty($opts['depth'])){ 4493abeade3SAndreas Gohr $return = true; // recurse forever 4503abeade3SAndreas Gohr }else{ 4513abeade3SAndreas Gohr $depth = substr_count($file,'/'); 4523abeade3SAndreas Gohr if($depth >= $opts['depth']){ 4533abeade3SAndreas Gohr $return = false; // depth reached 4543abeade3SAndreas Gohr }else{ 4553abeade3SAndreas Gohr $return = true; 4563abeade3SAndreas Gohr } 4573abeade3SAndreas Gohr } 4589b4337c6SChristopher Smith 4599b4337c6SChristopher Smith if ($return) { 4609b4337c6SChristopher Smith $match = empty($opts['recmatch']) || preg_match('/'.$opts['recmatch'].'/',$file); 4619b4337c6SChristopher Smith if (!$match) { 4629b4337c6SChristopher Smith return false; // doesn't match 4639b4337c6SChristopher Smith } 4643abeade3SAndreas Gohr } 4653abeade3SAndreas Gohr } 4663abeade3SAndreas Gohr 4673abeade3SAndreas Gohr // check ACL 468443e135dSChristopher Smith if(empty($opts['skipacl'])){ 4693abeade3SAndreas Gohr if($type == 'd'){ 4703abeade3SAndreas Gohr $item['perm'] = auth_quickaclcheck($item['id'].':*'); 4713abeade3SAndreas Gohr }else{ 4723abeade3SAndreas Gohr $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files 4733abeade3SAndreas Gohr } 4743abeade3SAndreas Gohr }else{ 4753abeade3SAndreas Gohr $item['perm'] = AUTH_DELETE; 4763abeade3SAndreas Gohr } 4773abeade3SAndreas Gohr 4783abeade3SAndreas Gohr // are we done here maybe? 4793abeade3SAndreas Gohr if($type == 'd'){ 480443e135dSChristopher Smith if(empty($opts['listdirs'])) return $return; 481*64159a61SAndreas Gohr //neither list nor recurse forbidden items: 482*64159a61SAndreas Gohr if(empty($opts['skipacl']) && !empty($opts['sneakyacl']) && $item['perm'] < AUTH_READ) return false; 483443e135dSChristopher Smith if(!empty($opts['dirmatch']) && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return; 484443e135dSChristopher Smith if(!empty($opts['nsmatch']) && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return; 4853abeade3SAndreas Gohr }else{ 486443e135dSChristopher Smith if(empty($opts['listfiles'])) return $return; 487443e135dSChristopher Smith if(empty($opts['skipacl']) && $item['perm'] < AUTH_READ) return $return; 488443e135dSChristopher Smith if(!empty($opts['pagesonly']) && (substr($file,-4) != '.txt')) return $return; 489443e135dSChristopher Smith if(empty($opts['showhidden']) && isHiddenPage($item['id'])) return $return; 490443e135dSChristopher Smith if(!empty($opts['filematch']) && !preg_match('/'.$opts['filematch'].'/',$file)) return $return; 491443e135dSChristopher Smith if(!empty($opts['idmatch']) && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return; 4923abeade3SAndreas Gohr } 4933abeade3SAndreas Gohr 4943abeade3SAndreas Gohr // still here? prepare the item 4953abeade3SAndreas Gohr $item['type'] = $type; 49632d6093dSAndreas Gohr $item['level'] = $lvl; 4973abeade3SAndreas Gohr $item['open'] = $return; 4983abeade3SAndreas Gohr 4990e80bb5eSChristopher Smith if(!empty($opts['meta'])){ 5003009a773SAndreas Gohr $item['file'] = utf8_basename($file); 5013abeade3SAndreas Gohr $item['size'] = filesize($base.'/'.$file); 5023abeade3SAndreas Gohr $item['mtime'] = filemtime($base.'/'.$file); 5033abeade3SAndreas Gohr $item['rev'] = $item['mtime']; 5043abeade3SAndreas Gohr $item['writable'] = is_writable($base.'/'.$file); 5053abeade3SAndreas Gohr $item['executable'] = is_executable($base.'/'.$file); 5063abeade3SAndreas Gohr } 5073abeade3SAndreas Gohr 5083abeade3SAndreas Gohr if($type == 'f'){ 5090e80bb5eSChristopher Smith if(!empty($opts['hash'])) $item['hash'] = md5(io_readFile($base.'/'.$file,false)); 5100e80bb5eSChristopher Smith if(!empty($opts['firsthead'])) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER); 5113abeade3SAndreas Gohr } 5123abeade3SAndreas Gohr 5133abeade3SAndreas Gohr // finally add the item 5143abeade3SAndreas Gohr $data[] = $item; 5153abeade3SAndreas Gohr return $return; 5163abeade3SAndreas Gohr} 5173abeade3SAndreas Gohr 518e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 519