server = $server; $this->user = $user; $this->password = $password; $this->port = $port; $this->log()->info("Server: {$this->server}, User: {$this->user}, Password: {$this->password}, Port: {$this->port}"); } /** * Diretorio padrao para a transferencia do arquivo; * @param string $remote_dir * @param string $file_local */ public function directorys($change_dir, $remote_file, $file_local) { $this->log()->debug("FTP " . __FUNCTION__, debug_backtrace()); $this->dirs['change_dir'] = $this->removeslash($change_dir); $this->dirs['remote_file'] = $remote_file; $this->dirs['file_local'] = $file_local; } ######################################################################## #### FTP #### ######################################################################## /** * Metodo para realizar a conexao com o servidor FTP * @return boolean * @throws Exception */ public function ftp() { $this->log()->debug("Connection FTP", debug_backtrace()); try { if (!$this->dirs) { $this->log()->error("Selecione os diretorios para serem enviados!"); throw new Exception($this->log()->getText()); } /** * Iniciando uma conexao com o servidor FTP */ $this->ftp = new FTPAccess($this->server, $this->port); if(!$this->ftp->getConnection()){ $this->log()->debug("Nao foi possível realizar conexao com o FTP!", debug_backtrace()); } if ($this->ftp->authentication($this->user, $this->password)) { if($this->ftp->mode_passive(true)){ $this->log()->debug("Sucesso em modo passivo!", debug_backtrace()); } else { $this->log()->debug("Nao foi possivel entrar em modo passivo!", debug_backtrace()); } } else { $this->log()->error("Nao foi possivel conectar com as credenciais informada!", debug_backtrace()); throw new Exception($this->log()->getText()); } return true; } catch (Exception $ex) { $this->ftp->connection_close(); $this->log()->error("Backup nao enviado, " . $ex->getMessage()); return false; } } /** * Cria um arvore de pastas com ano e mes. */ public function ftp_folder_bydate() { $this->log()->debug("FTP Folder Bydate", debug_backtrace()); $folders = $this->date_folders(); $this->ftp_chdir($this->dirs['change_dir']); if ($this->ftp_chdir($folders['year'])) { if (!$this->ftp_chdir($folders['month'])) { if (!$this->ftp_mkdir($folders['month'])) { return false; } $this->log()->debug("Diretorio {$folders['month']} criado!"); } } else { if ($this->ftp_mkdir($folders['year'])) { $this->ftp_chdir($folders['year']); if (!$this->ftp_mkdir($folders['month'])) { return false; } $this->log()->debug("Diretorio {$folders['year']} e {$folders['month']} criado!"); } } $this->dirs['change_dir'] = "/" . $this->strslash($this->dirs['change_dir']) . $this->strslash($folders['year']) . $this->strslash($folders['month']); return true; } /** * Chamada da funcao para a finalizacao do envio do arquivo. * @throws Exception */ public function ftp_finish() { if (!$this->ftp_chdir($this->dirs['change_dir'])) { $this->log()->error("Diretorio nao encontrado!", debug_backtrace()); return false; } $this->ftp_send($this->dirs['remote_file'], $this->dirs['file_local']); return true; } /** * Metodo para realizar a troca de diretorio; * @param string $directory * @return boolean */ public function ftp_chdir($directory) { $this->log()->debug("#### FTP Change Directory ####", debug_backtrace()); if (!$directory) { $this->log()->error("Diretorio informado vazio!"); return false; } $acess = $this->ftp->ch_dir($directory); if ($acess) { $this->log()->debug("Diretorio {$directory} encontrado!"); return true; } else { $this->log()->error("Diretorio {$directory} nao encontrado"); return false; } } /** * Metodo para criar diretorio. * @param string $directory * @throws Exception */ private function ftp_mkdir($directory) { $this->log()->debug("#### FTP Make Directory ####", debug_backtrace()); $create = $this->ftp->mk_dir($directory); if ($create) { $this->log()->debug("Criado o diretorio!"); return true; } else { $this->log()->error("Nao foi possivel criar o diretorio informado!"); return false; } } /** * * @param type $remote_file * @param type $local_file * @throws Exception */ private function ftp_send($remote_file, $local_file) { $this->log()->debug("#### FTP Send File ####", debug_backtrace()); if (!file_exists($local_file)) { $this->log()->error("Arquivo local nao localizado!"); throw new Exception($this->log()->getText()); } $sended = $this->ftp->send_file($remote_file, $local_file); if ($sended) { $this->log()->debug("Backup enviado realizado com sucesso!"); } else { $this->log()->error("Nao foi possivel enviar o backup!"); throw new Exception($this->log()->getText()); } } ######################################################################## #### SFTP #### ######################################################################## /** * Conecta no servidor SFTP. * @return boolean */ public function sftp($methods = null) { $this->log()->debug("Connection SFTP", debug_backtrace()); $this->sftp = new SFTPAccess($this->server, $this->port, $methods); if ($this->sftp->authentication($this->user, $this->password)) { $this->log()->debug("Conexao realizada com sucesso!"); return true; } else { $this->log()->error("Nao foi possivel realizar a conexao com o Servidor SFTP."); return false; } } /** * Realiza a troca de diretorio na conexão com o SFTP * @param string $directory * @return boolean */ public function sftp_chdir($directory) { if ($this->sftp->ch_dir($directory)) { $this->log()->debug("Diretorio encontrado."); return true; } else { $this->log()->error("Nao foi possivel encontrar o diretorio informado!"); return false; } } /** * Realiza a copia do arquivo local para o remoto. * @param string $remote_file * @param string $local_file * @return boolean */ public function sftp_copy($remote_file, $local_file) { if ($this->sftp->copy_file($local_file, $remote_file)) { $this->log()->success("A copia do arquivo para o SFTP foi concluida."); return true; } else { $this->log()->error("Nao foi possivel realizar a copia do arquivo para o SFTP."); return false; } } }