Merge pull request #2515 from Innovailable/master
[dokuwiki.git] / inc / io.php
blob0ab1d560a8491c4e3f03b8c4273e5a47fe7a077c
1 <?php
2 /**
3 * File IO functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 if(!defined('DOKU_INC')) die('meh.');
11 /**
12 * Removes empty directories
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.
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
23 * @author Andreas Gohr <andi@splitbrain.org>
24 * @author Ben Coburn <btcoburn@silicodon.net>
26 function io_sweepNS($id,$basedir='datadir'){
27 global $conf;
28 $types = array ('datadir'=>'pages', 'mediadir'=>'media');
29 $ns_type = (isset($types[$basedir])?$types[$basedir]:false);
31 $delone = false;
33 //scan all namespaces
34 while(($id = getNS($id)) !== false){
35 $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id));
37 //try to delete dir else return
38 if(@rmdir($dir)) {
39 if ($ns_type!==false) {
40 $data = array($id, $ns_type);
41 $delone = true; // we deleted at least one dir
42 trigger_event('IO_NAMESPACE_DELETED', $data);
44 } else { return $delone; }
46 return $delone;
49 /**
50 * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events.
52 * Generates the action event which delegates to io_readFile().
53 * Action plugins are allowed to modify the page content in transit.
54 * The file path should not be changed.
56 * Event data:
57 * $data[0] The raw arguments for io_readFile as an array.
58 * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns)
59 * $data[2] page_name: The wiki page name.
60 * $data[3] rev: The page revision, false for current wiki pages.
62 * @author Ben Coburn <btcoburn@silicodon.net>
64 * @param string $file filename
65 * @param string $id page id
66 * @param bool|int $rev revision timestamp
67 * @return string
69 function io_readWikiPage($file, $id, $rev=false) {
70 if (empty($rev)) { $rev = false; }
71 $data = array(array($file, true), getNS($id), noNS($id), $rev);
72 return trigger_event('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false);
75 /**
76 * Callback adapter for io_readFile().
78 * @author Ben Coburn <btcoburn@silicodon.net>
80 * @param array $data event data
81 * @return string
83 function _io_readWikiPage_action($data) {
84 if (is_array($data) && is_array($data[0]) && count($data[0])===2) {
85 return call_user_func_array('io_readFile', $data[0]);
86 } else {
87 return ''; //callback error
91 /**
92 * Returns content of $file as cleaned string.
94 * Uses gzip if extension is .gz
96 * If you want to use the returned value in unserialize
97 * be sure to set $clean to false!
99 * @author Andreas Gohr <andi@splitbrain.org>
101 * @param string $file filename
102 * @param bool $clean
103 * @return string|bool the file contents or false on error
105 function io_readFile($file,$clean=true){
106 $ret = '';
107 if(file_exists($file)){
108 if(substr($file,-3) == '.gz'){
109 if(!DOKU_HAS_GZIP) return false;
110 $ret = gzfile($file);
111 if(is_array($ret)) $ret = join('', $ret);
112 }else if(substr($file,-4) == '.bz2'){
113 if(!DOKU_HAS_BZIP) return false;
114 $ret = bzfile($file);
115 }else{
116 $ret = file_get_contents($file);
119 if($ret === null) return false;
120 if($ret !== false && $clean){
121 return cleanText($ret);
122 }else{
123 return $ret;
127 * Returns the content of a .bz2 compressed file as string
129 * @author marcel senf <marcel@rucksackreinigung.de>
130 * @author Andreas Gohr <andi@splitbrain.org>
132 * @param string $file filename
133 * @param bool $array return array of lines
134 * @return string|array|bool content or false on error
136 function bzfile($file, $array=false) {
137 $bz = bzopen($file,"r");
138 if($bz === false) return false;
140 if($array) $lines = array();
141 $str = '';
142 while (!feof($bz)) {
143 //8192 seems to be the maximum buffersize?
144 $buffer = bzread($bz,8192);
145 if(($buffer === false) || (bzerrno($bz) !== 0)) {
146 return false;
148 $str = $str . $buffer;
149 if($array) {
150 $pos = strpos($str, "\n");
151 while($pos !== false) {
152 $lines[] = substr($str, 0, $pos+1);
153 $str = substr($str, $pos+1);
154 $pos = strpos($str, "\n");
158 bzclose($bz);
159 if($array) {
160 if($str !== '') $lines[] = $str;
161 return $lines;
163 return $str;
167 * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events.
169 * This generates an action event and delegates to io_saveFile().
170 * Action plugins are allowed to modify the page content in transit.
171 * The file path should not be changed.
172 * (The append parameter is set to false.)
174 * Event data:
175 * $data[0] The raw arguments for io_saveFile as an array.
176 * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns)
177 * $data[2] page_name: The wiki page name.
178 * $data[3] rev: The page revision, false for current wiki pages.
180 * @author Ben Coburn <btcoburn@silicodon.net>
182 * @param string $file filename
183 * @param string $content
184 * @param string $id page id
185 * @param int|bool $rev timestamp of revision
186 * @return bool
188 function io_writeWikiPage($file, $content, $id, $rev=false) {
189 if (empty($rev)) { $rev = false; }
190 if ($rev===false) { io_createNamespace($id); } // create namespaces as needed
191 $data = array(array($file, $content, false), getNS($id), noNS($id), $rev);
192 return trigger_event('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false);
196 * Callback adapter for io_saveFile().
197 * @author Ben Coburn <btcoburn@silicodon.net>
199 * @param array $data event data
200 * @return bool
202 function _io_writeWikiPage_action($data) {
203 if (is_array($data) && is_array($data[0]) && count($data[0])===3) {
204 $ok = call_user_func_array('io_saveFile', $data[0]);
205 // for attic files make sure the file has the mtime of the revision
206 if($ok && is_int($data[3]) && $data[3] > 0) {
207 @touch($data[0][0], $data[3]);
209 return $ok;
210 } else {
211 return false; //callback error
216 * Internal function to save contents to a file.
218 * @author Andreas Gohr <andi@splitbrain.org>
220 * @param string $file filename path to file
221 * @param string $content
222 * @param bool $append
223 * @return bool true on success, otherwise false
225 function _io_saveFile($file, $content, $append) {
226 global $conf;
227 $mode = ($append) ? 'ab' : 'wb';
228 $fileexists = file_exists($file);
230 if(substr($file,-3) == '.gz'){
231 if(!DOKU_HAS_GZIP) return false;
232 $fh = @gzopen($file,$mode.'9');
233 if(!$fh) return false;
234 gzwrite($fh, $content);
235 gzclose($fh);
236 }else if(substr($file,-4) == '.bz2'){
237 if(!DOKU_HAS_BZIP) return false;
238 if($append) {
239 $bzcontent = bzfile($file);
240 if($bzcontent === false) return false;
241 $content = $bzcontent.$content;
243 $fh = @bzopen($file,'w');
244 if(!$fh) return false;
245 bzwrite($fh, $content);
246 bzclose($fh);
247 }else{
248 $fh = @fopen($file,$mode);
249 if(!$fh) return false;
250 fwrite($fh, $content);
251 fclose($fh);
254 if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']);
255 return true;
259 * Saves $content to $file.
261 * If the third parameter is set to true the given content
262 * will be appended.
264 * Uses gzip if extension is .gz
265 * and bz2 if extension is .bz2
267 * @author Andreas Gohr <andi@splitbrain.org>
269 * @param string $file filename path to file
270 * @param string $content
271 * @param bool $append
272 * @return bool true on success, otherwise false
274 function io_saveFile($file, $content, $append=false) {
275 io_makeFileDir($file);
276 io_lock($file);
277 if(!_io_saveFile($file, $content, $append)) {
278 msg("Writing $file failed",-1);
279 io_unlock($file);
280 return false;
282 io_unlock($file);
283 return true;
287 * Replace one or more occurrences of a line in a file.
289 * The default, when $maxlines is 0 is to delete all matching lines then append a single line.
290 * A regex that matches any part of the line will remove the entire line in this mode.
291 * Captures in $newline are not available.
293 * Otherwise each line is matched and replaced individually, up to the first $maxlines lines
294 * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline.
296 * Be sure to include the trailing newline in $oldline when replacing entire lines.
298 * Uses gzip if extension is .gz
299 * and bz2 if extension is .bz2
301 * @author Steven Danz <steven-danz@kc.rr.com>
302 * @author Christopher Smith <chris@jalakai.co.uk>
303 * @author Patrick Brown <ptbrown@whoopdedo.org>
305 * @param string $file filename
306 * @param string $oldline exact linematch to remove
307 * @param string $newline new line to insert
308 * @param bool $regex use regexp?
309 * @param int $maxlines number of occurrences of the line to replace
310 * @return bool true on success
312 function io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0) {
313 if ((string)$oldline === '') {
314 trigger_error('$oldline parameter cannot be empty in io_replaceInFile()', E_USER_WARNING);
315 return false;
318 if (!file_exists($file)) return true;
320 io_lock($file);
322 // load into array
323 if(substr($file,-3) == '.gz'){
324 if(!DOKU_HAS_GZIP) return false;
325 $lines = gzfile($file);
326 }else if(substr($file,-4) == '.bz2'){
327 if(!DOKU_HAS_BZIP) return false;
328 $lines = bzfile($file, true);
329 }else{
330 $lines = file($file);
333 // make non-regexes into regexes
334 $pattern = $regex ? $oldline : '/^'.preg_quote($oldline,'/').'$/';
335 $replace = $regex ? $newline : addcslashes($newline, '\$');
337 // remove matching lines
338 if ($maxlines > 0) {
339 $count = 0;
340 $matched = 0;
341 foreach($lines as $i => $line) {
342 if($count >= $maxlines) break;
343 // $matched will be set to 0|1 depending on whether pattern is matched and line replaced
344 $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched);
345 if ($matched) $count++;
347 } else if ($maxlines == 0) {
348 $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT);
350 if ((string)$newline !== ''){
351 $lines[] = $newline;
353 } else {
354 $lines = preg_replace($pattern, $replace, $lines);
357 if(count($lines)){
358 if(!_io_saveFile($file, join('',$lines), false)) {
359 msg("Removing content from $file failed",-1);
360 io_unlock($file);
361 return false;
363 }else{
364 @unlink($file);
367 io_unlock($file);
368 return true;
372 * Delete lines that match $badline from $file.
374 * Be sure to include the trailing newline in $badline
376 * @author Patrick Brown <ptbrown@whoopdedo.org>
378 * @param string $file filename
379 * @param string $badline exact linematch to remove
380 * @param bool $regex use regexp?
381 * @return bool true on success
383 function io_deleteFromFile($file,$badline,$regex=false){
384 return io_replaceInFile($file,$badline,null,$regex,0);
388 * Tries to lock a file
390 * Locking is only done for io_savefile and uses directories
391 * inside $conf['lockdir']
393 * It waits maximal 3 seconds for the lock, after this time
394 * the lock is assumed to be stale and the function goes on
396 * @author Andreas Gohr <andi@splitbrain.org>
398 * @param string $file filename
400 function io_lock($file){
401 global $conf;
403 $lockDir = $conf['lockdir'].'/'.md5($file);
404 @ignore_user_abort(1);
406 $timeStart = time();
407 do {
408 //waited longer than 3 seconds? -> stale lock
409 if ((time() - $timeStart) > 3) break;
410 $locked = @mkdir($lockDir, $conf['dmode']);
411 if($locked){
412 if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
413 break;
415 usleep(50);
416 } while ($locked === false);
420 * Unlocks a file
422 * @author Andreas Gohr <andi@splitbrain.org>
424 * @param string $file filename
426 function io_unlock($file){
427 global $conf;
429 $lockDir = $conf['lockdir'].'/'.md5($file);
430 @rmdir($lockDir);
431 @ignore_user_abort(0);
435 * Create missing namespace directories and send the IO_NAMESPACE_CREATED events
436 * in the order of directory creation. (Parent directories first.)
438 * Event data:
439 * $data[0] ns: The colon separated namespace path minus the trailing page name.
440 * $data[1] ns_type: 'pages' or 'media' namespace tree.
442 * @author Ben Coburn <btcoburn@silicodon.net>
444 * @param string $id page id
445 * @param string $ns_type 'pages' or 'media'
447 function io_createNamespace($id, $ns_type='pages') {
448 // verify ns_type
449 $types = array('pages'=>'wikiFN', 'media'=>'mediaFN');
450 if (!isset($types[$ns_type])) {
451 trigger_error('Bad $ns_type parameter for io_createNamespace().');
452 return;
454 // make event list
455 $missing = array();
456 $ns_stack = explode(':', $id);
457 $ns = $id;
458 $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) );
459 while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) {
460 array_pop($ns_stack);
461 $ns = implode(':', $ns_stack);
462 if (strlen($ns)==0) { break; }
463 $missing[] = $ns;
464 $tmp = dirname(call_user_func($types[$ns_type], $ns));
466 // make directories
467 io_makeFileDir($file);
468 // send the events
469 $missing = array_reverse($missing); // inside out
470 foreach ($missing as $ns) {
471 $data = array($ns, $ns_type);
472 trigger_event('IO_NAMESPACE_CREATED', $data);
477 * Create the directory needed for the given file
479 * @author Andreas Gohr <andi@splitbrain.org>
481 * @param string $file file name
483 function io_makeFileDir($file){
484 $dir = dirname($file);
485 if(!@is_dir($dir)){
486 io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
491 * Creates a directory hierachy.
493 * @link http://php.net/manual/en/function.mkdir.php
494 * @author <saint@corenova.com>
495 * @author Andreas Gohr <andi@splitbrain.org>
497 * @param string $target filename
498 * @return bool|int|string
500 function io_mkdir_p($target){
501 global $conf;
502 if (@is_dir($target)||empty($target)) return 1; // best case check first
503 if (file_exists($target) && !is_dir($target)) return 0;
504 //recursion
505 if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
506 $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
507 if($ret && !empty($conf['dperm'])) chmod($target, $conf['dperm']);
508 return $ret;
510 return 0;
514 * Recursively delete a directory
516 * @author Andreas Gohr <andi@splitbrain.org>
517 * @param string $path
518 * @param bool $removefiles defaults to false which will delete empty directories only
519 * @return bool
521 function io_rmdir($path, $removefiles = false) {
522 if(!is_string($path) || $path == "") return false;
523 if(!file_exists($path)) return true; // it's already gone or was never there, count as success
525 if(is_dir($path) && !is_link($path)) {
526 $dirs = array();
527 $files = array();
529 if(!$dh = @opendir($path)) return false;
530 while(false !== ($f = readdir($dh))) {
531 if($f == '..' || $f == '.') continue;
533 // collect dirs and files first
534 if(is_dir("$path/$f") && !is_link("$path/$f")) {
535 $dirs[] = "$path/$f";
536 } else if($removefiles) {
537 $files[] = "$path/$f";
538 } else {
539 return false; // abort when non empty
543 closedir($dh);
545 // now traverse into directories first
546 foreach($dirs as $dir) {
547 if(!io_rmdir($dir, $removefiles)) return false; // abort on any error
550 // now delete files
551 foreach($files as $file) {
552 if(!@unlink($file)) return false; //abort on any error
555 // remove self
556 return @rmdir($path);
557 } else if($removefiles) {
558 return @unlink($path);
560 return false;
564 * Creates a unique temporary directory and returns
565 * its path.
567 * @author Michael Klier <chi@chimeric.de>
569 * @return false|string path to new directory or false
571 function io_mktmpdir() {
572 global $conf;
574 $base = $conf['tmpdir'];
575 $dir = md5(uniqid(mt_rand(), true));
576 $tmpdir = $base.'/'.$dir;
578 if(io_mkdir_p($tmpdir)) {
579 return($tmpdir);
580 } else {
581 return false;
586 * downloads a file from the net and saves it
588 * if $useAttachment is false,
589 * - $file is the full filename to save the file, incl. path
590 * - if successful will return true, false otherwise
592 * if $useAttachment is true,
593 * - $file is the directory where the file should be saved
594 * - if successful will return the name used for the saved file, false otherwise
596 * @author Andreas Gohr <andi@splitbrain.org>
597 * @author Chris Smith <chris@jalakai.co.uk>
599 * @param string $url url to download
600 * @param string $file path to file or directory where to save
601 * @param bool $useAttachment if true: try to use name of download, uses otherwise $defaultName, false: uses $file as path to file
602 * @param string $defaultName fallback for if using $useAttachment
603 * @param int $maxSize maximum file size
604 * @return bool|string if failed false, otherwise true or the name of the file in the given dir
606 function io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){
607 global $conf;
608 $http = new DokuHTTPClient();
609 $http->max_bodysize = $maxSize;
610 $http->timeout = 25; //max. 25 sec
611 $http->keep_alive = false; // we do single ops here, no need for keep-alive
613 $data = $http->get($url);
614 if(!$data) return false;
616 $name = '';
617 if ($useAttachment) {
618 if (isset($http->resp_headers['content-disposition'])) {
619 $content_disposition = $http->resp_headers['content-disposition'];
620 $match=array();
621 if (is_string($content_disposition) &&
622 preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
624 $name = utf8_basename($match[1]);
629 if (!$name) {
630 if (!$defaultName) return false;
631 $name = $defaultName;
634 $file = $file.$name;
637 $fileexists = file_exists($file);
638 $fp = @fopen($file,"w");
639 if(!$fp) return false;
640 fwrite($fp,$data);
641 fclose($fp);
642 if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
643 if ($useAttachment) return $name;
644 return true;
648 * Windows compatible rename
650 * rename() can not overwrite existing files on Windows
651 * this function will use copy/unlink instead
653 * @param string $from
654 * @param string $to
655 * @return bool succes or fail
657 function io_rename($from,$to){
658 global $conf;
659 if(!@rename($from,$to)){
660 if(@copy($from,$to)){
661 if($conf['fperm']) chmod($to, $conf['fperm']);
662 @unlink($from);
663 return true;
665 return false;
667 return true;
671 * Runs an external command with input and output pipes.
672 * Returns the exit code from the process.
674 * @author Tom N Harris <tnharris@whoopdedo.org>
676 * @param string $cmd
677 * @param string $input input pipe
678 * @param string $output output pipe
679 * @return int exit code from process
681 function io_exec($cmd, $input, &$output){
682 $descspec = array(
683 0=>array("pipe","r"),
684 1=>array("pipe","w"),
685 2=>array("pipe","w"));
686 $ph = proc_open($cmd, $descspec, $pipes);
687 if(!$ph) return -1;
688 fclose($pipes[2]); // ignore stderr
689 fwrite($pipes[0], $input);
690 fclose($pipes[0]);
691 $output = stream_get_contents($pipes[1]);
692 fclose($pipes[1]);
693 return proc_close($ph);
697 * Search a file for matching lines
699 * This is probably not faster than file()+preg_grep() but less
700 * memory intensive because not the whole file needs to be loaded
701 * at once.
703 * @author Andreas Gohr <andi@splitbrain.org>
704 * @param string $file The file to search
705 * @param string $pattern PCRE pattern
706 * @param int $max How many lines to return (0 for all)
707 * @param bool $backref When true returns array with backreferences instead of lines
708 * @return array matching lines or backref, false on error
710 function io_grep($file,$pattern,$max=0,$backref=false){
711 $fh = @fopen($file,'r');
712 if(!$fh) return false;
713 $matches = array();
715 $cnt = 0;
716 $line = '';
717 while (!feof($fh)) {
718 $line .= fgets($fh, 4096); // read full line
719 if(substr($line,-1) != "\n") continue;
721 // check if line matches
722 if(preg_match($pattern,$line,$match)){
723 if($backref){
724 $matches[] = $match;
725 }else{
726 $matches[] = $line;
728 $cnt++;
730 if($max && $max == $cnt) break;
731 $line = '';
733 fclose($fh);
734 return $matches;
739 * Get size of contents of a file, for a compressed file the uncompressed size
740 * Warning: reading uncompressed size of content of bz-files requires uncompressing
742 * @author Gerrit Uitslag <klapinklapin@gmail.com>
744 * @param string $file filename path to file
745 * @return int size of file
747 function io_getSizeFile($file) {
748 if (!file_exists($file)) return 0;
750 if(substr($file,-3) == '.gz'){
751 $fp = @fopen($file, "rb");
752 if($fp === false) return 0;
754 fseek($fp, -4, SEEK_END);
755 $buffer = fread($fp, 4);
756 fclose($fp);
757 $array = unpack("V", $buffer);
758 $uncompressedsize = end($array);
759 }else if(substr($file,-4) == '.bz2'){
760 if(!DOKU_HAS_BZIP) return 0;
762 $bz = bzopen($file,"r");
763 if($bz === false) return 0;
765 $uncompressedsize = 0;
766 while (!feof($bz)) {
767 //8192 seems to be the maximum buffersize?
768 $buffer = bzread($bz,8192);
769 if(($buffer === false) || (bzerrno($bz) !== 0)) {
770 return 0;
772 $uncompressedsize += strlen($buffer);
774 }else{
775 $uncompressedsize = filesize($file);
778 return $uncompressedsize;