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