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