<?php
/**
* Class for ftp-user handling
* @author shredder
* @version 1.0
**/
class ftp {
private $fp;
private $commandlog;
/**
* Opens a socket connection to a server, params are read
* from database
* @param string $host FTP server address
* @param int $port FTP server port
* @param string $user Adminname
* @param string $pass Adminpassword
**/
public function connect($host, $port, $user, $pass) {
// Open socket connection
$this->fp = fsockopen($host,$port);
// Retrieve welcome message ("200.")
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>".fread($this->fp, 1000)."</td></tr>";
// Send admin username
fwrite($this->fp,"user $user\n");
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>".fread($this->fp, 1000)."</td></tr>";
// Send admin password
fwrite($this->fp,"pass $pass\n");
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>".fread($this->fp, 1000)."</td></tr>";
}
/**
* Creates a user with a specific group membership and/or
* a specific FTP-directory with one resource
* @param string $username
* @param string $password
* @param string $realname
* @param string $ftppath
* @param string $path
* @param string $group
* @return bool FALSE if user already exists
**/
public function createUser($username, $password, $realname, $ftppath, $path, $group) {
// Adds a specific user
fwrite($this->fp,"adduser \"$username\" \"$password\" \"$realname\" \"$ftppath\" \"$path\" \"$group\"\n");
$message = fread($this->fp, 1000);
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>$message</td></tr>";
if ($this->isError($message)) {
return false;
} else {
return true;
}
}
/**
* Adds a ressource to a user or a group
* @param string $username
* @param string $ftppath
* @param string $path
* @param string $rights
* @return bool
**/
public function addResource($username, $ftppath, $path, $rights) {
fwrite($this->fp,"addresource \"$username\" \"$ftppath\" \"$path\" \"$rights\"\n");
$message = fread($this->fp, 1000);
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>$message</td></tr>";
if ($this->isError($message)) {
return false;
} else {
return true;
}
}
/**
* Sets a ressource of a user or a group
* @param string $username
* @param string $ftppath
* @param string $path
* @param string $rights
* @return bool
**/
public function setResource($username, $ftppath, $path, $rights) {
fwrite($this->fp,"setresource \"$username\" \"$ftppath\" \"$path\" \"$rights\"\n");
$message = fread($this->fp, 1000);
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>$message</td></tr>";
if ($this->isError($message)) {
return false;
} else {
return true;
}
}
/**
* Changes the password of a specific user
* @param string $username
* @param string $password
* @return bool
**/
public function changePassword($username, $password) {
fwrite($this->fp, "passwd \"$username\" \"$password\"\n");
$message = fread($this->fp, 1000);
$this->commandlog .= "<tr><td>".date("i:s")."</td><td>$message</td></tr>";
if ($this->isError($message)) {
return false;
} else {
return true;
}
}
/**
* Checks if the command was succesful
* @param string $message
* @return bool
**/
private function isError($message) {
if (substr_count($message, "500")) {
return false;
} else {
return true;
}
}
/**
* Returns command log
* @return string
**/
public function getCommandlog() {
$this->commandlog = "<table border=1>".$this->commandlog."</table>";
return $this->commandlog;
}
}
?>
Script submitted by MasonSolutions and ReggaeGandalf