PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/backupbuddy2/destinations/ftp/init.php

https://gitlab.com/mattswann/launch-housing
PHP | 367 lines | 260 code | 66 blank | 41 comment | 67 complexity | 92a26cb0ebe24726b7f68b02a2300752 MD5 | raw file
  1. <?php
  2. // DO NOT CALL THIS CLASS DIRECTLY. CALL VIA: pb_backupbuddy_destination in bootstrap.php.
  3. class pb_backupbuddy_destination_ftp {
  4. public static $destination_info = array(
  5. 'name' => 'FTP',
  6. 'description' => 'File Transport Protocol. This is the most common way of sending larger files between servers. Most web hosting accounts provide FTP access. This common and well-known transfer method is tried and true.',
  7. );
  8. // Default settings. Should be public static for auto-merging.
  9. public static $default_settings = array(
  10. 'type' => 'ftp', // MUST MATCH your destination slug.
  11. 'title' => '', // Required destination field.
  12. 'address' => '',
  13. 'username' => '',
  14. 'password' => '',
  15. 'path' => '',
  16. 'active_mode' => 0, // 1 = active, 0=passive mode (default > v3.1.8).
  17. 'ftps' => 0,
  18. 'archive_limit' => 0,
  19. 'url' => '', // optional url for migration that corresponds to this ftp/path.
  20. 'disable_file_management' => '0', // When 1, _manage.php will not load which renders remote file management DISABLED.
  21. 'disabled' => '0', // When 1, disable this destination.
  22. );
  23. /* send()
  24. *
  25. * Send one or more files.
  26. *
  27. * @param array $files Array of one or more files to send.
  28. * @return boolean True on success, else false.
  29. */
  30. public static function send( $settings = array(), $files = array(), $send_id = '' ) {
  31. global $pb_backupbuddy_destination_errors;
  32. if ( '1' == $settings['disabled'] ) {
  33. $pb_backupbuddy_destination_errors[] = __( 'Error #48933: This destination is currently disabled. Enable it under this destination\'s Advanced Settings.', 'it-l10n-backupbuddy' );
  34. return false;
  35. }
  36. if ( ! is_array( $files ) ) {
  37. $files = array( $files );
  38. }
  39. pb_backupbuddy::status( 'details', 'FTP class send() function started.' );
  40. if ( $settings['active_mode'] == '0' ) {
  41. $active_mode = false;
  42. } else {
  43. $active_mode = true;
  44. }
  45. $server = $settings['address'];
  46. $username = $settings['username'];
  47. $password = $settings['password'];
  48. $path = $settings['path'];
  49. $ftps = $settings['ftps'];
  50. $limit = $settings['archive_limit'];
  51. $active = $settings['active_mode'];
  52. $port = '21'; // Default FTP port.
  53. if ( strstr( $server, ':' ) ) { // Handle custom FTP port.
  54. $server_params = explode( ':', $server );
  55. $server = $server_params[0];
  56. $port = $server_params[1];
  57. }
  58. // Connect to server.
  59. if ( $ftps == '1' ) { // Connect with FTPs.
  60. if ( function_exists( 'ftp_ssl_connect' ) ) {
  61. $conn_id = ftp_ssl_connect( $server, $port );
  62. if ( $conn_id === false ) {
  63. pb_backupbuddy::status( 'details', 'Unable to connect to FTPS `' . $server . '` on port `' . $port . '` (check address/FTPS support and that server can connect to this address via this port).', 'error' );
  64. return false;
  65. } else {
  66. pb_backupbuddy::status( 'details', 'Connected to FTPs.' );
  67. }
  68. } else {
  69. pb_backupbuddy::status( 'details', 'Your web server doesnt support FTPS in PHP.', 'error' );
  70. return false;
  71. }
  72. } else { // Connect with FTP (normal).
  73. if ( function_exists( 'ftp_connect' ) ) {
  74. $conn_id = ftp_connect( $server, $port );
  75. if ( $conn_id === false ) {
  76. pb_backupbuddy::status( 'details', 'ERROR: Unable to connect to FTP server `' . $server . '` on port `' . $port . '` (check address and that server can connect to this address via this port).', 'error' );
  77. return false;
  78. } else {
  79. pb_backupbuddy::status( 'details', 'Connected to FTP.' );
  80. }
  81. } else {
  82. pb_backupbuddy::status( 'details', 'Your web server doesnt support FTP in PHP.', 'error' );
  83. return false;
  84. }
  85. }
  86. // Log in.
  87. $login_result = @ftp_login( $conn_id, $username, $password );
  88. if ( $login_result === false ) {
  89. backupbuddy_core::mail_error( 'ERROR #9011 ( http://ithemes.com/codex/page/BackupBuddy:_Error_Codes#9011 ). FTP/FTPs login failed on scheduled FTP.' );
  90. return false;
  91. } else {
  92. pb_backupbuddy::status( 'details', 'Logged in. Sending backup via FTP/FTPs ...' );
  93. }
  94. if ( $active_mode === true ) {
  95. // do nothing, active is default.
  96. pb_backupbuddy::status( 'details', 'Active FTP mode based on settings.' );
  97. } elseif ( $active_mode === false ) {
  98. // Turn passive mode on.
  99. pb_backupbuddy::status( 'details', 'Passive FTP mode based on settings.' );
  100. ftp_pasv( $conn_id, true );
  101. } else {
  102. pb_backupbuddy::status( 'error', 'Unknown FTP active/passive mode: `' . $active_mode . '`.' );
  103. }
  104. // Create directory if it does not exist.
  105. @ftp_mkdir( $conn_id, $path );
  106. // Change to directory.
  107. pb_backupbuddy::status( 'details', 'Entering FTP directory `' . $path . '`.' );
  108. ftp_chdir( $conn_id, $path );
  109. // Upload files.
  110. $total_transfer_size = 0;
  111. $total_transfer_time = 0;
  112. foreach( $files as $file ) {
  113. if ( ! file_exists( $file ) ) {
  114. pb_backupbuddy::status( 'error', 'Error #859485495. Could not upload local file `' . $file . '` to send to FTP as it does not exist. Verify the file exists, permissions of file, parent directory, and that ownership is correct. You may need suphp installed on the server.' );
  115. }
  116. if ( ! is_readable( $file ) ) {
  117. pb_backupbuddy::status( 'error', 'Error #8594846548. Could not read local file `' . $file . '` to sendto FTP as it is not readable. Verify permissions of file, parent directory, and that ownership is correct. You may need suphp installed on the server.' );
  118. }
  119. $filesize = filesize( $file );
  120. $total_transfer_size += $filesize;
  121. $destination_file = basename( $file ); // Using chdir() so path not needed. $path . '/' . basename( $file );
  122. pb_backupbuddy::status( 'details', 'About to put to FTP local file `' . $file . '` of size `' . pb_backupbuddy::$format->file_size( $filesize ) . '` to remote file `' . $destination_file . '`.' );
  123. $send_time = -microtime( true );
  124. $upload = ftp_put( $conn_id, $destination_file, $file, FTP_BINARY );
  125. $send_time += microtime( true );
  126. $total_transfer_time += $send_time;
  127. if ( $upload === false ) {
  128. $error_message = 'ERROR #9012 ( http://ithemes.com/codex/page/BackupBuddy:_Error_Codes#9012 ). FTP/FTPs file upload failed. Check file permissions & disk quota.';
  129. pb_backupbuddy::status( 'error', $error_message );
  130. backupbuddy_core::mail_error( $error_message );
  131. return false;
  132. } else {
  133. pb_backupbuddy::status( 'details', 'Success completely sending `' . basename( $file ) . '` to destination.' );
  134. // Start remote backup limit
  135. if ( $limit > 0 ) {
  136. pb_backupbuddy::status( 'details', 'Getting contents of backup directory.' );
  137. ftp_chdir( $conn_id, $path );
  138. $contents = ftp_nlist( $conn_id, '' );
  139. // Create array of backups
  140. $bkupprefix = backupbuddy_core::backup_prefix();
  141. $backups = array();
  142. foreach ( $contents as $backup ) {
  143. // check if file is backup
  144. $pos = strpos( $backup, 'backup-' . $bkupprefix . '-' );
  145. if ( $pos !== FALSE ) {
  146. array_push( $backups, $backup );
  147. }
  148. }
  149. arsort( $backups ); // some ftp servers seem to not report in proper order so reversing insufficiently reliable. need to reverse sort by filename. array_reverse( (array)$backups );
  150. if ( ( count( $backups ) ) > $limit ) {
  151. $delete_fail_count = 0;
  152. $i = 0;
  153. foreach( $backups as $backup ) {
  154. $i++;
  155. if ( $i > $limit ) {
  156. if ( !ftp_delete( $conn_id, $backup ) ) {
  157. pb_backupbuddy::status( 'details', 'Unable to delete excess FTP file `' . $backup . '` in path `' . $path . '`.' );
  158. $delete_fail_count++;
  159. }
  160. }
  161. }
  162. if ( $delete_fail_count !== 0 ) {
  163. backupbuddy_core::mail_error( sprintf( __('FTP remote limit could not delete %s backups. Please check and verify file permissions.', 'it-l10n-backupbuddy' ), $delete_fail_count ) );
  164. }
  165. }
  166. } else {
  167. pb_backupbuddy::status( 'details', 'No FTP file limit to enforce.' );
  168. }
  169. // End remote backup limit
  170. }
  171. } // end $files loop.
  172. // Load destination fileoptions.
  173. pb_backupbuddy::status( 'details', 'About to load fileoptions data.' );
  174. require_once( pb_backupbuddy::plugin_path() . '/classes/fileoptions.php' );
  175. pb_backupbuddy::status( 'details', 'Fileoptions instance #13.' );
  176. $fileoptions_obj = new pb_backupbuddy_fileoptions( backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false );
  177. if ( true !== ( $result = $fileoptions_obj->is_ok() ) ) {
  178. pb_backupbuddy::status( 'error', __('Fatal Error #9034.84838. Unable to access fileoptions data.', 'it-l10n-backupbuddy' ) . ' Error: ' . $result );
  179. return false;
  180. }
  181. pb_backupbuddy::status( 'details', 'Fileoptions data loaded.' );
  182. $fileoptions = &$fileoptions_obj->options;
  183. // Save stats.
  184. $fileoptions['write_speed'] = $total_transfer_size / $total_transfer_time;
  185. //$fileoptions['finish_time'] = time();
  186. //$fileoptions['status'] = 'success';
  187. $fileoptions_obj->save();
  188. unset( $fileoptions_obj );
  189. ftp_close( $conn_id );
  190. return true;
  191. } // End send().
  192. /* test()
  193. *
  194. * function description
  195. *
  196. * @param array $settings Destination settings.
  197. * @return bool|string True on success, string error message on failure.
  198. */
  199. public static function test( $settings ) {
  200. if ( ( $settings['address'] == '' ) || ( $settings['username'] == '' ) || ( $settings['password'] == '' ) ) {
  201. return __('Missing required input.', 'it-l10n-backupbuddy' );
  202. }
  203. // Try sending a file.
  204. $send_response = pb_backupbuddy_destinations::send( $settings, dirname( dirname( __FILE__ ) ) . '/remote-send-test.php', $send_id = 'TEST-' . pb_backupbuddy::random_string( 12 ) ); // 3rd param true forces clearing of any current uploads.
  205. if ( false === $send_response ) {
  206. $send_response = 'Error sending test file to FTP.';
  207. } else {
  208. $send_response = 'Success.';
  209. }
  210. // Now we will need to go and cleanup this potentially uploaded file.
  211. $delete_response = 'Error deleting test file from FTP.'; // Default.
  212. // Settings.
  213. $server = $settings['address'];
  214. $username = $settings['username'];
  215. $password = $settings['password'];
  216. $path = $settings['path'];
  217. $ftps = $settings['ftps'];
  218. if ( $settings['active_mode'] == '0' ) {
  219. $active_mode = false;
  220. } else {
  221. $active_mode = true;
  222. }
  223. $url = $settings['url']; // optional url for using with migration.
  224. $port = '21';
  225. if ( strstr( $server, ':' ) ) {
  226. $server_params = explode( ':', $server );
  227. $server = $server_params[0];
  228. $port = $server_params[1];
  229. }
  230. // Connect.
  231. if ( $ftps == '0' ) {
  232. $conn_id = @ftp_connect( $server, $port, 10 ); // timeout of 10 seconds.
  233. if ( $conn_id === false ) {
  234. $error = __( 'Unable to connect to FTP address `' . $server . '` on port `' . $port . '`.', 'it-l10n-backupbuddy' );
  235. $error .= "\n" . __( 'Verify the server address and port (default 21). Verify your host allows outgoing FTP connections.', 'it-l10n-backupbuddy' );
  236. return $send_response . ' ' . $error;
  237. }
  238. } else {
  239. if ( function_exists( 'ftp_ssl_connect' ) ) {
  240. $conn_id = @ftp_ssl_connect( $server, $port );
  241. if ( $conn_id === false ) {
  242. return $send_response . ' ' . __('Destination server does not support FTPS?', 'it-l10n-backupbuddy' );
  243. }
  244. } else {
  245. return $send_response . ' ' . __('Your web server doesnt support FTPS.', 'it-l10n-backupbuddy' );
  246. }
  247. }
  248. $login_result = @ftp_login( $conn_id, $username, $password );
  249. if ( ( !$conn_id ) || ( !$login_result ) ) {
  250. pb_backupbuddy::status( 'details', 'FTP test: Invalid user/pass.' );
  251. $response = __('Unable to login. Bad user/pass.', 'it-l10n-backupbuddy' );
  252. if ( $ftps != '0' ) {
  253. $response .= "\n\nNote: You have FTPs enabled. You may get this error if your host does not support encryption at this address/port.";
  254. }
  255. return $send_response . ' ' . $response;
  256. }
  257. pb_backupbuddy::status( 'details', 'FTP test: Success logging in.' );
  258. // Handle active/pasive mode.
  259. if ( $active_mode === true ) {
  260. // do nothing, active is default.
  261. pb_backupbuddy::status( 'details', 'Active FTP mode based on settings.' );
  262. } elseif ( $active_mode === false ) {
  263. // Turn passive mode on.
  264. pb_backupbuddy::status( 'details', 'Passive FTP mode based on settings.' );
  265. ftp_pasv( $conn_id, true );
  266. } else {
  267. pb_backupbuddy::status( 'error', 'Unknown FTP active/passive mode: `' . $active_mode . '`.' );
  268. }
  269. // Delete test file.
  270. pb_backupbuddy::status( 'details', 'FTP test: Deleting temp test file.' );
  271. if ( true === ftp_delete( $conn_id, $path . '/remote-send-test.php' ) ) {
  272. $delete_response = 'Success.';
  273. }
  274. // Close FTP connection.
  275. pb_backupbuddy::status( 'details', 'FTP test: Closing FTP connection.' );
  276. @ftp_close($conn_id);
  277. // Load destination fileoptions.
  278. pb_backupbuddy::status( 'details', 'About to load fileoptions data.' );
  279. require_once( pb_backupbuddy::plugin_path() . '/classes/fileoptions.php' );
  280. pb_backupbuddy::status( 'details', 'Fileoptions instance #12.' );
  281. $fileoptions_obj = new pb_backupbuddy_fileoptions( backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false );
  282. if ( true !== ( $result = $fileoptions_obj->is_ok() ) ) {
  283. pb_backupbuddy::status( 'error', __('Fatal Error #9034.72373. Unable to access fileoptions data.', 'it-l10n-backupbuddy' ) . ' Error: ' . $result );
  284. return false;
  285. }
  286. pb_backupbuddy::status( 'details', 'Fileoptions data loaded.' );
  287. $fileoptions = &$fileoptions_obj->options;
  288. if ( ( 'Success.' != $send_response ) || ( 'Success.' != $delete_response ) ) {
  289. $fileoptions['status'] = 'failure';
  290. $fileoptions_obj->save();
  291. unset( $fileoptions_obj );
  292. return 'Send details: `' . $send_response . '`. Delete details: `' . $delete_response . '`.';
  293. } else {
  294. $fileoptions['status'] = 'success';
  295. $fileoptions['finish_time'] = microtime(true);
  296. }
  297. $fileoptions_obj->save();
  298. unset( $fileoptions_obj );
  299. return true;
  300. } // End test().
  301. } // End class.