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