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