1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
<?php
/**
* File IO functions
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
require_once(DOKU_INC.'inc/common.php');
/**
* Removes empty directories
*
* @todo use safemode hack
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_sweepNS($id){
global $conf;
//scan all namespaces
while(($id = getNS($id)) !== false){
$dir = $conf['datadir'].'/'.str_replace(':','/',$id);
$dir = utf8_encodeFN($dir);
//try to delete dir else return
if(!@rmdir($dir)) return;
}
}
/**
* Returns content of $file as cleaned string.
*
* Uses gzip if extension is .gz
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_readFile($file){
$ret = '';
if(@file_exists($file)){
if(substr($file,-3) == '.gz'){
$ret = join('',gzfile($file));
}else{
$ret = join('',file($file));
}
}
return cleanText($ret);
}
/**
* Saves $content to $file.
*
* Uses gzip if extension is .gz
*
* @author Andreas Gohr <andi@splitbrain.org>
* @return bool true on success
*/
function io_saveFile($file,$content){
io_makeFileDir($file);
io_lock($file);
if(substr($file,-3) == '.gz'){
$fh = @gzopen($file,'wb9');
if(!$fh){
msg("Writing $file failed",-1);
return false;
}
gzwrite($fh, $content);
gzclose($fh);
}else{
$fh = @fopen($file,'wb');
if(!$fh){
msg("Writing $file failed",-1);
return false;
}
fwrite($fh, $content);
fclose($fh);
}
io_unlock($file);
return true;
}
/**
* Tries to lock a file
*
* Locking is only done for io_savefile and uses directories
* inside $conf['lockdir']
*
* It waits maximal 3 seconds for the lock, after this time
* the lock is assumed to be stale and the function goes on
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_lock($file){
global $conf;
// no locking if safemode hack
if($conf['safemodehack']) return;
$lockDir = $conf['lockdir'].'/'.md5($file);
@ignore_user_abort(1);
$timeStart = time();
do {
//waited longer than 3 seconds? -> stale lock
if ((time() - $timeStart) > 3) break;
$locked = @mkdir($lockDir);
} while ($locked === false);
}
/**
* Unlocks a file
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_unlock($file){
global $conf;
// no locking if safemode hack
if($conf['safemodehack']) return;
$lockDir = $conf['lockdir'].'/'.md5($file);
@rmdir($lockDir);
@ignore_user_abort(0);
}
/**
* Create the directory needed for the given file
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_makeFileDir($file){
global $conf;
$dir = dirname($file);
umask($conf['dmask']);
if(!is_dir($dir)){
io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
}
umask($conf['umask']);
}
/**
* Creates a directory hierachy.
*
* @link http://www.php.net/manual/en/function.mkdir.php
* @author <saint@corenova.com>
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_mkdir_p($target){
global $conf;
if (is_dir($target)||empty($target)) return 1; // best case check first
if (@file_exists($target) && !is_dir($target)) return 0;
//recursion
if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
if($conf['safemodehack']){
$dir = preg_replace('/^'.preg_quote(realpath($conf['ftp']['root']),'/').'/','', $target);
return io_mkdir_ftp($dir);
}else{
return @mkdir($target,0777); // crawl back up & create dir tree
}
}
return 0;
}
/**
* Creates a directory using FTP
*
* This is used when the safemode workaround is enabled
*
* @author <andi@splitbrain.org>
*/
function io_mkdir_ftp($dir){
global $conf;
if(!function_exists('ftp_connect')){
msg("FTP support not found - safemode workaround not usable",-1);
return false;
}
$conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
if(!$conn){
msg("FTP connection failed",-1);
return false;
}
if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){
msg("FTP login failed",-1);
return false;
}
//create directory
$ok = @ftp_mkdir($conn, $dir);
//set permissions (using the directory umask)
@ftp_site($conn,sprintf("CHMOD %04o %s",(0777 - $conf['dmask']),$dir));
@ftp_close($conn);
return $ok;
}
/**
* downloads a file from the net and saves it to the given location
*
* @author Andreas Gohr <andi@splitbrain.org>
* @todo Add size limit
*/
function io_download($url,$file){
$fp = @fopen($url,"rb");
if(!$fp) return false;
$kb = 0;
$now = time();
while(!feof($fp)){
if($kb++ > 2048 || (time() - $now) > 45){
//abort on 2 MB and timeout on 45 sec
return false;
}
$cont.= fread($fp,1024);
}
fclose($fp);
$fp2 = @fopen($file,"w");
if(!$fp2) return false;
fwrite($fp2,$cont);
fclose($fp2);
return true;
}
/**
* Runs an external command and returns it's output as string
*
* @author Harry Brueckner <harry_b@eml.cc>
* @author Andreas Gohr <andi@splitbrain.org>
* @deprecated
*/
function io_runcmd($cmd){
$fh = popen($cmd, "r");
if(!$fh) return false;
$ret = '';
while (!feof($fh)) {
$ret .= fread($fh, 8192);
}
pclose($fh);
return $ret;
}
//Setup VIM: ex: et ts=2 enc=utf-8 :
|