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