xref: /dokuwiki/inc/io.php (revision 44881d272282937c9bb745f462c947319d404dd0)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * File IO functions
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
815fae107Sandi
9ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10ed7b5f09Sandi  require_once(DOKU_INC.'inc/common.php');
119b307a83SAndreas Gohr  require_once(DOKU_INC.'inc/HTTPClient.php');
12f3f0262cSandi
13f3f0262cSandi/**
1453d6ccfeSandi * Removes empty directories
1553d6ccfeSandi *
1653d6ccfeSandi * @todo use safemode hack
1753d6ccfeSandi * @author  Andreas Gohr <andi@splitbrain.org>
1853d6ccfeSandi */
1953d6ccfeSandifunction io_sweepNS($id){
2053d6ccfeSandi  global $conf;
2153d6ccfeSandi
2253d6ccfeSandi  //scan all namespaces
2353d6ccfeSandi  while(($id = getNS($id)) !== false){
2453d6ccfeSandi    $dir = $conf['datadir'].'/'.str_replace(':','/',$id);
2553d6ccfeSandi    $dir = utf8_encodeFN($dir);
2653d6ccfeSandi
2753d6ccfeSandi    //try to delete dir else return
2853d6ccfeSandi    if(!@rmdir($dir)) return;
2953d6ccfeSandi  }
3053d6ccfeSandi}
3153d6ccfeSandi
3253d6ccfeSandi/**
3315fae107Sandi * Returns content of $file as cleaned string.
3415fae107Sandi *
3515fae107Sandi * Uses gzip if extension is .gz
3615fae107Sandi *
3715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
38f3f0262cSandi */
39e34c0709SAndreas Gohrfunction io_readFile($file,$clean=true){
40f3f0262cSandi  $ret = '';
41f3f0262cSandi  if(@file_exists($file)){
42f3f0262cSandi    if(substr($file,-3) == '.gz'){
43f3f0262cSandi      $ret = join('',gzfile($file));
44f3f0262cSandi    }else{
45f3f0262cSandi      $ret = join('',file($file));
46f3f0262cSandi    }
47f3f0262cSandi  }
48e34c0709SAndreas Gohr  if($clean){
49f3f0262cSandi    return cleanText($ret);
50e34c0709SAndreas Gohr  }else{
51e34c0709SAndreas Gohr    return $ret;
52e34c0709SAndreas Gohr  }
53f3f0262cSandi}
54f3f0262cSandi
55f3f0262cSandi/**
5615fae107Sandi * Saves $content to $file.
57f3f0262cSandi *
581380fc45SAndreas Gohr * If the third parameter is set to true the given content
591380fc45SAndreas Gohr * will be appended.
601380fc45SAndreas Gohr *
6115fae107Sandi * Uses gzip if extension is .gz
6215fae107Sandi *
6315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
6415fae107Sandi * @return bool true on success
65f3f0262cSandi */
661380fc45SAndreas Gohrfunction io_saveFile($file,$content,$append=false){
67ac9115b0STroels Liebe Bentsen  global $conf;
681380fc45SAndreas Gohr  $mode = ($append) ? 'ab' : 'wb';
691380fc45SAndreas Gohr
70ac9115b0STroels Liebe Bentsen  $fileexists = file_exists($file);
71f3f0262cSandi  io_makeFileDir($file);
7290eb8392Sandi  io_lock($file);
73f3f0262cSandi  if(substr($file,-3) == '.gz'){
741380fc45SAndreas Gohr    $fh = @gzopen($file,$mode.'9');
75f3f0262cSandi    if(!$fh){
76f3f0262cSandi      msg("Writing $file failed",-1);
77f3f0262cSandi      return false;
78f3f0262cSandi    }
79f3f0262cSandi    gzwrite($fh, $content);
80f3f0262cSandi    gzclose($fh);
81f3f0262cSandi  }else{
821380fc45SAndreas Gohr    $fh = @fopen($file,$mode);
83f3f0262cSandi    if(!$fh){
84f3f0262cSandi      msg("Writing $file failed",-1);
85f3f0262cSandi      return false;
86f3f0262cSandi    }
87f3f0262cSandi    fwrite($fh, $content);
88f3f0262cSandi    fclose($fh);
89f3f0262cSandi  }
90ac9115b0STroels Liebe Bentsen
91*44881d27STroels Liebe Bentsen  if(!$fileexists and isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
9290eb8392Sandi  io_unlock($file);
93f3f0262cSandi  return true;
94f3f0262cSandi}
95f3f0262cSandi
96f3f0262cSandi/**
971380fc45SAndreas Gohr * Delete exact linematch for $badline from $file.
981380fc45SAndreas Gohr *
991380fc45SAndreas Gohr * Be sure to include the trailing newline in $badline
100b158d625SSteven Danz *
101b158d625SSteven Danz * Uses gzip if extension is .gz
102b158d625SSteven Danz *
1038b06d178Schris * 2005-10-14 : added regex option -- Christopher Smith <chris@jalakai.co.uk>
1048b06d178Schris *
105b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
106b158d625SSteven Danz * @return bool true on success
107b158d625SSteven Danz */
1088b06d178Schrisfunction io_deleteFromFile($file,$badline,$regex=false){
1091380fc45SAndreas Gohr  if (!@file_exists($file)) return true;
1101380fc45SAndreas Gohr
111b158d625SSteven Danz  io_lock($file);
1121380fc45SAndreas Gohr
1131380fc45SAndreas Gohr  // load into array
114b158d625SSteven Danz  if(substr($file,-3) == '.gz'){
1151380fc45SAndreas Gohr    $lines = gzfile($file);
116b158d625SSteven Danz  }else{
1171380fc45SAndreas Gohr    $lines = file($file);
118b158d625SSteven Danz  }
119b158d625SSteven Danz
1201380fc45SAndreas Gohr  // remove all matching lines
1218b06d178Schris  if ($regex) {
1228b06d178Schris    $lines = preg_grep($badline,$lines,PREG_GREP_INVERT);
1238b06d178Schris  } else {
1241380fc45SAndreas Gohr    $pos = array_search($badline,$lines); //return null or false if not found
1251380fc45SAndreas Gohr    while(is_int($pos)){
1261380fc45SAndreas Gohr      unset($lines[$pos]);
1271380fc45SAndreas Gohr      $pos = array_search($badline,$lines);
128b158d625SSteven Danz    }
1298b06d178Schris  }
130b158d625SSteven Danz
1311380fc45SAndreas Gohr  if(count($lines)){
1321380fc45SAndreas Gohr    $content = join('',$lines);
133b158d625SSteven Danz    if(substr($file,-3) == '.gz'){
134b158d625SSteven Danz      $fh = @gzopen($file,'wb9');
135b158d625SSteven Danz      if(!$fh){
136b158d625SSteven Danz        msg("Removing content from $file failed",-1);
137b158d625SSteven Danz        return false;
138b158d625SSteven Danz      }
139b158d625SSteven Danz      gzwrite($fh, $content);
140b158d625SSteven Danz      gzclose($fh);
141b158d625SSteven Danz    }else{
142b158d625SSteven Danz      $fh = @fopen($file,'wb');
143b158d625SSteven Danz      if(!$fh){
144b158d625SSteven Danz        msg("Removing content from $file failed",-1);
145b158d625SSteven Danz        return false;
146b158d625SSteven Danz      }
147b158d625SSteven Danz      fwrite($fh, $content);
148b158d625SSteven Danz      fclose($fh);
149b158d625SSteven Danz    }
150b158d625SSteven Danz  }else{
151b158d625SSteven Danz    @unlink($file);
152b158d625SSteven Danz  }
153b158d625SSteven Danz
154b158d625SSteven Danz  io_unlock($file);
155b158d625SSteven Danz  return true;
156b158d625SSteven Danz}
157b158d625SSteven Danz
158b158d625SSteven Danz/**
15990eb8392Sandi * Tries to lock a file
16090eb8392Sandi *
16190eb8392Sandi * Locking is only done for io_savefile and uses directories
16290eb8392Sandi * inside $conf['lockdir']
16390eb8392Sandi *
16490eb8392Sandi * It waits maximal 3 seconds for the lock, after this time
16590eb8392Sandi * the lock is assumed to be stale and the function goes on
16690eb8392Sandi *
16790eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org>
16890eb8392Sandi */
16990eb8392Sandifunction io_lock($file){
17090eb8392Sandi  global $conf;
17190eb8392Sandi  // no locking if safemode hack
17290eb8392Sandi  if($conf['safemodehack']) return;
17390eb8392Sandi
17490eb8392Sandi  $lockDir = $conf['lockdir'].'/'.md5($file);
17590eb8392Sandi  @ignore_user_abort(1);
17690eb8392Sandi
17790eb8392Sandi  $timeStart = time();
17890eb8392Sandi  do {
17990eb8392Sandi    //waited longer than 3 seconds? -> stale lock
18090eb8392Sandi    if ((time() - $timeStart) > 3) break;
181*44881d27STroels Liebe Bentsen    $locked = @mkdir($lockDir, $conf['dmode']);
182*44881d27STroels Liebe Bentsen    if($locked and isset($conf['dmask'])) { chmod($lockDir, $conf['dmask']); }
18390eb8392Sandi  } while ($locked === false);
18490eb8392Sandi}
18590eb8392Sandi
18690eb8392Sandi/**
18790eb8392Sandi * Unlocks a file
18890eb8392Sandi *
18990eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org>
19090eb8392Sandi */
19190eb8392Sandifunction io_unlock($file){
19290eb8392Sandi  global $conf;
19390eb8392Sandi  // no locking if safemode hack
19490eb8392Sandi  if($conf['safemodehack']) return;
19590eb8392Sandi
19690eb8392Sandi  $lockDir = $conf['lockdir'].'/'.md5($file);
19790eb8392Sandi  @rmdir($lockDir);
19890eb8392Sandi  @ignore_user_abort(0);
19990eb8392Sandi}
20090eb8392Sandi
20190eb8392Sandi/**
202f3f0262cSandi * Create the directory needed for the given file
20315fae107Sandi *
20415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
205f3f0262cSandi */
206f3f0262cSandifunction io_makeFileDir($file){
207f3f0262cSandi  global $conf;
208f3f0262cSandi
209f3f0262cSandi  $dir = dirname($file);
210f3f0262cSandi  if(!is_dir($dir)){
211f3f0262cSandi    io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
212f3f0262cSandi  }
213f3f0262cSandi}
214f3f0262cSandi
215f3f0262cSandi/**
216f3f0262cSandi * Creates a directory hierachy.
217f3f0262cSandi *
21815fae107Sandi * @link    http://www.php.net/manual/en/function.mkdir.php
219f3f0262cSandi * @author  <saint@corenova.com>
2203dc3a5f1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
221f3f0262cSandi */
222f3f0262cSandifunction io_mkdir_p($target){
2233dc3a5f1Sandi  global $conf;
224f3f0262cSandi  if (is_dir($target)||empty($target)) return 1; // best case check first
225f3f0262cSandi  if (@file_exists($target) && !is_dir($target)) return 0;
2263dc3a5f1Sandi  //recursion
2273dc3a5f1Sandi  if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
2283dc3a5f1Sandi    if($conf['safemodehack']){
229034138e2SRainer Weinhold      $dir = preg_replace('/^'.preg_quote(realpath($conf['ftp']['root']),'/').'/','', $target);
230034138e2SRainer Weinhold      return io_mkdir_ftp($dir);
2313dc3a5f1Sandi    }else{
232*44881d27STroels Liebe Bentsen      $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
233*44881d27STroels Liebe Bentsen      if($ret and isset($conf['dmask'])) { chmod($target, $conf['dmask']); }
234*44881d27STroels Liebe Bentsen      return $ret;
2353dc3a5f1Sandi    }
2363dc3a5f1Sandi  }
237f3f0262cSandi  return 0;
238f3f0262cSandi}
239f3f0262cSandi
240f3f0262cSandi/**
2413dc3a5f1Sandi * Creates a directory using FTP
2423dc3a5f1Sandi *
2433dc3a5f1Sandi * This is used when the safemode workaround is enabled
2443dc3a5f1Sandi *
2453dc3a5f1Sandi * @author <andi@splitbrain.org>
2463dc3a5f1Sandi */
2473dc3a5f1Sandifunction io_mkdir_ftp($dir){
2483dc3a5f1Sandi  global $conf;
2493dc3a5f1Sandi
2503dc3a5f1Sandi  if(!function_exists('ftp_connect')){
2513dc3a5f1Sandi    msg("FTP support not found - safemode workaround not usable",-1);
2523dc3a5f1Sandi    return false;
2533dc3a5f1Sandi  }
2543dc3a5f1Sandi
2553dc3a5f1Sandi  $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
2563dc3a5f1Sandi  if(!$conn){
2573dc3a5f1Sandi    msg("FTP connection failed",-1);
2583dc3a5f1Sandi    return false;
2593dc3a5f1Sandi  }
2603dc3a5f1Sandi
2613dc3a5f1Sandi  if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){
2623dc3a5f1Sandi    msg("FTP login failed",-1);
2633dc3a5f1Sandi    return false;
2643dc3a5f1Sandi  }
2653dc3a5f1Sandi
2663dc3a5f1Sandi  //create directory
267034138e2SRainer Weinhold  $ok = @ftp_mkdir($conn, $dir);
268ac9115b0STroels Liebe Bentsen  //set permissions (using the directory umask and dmode)
269*44881d27STroels Liebe Bentsen  @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmask'],$dir));
2703dc3a5f1Sandi
271034138e2SRainer Weinhold  @ftp_close($conn);
2723dc3a5f1Sandi  return $ok;
2733dc3a5f1Sandi}
2743dc3a5f1Sandi
2753dc3a5f1Sandi/**
27673ccfcb9Schris * downloads a file from the net and saves it
27773ccfcb9Schris *
27873ccfcb9Schris * if $useAttachment is false,
27973ccfcb9Schris * - $file is the full filename to save the file, incl. path
28073ccfcb9Schris * - if successful will return true, false otherwise
28173ccfcb9Schris
28273ccfcb9Schris * if $useAttachment is true,
28373ccfcb9Schris * - $file is the directory where the file should be saved
28473ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise
285b625487dSandi *
286b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
28773ccfcb9Schris * @author Chris Smith <chris@jalakai.co.uk>
288b625487dSandi */
28973ccfcb9Schrisfunction io_download($url,$file,$useAttachment=false,$defaultName=''){
290ac9115b0STroels Liebe Bentsen  global $conf;
2919b307a83SAndreas Gohr  $http = new DokuHTTPClient();
2929b307a83SAndreas Gohr  $http->max_bodysize = 2*1024*1024; //max. 2MB
2939b307a83SAndreas Gohr  $http->timeout = 25; //max. 25 sec
2949b307a83SAndreas Gohr
2959b307a83SAndreas Gohr  $data = $http->get($url);
2969b307a83SAndreas Gohr  if(!$data) return false;
2979b307a83SAndreas Gohr
29873ccfcb9Schris  if ($useAttachment) {
29973ccfcb9Schris    $name = '';
30073ccfcb9Schris      if (isset($http->resp_headers['content-disposition'])) {
30173ccfcb9Schris      $content_disposition = $http->resp_headers['content-disposition'];
302ce070a9fSchris      $match=array();
30373ccfcb9Schris      if (is_string($content_disposition) &&
304ce070a9fSchris          preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
30573ccfcb9Schris
30673ccfcb9Schris          $name = basename($match[1]);
30773ccfcb9Schris      }
30873ccfcb9Schris
30973ccfcb9Schris    }
31073ccfcb9Schris
31173ccfcb9Schris    if (!$name) {
31273ccfcb9Schris        if (!$defaultName) return false;
31373ccfcb9Schris        $name = $defaultName;
31473ccfcb9Schris    }
31573ccfcb9Schris
31673ccfcb9Schris    $file = $file.$name;
31773ccfcb9Schris  }
31873ccfcb9Schris
319ac9115b0STroels Liebe Bentsen  $fileexists = file_exists($file);
3209b307a83SAndreas Gohr  $fp = @fopen($file,"w");
321b625487dSandi  if(!$fp) return false;
3229b307a83SAndreas Gohr  fwrite($fp,$data);
323b625487dSandi  fclose($fp);
324*44881d27STroels Liebe Bentsen  if(!$fileexists and isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
32573ccfcb9Schris  if ($useAttachment) return $name;
326b625487dSandi  return true;
327b625487dSandi}
328b625487dSandi
329b625487dSandi/**
330ac9115b0STroels Liebe Bentsen * Windows compatible rename
331bf5e5a5bSAndreas Gohr *
332bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows
333bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead
334bf5e5a5bSAndreas Gohr */
335bf5e5a5bSAndreas Gohrfunction io_rename($from,$to){
336ac9115b0STroels Liebe Bentsen  global $conf;
337bf5e5a5bSAndreas Gohr  if(!@rename($from,$to)){
338bf5e5a5bSAndreas Gohr    if(@copy($from,$to)){
339*44881d27STroels Liebe Bentsen      if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); }
340bf5e5a5bSAndreas Gohr      @unlink($from);
341bf5e5a5bSAndreas Gohr      return true;
342bf5e5a5bSAndreas Gohr    }
343bf5e5a5bSAndreas Gohr    return false;
344bf5e5a5bSAndreas Gohr  }
345bf5e5a5bSAndreas Gohr  return true;
346bf5e5a5bSAndreas Gohr}
347bf5e5a5bSAndreas Gohr
348bf5e5a5bSAndreas Gohr
349bf5e5a5bSAndreas Gohr/**
350f3f0262cSandi * Runs an external command and returns it's output as string
35115fae107Sandi *
35215fae107Sandi * @author Harry Brueckner <harry_b@eml.cc>
35315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
3543dc3a5f1Sandi * @deprecated
355f3f0262cSandi */
356f3f0262cSandifunction io_runcmd($cmd){
357f3f0262cSandi  $fh = popen($cmd, "r");
358f3f0262cSandi  if(!$fh) return false;
359f3f0262cSandi  $ret = '';
360f3f0262cSandi  while (!feof($fh)) {
361f3f0262cSandi    $ret .= fread($fh, 8192);
362f3f0262cSandi  }
363f3f0262cSandi  pclose($fh);
364f3f0262cSandi  return $ret;
365f3f0262cSandi}
366f3f0262cSandi
367340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
368