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