xref: /dokuwiki/inc/io.php (revision a2d649c465f8f77c2b1dfdcc3724e69fde78a296)
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
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 * @deprecated -> parserutils
18 */
19function io_cacheParse($file){
20  trigger_error("deprecated io_cacheParse called");
21
22  global $conf;
23  global $CACHEGROUP;
24  global $parser; //we read parser options
25  $parsed = '';
26  $cache  = $conf['datadir'].'/_cache/';
27  $cache .= md5($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].$CACHEGROUP);
28  $purge  = $conf['datadir'].'/_cache/purgefile';
29
30  // check if cache can be used
31  $cachetime = @filemtime($cache);
32
33  if(   @file_exists($cache)                          // does the cachefile exist
34     && @file_exists($file)                           // and does the source exist
35     && !isset($_REQUEST['purge'])                    // no purge param was set
36     && filesize($cache)                              // and contains the cachefile any data
37     && ((time() - $cachetime) < $conf['cachetime'])  // and is cachefile young enough
38     && ($cachetime > filemtime($file))               // and newer than the source
39     && ($cachetime > @filemtime($purge))             // and newer than the purgefile
40     && ($cachetime > filemtime('conf/dokuwiki.php')) // and newer than the config file
41     && ($cachetime > @filemtime('conf/local.php'))   // and newer than the local config file
42     && ($cachetime > filemtime('inc/parser.php'))    // and newer than the parser
43     && ($cachetime > filemtime('inc/format.php')))   // and newer than the formating functions
44  {
45    $parsed  = io_readFile($cache); //give back cache
46    $parsed .= "\n<!-- cachefile $cache used -->\n";
47  }elseif(@file_exists($file)){
48    $parsed = parse(io_readFile($file)); //sets global parseroptions
49    if($parser['cache']){
50      io_saveFile($cache,$parsed); //save cachefile
51      $parsed .= "\n<!-- no cachefile used, but created -->\n";
52    }else{
53      @unlink($cache); //try to delete cachefile
54      $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
55    }
56  }
57
58  return $parsed;
59}
60
61/**
62 * Removes empty directories
63 *
64 * @todo use safemode hack
65 * @author  Andreas Gohr <andi@splitbrain.org>
66 */
67function io_sweepNS($id){
68  global $conf;
69
70  //scan all namespaces
71  while(($id = getNS($id)) !== false){
72		$dir = $conf['datadir'].'/'.str_replace(':','/',$id);
73    $dir = utf8_encodeFN($dir);
74
75    //try to delete dir else return
76    if(!@rmdir($dir)) return;
77  }
78}
79
80/**
81 * Returns content of $file as cleaned string.
82 *
83 * Uses gzip if extension is .gz
84 *
85 * @author  Andreas Gohr <andi@splitbrain.org>
86 */
87function io_readFile($file){
88  $ret = '';
89  if(@file_exists($file)){
90    if(substr($file,-3) == '.gz'){
91      $ret = join('',gzfile($file));
92    }else{
93      $ret = join('',file($file));
94    }
95  }
96  return cleanText($ret);
97}
98
99/**
100 * Saves $content to $file.
101 *
102 * Uses gzip if extension is .gz
103 *
104 * @author  Andreas Gohr <andi@splitbrain.org>
105 * @return bool true on success
106 */
107function io_saveFile($file,$content){
108  io_makeFileDir($file);
109  if(substr($file,-3) == '.gz'){
110    $fh = @gzopen($file,'wb9');
111    if(!$fh){
112      msg("Writing $file failed",-1);
113      return false;
114    }
115    gzwrite($fh, $content);
116    gzclose($fh);
117  }else{
118    $fh = @fopen($file,'wb');
119    if(!$fh){
120      msg("Writing $file failed",-1);
121      return false;
122    }
123    fwrite($fh, $content);
124    fclose($fh);
125  }
126  return true;
127}
128
129/**
130 * Create the directory needed for the given file
131 *
132 * @author  Andreas Gohr <andi@splitbrain.org>
133 */
134function io_makeFileDir($file){
135  global $conf;
136
137  $dir = dirname($file);
138  if($conf['safemodehack']){
139    $dir = preg_replace('/^'.preg_quote(realpath($conf['ftp']['root']),'/').'/','',$dir);
140  }
141  umask($conf['dmask']);
142  if(!is_dir($dir)){
143    io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
144  }
145  umask($conf['umask']);
146}
147
148/**
149 * Creates a directory hierachy.
150 *
151 * @link    http://www.php.net/manual/en/function.mkdir.php
152 * @author  <saint@corenova.com>
153 * @author  Andreas Gohr <andi@splitbrain.org>
154 */
155function io_mkdir_p($target){
156  global $conf;
157  if (is_dir($target)||empty($target)) return 1; // best case check first
158  if (@file_exists($target) && !is_dir($target)) return 0;
159  //recursion
160  if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
161    if($conf['safemodehack']){
162      return io_mkdir_ftp($target);
163    }else{
164      return @mkdir($target,0777); // crawl back up & create dir tree
165    }
166  }
167  return 0;
168}
169
170/**
171 * Creates a directory using FTP
172 *
173 * This is used when the safemode workaround is enabled
174 *
175 * @author <andi@splitbrain.org>
176 */
177function io_mkdir_ftp($dir){
178  global $conf;
179
180  if(!function_exists('ftp_connect')){
181    msg("FTP support not found - safemode workaround not usable",-1);
182    return false;
183  }
184
185  $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
186  if(!$conn){
187    msg("FTP connection failed",-1);
188    return false;
189  }
190
191  if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){
192    msg("FTP login failed",-1);
193    return false;
194  }
195
196//FIXME silence those commands again!
197  //create directory
198  $ok = ftp_mkdir($conn, $dir);
199  //set permissions (using the directory umask)
200  ftp_site($conn,sprintf("CHMOD %04o %s",$perm & (0777 - $conf['dmask']),$dir));
201
202  ftp_close($conn);
203  return $ok;
204}
205
206/**
207 * downloads a file from the net and saves it to the given location
208 *
209 * @author Andreas Gohr <andi@splitbrain.org>
210 * @todo   Add size limit
211 */
212function io_download($url,$file){
213  $fp = @fopen($url,"rb");
214  if(!$fp) return false;
215
216  $kb  = 0;
217  $now = time();
218
219  while(!feof($fp)){
220    if($kb++ > 2048 || (time() - $now) > 45){
221      //abort on 2 MB and timeout on 45 sec
222      return false;
223    }
224    $cont.= fread($fp,1024);
225  }
226  fclose($fp);
227
228  $fp2 = @fopen($file,"w");
229  if(!$fp2) return false;
230  fwrite($fp2,$cont);
231  fclose($fp2);
232  return true;
233}
234
235/**
236 * Runs an external command and returns it's output as string
237 *
238 * @author Harry Brueckner <harry_b@eml.cc>
239 * @author Andreas Gohr <andi@splitbrain.org>
240 * @deprecated
241 */
242function io_runcmd($cmd){
243  $fh = popen($cmd, "r");
244  if(!$fh) return false;
245  $ret = '';
246  while (!feof($fh)) {
247    $ret .= fread($fh, 8192);
248  }
249  pclose($fh);
250  return $ret;
251}
252
253
254//Setup VIM: ex: et ts=2 enc=utf-8 :
255