1<? 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 9 require_once("inc/common.php"); 10 require_once("inc/parser.php"); 11 12/** 13 * Returns the parsed text from the given sourcefile. Uses cache 14 * if exists. Creates it if not. 15 * 16 * @author Andreas Gohr <andi@splitbrain.org> 17 */ 18function io_cacheParse($file){ 19 global $conf; 20 global $CACHEGROUP; 21 global $parser; //we read parser options 22 $parsed = ''; 23 $cache = $conf['datadir'].'/.cache/'; 24 $cache .= md5($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].$CACHEGROUP); 25 $purge = $conf['datadir'].'/.cache/purgefile'; 26 27 // check if cache can be used 28 $cachetime = @filemtime($cache); 29 30 if( @file_exists($cache) // does the cachefile exist 31 && @file_exists($file) // and does the source exist 32 && !isset($_REQUEST['purge']) // no purge param was set 33 && filesize($cache) // and contains the cachefile any data 34 && ((time() - $cachetime) < $conf['cachetime']) // and is cachefile young enough 35 && ($cachetime > filemtime($file)) // and newer than the source 36 && ($cachetime > @filemtime($purge)) // and newer than the purgefile 37 && ($cachetime > filemtime('conf/dokuwiki.php')) // and newer than the config file 38 && ($cachetime > @filemtime('conf/local.php')) // and newer than the local config file 39 && ($cachetime > filemtime('inc/parser.php')) // and newer than the parser 40 && ($cachetime > filemtime('inc/format.php'))) // and newer than the formating functions 41 { 42 $parsed = io_readFile($cache); //give back cache 43 $parsed .= "\n<!-- cachefile $cache used -->\n"; 44 }elseif(@file_exists($file)){ 45 $parsed = parse(io_readFile($file)); //sets global parseroptions 46 if($parser['cache']){ 47 io_saveFile($cache,$parsed); //save cachefile 48 $parsed .= "\n<!-- no cachefile used, but created -->\n"; 49 }else{ 50 @unlink($cache); //try to delete cachefile 51 $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 52 } 53 } 54 55 return $parsed; 56} 57 58/** 59 * Returns content of $file as cleaned string. 60 * 61 * Uses gzip if extension is .gz 62 * 63 * @author Andreas Gohr <andi@splitbrain.org> 64 */ 65function io_readFile($file){ 66 $ret = ''; 67 if(@file_exists($file)){ 68 if(substr($file,-3) == '.gz'){ 69 $ret = join('',gzfile($file)); 70 }else{ 71 $ret = join('',file($file)); 72 } 73 } 74 return cleanText($ret); 75} 76 77/** 78 * Saves $content to $file. 79 * 80 * Uses gzip if extension is .gz 81 * 82 * @author Andreas Gohr <andi@splitbrain.org> 83 * @return bool true on success 84 */ 85function io_saveFile($file,$content){ 86 io_makeFileDir($file); 87 if(substr($file,-3) == '.gz'){ 88 $fh = @gzopen($file,'wb9'); 89 if(!$fh){ 90 msg("Writing $file failed",-1); 91 return false; 92 } 93 gzwrite($fh, $content); 94 gzclose($fh); 95 }else{ 96 $fh = @fopen($file,'wb'); 97 if(!$fh){ 98 msg("Writing $file failed",-1); 99 return false; 100 } 101 fwrite($fh, $content); 102 fclose($fh); 103 } 104 return true; 105} 106 107/** 108 * Create the directory needed for the given file 109 * 110 * @author Andreas Gohr <andi@splitbrain.org> 111 */ 112function io_makeFileDir($file){ 113 global $conf; 114 115 $dir = dirname($file); 116 umask($conf['dmask']); 117 if(!is_dir($dir)){ 118 io_mkdir_p($dir) || msg("Creating directory $dir failed",-1); 119 } 120 umask($conf['umask']); 121} 122 123/** 124 * Creates a directory hierachy. 125 * 126 * @link http://www.php.net/manual/en/function.mkdir.php 127 * @author <saint@corenova.com> 128 */ 129function io_mkdir_p($target){ 130 if (is_dir($target)||empty($target)) return 1; // best case check first 131 if (@file_exists($target) && !is_dir($target)) return 0; 132 if (io_mkdir_p(substr($target,0,strrpos($target,'/')))) 133 return @mkdir($target,0777); // crawl back up & create dir tree 134 return 0; 135} 136 137/** 138 * Runs an external command and returns it's output as string 139 * 140 * @author Harry Brueckner <harry_b@eml.cc> 141 * @author Andreas Gohr <andi@splitbrain.org> 142 */ 143function io_runcmd($cmd){ 144 $fh = popen($cmd, "r"); 145 if(!$fh) return false; 146 $ret = ''; 147 while (!feof($fh)) { 148 $ret .= fread($fh, 8192); 149 } 150 pclose($fh); 151 return $ret; 152} 153 154?> 155