xref: /dokuwiki/inc/io.php (revision bc3b6aec0f5bdef988488010807a94bee0808426)
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 * Removes empty directories
14 *
15 * @todo use safemode hack
16 * @author  Andreas Gohr <andi@splitbrain.org>
17 */
18function io_sweepNS($id){
19  global $conf;
20
21  //scan all namespaces
22  while(($id = getNS($id)) !== false){
23		$dir = $conf['datadir'].'/'.str_replace(':','/',$id);
24    $dir = utf8_encodeFN($dir);
25
26    //try to delete dir else return
27    if(!@rmdir($dir)) return;
28  }
29}
30
31/**
32 * Returns content of $file as cleaned string.
33 *
34 * Uses gzip if extension is .gz
35 *
36 * @author  Andreas Gohr <andi@splitbrain.org>
37 */
38function io_readFile($file){
39  $ret = '';
40  if(@file_exists($file)){
41    if(substr($file,-3) == '.gz'){
42      $ret = join('',gzfile($file));
43    }else{
44      $ret = join('',file($file));
45    }
46  }
47  return cleanText($ret);
48}
49
50/**
51 * Saves $content to $file.
52 *
53 * Uses gzip if extension is .gz
54 *
55 * @author  Andreas Gohr <andi@splitbrain.org>
56 * @return bool true on success
57 */
58function io_saveFile($file,$content){
59  io_makeFileDir($file);
60  if(substr($file,-3) == '.gz'){
61    $fh = @gzopen($file,'wb9');
62    if(!$fh){
63      msg("Writing $file failed",-1);
64      return false;
65    }
66    gzwrite($fh, $content);
67    gzclose($fh);
68  }else{
69    $fh = @fopen($file,'wb');
70    if(!$fh){
71      msg("Writing $file failed",-1);
72      return false;
73    }
74    fwrite($fh, $content);
75    fclose($fh);
76  }
77  return true;
78}
79
80/**
81 * Create the directory needed for the given file
82 *
83 * @author  Andreas Gohr <andi@splitbrain.org>
84 */
85function io_makeFileDir($file){
86  global $conf;
87
88  $dir = dirname($file);
89  if($conf['safemodehack']){
90    $dir = preg_replace('/^'.preg_quote(realpath($conf['ftp']['root']),'/').'/','',$dir);
91  }
92  umask($conf['dmask']);
93  if(!is_dir($dir)){
94    io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
95  }
96  umask($conf['umask']);
97}
98
99/**
100 * Creates a directory hierachy.
101 *
102 * @link    http://www.php.net/manual/en/function.mkdir.php
103 * @author  <saint@corenova.com>
104 * @author  Andreas Gohr <andi@splitbrain.org>
105 */
106function io_mkdir_p($target){
107  global $conf;
108  if (is_dir($target)||empty($target)) return 1; // best case check first
109  if (@file_exists($target) && !is_dir($target)) return 0;
110  //recursion
111  if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
112    if($conf['safemodehack']){
113      return io_mkdir_ftp($target);
114    }else{
115      return @mkdir($target,0777); // crawl back up & create dir tree
116    }
117  }
118  return 0;
119}
120
121/**
122 * Creates a directory using FTP
123 *
124 * This is used when the safemode workaround is enabled
125 *
126 * @author <andi@splitbrain.org>
127 */
128function io_mkdir_ftp($dir){
129  global $conf;
130
131  if(!function_exists('ftp_connect')){
132    msg("FTP support not found - safemode workaround not usable",-1);
133    return false;
134  }
135
136  $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
137  if(!$conn){
138    msg("FTP connection failed",-1);
139    return false;
140  }
141
142  if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){
143    msg("FTP login failed",-1);
144    return false;
145  }
146
147//FIXME silence those commands again!
148  //create directory
149  $ok = ftp_mkdir($conn, $dir);
150  //set permissions (using the directory umask)
151  ftp_site($conn,sprintf("CHMOD %04o %s",(0777 - $conf['dmask']),$dir));
152
153  ftp_close($conn);
154  return $ok;
155}
156
157/**
158 * downloads a file from the net and saves it to the given location
159 *
160 * @author Andreas Gohr <andi@splitbrain.org>
161 * @todo   Add size limit
162 */
163function io_download($url,$file){
164  $fp = @fopen($url,"rb");
165  if(!$fp) return false;
166
167  $kb  = 0;
168  $now = time();
169
170  while(!feof($fp)){
171    if($kb++ > 2048 || (time() - $now) > 45){
172      //abort on 2 MB and timeout on 45 sec
173      return false;
174    }
175    $cont.= fread($fp,1024);
176  }
177  fclose($fp);
178
179  $fp2 = @fopen($file,"w");
180  if(!$fp2) return false;
181  fwrite($fp2,$cont);
182  fclose($fp2);
183  return true;
184}
185
186/**
187 * Runs an external command and returns it's output as string
188 *
189 * @author Harry Brueckner <harry_b@eml.cc>
190 * @author Andreas Gohr <andi@splitbrain.org>
191 * @deprecated
192 */
193function io_runcmd($cmd){
194  $fh = popen($cmd, "r");
195  if(!$fh) return false;
196  $ret = '';
197  while (!feof($fh)) {
198    $ret .= fread($fh, 8192);
199  }
200  pclose($fh);
201  return $ret;
202}
203
204
205//Setup VIM: ex: et ts=2 enc=utf-8 :
206