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