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