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*6beb5edcSAndreas 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/** 141*6beb5edcSAndreas Gohr * Returns the content of a .gz compressed file as string 142*6beb5edcSAndreas Gohr * 143*6beb5edcSAndreas Gohr * This reads the file in chunks and decompresses using inflate_* functions 144*6beb5edcSAndreas Gohr * rather than gzfile(). This is necessary because PHP's zlib stream wrapper 145*6beb5edcSAndreas Gohr * has a bug (php/php-src#21376) in PHP 8.5.3+ where gzfile() fails to detect 146*6beb5edcSAndreas Gohr * corrupt gzip data and returns garbage instead of an error. 147*6beb5edcSAndreas Gohr * 148*6beb5edcSAndreas Gohr * Handles concatenated gzip streams as created by gzopen() in append mode. 149*6beb5edcSAndreas Gohr * 150*6beb5edcSAndreas Gohr * @param string $file filename 151*6beb5edcSAndreas Gohr * @return string|false content or false on error 152*6beb5edcSAndreas Gohr * 153*6beb5edcSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 154*6beb5edcSAndreas Gohr */ 155*6beb5edcSAndreas Gohrfunction gzfile_get_contents($file) 156*6beb5edcSAndreas Gohr{ 157*6beb5edcSAndreas Gohr $fh = @fopen($file, 'rb'); 158*6beb5edcSAndreas Gohr if ($fh === false) return false; 159*6beb5edcSAndreas Gohr 160*6beb5edcSAndreas Gohr $ret = ''; 161*6beb5edcSAndreas Gohr $leftover = ''; 162*6beb5edcSAndreas Gohr while ($leftover !== '' || !feof($fh)) { 163*6beb5edcSAndreas Gohr $ctx = inflate_init(ZLIB_ENCODING_GZIP); 164*6beb5edcSAndreas Gohr 165*6beb5edcSAndreas Gohr // decompress one gzip stream 166*6beb5edcSAndreas Gohr while (true) { 167*6beb5edcSAndreas Gohr if ($leftover !== '') { 168*6beb5edcSAndreas Gohr $chunk = $leftover; 169*6beb5edcSAndreas Gohr $leftover = ''; 170*6beb5edcSAndreas Gohr } else { 171*6beb5edcSAndreas Gohr $chunk = fread($fh, 8192); 172*6beb5edcSAndreas Gohr if ($chunk === '' || $chunk === false) break; 173*6beb5edcSAndreas Gohr } 174*6beb5edcSAndreas Gohr $readBefore = inflate_get_read_len($ctx); 175*6beb5edcSAndreas Gohr $decoded = @inflate_add($ctx, $chunk); 176*6beb5edcSAndreas Gohr if ($decoded === false) { 177*6beb5edcSAndreas Gohr fclose($fh); 178*6beb5edcSAndreas Gohr return false; 179*6beb5edcSAndreas Gohr } 180*6beb5edcSAndreas Gohr $ret .= $decoded; 181*6beb5edcSAndreas Gohr if (inflate_get_status($ctx) === ZLIB_STREAM_END) { 182*6beb5edcSAndreas Gohr $consumed = inflate_get_read_len($ctx) - $readBefore; 183*6beb5edcSAndreas Gohr $leftover = substr($chunk, $consumed); 184*6beb5edcSAndreas Gohr break; 185*6beb5edcSAndreas Gohr } 186*6beb5edcSAndreas Gohr } 187*6beb5edcSAndreas Gohr } 188*6beb5edcSAndreas Gohr fclose($fh); 189*6beb5edcSAndreas Gohr return $ret; 190*6beb5edcSAndreas Gohr} 191*6beb5edcSAndreas Gohr 192*6beb5edcSAndreas 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])) { 541cc7d0c94SBen Coburn trigger_error('Bad $ns_type parameter for io_createNamespace().'); 542cc7d0c94SBen Coburn return; 543cc7d0c94SBen Coburn } 544cc7d0c94SBen Coburn // make event list 54524870174SAndreas Gohr $missing = []; 546cc7d0c94SBen Coburn $ns_stack = explode(':', $id); 547cc7d0c94SBen Coburn $ns = $id; 548cc7d0c94SBen Coburn $tmp = dirname($file = call_user_func($types[$ns_type], $ns)); 54979e79377SAndreas Gohr while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) { 550cc7d0c94SBen Coburn array_pop($ns_stack); 551cc7d0c94SBen Coburn $ns = implode(':', $ns_stack); 552177d6836SAndreas Gohr if (strlen($ns) == 0) { 553d4f83172SAndreas Gohr break; 554d4f83172SAndreas Gohr } 555cc7d0c94SBen Coburn $missing[] = $ns; 556cc7d0c94SBen Coburn $tmp = dirname(call_user_func($types[$ns_type], $ns)); 557cc7d0c94SBen Coburn } 558cc7d0c94SBen Coburn // make directories 559cc7d0c94SBen Coburn io_makeFileDir($file); 560cc7d0c94SBen Coburn // send the events 561cc7d0c94SBen Coburn $missing = array_reverse($missing); // inside out 562cc7d0c94SBen Coburn foreach ($missing as $ns) { 56324870174SAndreas Gohr $data = [$ns, $ns_type]; 564cbb44eabSAndreas Gohr Event::createAndTrigger('IO_NAMESPACE_CREATED', $data); 565cc7d0c94SBen Coburn } 566cc7d0c94SBen Coburn} 567cc7d0c94SBen Coburn 568cc7d0c94SBen Coburn/** 569f3f0262cSandi * Create the directory needed for the given file 57015fae107Sandi * 57142ea7f44SGerrit Uitslag * @param string $file file name 572aa659bbaSGerrit Uitslag * 573aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 574f3f0262cSandi */ 575d868eb89SAndreas Gohrfunction io_makeFileDir($file) 576d868eb89SAndreas Gohr{ 577f3f0262cSandi $dir = dirname($file); 5780d8850c4SAndreas Gohr if (!@is_dir($dir)) { 579c347b097Ssplitbrain if (!io_mkdir_p($dir)) { 580c347b097Ssplitbrain msg("Creating directory $dir failed", -1); 581c347b097Ssplitbrain } 582f3f0262cSandi } 583f3f0262cSandi} 584f3f0262cSandi 585f3f0262cSandi/** 586f3f0262cSandi * Creates a directory hierachy. 587f3f0262cSandi * 588aa659bbaSGerrit Uitslag * @param string $target filename 589679f3774SGerrit Uitslag * @return bool 590aa659bbaSGerrit Uitslag * 59159752844SAnders Sandblad * @link http://php.net/manual/en/function.mkdir.php 592f3f0262cSandi * @author <saint@corenova.com> 5933dc3a5f1Sandi * @author Andreas Gohr <andi@splitbrain.org> 594f3f0262cSandi */ 595d868eb89SAndreas Gohrfunction io_mkdir_p($target) 596d868eb89SAndreas Gohr{ 5973dc3a5f1Sandi global $conf; 598679f3774SGerrit Uitslag if (@is_dir($target) || empty($target)) return true; // best case check first 599679f3774SGerrit Uitslag if (file_exists($target) && !is_dir($target)) return false; 6003dc3a5f1Sandi //recursion 6013dc3a5f1Sandi if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))) { 602bd539124SAndreas Gohr $ret = @mkdir($target); // crawl back up & create dir tree 603aa659bbaSGerrit Uitslag if ($ret && !empty($conf['dperm'])) { 604aa659bbaSGerrit Uitslag chmod($target, $conf['dperm']); 605aa659bbaSGerrit Uitslag } 60644881d27STroels Liebe Bentsen return $ret; 6073dc3a5f1Sandi } 608679f3774SGerrit Uitslag return false; 609f3f0262cSandi} 610f3f0262cSandi 611f3f0262cSandi/** 6124d47e8e3SAndreas Gohr * Recursively delete a directory 6134d47e8e3SAndreas Gohr * 6144d47e8e3SAndreas Gohr * @param string $path 6154d47e8e3SAndreas Gohr * @param bool $removefiles defaults to false which will delete empty directories only 6164d47e8e3SAndreas Gohr * @return bool 617aa659bbaSGerrit Uitslag * 618aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 6194d47e8e3SAndreas Gohr */ 620d868eb89SAndreas Gohrfunction io_rmdir($path, $removefiles = false) 621d868eb89SAndreas Gohr{ 6224d47e8e3SAndreas Gohr if (!is_string($path) || $path == "") return false; 623d8cf4dd4SAndreas Gohr if (!file_exists($path)) return true; // it's already gone or was never there, count as success 6244d47e8e3SAndreas Gohr 6254d47e8e3SAndreas Gohr if (is_dir($path) && !is_link($path)) { 62624870174SAndreas Gohr $dirs = []; 62724870174SAndreas Gohr $files = []; 6284d47e8e3SAndreas Gohr if (!$dh = @opendir($path)) return false; 6298426a3eeSAndreas Gohr while (false !== ($f = readdir($dh))) { 6304d47e8e3SAndreas Gohr if ($f == '..' || $f == '.') continue; 6314d47e8e3SAndreas Gohr 6324d47e8e3SAndreas Gohr // collect dirs and files first 6334d47e8e3SAndreas Gohr if (is_dir("$path/$f") && !is_link("$path/$f")) { 6344d47e8e3SAndreas Gohr $dirs[] = "$path/$f"; 6354d47e8e3SAndreas Gohr } elseif ($removefiles) { 6364d47e8e3SAndreas Gohr $files[] = "$path/$f"; 6374d47e8e3SAndreas Gohr } else { 6384d47e8e3SAndreas Gohr return false; // abort when non empty 6394d47e8e3SAndreas Gohr } 6404d47e8e3SAndreas Gohr } 6414d47e8e3SAndreas Gohr closedir($dh); 6424d47e8e3SAndreas Gohr // now traverse into directories first 6434d47e8e3SAndreas Gohr foreach ($dirs as $dir) { 6444d47e8e3SAndreas Gohr if (!io_rmdir($dir, $removefiles)) return false; // abort on any error 6454d47e8e3SAndreas Gohr } 6464d47e8e3SAndreas Gohr // now delete files 6474d47e8e3SAndreas Gohr foreach ($files as $file) { 6484d47e8e3SAndreas Gohr if (!@unlink($file)) return false; //abort on any error 6494d47e8e3SAndreas Gohr } 6504d47e8e3SAndreas Gohr // remove self 6514d47e8e3SAndreas Gohr return @rmdir($path); 6524d47e8e3SAndreas Gohr } elseif ($removefiles) { 6534d47e8e3SAndreas Gohr return @unlink($path); 6544d47e8e3SAndreas Gohr } 6554d47e8e3SAndreas Gohr return false; 6564d47e8e3SAndreas Gohr} 6574d47e8e3SAndreas Gohr 6584d47e8e3SAndreas Gohr/** 659de862555SMichael Klier * Creates a unique temporary directory and returns 660de862555SMichael Klier * its path. 661de862555SMichael Klier * 66242ea7f44SGerrit Uitslag * @return false|string path to new directory or false 663aa659bbaSGerrit Uitslag * @throws Exception 664aa659bbaSGerrit Uitslag * 665aa659bbaSGerrit Uitslag * @author Michael Klier <chi@chimeric.de> 666de862555SMichael Klier */ 667d868eb89SAndreas Gohrfunction io_mktmpdir() 668d868eb89SAndreas Gohr{ 669de862555SMichael Klier global $conf; 670de862555SMichael Klier 671da1e1077SChris Smith $base = $conf['tmpdir']; 67224870174SAndreas Gohr $dir = md5(uniqid(random_int(0, mt_getrandmax()), true)); 673287f35bdSAndreas Gohr $tmpdir = $base . '/' . $dir; 674de862555SMichael Klier 675de862555SMichael Klier if (io_mkdir_p($tmpdir)) { 676aa659bbaSGerrit Uitslag return $tmpdir; 677de862555SMichael Klier } else { 678de862555SMichael Klier return false; 679de862555SMichael Klier } 680de862555SMichael Klier} 681de862555SMichael Klier 682de862555SMichael Klier/** 68373ccfcb9Schris * downloads a file from the net and saves it 68473ccfcb9Schris * 68573ccfcb9Schris * if $useAttachment is false, 68673ccfcb9Schris * - $file is the full filename to save the file, incl. path 68773ccfcb9Schris * - if successful will return true, false otherwise 688db959ae3SAndreas Gohr * 68973ccfcb9Schris * if $useAttachment is true, 69073ccfcb9Schris * - $file is the directory where the file should be saved 69173ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise 692b625487dSandi * 69342ea7f44SGerrit Uitslag * @param string $url url to download 69442ea7f44SGerrit Uitslag * @param string $file path to file or directory where to save 69564159a61SAndreas Gohr * @param bool $useAttachment true: try to use name of download, uses otherwise $defaultName 69664159a61SAndreas Gohr * false: uses $file as path to file 69742ea7f44SGerrit Uitslag * @param string $defaultName fallback for if using $useAttachment 69842ea7f44SGerrit Uitslag * @param int $maxSize maximum file size 69942ea7f44SGerrit Uitslag * @return bool|string if failed false, otherwise true or the name of the file in the given dir 700aa659bbaSGerrit Uitslag * 701aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 702aa659bbaSGerrit Uitslag * @author Chris Smith <chris@jalakai.co.uk> 703b625487dSandi */ 704d868eb89SAndreas Gohrfunction io_download($url, $file, $useAttachment = false, $defaultName = '', $maxSize = 2_097_152) 705d868eb89SAndreas Gohr{ 706ac9115b0STroels Liebe Bentsen global $conf; 7079b307a83SAndreas Gohr $http = new DokuHTTPClient(); 708847b8298SAndreas Gohr $http->max_bodysize = $maxSize; 7099b307a83SAndreas Gohr $http->timeout = 25; //max. 25 sec 710a5951419SAndreas Gohr $http->keep_alive = false; // we do single ops here, no need for keep-alive 7119b307a83SAndreas Gohr 7129b307a83SAndreas Gohr $data = $http->get($url); 7139b307a83SAndreas Gohr if (!$data) return false; 7149b307a83SAndreas Gohr 71573ccfcb9Schris $name = ''; 716cd2f903bSMichael Hamann if ($useAttachment) { 71773ccfcb9Schris if (isset($http->resp_headers['content-disposition'])) { 71873ccfcb9Schris $content_disposition = $http->resp_headers['content-disposition']; 71924870174SAndreas Gohr $match = []; 7207d34963bSAndreas Gohr if ( 7217d34963bSAndreas Gohr is_string($content_disposition) && 7227d34963bSAndreas Gohr preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match) 7237d34963bSAndreas Gohr ) { 72424870174SAndreas Gohr $name = PhpString::basename($match[1]); 72573ccfcb9Schris } 72673ccfcb9Schris } 72773ccfcb9Schris 72873ccfcb9Schris if (!$name) { 72973ccfcb9Schris if (!$defaultName) return false; 73073ccfcb9Schris $name = $defaultName; 73173ccfcb9Schris } 73273ccfcb9Schris 73324870174SAndreas Gohr $file .= $name; 73473ccfcb9Schris } 73573ccfcb9Schris 73679e79377SAndreas Gohr $fileexists = file_exists($file); 7379b307a83SAndreas Gohr $fp = @fopen($file, "w"); 738b625487dSandi if (!$fp) return false; 7399b307a83SAndreas Gohr fwrite($fp, $data); 740b625487dSandi fclose($fp); 741aa659bbaSGerrit Uitslag if (!$fileexists && $conf['fperm']) { 742aa659bbaSGerrit Uitslag chmod($file, $conf['fperm']); 743aa659bbaSGerrit Uitslag } 74473ccfcb9Schris if ($useAttachment) return $name; 745b625487dSandi return true; 746b625487dSandi} 747b625487dSandi 748b625487dSandi/** 749ac9115b0STroels Liebe Bentsen * Windows compatible rename 750bf5e5a5bSAndreas Gohr * 751bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows 752bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead 75342ea7f44SGerrit Uitslag * 75442ea7f44SGerrit Uitslag * @param string $from 75542ea7f44SGerrit Uitslag * @param string $to 75642ea7f44SGerrit Uitslag * @return bool succes or fail 757bf5e5a5bSAndreas Gohr */ 758d868eb89SAndreas Gohrfunction io_rename($from, $to) 759d868eb89SAndreas Gohr{ 760ac9115b0STroels Liebe Bentsen global $conf; 761bf5e5a5bSAndreas Gohr if (!@rename($from, $to)) { 762bf5e5a5bSAndreas Gohr if (@copy($from, $to)) { 763aa659bbaSGerrit Uitslag if ($conf['fperm']) { 764aa659bbaSGerrit Uitslag chmod($to, $conf['fperm']); 765aa659bbaSGerrit Uitslag } 766bf5e5a5bSAndreas Gohr @unlink($from); 767bf5e5a5bSAndreas Gohr return true; 768bf5e5a5bSAndreas Gohr } 769bf5e5a5bSAndreas Gohr return false; 770bf5e5a5bSAndreas Gohr } 771bf5e5a5bSAndreas Gohr return true; 772bf5e5a5bSAndreas Gohr} 773bf5e5a5bSAndreas Gohr 774420edfd6STom N Harris/** 775420edfd6STom N Harris * Runs an external command with input and output pipes. 776420edfd6STom N Harris * Returns the exit code from the process. 777420edfd6STom N Harris * 77842ea7f44SGerrit Uitslag * @param string $cmd 77942ea7f44SGerrit Uitslag * @param string $input input pipe 78042ea7f44SGerrit Uitslag * @param string $output output pipe 78142ea7f44SGerrit Uitslag * @return int exit code from process 782aa659bbaSGerrit Uitslag * 783aa659bbaSGerrit Uitslag * @author Tom N Harris <tnharris@whoopdedo.org> 784420edfd6STom N Harris */ 785d868eb89SAndreas Gohrfunction io_exec($cmd, $input, &$output) 786d868eb89SAndreas Gohr{ 78724870174SAndreas Gohr $descspec = [ 78824870174SAndreas Gohr 0 => ["pipe", "r"], 78924870174SAndreas Gohr 1 => ["pipe", "w"], 79024870174SAndreas Gohr 2 => ["pipe", "w"] 79124870174SAndreas Gohr ]; 7926c528220STom N Harris $ph = proc_open($cmd, $descspec, $pipes); 7936c528220STom N Harris if (!$ph) return -1; 7946c528220STom N Harris fclose($pipes[2]); // ignore stderr 7956c528220STom N Harris fwrite($pipes[0], $input); 7966c528220STom N Harris fclose($pipes[0]); 7976c528220STom N Harris $output = stream_get_contents($pipes[1]); 7986c528220STom N Harris fclose($pipes[1]); 7996c528220STom N Harris return proc_close($ph); 800f3f0262cSandi} 801f3f0262cSandi 8027421c3ccSAndreas Gohr/** 8037421c3ccSAndreas Gohr * Search a file for matching lines 8047421c3ccSAndreas Gohr * 8057421c3ccSAndreas Gohr * This is probably not faster than file()+preg_grep() but less 8067421c3ccSAndreas Gohr * memory intensive because not the whole file needs to be loaded 8077421c3ccSAndreas Gohr * at once. 8087421c3ccSAndreas Gohr * 8097421c3ccSAndreas Gohr * @param string $file The file to search 8107421c3ccSAndreas Gohr * @param string $pattern PCRE pattern 8117421c3ccSAndreas Gohr * @param int $max How many lines to return (0 for all) 812cd2f903bSMichael Hamann * @param bool $backref When true returns array with backreferences instead of lines 813cd2f903bSMichael Hamann * @return array matching lines or backref, false on error 814aa659bbaSGerrit Uitslag * 815aa659bbaSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 8167421c3ccSAndreas Gohr */ 817d868eb89SAndreas Gohrfunction io_grep($file, $pattern, $max = 0, $backref = false) 818d868eb89SAndreas Gohr{ 8197421c3ccSAndreas Gohr $fh = @fopen($file, 'r'); 8207421c3ccSAndreas Gohr if (!$fh) return false; 82124870174SAndreas Gohr $matches = []; 8227421c3ccSAndreas Gohr 8237421c3ccSAndreas Gohr $cnt = 0; 8247421c3ccSAndreas Gohr $line = ''; 8257421c3ccSAndreas Gohr while (!feof($fh)) { 8267421c3ccSAndreas Gohr $line .= fgets($fh, 4096); // read full line 8276c16a3a9Sfiwswe if (!str_ends_with($line, "\n")) continue; 8287421c3ccSAndreas Gohr 8297421c3ccSAndreas Gohr // check if line matches 8307421c3ccSAndreas Gohr if (preg_match($pattern, $line, $match)) { 8317421c3ccSAndreas Gohr if ($backref) { 8327421c3ccSAndreas Gohr $matches[] = $match; 8337421c3ccSAndreas Gohr } else { 8347421c3ccSAndreas Gohr $matches[] = $line; 8357421c3ccSAndreas Gohr } 8367421c3ccSAndreas Gohr $cnt++; 8377421c3ccSAndreas Gohr } 8387421c3ccSAndreas Gohr if ($max && $max == $cnt) break; 8397421c3ccSAndreas Gohr $line = ''; 8407421c3ccSAndreas Gohr } 8417421c3ccSAndreas Gohr fclose($fh); 8427421c3ccSAndreas Gohr return $matches; 8437421c3ccSAndreas Gohr} 8447421c3ccSAndreas Gohr 845f549be3dSGerrit Uitslag 846f549be3dSGerrit Uitslag/** 847f549be3dSGerrit Uitslag * Get size of contents of a file, for a compressed file the uncompressed size 848f549be3dSGerrit Uitslag * Warning: reading uncompressed size of content of bz-files requires uncompressing 849f549be3dSGerrit Uitslag * 850f549be3dSGerrit Uitslag * @param string $file filename path to file 851f549be3dSGerrit Uitslag * @return int size of file 852aa659bbaSGerrit Uitslag * 853aa659bbaSGerrit Uitslag * @author Gerrit Uitslag <klapinklapin@gmail.com> 854f549be3dSGerrit Uitslag */ 855d868eb89SAndreas Gohrfunction io_getSizeFile($file) 856d868eb89SAndreas Gohr{ 857f549be3dSGerrit Uitslag if (!file_exists($file)) return 0; 858f549be3dSGerrit Uitslag 8596c16a3a9Sfiwswe if (str_ends_with($file, '.gz')) { 860f549be3dSGerrit Uitslag $fp = @fopen($file, "rb"); 861f549be3dSGerrit Uitslag if ($fp === false) return 0; 862f549be3dSGerrit Uitslag fseek($fp, -4, SEEK_END); 863f549be3dSGerrit Uitslag $buffer = fread($fp, 4); 864f549be3dSGerrit Uitslag fclose($fp); 865f549be3dSGerrit Uitslag $array = unpack("V", $buffer); 866f549be3dSGerrit Uitslag $uncompressedsize = end($array); 8676c16a3a9Sfiwswe } elseif (str_ends_with($file, '.bz2')) { 868f549be3dSGerrit Uitslag if (!DOKU_HAS_BZIP) return 0; 869f549be3dSGerrit Uitslag $bz = bzopen($file, "r"); 870f549be3dSGerrit Uitslag if ($bz === false) return 0; 871f549be3dSGerrit Uitslag $uncompressedsize = 0; 872f549be3dSGerrit Uitslag while (!feof($bz)) { 873f549be3dSGerrit Uitslag //8192 seems to be the maximum buffersize? 874f549be3dSGerrit Uitslag $buffer = bzread($bz, 8192); 875f549be3dSGerrit Uitslag if (($buffer === false) || (bzerrno($bz) !== 0)) { 876f549be3dSGerrit Uitslag return 0; 877f549be3dSGerrit Uitslag } 878f549be3dSGerrit Uitslag $uncompressedsize += strlen($buffer); 879f549be3dSGerrit Uitslag } 880f549be3dSGerrit Uitslag } else { 881f549be3dSGerrit Uitslag $uncompressedsize = filesize($file); 882f549be3dSGerrit Uitslag } 883f549be3dSGerrit Uitslag 884f549be3dSGerrit Uitslag return $uncompressedsize; 885f549be3dSGerrit Uitslag} 886