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