<?php
/**
* ftp.class.php
*
* Distributed under the GNU Lesser General Public License (LGPL v3)
* (http://www.gnu.org/licenses/lgpl.html)
* This program is distributed in the hope that it will be useful -
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @author Dominik Zogg <dominik.zogg@gmail.com
* @copyright Copyright (c) 2011, iframe AG
*
*/
class ftp {
/**
* @var array connectioninfo
*/
var $_connectioninfo = array(
"server" => "",
"username" => "",
"password" => "",
"encoding" => "",
"port" => 0,
"timeout" => 0,
);
/**
* @var null connection
*/
var $_connection = null;
/**
* construct
*
* @param string $server server must be a valid domain or ip
* @param string $username username of the ftp user
* @param string $password password of the ftp user
* @param int $port port of the ftp server
* @param int $timeout connection timeout time in seconds
*/
public function __construct($server, $username, $password, $encoding = "UTF-8", $port = 21, $timeout = 120) {
if(!preg_match("/^([a-zA-Z0-9\-\_\.\:]*)$/", trim($server))) {
throw new Exception("Please enter a valid Domain or IP!");
}
if(!is_int($port)) {
throw new Exception("Port must be a number!");
}
if(!is_int($timeout)) {
throw new Exception("Timeout must be a number!");
}
$this->_connectioninfo['server'] = trim($server);
$this->_connectioninfo['username'] = trim($username);
$this->_connectioninfo['password'] = trim($password);
$this->_connectioninfo['encoding'] = trim($encoding);
$this->_connectioninfo['port'] = $port;
$this->_connectioninfo['timeout'] = $timeout;
$this->_connect();
}
/**
* destruct
*/
public function __destruct() {
@ftp_close($this->_connection);
}
/**
* _connect
*/
private function _connect() {
if($this->_connection = @ftp_connect($this->_connectioninfo['server'], $this->_connectioninfo['port'], $this->_connectioninfo['timeout'])) {
if(!@ftp_login($this->_connection, $this->_connectioninfo['username'], $this->_connectioninfo['password'])) {
throw new Exception("Can't login to ftp server!");
}
if(!@ftp_pasv($this->_connection, true)) {
throw new Exception("Can't force passiv mode!");
}
}
else {
throw new Exception("Can't login to ftp server!");
}
}
/**
* filelist
*
* @param string $path path must be a valid unix path
* @return array filelist, each file is an array element
*/
public function filelist($path = "") {
$return = array();
$elements = $this->_rawlist($path);
if(is_array($elements)) {
foreach($elements as $elementkey => $element) {
if($element['type'] !== 1) {
$return[$elementkey] = $element;
}
}
}
return($return);
}
/**
* folderlist
*
* @param string $path path must be a valid unix path
* @return array folderlist, each file is an array element
*/
public function folderlist($path = "") {
$return = array();
$elements = $this->_rawlist($path);
if(is_array($elements)) {
foreach($elements as $elementkey => $element) {
if($element['type'] === 1) {
$return[$elementkey] = $element;
}
}
}
return($return);
}
/**
* downloadfile
*
* @param string $remotepath filepath on remotehost
* @param string $localpath filepath on localhost
*/
public function downloadfile($remotepath, $localpath, $overwrite = false) {
$this->_resetremotepath();
if(@is_file($localpath) && !$overwrite) {
throw new Exception("Localfile '{$localpath}' allready exists! Force overwrite by calling with option overwrite = true!");
}
if(!@ftp_get($this->_connection, $localpath, iconv("UTF-8", $this->_connectioninfo['encoding'], $remotepath), FTP_BINARY)) {
//reconnect if download failed
$this->_connect();
if(!@ftp_get($this->_connection, $localpath, iconv("UTF-8", $this->_connectioninfo['encoding'], $remotepath), FTP_BINARY)) {
throw new Exception("Remotefile is not downloadable: {$remotepath}");
}
}
}
/**
* downloadfolder
*
* @param string $remotepath filepath on remotehost
* @param string $localpath filepath on localhost
*/
public function downloadfolder($remotepath, $localpath, $overwrite = false) {
$this->_resetremotepath@import url('https://gist.github.com/assets/embed-0af287a4b5c981db301049e56f06e5d3.css');