1ed7b5f09Sandi<?php 2d4f83172SAndreas Gohr 315fae107Sandi/** 415fae107Sandi * File IO functions 515fae107Sandi * 615fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 815fae107Sandi */ 9d4f83172SAndreas Gohr 10109ebc86SAndreas Gohruse dokuwiki\Logger; 1124870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 125a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient; 13cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 14198564abSMichael Große 15f3f0262cSandi/** 1653d6ccfeSandi * Removes empty directories 1753d6ccfeSandi * 18cc7d0c94SBen Coburn * Sends IO_NAMESPACE_DELETED events for 'pages' and 'media' namespaces. 19cc7d0c94SBen Coburn * Event data: 20cc7d0c94SBen Coburn * $data[0] ns: The colon separated namespace path minus the trailing page name. 21cc7d0c94SBen Coburn * $data[1] ns_type: 'pages' or 'media' namespace tree. 22cc7d0c94SBen Coburn * 23d186898bSAndreas Gohr * @param string $id - a pageid, the namespace of that id will be tried to deleted 24cd2f903bSMichael Hamann * @param string $basedir - the config name of the type to delete (datadir or mediadir usally) 25cd2f903bSMichael Hamann * @return bool - true if at least one namespace was deleted 2642ea7f44SGerrit Uitslag * 2753d6ccfeSandi * @author Andreas Gohr <andi@splitbrain.org> 28cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 2953d6ccfeSandi */ 30d868eb89SAndreas Gohrfunction io_sweepNS($id, $basedir = 'datadir') 31d868eb89SAndreas Gohr{ 3253d6ccfeSandi global $conf; 3324870174SAndreas Gohr $types = ['datadir' => 'pages', 'mediadir' => 'media']; 3424870174SAndreas Gohr $ns_type = ($types[$basedir] ?? false); 3553d6ccfeSandi 36d186898bSAndreas Gohr $delone = false; 37d186898bSAndreas Gohr 3853d6ccfeSandi //scan all namespaces 3953d6ccfeSandi while (($id = getNS($id)) !== false) { 40755f1e03SAndreas Gohr $dir = $conf[$basedir] . '/' . utf8_encodeFN(str_replace(':', '/', $id)); 4153d6ccfeSandi 4253d6ccfeSandi //try to delete dir else return 43cc7d0c94SBen Coburn if (@rmdir($dir)) { 44cc7d0c94SBen Coburn if ($ns_type !== false) { 4524870174SAndreas Gohr $data = [$id, $ns_type]; 46d186898bSAndreas Gohr $delone = true; // we deleted at least one dir 47cbb44eabSAndreas Gohr Event::createAndTrigger('IO_NAMESPACE_DELETED', $data); 48cc7d0c94SBen Coburn } 49177d6836SAndreas Gohr } else { 50d4f83172SAndreas Gohr return $delone; 51d4f83172SAndreas Gohr } 52cc7d0c94SBen Coburn } 53d186898bSAndreas Gohr return $delone; 54cc7d0c94SBen Coburn} 55cc7d0c94SBen Coburn 56cc7d0c94SBen Coburn/** 57cc7d0c94SBen Coburn * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events. 58cc7d0c94SBen Coburn * 59cc7d0c94SBen Coburn * Generates the action event which delegates to io_readFile(). 60cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit. 61cc7d0c94SBen Coburn * The file path should not be changed. 62cc7d0c94SBen Coburn * 63cc7d0c94SBen Coburn * Event data: 64cc7d0c94SBen Coburn * $data[0] The raw arguments for io_readFile as an array. 65cc7d0c94SBen Coburn * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 66cc7d0c94SBen Coburn * $data[2] page_name: The wiki page name. 67cc7d0c94SBen Coburn * $data[3] rev: The page revision, false for current wiki pages. 68cc7d0c94SBen Coburn * 6942ea7f44SGerrit Uitslag * @param string $file filename 7042ea7f44SGerrit Uitslag * @param string $id page id 71c826df86SAndreas Gohr * @param bool|int|string $rev revision timestamp 7242ea7f44SGerrit Uitslag * @return string 73aa659bbaSGerrit Uitslag * 74aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 75cc7d0c94SBen Coburn */ 76d868eb89SAndreas Gohrfunction io_readWikiPage($file, $id, $rev = false) 77d868eb89SAndreas Gohr{ 78177d6836SAndreas Gohr if (empty($rev)) { 79d4f83172SAndreas Gohr $rev = false; 80d4f83172SAndreas Gohr } 8124870174SAndreas Gohr $data = [[$file, true], getNS($id), noNS($id), $rev]; 82cbb44eabSAndreas Gohr return Event::createAndTrigger('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false); 83cc7d0c94SBen Coburn} 84cc7d0c94SBen Coburn 85cc7d0c94SBen Coburn/** 86cc7d0c94SBen Coburn * Callback adapter for io_readFile(). 8742ea7f44SGerrit Uitslag * 8842ea7f44SGerrit Uitslag * @param array $data event data 8942ea7f44SGerrit Uitslag * @return string 90aa659bbaSGerrit Uitslag * 91aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 92cc7d0c94SBen Coburn */ 93d868eb89SAndreas Gohrfunction _io_readWikiPage_action($data) 94d868eb89SAndreas Gohr{ 95cc7d0c94SBen Coburn if (is_array($data) && is_array($data[0]) && count($data[0]) === 2) { 9624870174SAndreas Gohr return io_readFile(...$data[0]); 97cc7d0c94SBen Coburn } else { 98cc7d0c94SBen Coburn return ''; //callback error 9953d6ccfeSandi } 10053d6ccfeSandi} 10153d6ccfeSandi 10253d6ccfeSandi/** 10315fae107Sandi * Returns content of $file as cleaned string. 10415fae107Sandi * 10515fae107Sandi * Uses gzip if extension is .gz 10615fae107Sandi * 107ee4c4a1bSAndreas Gohr * If you want to use the returned value in unserialize 108ee4c4a1bSAndreas Gohr * be sure to set $clean to false! 109ee4c4a1bSAndreas Gohr * 11042ea7f44SGerrit Uitslag * 11142ea7f44SGerrit Uitslag * @param string $file filename 11242ea7f44SGerrit Uitslag * @param bool $clean 113d387bf5eSAndreas Gohr * @return string|bool the file contents or false on error 114aa659bbaSGerrit Uitslag * 115aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 116f3f0262cSandi */ 117d868eb89SAndreas Gohrfunction io_readFile($file, $clean = true) 118d868eb89SAndreas Gohr{ 119f3f0262cSandi $ret = ''; 12079e79377SAndreas Gohr if (file_exists($file)) { 1216c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 12213c37900SAndreas Gohr if (!DOKU_HAS_GZIP) return false; 123*3b335c64SAndreas Gohr $ret = @gzdecode(file_get_contents($file)); 124*3b335c64SAndreas Gohr if ($ret === false) return false; 1256c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 12613c37900SAndreas Gohr if (!DOKU_HAS_BZIP) return false; 127ff3ed99fSmarcel $ret = bzfile($file); 128f3f0262cSandi } else { 12943078d10SAndreas Gohr $ret = file_get_contents($file); 130f3f0262cSandi } 131f3f0262cSandi } 1322ad45addSAndreas Gohr if ($ret === null) return false; 133d387bf5eSAndreas Gohr if ($ret !== false && $clean) { 134f3f0262cSandi return cleanText($ret); 135e34c0709SAndreas Gohr } else { 136e34c0709SAndreas Gohr return $ret; 137e34c0709SAndreas Gohr } 138f3f0262cSandi} 139aa659bbaSGerrit Uitslag 140ff3ed99fSmarcel/** 141ff3ed99fSmarcel * Returns the content of a .bz2 compressed file as string 14242ea7f44SGerrit Uitslag * 14342ea7f44SGerrit Uitslag * @param string $file filename 144cfb71e37SPatrick Brown * @param bool $array return array of lines 145cfb71e37SPatrick Brown * @return string|array|bool content or false on error 146aa659bbaSGerrit Uitslag * 147aa659bbaSGerrit Uitslag * @author marcel senf <marcel@rucksackreinigung.de> 148aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 149ff3ed99fSmarcel */ 150d868eb89SAndreas Gohrfunction bzfile($file, $array = false) 151d868eb89SAndreas Gohr{ 152ff3ed99fSmarcel $bz = bzopen($file, "r"); 153d387bf5eSAndreas Gohr if ($bz === false) return false; 154d387bf5eSAndreas Gohr 155aa659bbaSGerrit Uitslag if ($array) { 156aa659bbaSGerrit Uitslag $lines = []; 157aa659bbaSGerrit Uitslag } 158cd2f903bSMichael Hamann $str = ''; 159ff3ed99fSmarcel while (!feof($bz)) { 160ff3ed99fSmarcel //8192 seems to be the maximum buffersize? 161d387bf5eSAndreas Gohr $buffer = bzread($bz, 8192); 162d387bf5eSAndreas Gohr if (($buffer === false) || (bzerrno($bz) !== 0)) { 163d387bf5eSAndreas Gohr return false; 164d387bf5eSAndreas Gohr } 16524870174SAndreas Gohr $str .= $buffer; 166cfb71e37SPatrick Brown if ($array) { 167cfb71e37SPatrick Brown $pos = strpos($str, "\n"); 168cfb71e37SPatrick Brown while ($pos !== false) { 169cfb71e37SPatrick Brown $lines[] = substr($str, 0, $pos + 1); 170cfb71e37SPatrick Brown $str = substr($str, $pos + 1); 171cfb71e37SPatrick Brown $pos = strpos($str, "\n"); 172cfb71e37SPatrick Brown } 173cfb71e37SPatrick Brown } 174ff3ed99fSmarcel } 175ff3ed99fSmarcel bzclose($bz); 176cfb71e37SPatrick Brown if ($array) { 177aa659bbaSGerrit Uitslag if ($str !== '') { 178aa659bbaSGerrit Uitslag $lines[] = $str; 179aa659bbaSGerrit Uitslag } 180cfb71e37SPatrick Brown return $lines; 181cfb71e37SPatrick Brown } 182ff3ed99fSmarcel return $str; 183ff3ed99fSmarcel} 184ff3ed99fSmarcel 185f3f0262cSandi/** 186cc7d0c94SBen Coburn * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events. 187cc7d0c94SBen Coburn * 188cc7d0c94SBen Coburn * This generates an action event and delegates to io_saveFile(). 189cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit. 190cc7d0c94SBen Coburn * The file path should not be changed. 191cc7d0c94SBen Coburn * (The append parameter is set to false.) 192cc7d0c94SBen Coburn * 193cc7d0c94SBen Coburn * Event data: 194cc7d0c94SBen Coburn * $data[0] The raw arguments for io_saveFile as an array. 195cc7d0c94SBen Coburn * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 196cc7d0c94SBen Coburn * $data[2] page_name: The wiki page name. 197cc7d0c94SBen Coburn * $data[3] rev: The page revision, false for current wiki pages. 198cc7d0c94SBen Coburn * 19942ea7f44SGerrit Uitslag * @param string $file filename 20042ea7f44SGerrit Uitslag * @param string $content 20142ea7f44SGerrit Uitslag * @param string $id page id 202c826df86SAndreas Gohr * @param int|bool|string $rev timestamp of revision 20342ea7f44SGerrit Uitslag * @return bool 204aa659bbaSGerrit Uitslag * 205aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 206cc7d0c94SBen Coburn */ 207d868eb89SAndreas Gohrfunction io_writeWikiPage($file, $content, $id, $rev = false) 208d868eb89SAndreas Gohr{ 209177d6836SAndreas Gohr if (empty($rev)) { 210d4f83172SAndreas Gohr $rev = false; 211d4f83172SAndreas Gohr } 212177d6836SAndreas Gohr if ($rev === false) { 2134e2eb11eSGerrit Uitslag io_createNamespace($id); // create namespaces as needed 2144e2eb11eSGerrit Uitslag } 21524870174SAndreas Gohr $data = [[$file, $content, false], getNS($id), noNS($id), $rev]; 216cbb44eabSAndreas Gohr return Event::createAndTrigger('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false); 217cc7d0c94SBen Coburn} 218cc7d0c94SBen Coburn 219cc7d0c94SBen Coburn/** 220cc7d0c94SBen Coburn * Callback adapter for io_saveFile(). 22142ea7f44SGerrit Uitslag * 22242ea7f44SGerrit Uitslag * @param array $data event data 22342ea7f44SGerrit Uitslag * @return bool 224aa659bbaSGerrit Uitslag * 225aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 226cc7d0c94SBen Coburn */ 227d868eb89SAndreas Gohrfunction _io_writeWikiPage_action($data) 228d868eb89SAndreas Gohr{ 229cc7d0c94SBen Coburn if (is_array($data) && is_array($data[0]) && count($data[0]) === 3) { 23024870174SAndreas Gohr $ok = io_saveFile(...$data[0]); 231a4306b74SAndreas Gohr // for attic files make sure the file has the mtime of the revision 232a4306b74SAndreas Gohr if ($ok && is_int($data[3]) && $data[3] > 0) { 233a4306b74SAndreas Gohr @touch($data[0][0], $data[3]); 234a4306b74SAndreas Gohr } 235a4306b74SAndreas Gohr return $ok; 236cc7d0c94SBen Coburn } else { 237cc7d0c94SBen Coburn return false; //callback error 238cc7d0c94SBen Coburn } 239cc7d0c94SBen Coburn} 240cc7d0c94SBen Coburn 241cc7d0c94SBen Coburn/** 2421bd6bbdeSPatrick Brown * Internal function to save contents to a file. 2431bd6bbdeSPatrick Brown * 2441bd6bbdeSPatrick Brown * @param string $file filename path to file 2451bd6bbdeSPatrick Brown * @param string $content 2461bd6bbdeSPatrick Brown * @param bool $append 2471bd6bbdeSPatrick Brown * @return bool true on success, otherwise false 248aa659bbaSGerrit Uitslag * 249aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 2501bd6bbdeSPatrick Brown */ 251d868eb89SAndreas Gohrfunction _io_saveFile($file, $content, $append) 252d868eb89SAndreas Gohr{ 2531bd6bbdeSPatrick Brown global $conf; 2541bd6bbdeSPatrick Brown $mode = ($append) ? 'ab' : 'wb'; 2551bd6bbdeSPatrick Brown $fileexists = file_exists($file); 2561bd6bbdeSPatrick Brown 2576c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 25813c37900SAndreas Gohr if (!DOKU_HAS_GZIP) return false; 2591bd6bbdeSPatrick Brown $fh = @gzopen($file, $mode . '9'); 2601bd6bbdeSPatrick Brown if (!$fh) return false; 2611bd6bbdeSPatrick Brown gzwrite($fh, $content); 2621bd6bbdeSPatrick Brown gzclose($fh); 2636c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 26413c37900SAndreas Gohr if (!DOKU_HAS_BZIP) return false; 2651bd6bbdeSPatrick Brown if ($append) { 2661bd6bbdeSPatrick Brown $bzcontent = bzfile($file); 2671bd6bbdeSPatrick Brown if ($bzcontent === false) return false; 2681bd6bbdeSPatrick Brown $content = $bzcontent . $content; 2691bd6bbdeSPatrick Brown } 2701bd6bbdeSPatrick Brown $fh = @bzopen($file, 'w'); 2711bd6bbdeSPatrick Brown if (!$fh) return false; 2721bd6bbdeSPatrick Brown bzwrite($fh, $content); 2731bd6bbdeSPatrick Brown bzclose($fh); 2741bd6bbdeSPatrick Brown } else { 2751bd6bbdeSPatrick Brown $fh = @fopen($file, $mode); 2761bd6bbdeSPatrick Brown if (!$fh) return false; 2771bd6bbdeSPatrick Brown fwrite($fh, $content); 2781bd6bbdeSPatrick Brown fclose($fh); 2791bd6bbdeSPatrick Brown } 2801bd6bbdeSPatrick Brown 281aa659bbaSGerrit Uitslag if (!$fileexists && $conf['fperm']) { 282aa659bbaSGerrit Uitslag chmod($file, $conf['fperm']); 283aa659bbaSGerrit Uitslag } 2841bd6bbdeSPatrick Brown return true; 2851bd6bbdeSPatrick Brown} 2861bd6bbdeSPatrick Brown 2871bd6bbdeSPatrick Brown/** 28815fae107Sandi * Saves $content to $file. 289f3f0262cSandi * 2901380fc45SAndreas Gohr * If the third parameter is set to true the given content 2911380fc45SAndreas Gohr * will be appended. 2921380fc45SAndreas Gohr * 29315fae107Sandi * Uses gzip if extension is .gz 294ff3ed99fSmarcel * and bz2 if extension is .bz2 29515fae107Sandi * 29642ea7f44SGerrit Uitslag * @param string $file filename path to file 29742ea7f44SGerrit Uitslag * @param string $content 29842ea7f44SGerrit Uitslag * @param bool $append 29942ea7f44SGerrit Uitslag * @return bool true on success, otherwise false 300aa659bbaSGerrit Uitslag * 301aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 302f3f0262cSandi */ 303d868eb89SAndreas Gohrfunction io_saveFile($file, $content, $append = false) 304d868eb89SAndreas Gohr{ 305f3f0262cSandi io_makeFileDir($file); 30690eb8392Sandi io_lock($file); 3071bd6bbdeSPatrick Brown if (!_io_saveFile($file, $content, $append)) { 308f3f0262cSandi msg("Writing $file failed", -1); 309fb7125eeSAndreas Gohr io_unlock($file); 310f3f0262cSandi return false; 311f3f0262cSandi } 31290eb8392Sandi io_unlock($file); 313f3f0262cSandi return true; 314f3f0262cSandi} 315f3f0262cSandi 316f3f0262cSandi/** 3171bd6bbdeSPatrick Brown * Replace one or more occurrences of a line in a file. 3181380fc45SAndreas Gohr * 319d93ba631SPatrick Brown * The default, when $maxlines is 0 is to delete all matching lines then append a single line. 320d93ba631SPatrick Brown * A regex that matches any part of the line will remove the entire line in this mode. 321d93ba631SPatrick Brown * Captures in $newline are not available. 3221bd6bbdeSPatrick Brown * 323d93ba631SPatrick Brown * Otherwise each line is matched and replaced individually, up to the first $maxlines lines 324d93ba631SPatrick Brown * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline. 325d93ba631SPatrick Brown * 326d93ba631SPatrick Brown * Be sure to include the trailing newline in $oldline when replacing entire lines. 327b158d625SSteven Danz * 328b158d625SSteven Danz * Uses gzip if extension is .gz 3291bd6bbdeSPatrick Brown * and bz2 if extension is .bz2 3308b06d178Schris * 33142ea7f44SGerrit Uitslag * @param string $file filename 3321bd6bbdeSPatrick Brown * @param string $oldline exact linematch to remove 3331bd6bbdeSPatrick Brown * @param string $newline new line to insert 33442ea7f44SGerrit Uitslag * @param bool $regex use regexp? 3351bd6bbdeSPatrick Brown * @param int $maxlines number of occurrences of the line to replace 336b158d625SSteven Danz * @return bool true on success 337aa659bbaSGerrit Uitslag * 338aa659bbaSGerrit Uitslag * @author Steven Danz <steven-danz@kc.rr.com> 339aa659bbaSGerrit Uitslag * @author Christopher Smith <chris@jalakai.co.uk> 340aa659bbaSGerrit Uitslag * @author Patrick Brown <ptbrown@whoopdedo.org> 341b158d625SSteven Danz */ 342d868eb89SAndreas Gohrfunction io_replaceInFile($file, $oldline, $newline, $regex = false, $maxlines = 0) 343d868eb89SAndreas Gohr{ 344dc4a4eb0SPatrick Brown if ((string)$oldline === '') { 345109ebc86SAndreas Gohr Logger::error('io_replaceInFile() $oldline parameter cannot be empty'); 346dc4a4eb0SPatrick Brown return false; 347dc4a4eb0SPatrick Brown } 348dc4a4eb0SPatrick Brown 34979e79377SAndreas Gohr if (!file_exists($file)) return true; 3501380fc45SAndreas Gohr 351b158d625SSteven Danz io_lock($file); 3521380fc45SAndreas Gohr 3531380fc45SAndreas Gohr // load into array 3546c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 35513c37900SAndreas Gohr if (!DOKU_HAS_GZIP) return false; 3561380fc45SAndreas Gohr $lines = gzfile($file); 3576c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 35813c37900SAndreas Gohr if (!DOKU_HAS_BZIP) return false; 359cfb71e37SPatrick Brown $lines = bzfile($file, true); 360b158d625SSteven Danz } else { 3611380fc45SAndreas Gohr $lines = file($file); 362b158d625SSteven Danz } 363b158d625SSteven Danz 3649a734b7aSChristopher Smith // make non-regexes into regexes 3653dfe7d64SChristopher Smith $pattern = $regex ? $oldline : '/^' . preg_quote($oldline, '/') . '$/'; 3669a734b7aSChristopher Smith $replace = $regex ? $newline : addcslashes($newline, '\$'); 3679a734b7aSChristopher Smith 3689a734b7aSChristopher Smith // remove matching lines 3696c000204SPatrick Brown if ($maxlines > 0) { 3706c000204SPatrick Brown $count = 0; 3719a734b7aSChristopher Smith $matched = 0; 372a93ad676SAndreas Gohr foreach ($lines as $i => $line) { 373a93ad676SAndreas Gohr if ($count >= $maxlines) break; 3749a734b7aSChristopher Smith // $matched will be set to 0|1 depending on whether pattern is matched and line replaced 3759a734b7aSChristopher Smith $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched); 376aa659bbaSGerrit Uitslag if ($matched) { 377aa659bbaSGerrit Uitslag $count++; 378aa659bbaSGerrit Uitslag } 3796c000204SPatrick Brown } 380e12c5ac7SChristopher Smith } elseif ($maxlines == 0) { 381e12c5ac7SChristopher Smith $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT); 382e12c5ac7SChristopher Smith if ((string)$newline !== '') { 3831bd6bbdeSPatrick Brown $lines[] = $newline; 3841bd6bbdeSPatrick Brown } 385e12c5ac7SChristopher Smith } else { 386e12c5ac7SChristopher Smith $lines = preg_replace($pattern, $replace, $lines); 387e12c5ac7SChristopher Smith } 3881bd6bbdeSPatrick Brown 3891380fc45SAndreas Gohr if (count($lines)) { 39024870174SAndreas Gohr if (!_io_saveFile($file, implode('', $lines), false)) { 391b158d625SSteven Danz msg("Removing content from $file failed", -1); 392fb7125eeSAndreas Gohr io_unlock($file); 393b158d625SSteven Danz return false; 394b158d625SSteven Danz } 395b158d625SSteven Danz } else { 396b158d625SSteven Danz @unlink($file); 397b158d625SSteven Danz } 398b158d625SSteven Danz 399b158d625SSteven Danz io_unlock($file); 400b158d625SSteven Danz return true; 401b158d625SSteven Danz} 402b158d625SSteven Danz 403b158d625SSteven Danz/** 4041bd6bbdeSPatrick Brown * Delete lines that match $badline from $file. 4051bd6bbdeSPatrick Brown * 4061bd6bbdeSPatrick Brown * Be sure to include the trailing newline in $badline 4071bd6bbdeSPatrick Brown * 4081bd6bbdeSPatrick Brown * @param string $file filename 4091bd6bbdeSPatrick Brown * @param string $badline exact linematch to remove 4101bd6bbdeSPatrick Brown * @param bool $regex use regexp? 4111bd6bbdeSPatrick Brown * @return bool true on success 412aa659bbaSGerrit Uitslag * 413aa659bbaSGerrit Uitslag * @author Patrick Brown <ptbrown@whoopdedo.org> 4141bd6bbdeSPatrick Brown */ 415d868eb89SAndreas Gohrfunction io_deleteFromFile($file, $badline, $regex = false) 416d868eb89SAndreas Gohr{ 4172fb31c4fSAndreas Gohr return io_replaceInFile($file, $badline, '', $regex, 0); 4181bd6bbdeSPatrick Brown} 4191bd6bbdeSPatrick Brown 4201bd6bbdeSPatrick Brown/** 42190eb8392Sandi * Tries to lock a file 42290eb8392Sandi * 42390eb8392Sandi * Locking is only done for io_savefile and uses directories 42490eb8392Sandi * inside $conf['lockdir'] 42590eb8392Sandi * 42690eb8392Sandi * It waits maximal 3 seconds for the lock, after this time 42790eb8392Sandi * the lock is assumed to be stale and the function goes on 42890eb8392Sandi * 42942ea7f44SGerrit Uitslag * @param string $file filename 430aa659bbaSGerrit Uitslag * 431aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 43290eb8392Sandi */ 433d868eb89SAndreas Gohrfunction io_lock($file) 434d868eb89SAndreas Gohr{ 43590eb8392Sandi global $conf; 43690eb8392Sandi 43790eb8392Sandi $lockDir = $conf['lockdir'] . '/' . md5($file); 43890eb8392Sandi @ignore_user_abort(1); 43990eb8392Sandi 44090eb8392Sandi $timeStart = time(); 44190eb8392Sandi do { 44290eb8392Sandi //waited longer than 3 seconds? -> stale lock 44390eb8392Sandi if ((time() - $timeStart) > 3) break; 444bd539124SAndreas Gohr $locked = @mkdir($lockDir); 44577b98903SAndreas Gohr if ($locked) { 446aa659bbaSGerrit Uitslag if ($conf['dperm']) { 447aa659bbaSGerrit Uitslag chmod($lockDir, $conf['dperm']); 448aa659bbaSGerrit Uitslag } 44977b98903SAndreas Gohr break; 45077b98903SAndreas Gohr } 45177b98903SAndreas Gohr usleep(50); 45290eb8392Sandi } while ($locked === false); 45390eb8392Sandi} 45490eb8392Sandi 45590eb8392Sandi/** 45690eb8392Sandi * Unlocks a file 45790eb8392Sandi * 45842ea7f44SGerrit Uitslag * @param string $file filename 459aa659bbaSGerrit Uitslag * 460aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 46190eb8392Sandi */ 462d868eb89SAndreas Gohrfunction io_unlock($file) 463d868eb89SAndreas Gohr{ 46490eb8392Sandi global $conf; 46590eb8392Sandi 46690eb8392Sandi $lockDir = $conf['lockdir'] . '/' . md5($file); 46790eb8392Sandi @rmdir($lockDir); 46890eb8392Sandi @ignore_user_abort(0); 46990eb8392Sandi} 47090eb8392Sandi 47190eb8392Sandi/** 472cc7d0c94SBen Coburn * Create missing namespace directories and send the IO_NAMESPACE_CREATED events 473cc7d0c94SBen Coburn * in the order of directory creation. (Parent directories first.) 474cc7d0c94SBen Coburn * 475cc7d0c94SBen Coburn * Event data: 476cc7d0c94SBen Coburn * $data[0] ns: The colon separated namespace path minus the trailing page name. 477cc7d0c94SBen Coburn * $data[1] ns_type: 'pages' or 'media' namespace tree. 478cc7d0c94SBen Coburn * 47942ea7f44SGerrit Uitslag * @param string $id page id 48042ea7f44SGerrit Uitslag * @param string $ns_type 'pages' or 'media' 481aa659bbaSGerrit Uitslag * 482aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 483cc7d0c94SBen Coburn */ 484d868eb89SAndreas Gohrfunction io_createNamespace($id, $ns_type = 'pages') 485d868eb89SAndreas Gohr{ 486cc7d0c94SBen Coburn // verify ns_type 48724870174SAndreas Gohr $types = ['pages' => 'wikiFN', 'media' => 'mediaFN']; 488cc7d0c94SBen Coburn if (!isset($types[$ns_type])) { 489cc7d0c94SBen Coburn trigger_error('Bad $ns_type parameter for io_createNamespace().'); 490cc7d0c94SBen Coburn return; 491cc7d0c94SBen Coburn } 492cc7d0c94SBen Coburn // make event list 49324870174SAndreas Gohr $missing = []; 494cc7d0c94SBen Coburn $ns_stack = explode(':', $id); 495cc7d0c94SBen Coburn $ns = $id; 496cc7d0c94SBen Coburn $tmp = dirname($file = call_user_func($types[$ns_type], $ns)); 49779e79377SAndreas Gohr while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) { 498cc7d0c94SBen Coburn array_pop($ns_stack); 499cc7d0c94SBen Coburn $ns = implode(':', $ns_stack); 500177d6836SAndreas Gohr if (strlen($ns) == 0) { 501d4f83172SAndreas Gohr break; 502d4f83172SAndreas Gohr } 503cc7d0c94SBen Coburn $missing[] = $ns; 504cc7d0c94SBen Coburn $tmp = dirname(call_user_func($types[$ns_type], $ns)); 505cc7d0c94SBen Coburn } 506cc7d0c94SBen Coburn // make directories 507cc7d0c94SBen Coburn io_makeFileDir($file); 508cc7d0c94SBen Coburn // send the events 509cc7d0c94SBen Coburn $missing = array_reverse($missing); // inside out 510cc7d0c94SBen Coburn foreach ($missing as $ns) { 51124870174SAndreas Gohr $data = [$ns, $ns_type]; 512cbb44eabSAndreas Gohr Event::createAndTrigger('IO_NAMESPACE_CREATED', $data); 513cc7d0c94SBen Coburn } 514cc7d0c94SBen Coburn} 515cc7d0c94SBen Coburn 516cc7d0c94SBen Coburn/** 517f3f0262cSandi * Create the directory needed for the given file 51815fae107Sandi * 51942ea7f44SGerrit Uitslag * @param string $file file name 520aa659bbaSGerrit Uitslag * 521aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 522f3f0262cSandi */ 523d868eb89SAndreas Gohrfunction io_makeFileDir($file) 524d868eb89SAndreas Gohr{ 525f3f0262cSandi $dir = dirname($file); 5260d8850c4SAndreas Gohr if (!@is_dir($dir)) { 527c347b097Ssplitbrain if (!io_mkdir_p($dir)) { 528c347b097Ssplitbrain msg("Creating directory $dir failed", -1); 529c347b097Ssplitbrain } 530f3f0262cSandi } 531f3f0262cSandi} 532f3f0262cSandi 533f3f0262cSandi/** 534f3f0262cSandi * Creates a directory hierachy. 535f3f0262cSandi * 536aa659bbaSGerrit Uitslag * @param string $target filename 537679f3774SGerrit Uitslag * @return bool 538aa659bbaSGerrit Uitslag * 53959752844SAnders Sandblad * @link http://php.net/manual/en/function.mkdir.php 540f3f0262cSandi * @author <saint@corenova.com> 5413dc3a5f1Sandi * @author Andreas Gohr <andi@splitbrain.org> 542f3f0262cSandi */ 543d868eb89SAndreas Gohrfunction io_mkdir_p($target) 544d868eb89SAndreas Gohr{ 5453dc3a5f1Sandi global $conf; 546679f3774SGerrit Uitslag if (@is_dir($target) || empty($target)) return true; // best case check first 547679f3774SGerrit Uitslag if (file_exists($target) && !is_dir($target)) return false; 5483dc3a5f1Sandi //recursion 5493dc3a5f1Sandi if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))) { 550bd539124SAndreas Gohr $ret = @mkdir($target); // crawl back up & create dir tree 551aa659bbaSGerrit Uitslag if ($ret && !empty($conf['dperm'])) { 552aa659bbaSGerrit Uitslag chmod($target, $conf['dperm']); 553aa659bbaSGerrit Uitslag } 55444881d27STroels Liebe Bentsen return $ret; 5553dc3a5f1Sandi } 556679f3774SGerrit Uitslag return false; 557f3f0262cSandi} 558f3f0262cSandi 559f3f0262cSandi/** 5604d47e8e3SAndreas Gohr * Recursively delete a directory 5614d47e8e3SAndreas Gohr * 5624d47e8e3SAndreas Gohr * @param string $path 5634d47e8e3SAndreas Gohr * @param bool $removefiles defaults to false which will delete empty directories only 5644d47e8e3SAndreas Gohr * @return bool 565aa659bbaSGerrit Uitslag * 566aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 5674d47e8e3SAndreas Gohr */ 568d868eb89SAndreas Gohrfunction io_rmdir($path, $removefiles = false) 569d868eb89SAndreas Gohr{ 5704d47e8e3SAndreas Gohr if (!is_string($path) || $path == "") return false; 571d8cf4dd4SAndreas Gohr if (!file_exists($path)) return true; // it's already gone or was never there, count as success 5724d47e8e3SAndreas Gohr 5734d47e8e3SAndreas Gohr if (is_dir($path) && !is_link($path)) { 57424870174SAndreas Gohr $dirs = []; 57524870174SAndreas Gohr $files = []; 5764d47e8e3SAndreas Gohr if (!$dh = @opendir($path)) return false; 5778426a3eeSAndreas Gohr while (false !== ($f = readdir($dh))) { 5784d47e8e3SAndreas Gohr if ($f == '..' || $f == '.') continue; 5794d47e8e3SAndreas Gohr 5804d47e8e3SAndreas Gohr // collect dirs and files first 5814d47e8e3SAndreas Gohr if (is_dir("$path/$f") && !is_link("$path/$f")) { 5824d47e8e3SAndreas Gohr $dirs[] = "$path/$f"; 5834d47e8e3SAndreas Gohr } elseif ($removefiles) { 5844d47e8e3SAndreas Gohr $files[] = "$path/$f"; 5854d47e8e3SAndreas Gohr } else { 5864d47e8e3SAndreas Gohr return false; // abort when non empty 5874d47e8e3SAndreas Gohr } 5884d47e8e3SAndreas Gohr } 5894d47e8e3SAndreas Gohr closedir($dh); 5904d47e8e3SAndreas Gohr // now traverse into directories first 5914d47e8e3SAndreas Gohr foreach ($dirs as $dir) { 5924d47e8e3SAndreas Gohr if (!io_rmdir($dir, $removefiles)) return false; // abort on any error 5934d47e8e3SAndreas Gohr } 5944d47e8e3SAndreas Gohr // now delete files 5954d47e8e3SAndreas Gohr foreach ($files as $file) { 5964d47e8e3SAndreas Gohr if (!@unlink($file)) return false; //abort on any error 5974d47e8e3SAndreas Gohr } 5984d47e8e3SAndreas Gohr // remove self 5994d47e8e3SAndreas Gohr return @rmdir($path); 6004d47e8e3SAndreas Gohr } elseif ($removefiles) { 6014d47e8e3SAndreas Gohr return @unlink($path); 6024d47e8e3SAndreas Gohr } 6034d47e8e3SAndreas Gohr return false; 6044d47e8e3SAndreas Gohr} 6054d47e8e3SAndreas Gohr 6064d47e8e3SAndreas Gohr/** 607de862555SMichael Klier * Creates a unique temporary directory and returns 608de862555SMichael Klier * its path. 609de862555SMichael Klier * 61042ea7f44SGerrit Uitslag * @return false|string path to new directory or false 611aa659bbaSGerrit Uitslag * @throws Exception 612aa659bbaSGerrit Uitslag * 613aa659bbaSGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 614de862555SMichael Klier */ 615d868eb89SAndreas Gohrfunction io_mktmpdir() 616d868eb89SAndreas Gohr{ 617de862555SMichael Klier global $conf; 618de862555SMichael Klier 619da1e1077SChris Smith $base = $conf['tmpdir']; 62024870174SAndreas Gohr $dir = md5(uniqid(random_int(0, mt_getrandmax()), true)); 621287f35bdSAndreas Gohr $tmpdir = $base . '/' . $dir; 622de862555SMichael Klier 623de862555SMichael Klier if (io_mkdir_p($tmpdir)) { 624aa659bbaSGerrit Uitslag return $tmpdir; 625de862555SMichael Klier } else { 626de862555SMichael Klier return false; 627de862555SMichael Klier } 628de862555SMichael Klier} 629de862555SMichael Klier 630de862555SMichael Klier/** 63173ccfcb9Schris * downloads a file from the net and saves it 63273ccfcb9Schris * 63373ccfcb9Schris * if $useAttachment is false, 63473ccfcb9Schris * - $file is the full filename to save the file, incl. path 63573ccfcb9Schris * - if successful will return true, false otherwise 636db959ae3SAndreas Gohr * 63773ccfcb9Schris * if $useAttachment is true, 63873ccfcb9Schris * - $file is the directory where the file should be saved 63973ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise 640b625487dSandi * 64142ea7f44SGerrit Uitslag * @param string $url url to download 64242ea7f44SGerrit Uitslag * @param string $file path to file or directory where to save 64364159a61SAndreas Gohr * @param bool $useAttachment true: try to use name of download, uses otherwise $defaultName 64464159a61SAndreas Gohr * false: uses $file as path to file 64542ea7f44SGerrit Uitslag * @param string $defaultName fallback for if using $useAttachment 64642ea7f44SGerrit Uitslag * @param int $maxSize maximum file size 64742ea7f44SGerrit Uitslag * @return bool|string if failed false, otherwise true or the name of the file in the given dir 648aa659bbaSGerrit Uitslag * 649aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 650aa659bbaSGerrit Uitslag * @author Chris Smith <chris@jalakai.co.uk> 651b625487dSandi */ 652d868eb89SAndreas Gohrfunction io_download($url, $file, $useAttachment = false, $defaultName = '', $maxSize = 2_097_152) 653d868eb89SAndreas Gohr{ 654ac9115b0STroels Liebe Bentsen global $conf; 6559b307a83SAndreas Gohr $http = new DokuHTTPClient(); 656847b8298SAndreas Gohr $http->max_bodysize = $maxSize; 6579b307a83SAndreas Gohr $http->timeout = 25; //max. 25 sec 658a5951419SAndreas Gohr $http->keep_alive = false; // we do single ops here, no need for keep-alive 6599b307a83SAndreas Gohr 6609b307a83SAndreas Gohr $data = $http->get($url); 6619b307a83SAndreas Gohr if (!$data) return false; 6629b307a83SAndreas Gohr 66373ccfcb9Schris $name = ''; 664cd2f903bSMichael Hamann if ($useAttachment) { 66573ccfcb9Schris if (isset($http->resp_headers['content-disposition'])) { 66673ccfcb9Schris $content_disposition = $http->resp_headers['content-disposition']; 66724870174SAndreas Gohr $match = []; 6687d34963bSAndreas Gohr if ( 6697d34963bSAndreas Gohr is_string($content_disposition) && 6707d34963bSAndreas Gohr preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match) 6717d34963bSAndreas Gohr ) { 67224870174SAndreas Gohr $name = PhpString::basename($match[1]); 67373ccfcb9Schris } 67473ccfcb9Schris } 67573ccfcb9Schris 67673ccfcb9Schris if (!$name) { 67773ccfcb9Schris if (!$defaultName) return false; 67873ccfcb9Schris $name = $defaultName; 67973ccfcb9Schris } 68073ccfcb9Schris 68124870174SAndreas Gohr $file .= $name; 68273ccfcb9Schris } 68373ccfcb9Schris 68479e79377SAndreas Gohr $fileexists = file_exists($file); 6859b307a83SAndreas Gohr $fp = @fopen($file, "w"); 686b625487dSandi if (!$fp) return false; 6879b307a83SAndreas Gohr fwrite($fp, $data); 688b625487dSandi fclose($fp); 689aa659bbaSGerrit Uitslag if (!$fileexists && $conf['fperm']) { 690aa659bbaSGerrit Uitslag chmod($file, $conf['fperm']); 691aa659bbaSGerrit Uitslag } 69273ccfcb9Schris if ($useAttachment) return $name; 693b625487dSandi return true; 694b625487dSandi} 695b625487dSandi 696b625487dSandi/** 697ac9115b0STroels Liebe Bentsen * Windows compatible rename 698bf5e5a5bSAndreas Gohr * 699bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows 700bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead 70142ea7f44SGerrit Uitslag * 70242ea7f44SGerrit Uitslag * @param string $from 70342ea7f44SGerrit Uitslag * @param string $to 70442ea7f44SGerrit Uitslag * @return bool succes or fail 705bf5e5a5bSAndreas Gohr */ 706d868eb89SAndreas Gohrfunction io_rename($from, $to) 707d868eb89SAndreas Gohr{ 708ac9115b0STroels Liebe Bentsen global $conf; 709bf5e5a5bSAndreas Gohr if (!@rename($from, $to)) { 710bf5e5a5bSAndreas Gohr if (@copy($from, $to)) { 711aa659bbaSGerrit Uitslag if ($conf['fperm']) { 712aa659bbaSGerrit Uitslag chmod($to, $conf['fperm']); 713aa659bbaSGerrit Uitslag } 714bf5e5a5bSAndreas Gohr @unlink($from); 715bf5e5a5bSAndreas Gohr return true; 716bf5e5a5bSAndreas Gohr } 717bf5e5a5bSAndreas Gohr return false; 718bf5e5a5bSAndreas Gohr } 719bf5e5a5bSAndreas Gohr return true; 720bf5e5a5bSAndreas Gohr} 721bf5e5a5bSAndreas Gohr 722420edfd6STom N Harris/** 723420edfd6STom N Harris * Runs an external command with input and output pipes. 724420edfd6STom N Harris * Returns the exit code from the process. 725420edfd6STom N Harris * 72642ea7f44SGerrit Uitslag * @param string $cmd 72742ea7f44SGerrit Uitslag * @param string $input input pipe 72842ea7f44SGerrit Uitslag * @param string $output output pipe 72942ea7f44SGerrit Uitslag * @return int exit code from process 730aa659bbaSGerrit Uitslag * 731aa659bbaSGerrit Uitslag * @author Tom N Harris <tnharris@whoopdedo.org> 732420edfd6STom N Harris */ 733d868eb89SAndreas Gohrfunction io_exec($cmd, $input, &$output) 734d868eb89SAndreas Gohr{ 73524870174SAndreas Gohr $descspec = [ 73624870174SAndreas Gohr 0 => ["pipe", "r"], 73724870174SAndreas Gohr 1 => ["pipe", "w"], 73824870174SAndreas Gohr 2 => ["pipe", "w"] 73924870174SAndreas Gohr ]; 7406c528220STom N Harris $ph = proc_open($cmd, $descspec, $pipes); 7416c528220STom N Harris if (!$ph) return -1; 7426c528220STom N Harris fclose($pipes[2]); // ignore stderr 7436c528220STom N Harris fwrite($pipes[0], $input); 7446c528220STom N Harris fclose($pipes[0]); 7456c528220STom N Harris $output = stream_get_contents($pipes[1]); 7466c528220STom N Harris fclose($pipes[1]); 7476c528220STom N Harris return proc_close($ph); 748f3f0262cSandi} 749f3f0262cSandi 7507421c3ccSAndreas Gohr/** 7517421c3ccSAndreas Gohr * Search a file for matching lines 7527421c3ccSAndreas Gohr * 7537421c3ccSAndreas Gohr * This is probably not faster than file()+preg_grep() but less 7547421c3ccSAndreas Gohr * memory intensive because not the whole file needs to be loaded 7557421c3ccSAndreas Gohr * at once. 7567421c3ccSAndreas Gohr * 7577421c3ccSAndreas Gohr * @param string $file The file to search 7587421c3ccSAndreas Gohr * @param string $pattern PCRE pattern 7597421c3ccSAndreas Gohr * @param int $max How many lines to return (0 for all) 760cd2f903bSMichael Hamann * @param bool $backref When true returns array with backreferences instead of lines 761cd2f903bSMichael Hamann * @return array matching lines or backref, false on error 762aa659bbaSGerrit Uitslag * 763aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 7647421c3ccSAndreas Gohr */ 765d868eb89SAndreas Gohrfunction io_grep($file, $pattern, $max = 0, $backref = false) 766d868eb89SAndreas Gohr{ 7677421c3ccSAndreas Gohr $fh = @fopen($file, 'r'); 7687421c3ccSAndreas Gohr if (!$fh) return false; 76924870174SAndreas Gohr $matches = []; 7707421c3ccSAndreas Gohr 7717421c3ccSAndreas Gohr $cnt = 0; 7727421c3ccSAndreas Gohr $line = ''; 7737421c3ccSAndreas Gohr while (!feof($fh)) { 7747421c3ccSAndreas Gohr $line .= fgets($fh, 4096); // read full line 7756c16a3a9Sfiwswe if (!str_ends_with($line, "\n")) continue; 7767421c3ccSAndreas Gohr 7777421c3ccSAndreas Gohr // check if line matches 7787421c3ccSAndreas Gohr if (preg_match($pattern, $line, $match)) { 7797421c3ccSAndreas Gohr if ($backref) { 7807421c3ccSAndreas Gohr $matches[] = $match; 7817421c3ccSAndreas Gohr } else { 7827421c3ccSAndreas Gohr $matches[] = $line; 7837421c3ccSAndreas Gohr } 7847421c3ccSAndreas Gohr $cnt++; 7857421c3ccSAndreas Gohr } 7867421c3ccSAndreas Gohr if ($max && $max == $cnt) break; 7877421c3ccSAndreas Gohr $line = ''; 7887421c3ccSAndreas Gohr } 7897421c3ccSAndreas Gohr fclose($fh); 7907421c3ccSAndreas Gohr return $matches; 7917421c3ccSAndreas Gohr} 7927421c3ccSAndreas Gohr 793f549be3dSGerrit Uitslag 794f549be3dSGerrit Uitslag/** 795f549be3dSGerrit Uitslag * Get size of contents of a file, for a compressed file the uncompressed size 796f549be3dSGerrit Uitslag * Warning: reading uncompressed size of content of bz-files requires uncompressing 797f549be3dSGerrit Uitslag * 798f549be3dSGerrit Uitslag * @param string $file filename path to file 799f549be3dSGerrit Uitslag * @return int size of file 800aa659bbaSGerrit Uitslag * 801aa659bbaSGerrit Uitslag * @author Gerrit Uitslag <klapinklapin@gmail.com> 802f549be3dSGerrit Uitslag */ 803d868eb89SAndreas Gohrfunction io_getSizeFile($file) 804d868eb89SAndreas Gohr{ 805f549be3dSGerrit Uitslag if (!file_exists($file)) return 0; 806f549be3dSGerrit Uitslag 8076c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 808f549be3dSGerrit Uitslag $fp = @fopen($file, "rb"); 809f549be3dSGerrit Uitslag if ($fp === false) return 0; 810f549be3dSGerrit Uitslag fseek($fp, -4, SEEK_END); 811f549be3dSGerrit Uitslag $buffer = fread($fp, 4); 812f549be3dSGerrit Uitslag fclose($fp); 813f549be3dSGerrit Uitslag $array = unpack("V", $buffer); 814f549be3dSGerrit Uitslag $uncompressedsize = end($array); 8156c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 816f549be3dSGerrit Uitslag if (!DOKU_HAS_BZIP) return 0; 817f549be3dSGerrit Uitslag $bz = bzopen($file, "r"); 818f549be3dSGerrit Uitslag if ($bz === false) return 0; 819f549be3dSGerrit Uitslag $uncompressedsize = 0; 820f549be3dSGerrit Uitslag while (!feof($bz)) { 821f549be3dSGerrit Uitslag //8192 seems to be the maximum buffersize? 822f549be3dSGerrit Uitslag $buffer = bzread($bz, 8192); 823f549be3dSGerrit Uitslag if (($buffer === false) || (bzerrno($bz) !== 0)) { 824f549be3dSGerrit Uitslag return 0; 825f549be3dSGerrit Uitslag } 826f549be3dSGerrit Uitslag $uncompressedsize += strlen($buffer); 827f549be3dSGerrit Uitslag } 828f549be3dSGerrit Uitslag } else { 829f549be3dSGerrit Uitslag $uncompressedsize = filesize($file); 830f549be3dSGerrit Uitslag } 831f549be3dSGerrit Uitslag 832f549be3dSGerrit Uitslag return $uncompressedsize; 833f549be3dSGerrit Uitslag} 834