-
Notifications
You must be signed in to change notification settings - Fork 9
/
filewrite-ftp.lib.php
executable file
·235 lines (208 loc) · 6.98 KB
/
filewrite-ftp.lib.php
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
<?php
/*
*
* Filecharger FTP Write Library
* by Zarel of Novawave
*
* Module - do not delete unless you don't need FTP access functionality
* and you know what you're doing.
*
*/
if ($ext_pclzip) include 'addons/pclzip.lib.php';
$ftp = false;
function fm_ftpconnect()
{
global $ftp, $ftp_server, $ftp_port, $ftp_username, $ftp_password, $ftp_passive, $ftp_ftps;
if ($ftp) return true;
if ($ftp_ftps)
{
if (!($ftp = @ftp_ssl_connect($ftp_server, $ftp_port))) return false;
}
else
{
if (!($ftp = @ftp_connect($ftp_server, $ftp_port))) return false;
}
if (!@ftp_login($ftp, $ftp_username, $ftp_password))
{
@ftp_close($ftp);
return ($ftp = false);
}
if ($ftp_passive) @ftp_pasv();
return true;
}
function fm_sanitize($file)
{
if (!fm_isdir($file))
{
$newfile = $file; aphp($newfile);
if ($newfile !== $file) fm_rename($file, $newfile, $true) or fm_delete($file);
return true;
}
if (substr('/'.$file,-1)!='/') $file .= '/';
$cfiles = fm_fastgetfiles($file);
foreach ($cfiles as $cfile)
{
if ($cfile == '.' || $cfile == '..') continue;
fm_sanitize($file.$cfile);
}
return true;
}
function fm_copy($source, $dest, $overwrite = false)
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
$success = true;
if (!fm_isdir($source))
{
aphp($dest);
if (fm_exists($dest) && !$overwrite) return false;
if (($local = fm_mklocal($source))===false) return false;
$dest = fm_prepareoverwrite($dest, $overwrite);
if (!$dest) return false;
return @ftp_put($ftp,$ftp_prepath.$dest,$local,FTP_BINARY);
}
// A hack, but this is the only way FTP will let us do it.
if (substr('/'.$source,-1)!='/') $source .= '/'; // enforce trailing slash
if (substr('/'.$dest,-1)!='/') $dest .= '/'; // enforce trailing slash
if (!fm_exists($dest) && !fm_mkdir($dest)) return false;
$csources = fm_fastgetfiles($source);
foreach ($csources as $csource)
{
if ($csource == '.' || $csource == '..') continue;
if (!fm_copy($source.$csource,$dest.$csource,$overwrite)) $success = false;
}
return $success;
}
function fm_editfile($source, $data)
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
aphp($source);
$fhandle = @fopen('cache/temp.bin','w');
if ($fhandle === false) return false;
@fwrite($fhandle,$data);
@fclose($fhandle);
return @ftp_put($ftp,$ftp_prepath.$source,'cache/temp.bin',FTP_BINARY);
}
function fm_upload($source,$dest,$overwrite=false)
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
if (!is_dir('cache/up/'.$source))
{
aphp($dest);
$dest = fm_prepareoverwrite($dest, $overwrite);
if (!$dest) return false;
$success = @ftp_put($ftp,$ftp_prepath.$dest,'cache/up/'.$source,FTP_BINARY);
if ($success) @unlink('cache/up/'.$source);
return $success;
}
// A hack, but this is the only way FTP will let us do it.
$success = true;
if (substr('/'.$source,-1)!='/') $source .= '/'; // enforce trailing slash
if (substr('/'.$dest,-1)!='/') $dest .= '/'; // enforce trailing slash
if (!fm_exists($dest) && !fm_mkdir($dest)) return false;
if (!($handle = @opendir('cache/up/'.$source))) return false;
while (false !== ($csource = readdir($handle)))
{
if ($csource == '.' || $csource == '..') continue;
if (!fm_upload($source.$csource,$dest.$csource,$overwrite)) $success = false;
}
@closedir($handle);
@rmdir('cache/up/'.$source);
return $success;
}
function fm_urlupload($url,$dest='',$overwrite=false)
{
global $prepath;
if (substr($url,0,7)!=='http://') 'http://'.$url;
if (substr('/'.$dest,-1)=='/') $dest .= basename($url);
aphp($dest);
if (!$dest) return false;
if (!@copy($url,'cache/up/temp.bin')) return false;
return fm_upload('temp.bin',$dest,$overwrite);
}
function fm_move($old,$new,$overwrite=false) { return fm_rename($old,$new,$overwrite=false); }
function fm_rename($old,$new,$overwrite=false) // or move
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
aphp($new);
$new = fm_prepareoverwrite($new, $overwrite);
if (!$new) return false;
return @ftp_rename($ftp,$ftp_prepath.$old,$ftp_prepath.$new);
}
function fm_delete($source)
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
if (!fm_isdir($source))
return @ftp_delete($ftp, $ftp_prepath.$source);
if (substr('/'.$source,-1)!='/') $source .= '/'; // enforce trailing slash
$csources = fm_fastgetfiles($source);
foreach ($csources as $csource)
{
if ($csource == '.' || $csource == '..') continue 1;
fm_delete($source.$csource);
}
return @ftp_rmdir($ftp, substr($ftp_prepath.$source,0,-1));
}
function fm_mkdir($source,$overwrite=false)
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
aphp($source);
if (fm_exists($source)) return $overwrite?true:false;
return @ftp_mkdir($ftp,$ftp_prepath.$source);
}
function fm_mkfile($dest,$overwrite=false,$content='')
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
aphp($dest);
$dest = fm_prepareoverwrite($dest, $overwrite);
if (!$dest) return false;
$fhandle = @fopen('cache/temp.bin','w');
if ($fhandle === false) return false;
if ($content) @fwrite($fhandle,$content);
@fclose($fhandle);
return @ftp_put($ftp,$ftp_prepath.$dest,'cache/temp.bin',FTP_BINARY);
}
function fm_chmod($file,$perms)
{
global $ftp,$ftp_prepath; if (!fm_ftpconnect()) return false;
$ofile = $file; aphp($file);
if ($ofile !== $file) fm_copy($ofile,$file);
return @ftp_chmod($ftp,octdec($perms),$ftp_prepath.$file);
}
function fm_iswritable($source)
{
if (startsWith(cleanPath($source),str_repeat('../',1+substr_count($GLOBALS['ftp_prepath'],'/')))) return FALSE;
//$fileinfo = fm_fileinfo($source);
//return (intval(substr(@($fileinfo['perms']),0,1))&2)?TRUE:FALSE;
return true;
}
function fm_extractzip($file,$dest=FALSE,$overwrite=FALSE)
{
if (!$GLOBALS['ext_pclzip']) return false;
$archive = new PclZip(fm_mklocal($file));
if (is_dir('cache/up/extracted/')) fm_clearcache('up/extracted/');
@mkdir('cache/up/extracted/');
$res = $archive->extract(PCLZIP_OPT_PATH, 'cache/up/extracted/');
if ($res == 0) return FALSE;
$res = fm_upload('extracted/',$dest,$overwrite);
fm_clearcache('up/extracted/');
return $res;
}
function fm_compresszip($files, $dest=FALSE, $overwrite=FALSE)
{
if (file_exists($dest) && !$overwrite || !count($files)) return FALSE;
else fm_delete($dest);
@unlink('cache/up/temp.zip');
$lfiles = fm_mkalllocal($files);
$archive = new PclZip('cache/up/temp.zip');
$archive->create($lfiles, PCLZIP_OPT_REMOVE_PATH, upOne($lfiles[0]));
fm_recachedir('down/local/');
$res = fm_upload('temp.zip',$dest,$overwrite);
@unlink('cache/up/temp.zip');
return $res;
}
function fm_close()
{
global $ftp;
if ($ftp) ftp_close($ftp);
}
?>