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; 1236beb5edcSAndreas Gohr $ret = gzfile_get_contents($file); 1243b335c64SAndreas 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/** 1416beb5edcSAndreas Gohr * Returns the content of a .gz compressed file as string 1426beb5edcSAndreas Gohr * 1436beb5edcSAndreas Gohr * This reads the file in chunks and decompresses using inflate_* functions 1446beb5edcSAndreas Gohr * rather than gzfile(). This is necessary because PHP's zlib stream wrapper 1456beb5edcSAndreas Gohr * has a bug (php/php-src#21376) in PHP 8.5.3+ where gzfile() fails to detect 1466beb5edcSAndreas Gohr * corrupt gzip data and returns garbage instead of an error. 1476beb5edcSAndreas Gohr * 1486beb5edcSAndreas Gohr * Handles concatenated gzip streams as created by gzopen() in append mode. 1496beb5edcSAndreas Gohr * 1506beb5edcSAndreas Gohr * @param string $file filename 1516beb5edcSAndreas Gohr * @return string|false content or false on error 1526beb5edcSAndreas Gohr * 1536beb5edcSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1546beb5edcSAndreas Gohr */ 1556beb5edcSAndreas Gohrfunction gzfile_get_contents($file) 1566beb5edcSAndreas Gohr{ 1576beb5edcSAndreas Gohr $fh = @fopen($file, 'rb'); 1586beb5edcSAndreas Gohr if ($fh === false) return false; 1596beb5edcSAndreas Gohr 1606beb5edcSAndreas Gohr $ret = ''; 1616beb5edcSAndreas Gohr $leftover = ''; 1626beb5edcSAndreas Gohr while ($leftover !== '' || !feof($fh)) { 1636beb5edcSAndreas Gohr $ctx = inflate_init(ZLIB_ENCODING_GZIP); 1646beb5edcSAndreas Gohr 1656beb5edcSAndreas Gohr // decompress one gzip stream 1666beb5edcSAndreas Gohr while (true) { 1676beb5edcSAndreas Gohr if ($leftover !== '') { 1686beb5edcSAndreas Gohr $chunk = $leftover; 1696beb5edcSAndreas Gohr $leftover = ''; 1706beb5edcSAndreas Gohr } else { 1716beb5edcSAndreas Gohr $chunk = fread($fh, 8192); 1726beb5edcSAndreas Gohr if ($chunk === '' || $chunk === false) break; 1736beb5edcSAndreas Gohr } 1746beb5edcSAndreas Gohr $readBefore = inflate_get_read_len($ctx); 1756beb5edcSAndreas Gohr $decoded = @inflate_add($ctx, $chunk); 1766beb5edcSAndreas Gohr if ($decoded === false) { 1776beb5edcSAndreas Gohr fclose($fh); 1786beb5edcSAndreas Gohr return false; 1796beb5edcSAndreas Gohr } 1806beb5edcSAndreas Gohr $ret .= $decoded; 1816beb5edcSAndreas Gohr if (inflate_get_status($ctx) === ZLIB_STREAM_END) { 1826beb5edcSAndreas Gohr $consumed = inflate_get_read_len($ctx) - $readBefore; 1836beb5edcSAndreas Gohr $leftover = substr($chunk, $consumed); 1846beb5edcSAndreas Gohr break; 1856beb5edcSAndreas Gohr } 1866beb5edcSAndreas Gohr } 1876beb5edcSAndreas Gohr } 1886beb5edcSAndreas Gohr fclose($fh); 1896beb5edcSAndreas Gohr return $ret; 1906beb5edcSAndreas Gohr} 1916beb5edcSAndreas Gohr 1926beb5edcSAndreas Gohr/** 193ff3ed99fSmarcel * Returns the content of a .bz2 compressed file as string 19442ea7f44SGerrit Uitslag * 19542ea7f44SGerrit Uitslag * @param string $file filename 196cfb71e37SPatrick Brown * @param bool $array return array of lines 197cfb71e37SPatrick Brown * @return string|array|bool content or false on error 198aa659bbaSGerrit Uitslag * 199aa659bbaSGerrit Uitslag * @author marcel senf <marcel@rucksackreinigung.de> 200aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 201ff3ed99fSmarcel */ 202d868eb89SAndreas Gohrfunction bzfile($file, $array = false) 203d868eb89SAndreas Gohr{ 204ff3ed99fSmarcel $bz = bzopen($file, "r"); 205d387bf5eSAndreas Gohr if ($bz === false) return false; 206d387bf5eSAndreas Gohr 207aa659bbaSGerrit Uitslag if ($array) { 208aa659bbaSGerrit Uitslag $lines = []; 209aa659bbaSGerrit Uitslag } 210cd2f903bSMichael Hamann $str = ''; 211ff3ed99fSmarcel while (!feof($bz)) { 212ff3ed99fSmarcel //8192 seems to be the maximum buffersize? 213d387bf5eSAndreas Gohr $buffer = bzread($bz, 8192); 214d387bf5eSAndreas Gohr if (($buffer === false) || (bzerrno($bz) !== 0)) { 215d387bf5eSAndreas Gohr return false; 216d387bf5eSAndreas Gohr } 21724870174SAndreas Gohr $str .= $buffer; 218cfb71e37SPatrick Brown if ($array) { 219cfb71e37SPatrick Brown $pos = strpos($str, "\n"); 220cfb71e37SPatrick Brown while ($pos !== false) { 221cfb71e37SPatrick Brown $lines[] = substr($str, 0, $pos + 1); 222cfb71e37SPatrick Brown $str = substr($str, $pos + 1); 223cfb71e37SPatrick Brown $pos = strpos($str, "\n"); 224cfb71e37SPatrick Brown } 225cfb71e37SPatrick Brown } 226ff3ed99fSmarcel } 227ff3ed99fSmarcel bzclose($bz); 228cfb71e37SPatrick Brown if ($array) { 229aa659bbaSGerrit Uitslag if ($str !== '') { 230aa659bbaSGerrit Uitslag $lines[] = $str; 231aa659bbaSGerrit Uitslag } 232cfb71e37SPatrick Brown return $lines; 233cfb71e37SPatrick Brown } 234ff3ed99fSmarcel return $str; 235ff3ed99fSmarcel} 236ff3ed99fSmarcel 237f3f0262cSandi/** 238cc7d0c94SBen Coburn * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events. 239cc7d0c94SBen Coburn * 240cc7d0c94SBen Coburn * This generates an action event and delegates to io_saveFile(). 241cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit. 242cc7d0c94SBen Coburn * The file path should not be changed. 243cc7d0c94SBen Coburn * (The append parameter is set to false.) 244cc7d0c94SBen Coburn * 245cc7d0c94SBen Coburn * Event data: 246cc7d0c94SBen Coburn * $data[0] The raw arguments for io_saveFile as an array. 247cc7d0c94SBen Coburn * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 248cc7d0c94SBen Coburn * $data[2] page_name: The wiki page name. 249cc7d0c94SBen Coburn * $data[3] rev: The page revision, false for current wiki pages. 250cc7d0c94SBen Coburn * 25142ea7f44SGerrit Uitslag * @param string $file filename 25242ea7f44SGerrit Uitslag * @param string $content 25342ea7f44SGerrit Uitslag * @param string $id page id 254c826df86SAndreas Gohr * @param int|bool|string $rev timestamp of revision 25542ea7f44SGerrit Uitslag * @return bool 256aa659bbaSGerrit Uitslag * 257aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 258cc7d0c94SBen Coburn */ 259d868eb89SAndreas Gohrfunction io_writeWikiPage($file, $content, $id, $rev = false) 260d868eb89SAndreas Gohr{ 261177d6836SAndreas Gohr if (empty($rev)) { 262d4f83172SAndreas Gohr $rev = false; 263d4f83172SAndreas Gohr } 264177d6836SAndreas Gohr if ($rev === false) { 2654e2eb11eSGerrit Uitslag io_createNamespace($id); // create namespaces as needed 2664e2eb11eSGerrit Uitslag } 26724870174SAndreas Gohr $data = [[$file, $content, false], getNS($id), noNS($id), $rev]; 268cbb44eabSAndreas Gohr return Event::createAndTrigger('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false); 269cc7d0c94SBen Coburn} 270cc7d0c94SBen Coburn 271cc7d0c94SBen Coburn/** 272cc7d0c94SBen Coburn * Callback adapter for io_saveFile(). 27342ea7f44SGerrit Uitslag * 27442ea7f44SGerrit Uitslag * @param array $data event data 27542ea7f44SGerrit Uitslag * @return bool 276aa659bbaSGerrit Uitslag * 277aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 278cc7d0c94SBen Coburn */ 279d868eb89SAndreas Gohrfunction _io_writeWikiPage_action($data) 280d868eb89SAndreas Gohr{ 281cc7d0c94SBen Coburn if (is_array($data) && is_array($data[0]) && count($data[0]) === 3) { 28224870174SAndreas Gohr $ok = io_saveFile(...$data[0]); 283a4306b74SAndreas Gohr // for attic files make sure the file has the mtime of the revision 284a4306b74SAndreas Gohr if ($ok && is_int($data[3]) && $data[3] > 0) { 285a4306b74SAndreas Gohr @touch($data[0][0], $data[3]); 286a4306b74SAndreas Gohr } 287a4306b74SAndreas Gohr return $ok; 288cc7d0c94SBen Coburn } else { 289cc7d0c94SBen Coburn return false; //callback error 290cc7d0c94SBen Coburn } 291cc7d0c94SBen Coburn} 292cc7d0c94SBen Coburn 293cc7d0c94SBen Coburn/** 2941bd6bbdeSPatrick Brown * Internal function to save contents to a file. 2951bd6bbdeSPatrick Brown * 2961bd6bbdeSPatrick Brown * @param string $file filename path to file 2971bd6bbdeSPatrick Brown * @param string $content 2981bd6bbdeSPatrick Brown * @param bool $append 2991bd6bbdeSPatrick Brown * @return bool true on success, otherwise false 300aa659bbaSGerrit Uitslag * 301aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 3021bd6bbdeSPatrick Brown */ 303d868eb89SAndreas Gohrfunction _io_saveFile($file, $content, $append) 304d868eb89SAndreas Gohr{ 3051bd6bbdeSPatrick Brown global $conf; 3061bd6bbdeSPatrick Brown $mode = ($append) ? 'ab' : 'wb'; 3071bd6bbdeSPatrick Brown $fileexists = file_exists($file); 3081bd6bbdeSPatrick Brown 3096c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 31013c37900SAndreas Gohr if (!DOKU_HAS_GZIP) return false; 3111bd6bbdeSPatrick Brown $fh = @gzopen($file, $mode . '9'); 3121bd6bbdeSPatrick Brown if (!$fh) return false; 3131bd6bbdeSPatrick Brown gzwrite($fh, $content); 3141bd6bbdeSPatrick Brown gzclose($fh); 3156c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 31613c37900SAndreas Gohr if (!DOKU_HAS_BZIP) return false; 3171bd6bbdeSPatrick Brown if ($append) { 3181bd6bbdeSPatrick Brown $bzcontent = bzfile($file); 3191bd6bbdeSPatrick Brown if ($bzcontent === false) return false; 3201bd6bbdeSPatrick Brown $content = $bzcontent . $content; 3211bd6bbdeSPatrick Brown } 3221bd6bbdeSPatrick Brown $fh = @bzopen($file, 'w'); 3231bd6bbdeSPatrick Brown if (!$fh) return false; 3241bd6bbdeSPatrick Brown bzwrite($fh, $content); 3251bd6bbdeSPatrick Brown bzclose($fh); 3261bd6bbdeSPatrick Brown } else { 3271bd6bbdeSPatrick Brown $fh = @fopen($file, $mode); 3281bd6bbdeSPatrick Brown if (!$fh) return false; 3291bd6bbdeSPatrick Brown fwrite($fh, $content); 3301bd6bbdeSPatrick Brown fclose($fh); 3311bd6bbdeSPatrick Brown } 3321bd6bbdeSPatrick Brown 333aa659bbaSGerrit Uitslag if (!$fileexists && $conf['fperm']) { 334aa659bbaSGerrit Uitslag chmod($file, $conf['fperm']); 335aa659bbaSGerrit Uitslag } 3361bd6bbdeSPatrick Brown return true; 3371bd6bbdeSPatrick Brown} 3381bd6bbdeSPatrick Brown 3391bd6bbdeSPatrick Brown/** 34015fae107Sandi * Saves $content to $file. 341f3f0262cSandi * 3421380fc45SAndreas Gohr * If the third parameter is set to true the given content 3431380fc45SAndreas Gohr * will be appended. 3441380fc45SAndreas Gohr * 34515fae107Sandi * Uses gzip if extension is .gz 346ff3ed99fSmarcel * and bz2 if extension is .bz2 34715fae107Sandi * 34842ea7f44SGerrit Uitslag * @param string $file filename path to file 34942ea7f44SGerrit Uitslag * @param string $content 35042ea7f44SGerrit Uitslag * @param bool $append 35142ea7f44SGerrit Uitslag * @return bool true on success, otherwise false 352aa659bbaSGerrit Uitslag * 353aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 354f3f0262cSandi */ 355d868eb89SAndreas Gohrfunction io_saveFile($file, $content, $append = false) 356d868eb89SAndreas Gohr{ 357f3f0262cSandi io_makeFileDir($file); 35890eb8392Sandi io_lock($file); 3591bd6bbdeSPatrick Brown if (!_io_saveFile($file, $content, $append)) { 360f3f0262cSandi msg("Writing $file failed", -1); 361fb7125eeSAndreas Gohr io_unlock($file); 362f3f0262cSandi return false; 363f3f0262cSandi } 36490eb8392Sandi io_unlock($file); 365f3f0262cSandi return true; 366f3f0262cSandi} 367f3f0262cSandi 368f3f0262cSandi/** 3691bd6bbdeSPatrick Brown * Replace one or more occurrences of a line in a file. 3701380fc45SAndreas Gohr * 371d93ba631SPatrick Brown * The default, when $maxlines is 0 is to delete all matching lines then append a single line. 372d93ba631SPatrick Brown * A regex that matches any part of the line will remove the entire line in this mode. 373d93ba631SPatrick Brown * Captures in $newline are not available. 3741bd6bbdeSPatrick Brown * 375d93ba631SPatrick Brown * Otherwise each line is matched and replaced individually, up to the first $maxlines lines 376d93ba631SPatrick Brown * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline. 377d93ba631SPatrick Brown * 378d93ba631SPatrick Brown * Be sure to include the trailing newline in $oldline when replacing entire lines. 379b158d625SSteven Danz * 380b158d625SSteven Danz * Uses gzip if extension is .gz 3811bd6bbdeSPatrick Brown * and bz2 if extension is .bz2 3828b06d178Schris * 38342ea7f44SGerrit Uitslag * @param string $file filename 3841bd6bbdeSPatrick Brown * @param string $oldline exact linematch to remove 3851bd6bbdeSPatrick Brown * @param string $newline new line to insert 38642ea7f44SGerrit Uitslag * @param bool $regex use regexp? 3871bd6bbdeSPatrick Brown * @param int $maxlines number of occurrences of the line to replace 388b158d625SSteven Danz * @return bool true on success 389aa659bbaSGerrit Uitslag * 390aa659bbaSGerrit Uitslag * @author Steven Danz <steven-danz@kc.rr.com> 391aa659bbaSGerrit Uitslag * @author Christopher Smith <chris@jalakai.co.uk> 392aa659bbaSGerrit Uitslag * @author Patrick Brown <ptbrown@whoopdedo.org> 393b158d625SSteven Danz */ 394d868eb89SAndreas Gohrfunction io_replaceInFile($file, $oldline, $newline, $regex = false, $maxlines = 0) 395d868eb89SAndreas Gohr{ 396dc4a4eb0SPatrick Brown if ((string)$oldline === '') { 397109ebc86SAndreas Gohr Logger::error('io_replaceInFile() $oldline parameter cannot be empty'); 398dc4a4eb0SPatrick Brown return false; 399dc4a4eb0SPatrick Brown } 400dc4a4eb0SPatrick Brown 40179e79377SAndreas Gohr if (!file_exists($file)) return true; 4021380fc45SAndreas Gohr 403b158d625SSteven Danz io_lock($file); 4041380fc45SAndreas Gohr 4051380fc45SAndreas Gohr // load into array 4066c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 40713c37900SAndreas Gohr if (!DOKU_HAS_GZIP) return false; 4081380fc45SAndreas Gohr $lines = gzfile($file); 4096c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 41013c37900SAndreas Gohr if (!DOKU_HAS_BZIP) return false; 411cfb71e37SPatrick Brown $lines = bzfile($file, true); 412b158d625SSteven Danz } else { 4131380fc45SAndreas Gohr $lines = file($file); 414b158d625SSteven Danz } 415b158d625SSteven Danz 4169a734b7aSChristopher Smith // make non-regexes into regexes 4173dfe7d64SChristopher Smith $pattern = $regex ? $oldline : '/^' . preg_quote($oldline, '/') . '$/'; 4189a734b7aSChristopher Smith $replace = $regex ? $newline : addcslashes($newline, '\$'); 4199a734b7aSChristopher Smith 4209a734b7aSChristopher Smith // remove matching lines 4216c000204SPatrick Brown if ($maxlines > 0) { 4226c000204SPatrick Brown $count = 0; 4239a734b7aSChristopher Smith $matched = 0; 424a93ad676SAndreas Gohr foreach ($lines as $i => $line) { 425a93ad676SAndreas Gohr if ($count >= $maxlines) break; 4269a734b7aSChristopher Smith // $matched will be set to 0|1 depending on whether pattern is matched and line replaced 4279a734b7aSChristopher Smith $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched); 428aa659bbaSGerrit Uitslag if ($matched) { 429aa659bbaSGerrit Uitslag $count++; 430aa659bbaSGerrit Uitslag } 4316c000204SPatrick Brown } 432e12c5ac7SChristopher Smith } elseif ($maxlines == 0) { 433e12c5ac7SChristopher Smith $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT); 434e12c5ac7SChristopher Smith if ((string)$newline !== '') { 4351bd6bbdeSPatrick Brown $lines[] = $newline; 4361bd6bbdeSPatrick Brown } 437e12c5ac7SChristopher Smith } else { 438e12c5ac7SChristopher Smith $lines = preg_replace($pattern, $replace, $lines); 439e12c5ac7SChristopher Smith } 4401bd6bbdeSPatrick Brown 4411380fc45SAndreas Gohr if (count($lines)) { 44224870174SAndreas Gohr if (!_io_saveFile($file, implode('', $lines), false)) { 443b158d625SSteven Danz msg("Removing content from $file failed", -1); 444fb7125eeSAndreas Gohr io_unlock($file); 445b158d625SSteven Danz return false; 446b158d625SSteven Danz } 447b158d625SSteven Danz } else { 448b158d625SSteven Danz @unlink($file); 449b158d625SSteven Danz } 450b158d625SSteven Danz 451b158d625SSteven Danz io_unlock($file); 452b158d625SSteven Danz return true; 453b158d625SSteven Danz} 454b158d625SSteven Danz 455b158d625SSteven Danz/** 4561bd6bbdeSPatrick Brown * Delete lines that match $badline from $file. 4571bd6bbdeSPatrick Brown * 4581bd6bbdeSPatrick Brown * Be sure to include the trailing newline in $badline 4591bd6bbdeSPatrick Brown * 4601bd6bbdeSPatrick Brown * @param string $file filename 4611bd6bbdeSPatrick Brown * @param string $badline exact linematch to remove 4621bd6bbdeSPatrick Brown * @param bool $regex use regexp? 4631bd6bbdeSPatrick Brown * @return bool true on success 464aa659bbaSGerrit Uitslag * 465aa659bbaSGerrit Uitslag * @author Patrick Brown <ptbrown@whoopdedo.org> 4661bd6bbdeSPatrick Brown */ 467d868eb89SAndreas Gohrfunction io_deleteFromFile($file, $badline, $regex = false) 468d868eb89SAndreas Gohr{ 4692fb31c4fSAndreas Gohr return io_replaceInFile($file, $badline, '', $regex, 0); 4701bd6bbdeSPatrick Brown} 4711bd6bbdeSPatrick Brown 4721bd6bbdeSPatrick Brown/** 47390eb8392Sandi * Tries to lock a file 47490eb8392Sandi * 47590eb8392Sandi * Locking is only done for io_savefile and uses directories 47690eb8392Sandi * inside $conf['lockdir'] 47790eb8392Sandi * 47890eb8392Sandi * It waits maximal 3 seconds for the lock, after this time 47990eb8392Sandi * the lock is assumed to be stale and the function goes on 48090eb8392Sandi * 48142ea7f44SGerrit Uitslag * @param string $file filename 482aa659bbaSGerrit Uitslag * 483aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 48490eb8392Sandi */ 485d868eb89SAndreas Gohrfunction io_lock($file) 486d868eb89SAndreas Gohr{ 48790eb8392Sandi global $conf; 48890eb8392Sandi 48990eb8392Sandi $lockDir = $conf['lockdir'] . '/' . md5($file); 49090eb8392Sandi @ignore_user_abort(1); 49190eb8392Sandi 49290eb8392Sandi $timeStart = time(); 49390eb8392Sandi do { 49490eb8392Sandi //waited longer than 3 seconds? -> stale lock 49590eb8392Sandi if ((time() - $timeStart) > 3) break; 496bd539124SAndreas Gohr $locked = @mkdir($lockDir); 49777b98903SAndreas Gohr if ($locked) { 498aa659bbaSGerrit Uitslag if ($conf['dperm']) { 499aa659bbaSGerrit Uitslag chmod($lockDir, $conf['dperm']); 500aa659bbaSGerrit Uitslag } 50177b98903SAndreas Gohr break; 50277b98903SAndreas Gohr } 50377b98903SAndreas Gohr usleep(50); 50490eb8392Sandi } while ($locked === false); 50590eb8392Sandi} 50690eb8392Sandi 50790eb8392Sandi/** 50890eb8392Sandi * Unlocks a file 50990eb8392Sandi * 51042ea7f44SGerrit Uitslag * @param string $file filename 511aa659bbaSGerrit Uitslag * 512aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 51390eb8392Sandi */ 514d868eb89SAndreas Gohrfunction io_unlock($file) 515d868eb89SAndreas Gohr{ 51690eb8392Sandi global $conf; 51790eb8392Sandi 51890eb8392Sandi $lockDir = $conf['lockdir'] . '/' . md5($file); 51990eb8392Sandi @rmdir($lockDir); 52090eb8392Sandi @ignore_user_abort(0); 52190eb8392Sandi} 52290eb8392Sandi 52390eb8392Sandi/** 524cc7d0c94SBen Coburn * Create missing namespace directories and send the IO_NAMESPACE_CREATED events 525cc7d0c94SBen Coburn * in the order of directory creation. (Parent directories first.) 526cc7d0c94SBen Coburn * 527cc7d0c94SBen Coburn * Event data: 528cc7d0c94SBen Coburn * $data[0] ns: The colon separated namespace path minus the trailing page name. 529cc7d0c94SBen Coburn * $data[1] ns_type: 'pages' or 'media' namespace tree. 530cc7d0c94SBen Coburn * 53142ea7f44SGerrit Uitslag * @param string $id page id 53242ea7f44SGerrit Uitslag * @param string $ns_type 'pages' or 'media' 533aa659bbaSGerrit Uitslag * 534aa659bbaSGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 535cc7d0c94SBen Coburn */ 536d868eb89SAndreas Gohrfunction io_createNamespace($id, $ns_type = 'pages') 537d868eb89SAndreas Gohr{ 538cc7d0c94SBen Coburn // verify ns_type 53924870174SAndreas Gohr $types = ['pages' => 'wikiFN', 'media' => 'mediaFN']; 540cc7d0c94SBen Coburn if (!isset($types[$ns_type])) { 541*bfc167dbSAndreas Gohr throw new RuntimeException('Bad $ns_type parameter for io_createNamespace().'); 542cc7d0c94SBen Coburn } 543*bfc167dbSAndreas Gohr // refuse to create excessively deep hierarchies #4613 544*bfc167dbSAndreas Gohr if (substr_count($id, ':') >= 128) { 545*bfc167dbSAndreas Gohr throw new RuntimeException('Refusing to create nested namespace hierarchy deeper than 128 levels'); 546*bfc167dbSAndreas Gohr } 547*bfc167dbSAndreas Gohr 548cc7d0c94SBen Coburn // make event list 54924870174SAndreas Gohr $missing = []; 550cc7d0c94SBen Coburn $ns_stack = explode(':', $id); 551cc7d0c94SBen Coburn $ns = $id; 552cc7d0c94SBen Coburn $tmp = dirname($file = call_user_func($types[$ns_type], $ns)); 55379e79377SAndreas Gohr while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) { 554cc7d0c94SBen Coburn array_pop($ns_stack); 555cc7d0c94SBen Coburn $ns = implode(':', $ns_stack); 556177d6836SAndreas Gohr if (strlen($ns) == 0) { 557d4f83172SAndreas Gohr break; 558d4f83172SAndreas Gohr } 559cc7d0c94SBen Coburn $missing[] = $ns; 560cc7d0c94SBen Coburn $tmp = dirname(call_user_func($types[$ns_type], $ns)); 561cc7d0c94SBen Coburn } 562cc7d0c94SBen Coburn // make directories 563cc7d0c94SBen Coburn io_makeFileDir($file); 564cc7d0c94SBen Coburn // send the events 565cc7d0c94SBen Coburn $missing = array_reverse($missing); // inside out 566cc7d0c94SBen Coburn foreach ($missing as $ns) { 56724870174SAndreas Gohr $data = [$ns, $ns_type]; 568cbb44eabSAndreas Gohr Event::createAndTrigger('IO_NAMESPACE_CREATED', $data); 569cc7d0c94SBen Coburn } 570cc7d0c94SBen Coburn} 571cc7d0c94SBen Coburn 572cc7d0c94SBen Coburn/** 573f3f0262cSandi * Create the directory needed for the given file 57415fae107Sandi * 57542ea7f44SGerrit Uitslag * @param string $file file name 576aa659bbaSGerrit Uitslag * 577aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 578f3f0262cSandi */ 579d868eb89SAndreas Gohrfunction io_makeFileDir($file) 580d868eb89SAndreas Gohr{ 581f3f0262cSandi $dir = dirname($file); 5820d8850c4SAndreas Gohr if (!@is_dir($dir)) { 583c347b097Ssplitbrain if (!io_mkdir_p($dir)) { 584c347b097Ssplitbrain msg("Creating directory $dir failed", -1); 585c347b097Ssplitbrain } 586f3f0262cSandi } 587f3f0262cSandi} 588f3f0262cSandi 589f3f0262cSandi/** 590f3f0262cSandi * Creates a directory hierachy. 591f3f0262cSandi * 592aa659bbaSGerrit Uitslag * @param string $target filename 593679f3774SGerrit Uitslag * @return bool 594aa659bbaSGerrit Uitslag * 59559752844SAnders Sandblad * @link http://php.net/manual/en/function.mkdir.php 596f3f0262cSandi * @author <saint@corenova.com> 5973dc3a5f1Sandi * @author Andreas Gohr <andi@splitbrain.org> 598f3f0262cSandi */ 599d868eb89SAndreas Gohrfunction io_mkdir_p($target) 600d868eb89SAndreas Gohr{ 6013dc3a5f1Sandi global $conf; 602679f3774SGerrit Uitslag if (@is_dir($target) || empty($target)) return true; // best case check first 603679f3774SGerrit Uitslag if (file_exists($target) && !is_dir($target)) return false; 6043dc3a5f1Sandi //recursion 6053dc3a5f1Sandi if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))) { 606bd539124SAndreas Gohr $ret = @mkdir($target); // crawl back up & create dir tree 607aa659bbaSGerrit Uitslag if ($ret && !empty($conf['dperm'])) { 608aa659bbaSGerrit Uitslag chmod($target, $conf['dperm']); 609aa659bbaSGerrit Uitslag } 61044881d27STroels Liebe Bentsen return $ret; 6113dc3a5f1Sandi } 612679f3774SGerrit Uitslag return false; 613f3f0262cSandi} 614f3f0262cSandi 615f3f0262cSandi/** 6164d47e8e3SAndreas Gohr * Recursively delete a directory 6174d47e8e3SAndreas Gohr * 6184d47e8e3SAndreas Gohr * @param string $path 6194d47e8e3SAndreas Gohr * @param bool $removefiles defaults to false which will delete empty directories only 6204d47e8e3SAndreas Gohr * @return bool 621aa659bbaSGerrit Uitslag * 622aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 6234d47e8e3SAndreas Gohr */ 624d868eb89SAndreas Gohrfunction io_rmdir($path, $removefiles = false) 625d868eb89SAndreas Gohr{ 6264d47e8e3SAndreas Gohr if (!is_string($path) || $path == "") return false; 627d8cf4dd4SAndreas Gohr if (!file_exists($path)) return true; // it's already gone or was never there, count as success 6284d47e8e3SAndreas Gohr 6294d47e8e3SAndreas Gohr if (is_dir($path) && !is_link($path)) { 63024870174SAndreas Gohr $dirs = []; 63124870174SAndreas Gohr $files = []; 6324d47e8e3SAndreas Gohr if (!$dh = @opendir($path)) return false; 6338426a3eeSAndreas Gohr while (false !== ($f = readdir($dh))) { 6344d47e8e3SAndreas Gohr if ($f == '..' || $f == '.') continue; 6354d47e8e3SAndreas Gohr 6364d47e8e3SAndreas Gohr // collect dirs and files first 6374d47e8e3SAndreas Gohr if (is_dir("$path/$f") && !is_link("$path/$f")) { 6384d47e8e3SAndreas Gohr $dirs[] = "$path/$f"; 6394d47e8e3SAndreas Gohr } elseif ($removefiles) { 6404d47e8e3SAndreas Gohr $files[] = "$path/$f"; 6414d47e8e3SAndreas Gohr } else { 6424d47e8e3SAndreas Gohr return false; // abort when non empty 6434d47e8e3SAndreas Gohr } 6444d47e8e3SAndreas Gohr } 6454d47e8e3SAndreas Gohr closedir($dh); 6464d47e8e3SAndreas Gohr // now traverse into directories first 6474d47e8e3SAndreas Gohr foreach ($dirs as $dir) { 6484d47e8e3SAndreas Gohr if (!io_rmdir($dir, $removefiles)) return false; // abort on any error 6494d47e8e3SAndreas Gohr } 6504d47e8e3SAndreas Gohr // now delete files 6514d47e8e3SAndreas Gohr foreach ($files as $file) { 6524d47e8e3SAndreas Gohr if (!@unlink($file)) return false; //abort on any error 6534d47e8e3SAndreas Gohr } 6544d47e8e3SAndreas Gohr // remove self 6554d47e8e3SAndreas Gohr return @rmdir($path); 6564d47e8e3SAndreas Gohr } elseif ($removefiles) { 6574d47e8e3SAndreas Gohr return @unlink($path); 6584d47e8e3SAndreas Gohr } 6594d47e8e3SAndreas Gohr return false; 6604d47e8e3SAndreas Gohr} 6614d47e8e3SAndreas Gohr 6624d47e8e3SAndreas Gohr/** 663de862555SMichael Klier * Creates a unique temporary directory and returns 664de862555SMichael Klier * its path. 665de862555SMichael Klier * 66642ea7f44SGerrit Uitslag * @return false|string path to new directory or false 667aa659bbaSGerrit Uitslag * @throws Exception 668aa659bbaSGerrit Uitslag * 669aa659bbaSGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 670de862555SMichael Klier */ 671d868eb89SAndreas Gohrfunction io_mktmpdir() 672d868eb89SAndreas Gohr{ 673de862555SMichael Klier global $conf; 674de862555SMichael Klier 675da1e1077SChris Smith $base = $conf['tmpdir']; 67624870174SAndreas Gohr $dir = md5(uniqid(random_int(0, mt_getrandmax()), true)); 677287f35bdSAndreas Gohr $tmpdir = $base . '/' . $dir; 678de862555SMichael Klier 679de862555SMichael Klier if (io_mkdir_p($tmpdir)) { 680aa659bbaSGerrit Uitslag return $tmpdir; 681de862555SMichael Klier } else { 682de862555SMichael Klier return false; 683de862555SMichael Klier } 684de862555SMichael Klier} 685de862555SMichael Klier 686de862555SMichael Klier/** 68773ccfcb9Schris * downloads a file from the net and saves it 68873ccfcb9Schris * 68973ccfcb9Schris * if $useAttachment is false, 69073ccfcb9Schris * - $file is the full filename to save the file, incl. path 69173ccfcb9Schris * - if successful will return true, false otherwise 692db959ae3SAndreas Gohr * 69373ccfcb9Schris * if $useAttachment is true, 69473ccfcb9Schris * - $file is the directory where the file should be saved 69573ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise 696b625487dSandi * 69742ea7f44SGerrit Uitslag * @param string $url url to download 69842ea7f44SGerrit Uitslag * @param string $file path to file or directory where to save 69964159a61SAndreas Gohr * @param bool $useAttachment true: try to use name of download, uses otherwise $defaultName 70064159a61SAndreas Gohr * false: uses $file as path to file 70142ea7f44SGerrit Uitslag * @param string $defaultName fallback for if using $useAttachment 70242ea7f44SGerrit Uitslag * @param int $maxSize maximum file size 70342ea7f44SGerrit Uitslag * @return bool|string if failed false, otherwise true or the name of the file in the given dir 704aa659bbaSGerrit Uitslag * 705aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 706aa659bbaSGerrit Uitslag * @author Chris Smith <chris@jalakai.co.uk> 707b625487dSandi */ 708d868eb89SAndreas Gohrfunction io_download($url, $file, $useAttachment = false, $defaultName = '', $maxSize = 2_097_152) 709d868eb89SAndreas Gohr{ 710ac9115b0STroels Liebe Bentsen global $conf; 7119b307a83SAndreas Gohr $http = new DokuHTTPClient(); 712847b8298SAndreas Gohr $http->max_bodysize = $maxSize; 7139b307a83SAndreas Gohr $http->timeout = 25; //max. 25 sec 714a5951419SAndreas Gohr $http->keep_alive = false; // we do single ops here, no need for keep-alive 7159b307a83SAndreas Gohr 7169b307a83SAndreas Gohr $data = $http->get($url); 7179b307a83SAndreas Gohr if (!$data) return false; 7189b307a83SAndreas Gohr 71973ccfcb9Schris $name = ''; 720cd2f903bSMichael Hamann if ($useAttachment) { 72173ccfcb9Schris if (isset($http->resp_headers['content-disposition'])) { 72273ccfcb9Schris $content_disposition = $http->resp_headers['content-disposition']; 72324870174SAndreas Gohr $match = []; 7247d34963bSAndreas Gohr if ( 7257d34963bSAndreas Gohr is_string($content_disposition) && 7267d34963bSAndreas Gohr preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match) 7277d34963bSAndreas Gohr ) { 72824870174SAndreas Gohr $name = PhpString::basename($match[1]); 72973ccfcb9Schris } 73073ccfcb9Schris } 73173ccfcb9Schris 73273ccfcb9Schris if (!$name) { 73373ccfcb9Schris if (!$defaultName) return false; 73473ccfcb9Schris $name = $defaultName; 73573ccfcb9Schris } 73673ccfcb9Schris 73724870174SAndreas Gohr $file .= $name; 73873ccfcb9Schris } 73973ccfcb9Schris 74079e79377SAndreas Gohr $fileexists = file_exists($file); 7419b307a83SAndreas Gohr $fp = @fopen($file, "w"); 742b625487dSandi if (!$fp) return false; 7439b307a83SAndreas Gohr fwrite($fp, $data); 744b625487dSandi fclose($fp); 745aa659bbaSGerrit Uitslag if (!$fileexists && $conf['fperm']) { 746aa659bbaSGerrit Uitslag chmod($file, $conf['fperm']); 747aa659bbaSGerrit Uitslag } 74873ccfcb9Schris if ($useAttachment) return $name; 749b625487dSandi return true; 750b625487dSandi} 751b625487dSandi 752b625487dSandi/** 753ac9115b0STroels Liebe Bentsen * Windows compatible rename 754bf5e5a5bSAndreas Gohr * 755bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows 756bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead 75742ea7f44SGerrit Uitslag * 75842ea7f44SGerrit Uitslag * @param string $from 75942ea7f44SGerrit Uitslag * @param string $to 76042ea7f44SGerrit Uitslag * @return bool succes or fail 761bf5e5a5bSAndreas Gohr */ 762d868eb89SAndreas Gohrfunction io_rename($from, $to) 763d868eb89SAndreas Gohr{ 764ac9115b0STroels Liebe Bentsen global $conf; 765bf5e5a5bSAndreas Gohr if (!@rename($from, $to)) { 766bf5e5a5bSAndreas Gohr if (@copy($from, $to)) { 767aa659bbaSGerrit Uitslag if ($conf['fperm']) { 768aa659bbaSGerrit Uitslag chmod($to, $conf['fperm']); 769aa659bbaSGerrit Uitslag } 770bf5e5a5bSAndreas Gohr @unlink($from); 771bf5e5a5bSAndreas Gohr return true; 772bf5e5a5bSAndreas Gohr } 773bf5e5a5bSAndreas Gohr return false; 774bf5e5a5bSAndreas Gohr } 775bf5e5a5bSAndreas Gohr return true; 776bf5e5a5bSAndreas Gohr} 777bf5e5a5bSAndreas Gohr 778420edfd6STom N Harris/** 779420edfd6STom N Harris * Runs an external command with input and output pipes. 780420edfd6STom N Harris * Returns the exit code from the process. 781420edfd6STom N Harris * 78242ea7f44SGerrit Uitslag * @param string $cmd 78342ea7f44SGerrit Uitslag * @param string $input input pipe 78442ea7f44SGerrit Uitslag * @param string $output output pipe 78542ea7f44SGerrit Uitslag * @return int exit code from process 786aa659bbaSGerrit Uitslag * 787aa659bbaSGerrit Uitslag * @author Tom N Harris <tnharris@whoopdedo.org> 788420edfd6STom N Harris */ 789d868eb89SAndreas Gohrfunction io_exec($cmd, $input, &$output) 790d868eb89SAndreas Gohr{ 79124870174SAndreas Gohr $descspec = [ 79224870174SAndreas Gohr 0 => ["pipe", "r"], 79324870174SAndreas Gohr 1 => ["pipe", "w"], 79424870174SAndreas Gohr 2 => ["pipe", "w"] 79524870174SAndreas Gohr ]; 7966c528220STom N Harris $ph = proc_open($cmd, $descspec, $pipes); 7976c528220STom N Harris if (!$ph) return -1; 7986c528220STom N Harris fclose($pipes[2]); // ignore stderr 7996c528220STom N Harris fwrite($pipes[0], $input); 8006c528220STom N Harris fclose($pipes[0]); 8016c528220STom N Harris $output = stream_get_contents($pipes[1]); 8026c528220STom N Harris fclose($pipes[1]); 8036c528220STom N Harris return proc_close($ph); 804f3f0262cSandi} 805f3f0262cSandi 8067421c3ccSAndreas Gohr/** 8077421c3ccSAndreas Gohr * Search a file for matching lines 8087421c3ccSAndreas Gohr * 8097421c3ccSAndreas Gohr * This is probably not faster than file()+preg_grep() but less 8107421c3ccSAndreas Gohr * memory intensive because not the whole file needs to be loaded 8117421c3ccSAndreas Gohr * at once. 8127421c3ccSAndreas Gohr * 8137421c3ccSAndreas Gohr * @param string $file The file to search 8147421c3ccSAndreas Gohr * @param string $pattern PCRE pattern 8157421c3ccSAndreas Gohr * @param int $max How many lines to return (0 for all) 816cd2f903bSMichael Hamann * @param bool $backref When true returns array with backreferences instead of lines 817cd2f903bSMichael Hamann * @return array matching lines or backref, false on error 818aa659bbaSGerrit Uitslag * 819aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 8207421c3ccSAndreas Gohr */ 821d868eb89SAndreas Gohrfunction io_grep($file, $pattern, $max = 0, $backref = false) 822d868eb89SAndreas Gohr{ 8237421c3ccSAndreas Gohr $fh = @fopen($file, 'r'); 8247421c3ccSAndreas Gohr if (!$fh) return false; 82524870174SAndreas Gohr $matches = []; 8267421c3ccSAndreas Gohr 8277421c3ccSAndreas Gohr $cnt = 0; 8287421c3ccSAndreas Gohr $line = ''; 8297421c3ccSAndreas Gohr while (!feof($fh)) { 8307421c3ccSAndreas Gohr $line .= fgets($fh, 4096); // read full line 8316c16a3a9Sfiwswe if (!str_ends_with($line, "\n")) continue; 8327421c3ccSAndreas Gohr 8337421c3ccSAndreas Gohr // check if line matches 8347421c3ccSAndreas Gohr if (preg_match($pattern, $line, $match)) { 8357421c3ccSAndreas Gohr if ($backref) { 8367421c3ccSAndreas Gohr $matches[] = $match; 8377421c3ccSAndreas Gohr } else { 8387421c3ccSAndreas Gohr $matches[] = $line; 8397421c3ccSAndreas Gohr } 8407421c3ccSAndreas Gohr $cnt++; 8417421c3ccSAndreas Gohr } 8427421c3ccSAndreas Gohr if ($max && $max == $cnt) break; 8437421c3ccSAndreas Gohr $line = ''; 8447421c3ccSAndreas Gohr } 8457421c3ccSAndreas Gohr fclose($fh); 8467421c3ccSAndreas Gohr return $matches; 8477421c3ccSAndreas Gohr} 8487421c3ccSAndreas Gohr 849f549be3dSGerrit Uitslag 850f549be3dSGerrit Uitslag/** 851f549be3dSGerrit Uitslag * Get size of contents of a file, for a compressed file the uncompressed size 852f549be3dSGerrit Uitslag * Warning: reading uncompressed size of content of bz-files requires uncompressing 853f549be3dSGerrit Uitslag * 854f549be3dSGerrit Uitslag * @param string $file filename path to file 855f549be3dSGerrit Uitslag * @return int size of file 856aa659bbaSGerrit Uitslag * 857aa659bbaSGerrit Uitslag * @author Gerrit Uitslag <klapinklapin@gmail.com> 858f549be3dSGerrit Uitslag */ 859d868eb89SAndreas Gohrfunction io_getSizeFile($file) 860d868eb89SAndreas Gohr{ 861f549be3dSGerrit Uitslag if (!file_exists($file)) return 0; 862f549be3dSGerrit Uitslag 8636c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 864f549be3dSGerrit Uitslag $fp = @fopen($file, "rb"); 865f549be3dSGerrit Uitslag if ($fp === false) return 0; 866f549be3dSGerrit Uitslag fseek($fp, -4, SEEK_END); 867f549be3dSGerrit Uitslag $buffer = fread($fp, 4); 868f549be3dSGerrit Uitslag fclose($fp); 869f549be3dSGerrit Uitslag $array = unpack("V", $buffer); 870f549be3dSGerrit Uitslag $uncompressedsize = end($array); 8716c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 872f549be3dSGerrit Uitslag if (!DOKU_HAS_BZIP) return 0; 873f549be3dSGerrit Uitslag $bz = bzopen($file, "r"); 874f549be3dSGerrit Uitslag if ($bz === false) return 0; 875f549be3dSGerrit Uitslag $uncompressedsize = 0; 876f549be3dSGerrit Uitslag while (!feof($bz)) { 877f549be3dSGerrit Uitslag //8192 seems to be the maximum buffersize? 878f549be3dSGerrit Uitslag $buffer = bzread($bz, 8192); 879f549be3dSGerrit Uitslag if (($buffer === false) || (bzerrno($bz) !== 0)) { 880f549be3dSGerrit Uitslag return 0; 881f549be3dSGerrit Uitslag } 882f549be3dSGerrit Uitslag $uncompressedsize += strlen($buffer); 883f549be3dSGerrit Uitslag } 884f549be3dSGerrit Uitslag } else { 885f549be3dSGerrit Uitslag $uncompressedsize = filesize($file); 886f549be3dSGerrit Uitslag } 887f549be3dSGerrit Uitslag 888f549be3dSGerrit Uitslag return $uncompressedsize; 889f549be3dSGerrit Uitslag} 890