PABX da Simples IP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

92 lines
2.5 KiB

<?php
/**
* Class SFTPAccess
*
* @author Lucas Awade
*/
class SFTPAccess {
/** @conexao */
private $connection;
/** @sftp */
private $sftp;
function __construct($server, $port, $methods = null) {
$this->connect($server, $port, $methods);
}
/**
* Informa o servidor SFTP para inciar uma conexao.
* @param string $server
* @param string|int $port
* @return boolean
*/
function connect($server, $port = 22, $methods = null) {
$this->connection = ssh2_connect($server, $port, $methods);
if ($this->connection) {
return true;
} else {
return false;
}
}
/**
* Autentica as credencias para conectar ao SFTP.
* @param string $username
* @param string $password
* @return boolean
*/
function authentication($username, $password) {
if (ssh2_auth_password($this->connection, $username, $password)) {
$this->sftp = ssh2_sftp($this->connection);
if ($this->sftp) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Altera o diretorio conexao inicial do servidor SFTP.
* @param type $directory
* @return boolean
*/
function ch_dir($directory) {
$stream = ssh2_exec($this->sftp, "cd {$directory}");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, true);
$erro = trim(stream_get_contents($errorStream));
if (!empty($erro)) {
return false;
}
return true;
}
/**
* Transfere os dados em modo de escrita do arquivo local para o remoto.
* @param string $localfile
* @param string $remotefile
* @return boolean
*/
function copy_file($localfile, $remotefile) {
$stream = fopen("ssh2.sftp://{$this->sftp}{$remotefile}", "w");
if (!$stream) {
return false;
}
$file = file_get_contents($localfile);
fwrite($stream, $file);
fclose($stream);
return true;
}
}