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