class-wpvivid-amazons3-plus.php
3 years ago
class-wpvivid-base-dropbox.php
3 years ago
class-wpvivid-base-s3.php
5 years ago
class-wpvivid-dropbox.php
3 years ago
class-wpvivid-extend-sftp.php
7 years ago
class-wpvivid-ftpclass.php
3 years ago
class-wpvivid-google-drive.php
3 years ago
class-wpvivid-one-drive.php
3 years ago
class-wpvivid-remote-default.php
6 years ago
class-wpvivid-remote.php
7 years ago
class-wpvivid-s3.php
5 years ago
class-wpvivid-s3compat.php
3 years ago
class-wpvivid-send-to-site.php
6 years ago
class-wpvivid-sftpclass.php
3 years ago
client_secrets.json
7 years ago
class-wpvivid-ftpclass.php
693 lines
| 1 | <?php |
| 2 | |
| 3 | if (!defined('WPVIVID_PLUGIN_DIR')){ |
| 4 | die; |
| 5 | } |
| 6 | |
| 7 | if(!defined('WPVIVID_REMOTE_FTP')) |
| 8 | define('WPVIVID_REMOTE_FTP','ftp'); |
| 9 | |
| 10 | require_once WPVIVID_PLUGIN_DIR .'/includes/customclass/class-wpvivid-remote.php'; |
| 11 | class WPvivid_FTPClass extends WPvivid_Remote{ |
| 12 | private $time_out = 20; |
| 13 | private $callback; |
| 14 | private $options=array(); |
| 15 | |
| 16 | public function __construct($options=array()) |
| 17 | { |
| 18 | if(empty($options)) |
| 19 | { |
| 20 | add_action('wpvivid_add_storage_tab',array($this,'wpvivid_add_storage_tab_ftp'), 15); |
| 21 | add_action('wpvivid_add_storage_page',array($this,'wpvivid_add_storage_page_ftp'), 15); |
| 22 | add_action('wpvivid_edit_remote_page',array($this,'wpvivid_edit_storage_page_ftp'), 15); |
| 23 | add_filter('wpvivid_remote_pic',array($this,'wpvivid_remote_pic_ftp'),9); |
| 24 | add_filter('wpvivid_get_out_of_date_remote',array($this,'wpvivid_get_out_of_date_ftp'),10,2); |
| 25 | add_filter('wpvivid_storage_provider_tran',array($this,'wpvivid_storage_provider_ftp'),10); |
| 26 | |
| 27 | }else{ |
| 28 | $this->options = $options; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public function wpvivid_add_storage_tab_ftp() |
| 33 | { |
| 34 | ?> |
| 35 | <div class="storage-providers" remote_type="ftp" onclick="select_remote_storage(event, 'storage_account_ftp');"> |
| 36 | <img src="<?php echo esc_url(WPVIVID_PLUGIN_URL.'/admin/partials/images/storage-ftp.png'); ?>" style="vertical-align:middle;"/><?php _e('FTP', 'wpvivid-backuprestore'); ?> |
| 37 | </div> |
| 38 | <?php |
| 39 | } |
| 40 | |
| 41 | public function wpvivid_add_storage_page_ftp() |
| 42 | { |
| 43 | ?> |
| 44 | <div id="storage_account_ftp" class="storage-account-page" style="display:none;"> |
| 45 | <div style="padding: 0 10px 10px 0;"><strong><?php _e('Enter Your FTP Account', 'wpvivid-backuprestore'); ?></strong></div> |
| 46 | <table class="wp-list-table widefat plugins" style="width:100%;"> |
| 47 | <tbody> |
| 48 | <form> |
| 49 | <tr> |
| 50 | <td class="plugin-title column-primary"> |
| 51 | <div class="wpvivid-storage-form"> |
| 52 | <input type="text" autocomplete="off" option="ftp" name="name" placeholder="<?php esc_attr_e('Enter an unique alias: e.g. FTP-001', 'wpvivid-backuprestore'); ?>" class="regular-text" onkeyup="value=value.replace(/[^a-zA-Z0-9\-_]/g,'')" /> |
| 53 | </div> |
| 54 | </td> |
| 55 | <td class="column-description desc"> |
| 56 | <div class="wpvivid-storage-form-desc"> |
| 57 | <i><?php _e('A name to help you identify the storage if you have multiple remote storage connected.', 'wpvivid-backuprestore'); ?></i> |
| 58 | </div> |
| 59 | </td> |
| 60 | </tr> |
| 61 | <tr> |
| 62 | <td class="plugin-title column-primary"> |
| 63 | <div class="wpvivid-storage-form"> |
| 64 | <input type="text" autocomplete="off" option="ftp" name="server" placeholder="<?php esc_attr_e('FTP server (server\'s port 21)','wpvivid-backuprestore'); ?>" class="regular-text"/> |
| 65 | </div> |
| 66 | </td> |
| 67 | <td class="column-description desc"> |
| 68 | <div class="wpvivid-storage-form-desc"> |
| 69 | <i style="margin-right: 10px;"><?php _e('Enter the FTP server.', 'wpvivid-backuprestore'); ?></i> |
| 70 | </div> |
| 71 | </td> |
| 72 | </tr> |
| 73 | <tr> |
| 74 | <td class="plugin-title column-primary"> |
| 75 | <div class="wpvivid-storage-form"> |
| 76 | <input type="text" autocomplete="off" class="regular-text" value="21" readonly="readonly" /> |
| 77 | </div> |
| 78 | </td> |
| 79 | <td class="column-description desc"> |
| 80 | <div class="wpvivid-storage-form-desc"> |
| 81 | <a href="https://docs.wpvivid.com/wpvivid-backup-pro-ftp-change-ftp-default-port.html"><?php _e('Pro feature: Change the FTP default port number', 'wpvivid-backuprestore'); ?></a> |
| 82 | </div> |
| 83 | </td> |
| 84 | </tr> |
| 85 | <tr> |
| 86 | <td class="plugin-title column-primary"> |
| 87 | <div class="wpvivid-storage-form"> |
| 88 | <input type="text" class="regular-text" autocomplete="off" option="ftp" name="username" placeholder="<?php esc_attr_e('FTP login', 'wpvivid-backuprestore'); ?>" /> |
| 89 | </div> |
| 90 | </td> |
| 91 | <td class="column-description desc"> |
| 92 | <div class="wpvivid-storage-form-desc"> |
| 93 | <i><?php _e('Enter your FTP server user name.', 'wpvivid-backuprestore'); ?></i> |
| 94 | </div> |
| 95 | </td> |
| 96 | </tr> |
| 97 | <tr> |
| 98 | <td class="plugin-title column-primary"> |
| 99 | <div class="wpvivid-storage-form"> |
| 100 | <input type="password" class="regular-text" autocomplete="new-password" option="ftp" name="password" placeholder="<?php esc_attr_e('FTP password', 'wpvivid-backuprestore'); ?>" /> |
| 101 | </div> |
| 102 | </td> |
| 103 | <td class="column-description desc"> |
| 104 | <div class="wpvivid-storage-form-desc"> |
| 105 | <i><?php _e('Enter the FTP server password.', 'wpvivid-backuprestore'); ?></i> |
| 106 | </div> |
| 107 | </td> |
| 108 | </tr> |
| 109 | <tr> |
| 110 | <td class="plugin-title column-primary"> |
| 111 | <div class="wpvivid-storage-form"> |
| 112 | <input type="text" autocomplete="off" option="ftp" name="path" placeholder="<?php esc_attr_e('Absolute path must exist(e.g. /home/username)', 'wpvivid-backuprestore'); ?>" class="regular-text"/> |
| 113 | </div> |
| 114 | </td> |
| 115 | <td class="column-description desc"> |
| 116 | <div class="wpvivid-storage-form-desc"> |
| 117 | <i><?php _e('Enter an absolute path and a custom subdirectory (optional) for holding the backups of current website. For example, /home/username/customfolder', 'wpvivid-backuprestore'); ?></i> |
| 118 | </div> |
| 119 | </td> |
| 120 | </tr> |
| 121 | <tr> |
| 122 | <td class="plugin-title column-primary"> |
| 123 | <div class="wpvivid-storage-select"> |
| 124 | <label> |
| 125 | <input type="checkbox" option="ftp" name="default" checked /><?php _e('Set as the default remote storage.', 'wpvivid-backuprestore'); ?> |
| 126 | </label> |
| 127 | </div> |
| 128 | </td> |
| 129 | <td class="column-description desc"> |
| 130 | <div class="wpvivid-storage-form-desc"> |
| 131 | <i><?php _e('Once checked, all this sites backups sent to a remote storage destination will be uploaded to this storage by default.', 'wpvivid-backuprestore'); ?></i> |
| 132 | </div> |
| 133 | </td> |
| 134 | </tr> |
| 135 | <tr> |
| 136 | <td class="plugin-title column-primary"> |
| 137 | <div class="wpvivid-storage-select"> |
| 138 | <label> |
| 139 | <input type="checkbox" option="ftp" name="passive" checked /><?php _e('Uncheck this to enable FTP active mode.', 'wpvivid-backuprestore'); ?> |
| 140 | </label> |
| 141 | </div> |
| 142 | </td> |
| 143 | <td class="column-description desc"> |
| 144 | <div class="wpvivid-storage-form-desc"> |
| 145 | <i><?php _e('Uncheck the option to use FTP active mode when transferring files. Make sure the FTP server you are configuring supports the active FTP mode.', 'wpvivid-backuprestore'); ?></i> |
| 146 | </div> |
| 147 | </td> |
| 148 | </tr> |
| 149 | </form> |
| 150 | <tr> |
| 151 | <td class="plugin-title column-primary"> |
| 152 | <div class="wpvivid-storage-form"> |
| 153 | <input class="button-primary" type="submit" option="add-remote" value="<?php esc_attr_e('Test and Add', 'wpvivid-backuprestore'); ?>"> |
| 154 | </div> |
| 155 | </td> |
| 156 | <td class="column-description desc"> |
| 157 | <div class="wpvivid-storage-form-desc"> |
| 158 | <i><?php _e('Click the button to connect to FTP server and add it to the storage list below.', 'wpvivid-backuprestore'); ?></i> |
| 159 | </div> |
| 160 | </td> |
| 161 | </tr> |
| 162 | </tbody> |
| 163 | </table> |
| 164 | </div> |
| 165 | <?php |
| 166 | } |
| 167 | |
| 168 | public function wpvivid_edit_storage_page_ftp() |
| 169 | { |
| 170 | ?> |
| 171 | <div id="remote_storage_edit_ftp" class="postbox storage-account-block remote-storage-edit" style="display:none;"> |
| 172 | <div style="padding: 0 10px 10px 0;"><strong><?php _e('Enter Your FTP Account', 'wpvivid-backuprestore'); ?></strong></div> |
| 173 | <table class="wp-list-table widefat plugins" style="width:100%;"> |
| 174 | <tbody> |
| 175 | <form> |
| 176 | <tr> |
| 177 | <td class="plugin-title column-primary"> |
| 178 | <div class="wpvivid-storage-form"> |
| 179 | <input type="text" autocomplete="off" option="edit-ftp" name="name" placeholder="<?php esc_attr_e('Enter an unique alias: e.g. FTP-001', 'wpvivid-backuprestore'); ?>" class="regular-text" onkeyup="value=value.replace(/[^a-zA-Z0-9\-_]/g,'')" /> |
| 180 | </div> |
| 181 | </td> |
| 182 | <td class="column-description desc"> |
| 183 | <div class="wpvivid-storage-form-desc"> |
| 184 | <i><?php _e('A name to help you identify the storage if you have multiple remote storage connected.', 'wpvivid-backuprestore'); ?></i> |
| 185 | </div> |
| 186 | </td> |
| 187 | </tr> |
| 188 | <tr> |
| 189 | <td class="plugin-title column-primary"> |
| 190 | <div class="wpvivid-storage-form"> |
| 191 | <input type="text" autocomplete="off" option="edit-ftp" name="server" placeholder="<?php esc_attr_e('FTP server (server\'s port 21)', 'wpvivid-backuprestore'); ?>" class="regular-text"/> |
| 192 | </div> |
| 193 | </td> |
| 194 | <td class="column-description desc"> |
| 195 | <div class="wpvivid-storage-form-desc"> |
| 196 | <i style="margin-right: 10px;"><?php _e('Enter the FTP server.', 'wpvivid-backuprestore'); ?></i> |
| 197 | </div> |
| 198 | </td> |
| 199 | </tr> |
| 200 | <tr> |
| 201 | <td class="plugin-title column-primary"> |
| 202 | <div class="wpvivid-storage-form"> |
| 203 | <input type="text" class="regular-text" autocomplete="off" option="edit-ftp" name="username" placeholder="<?php esc_attr_e('FTP login', 'wpvivid-backuprestore'); ?>" /> |
| 204 | </div> |
| 205 | </td> |
| 206 | <td class="column-description desc"> |
| 207 | <div class="wpvivid-storage-form-desc"> |
| 208 | <i><?php _e('Enter your FTP server user name.', 'wpvivid-backuprestore'); ?></i> |
| 209 | </div> |
| 210 | </td> |
| 211 | </tr> |
| 212 | <tr> |
| 213 | <td class="plugin-title column-primary"> |
| 214 | <div class="wpvivid-storage-form"> |
| 215 | <input type="password" class="regular-text" autocomplete="new-password" option="edit-ftp" name="password" placeholder="<?php esc_attr_e('FTP password', 'wpvivid-backuprestore'); ?>" /> |
| 216 | </div> |
| 217 | </td> |
| 218 | <td class="column-description desc"> |
| 219 | <div class="wpvivid-storage-form-desc"> |
| 220 | <i><?php _e('Enter the FTP server password.', 'wpvivid-backuprestore'); ?></i> |
| 221 | </div> |
| 222 | </td> |
| 223 | </tr> |
| 224 | <tr> |
| 225 | <td class="plugin-title column-primary"> |
| 226 | <div class="wpvivid-storage-form"> |
| 227 | <input type="text" autocomplete="off" option="edit-ftp" name="path" placeholder="<?php esc_attr_e('Absolute path must exist(e.g. /home/username)', 'wpvivid-backuprestore'); ?>" class="regular-text"/> |
| 228 | </div> |
| 229 | </td> |
| 230 | <td class="column-description desc"> |
| 231 | <div class="wpvivid-storage-form-desc"> |
| 232 | <i><?php _e('Enter an absolute path and a custom subdirectory (optional) for holding the backups of current website. For example, /home/username/customfolder', 'wpvivid-backuprestore'); ?></i> |
| 233 | </div> |
| 234 | </td> |
| 235 | </tr> |
| 236 | <tr> |
| 237 | <td class="plugin-title column-primary"> |
| 238 | <div class="wpvivid-storage-select"> |
| 239 | <label> |
| 240 | <input type="checkbox" option="edit-ftp" name="passive" checked /><?php _e('Uncheck this to enable FTP active mode.', 'wpvivid-backuprestore'); ?> |
| 241 | </label> |
| 242 | </div> |
| 243 | </td> |
| 244 | <td class="column-description desc"> |
| 245 | <div class="wpvivid-storage-form-desc"> |
| 246 | <i><?php _e('Uncheck the option to use FTP active mode when transferring files. Make sure the FTP server you are configuring supports the active FTP mode.', 'wpvivid-backuprestore'); ?></i> |
| 247 | </div> |
| 248 | </td> |
| 249 | </tr> |
| 250 | </form> |
| 251 | <tr> |
| 252 | <td class="plugin-title column-primary"> |
| 253 | <div class="wpvivid-storage-form"> |
| 254 | <input class="button-primary" type="submit" option="edit-remote" value="<?php esc_attr_e('Save Changes', 'wpvivid-backuprestore'); ?>"> |
| 255 | </div> |
| 256 | </td> |
| 257 | <td class="column-description desc"> |
| 258 | <div class="wpvivid-storage-form-desc"> |
| 259 | <i><?php _e('Click the button to save the changes.', 'wpvivid-backuprestore'); ?></i> |
| 260 | </div> |
| 261 | </td> |
| 262 | </tr> |
| 263 | </tbody> |
| 264 | </table> |
| 265 | </div> |
| 266 | <?php |
| 267 | } |
| 268 | |
| 269 | public function wpvivid_remote_pic_ftp($remote){ |
| 270 | $remote['ftp']['default_pic'] = '/admin/partials/images/storage-ftp(gray).png'; |
| 271 | $remote['ftp']['selected_pic'] = '/admin/partials/images/storage-ftp.png'; |
| 272 | $remote['ftp']['title'] = 'FTP'; |
| 273 | return $remote; |
| 274 | } |
| 275 | |
| 276 | public function test_connect() |
| 277 | { |
| 278 | $passive =$this->options['passive']; |
| 279 | $host = $this->options['host']; |
| 280 | $username = $this->options['username']; |
| 281 | if(isset($this->options['is_encrypt']) && $this->options['is_encrypt'] == 1){ |
| 282 | $password = base64_decode($this->options['password']); |
| 283 | } |
| 284 | else { |
| 285 | $password = $this->options['password']; |
| 286 | } |
| 287 | $path = $this->options['path']; |
| 288 | $port = empty($this->options['port'])?21:$this->options['port']; |
| 289 | $conn = $this -> do_connect($host,$username,$password,$port); |
| 290 | if(is_array($conn) && array_key_exists('result',$conn)) |
| 291 | return $conn; |
| 292 | ftp_pasv($conn,$passive); |
| 293 | return $this->do_chdir($conn,$path); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | public function sanitize_options($skip_name='') |
| 298 | { |
| 299 | $ret['result']=WPVIVID_FAILED; |
| 300 | if(!isset($this->options['name'])) |
| 301 | { |
| 302 | $ret['error']=__('Warning: An alias for remote storage is required.','wpvivid-backuprestore'); |
| 303 | return $ret; |
| 304 | } |
| 305 | |
| 306 | $this->options['name']=sanitize_text_field($this->options['name']); |
| 307 | |
| 308 | if(empty($this->options['name'])) |
| 309 | { |
| 310 | $ret['error']=__('Warning: An alias for remote storage is required.','wpvivid-backuprestore'); |
| 311 | return $ret; |
| 312 | } |
| 313 | |
| 314 | $remoteslist=WPvivid_Setting::get_all_remote_options(); |
| 315 | foreach ($remoteslist as $key=>$value) |
| 316 | { |
| 317 | if(isset($value['name'])&&$value['name'] == $this->options['name']&&$skip_name!=$value['name']) |
| 318 | { |
| 319 | $ret['error']="Warning: The alias already exists in storage list."; |
| 320 | return $ret; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | $this->options['server']=sanitize_text_field($this->options['server']); |
| 325 | |
| 326 | if(empty($this->options['server'])) |
| 327 | { |
| 328 | $ret['error']="Warning: The FTP server is required."; |
| 329 | return $ret; |
| 330 | } |
| 331 | $res = explode(':',$this -> options['server']); |
| 332 | if(sizeof($res) > 1){ |
| 333 | $this ->options['host'] = $res[0]; |
| 334 | if($res[1] != 21){ |
| 335 | $ret['error']='Currently, only port 21 is supported.'; |
| 336 | return $ret; |
| 337 | } |
| 338 | |
| 339 | }else{ |
| 340 | $this -> options['host'] = $res[0]; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | if(!isset($this->options['username'])) |
| 345 | { |
| 346 | $ret['error']="Warning: The FTP login is required."; |
| 347 | return $ret; |
| 348 | } |
| 349 | |
| 350 | $this->options['username']=sanitize_text_field($this->options['username']); |
| 351 | |
| 352 | if(empty($this->options['username'])) |
| 353 | { |
| 354 | $ret['error']="Warning: The FTP login is required."; |
| 355 | return $ret; |
| 356 | } |
| 357 | |
| 358 | if(!isset($this->options['password'])||empty($this->options['password'])) |
| 359 | { |
| 360 | $ret['error']="Warning: The FTP password is required."; |
| 361 | return $ret; |
| 362 | } |
| 363 | |
| 364 | $this->options['password']=sanitize_text_field($this->options['password']); |
| 365 | |
| 366 | if(empty($this->options['password'])) |
| 367 | { |
| 368 | $ret['error']="Warning: The FTP password is required."; |
| 369 | return $ret; |
| 370 | } |
| 371 | $this->options['password'] = base64_encode($this->options['password']); |
| 372 | $this->options['is_encrypt'] = 1; |
| 373 | |
| 374 | if(!isset($this->options['path'])||empty($this->options['path'])) |
| 375 | { |
| 376 | $ret['error']="Warning: The storage path is required."; |
| 377 | return $ret; |
| 378 | } |
| 379 | |
| 380 | $this->options['path']=sanitize_text_field($this->options['path']); |
| 381 | |
| 382 | if(empty($this->options['path'])) |
| 383 | { |
| 384 | $ret['error']="Warning: The storage path is required."; |
| 385 | return $ret; |
| 386 | } |
| 387 | |
| 388 | if($this->options['path']=='/') |
| 389 | { |
| 390 | $ret['error']="Warning: Root directory is forbidden to set to '/'."; |
| 391 | return $ret; |
| 392 | } |
| 393 | |
| 394 | $ret['result']=WPVIVID_SUCCESS; |
| 395 | $ret['options']=$this->options; |
| 396 | return $ret; |
| 397 | } |
| 398 | |
| 399 | public function do_connect($server,$username,$password,$port = 21) |
| 400 | { |
| 401 | $conn = ftp_connect( $server, $port, $this ->time_out ); |
| 402 | |
| 403 | if($conn) |
| 404 | { |
| 405 | if(ftp_login($conn,$username,$password)) |
| 406 | { |
| 407 | return $conn; |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | return array('result'=>WPVIVID_FAILED,'error'=>'Login failed. You have entered the incorrect credential(s). Please try again.'); |
| 412 | } |
| 413 | } |
| 414 | else{ |
| 415 | return array('result'=>WPVIVID_FAILED,'error'=>'Login failed. The connection has timed out. Please try again later.'); |
| 416 | } |
| 417 | } |
| 418 | public function do_chdir($conn,$path){ |
| 419 | @ftp_chdir($conn,'/'); |
| 420 | if(!@ftp_chdir($conn,$path)) |
| 421 | { |
| 422 | $parts = explode('/',$path); |
| 423 | foreach($parts as $part){ |
| 424 | if($part !== '') { |
| 425 | if (!@ftp_chdir($conn, $part)) { |
| 426 | if (!ftp_mkdir($conn, $part)) { |
| 427 | return array('result' => WPVIVID_FAILED, 'error' => 'Failed to create a backup. Make sure you have sufficient privileges to perform the operation.'); |
| 428 | } |
| 429 | |
| 430 | if (!@ftp_chdir($conn, $part)) { |
| 431 | return array('result' => WPVIVID_FAILED, 'error' => 'Failed to create a backup. Make sure you have sufficient privileges to perform the operation.'); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /*if ( ! ftp_mkdir( $conn, $path ) ) |
| 438 | { |
| 439 | return array('result'=>WPVIVID_FAILED,'error'=>'Failed to create a backup. Make sure you have sufficient privileges to perform the operation.'); |
| 440 | } |
| 441 | if (!@ftp_chdir($conn,$path)) |
| 442 | { |
| 443 | return array('result'=>WPVIVID_FAILED,'error'=>'Failed to create a backup. Make sure you have sufficient privileges to perform the operation.'); |
| 444 | }*/ |
| 445 | } |
| 446 | $temp_file = md5(rand()); |
| 447 | $temp_path = trailingslashit(WP_CONTENT_DIR).WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.$temp_file; |
| 448 | file_put_contents($temp_path,print_r($temp_file,true)); |
| 449 | if(! ftp_put($conn,trailingslashit($path).$temp_file,$temp_path,FTP_BINARY)){ |
| 450 | return array('result'=>WPVIVID_FAILED,'error'=>'Failed to add FTP storage. It can be because the FTP folder permissions are insufficient, or calling PHP ftp_put function of your web server failed. Please make sure the folder has write permission and the ftp_put function works properly.'); |
| 451 | } |
| 452 | @unlink($temp_path); |
| 453 | @ftp_delete($conn,trailingslashit($path).$temp_file); |
| 454 | return array('result'=>WPVIVID_SUCCESS); |
| 455 | } |
| 456 | |
| 457 | public function upload($task_id,$files,$callback = '') |
| 458 | { |
| 459 | global $wpvivid_plugin; |
| 460 | $this -> callback = $callback; |
| 461 | |
| 462 | $passive =$this->options['passive']; |
| 463 | $host = $this->options['host']; |
| 464 | $username = $this->options['username']; |
| 465 | if(isset($this->options['is_encrypt']) && $this->options['is_encrypt'] == 1){ |
| 466 | $password = base64_decode($this->options['password']); |
| 467 | } |
| 468 | else { |
| 469 | $password = $this->options['password']; |
| 470 | } |
| 471 | $path = $this->options['path']; |
| 472 | $port = empty($this->options['port'])?21:$this->options['port']; |
| 473 | |
| 474 | $upload_job=WPvivid_taskmanager::get_backup_sub_task_progress($task_id,'upload',WPVIVID_REMOTE_FTP); |
| 475 | if(empty($upload_job)) |
| 476 | { |
| 477 | $job_data=array(); |
| 478 | foreach ($files as $file) |
| 479 | { |
| 480 | if(!file_exists($file)) |
| 481 | return array('result'=>WPVIVID_FAILED,'error'=>$file.' not found. The file might has been moved, renamed or deleted. Please back it up again.'); |
| 482 | $file_data['size']=filesize($file); |
| 483 | $file_data['uploaded']=0; |
| 484 | $job_data[basename($file)]=$file_data; |
| 485 | } |
| 486 | WPvivid_taskmanager::update_backup_sub_task_progress($task_id,'upload',WPVIVID_REMOTE_FTP,WPVIVID_UPLOAD_UNDO,'Start uploading.',$job_data); |
| 487 | $upload_job=WPvivid_taskmanager::get_backup_sub_task_progress($task_id,'upload',WPVIVID_REMOTE_FTP); |
| 488 | } |
| 489 | $wpvivid_plugin->wpvivid_log->WriteLog('Connecting to server '.$host,'notice'); |
| 490 | $conn = $this -> do_connect($host,$username,$password,$port); |
| 491 | if(is_array($conn) && array_key_exists('result',$conn)) |
| 492 | return $conn; |
| 493 | ftp_pasv($conn,$passive); |
| 494 | $wpvivid_plugin->wpvivid_log->WriteLog('chdir '.$path,'notice'); |
| 495 | $str = $this -> do_chdir($conn , $path); |
| 496 | if($str['result'] !== WPVIVID_SUCCESS) |
| 497 | return $str; |
| 498 | |
| 499 | $flag = true; |
| 500 | $error = ''; |
| 501 | foreach ($files as $key => $file) |
| 502 | { |
| 503 | if(is_array($upload_job['job_data']) && array_key_exists(basename($file),$upload_job['job_data'])) |
| 504 | { |
| 505 | if($upload_job['job_data'][basename($file)]['uploaded']==1) |
| 506 | continue; |
| 507 | } |
| 508 | $this ->last_time = time(); |
| 509 | $this -> last_size = 0; |
| 510 | $wpvivid_plugin->wpvivid_log->WriteLog('Start uploading '.basename($file),'notice'); |
| 511 | $remote_file = trailingslashit($path).basename($file); |
| 512 | if(!file_exists($file)) |
| 513 | return array('result'=>WPVIVID_FAILED,'error'=>$file.' not found. The file might has been moved, renamed or deleted. Please back it up again.'); |
| 514 | |
| 515 | $wpvivid_plugin->set_time_limit($task_id); |
| 516 | |
| 517 | for($i =0;$i <WPVIVID_REMOTE_CONNECT_RETRY_TIMES;$i ++) |
| 518 | { |
| 519 | $this -> current_file_name = basename($file); |
| 520 | $this -> current_file_size = filesize($file); |
| 521 | $this -> last_time = time(); |
| 522 | $this -> last_size = 0; |
| 523 | $local_handle = fopen($file,'rb'); |
| 524 | if(!$local_handle) |
| 525 | { |
| 526 | return array('result'=>WPVIVID_FAILED,'error'=>'Failed to open '.$this->current_file_name.'.'); |
| 527 | } |
| 528 | $status = ftp_nb_fput($conn,$remote_file,$local_handle,FTP_BINARY,0); |
| 529 | while ($status == FTP_MOREDATA) |
| 530 | { |
| 531 | $status = ftp_nb_continue($conn); |
| 532 | if((time() - $this -> last_time) >3) |
| 533 | { |
| 534 | if(is_callable($callback)){ |
| 535 | call_user_func_array($callback,array(ftell($local_handle),$this -> current_file_name, |
| 536 | $this->current_file_size,$this -> last_time,$this -> last_size)); |
| 537 | } |
| 538 | $this -> last_size = ftell($local_handle); |
| 539 | $this -> last_time = time(); |
| 540 | } |
| 541 | } |
| 542 | if ($status != FTP_FINISHED) |
| 543 | { |
| 544 | return array('result'=>WPVIVID_FAILED,'error'=>'Uploading '.$remote_file.' to FTP server failed. '.$remote_file.' might be deleted or network doesn\'t work properly. Please verify the file and confirm the network connection and try again later.'); |
| 545 | } |
| 546 | |
| 547 | if($status == FTP_FINISHED) |
| 548 | { |
| 549 | $wpvivid_plugin->wpvivid_log->WriteLog('Finished uploading '.basename($file),'notice'); |
| 550 | $upload_job['job_data'][basename($file)]['uploaded']=1; |
| 551 | WPvivid_taskmanager::update_backup_sub_task_progress($task_id,'upload',WPVIVID_REMOTE_FTP,WPVIVID_UPLOAD_SUCCESS,'Uploading '.basename($file).' completed.',$upload_job['job_data']); |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | if($status != FTP_FINISHED && $i == (WPVIVID_REMOTE_CONNECT_RETRY_TIMES - 1)) |
| 556 | { |
| 557 | $flag = false; |
| 558 | $error = 'Uploading '.basename($file).' to FTP server failed. '.basename($file).' might be deleted or network doesn\'t work properly. Please verify the file and confirm the network connection and try again later.'; |
| 559 | break 2; |
| 560 | } |
| 561 | sleep(WPVIVID_REMOTE_CONNECT_RETRY_INTERVAL); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | if($flag){ |
| 566 | return array('result'=>WPVIVID_SUCCESS); |
| 567 | }else{ |
| 568 | return array('result'=>WPVIVID_FAILED,'error'=>$error); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | public function download($file,$local_path,$callback = '') |
| 573 | { |
| 574 | try { |
| 575 | global $wpvivid_plugin; |
| 576 | $passive = $this->options['passive']; |
| 577 | $host = $this->options['host']; |
| 578 | $username = $this->options['username']; |
| 579 | if(isset($this->options['is_encrypt']) && $this->options['is_encrypt'] == 1){ |
| 580 | $password = base64_decode($this->options['password']); |
| 581 | } |
| 582 | else { |
| 583 | $password = $this->options['password']; |
| 584 | } |
| 585 | $path = $this->options['path']; |
| 586 | $port = empty($this->options['port']) ? 21 : $this->options['port']; |
| 587 | |
| 588 | $local_path = trailingslashit($local_path) . $file['file_name']; |
| 589 | $remote_file = trailingslashit($path) . $file['file_name']; |
| 590 | |
| 591 | $this->current_file_name = $file['file_name']; |
| 592 | $this->current_file_size = $file['size']; |
| 593 | |
| 594 | $wpvivid_plugin->wpvivid_download_log->WriteLog('Connecting FTP server.','notice'); |
| 595 | $conn = $this->do_connect($host, $username, $password, $port); |
| 596 | if (is_array($conn) && array_key_exists('result', $conn)) { |
| 597 | return $conn; |
| 598 | } |
| 599 | |
| 600 | ftp_pasv($conn, $passive); |
| 601 | $wpvivid_plugin->wpvivid_download_log->WriteLog('Create local file.','notice'); |
| 602 | $local_handle = fopen($local_path, 'ab'); |
| 603 | if (!$local_handle) { |
| 604 | return array('result' => WPVIVID_FAILED, 'error' => 'Unable to create the local file. Please make sure the folder is writable and try again.'); |
| 605 | } |
| 606 | |
| 607 | $stat = fstat($local_handle); |
| 608 | $offset = $stat['size']; |
| 609 | $wpvivid_plugin->wpvivid_download_log->WriteLog('Downloading file ' . $file['file_name'] . ', Size: ' . $file['size'] ,'notice'); |
| 610 | $status = ftp_nb_fget($conn, $local_handle, $remote_file, FTP_BINARY, $offset); |
| 611 | while ($status == FTP_MOREDATA) { |
| 612 | $status = ftp_nb_continue($conn); |
| 613 | if ((time() - $this->last_time) > 3) { |
| 614 | if (is_callable($callback)) { |
| 615 | call_user_func_array($callback, array(ftell($local_handle), $this->current_file_name, |
| 616 | $this->current_file_size, $this->last_time, $this->last_size)); |
| 617 | } |
| 618 | $this->last_size = ftell($local_handle); |
| 619 | $this->last_time = time(); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if(filesize($local_path) == $file['size']){ |
| 624 | if($wpvivid_plugin->wpvivid_check_zip_valid()) { |
| 625 | $res = TRUE; |
| 626 | } |
| 627 | else{ |
| 628 | $res = FALSE; |
| 629 | } |
| 630 | } |
| 631 | else{ |
| 632 | $res = FALSE; |
| 633 | } |
| 634 | |
| 635 | if ($status != FTP_FINISHED || $res !== TRUE) { |
| 636 | @unlink($local_path); |
| 637 | return array('result' => WPVIVID_FAILED, 'error' => 'Downloading ' . $remote_file . ' failed. ' . $remote_file . ' might be deleted or network doesn\'t work properly. Please verify the file and confirm the network connection and try again later.'); |
| 638 | } |
| 639 | |
| 640 | ftp_close($conn); |
| 641 | fclose($local_handle); |
| 642 | return array('result' => WPVIVID_SUCCESS); |
| 643 | } |
| 644 | catch (Exception $error){ |
| 645 | $message = 'An exception has occurred. class: '.get_class($error).';msg: '.$error->getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; |
| 646 | error_log($message); |
| 647 | return array('result'=>WPVIVID_FAILED, 'error'=>$message); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | public function cleanup($file){ |
| 652 | $host = $this->options['host']; |
| 653 | $username = $this->options['username']; |
| 654 | if(isset($this->options['is_encrypt']) && $this->options['is_encrypt'] == 1){ |
| 655 | $password = base64_decode($this->options['password']); |
| 656 | } |
| 657 | else { |
| 658 | $password = $this->options['password']; |
| 659 | } |
| 660 | $path = $this->options['path']; |
| 661 | $port = empty($this->options['port'])?21:$this->options['port']; |
| 662 | |
| 663 | $conn = $this -> do_connect($host,$username,$password,$port); |
| 664 | if(is_array($conn) && array_key_exists('result',$conn)) |
| 665 | return $conn; |
| 666 | |
| 667 | foreach ($file as $value){ |
| 668 | @ftp_delete($conn,trailingslashit($path).$value); |
| 669 | } |
| 670 | return array('result'=>WPVIVID_SUCCESS); |
| 671 | } |
| 672 | |
| 673 | public function init_remotes($remote_collection){ |
| 674 | $remote_collection[WPVIVID_REMOTE_FTP] = 'WPvivid_FTPClass'; |
| 675 | return $remote_collection; |
| 676 | } |
| 677 | |
| 678 | public function wpvivid_get_out_of_date_ftp($out_of_date_remote, $remote) |
| 679 | { |
| 680 | if($remote['type'] == WPVIVID_REMOTE_FTP){ |
| 681 | $out_of_date_remote = $remote['path']; |
| 682 | } |
| 683 | return $out_of_date_remote; |
| 684 | } |
| 685 | |
| 686 | public function wpvivid_storage_provider_ftp($storage_type) |
| 687 | { |
| 688 | if($storage_type == WPVIVID_REMOTE_FTP){ |
| 689 | $storage_type = 'FTP'; |
| 690 | } |
| 691 | return $storage_type; |
| 692 | } |
| 693 | } |