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