PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.1
Import WP – CSV & XML Import Export for WordPress v2.8.1
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Ftp / Ftp.php

Ftp.php in Import WP – CSV & XML Import Export for WordPress 2.8.1, at class/Common/Ftp/Ftp.php

118 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 (!$this->_conn) {
39 if (!$this->login($host, $user, $pass, $port)) {
40 return new \WP_Error('IWP_FTP_0', "Unable to login to ftp server");
41 }
42 }
43
44 if (!$this->_conn) {
45 return new \WP_Error('IWP_FTP_0', "Unable to connect to ftp server");
46 }
47
48 $size = ftp_size($this->_conn, $url);
49 if ($size === -1) {
50 return new \WP_Error('IWP_FTP_1', 'File doesn\'t exist on ftp server.');
51 }
52
53
54 if ($size === 0) {
55 return false;
56 }
57
58 if (!empty($override_filename)) {
59
60 $wp_dest = $override_filename;
61 } else {
62
63 $wp_upload_dir = wp_upload_dir();
64
65 $dest = wp_unique_filename($wp_upload_dir['path'], basename($url));
66 $wp_dest = $wp_upload_dir['path'] . '/' . $dest;
67 }
68
69 $passive_mode = apply_filters('iwp/ftp/passive_mode', true);
70 ftp_pasv($this->_conn, $passive_mode);
71
72 $result = ftp_get($this->_conn, $wp_dest, $url, FTP_BINARY);
73 if (false === $result) {
74 return new \WP_Error('IWP_FTP_2', 'Unable to download: ' . $url . ' file via ftp.');
75 }
76
77 return array(
78 'dest' => $wp_dest,
79 'type' => $this->filesystem->get_filetype($wp_dest),
80 'mime' => $this->filesystem->get_file_mime($wp_dest)
81 );
82 }
83
84 public function connect($host, $port = 21)
85 {
86 $this->_conn = ftp_connect($host, $port);
87 }
88
89 public function disconnect()
90 {
91 if ($this->_conn) {
92 ftp_close($this->_conn);
93 $this->_conn = false;
94 }
95 }
96
97 public function login($host, $user, $pass, $port = 21)
98 {
99 if (!$this->_conn) {
100 $this->connect($host, $port);
101 }
102
103 if ($this->_conn) {
104 if (true === ftp_login($this->_conn, $user, $pass)) {
105 $this->conn_hash = md5($host . $user . $pass);
106 return true;
107 }
108 }
109
110 return false;
111 }
112
113 public function get_connection()
114 {
115 return $this->_conn;
116 }
117 }
118