| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* Net_FTP socket implementation of FTP functions.
|
|
|
7 |
*
|
|
|
8 |
* The functions in this file emulate the ext/FTP functions through
|
|
|
9 |
* ext/Socket.
|
|
|
10 |
*
|
|
|
11 |
* PHP versions 4 and 5
|
|
|
12 |
*
|
|
|
13 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
14 |
* that is available through the world-wide-web at the following URI:
|
|
|
15 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
16 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
17 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
18 |
*
|
|
|
19 |
* @category Networking
|
|
|
20 |
* @package FTP
|
|
|
21 |
* @author Tobias Schlitt <toby@php.net>
|
|
|
22 |
* @copyright 1997-2008 The PHP Group
|
|
|
23 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
24 |
* @version CVS: $Id: Socket.php,v 1.5.2.2 2008/04/22 19:47:08 jschippers Exp $
|
|
|
25 |
* @link http://pear.php.net/package/Net_FTP
|
|
|
26 |
* @since File available since Release 0.0.1
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
error_reporting(E_ALL);
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Default FTP extension constants
|
|
|
33 |
*/
|
|
|
34 |
define('FTP_ASCII', 0);
|
|
|
35 |
define('FTP_TEXT', 0);
|
|
|
36 |
define('FTP_BINARY', 1);
|
|
|
37 |
define('FTP_IMAGE', 1);
|
|
|
38 |
define('FTP_TIMEOUT_SEC', 0);
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* What needs to be done overall?
|
|
|
42 |
* #1 Install the rest of these functions
|
|
|
43 |
* #2 Document better
|
|
|
44 |
* #3 Alot of other things I don't remember
|
|
|
45 |
*/
|
|
|
46 |
|
|
|
47 |
/*
|
|
|
48 |
* !!! NOTE !!!
|
|
|
49 |
* Most of the comment's are "not working",
|
|
|
50 |
* meaning they are not all up-to-date
|
|
|
51 |
* !!! NOTE !!!
|
|
|
52 |
*/
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* &resource ftp_connect ( string host [, int port [, int timeout ] ] );
|
|
|
56 |
*
|
|
|
57 |
* Opens an FTP connection and return resource or false on failure.
|
|
|
58 |
*
|
|
|
59 |
* FTP Success respons code: 220
|
|
|
60 |
*
|
|
|
61 |
* @param string $host Host to connect to
|
|
|
62 |
* @param int $port Optional, port to connect to
|
|
|
63 |
* @param int $timeout Optional, seconds until function timeouts
|
|
|
64 |
*
|
|
|
65 |
* @todo The FTP extension has ftp_get_option() function which returns the
|
|
|
66 |
* timeout variable. This function needs to be created and contain it as
|
|
|
67 |
* static variable.
|
|
|
68 |
* @todo The FTP extension has ftp_set_option() function which sets the
|
|
|
69 |
* timeout variable. This function needs to be created and called here.
|
|
|
70 |
* @access public
|
|
|
71 |
* @return &resource
|
|
|
72 |
*/
|
|
|
73 |
function &ftp_connect($host, $port = 21, $timeout = 90)
|
|
|
74 |
{
|
|
|
75 |
$false = false; // We are going to return refrence (E_STRICT)
|
|
|
76 |
|
|
|
77 |
if (!is_string($host) || !is_integer($port) || !is_integer($timeout)) {
|
|
|
78 |
return $false;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$control = @fsockopen($host, $port, $iError, $sError,
|
|
|
82 |
$timeout);
|
|
|
83 |
$GLOBALS['_NET_FTP']['timeout'] = $timeout;
|
|
|
84 |
|
|
|
85 |
if (!is_resource($control)) {
|
|
|
86 |
return $false;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
stream_set_blocking($control, true);
|
|
|
90 |
stream_set_timeout($control, $timeout);
|
|
|
91 |
|
|
|
92 |
do {
|
|
|
93 |
$content[] = fgets($control, 8129);
|
|
|
94 |
$array = socket_get_status($control);
|
|
|
95 |
} while ($array['unread_bytes'] > 0);
|
|
|
96 |
|
|
|
97 |
if (substr($content[count($content)-1], 0, 3) == 220) {
|
|
|
98 |
return $control;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $false;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* boolean ftp_login ( resource stream, string username, string password );
|
|
|
106 |
*
|
|
|
107 |
* Logs in to an given FTP connection stream.
|
|
|
108 |
* Returns TRUE on success or FALSE on failure.
|
|
|
109 |
*
|
|
|
110 |
* NOTE:
|
|
|
111 |
* Username and password are *not* optional. Function will *not*
|
|
|
112 |
* assume "anonymous" if username and/or password is empty
|
|
|
113 |
*
|
|
|
114 |
* FTP Success respons code: 230
|
|
|
115 |
*
|
|
|
116 |
* @param resource &$control FTP resource to login to
|
|
|
117 |
* @param string $username FTP Username to be used
|
|
|
118 |
* @param string $password FTP Password to be used
|
|
|
119 |
*
|
|
|
120 |
* @access public
|
|
|
121 |
* @return boolean
|
|
|
122 |
*/
|
|
|
123 |
function ftp_login(&$control, $username, $password)
|
|
|
124 |
{
|
|
|
125 |
if (!is_resource($control) || is_null($username)) {
|
|
|
126 |
return false;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
fputs($control, 'USER '.$username."\r\n");
|
|
|
130 |
$contents = array();
|
|
|
131 |
do {
|
|
|
132 |
$contents[] = fgets($control, 8192);
|
|
|
133 |
$array = socket_get_status($control);
|
|
|
134 |
} while ($array['unread_bytes'] > 0);
|
|
|
135 |
|
|
|
136 |
if (substr($contents[count($contents)-1], 0, 3) != 331) {
|
|
|
137 |
return false;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
fputs($control, 'PASS '.$password."\r\n");
|
|
|
141 |
$contents = array();
|
|
|
142 |
do {
|
|
|
143 |
$contents[] = fgets($control, 8192);
|
|
|
144 |
$array = socket_get_status($control);
|
|
|
145 |
} while ($array['unread_bytes']);
|
|
|
146 |
|
|
|
147 |
if (substr($contents[count($contents)-1], 0, 3) == 230) {
|
|
|
148 |
return true;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
trigger_error('ftp_login() [<a href="function.ftp-login">function.ftp-login'.
|
|
|
152 |
'</a>]: '.$contents[count($contents)-1], E_USER_WARNING);
|
|
|
153 |
|
|
|
154 |
return false;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* boolean ftp_quit ( resource stream );
|
|
|
159 |
*
|
|
|
160 |
* Closes FTP connection.
|
|
|
161 |
* Returns TRUE or FALSE on error.
|
|
|
162 |
*
|
|
|
163 |
* NOTE: The PHP function ftp_quit is *alias* to ftp_close, here it is
|
|
|
164 |
* the *other-way-around* ( ftp_close() is alias to ftp_quit() ).
|
|
|
165 |
*
|
|
|
166 |
* NOTE:
|
|
|
167 |
* resource is set to null since unset() can't unset the variable.
|
|
|
168 |
*
|
|
|
169 |
* @param resource &$control FTP resource
|
|
|
170 |
*
|
|
|
171 |
* @access public
|
|
|
172 |
* @return boolean
|
|
|
173 |
*/
|
|
|
174 |
function ftp_quit(&$control)
|
|
|
175 |
{
|
|
|
176 |
if (!is_resource($control)) {
|
|
|
177 |
return false;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
fputs($control, 'QUIT'."\r\n");
|
|
|
181 |
fclose($control);
|
|
|
182 |
$control = null;
|
|
|
183 |
return true;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Alias to ftp_quit()
|
|
|
188 |
*
|
|
|
189 |
* @param resource &$control FTP resource
|
|
|
190 |
*
|
|
|
191 |
* @see ftp_quit()
|
|
|
192 |
* @access public
|
|
|
193 |
* @return boolean
|
|
|
194 |
*/
|
|
|
195 |
function ftp_close(&$control)
|
|
|
196 |
{
|
|
|
197 |
return ftp_quit($control);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* string ftp_pwd ( resource stream );
|
|
|
202 |
*
|
|
|
203 |
* Gets the current directory name.
|
|
|
204 |
* Returns the current directory.
|
|
|
205 |
*
|
|
|
206 |
* Needs data connection: NO
|
|
|
207 |
* Success response code: 257
|
|
|
208 |
*
|
|
|
209 |
* @param resource &$control FTP resource
|
|
|
210 |
*
|
|
|
211 |
* @access public
|
|
|
212 |
* @return string
|
|
|
213 |
*/
|
|
|
214 |
function ftp_pwd(&$control)
|
|
|
215 |
{
|
|
|
216 |
if (!is_resource($control)) {
|
|
|
217 |
return $control;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
fputs($control, 'PWD'."\r\n");
|
|
|
221 |
|
|
|
222 |
$content = array();
|
|
|
223 |
do {
|
|
|
224 |
$content[] = fgets($control, 8192);
|
|
|
225 |
$array = socket_get_status($control);
|
|
|
226 |
} while ($array['unread_bytes'] > 0);
|
|
|
227 |
|
|
|
228 |
if (substr($cont = $content[count($content)-1], 0, 3) == 257) {
|
|
|
229 |
$pos = strpos($cont, '"')+1;
|
|
|
230 |
$pos2 = strrpos($cont, '"') - $pos;
|
|
|
231 |
$path = substr($cont, $pos, $pos2);
|
|
|
232 |
return $path;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
return false;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* boolean ftp_chdir ( resource stream, string directory );
|
|
|
240 |
*
|
|
|
241 |
* Changes the current directory to the specified directory.
|
|
|
242 |
* Returns TRUE on success or FALSE on failure.
|
|
|
243 |
*
|
|
|
244 |
* FTP success response code: 250
|
|
|
245 |
* Needs data connection: NO
|
|
|
246 |
*
|
|
|
247 |
* @param resource &$control FTP stream
|
|
|
248 |
* @param string $pwd Directory name
|
|
|
249 |
*
|
|
|
250 |
* @access public
|
|
|
251 |
* @return boolean
|
|
|
252 |
*/
|
|
|
253 |
function ftp_chdir(&$control, $pwd)
|
|
|
254 |
{
|
|
|
255 |
if (!is_resource($control) || !is_string($pwd)) {
|
|
|
256 |
return false;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
fputs($control, 'CWD '.$pwd."\r\n");
|
|
|
260 |
$content = array();
|
|
|
261 |
do {
|
|
|
262 |
$content[] = fgets($control, 8192);
|
|
|
263 |
$array = socket_get_status($control);
|
|
|
264 |
} while ($array['unread_bytes'] > 0);
|
|
|
265 |
|
|
|
266 |
if (substr($content[count($content)-1], 0, 3) == 250) {
|
|
|
267 |
return true;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
trigger_error('ftp_chdir() [<a
|
|
|
271 |
href="function.ftp-chdir">function.ftp-chdir</a>]:
|
|
|
272 |
' .$content[count($content)-1], E_USER_WARNING);
|
|
|
273 |
|
|
|
274 |
return false;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
$_NET_FTP = array();
|
|
|
278 |
$_NET_FTP['USE_PASSIVE'] = false;
|
|
|
279 |
$_NET_FTP['DATA'] = null;
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* boolean ftp_pasv ( resource stream, boolean passive );
|
|
|
283 |
*
|
|
|
284 |
* Toggles passive mode ON/OFF.
|
|
|
285 |
* Returns TRUE on success or FALSE on failure.
|
|
|
286 |
*
|
|
|
287 |
* Comment:
|
|
|
288 |
* Although my lack of C knowlege I checked how the PHP FTP extension
|
|
|
289 |
* do things here. Seems like they create the data connection and store
|
|
|
290 |
* it in object for other functions to use.
|
|
|
291 |
* This is now done here.
|
|
|
292 |
*
|
|
|
293 |
* FTP success response code: 227
|
|
|
294 |
*
|
|
|
295 |
* @param stream &$control FTP stream
|
|
|
296 |
* @param boolean $pasv True to switch to passive, false for active mode
|
|
|
297 |
*
|
|
|
298 |
* @access public
|
|
|
299 |
* @return boolean
|
|
|
300 |
*/
|
|
|
301 |
function ftp_pasv(&$control, $pasv)
|
|
|
302 |
{
|
|
|
303 |
if (!is_resource($control) || !is_bool($pasv)) {
|
|
|
304 |
return false;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
// If data connection exists, destroy it
|
|
|
308 |
if (isset($GLOBALS['_NET_FTP']['DATA'])) {
|
|
|
309 |
fclose($GLOBALS['_NET_FTP']['DATA']);
|
|
|
310 |
$GLOBALS['_NET_FTP']['DATA'] = null;
|
|
|
311 |
|
|
|
312 |
do {
|
|
|
313 |
fgets($control, 16);
|
|
|
314 |
$array = socket_get_status($control);
|
|
|
315 |
} while ($array['unread_bytes'] > 0);
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
// Are we suppost to create active or passive connection?
|
|
|
319 |
if (!$pasv) {
|
|
|
320 |
$GLOBALS['_NET_FTP']['USE_PASSIVE'] = false;
|
|
|
321 |
// Pick random "low bit"
|
|
|
322 |
$low = rand(39, 250);
|
|
|
323 |
// Pick random "high bit"
|
|
|
324 |
$high = rand(39, 250);
|
|
|
325 |
// Lowest possible port would be; 10023
|
|
|
326 |
// Highest possible port would be; 64246
|
|
|
327 |
|
|
|
328 |
$port = ($low<<8)+$high;
|
|
|
329 |
$ip = str_replace('.', ',', $_SERVER['SERVER_ADDR']);
|
|
|
330 |
$s = $ip.','.$low.','.$high;
|
|
|
331 |
|
|
|
332 |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
|
333 |
if (is_resource($socket)) {
|
|
|
334 |
if (socket_bind($socket, '0.0.0.0', $port)) {
|
|
|
335 |
if (socket_listen($socket)) {
|
|
|
336 |
$GLOBALS['_NET_FTP']['DATA'] = &$socket;
|
|
|
337 |
fputs($control, 'PORT '.$s."\r\n");
|
|
|
338 |
$line = fgets($control, 512);
|
|
|
339 |
if (substr($line, 0, 3) == 200) {
|
|
|
340 |
return true;
|
|
|
341 |
}
|
|
|
342 |
}
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
return false;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
// Since we are here, we are suppost to create passive data connection.
|
|
|
349 |
$i = fputs($control, 'PASV' ."\r\n");
|
|
|
350 |
|
|
|
351 |
$content = array();
|
|
|
352 |
do {
|
|
|
353 |
$content[] = fgets($control, 128);
|
|
|
354 |
$array = socket_get_status($control);
|
|
|
355 |
} while ($array['unread_bytes']);
|
|
|
356 |
|
|
|
357 |
if (substr($cont = $content[count($content)-1], 0, 3) != 227) {
|
|
|
358 |
return false;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
$pos = strpos($cont, '(')+1;
|
|
|
362 |
$pos2 = strrpos($cont, ')')-$pos;
|
|
|
363 |
$string = substr($cont, $pos, $pos2);
|
|
|
364 |
|
|
|
365 |
$array = split(',', $string);
|
|
|
366 |
// IP we are connecting to
|
|
|
367 |
$ip = $array[0]. '.' .$array[1]. '.' .$array[2]. '.' .$array[3];
|
|
|
368 |
// Port ( 256*lowbit + highbit
|
|
|
369 |
$port = ($array[4] << 8)+$array[5];
|
|
|
370 |
|
|
|
371 |
// Our data connection
|
|
|
372 |
$data = fsockopen($ip, $port, $iError, $sError,
|
|
|
373 |
$GLOBALS['_NET_FTP']['timeout']);
|
|
|
374 |
|
|
|
375 |
if (is_resource($data)) {
|
|
|
376 |
$GLOBALS['_NET_FTP']['USE_PASSIVE'] = true;
|
|
|
377 |
$GLOBALS['_NET_FTP']['DATA'] = &$data;
|
|
|
378 |
stream_set_blocking($data, true);
|
|
|
379 |
stream_set_timeout($data, $GLOBALS['_NET_FTP']['timeout']);
|
|
|
380 |
|
|
|
381 |
return true;
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
return false;
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
/**
|
|
|
388 |
* array ftp_rawlist ( resource stream, string directory [,bool recursive] );
|
|
|
389 |
*
|
|
|
390 |
* Returns a detailed list of files in the given directory.
|
|
|
391 |
*
|
|
|
392 |
* Needs data connection: YES
|
|
|
393 |
*
|
|
|
394 |
* @param integer &$control FTP resource
|
|
|
395 |
* @param string $pwd Path to retrieve
|
|
|
396 |
* @param boolean $recursive Optional, retrieve recursive listing
|
|
|
397 |
*
|
|
|
398 |
* @todo Enable the recursive feature.
|
|
|
399 |
* @access public
|
|
|
400 |
* @return array
|
|
|
401 |
*/
|
|
|
402 |
function ftp_rawlist(&$control, $pwd, $recursive = false)
|
|
|
403 |
{
|
|
|
404 |
if (!is_resource($control) || !is_string($pwd)) {
|
|
|
405 |
return false;
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
if (!isset($GLOBALS['_NET_FTP']['DATA']) ||
|
|
|
409 |
!is_resource($GLOBALS['_NET_FTP']['DATA'])) {
|
|
|
410 |
ftp_pasv($control, $GLOBALS['_NET_FTP']['USE_PASSIVE']);
|
|
|
411 |
}
|
|
|
412 |
fputs($control, 'LIST '.$pwd."\r\n");
|
|
|
413 |
|
|
|
414 |
$msg = fgets($control, 512);
|
|
|
415 |
if (substr($msg, 0, 3) == 425) {
|
|
|
416 |
return false;
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
$data = &$GLOBALS['_NET_FTP']['DATA'];
|
|
|
420 |
if (!$GLOBALS['_NET_FTP']['USE_PASSIVE']) {
|
|
|
421 |
$data = &socket_accept($data);
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
$content = array();
|
|
|
425 |
|
|
|
426 |
switch ($GLOBALS['_NET_FTP']['USE_PASSIVE']) {
|
|
|
427 |
case true:
|
|
|
428 |
while (true) {
|
|
|
429 |
$string = rtrim(fgets($data, 1024));
|
|
|
430 |
|
|
|
431 |
if ($string=='') {
|
|
|
432 |
break;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
$content[] = $string;
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
fclose($data);
|
|
|
439 |
break;
|
|
|
440 |
|
|
|
441 |
case false:
|
|
|
442 |
$string = socket_read($data, 1024, PHP_BINARY_READ);
|
|
|
443 |
|
|
|
444 |
$content = explode("\n", $string);
|
|
|
445 |
unset($content[count($content)-1]);
|
|
|
446 |
|
|
|
447 |
socket_close($GLOBALS['_NET_FTP']['DATA']);
|
|
|
448 |
socket_close($data);
|
|
|
449 |
break;
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
$data = $GLOBALS['_NET_FTP']['DATA'] = null;
|
|
|
453 |
|
|
|
454 |
$f = fgets($control, 1024);
|
|
|
455 |
return $content;
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
/**
|
|
|
459 |
* string ftp_systype ( resource stream );
|
|
|
460 |
*
|
|
|
461 |
* Gets system type identifier of remote FTP server
|
|
|
462 |
* Returns the remote system type
|
|
|
463 |
*
|
|
|
464 |
* @param resource &$control FTP resource
|
|
|
465 |
*
|
|
|
466 |
* @access public
|
|
|
467 |
* @return string
|
|
|
468 |
*/
|
|
|
469 |
function ftp_systype(&$control)
|
|
|
470 |
{
|
|
|
471 |
if (!is_resource($control)) {
|
|
|
472 |
return false;
|
|
|
473 |
}
|
|
|
474 |
|
|
|
475 |
fputs($control, 'SYST'."\r\n");
|
|
|
476 |
$line = fgets($control, 256);
|
|
|
477 |
|
|
|
478 |
if (substr($line, 0, 3) != 215) {
|
|
|
479 |
return false;
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
$os = substr($line, 4, strpos($line, ' ', 4)-4);
|
|
|
483 |
return $os;
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
/**
|
|
|
487 |
* boolean ftp_alloc ( resource stream, integer bytes [, string &message ] );
|
|
|
488 |
*
|
|
|
489 |
* Allocates space for a file to be uploaded
|
|
|
490 |
* Return TRUE on success or FALSE on failure
|
|
|
491 |
*
|
|
|
492 |
* NOTE; Many FTP servers do not support this command and/or don't need it.
|
|
|
493 |
*
|
|
|
494 |
* FTP success respons key: Belive it's 200
|
|
|
495 |
* Needs data connection: NO
|
|
|
496 |
*
|
|
|
497 |
* @param resource &$control FTP stream
|
|
|
498 |
* @param integer $int Space to allocate
|
|
|
499 |
* @param string &$msg Optional, textual representation of the servers response
|
|
|
500 |
* will be returned by reference
|
|
|
501 |
*
|
|
|
502 |
* @access public
|
|
|
503 |
* @return boolean
|
|
|
504 |
*/
|
|
|
505 |
function ftp_alloc(&$control, $int, &$msg = null)
|
|
|
506 |
{
|
|
|
507 |
if (!is_resource($control) || !is_integer($int)) {
|
|
|
508 |
return false;
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
fputs($control, 'ALLO '.$int.' R '.$int."\r\n");
|
|
|
512 |
|
|
|
513 |
$msg = rtrim(fgets($control, 256));
|
|
|
514 |
|
|
|
515 |
$code = substr($msg, 0, 3);
|
|
|
516 |
if ($code == 200 || $code == 202) {
|
|
|
517 |
return true;
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
return false;
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
/**
|
|
|
524 |
* bool ftp_put ( resource stream, string remote_file, string local_file,
|
|
|
525 |
* int mode [, int startpos ] );
|
|
|
526 |
*
|
|
|
527 |
* Uploads a file to the FTP server
|
|
|
528 |
* Returns TRUE on success or FALSE on failure.
|
|
|
529 |
*
|
|
|
530 |
* NOTE:
|
|
|
531 |
* The transfer mode specified must be either FTP_ASCII or FTP_BINARY.
|
|
|
532 |
*
|
|
|
533 |
* @param resource &$control FTP stream
|
|
|
534 |
* @param string $remote Remote file to write
|
|
|
535 |
* @param string $local Local file to upload
|
|
|
536 |
* @param integer $mode Upload mode, FTP_ASCI || FTP_BINARY
|
|
|
537 |
* @param integer $pos Optional, start upload at position
|
|
|
538 |
*
|
|
|
539 |
* @access public
|
|
|
540 |
* @return boolean
|
|
|
541 |
*/
|
|
|
542 |
function ftp_put(&$control, $remote, $local, $mode, $pos = 0)
|
|
|
543 |
{
|
|
|
544 |
if (!is_resource($control) || !is_readable($local) ||
|
|
|
545 |
!is_integer($mode) || !is_integer($pos)) {
|
|
|
546 |
return false;
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
$types = array (
|
|
|
550 |
|
|
|
551 |
1 => 'I'
|
|
|
552 |
);
|
|
|
553 |
$windows = array (
|
|
|
554 |
|
|
|
555 |
1 => 'b'
|
|
|
556 |
);
|
|
|
557 |
|
|
|
558 |
/**
|
|
|
559 |
* TYPE values:
|
|
|
560 |
* A ( ASCII )
|
|
|
561 |
* I ( BINARY )
|
|
|
562 |
* E ( EBCDIC )
|
|
|
563 |
* L ( BYTE )
|
|
|
564 |
*/
|
|
|
565 |
|
|
|
566 |
if (!isset($GLOBALS['_NET_FTP']['DATA']) ||
|
|
|
567 |
!is_resource($GLOBALS['_NET_FTP']['DATA'])) {
|
|
|
568 |
ftp_pasv($control, $GLOBALS['_NET_FTP']['USE_PASSIVE']);
|
|
|
569 |
|
|
|
570 |
}
|
|
|
571 |
// Establish data connection variable
|
|
|
572 |
$data = &$GLOBALS['_NET_FTP']['DATA'];
|
|
|
573 |
|
|
|
574 |
// Decide TYPE to use
|
|
|
575 |
fputs($control, 'TYPE '.$types[$mode]."\r\n");
|
|
|
576 |
$line = fgets($control, 256); // "Type set to TYPE"
|
|
|
577 |
if (substr($line, 0, 3) != 200) {
|
|
|
578 |
return false;
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
fputs($control, 'STOR '.$remote."\r\n");
|
|
|
582 |
sleep(1);
|
|
|
583 |
$line = fgets($control, 256); // "Opening TYPE mode data connect."
|
|
|
584 |
|
|
|
585 |
if (substr($line, 0, 3) != 150) {
|
|
|
586 |
return false;
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
// Creating resource to $local file
|
|
|
590 |
$fp = fopen($local, 'r'. $windows[$mode]);
|
|
|
591 |
if (!is_resource($fp)) {
|
|
|
592 |
$fp = null;
|
|
|
593 |
return false;
|
|
|
594 |
}
|
|
|
595 |
|
|
|
596 |
// Loop throu that file and echo it to the data socket
|
|
|
597 |
$i = 0;
|
|
|
598 |
switch ($GLOBALS['_NET_FTP']['USE_PASSIVE']) {
|
|
|
599 |
case false:
|
|
|
600 |
$data = &socket_accept($data);
|
|
|
601 |
while (!feof($fp)) {
|
|
|
602 |
$i += socket_write($data, fread($fp, 10240), 10240);
|
|
|
603 |
}
|
|
|
604 |
socket_close($data);
|
|
|
605 |
break;
|
|
|
606 |
|
|
|
607 |
case true:
|
|
|
608 |
while (!feof($fp)) {
|
|
|
609 |
$i += fputs($data, fread($fp, 10240), 10240);
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
fclose($data);
|
|
|
613 |
break;
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
$data = null;
|
|
|
617 |
do {
|
|
|
618 |
$line = fgets($control, 256);
|
|
|
619 |
} while (substr($line, 0, 4) != "226 ");
|
|
|
620 |
return true;
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
/**
|
|
|
624 |
* Retrieve a remote file to a local file
|
|
|
625 |
* Returns TRUE on success or FALSE on failure
|
|
|
626 |
*
|
|
|
627 |
* @param integer &$control Stream ID
|
|
|
628 |
* @param string $local Local filename
|
|
|
629 |
* @param string $remote Remote filename
|
|
|
630 |
* @param integer $mode Transfer mode (FTP_ASCII or FTP_BINARY)
|
|
|
631 |
* @param integer $resume Resume the file transfer or not
|
|
|
632 |
*
|
|
|
633 |
* @access public
|
|
|
634 |
* @return boolean
|
|
|
635 |
*/
|
|
|
636 |
function ftp_get(&$control, $local, $remote, $mode, $resume = 0)
|
|
|
637 |
{
|
|
|
638 |
if (!is_resource($control) || !is_writable(dirname($local)) ||
|
|
|
639 |
!is_integer($mode) || !is_integer($resume)) {
|
|
|
640 |
return false;
|
|
|
641 |
}
|
|
|
642 |
$types = array (
|
|
|
643 |
|
|
|
644 |
1 => 'I'
|
|
|
645 |
);
|
|
|
646 |
$windows = array (
|
|
|
647 |
|
|
|
648 |
1 => 'b'
|
|
|
649 |
);
|
|
|
650 |
|
|
|
651 |
if (!isset($GLOBALS['_NET_FTP']['DATA']) ||
|
|
|
652 |
!is_resource($GLOBALS['_NET_FTP'][ 'DATA'])) {
|
|
|
653 |
ftp_pasv($control, $GLOBALS['_NET_FTP']['USE_PASSIVE']);
|
|
|
654 |
}
|
|
|
655 |
$data = &$GLOBALS['NET_FTP']['DATA'];
|
|
|
656 |
|
|
|
657 |
fputs($control, 'TYPE '.$types[$mode]."\r\n");
|
|
|
658 |
$line = fgets($control, 256);
|
|
|
659 |
if (substr($line, 0, 3) != 200) {
|
|
|
660 |
return false;
|
|
|
661 |
}
|
|
|
662 |
|
|
|
663 |
$fp = fopen($local, 'w'.$windows[$mode]);
|
|
|
664 |
if (!is_resource($fp)) {
|
|
|
665 |
$fp = null;
|
|
|
666 |
return false;
|
|
|
667 |
}
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
/**
|
|
|
671 |
* Changes to the parent directory
|
|
|
672 |
* Returns TRUE on success or FALSE on failure
|
|
|
673 |
*
|
|
|
674 |
* @param integer &$control Stream ID
|
|
|
675 |
*
|
|
|
676 |
* @access public
|
|
|
677 |
* @return boolean
|
|
|
678 |
*/
|
|
|
679 |
function ftp_cdup(&$control)
|
|
|
680 |
{
|
|
|
681 |
fputs($control, 'CDUP'."\r\n");
|
|
|
682 |
$line = fgets($control, 256);
|
|
|
683 |
|
|
|
684 |
if (substr($line, 0, 3) != 250) {
|
|
|
685 |
return false;
|
|
|
686 |
}
|
|
|
687 |
|
|
|
688 |
return true;
|
|
|
689 |
}
|
|
|
690 |
|
|
|
691 |
/**
|
|
|
692 |
* Set permissions on a file via FTP
|
|
|
693 |
* Returns the new file permission on success or false on error
|
|
|
694 |
*
|
|
|
695 |
* NOTE: This command is *not* supported by the standard
|
|
|
696 |
* NOTE: This command not ready!
|
|
|
697 |
*
|
|
|
698 |
* @param integer &$control Stream ID
|
|
|
699 |
* @param integer $mode Octal value
|
|
|
700 |
* @param string $file File to change permissions on
|
|
|
701 |
*
|
|
|
702 |
* @todo Figure out a way to chmod files via FTP
|
|
|
703 |
* @access public
|
|
|
704 |
* @return integer
|
|
|
705 |
*/
|
|
|
706 |
function ftp_chmod(&$control, $mode, $file)
|
|
|
707 |
{
|
|
|
708 |
if (!is_resource($control) || !is_integer($mode) || !is_string($file)) {
|
|
|
709 |
return false;
|
|
|
710 |
}
|
|
|
711 |
|
|
|
712 |
// chmod not in the standard, proftpd doesn't recognize it
|
|
|
713 |
// use SITE CHMOD?
|
|
|
714 |
fputs($control, 'SITE CHMOD '.$mode. ' ' .$file."\r\n");
|
|
|
715 |
$line = fgets($control, 256);
|
|
|
716 |
|
|
|
717 |
if (substr($line, 0, 3) == 200) {
|
|
|
718 |
return $mode;
|
|
|
719 |
}
|
|
|
720 |
|
|
|
721 |
trigger_error('ftp_chmod() [<a
|
|
|
722 |
href="function.ftp-chmod">function.ftp-chmod</a>]: ' .
|
|
|
723 |
rtrim($line), E_USER_WARNING);
|
|
|
724 |
return false;
|
|
|
725 |
}
|
|
|
726 |
|
|
|
727 |
/**
|
|
|
728 |
* Deletes a file on the FTP server
|
|
|
729 |
* Returns TRUE on success or FALSE on failure
|
|
|
730 |
*
|
|
|
731 |
* @param integer &$control Stream ID
|
|
|
732 |
* @param string $path File to delete
|
|
|
733 |
*
|
|
|
734 |
* @access public
|
|
|
735 |
* @return boolean
|
|
|
736 |
*/
|
|
|
737 |
function ftp_delete(&$control, $path)
|
|
|
738 |
{
|
|
|
739 |
if (!is_resource($control) || !is_string($path)) {
|
|
|
740 |
return false;
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
fputs($control, 'DELE '.$path."\r\n");
|
|
|
744 |
$line = fgets($control, 256);
|
|
|
745 |
|
|
|
746 |
if (substr($line, 0, 3) == 250) {
|
|
|
747 |
return true;
|
|
|
748 |
}
|
|
|
749 |
|
|
|
750 |
return false;
|
|
|
751 |
}
|
|
|
752 |
|
|
|
753 |
/**
|
|
|
754 |
* Requests execution of a program on the FTP server
|
|
|
755 |
* NOTE; SITE EXEC is *not* supported by the standart
|
|
|
756 |
* Returns TRUE on success or FALSE on error
|
|
|
757 |
*
|
|
|
758 |
* @param integer &$control Stream ID
|
|
|
759 |
* @param string $cmd Command to send
|
|
|
760 |
*
|
|
|
761 |
* @access public
|
|
|
762 |
* @todo Look a littlebit better into this
|
|
|
763 |
* @return boolean
|
|
|
764 |
*/
|
|
|
765 |
function ftp_exec(&$control, $cmd)
|
|
|
766 |
{
|
|
|
767 |
if (!is_resource($control) || !is_string($cmd)) {
|
|
|
768 |
return false;
|
|
|
769 |
}
|
|
|
770 |
// Command not defined in the standart
|
|
|
771 |
// proftpd doesn't recognize SITE EXEC (only help,chgrp,chmod and ratio)
|
|
|
772 |
fputs($control, 'SITE EXEC '.$cmd."\r\n");
|
|
|
773 |
$line = fgets($control, 256);
|
|
|
774 |
|
|
|
775 |
// php.net/ftp_exec uses respons code 200 to verify if command was sent
|
|
|
776 |
// successfully or not, so we'll just do the same
|
|
|
777 |
if (substr($line, 0, 3) == 200) {
|
|
|
778 |
return true;
|
|
|
779 |
}
|
|
|
780 |
|
|
|
781 |
return false;
|
|
|
782 |
}
|
|
|
783 |
?>
|