FTP protocol in PHP
Daar zijn we weer, maar dit keer met een oud scriptje. Altijd leuk om oude dingen weer even op te rakelen. Dit script laat zien
hoe je met een FTP server kan communiceren. Hier heb ik een aardig rfc’tje voor moeten doorspitten (niet helemaal, dat was echt teveel)
<?php
class ftp
{
private $host;
private $port;
private $username;
private $password;
private $passive;
private $socket;
private $CRLF = "\r\n";
private $list = '';
public function __construct(Array $config)
{
$this->host = $config['host'];
$this->port = $config['port'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->passive = $config['passive'];
}
public function connect()
{
$this->socket = fsockopen(
$this->host,
$this->port,$err,$errstr,10
);
if (!$this->socket) {
echo "an error occured while connection to host : " . $this->host;
exit;
}
$this->output("<font color=green> >> Connected to ". $this->host . " on port ". $this->port . "</font><br />");
$this->fetchData();
}
public function fetchData($showOutput = true)
{
$buffer = fread($this->socket, 1024);
$status = socket_get_status($this->socket);
if ($status["unread_bytes"] > 0) {
$buffer .= fread($this->socket, $status["unread_bytes"]);
}
if ($showOutput)
{
$this->output("<font color=blue> << ".$buffer . "</font><br />");
}
return $buffer;
}
// ftp is two way communication
public function getTransferData($command)
{
$fd = fsockopen($this->ip, $this->port, $e, $d, 5);
$list = "";
while (!feof($fd)) {
$t = fgets($fd,1024);
$list .= "<font color=#a2a2a2>" . $t . "</font>";
$list = str_replace("\n","<br>",$list);
if (preg_match("/". $this->CRLF . $this->CRLF ."/",$t)) {
break;
}
}
fwrite($fd, "QUIT" . $this->CRLF);
$this->list = $list;
}
public function command($command)
{
fwrite($this->socket, $command . $this->CRLF);
$p_command = str_replace($this->password, str_repeat("*",strlen($this->password)), $command);
$this->output("<font color=green> >> ". $p_command . "</font><br />");
$split = explode(" ",$command);
$exec = $split[0];
switch ($exec) {
case "LIST":
$this->getTransferData($command);
break;
case "PASV":
$this->setPassiveMode();
break;
default:
$this->fetchData();
break;
}
}
public function setPassiveMode()
{
$buffer = $this->fetchData(false);
preg_match("/\((.*)?\)/", $buffer, $match);
$transfer = str_replace("(", "", $match[1]);
$transfer = str_replace(")", "", $match[1]);
$split = explode(",",$transfer);
$ip = $split[0] . "." . $split[1] . "." . $split[2] . "." . $split[3];
$port = $split[4] * 256 + $split[5];
$this->ip = $ip;
$this->port = $port;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function getList()
{
return $this->list;
}
public function output($output)
{
echo $output;
}
}
$ftp = new ftp(Array(
'host' => '127.0.0.1',
'port' => '21',
'username' => 'user',
'password' => 'pass',
'passive' => true,
));
$ftp->connect();
$ftp->command("USER " . $ftp->getUsername());
$ftp->command("PASS " . $ftp->getPassword());
$ftp->command("PASV");
$ftp->command("CWD");
$ftp->command("PWD");
$ftp->command("PASV");
$ftp->command("TYPE A");
$ftp->command("LIST");
$ftp->command("QUIT");
echo $ftp->getList();