Kusura bakmayın, SFTP_FileLink() fonksiyonu eklemeyi unutmuşum, Eğer yine çalışmazsa hata logları ile beraber Eklenti_Kontrol.php gönderir misiniz.
<?php
use phpseclib3\Net\SSH2;
use phpseclib3\Net\SFTP;
require 'vendor/autoload.php';
class ogcp_ssh2 {
private $ssh;
private $sftp;
private $error;
private $host;
private $port;
private $username;
private $password;
private $debug = true; // Hata ayıklama modu
private function logError($message, $function = '') {
$timestamp = date('Y-m-d H:i:s');
$logMessage = "[{$timestamp}] {$function}: {$message}\n";
if($this->debug) {
error_log($logMessage, 3, 'sftp_errors.log');
$this->error = $message;
}
}
public function __construct() {
$this->ssh = null;
$this->sftp = null;
$this->error = "";
}
public function getLastError() {
return $this->error;
}
public function Connect($host, $port = 22) {
if ($host == "") {
$this->logError("Boş host adresi", __FUNCTION__);
return false;
}
try {
$this->host = gethostbyname($host);
$this->port = $port;
$this->logError("Bağlantı deneniyor: {$this->host}:{$this->port}", __FUNCTION__);
$this->ssh = new SSH2($this->host, $this->port);
if ($this->ssh) {
$this->logError("SSH bağlantısı başarılı", __FUNCTION__);
return true;
} else {
$this->logError("SSH bağlantısı başarısız", __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("Bağlantı hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function ConnectwAuth($host, $port = 22, $user, $pass) {
if ($host == "" || $user == "" || $pass == "") {
$this->logError("Eksik parametreler: host, user veya pass boş olamaz", __FUNCTION__);
return false;
}
$this->username = $user;
$this->password = $pass;
try {
if ($this->Connect($host, $port)) {
$this->logError("Login deneniyor: {$user}@{$host}", __FUNCTION__);
if ($this->ssh->login($user, $pass)) {
$this->logError("Login başarılı", __FUNCTION__);
return true;
} else {
$this->logError("Authentication rejected by server", __FUNCTION__);
return false;
}
}
$this->logError("Sunucu bulunamadı", __FUNCTION__);
return false;
} catch (\Exception $e) {
$this->logError("Login hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_DownloadFile($remote, $local) {
try {
$this->logError("SFTP indirme başlatılıyor - Remote: {$remote}, Local: {$local}", __FUNCTION__);
if (!$this->sftp) {
$this->logError("Yeni SFTP bağlantısı oluşturuluyor", __FUNCTION__);
$this->sftp = new SFTP($this->host, $this->port);
if (!$this->sftp->login($this->username, $this->password)) {
$this->logError("SFTP login başarısız", __FUNCTION__);
return false;
}
$this->logError("SFTP login başarılı", __FUNCTION__);
}
// Dosyanın varlığını kontrol et
if (!$this->sftp->file_exists($remote)) {
$this->logError("Uzak dosya bulunamadı: {$remote}", __FUNCTION__);
return false;
}
// Yerel dizinin yazılabilir olduğunu kontrol et
$localDir = dirname($local);
if (!is_writable($localDir)) {
$this->logError("Yerel dizin yazılabilir değil: {$localDir}", __FUNCTION__);
return false;
}
$result = $this->sftp->get($remote, $local);
if ($result) {
$this->logError("Dosya başarıyla indirildi", __FUNCTION__);
return true;
} else {
$this->logError("Dosya indirme başarısız", __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("Dosya indirme hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_UploadFile($local, $remote) {
try {
$this->logError("SFTP yükleme başlatılıyor - Local: {$local}, Remote: {$remote}", __FUNCTION__);
if (!file_exists($local)) {
$this->logError("Yerel dosya bulunamadı: {$local}", __FUNCTION__);
return false;
}
if (!$this->sftp) {
$this->logError("Yeni SFTP bağlantısı oluşturuluyor", __FUNCTION__);
$this->sftp = new SFTP($this->host, $this->port);
if (!$this->sftp->login($this->username, $this->password)) {
$this->logError("SFTP login başarısız", __FUNCTION__);
return false;
}
$this->logError("SFTP login başarılı", __FUNCTION__);
}
$result = $this->sftp->put($remote, $local, SFTP::SOURCE_LOCAL_FILE);
if ($result) {
$this->logError("Dosya başarıyla yüklendi", __FUNCTION__);
return true;
} else {
$this->logError("Dosya yükleme başarısız", __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("Dosya yükleme hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function OpenSFTP() {
try {
$this->logError("SFTP bağlantısı açılıyor", __FUNCTION__);
$this->sftp = new SFTP($this->host, $this->port);
if ($this->sftp->login($this->username, $this->password)) {
$this->logError("SFTP bağlantısı başarılı", __FUNCTION__);
return true;
} else {
$this->logError("SFTP bağlantısı reddedildi", __FUNCTION__);
return false;
}
} catch (\Exception $e) {
$this->logError("SFTP bağlantı hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_ReadFile($filepath) {
try {
if ($filepath == "") {
$this->logError("Dosya yolu boş olamaz", __FUNCTION__);
return false;
}
if (!$this->sftp) {
if (!$this->OpenSFTP()) {
return false;
}
}
if (!$this->sftp->file_exists($filepath)) {
$this->logError("Dosya bulunamadı: {$filepath}", __FUNCTION__);
return false;
}
$content = $this->sftp->get($filepath);
if ($content === false) {
$this->logError("Dosya okuma başarısız: {$filepath}", __FUNCTION__);
return false;
}
return $content;
} catch (\Exception $e) {
$this->logError("Dosya okuma hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function Disconnect() {
try {
if ($this->ssh) {
$this->ssh->disconnect();
$this->logError("SSH bağlantısı kapatıldı", __FUNCTION__);
}
$this->sftp = null;
$this->ssh = null;
return true;
} catch (\Exception $e) {
$this->logError("Bağlantı kapatma hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function SFTP_FileLink($filepath) {
try {
if (empty($filepath)) {
$this->logError("Dosya yolu boş olamaz", __FUNCTION__);
return false;
}
if (!$this->sftp) {
if (!$this->OpenSFTP()) {
$this->logError("SFTP bağlantısı açılamadı", __FUNCTION__);
return false;
}
}
// Dosyanın var olup olmadığını kontrol et
if (!$this->sftp->file_exists($filepath)) {
$this->logError("Dosya bulunamadı: {$filepath}", __FUNCTION__);
return false;
}
$this->logError("Dosya linki oluşturuldu: {$filepath}", __FUNCTION__);
return $filepath;
} catch (\Exception $e) {
$this->logError("Dosya link hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
public function Exec($cmd) {
try {
$this->logError("Komut çalıştırılıyor: {$cmd}", __FUNCTION__);
$result = $this->ssh->exec($cmd);
$this->logError("Komut sonucu: {$result}", __FUNCTION__);
return $result;
} catch (\Exception $e) {
$this->logError("Komut çalıştırma hatası: " . $e->getMessage(), __FUNCTION__);
return false;
}
}
}
?>