| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Ftp; |
| 4 |
|
| 5 |
use ImportWP\Common\Filesystem\Filesystem; |
| 6 |
|
| 7 |
class Ftp |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var resource $_conn |
| 11 |
*/ |
| 12 |
private $_conn; |
| 13 |
/** |
| 14 |
* @var string $conn_hash |
| 15 |
*/ |
| 16 |
private $conn_hash; |
| 17 |
/** |
| 18 |
* @var Filesystem $filesystem |
| 19 |
*/ |
| 20 |
private $filesystem; |
| 21 |
|
| 22 |
public function __construct(Filesystem $filesystem) |
| 23 |
{ |
| 24 |
$this->filesystem = $filesystem; |
| 25 |
} |
| 26 |
|
| 27 |
public function __destruct() |
| 28 |
{ |
| 29 |
$this->disconnect(); |
| 30 |
} |
| 31 |
|
| 32 |
public function download_file($url, $host, $user, $pass, $override_filename = null, $port = 21) |
| 33 |
{ |
| 34 |
if ($this->_conn && $this->conn_hash !== md5($host . $user . $pass)) { |
| 35 |
$this->disconnect(); |
| 36 |
} |
| 37 |
|
| 38 |
if (!function_exists('\ftp_connect')) { |
| 39 |
return new \WP_Error('IWP_FTP_3', __("To download via FTP please enable the php ftp extension.", 'jc-importer')); |
| 40 |
} |
| 41 |
|
| 42 |
if (!$this->_conn) { |
| 43 |
if (!$this->login($host, $user, $pass, $port)) { |
| 44 |
return new \WP_Error('IWP_FTP_0', __("Unable to login to ftp server", 'jc-importer')); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if (!$this->_conn) { |
| 49 |
return new \WP_Error('IWP_FTP_0', __("Unable to connect to ftp server", 'jc-importer')); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
// Note: Not all ftp servers support SIZE checks. |
| 54 |
$disable_filesize = apply_filters('iwp/ftp/disable_size_check', false); |
| 55 |
if(false === $disable_filesize){ |
| 56 |
|
| 57 |
$size = ftp_size($this->_conn, $url); |
| 58 |
if ($size === -1) { |
| 59 |
return new \WP_Error('IWP_FTP_1', __('File doesn\'t exist on ftp server.', 'jc-importer')); |
| 60 |
} |
| 61 |
|
| 62 |
if ($size === 0) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($override_filename)) { |
| 68 |
|
| 69 |
$wp_dest = $override_filename; |
| 70 |
} else { |
| 71 |
|
| 72 |
$wp_upload_dir = wp_upload_dir(); |
| 73 |
|
| 74 |
$dest = wp_unique_filename($wp_upload_dir['path'], basename($url)); |
| 75 |
$wp_dest = $wp_upload_dir['path'] . '/' . $dest; |
| 76 |
} |
| 77 |
|
| 78 |
$passive_mode = apply_filters('iwp/ftp/passive_mode', true); |
| 79 |
ftp_pasv($this->_conn, $passive_mode); |
| 80 |
|
| 81 |
$result = ftp_get($this->_conn, $wp_dest, $url, FTP_BINARY); |
| 82 |
if (false === $result) { |
| 83 |
return new \WP_Error('IWP_FTP_2', sprintf(__('Unable to download: %s file via ftp.', 'jc-importer'), $url)); |
| 84 |
} |
| 85 |
|
| 86 |
return array( |
| 87 |
'dest' => $wp_dest, |
| 88 |
'type' => $this->filesystem->get_filetype($wp_dest), |
| 89 |
'mime' => $this->filesystem->get_file_mime($wp_dest) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
public function connect($host, $port = 21) |
| 94 |
{ |
| 95 |
$this->_conn = ftp_connect($host, $port); |
| 96 |
} |
| 97 |
|
| 98 |
public function disconnect() |
| 99 |
{ |
| 100 |
if ($this->_conn) { |
| 101 |
ftp_close($this->_conn); |
| 102 |
$this->_conn = false; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public function login($host, $user, $pass, $port = 21) |
| 107 |
{ |
| 108 |
if (!$this->_conn) { |
| 109 |
$this->connect($host, $port); |
| 110 |
} |
| 111 |
|
| 112 |
if ($this->_conn) { |
| 113 |
if (true === ftp_login($this->_conn, $user, $pass)) { |
| 114 |
$this->conn_hash = md5($host . $user . $pass); |
| 115 |
return true; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
public function get_connection() |
| 123 |
{ |
| 124 |
return $this->_conn; |
| 125 |
} |
| 126 |
} |
| 127 |
|