File: /var/www/shoetique/wp-content/plugins/Ipis-product-Import/sftp_class.php
<?php
class SFTPClient
{
private $connection;
private $sftp;
public function __construct($host, $port=22){
$this->connection = @ssh2_connect($host, $port);
if (!$this->connection){
throw new Exception("Failed to connect to ${host} on port ${port}.");
}
}
public function auth_password($username, $password){
if (!@ssh2_auth_password($this->connection, $username, $password)){
throw new Exception("Failed to authenticate with username ${username} and password.");
}
$this->sftp = @ssh2_sftp($this->connection);
if (!$this->sftp){
throw new Exception("Could not initialize SFTP subsystem");
}
}
public function get_latest_file($dir = "."){
$sftp = $this->sftp;
$realPath = ssh2_sftp_realpath($sftp, $dir);
$dir = "ssh2.sftp://$sftp$realPath";
$tempArray = array();
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) !== false){
if(strpos($file, 'WEBShop.cjenik.') === false){
continue;
}
$tempArray[] = $file;
}
closedir($dh);
}
} else {
throw new Exception("The selected directory does not exist");
}
arsort($tempArray);
$latest_file = array_shift($tempArray);
return $dir . '/' . $latest_file;
}
public function change_dir($file){
if (!$file){
throw new Exception("Could not find file: $file");
}
$sftp = $this->sftp;
$realPath = ssh2_sftp_realpath($sftp, "./imported");
$dir = "ssh2.sftp://$sftp$realPath";
$fileName = end(explode("/", $file));
$stream = @fopen($file, 'r');
$newFile = @fopen($dir . "/" . $fileName . '_imported', 'w');
if (!$stream){
throw new Exception("Could not open file: $file");
}
stream_copy_to_stream($stream, $newFile);
@fclose($stream);
@fclose($newFile);
}
public function delete_file($file){
if (!$file){
throw new Exception("Could not find file: $file");
}
$sftp = $this->sftp;
$realpath = ssh2_sftp_realpath($sftp, $file);
unlink("ssh2.sftp://$sftp$realpath");
}
public function disconnect(){
@ssh2_disconnect($this->connection);
}
}
?>