From ddbd6ea5ab5c5cf4cf134a41cf365624e3a0b62b Mon Sep 17 00:00:00 2001 From: Harald-Innetzberger Date: Mon, 30 Apr 2012 18:28:18 +0000 Subject: [PATCH] Default --- application/controllers/ConfigController.php | 102 +++++++++++++++++++ application/forms/Config/Ftp.php | 10 +- application/views/scripts/config/index.phtml | 15 +++ 3 files changed, 122 insertions(+), 5 deletions(-) diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 8a1ddd8..681e55b 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -286,6 +286,108 @@ class ConfigController extends Zend_Controller_Action $this->_forward('index'); } + /** + * Test FTP-Connection + * + * @return void + */ + public function testFtpConnectionAction() + { + $translator = $this->view->lang->getTranslator(); + + if ($this->_request->isPost()) { + + $postData = $this->_request->getPost(); + $index = (int)$this->_request->getPost('param'); + + // fetch the required params + $server = $postData['ftp_'.$index.'_server']; + $port = $postData['ftp_'.$index.'_port']; + $timeout = $postData['ftp_'.$index.'_timeout']; + $mode = $postData['ftp_'.$index.'_passiveMode']; + $ssl = $postData['ftp_'.$index.'_ssl']; + $user = $postData['ftp_'.$index.'_user']; + $password = $postData['ftp_'.$index.'_pass']; + $directory = $postData['ftp_'.$index.'_dir']; + + // Params for transferring a test file + $name = 'ftp_transfer_testfile.txt'; + $filename = APPLICATION_PATH . '/forms/Config/ftp_transfertest/ftp_transfer_testfile.txt'; + $target_folder = APPLICATION_PATH . '/forms/Config/ftp_transfertest/ftp_target/'; + $upload = false; + + // try to connect via ssl to the ftp server + if ($ssl == 'y' && function_exists('ftp_ssl_connect')) { + $ftpStream = ftp_ssl_connect($server, $port, $timeout); + } else { + // otherwise try to connect to the ftp server normally + $ftpStream = ftp_connect($server, $port, $timeout); + } + + // got resource? + if (!is_resource($ftpStream)) { + $message = sprintf($translator->_('L_FTP_CONNECTION_ERROR'), $server, $port); + + // connection ok? let's try to login + } else if (!ftp_login($ftpStream, $user, $password)) { + $message = sprintf($translator->_('L_FTP_LOGIN_ERROR'), $user); + + // if passive mode is set turn it on + if ($mode == 'y') { + ftp_pasv($ftpStream, true); + } + + // login ok? let's set/change the ftp upload directory + } else if (!ftp_chdir($ftpStream, $directory)) { + $message = sprintf($translator->_('L_CHANGEDIRERROR')); + // chmod target_folder if it's necessary + } else if (file_exists($target_folder) && substr(sprintf('%o', fileperms($target_folder)), -4) < '0755') { + ftp_chmod($ftpStream, 0755, $target_folder); + $message = ''; + // ftp directory exists and chmod ok? let's test the ftp transfer with a test file + } else if (!ftp_put($ftpStream, $target_folder.$name, $filename, FTP_ASCII)) { + $message = sprintf($translator->_('L_FTP_FILE_TRANSFER_ERROR'), $name); + + } else { + $upload = true; + $message = sprintf($translator->_('L_FTP_FILE_TRANSFER_SUCCESS'), $name) + . '

' . + $translator->_('L_FTP_OK'); + + // delete the test file after a successful transfer test + if (file_exists($target_folder.$name)) { + ftp_delete($ftpStream, $target_folder.$name); + } + } + + // let's show the error messages + if (!$upload && count($message) > 0) { + $this->view->popUpMessage()->addMessage( + 'config-validate-error', + 'L_ERROR', + $message, + array( + 'modal' => true + ) + ); + // or show the confirmation message + } else if ($upload && count($message) > 0) { + $this->view->popUpMessage()->addMessage( + 'config-validate-message', + 'L_NOTICE', + $message, + array( + 'modal' => true + ) + ); + } + + // close ftp connection + ftp_close($ftpStream); + } + $this->_forward('index'); + } + /** * Set the default value of all sub forms to the configuration values * diff --git a/application/forms/Config/Ftp.php b/application/forms/Config/Ftp.php index 022efc2..095ef69 100644 --- a/application/forms/Config/Ftp.php +++ b/application/forms/Config/Ftp.php @@ -51,7 +51,7 @@ class Application_Form_Config_Ftp extends Zend_Form_SubForm $this->_addInputServerAndPort($ftpConnectionId); $this->_addInputUserAndPass($ftpConnectionId); $this->_addInputPath($ftpConnectionId); - + if ($nrOfFtpProfiles > 1) { $buttonDelete = 'ftpDelete' . $ftpConnectionId; } else { @@ -305,7 +305,7 @@ class Application_Form_Config_Ftp extends Zend_Form_SubForm /** * Add Button "Test connection" * Add Button "Delete connection" - * + * * @param int $index * @param string $buttonDelete * @@ -318,7 +318,7 @@ class Application_Form_Config_Ftp extends Zend_Form_SubForm } else { $buttonDecorator = 'Default'; } - + $this->addElement( 'button', 'ftpCheck' . $index, @@ -331,8 +331,8 @@ class Application_Form_Config_Ftp extends Zend_Form_SubForm 'escape' => false, 'label' => '', 'class' => 'Formbutton ftpToggle' . $index, - 'onclick' => "alert('checkConnection(" . - $index . ")');", + 'onclick' => "testFtpConnection(" . + $index . ");", ) ); diff --git a/application/views/scripts/config/index.phtml b/application/views/scripts/config/index.phtml index 99bd969..c13bd1c 100644 --- a/application/views/scripts/config/index.phtml +++ b/application/views/scripts/config/index.phtml @@ -31,6 +31,14 @@ $deleteFtpConnectionUrl = $this->url( null, true ); +$testFtpConnectionUrl = $this->url( + array( + 'controller' => 'config', + 'action' => 'test.Ftp.Connection', + ), + null, + true +); ?>