You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

835 lines
23 KiB

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