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){ 67*ac9115b0STroels Liebe Bentsen global $conf; 681380fc45SAndreas Gohr $mode = ($append) ? 'ab' : 'wb'; 691380fc45SAndreas Gohr 70*ac9115b0STroels 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 } 90*ac9115b0STroels Liebe Bentsen 91*ac9115b0STroels Liebe Bentsen if(!$fileexists && $conf['fmode'] != 0666) { chmod($file, $conf['fmode']); } 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; 18190eb8392Sandi $locked = @mkdir($lockDir); 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*ac9115b0STroels Liebe Bentsen umask($conf['umask']); 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*ac9115b0STroels Liebe Bentsen return @mkdir($target,$conf['dmode']); // crawl back up & create dir tree 2333dc3a5f1Sandi } 2343dc3a5f1Sandi } 235f3f0262cSandi return 0; 236f3f0262cSandi} 237f3f0262cSandi 238f3f0262cSandi/** 2393dc3a5f1Sandi * Creates a directory using FTP 2403dc3a5f1Sandi * 2413dc3a5f1Sandi * This is used when the safemode workaround is enabled 2423dc3a5f1Sandi * 2433dc3a5f1Sandi * @author <andi@splitbrain.org> 2443dc3a5f1Sandi */ 2453dc3a5f1Sandifunction io_mkdir_ftp($dir){ 2463dc3a5f1Sandi global $conf; 2473dc3a5f1Sandi 2483dc3a5f1Sandi if(!function_exists('ftp_connect')){ 2493dc3a5f1Sandi msg("FTP support not found - safemode workaround not usable",-1); 2503dc3a5f1Sandi return false; 2513dc3a5f1Sandi } 2523dc3a5f1Sandi 2533dc3a5f1Sandi $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10); 2543dc3a5f1Sandi if(!$conn){ 2553dc3a5f1Sandi msg("FTP connection failed",-1); 2563dc3a5f1Sandi return false; 2573dc3a5f1Sandi } 2583dc3a5f1Sandi 2593dc3a5f1Sandi if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){ 2603dc3a5f1Sandi msg("FTP login failed",-1); 2613dc3a5f1Sandi return false; 2623dc3a5f1Sandi } 2633dc3a5f1Sandi 2643dc3a5f1Sandi //create directory 265034138e2SRainer Weinhold $ok = @ftp_mkdir($conn, $dir); 266*ac9115b0STroels Liebe Bentsen //set permissions (using the directory umask and dmode) 267*ac9115b0STroels Liebe Bentsen @ftp_site($conn,sprintf("CHMOD %04o %s",($conf['dmode'] & ~$conf['umask']),$dir)); 2683dc3a5f1Sandi 269034138e2SRainer Weinhold @ftp_close($conn); 2703dc3a5f1Sandi return $ok; 2713dc3a5f1Sandi} 2723dc3a5f1Sandi 2733dc3a5f1Sandi/** 27473ccfcb9Schris * downloads a file from the net and saves it 27573ccfcb9Schris * 27673ccfcb9Schris * if $useAttachment is false, 27773ccfcb9Schris * - $file is the full filename to save the file, incl. path 27873ccfcb9Schris * - if successful will return true, false otherwise 27973ccfcb9Schris 28073ccfcb9Schris * if $useAttachment is true, 28173ccfcb9Schris * - $file is the directory where the file should be saved 28273ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise 283b625487dSandi * 284b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 28573ccfcb9Schris * @author Chris Smith <chris@jalakai.co.uk> 286b625487dSandi */ 28773ccfcb9Schrisfunction io_download($url,$file,$useAttachment=false,$defaultName=''){ 288*ac9115b0STroels Liebe Bentsen global $conf; 2899b307a83SAndreas Gohr $http = new DokuHTTPClient(); 2909b307a83SAndreas Gohr $http->max_bodysize = 2*1024*1024; //max. 2MB 2919b307a83SAndreas Gohr $http->timeout = 25; //max. 25 sec 2929b307a83SAndreas Gohr 2939b307a83SAndreas Gohr $data = $http->get($url); 2949b307a83SAndreas Gohr if(!$data) return false; 2959b307a83SAndreas Gohr 29673ccfcb9Schris if ($useAttachment) { 29773ccfcb9Schris $name = ''; 29873ccfcb9Schris if (isset($http->resp_headers['content-disposition'])) { 29973ccfcb9Schris $content_disposition = $http->resp_headers['content-disposition']; 300ce070a9fSchris $match=array(); 30173ccfcb9Schris if (is_string($content_disposition) && 302ce070a9fSchris preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) { 30373ccfcb9Schris 30473ccfcb9Schris $name = basename($match[1]); 30573ccfcb9Schris } 30673ccfcb9Schris 30773ccfcb9Schris } 30873ccfcb9Schris 30973ccfcb9Schris if (!$name) { 31073ccfcb9Schris if (!$defaultName) return false; 31173ccfcb9Schris $name = $defaultName; 31273ccfcb9Schris } 31373ccfcb9Schris 31473ccfcb9Schris $file = $file.$name; 31573ccfcb9Schris } 31673ccfcb9Schris 317*ac9115b0STroels Liebe Bentsen $fileexists = file_exists($file); 318*ac9115b0STroels Liebe Bentsen umask($conf['umask']); 3199b307a83SAndreas Gohr $fp = @fopen($file,"w"); 320b625487dSandi if(!$fp) return false; 3219b307a83SAndreas Gohr fwrite($fp,$data); 322b625487dSandi fclose($fp); 323*ac9115b0STroels Liebe Bentsen if(!$fileexists && $conf['fmode'] != 0666) { chmod($file, $conf['fmode']); } 32473ccfcb9Schris if ($useAttachment) return $name; 325b625487dSandi return true; 326b625487dSandi} 327b625487dSandi 328b625487dSandi/** 329*ac9115b0STroels 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){ 335*ac9115b0STroels Liebe Bentsen global $conf; 336bf5e5a5bSAndreas Gohr if(!@rename($from,$to)){ 337bf5e5a5bSAndreas Gohr if(@copy($from,$to)){ 338*ac9115b0STroels Liebe Bentsen if($conf['fmode'] != 0666) { chmod($file, $conf['fmode']); } 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 367340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 368