Que sabes de GNU/Linux?

miércoles, 31 de diciembre de 2008

CAKE-COMPONENT-CONTROLLER

Controller

class FtpsController extends AppController{
var $name = 'Ftps';
var $component=array('Ftp');
/*
*Funcion utilizada para realizar la aplicacion principal de php...
*/
function index(){
}

function conectarFTP(){
if($this->data['Ftp']['usuario'] == null || strlen($this->data['Ftp']['usuario'])<=0 ){
$this->Session->SetFlash('Debe ingresar el usuario');
$this->redirect('/ftps/');
return;
}

if($this->data['Ftp']['host']==null || strlen($this->data['Ftp']['password'])<=0){
$this->Session->SetFlash('Debe ingresar el servidor');
$this->redirect('/ftps/');
return;
}
//$this->redirect('/users/');
}
}

?>
La vista

Conectar a Servidor FTP


echo $form->Create('Ftp',array('action'=>'conectarFTP'));
echo $form->input('usuario',array('type'=>'input','label'=>'Usuario'));
echo $form->input('password',array('type'=>'password','label'=>'Contraseña'));
echo $form->input('host',array('type'=>'input','label'=>'Servidor'));
echo $form->input('puerto',array('type'=>'input','label'=>'Puerto'));
echo $form->end('Conectar');
?>

Modelo

class Ftp extends AppModel{
var $name = 'Ftp';
var $useTable = false;
var $table=false;
var $validate = array('usuario'=>array(
'alphaNumeric'=>array('rule'=>'alphaNumeric','required'=>true,
'message'=>'Solo Letras y Numeros')));


}

?>

Component

define("ERROR_CONECT",-1);

class FtpComponent extends Object{
private $server = '';
private $puerto = '';
private $usuario ='';
private $password = '';
private $pasv = true;
private $idftp=null;
var $controller = true;
var $someVar = null;


/*
* Function: contruimos el objeto con los datos necesario para inicializar la conexion
*/
function __construct($get_server = null,
$get_puerto = null,
$get_usuario = null,
$get_password= null){
if($get_server == null) $get_server = 'localhost';
if($get_puerto == null) $get_puerto = 21;
if($get_usuario == null) $get_usuario = 'anonymous';
if($get_password == null) $get_password = 'anonymous';

$this->server = $get_server;
$this->puerto = $get_puerto;
$this->usuario= $get_usuario;
$this->password= $get_password;
}


/*
* Funcion: permite realizar la conexion al servidor de base de datos
*/
function ConectFtp(){
/*si el id se encuentra no debemo intentar conectar de nuevo*/
if( $this->idftp != null) return 0;
try{
$this->idftp = ftp_connect($this->server,$this->puerto);
}catch(Exception $ex){
echo "No se conecto al server";
}

if($this->idftp == null ) return ERROR_CONECT; /*No connect to host*/
$result = ftp_login($this->id_ftp,$this->usuario,$this->password);

if($result < 0 ) return -2; /*User or login is no valid*/
ftp_pasv($this->id_ftp,$this->pasv);
/*retornamos el identificador para el FTP*/
return $this->id_ftp;
}

/*
* Funcion: Permite traer una ruta desde el servidor FTP
*/
function putFile($archivo_local,$archivo_remoto){
$this->idftp = $this->ConectFtp();
if($this->idftp < 0 ) return $this->idftp; /*No existe conexion al servidor*/
ftp_put($this->idftp,$archivo_remoto,$archivo_local,FTP_BINARY);
}


/*
* Funcion: permite desconectarse del servidor FTP
*/
function destroyConect(){
if($this->idftp != null && $this->idftp > 0 ) ftp_quit($this->idftp);
$this->idftp = null;
}

/*
*Funcion: permite recuperar el directorio de trabajo del servidor FTP
*/
function getPathServer(){
if($this->idftp == null || $this->idftp < 0 ) return ERROR_CONECT; /*No conect*/
$pathserver=ftp_pwd($this->idftp);
return $pathserver;
}

/*
* Funcion: permite retornar un arreglo con los ficheros en el path server
*/
function getFilePath(){
$pathserver = getPathServer();
$files_path=ftp_nlist($this->idftp,$pathserver);
return $files_path;
}
}
?>

Esto es un componente para conectar al FTP