| @@ -28,36 +28,60 @@ | ||
| 28 | 28 | { |
| 29 | 29 | $this->disconnect(); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - public function download_file($url, $host, $user, $pass, $override_filename) | |
| 32 | + public function download_file($url, $host, $user, $pass, $override_filename = null, $port = 21) | |
| 33 | 33 | { |
| 34 | 34 | if ($this->_conn && $this->conn_hash !== md5($host . $user . $pass)) { |
| 35 | 35 | $this->disconnect(); |
| 36 | 36 | } |
| 37 | 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 | + | |
| 38 | 42 | if (!$this->_conn) { |
| 39 | - $this->login($host, $user, $pass); | |
| 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 | + } | |
| 40 | 46 | } |
| 41 | 47 | |
| 42 | - $size = ftp_size($this->_conn, $url); | |
| 43 | - if ($size === -1) { | |
| 44 | - return new \WP_Error('IWP_FTP_1', 'Could not get ftp file size.'); | |
| 48 | + if (!$this->_conn) { | |
| 49 | + return new \WP_Error('IWP_FTP_0', __("Unable to connect to ftp server", 'jc-importer')); | |
| 45 | 50 | } |
| 46 | 51 | |
| 47 | 52 | |
| 48 | - if ($size === 0) { | |
| 49 | - return false; | |
| 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 | + } | |
| 50 | 65 | } |
| 51 | 66 | |
| 52 | - $wp_upload_dir = wp_upload_dir(); | |
| 67 | + if (!empty($override_filename)) { | |
| 53 | 68 | |
| 54 | - $dest = wp_unique_filename($wp_upload_dir['path'], basename($url)); | |
| 55 | - $wp_dest = $wp_upload_dir['path'] . '/' . $dest; | |
| 69 | + $wp_dest = $override_filename; | |
| 70 | + } else { | |
| 56 | 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 | + | |
| 57 | 81 | $result = ftp_get($this->_conn, $wp_dest, $url, FTP_BINARY); |
| 58 | 82 | if (false === $result) { |
| 59 | - return new \WP_Error('IWP_FTP_2', 'Unable to download: ' . $url . ' file via ftp.'); | |
| 83 | + return new \WP_Error('IWP_FTP_2', sprintf(__('Unable to download: %s file via ftp.', 'jc-importer'), $url)); | |
| 60 | 84 | } |
| 61 | 85 | |
| 62 | 86 | return array( |
| 63 | 87 | 'dest' => $wp_dest, |
| @@ -65,11 +89,11 @@ | ||
| 65 | 89 | 'mime' => $this->filesystem->get_file_mime($wp_dest) |
| 66 | 90 | ); |
| 67 | 91 | } |
| 68 | 92 | |
| 69 | - public function connect($host) | |
| 93 | + public function connect($host, $port = 21) | |
| 70 | 94 | { |
| 71 | - $this->_conn = ftp_connect($host); | |
| 95 | + $this->_conn = ftp_connect($host, $port); | |
| 72 | 96 | } |
| 73 | 97 | |
| 74 | 98 | public function disconnect() |
| 75 | 99 | { |
| @@ -78,17 +102,25 @@ | ||
| 78 | 102 | $this->_conn = false; |
| 79 | 103 | } |
| 80 | 104 | } |
| 81 | 105 | |
| 82 | - public function login($host, $user, $pass) | |
| 106 | + public function login($host, $user, $pass, $port = 21) | |
| 83 | 107 | { |
| 84 | 108 | if (!$this->_conn) { |
| 85 | - $this->connect($host); | |
| 109 | + $this->connect($host, $port); | |
| 86 | 110 | } |
| 87 | 111 | |
| 88 | 112 | if ($this->_conn) { |
| 89 | 113 | if (true === ftp_login($this->_conn, $user, $pass)) { |
| 90 | 114 | $this->conn_hash = md5($host . $user . $pass); |
| 115 | + return true; | |
| 91 | 116 | } |
| 92 | 117 | } |
| 118 | + | |
| 119 | + return false; | |
| 120 | + } | |
| 121 | + | |
| 122 | + public function get_connection() | |
| 123 | + { | |
| 124 | + return $this->_conn; | |
| 93 | 125 | } |
| 94 | 126 | } |