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