PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / backup / ftp.php

ftp.php in InfiniteWP Client trunk, at backup/ftp.php

278 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined('ABSPATH') )
4 die();
5
6 if (!class_exists('IWP_MMB_UploadModule')) require_once($GLOBALS['iwp_mmb_plugin_dir'].'/backup/backup.upload.php');
7
8 class IWP_MMB_UploadModule_ftp extends IWP_MMB_UploadModule {
9
10 // Get FTP object with parameters set
11 private function getFTP($server, $user, $pass, $disable_ssl = false, $disable_verify = true, $use_server_certs = false, $passive = true) {
12
13 if ('' == trim($server) || '' == trim($user) || '' == trim($pass)) return new WP_Error('no_settings', sprintf(__('No %s settings were found','InfiniteWP'), 'FTP'));
14
15 if( !class_exists('IWP_MMB_ftp_wrapper')) require_once($GLOBALS['iwp_mmb_plugin_dir'].'/lib/ftp.class.php');
16
17 $port = 21;
18 if (preg_match('/^(.*):(\d+)$/', $server, $matches)) {
19 $server = $matches[1];
20 $port = $matches[2];
21 }
22
23 $ftp = new IWP_MMB_ftp_wrapper($server, $user, $pass, $port);
24
25 if ($disable_ssl) $ftp->ssl = false;
26 $ftp->use_server_certs = $use_server_certs;
27 $ftp->disable_verify = $disable_verify;
28 $ftp->passive = ($passive) ? true : false;
29
30 return $ftp;
31
32 }
33
34 public function get_supported_features() {
35 // This options format is handled via only accessing options via $this->get_options()
36 return array('multi_options');
37 }
38
39 public function get_default_options() {
40 return array(
41 'host' => '',
42 'user' => '',
43 'pass' => '',
44 'path' => '',
45 'passive' => true
46 );
47 }
48
49 public function backup($backup_array) {
50
51 global $iwp_backup_core;
52
53 $opts = $this->get_options();
54
55 $ftp = $this->getFTP(
56 $opts['host'],
57 $opts['user'],
58 $opts['pass'],
59 $iwp_backup_core->get_job_option('IWP_ssl_nossl'),
60 $iwp_backup_core->get_job_option('IWP_ssl_disableverify'),
61 $iwp_backup_core->get_job_option('IWP_ssl_useservercerts'),
62 $opts['passive']
63 );
64
65 if (is_wp_error($ftp) || !$ftp->connect()) {
66 if (is_wp_error($ftp)) {
67 $iwp_backup_core->log_wp_error($ftp);
68 } else {
69 $iwp_backup_core->log("FTP Failure: we did not successfully log in with those credentials.");
70 }
71 $iwp_backup_core->log(sprintf(__("%s login failure",'InfiniteWP'), 'FTP'), 'error');
72 return false;
73 }
74
75 //$ftp->make_dir(); we may need to recursively create dirs? TODO
76
77 $iwp_backup_dir = $iwp_backup_core->backups_dir_location().'/';
78
79 $ftp_remote_path = trailingslashit($opts['path']);
80 if (!empty($opts['ftp_site_folder'])) {
81 $site_name = iwp_getSiteName();
82 $ftp_remote_path.= trailingslashit($site_name);
83 }
84 foreach($backup_array as $file) {
85 $fullpath = $iwp_backup_dir.$file;
86 $iwp_backup_core->log("FTP upload attempt: $file -> ftp://".$opts['user']."@".$opts['host']."/".$ftp_remote_path.$file);
87 $timer_start = microtime(true);
88 $size_k = round(filesize($fullpath)/1024,1);
89 # Note :Setting $resume to true unnecessarily is not meant to be a problem. Only ever (Feb 2014) seen one weird FTP server where calling SIZE on a non-existent file did create a problem. So, this code just helps that case. (the check for non-empty upload_status[p] is being cautious.
90 $upload_status = $iwp_backup_core->jobdata_get('uploading_substatus');
91 if (0 == $iwp_backup_core->current_resumption || (is_array($upload_status) && !empty($upload_status['p']) && $upload_status['p'] == 0)) {
92 $resume = false;
93 } else {
94 $resume = true;
95 }
96
97 if ($ftp->put($fullpath, $ftp_remote_path.$file, FTP_BINARY, $resume, $iwp_backup_core, $ftp_remote_path)) {
98 $iwp_backup_core->log("FTP upload attempt successful (".$size_k."KB in ".(round(microtime(true)-$timer_start,2)).'s)');
99 $iwp_backup_core->uploaded_file($file);
100 } else {
101 $iwp_backup_core->log("ERROR: FTP upload failed" );
102 $iwp_backup_core->log(sprintf(__("%s upload failed",'InfiniteWP'), 'FTP'), 'error');
103 }
104 }
105
106 return array('ftp_object' => $ftp, 'ftp_remote_path' => $ftp_remote_path);
107 }
108
109 public function listfiles($match = 'backup_') {
110 global $iwp_backup_core;
111
112 $opts = $this->get_options();
113
114 $ftp = $this->getFTP(
115 $opts['host'],
116 $opts['user'],
117 $opts['pass'],
118 $iwp_backup_core->get_job_option('IWP_ssl_nossl'),
119 $iwp_backup_core->get_job_option('IWP_ssl_disableverify'),
120 $iwp_backup_core->get_job_option('IWP_ssl_useservercerts'),
121 $opts['passive']
122 );
123
124 if (is_wp_error($ftp)) return $ftp;
125
126 if (!$ftp->connect()) return new WP_Error('ftp_login_failed', sprintf(__("%s login failure",'InfiniteWP'), 'FTP'));
127
128 $ftp_remote_path = $opts['path'];
129 if ($ftp_remote_path) $ftp_remote_path = trailingslashit($ftp_remote_path);
130 if (!empty($opts['ftp_site_folder'])) {
131 $site_name = iwp_getSiteName();
132 $ftp_remote_path.= trailingslashit($site_name);
133 }
134
135 $dirlist = $ftp->dir_list($ftp_remote_path);
136 if (!is_array($dirlist)) return array();
137
138 $results = array();
139
140 foreach ($dirlist as $k => $path) {
141
142 if ($ftp_remote_path) {
143 // Feb 2015 - found a case where the directory path was not prefixed on
144 if (0 !== strpos($path, $ftp_remote_path) && (false !== strpos('/', $ftp_remote_path) && false !== strpos('\\', $ftp_remote_path))) continue;
145 if (0 === strpos($path, $ftp_remote_path)) $path = substr($path, strlen($ftp_remote_path));
146 // if (0 !== strpos($path, $ftp_remote_path)) continue;
147 // $path = substr($path, strlen($ftp_remote_path));
148 if (0 === strpos($path, $match)) $results[]['name'] = $path;
149 } else {
150 if ('/' == substr($path, 0, 1)) $path = substr($path, 1);
151 if (false !== strpos($path, '/')) continue;
152 if (0 === strpos($path, $match)) $results[]['name'] = $path;
153 }
154
155 unset($dirlist[$k]);
156 }
157
158 # ftp_nlist() doesn't return file sizes. rawlist() does, but is tricky to parse. So, we get the sizes manually.
159 foreach ($results as $ind => $name) {
160 $size = $ftp->size($ftp_remote_path.$name['name']);
161 if (0 === $size) {
162 unset($results[$ind]);
163 } elseif ($size>0) {
164 $results[$ind]['size'] = $size;
165 }
166 }
167
168 return $results;
169
170 }
171
172 public function delete($files, $ftparr = array(), $sizeinfo = array()) {
173
174 global $iwp_backup_core;
175 if (is_string($files)) $files=array($files);
176
177 $opts = $this->get_options();
178
179 if (is_array($ftparr) && isset($ftparr['ftp_object'])) {
180 $ftp = $ftparr['ftp_object'];
181 } else {
182 $ftp = $this->getFTP(
183 $opts['host'],
184 $opts['user'],
185 $opts['pass'],
186 $iwp_backup_core->get_job_option('IWP_ssl_nossl'),
187 $iwp_backup_core->get_job_option('IWP_ssl_disableverify'),
188 $iwp_backup_core->get_job_option('IWP_ssl_useservercerts'),
189 $opts['passive']
190 );
191
192 if (is_wp_error($ftp) || !$ftp->connect()) {
193 if (is_wp_error($ftp)) $iwp_backup_core->log_wp_error($ftp);
194 $iwp_backup_core->log("FTP Failure: we did not successfully log in with those credentials (host=".$opts['host'].").");
195 return false;
196 }
197
198 }
199
200 $ftp_remote_path = isset($ftparr['ftp_remote_path']) ? $ftparr['ftp_remote_path'] : trailingslashit($opts['path']);
201
202 if (!empty($opts['ftp_site_folder'])) {
203 $site_name = iwp_getSiteName();
204 $ftp_remote_path.= trailingslashit($site_name);
205 }
206
207 $ret = true;
208 foreach ($files as $file) {
209 if (@$ftp->delete($ftp_remote_path.$file)) {
210 $iwp_backup_core->log("FTP delete: succeeded (".$ftp_remote_path.$file.")");
211 } else {
212 $iwp_backup_core->log("FTP delete: failed (".$ftp_remote_path.$file.")");
213 $ret = false;
214 }
215 }
216 return $ret;
217
218 }
219
220 public function download($file) {
221
222 global $iwp_backup_core;
223
224 $opts = $this->get_options();
225
226 $ftp = $this->getFTP(
227 $opts['host'],
228 $opts['user'],
229 $opts['pass'],
230 $iwp_backup_core->get_job_option('IWP_ssl_nossl'),
231 $iwp_backup_core->get_job_option('IWP_ssl_disableverify'),
232 $iwp_backup_core->get_job_option('IWP_ssl_useservercerts'),
233 $opts['passive']
234 );
235 if (is_wp_error($ftp)) return $ftp;
236
237 if (!$ftp->connect()) {
238 $iwp_backup_core->log("FTP Failure: we did not successfully log in with those credentials.");
239 $iwp_backup_core->log(sprintf(__("%s login failure",'iwp_backup_core'), 'FTP'), 'error');
240 return false;
241 }
242
243 //$ftp->make_dir(); we may need to recursively create dirs? TODO
244
245 $ftp_remote_path = trailingslashit($opts['path']);
246 if (!empty($opts['ftp_site_folder'])) {
247 $site_name = iwp_getSiteName();
248 $ftp_remote_path.= trailingslashit($site_name);
249 }
250 $fullpath = $iwp_backup_core->backups_dir_location().'/'.$file;
251
252 $resume = false;
253 if (file_exists($fullpath)) {
254 $resume = true;
255 $iwp_backup_core->log("File already exists locally; will resume: size: ".filesize($fullpath));
256 }
257
258 return $ftp->get($fullpath, $ftp_remote_path.$file, FTP_BINARY, $resume, $iwp_backup_core);
259 }
260
261 private function ftp_possible() {
262 $funcs_disabled = array();
263 foreach (array('ftp_connect', 'ftp_login', 'ftp_nb_fput') as $func) {
264 if (!function_exists($func)) $funcs_disabled['ftp'][] = $func;
265 }
266 $funcs_disabled = apply_filters('IWP_ftp_possible', $funcs_disabled);
267 return (0 == count($funcs_disabled)) ? true : $funcs_disabled;
268 }
269
270 public function config_print() {
271 global $iwp_backup_core;
272 $possible = $this->ftp_possible();
273
274 $opts = $this->get_options();
275 }
276
277 }
278