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){ 24db9d7f2fSAndreas Gohr $dir = $conf['datadir'].'/'.utf8_encodeFN(str_replace(':','/',$id)); 2553d6ccfeSandi 2653d6ccfeSandi //try to delete dir else return 2753d6ccfeSandi if(!@rmdir($dir)) return; 2853d6ccfeSandi } 2953d6ccfeSandi} 3053d6ccfeSandi 3153d6ccfeSandi/** 3215fae107Sandi * Returns content of $file as cleaned string. 3315fae107Sandi * 3415fae107Sandi * Uses gzip if extension is .gz 3515fae107Sandi * 3615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 37f3f0262cSandi */ 38e34c0709SAndreas Gohrfunction io_readFile($file,$clean=true){ 39f3f0262cSandi $ret = ''; 40f3f0262cSandi if(@file_exists($file)){ 41f3f0262cSandi if(substr($file,-3) == '.gz'){ 42f3f0262cSandi $ret = join('',gzfile($file)); 43f3f0262cSandi }else{ 44f3f0262cSandi $ret = join('',file($file)); 45f3f0262cSandi } 46f3f0262cSandi } 47e34c0709SAndreas Gohr if($clean){ 48f3f0262cSandi return cleanText($ret); 49e34c0709SAndreas Gohr }else{ 50e34c0709SAndreas Gohr return $ret; 51e34c0709SAndreas Gohr } 52f3f0262cSandi} 53f3f0262cSandi 54f3f0262cSandi/** 5515fae107Sandi * Saves $content to $file. 56f3f0262cSandi * 571380fc45SAndreas Gohr * If the third parameter is set to true the given content 581380fc45SAndreas Gohr * will be appended. 591380fc45SAndreas Gohr * 6015fae107Sandi * Uses gzip if extension is .gz 6115fae107Sandi * 6215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6315fae107Sandi * @return bool true on success 64f3f0262cSandi */ 651380fc45SAndreas Gohrfunction io_saveFile($file,$content,$append=false){ 66ac9115b0STroels Liebe Bentsen global $conf; 671380fc45SAndreas Gohr $mode = ($append) ? 'ab' : 'wb'; 681380fc45SAndreas Gohr 69ac9115b0STroels Liebe Bentsen $fileexists = file_exists($file); 70f3f0262cSandi io_makeFileDir($file); 7190eb8392Sandi io_lock($file); 72f3f0262cSandi if(substr($file,-3) == '.gz'){ 731380fc45SAndreas Gohr $fh = @gzopen($file,$mode.'9'); 74f3f0262cSandi if(!$fh){ 75f3f0262cSandi msg("Writing $file failed",-1); 76f3f0262cSandi return false; 77f3f0262cSandi } 78f3f0262cSandi gzwrite($fh, $content); 79f3f0262cSandi gzclose($fh); 80f3f0262cSandi }else{ 811380fc45SAndreas Gohr $fh = @fopen($file,$mode); 82f3f0262cSandi if(!$fh){ 83f3f0262cSandi msg("Writing $file failed",-1); 84f3f0262cSandi return false; 85f3f0262cSandi } 86f3f0262cSandi fwrite($fh, $content); 87f3f0262cSandi fclose($fh); 88f3f0262cSandi } 89ac9115b0STroels Liebe Bentsen 9044881d27STroels Liebe Bentsen if(!$fileexists and isset($conf['fmask'])) { chmod($file, $conf['fmask']); } 9190eb8392Sandi io_unlock($file); 92f3f0262cSandi return true; 93f3f0262cSandi} 94f3f0262cSandi 95f3f0262cSandi/** 961380fc45SAndreas Gohr * Delete exact linematch for $badline from $file. 971380fc45SAndreas Gohr * 981380fc45SAndreas Gohr * Be sure to include the trailing newline in $badline 99b158d625SSteven Danz * 100b158d625SSteven Danz * Uses gzip if extension is .gz 101b158d625SSteven Danz * 1028b06d178Schris * 2005-10-14 : added regex option -- Christopher Smith <chris@jalakai.co.uk> 1038b06d178Schris * 104b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 105b158d625SSteven Danz * @return bool true on success 106b158d625SSteven Danz */ 1078b06d178Schrisfunction io_deleteFromFile($file,$badline,$regex=false){ 1081380fc45SAndreas Gohr if (!@file_exists($file)) return true; 1091380fc45SAndreas Gohr 110b158d625SSteven Danz io_lock($file); 1111380fc45SAndreas Gohr 1121380fc45SAndreas Gohr // load into array 113b158d625SSteven Danz if(substr($file,-3) == '.gz'){ 1141380fc45SAndreas Gohr $lines = gzfile($file); 115b158d625SSteven Danz }else{ 1161380fc45SAndreas Gohr $lines = file($file); 117b158d625SSteven Danz } 118b158d625SSteven Danz 1191380fc45SAndreas Gohr // remove all matching lines 1208b06d178Schris if ($regex) { 1218b06d178Schris $lines = preg_grep($badline,$lines,PREG_GREP_INVERT); 1228b06d178Schris } else { 1231380fc45SAndreas Gohr $pos = array_search($badline,$lines); //return null or false if not found 1241380fc45SAndreas Gohr while(is_int($pos)){ 1251380fc45SAndreas Gohr unset($lines[$pos]); 1261380fc45SAndreas Gohr $pos = array_search($badline,$lines); 127b158d625SSteven Danz } 1288b06d178Schris } 129b158d625SSteven Danz 1301380fc45SAndreas Gohr if(count($lines)){ 1311380fc45SAndreas Gohr $content = join('',$lines); 132b158d625SSteven Danz if(substr($file,-3) == '.gz'){ 133b158d625SSteven Danz $fh = @gzopen($file,'wb9'); 134b158d625SSteven Danz if(!$fh){ 135b158d625SSteven Danz msg("Removing content from $file failed",-1); 136b158d625SSteven Danz return false; 137b158d625SSteven Danz } 138b158d625SSteven Danz gzwrite($fh, $content); 139b158d625SSteven Danz gzclose($fh); 140b158d625SSteven Danz }else{ 141b158d625SSteven Danz $fh = @fopen($file,'wb'); 142b158d625SSteven Danz if(!$fh){ 143b158d625SSteven Danz msg("Removing content from $file failed",-1); 144b158d625SSteven Danz return false; 145b158d625SSteven Danz } 146b158d625SSteven Danz fwrite($fh, $content); 147b158d625SSteven Danz fclose($fh); 148b158d625SSteven Danz } 149b158d625SSteven Danz }else{ 150b158d625SSteven Danz @unlink($file); 151b158d625SSteven Danz } 152b158d625SSteven Danz 153b158d625SSteven Danz io_unlock($file); 154b158d625SSteven Danz return true; 155b158d625SSteven Danz} 156b158d625SSteven Danz 157b158d625SSteven Danz/** 15890eb8392Sandi * Tries to lock a file 15990eb8392Sandi * 16090eb8392Sandi * Locking is only done for io_savefile and uses directories 16190eb8392Sandi * inside $conf['lockdir'] 16290eb8392Sandi * 16390eb8392Sandi * It waits maximal 3 seconds for the lock, after this time 16490eb8392Sandi * the lock is assumed to be stale and the function goes on 16590eb8392Sandi * 16690eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org> 16790eb8392Sandi */ 16890eb8392Sandifunction io_lock($file){ 16990eb8392Sandi global $conf; 17090eb8392Sandi // no locking if safemode hack 17190eb8392Sandi if($conf['safemodehack']) return; 17290eb8392Sandi 17390eb8392Sandi $lockDir = $conf['lockdir'].'/'.md5($file); 17490eb8392Sandi @ignore_user_abort(1); 17590eb8392Sandi 17690eb8392Sandi $timeStart = time(); 17790eb8392Sandi do { 17890eb8392Sandi //waited longer than 3 seconds? -> stale lock 17990eb8392Sandi if ((time() - $timeStart) > 3) break; 18044881d27STroels Liebe Bentsen $locked = @mkdir($lockDir, $conf['dmode']); 18144881d27STroels Liebe Bentsen if($locked and isset($conf['dmask'])) { chmod($lockDir, $conf['dmask']); } 18290eb8392Sandi } while ($locked === false); 18390eb8392Sandi} 18490eb8392Sandi 18590eb8392Sandi/** 18690eb8392Sandi * Unlocks a file 18790eb8392Sandi * 18890eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org> 18990eb8392Sandi */ 19090eb8392Sandifunction io_unlock($file){ 19190eb8392Sandi global $conf; 19290eb8392Sandi // no locking if safemode hack 19390eb8392Sandi if($conf['safemodehack']) return; 19490eb8392Sandi 19590eb8392Sandi $lockDir = $conf['lockdir'].'/'.md5($file); 19690eb8392Sandi @rmdir($lockDir); 19790eb8392Sandi @ignore_user_abort(0); 19890eb8392Sandi} 19990eb8392Sandi 20090eb8392Sandi/** 201f3f0262cSandi * Create the directory needed for the given file 20215fae107Sandi * 20315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 204f3f0262cSandi */ 205f3f0262cSandifunction io_makeFileDir($file){ 206f3f0262cSandi global $conf; 207f3f0262cSandi 208f3f0262cSandi $dir = dirname($file); 209*0d8850c4SAndreas Gohr if(!@is_dir($dir)){ 210f3f0262cSandi io_mkdir_p($dir) || msg("Creating directory $dir failed",-1); 211f3f0262cSandi } 212f3f0262cSandi} 213f3f0262cSandi 214f3f0262cSandi/** 215f3f0262cSandi * Creates a directory hierachy. 216f3f0262cSandi * 21715fae107Sandi * @link http://www.php.net/manual/en/function.mkdir.php 218f3f0262cSandi * @author <saint@corenova.com> 2193dc3a5f1Sandi * @author Andreas Gohr <andi@splitbrain.org> 220f3f0262cSandi */ 221f3f0262cSandifunction io_mkdir_p($target){ 2223dc3a5f1Sandi global $conf; 223*0d8850c4SAndreas Gohr if (@is_dir($target)||empty($target)) return 1; // best case check first 224f3f0262cSandi if (@file_exists($target) && !is_dir($target)) return 0; 2253dc3a5f1Sandi //recursion 2263dc3a5f1Sandi if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){ 2273dc3a5f1Sandi if($conf['safemodehack']){ 228034138e2SRainer Weinhold $dir = preg_replace('/^'.preg_quote(realpath($conf['ftp']['root']),'/').'/','', $target); 229034138e2SRainer Weinhold return io_mkdir_ftp($dir); 2303dc3a5f1Sandi }else{ 23144881d27STroels Liebe Bentsen $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree 23244881d27STroels Liebe Bentsen if($ret and isset($conf['dmask'])) { chmod($target, $conf['dmask']); } 23344881d27STroels Liebe Bentsen return $ret; 2343dc3a5f1Sandi } 2353dc3a5f1Sandi } 236f3f0262cSandi return 0; 237f3f0262cSandi} 238f3f0262cSandi 239f3f0262cSandi/** 2403dc3a5f1Sandi * Creates a directory using FTP 2413dc3a5f1Sandi * 2423dc3a5f1Sandi * This is used when the safemode workaround is enabled 2433dc3a5f1Sandi * 2443dc3a5f1Sandi * @author <andi@splitbrain.org> 2453dc3a5f1Sandi */ 2463dc3a5f1Sandifunction io_mkdir_ftp($dir){ 2473dc3a5f1Sandi global $conf; 2483dc3a5f1Sandi 2493dc3a5f1Sandi if(!function_exists('ftp_connect')){ 2503dc3a5f1Sandi msg("FTP support not found - safemode workaround not usable",-1); 2513dc3a5f1Sandi return false; 2523dc3a5f1Sandi } 2533dc3a5f1Sandi 2543dc3a5f1Sandi $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10); 2553dc3a5f1Sandi if(!$conn){ 2563dc3a5f1Sandi msg("FTP connection failed",-1); 2573dc3a5f1Sandi return false; 2583dc3a5f1Sandi } 2593dc3a5f1Sandi 2603dc3a5f1Sandi if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){ 2613dc3a5f1Sandi msg("FTP login failed",-1); 2623dc3a5f1Sandi return false; 2633dc3a5f1Sandi } 2643dc3a5f1Sandi 2653dc3a5f1Sandi //create directory 266034138e2SRainer Weinhold $ok = @ftp_mkdir($conn, $dir); 267ac9115b0STroels Liebe Bentsen //set permissions (using the directory umask and dmode) 26844881d27STroels Liebe Bentsen @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmask'],$dir)); 2693dc3a5f1Sandi 270034138e2SRainer Weinhold @ftp_close($conn); 2713dc3a5f1Sandi return $ok; 2723dc3a5f1Sandi} 2733dc3a5f1Sandi 2743dc3a5f1Sandi/** 27573ccfcb9Schris * downloads a file from the net and saves it 27673ccfcb9Schris * 27773ccfcb9Schris * if $useAttachment is false, 27873ccfcb9Schris * - $file is the full filename to save the file, incl. path 27973ccfcb9Schris * - if successful will return true, false otherwise 28073ccfcb9Schris 28173ccfcb9Schris * if $useAttachment is true, 28273ccfcb9Schris * - $file is the directory where the file should be saved 28373ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise 284b625487dSandi * 285b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 28673ccfcb9Schris * @author Chris Smith <chris@jalakai.co.uk> 287b625487dSandi */ 28873ccfcb9Schrisfunction io_download($url,$file,$useAttachment=false,$defaultName=''){ 289ac9115b0STroels Liebe Bentsen global $conf; 2909b307a83SAndreas Gohr $http = new DokuHTTPClient(); 2919b307a83SAndreas Gohr $http->max_bodysize = 2*1024*1024; //max. 2MB 2929b307a83SAndreas Gohr $http->timeout = 25; //max. 25 sec 2939b307a83SAndreas Gohr 2949b307a83SAndreas Gohr $data = $http->get($url); 2959b307a83SAndreas Gohr if(!$data) return false; 2969b307a83SAndreas Gohr 29773ccfcb9Schris if ($useAttachment) { 29873ccfcb9Schris $name = ''; 29973ccfcb9Schris if (isset($http->resp_headers['content-disposition'])) { 30073ccfcb9Schris $content_disposition = $http->resp_headers['content-disposition']; 301ce070a9fSchris $match=array(); 30273ccfcb9Schris if (is_string($content_disposition) && 303ce070a9fSchris preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) { 30473ccfcb9Schris 30573ccfcb9Schris $name = basename($match[1]); 30673ccfcb9Schris } 30773ccfcb9Schris 30873ccfcb9Schris } 30973ccfcb9Schris 31073ccfcb9Schris if (!$name) { 31173ccfcb9Schris if (!$defaultName) return false; 31273ccfcb9Schris $name = $defaultName; 31373ccfcb9Schris } 31473ccfcb9Schris 31573ccfcb9Schris $file = $file.$name; 31673ccfcb9Schris } 31773ccfcb9Schris 318ac9115b0STroels Liebe Bentsen $fileexists = file_exists($file); 3199b307a83SAndreas Gohr $fp = @fopen($file,"w"); 320b625487dSandi if(!$fp) return false; 3219b307a83SAndreas Gohr fwrite($fp,$data); 322b625487dSandi fclose($fp); 32344881d27STroels Liebe Bentsen if(!$fileexists and isset($conf['fmask'])) { chmod($file, $conf['fmask']); } 32473ccfcb9Schris if ($useAttachment) return $name; 325b625487dSandi return true; 326b625487dSandi} 327b625487dSandi 328b625487dSandi/** 329ac9115b0STroels Liebe Bentsen * Windows compatible rename 330bf5e5a5bSAndreas Gohr * 331bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows 332bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead 333bf5e5a5bSAndreas Gohr */ 334bf5e5a5bSAndreas Gohrfunction io_rename($from,$to){ 335ac9115b0STroels Liebe Bentsen global $conf; 336bf5e5a5bSAndreas Gohr if(!@rename($from,$to)){ 337bf5e5a5bSAndreas Gohr if(@copy($from,$to)){ 33844881d27STroels Liebe Bentsen if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); } 339bf5e5a5bSAndreas Gohr @unlink($from); 340bf5e5a5bSAndreas Gohr return true; 341bf5e5a5bSAndreas Gohr } 342bf5e5a5bSAndreas Gohr return false; 343bf5e5a5bSAndreas Gohr } 344bf5e5a5bSAndreas Gohr return true; 345bf5e5a5bSAndreas Gohr} 346bf5e5a5bSAndreas Gohr 347bf5e5a5bSAndreas Gohr 348bf5e5a5bSAndreas Gohr/** 349f3f0262cSandi * Runs an external command and returns it's output as string 35015fae107Sandi * 35115fae107Sandi * @author Harry Brueckner <harry_b@eml.cc> 35215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 3533dc3a5f1Sandi * @deprecated 354f3f0262cSandi */ 355f3f0262cSandifunction io_runcmd($cmd){ 356f3f0262cSandi $fh = popen($cmd, "r"); 357f3f0262cSandi if(!$fh) return false; 358f3f0262cSandi $ret = ''; 359f3f0262cSandi while (!feof($fh)) { 360f3f0262cSandi $ret .= fread($fh, 8192); 361f3f0262cSandi } 362f3f0262cSandi pclose($fh); 363f3f0262cSandi return $ret; 364f3f0262cSandi} 365f3f0262cSandi 366340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 367