1<?php 2/** 3 * File IO 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 * Removes empty directories 13 * 14 * Sends IO_NAMESPACE_DELETED events for 'pages' and 'media' namespaces. 15 * Event data: 16 * $data[0] ns: The colon separated namespace path minus the trailing page name. 17 * $data[1] ns_type: 'pages' or 'media' namespace tree. 18 * 19 * @todo use safemode hack 20 * @param string $id - a pageid, the namespace of that id will be tried to deleted 21 * @param string $basedir - the config name of the type to delete (datadir or mediadir usally) 22 * @return bool - true if at least one namespace was deleted 23 * 24 * @author Andreas Gohr <andi@splitbrain.org> 25 * @author Ben Coburn <btcoburn@silicodon.net> 26 */ 27function io_sweepNS($id,$basedir='datadir'){ 28 global $conf; 29 $types = array ('datadir'=>'pages', 'mediadir'=>'media'); 30 $ns_type = (isset($types[$basedir])?$types[$basedir]:false); 31 32 $delone = false; 33 34 //scan all namespaces 35 while(($id = getNS($id)) !== false){ 36 $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id)); 37 38 //try to delete dir else return 39 if(@rmdir($dir)) { 40 if ($ns_type!==false) { 41 $data = array($id, $ns_type); 42 $delone = true; // we deleted at least one dir 43 trigger_event('IO_NAMESPACE_DELETED', $data); 44 } 45 } else { return $delone; } 46 } 47 return $delone; 48} 49 50/** 51 * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events. 52 * 53 * Generates the action event which delegates to io_readFile(). 54 * Action plugins are allowed to modify the page content in transit. 55 * The file path should not be changed. 56 * 57 * Event data: 58 * $data[0] The raw arguments for io_readFile as an array. 59 * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 60 * $data[2] page_name: The wiki page name. 61 * $data[3] rev: The page revision, false for current wiki pages. 62 * 63 * @author Ben Coburn <btcoburn@silicodon.net> 64 * 65 * @param string $file filename 66 * @param string $id page id 67 * @param bool|int $rev revision timestamp 68 * @return string 69 */ 70function io_readWikiPage($file, $id, $rev=false) { 71 if (empty($rev)) { $rev = false; } 72 $data = array(array($file, true), getNS($id), noNS($id), $rev); 73 return trigger_event('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false); 74} 75 76/** 77 * Callback adapter for io_readFile(). 78 * 79 * @author Ben Coburn <btcoburn@silicodon.net> 80 * 81 * @param array $data event data 82 * @return string 83 */ 84function _io_readWikiPage_action($data) { 85 if (is_array($data) && is_array($data[0]) && count($data[0])===2) { 86 return call_user_func_array('io_readFile', $data[0]); 87 } else { 88 return ''; //callback error 89 } 90} 91 92/** 93 * Returns content of $file as cleaned string. 94 * 95 * Uses gzip if extension is .gz 96 * 97 * If you want to use the returned value in unserialize 98 * be sure to set $clean to false! 99 * 100 * @author Andreas Gohr <andi@splitbrain.org> 101 * 102 * @param string $file filename 103 * @param bool $clean 104 * @return string|bool the file contents or false on error 105 */ 106function io_readFile($file,$clean=true){ 107 $ret = ''; 108 if(file_exists($file)){ 109 if(substr($file,-3) == '.gz'){ 110 $ret = join('',gzfile($file)); 111 }else if(substr($file,-4) == '.bz2'){ 112 $ret = bzfile($file); 113 }else{ 114 $ret = file_get_contents($file); 115 } 116 } 117 if($ret !== false && $clean){ 118 return cleanText($ret); 119 }else{ 120 return $ret; 121 } 122} 123/** 124 * Returns the content of a .bz2 compressed file as string 125 * 126 * @author marcel senf <marcel@rucksackreinigung.de> 127 * @author Andreas Gohr <andi@splitbrain.org> 128 * 129 * @param string $file filename 130 * @param bool $array return array of lines 131 * @return string|array|bool content or false on error 132 */ 133function bzfile($file, $array=false) { 134 $bz = bzopen($file,"r"); 135 if($bz === false) return false; 136 137 if($array) $lines = array(); 138 $str = ''; 139 while (!feof($bz)) { 140 //8192 seems to be the maximum buffersize? 141 $buffer = bzread($bz,8192); 142 if(($buffer === false) || (bzerrno($bz) !== 0)) { 143 return false; 144 } 145 $str = $str . $buffer; 146 if($array) { 147 $pos = strpos($str, "\n"); 148 while($pos !== false) { 149 $lines[] = substr($str, 0, $pos+1); 150 $str = substr($str, $pos+1); 151 $pos = strpos($str, "\n"); 152 } 153 } 154 } 155 bzclose($bz); 156 if($array) { 157 if($str !== '') $lines[] = $str; 158 return $lines; 159 } 160 return $str; 161} 162 163/** 164 * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events. 165 * 166 * This generates an action event and delegates to io_saveFile(). 167 * Action plugins are allowed to modify the page content in transit. 168 * The file path should not be changed. 169 * (The append parameter is set to false.) 170 * 171 * Event data: 172 * $data[0] The raw arguments for io_saveFile as an array. 173 * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 174 * $data[2] page_name: The wiki page name. 175 * $data[3] rev: The page revision, false for current wiki pages. 176 * 177 * @author Ben Coburn <btcoburn@silicodon.net> 178 * 179 * @param string $file filename 180 * @param string $content 181 * @param string $id page id 182 * @param int|bool $rev timestamp of revision 183 * @return bool 184 */ 185function io_writeWikiPage($file, $content, $id, $rev=false) { 186 if (empty($rev)) { $rev = false; } 187 if ($rev===false) { io_createNamespace($id); } // create namespaces as needed 188 $data = array(array($file, $content, false), getNS($id), noNS($id), $rev); 189 return trigger_event('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false); 190} 191 192/** 193 * Callback adapter for io_saveFile(). 194 * @author Ben Coburn <btcoburn@silicodon.net> 195 * 196 * @param array $data event data 197 * @return bool 198 */ 199function _io_writeWikiPage_action($data) { 200 if (is_array($data) && is_array($data[0]) && count($data[0])===3) { 201 return call_user_func_array('io_saveFile', $data[0]); 202 } else { 203 return false; //callback error 204 } 205} 206 207/** 208 * Internal function to save contents to a file. 209 * 210 * @author Andreas Gohr <andi@splitbrain.org> 211 * 212 * @param string $file filename path to file 213 * @param string $content 214 * @param bool $append 215 * @return bool true on success, otherwise false 216 */ 217function _io_saveFile($file, $content, $append) { 218 global $conf; 219 $mode = ($append) ? 'ab' : 'wb'; 220 $fileexists = file_exists($file); 221 222 if(substr($file,-3) == '.gz'){ 223 $fh = @gzopen($file,$mode.'9'); 224 if(!$fh) return false; 225 gzwrite($fh, $content); 226 gzclose($fh); 227 }else if(substr($file,-4) == '.bz2'){ 228 if($append) { 229 $bzcontent = bzfile($file); 230 if($bzcontent === false) return false; 231 $content = $bzcontent.$content; 232 } 233 $fh = @bzopen($file,'w'); 234 if(!$fh) return false; 235 bzwrite($fh, $content); 236 bzclose($fh); 237 }else{ 238 $fh = @fopen($file,$mode); 239 if(!$fh) return false; 240 fwrite($fh, $content); 241 fclose($fh); 242 } 243 244 if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']); 245 return true; 246} 247 248/** 249 * Saves $content to $file. 250 * 251 * If the third parameter is set to true the given content 252 * will be appended. 253 * 254 * Uses gzip if extension is .gz 255 * and bz2 if extension is .bz2 256 * 257 * @author Andreas Gohr <andi@splitbrain.org> 258 * 259 * @param string $file filename path to file 260 * @param string $content 261 * @param bool $append 262 * @return bool true on success, otherwise false 263 */ 264function io_saveFile($file, $content, $append=false) { 265 io_makeFileDir($file); 266 io_lock($file); 267 if(!_io_saveFile($file, $content, $append)) { 268 msg("Writing $file failed",-1); 269 io_unlock($file); 270 return false; 271 } 272 io_unlock($file); 273 return true; 274} 275 276/** 277 * Replace one or more occurrences of a line in a file. 278 * 279 * The default, when $maxlines is 0 is to delete all matching lines then append a single line. 280 * A regex that matches any part of the line will remove the entire line in this mode. 281 * Captures in $newline are not available. 282 * 283 * Otherwise each line is matched and replaced individually, up to the first $maxlines lines 284 * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline. 285 * 286 * Be sure to include the trailing newline in $oldline when replacing entire lines. 287 * 288 * Uses gzip if extension is .gz 289 * and bz2 if extension is .bz2 290 * 291 * @author Steven Danz <steven-danz@kc.rr.com> 292 * @author Christopher Smith <chris@jalakai.co.uk> 293 * @author Patrick Brown <ptbrown@whoopdedo.org> 294 * 295 * @param string $file filename 296 * @param string $oldline exact linematch to remove 297 * @param string $newline new line to insert 298 * @param bool $regex use regexp? 299 * @param int $maxlines number of occurrences of the line to replace 300 * @return bool true on success 301 */ 302function io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0) { 303 if ((string)$oldline === '') { 304 trigger_error('$oldline parameter cannot be empty in io_replaceInFile()', E_USER_WARNING); 305 return false; 306 } 307 308 if (!file_exists($file)) return true; 309 310 io_lock($file); 311 312 // load into array 313 if(substr($file,-3) == '.gz'){ 314 $lines = gzfile($file); 315 }else if(substr($file,-4) == '.bz2'){ 316 $lines = bzfile($file, true); 317 }else{ 318 $lines = file($file); 319 } 320 321 // make non-regexes into regexes 322 $pattern = $regex ? $oldline : '/^'.preg_quote($oldline,'/').'$/'; 323 $replace = $regex ? $newline : addcslashes($newline, '\$'); 324 325 // remove matching lines 326 if ($maxlines > 0) { 327 $count = 0; 328 $matched = 0; 329 while (($count < $maxlines) && (list($i,$line) = each($lines))) { 330 // $matched will be set to 0|1 depending on whether pattern is matched and line replaced 331 $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched); 332 if ($matched) $count++; 333 } 334 } else if ($maxlines == 0) { 335 $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT); 336 337 if ((string)$newline !== ''){ 338 $lines[] = $newline; 339 } 340 } else { 341 $lines = preg_replace($pattern, $replace, $lines); 342 } 343 344 if(count($lines)){ 345 if(!_io_saveFile($file, join('',$lines), false)) { 346 msg("Removing content from $file failed",-1); 347 io_unlock($file); 348 return false; 349 } 350 }else{ 351 @unlink($file); 352 } 353 354 io_unlock($file); 355 return true; 356} 357 358/** 359 * Delete lines that match $badline from $file. 360 * 361 * Be sure to include the trailing newline in $badline 362 * 363 * @author Patrick Brown <ptbrown@whoopdedo.org> 364 * 365 * @param string $file filename 366 * @param string $badline exact linematch to remove 367 * @param bool $regex use regexp? 368 * @return bool true on success 369 */ 370function io_deleteFromFile($file,$badline,$regex=false){ 371 return io_replaceInFile($file,$badline,null,$regex,0); 372} 373 374/** 375 * Tries to lock a file 376 * 377 * Locking is only done for io_savefile and uses directories 378 * inside $conf['lockdir'] 379 * 380 * It waits maximal 3 seconds for the lock, after this time 381 * the lock is assumed to be stale and the function goes on 382 * 383 * @author Andreas Gohr <andi@splitbrain.org> 384 * 385 * @param string $file filename 386 */ 387function io_lock($file){ 388 global $conf; 389 // no locking if safemode hack 390 if($conf['safemodehack']) return; 391 392 $lockDir = $conf['lockdir'].'/'.md5($file); 393 @ignore_user_abort(1); 394 395 $timeStart = time(); 396 do { 397 //waited longer than 3 seconds? -> stale lock 398 if ((time() - $timeStart) > 3) break; 399 $locked = @mkdir($lockDir, $conf['dmode']); 400 if($locked){ 401 if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']); 402 break; 403 } 404 usleep(50); 405 } while ($locked === false); 406} 407 408/** 409 * Unlocks a file 410 * 411 * @author Andreas Gohr <andi@splitbrain.org> 412 * 413 * @param string $file filename 414 */ 415function io_unlock($file){ 416 global $conf; 417 // no locking if safemode hack 418 if($conf['safemodehack']) return; 419 420 $lockDir = $conf['lockdir'].'/'.md5($file); 421 @rmdir($lockDir); 422 @ignore_user_abort(0); 423} 424 425/** 426 * Create missing namespace directories and send the IO_NAMESPACE_CREATED events 427 * in the order of directory creation. (Parent directories first.) 428 * 429 * Event data: 430 * $data[0] ns: The colon separated namespace path minus the trailing page name. 431 * $data[1] ns_type: 'pages' or 'media' namespace tree. 432 * 433 * @author Ben Coburn <btcoburn@silicodon.net> 434 * 435 * @param string $id page id 436 * @param string $ns_type 'pages' or 'media' 437 */ 438function io_createNamespace($id, $ns_type='pages') { 439 // verify ns_type 440 $types = array('pages'=>'wikiFN', 'media'=>'mediaFN'); 441 if (!isset($types[$ns_type])) { 442 trigger_error('Bad $ns_type parameter for io_createNamespace().'); 443 return; 444 } 445 // make event list 446 $missing = array(); 447 $ns_stack = explode(':', $id); 448 $ns = $id; 449 $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) ); 450 while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) { 451 array_pop($ns_stack); 452 $ns = implode(':', $ns_stack); 453 if (strlen($ns)==0) { break; } 454 $missing[] = $ns; 455 $tmp = dirname(call_user_func($types[$ns_type], $ns)); 456 } 457 // make directories 458 io_makeFileDir($file); 459 // send the events 460 $missing = array_reverse($missing); // inside out 461 foreach ($missing as $ns) { 462 $data = array($ns, $ns_type); 463 trigger_event('IO_NAMESPACE_CREATED', $data); 464 } 465} 466 467/** 468 * Create the directory needed for the given file 469 * 470 * @author Andreas Gohr <andi@splitbrain.org> 471 * 472 * @param string $file file name 473 */ 474function io_makeFileDir($file){ 475 $dir = dirname($file); 476 if(!@is_dir($dir)){ 477 io_mkdir_p($dir) || msg("Creating directory $dir failed",-1); 478 } 479} 480 481/** 482 * Creates a directory hierachy. 483 * 484 * @link http://www.php.net/manual/en/function.mkdir.php 485 * @author <saint@corenova.com> 486 * @author Andreas Gohr <andi@splitbrain.org> 487 * 488 * @param string $target filename 489 * @return bool|int|string 490 */ 491function io_mkdir_p($target){ 492 global $conf; 493 if (@is_dir($target)||empty($target)) return 1; // best case check first 494 if (file_exists($target) && !is_dir($target)) return 0; 495 //recursion 496 if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){ 497 if($conf['safemodehack']){ 498 $dir = preg_replace('/^'.preg_quote(fullpath($conf['ftp']['root']),'/').'/','', $target); 499 return io_mkdir_ftp($dir); 500 }else{ 501 $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree 502 if($ret && !empty($conf['dperm'])) chmod($target, $conf['dperm']); 503 return $ret; 504 } 505 } 506 return 0; 507} 508 509/** 510 * Recursively delete a directory 511 * 512 * @author Andreas Gohr <andi@splitbrain.org> 513 * @param string $path 514 * @param bool $removefiles defaults to false which will delete empty directories only 515 * @return bool 516 */ 517function io_rmdir($path, $removefiles = false) { 518 if(!is_string($path) || $path == "") return false; 519 if(!file_exists($path)) return true; // it's already gone or was never there, count as success 520 521 if(is_dir($path) && !is_link($path)) { 522 $dirs = array(); 523 $files = array(); 524 525 if(!$dh = @opendir($path)) return false; 526 while(false !== ($f = readdir($dh))) { 527 if($f == '..' || $f == '.') continue; 528 529 // collect dirs and files first 530 if(is_dir("$path/$f") && !is_link("$path/$f")) { 531 $dirs[] = "$path/$f"; 532 } else if($removefiles) { 533 $files[] = "$path/$f"; 534 } else { 535 return false; // abort when non empty 536 } 537 538 } 539 closedir($dh); 540 541 // now traverse into directories first 542 foreach($dirs as $dir) { 543 if(!io_rmdir($dir, $removefiles)) return false; // abort on any error 544 } 545 546 // now delete files 547 foreach($files as $file) { 548 if(!@unlink($file)) return false; //abort on any error 549 } 550 551 // remove self 552 return @rmdir($path); 553 } else if($removefiles) { 554 return @unlink($path); 555 } 556 return false; 557} 558 559/** 560 * Creates a directory using FTP 561 * 562 * This is used when the safemode workaround is enabled 563 * 564 * @author <andi@splitbrain.org> 565 * 566 * @param string $dir name of the new directory 567 * @return false|string 568 */ 569function io_mkdir_ftp($dir){ 570 global $conf; 571 572 if(!function_exists('ftp_connect')){ 573 msg("FTP support not found - safemode workaround not usable",-1); 574 return false; 575 } 576 577 $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10); 578 if(!$conn){ 579 msg("FTP connection failed",-1); 580 return false; 581 } 582 583 if(!@ftp_login($conn, $conf['ftp']['user'], conf_decodeString($conf['ftp']['pass']))){ 584 msg("FTP login failed",-1); 585 return false; 586 } 587 588 //create directory 589 $ok = @ftp_mkdir($conn, $dir); 590 //set permissions 591 @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir)); 592 593 @ftp_close($conn); 594 return $ok; 595} 596 597/** 598 * Creates a unique temporary directory and returns 599 * its path. 600 * 601 * @author Michael Klier <chi@chimeric.de> 602 * 603 * @return false|string path to new directory or false 604 */ 605function io_mktmpdir() { 606 global $conf; 607 608 $base = $conf['tmpdir']; 609 $dir = md5(uniqid(mt_rand(), true)); 610 $tmpdir = $base.'/'.$dir; 611 612 if(io_mkdir_p($tmpdir)) { 613 return($tmpdir); 614 } else { 615 return false; 616 } 617} 618 619/** 620 * downloads a file from the net and saves it 621 * 622 * if $useAttachment is false, 623 * - $file is the full filename to save the file, incl. path 624 * - if successful will return true, false otherwise 625 * 626 * if $useAttachment is true, 627 * - $file is the directory where the file should be saved 628 * - if successful will return the name used for the saved file, false otherwise 629 * 630 * @author Andreas Gohr <andi@splitbrain.org> 631 * @author Chris Smith <chris@jalakai.co.uk> 632 * 633 * @param string $url url to download 634 * @param string $file path to file or directory where to save 635 * @param bool $useAttachment if true: try to use name of download, uses otherwise $defaultName, false: uses $file as path to file 636 * @param string $defaultName fallback for if using $useAttachment 637 * @param int $maxSize maximum file size 638 * @return bool|string if failed false, otherwise true or the name of the file in the given dir 639 */ 640function io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){ 641 global $conf; 642 $http = new DokuHTTPClient(); 643 $http->max_bodysize = $maxSize; 644 $http->timeout = 25; //max. 25 sec 645 $http->keep_alive = false; // we do single ops here, no need for keep-alive 646 647 $data = $http->get($url); 648 if(!$data) return false; 649 650 $name = ''; 651 if ($useAttachment) { 652 if (isset($http->resp_headers['content-disposition'])) { 653 $content_disposition = $http->resp_headers['content-disposition']; 654 $match=array(); 655 if (is_string($content_disposition) && 656 preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) { 657 658 $name = utf8_basename($match[1]); 659 } 660 661 } 662 663 if (!$name) { 664 if (!$defaultName) return false; 665 $name = $defaultName; 666 } 667 668 $file = $file.$name; 669 } 670 671 $fileexists = file_exists($file); 672 $fp = @fopen($file,"w"); 673 if(!$fp) return false; 674 fwrite($fp,$data); 675 fclose($fp); 676 if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']); 677 if ($useAttachment) return $name; 678 return true; 679} 680 681/** 682 * Windows compatible rename 683 * 684 * rename() can not overwrite existing files on Windows 685 * this function will use copy/unlink instead 686 * 687 * @param string $from 688 * @param string $to 689 * @return bool succes or fail 690 */ 691function io_rename($from,$to){ 692 global $conf; 693 if(!@rename($from,$to)){ 694 if(@copy($from,$to)){ 695 if($conf['fperm']) chmod($to, $conf['fperm']); 696 @unlink($from); 697 return true; 698 } 699 return false; 700 } 701 return true; 702} 703 704/** 705 * Runs an external command with input and output pipes. 706 * Returns the exit code from the process. 707 * 708 * @author Tom N Harris <tnharris@whoopdedo.org> 709 * 710 * @param string $cmd 711 * @param string $input input pipe 712 * @param string $output output pipe 713 * @return int exit code from process 714 */ 715function io_exec($cmd, $input, &$output){ 716 $descspec = array( 717 0=>array("pipe","r"), 718 1=>array("pipe","w"), 719 2=>array("pipe","w")); 720 $ph = proc_open($cmd, $descspec, $pipes); 721 if(!$ph) return -1; 722 fclose($pipes[2]); // ignore stderr 723 fwrite($pipes[0], $input); 724 fclose($pipes[0]); 725 $output = stream_get_contents($pipes[1]); 726 fclose($pipes[1]); 727 return proc_close($ph); 728} 729 730/** 731 * Search a file for matching lines 732 * 733 * This is probably not faster than file()+preg_grep() but less 734 * memory intensive because not the whole file needs to be loaded 735 * at once. 736 * 737 * @author Andreas Gohr <andi@splitbrain.org> 738 * @param string $file The file to search 739 * @param string $pattern PCRE pattern 740 * @param int $max How many lines to return (0 for all) 741 * @param bool $backref When true returns array with backreferences instead of lines 742 * @return array matching lines or backref, false on error 743 */ 744function io_grep($file,$pattern,$max=0,$backref=false){ 745 $fh = @fopen($file,'r'); 746 if(!$fh) return false; 747 $matches = array(); 748 749 $cnt = 0; 750 $line = ''; 751 while (!feof($fh)) { 752 $line .= fgets($fh, 4096); // read full line 753 if(substr($line,-1) != "\n") continue; 754 755 // check if line matches 756 if(preg_match($pattern,$line,$match)){ 757 if($backref){ 758 $matches[] = $match; 759 }else{ 760 $matches[] = $line; 761 } 762 $cnt++; 763 } 764 if($max && $max == $cnt) break; 765 $line = ''; 766 } 767 fclose($fh); 768 return $matches; 769} 770 771