status = "FTP Server not specified.";
return;
}
else {
$this->_server = $options['ftp_server'];
}
if (false && function_exists('ftp_ssl_connect')) {
$this->_connId = ftp_ssl_connect($this->_server);
}
else {
$this->_connId = ftp_connect($this->_server);
}
if (!$this->_connId) {
$this->status = "Cannot reach FTP server.";
return;
}
if (empty($options['ftp_user'])) {
$this->status = "FTP User ID not specified.";
return;
}
else {
$this->_user = $options['ftp_user'];
}
if (empty($options['ftp_password'])) {
$this->status = "FTP Password not specified.";
return;
}
else {
$this->_password = $options['ftp_password'];
}
$this->_loggedIn = @ftp_login($this->_connId, $this->_user, $this->_password);
if (!$this->_loggedIn) {
$this->status = "FTP login fails";
return;
}
if (empty($options['ftp_rootdir'])) {
if (!$this->guessDir()) {
$this->status = "FTP Root Directory needed.";
$this->_showRootDir = true;
return;
}
}
else {
$this->_dir = $options['ftp_rootdir'];
ftp_chdir($this->_connId, $this->_dir);
}
$this->isReady = true;
}
function __destruct() {
if ($this->_connId) {
ftp_close($this->_connId);
}
}
function Ftp() {
if (version_compare(PHP_VERSION, "5.0.0", "<")) {
$this->__construct();
register_shutdown_function(array($this, "__destruct"));
}
}
function guessDir() {
$dir0 = __DIR__;
$ds = DIRECTORY_SEPARATOR;
$parts = explode($ds, $dir0);
foreach ($parts as $p) {
if (empty($p)) {
continue;
}
$this->_dir .= $ds . $p;
$dir = str_replace($this->_dir, '', $dir0);
$exists = @ftp_chdir($this->_connId, $dir);
if ($exists) {
return true;
}
}
return false;
}
static function isNeeded($path) {
$target = realpath($path) . DIRECTORY_SEPARATOR . EZ::randString();
if (@file_put_contents($target, "This is a test file") !== false) {
if (@unlink($target)) {
return false;
}
}
return true;
}
function printForm() {
$table = '';
if (self::isNeeded($this->_dir)) {
if ($this->_loggedIn) {
$display = "style='display:none'";
$table = "
";
}
else {
$display = '';
}
if (empty($this->status)) {
$btn = " All Okay";
}
else {
$btn = " Check Again";
}
$table .= "FTP Details
Looks like FTP is needed to update your application. Please provide the FTP details below. Note that FTP details will be stored in your database in plain text.
$this->status $btn
";
$options = array();
$options['ftp_server'] = array('name' => __('FTP Server', 'easy-paypal'),
'value' => 'localhost',
'validator' => 'notNull',
'help' => __('Enter your FPT server name.', 'easy-paypal'),
'dataTpl' => 'none',
'dataMode' => '');
if (defined('FTP_HOST')) {
$options['ftp_server']['value'] = FTP_HOST;
}
$options['ftp_user'] = array('name' => __('FTP User Name', 'easy-paypal'),
'value' => '',
'validator' => 'notNull',
'help' => __('Enter your FPT user name.', 'easy-paypal'),
'dataTpl' => 'none',
'dataMode' => '');
if (defined('FTP_USER')) {
$options['ftp_user']['value'] = FTP_USER;
}
$options['ftp_password'] = array('name' => __('FTP Password', 'easy-paypal'),
'value' => '',
'validator' => 'notNull',
'help' => __('Enter your FPT password.', 'easy-paypal'),
'dataTpl' => 'none',
'dataMode' => '');
if (defined('FTP_PASS')) {
$options['ftp_password']['value'] = FTP_PASS;
}
if ($this->_showRootDir) {
$options['ftp_rootdir'] = array('name' => __('FTP Root Directory', 'easy-paypal'),
'value' => '',
'help' => __('When you logon to your server using FTP, it puts you in a folder. In most cases, we can discover this folder automatically. So you can start by leaving this option empty, but may have to come back and enter the value if prompted later.', 'easy-paypal'),
'dataTpl' => 'none',
'dataMode' => '');
}
EZ::putDefaultOptions($options);
foreach ($options as $pk => $option) {
$table .= EZ::renderOption($pk, $option);
}
$table .= "
";
$table .= "";
}
return $table;
}
function copy($source, $target) { // ftp equivalent of php copy
if (!@copy($source, $target)) {
if (!$this->isReady) {
return false;
}
$target = str_replace($this->_dir, "", $target);
return ftp_put($this->_connId, $target, $source, FTP_BINARY);
}
else {
return true;
}
}
function rename($source, $target) {
if (!@rename($source, $target)) {
if (!$this->isReady) {
return false;
}
$target = str_replace($this->_dir, "", $target);
return ftp_rename($this->_connId, $target, $source);
}
else {
return true;
}
}
function mkdir($pathname) {
if (!@mkdir($pathname)) {
if (!$this->isReady) {
return false;
}
$pathname = str_replace($this->_dir, "", $pathname);
return ftp_mkdir($this->_connId, $pathname);
}
else {
return true;
}
}
function unlink($filename) {
if (!@unlink($filename)) {
if (!$this->isReady) {
return false;
}
$filename = str_replace($this->_dir, "", $filename);
return ftp_delete($this->_connId, $filename);
}
else {
return true;
}
}
}
}