| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Drive { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Sftp extends WPDA_Drive { |
| 8 |
|
| 9 |
private $connection = null; |
| 10 |
private $login = false; |
| 11 |
|
| 12 |
public function __construct( $drive_name, $drive = array(), $enabled = false ) { |
| 13 |
|
| 14 |
$this->drive_type = 'sftp'; |
| 15 |
$this->register_phpseclib(); |
| 16 |
|
| 17 |
parent::__construct( $drive_name, $drive, $enabled ); |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
private function register_phpseclib() { |
| 22 |
|
| 23 |
$baseDir = __DIR__ . '/../../vendor/phpseclib'; |
| 24 |
|
| 25 |
spl_autoload_register(function ($class) use ($baseDir) { |
| 26 |
if (strpos($class, 'phpseclib3\\') === 0) { |
| 27 |
$path = str_replace('\\', '/', substr($class, strlen('phpseclib3\\'))); |
| 28 |
$file = $baseDir . '/' . $path . '.php'; |
| 29 |
if (file_exists($file)) { |
| 30 |
require_once $file; |
| 31 |
} |
| 32 |
} |
| 33 |
}); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public function authorize( $authorization, $enabled = true ) { |
| 38 |
|
| 39 |
if ( |
| 40 |
isset( |
| 41 |
$authorization['host'], |
| 42 |
$authorization['username'], |
| 43 |
$authorization['password'], |
| 44 |
$authorization['port'], |
| 45 |
$authorization['timeout'], |
| 46 |
$authorization['directory'] |
| 47 |
) |
| 48 |
) { |
| 49 |
$this->drive = array( |
| 50 |
'host' => $authorization['host'], |
| 51 |
'username' => $authorization['username'], |
| 52 |
'password' => $authorization['password'], |
| 53 |
'port' => $authorization['port'], |
| 54 |
'timeout' => $authorization['timeout'], |
| 55 |
'directory' => $authorization['directory'], |
| 56 |
); |
| 57 |
$this->enabled = $enabled; |
| 58 |
$this->save(); |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
private function prepare_connection() { |
| 68 |
|
| 69 |
return $this->enabled; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public function refresh_token() { |
| 74 |
|
| 75 |
return true; // N.A. |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
public function connect() { |
| 80 |
|
| 81 |
if ( ! $this->enabled ) { return false; } |
| 82 |
|
| 83 |
if ( |
| 84 |
! isset( |
| 85 |
$this->drive['host'], |
| 86 |
$this->drive['port'], |
| 87 |
$this->drive['timeout'], |
| 88 |
$this->drive['username'], |
| 89 |
$this->drive['password'], |
| 90 |
$this->drive['directory'] |
| 91 |
) |
| 92 |
) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
$this->connection = new \phpseclib3\Net\SFTP( $this->drive['host'], $this->drive['port'], $this->drive['timeout'] ); |
| 97 |
|
| 98 |
$this->login = $this->connection->login( $this->drive['username'], $this->drive['password'] ); |
| 99 |
|
| 100 |
if ( ! $this->login ) { |
| 101 |
WPDA::wpda_log_wp_error( "ERROR: SFTP login failed - {$this->drive_name}" ); |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
return true; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
public function close() { |
| 110 |
|
| 111 |
if ( |
| 112 |
null !== $this->connection && |
| 113 |
$this->connection->isConnected() |
| 114 |
) { |
| 115 |
$this->connection->disconnect(); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
public function upload_file( $local_file, $remote_file ) { |
| 121 |
|
| 122 |
if ( null !== $this->connection && ! $this->connection->isConnected() || ! $this->login ) { return false; } |
| 123 |
|
| 124 |
$path = '/' === substr( $this->drive['directory'], -1 ) ? $this->drive['directory'] : $this->drive['directory'] . '/'; |
| 125 |
|
| 126 |
return $this->connection->put( $path . $remote_file, $local_file, \phpseclib3\Net\SFTP::SOURCE_LOCAL_FILE ); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
public function cleanup( $query, $keep ) { |
| 131 |
|
| 132 |
if ( 'ALL' === $keep ) return true; |
| 133 |
|
| 134 |
if ( null !== $this->connection && ! $this->connection->isConnected() || ! $this->login ) { return false; } |
| 135 |
|
| 136 |
$path = '/' === substr( $this->drive['directory'], -1 ) ? $this->drive['directory'] : $this->drive['directory'] . '/'; |
| 137 |
|
| 138 |
$files = $this->connection->nlist( $path ); |
| 139 |
if ( ! $files || ! is_array( $files ) ) return false; |
| 140 |
|
| 141 |
$matched_files = array_filter( $files, function ( $file ) use ( $query ) { |
| 142 |
return fnmatch( $query, basename( $file ) ) !== false && $file !== '.' && $file !== '..'; |
| 143 |
}); |
| 144 |
|
| 145 |
usort( $matched_files, function ( $a, $b ) { |
| 146 |
return strcmp( $b, $a ); |
| 147 |
}); |
| 148 |
|
| 149 |
$count = 0; |
| 150 |
foreach ( $matched_files as $file ) { |
| 151 |
$count++; |
| 152 |
if ( $count > (int) $keep ) { |
| 153 |
$this->connection->delete( $path . $file ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
} |