PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backupbuddy/destinations/bootstrap.php

https://bitbucket.org/betaimages/chakalos
PHP | 338 lines | 190 code | 89 blank | 59 comment | 34 complexity | 3da709d11eff8adada82a1495b512cb5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /* Destinations class
  3. *
  4. * Handles everything remote destinations and passes onto individual destination
  5. * class functions.
  6. *
  7. * @author Dustin Bolton
  8. *
  9. */
  10. class pb_backupbuddy_destinations {
  11. private $_destination; // Object containing destination.
  12. private $_settings; // Array of settings for the destination.
  13. private $_destination_type; // Destination type.
  14. // Default destination information.
  15. private static $_destination_info_defaults = array(
  16. 'name' => '{Err_3448}',
  17. 'description' => '{Err_4586. Unknown destination type.}',
  18. );
  19. // boolean false on failure, destination class name on success.
  20. private static function _init_destination( $destination_type ) {
  21. // Load init file.
  22. $destination_init_file = pb_backupbuddy::plugin_path() . '/destinations/' . $destination_type . '/init.php';
  23. if ( file_exists( $destination_init_file ) ) {
  24. require_once( $destination_init_file );
  25. } else {
  26. pb_backupbuddy::status( 'error', 'Destination type `' . $destination_type . '` init.php file not found.' );
  27. return false;
  28. }
  29. // Load class.
  30. $destination_class = 'pb_backupbuddy_destination_' . $destination_type;
  31. if ( !class_exists( $destination_class ) ) {
  32. pb_backupbuddy::status( 'error', 'Destination type `' . $destination_type . '` class not found.' );
  33. return false;
  34. }
  35. if ( method_exists( $destination_class, 'init' ) ) {
  36. call_user_func_array( "{$destination_class}::init", array() ); // Initialize.
  37. }
  38. pb_backupbuddy::status( 'details', 'Initialized `' . $destination_type . '` destination.' );
  39. return $destination_class;
  40. } // End _init_destination().
  41. public static function get_info( $destination_type ) {
  42. // Initialize destination.
  43. $destination_class = self::_init_destination( $destination_type );
  44. if ( !class_exists( $destination_class ) ) {
  45. pb_backupbuddy::alert( 'Unable to load class `' . $destination_class . '` for destination type `' . $destination_type . '`.' );
  46. return self::$_destination_info_defaults;
  47. }
  48. // Get default dest info from class. Was using a variable class name but had to change this for PHP 5.2 compat.
  49. $vars = get_class_vars( $destination_class );
  50. $default_info = $vars['destination_info'];
  51. unset( $vars );
  52. $destination_info = array_merge( self::$_destination_info_defaults, $default_info ); // Merge in defaults from destination over defaults for BB destinations.
  53. return $destination_info;
  54. } // End get_details().
  55. // returns settings form object. false on error.
  56. // mode = add, edit, or save
  57. public static function configure( $destination_settings, $mode ) {
  58. // Initialize destination.
  59. $destination_class = self::_init_destination( $destination_settings['type'] );
  60. if ( !class_exists( $destination_class ) ) {
  61. echo '{Error #546893498a. Destination configuration file missing.}';
  62. return false;
  63. }
  64. // Default settings.
  65. // Get default settings from class. Was using a variable class name but had to change this for PHP 5.2 compat.
  66. $vars = get_class_vars( $destination_class );
  67. $default_settings = $vars['default_settings'];
  68. unset( $vars );
  69. $destination_settings = array_merge( $default_settings, $destination_settings ); // Merge in defaults.
  70. // Get default info from class. Was using a variable class name but had to change this for PHP 5.2 compat.
  71. $vars = get_class_vars( $destination_class );
  72. $default_info = $vars['destination_info'];
  73. unset( $vars );
  74. $destination_info = array_merge( self::$_destination_info_defaults, $default_info ); // Merge in defaults.
  75. $settings_form = new pb_backupbuddy_settings( 'settings', $destination_settings, '' );
  76. $settings_form->add_setting( array(
  77. 'type' => 'hidden',
  78. 'name' => 'type',
  79. 'value' => $destination_settings['type'],
  80. /*
  81. 'title' => __( 'Destination name', 'it-l10n-backupbuddy' ),
  82. 'tip' => __( 'Name of the new destination to create. This is for your convenience only.', 'it-l10n-backupbuddy' ),
  83. 'rules' => 'required|string[0-500]',
  84. */
  85. ) );
  86. $config_file = pb_backupbuddy::plugin_path() . '/destinations/' . $destination_settings['type'] . '/_configure.php';
  87. if ( file_exists( $config_file ) ) {
  88. require( $config_file );
  89. } else {
  90. echo '{Error #54556543. Missing destination config file `' . $config_file . '`.}';
  91. return false;
  92. }
  93. return $settings_form;
  94. } // End configure().
  95. /* send()
  96. *
  97. * function description
  98. *
  99. * @param array Array of settings to pass to destination.
  100. * @param array Array of files to send (full path).
  101. * @return boolean|array true success, false on failure, array for multipart send information (transfer is being chunked up into parts).
  102. */
  103. public function send( $destination_settings, $files ) {
  104. // Register PHP shutdown function to help catch and log fatal PHP errors during backup.
  105. register_shutdown_function( 'pb_backupbuddy_destinations::shutdown_function' );
  106. // Initialize destination.
  107. $destination_class = self::_init_destination( $destination_settings['type'] );
  108. // Get default settings from class. Was using a variable class name but had to change this for PHP 5.2 compat.
  109. $vars = get_class_vars( $destination_class );
  110. $default_settings = $vars['default_settings'];
  111. unset( $vars );
  112. $destination_settings = array_merge( $default_settings, $destination_settings ); // Merge in defaults.
  113. if ( !is_array( $files ) ) {
  114. $files = array( $files );
  115. }
  116. $files_with_sizes = '';
  117. foreach( $files as $file ) {
  118. if ( ! file_exists( $file ) ) {
  119. pb_backupbuddy::status( 'error', 'Error #58459458743. The file that was attempted to be sent to a remote destination, `' . $file . '`, was not found. It either does not exist or permissions prevent accessing it.' );
  120. return false;
  121. }
  122. $files_with_sizes .= $file .' (' . pb_backupbuddy::$format->file_size( filesize( $file ) ) . '); ';
  123. }
  124. pb_backupbuddy::status( 'details', 'Sending files `' . $files_with_sizes . '` to destination type `' . $destination_settings['type'] . '` titled `' . $destination_settings['title'] . '`.' );
  125. unset( $files_with_sizes );
  126. pb_backupbuddy::status( 'details', 'Calling send function.' );
  127. //$result = $destination_class::send( $destination_settings, $files );
  128. global $pb_backupbuddy_destination_errors;
  129. $pb_backupbuddy_destination_errors = array();
  130. $result = call_user_func_array( "{$destination_class}::send", array( $destination_settings, $files ) );
  131. if ( $result === false ) {
  132. $error_details = implode( '; ', $pb_backupbuddy_destination_errors );
  133. pb_backupbuddy::$classes['core']->mail_error( 'There was an error sending to the remote destination. One or more files may have not been fully transferred. Please see error details for additional information. If the error persists, enable full error logging and try again for full details and troubleshooting. Details: ' . "\n\n" . $error_details );
  134. }
  135. if ( is_array( $result ) ) { // Send is multipart.
  136. pb_backupbuddy::status( 'details', 'Completed send function. This file will be sent in multipart chunks. Result: `' . implode( '; ', $result ) . '`.' );
  137. } else { // Single all-at-once send.
  138. pb_backupbuddy::status( 'details', 'Completed send function. Result: `' . $result . '`.' );
  139. }
  140. return $result;
  141. } // End send().
  142. // return true on success, else error message.
  143. public function test( $destination_settings ) {
  144. // Initialize destination.
  145. $destination_class = self::_init_destination( $destination_settings['type'] );
  146. if ( !class_exists( $destination_class ) ) {
  147. echo '{Error #546893498b. Destination configuration file missing.}';
  148. return false;
  149. }
  150. // Get default settings from class. Was using a variable class name but had to change this for PHP 5.2 compat.
  151. $vars = get_class_vars( $destination_class );
  152. $default_settings = $vars['default_settings'];
  153. unset( $vars );
  154. $destination_settings = array_merge( $default_settings, $destination_settings ); // Merge in defaults.
  155. // test() returns true on success, else error message.
  156. return call_user_func_array( "{$destination_class}::test", array( $destination_settings ) );
  157. } // End test().
  158. /* shutdown_function()
  159. *
  160. * Used for catching fatal PHP errors during backup to write to log for debugging.
  161. *
  162. * @return null
  163. */
  164. public static function shutdown_function() {
  165. //error_log ('shutdown_function()');
  166. // Get error message.
  167. // Error types: http://php.net/manual/en/errorfunc.constants.php
  168. $e = error_get_last();
  169. if ( $e === NULL ) { // No error of any kind.
  170. return;
  171. } else { // Some type of error.
  172. if ( !is_array( $e ) || ( $e['type'] != E_ERROR ) && ( $e['type'] != E_USER_ERROR ) ) { // Return if not a fatal error.
  173. return;
  174. }
  175. }
  176. // Calculate log directory.
  177. if ( defined( 'PB_STANDALONE' ) && PB_STANDALONE === true ) {
  178. $log_directory = ABSPATH . 'importbuddy/';
  179. } else {
  180. $log_directory = WP_CONTENT_DIR . '/uploads/pb_backupbuddy/';
  181. }
  182. $main_file = $log_directory . 'log-' . pb_backupbuddy::$options['log_serial'] . '.txt';
  183. // Determine if writing to a serial log.
  184. if ( pb_backupbuddy::$_status_serial != '' ) {
  185. $serial = pb_backupbuddy::$_status_serial;
  186. $serial_file = $log_directory . 'status-' . $serial . '_' . pb_backupbuddy::$options['log_serial'] . '.txt';
  187. $write_serial = true;
  188. } else {
  189. $write_serial = false;
  190. }
  191. // Format error message.
  192. $e_string = "---\n" . __( 'Fatal PHP error encountered:', 'it-l10n-backupbuddy' ) . "\n";
  193. foreach( (array)$e as $e_line_title => $e_line ) {
  194. $e_string .= $e_line_title . ' => ' . $e_line . "\n";
  195. }
  196. $e_string .= "---\n";
  197. // Write to log.
  198. file_put_contents( $main_file, $e_string, FILE_APPEND );
  199. if ( $write_serial === true ) {
  200. @file_put_contents( $serial_file, $e_string, FILE_APPEND );
  201. }
  202. } // End shutdown_function.
  203. public function get_destinations_list() {
  204. $destinations_root = dirname( __FILE__ ) . '/';
  205. $destination_dirs = glob( $destinations_root . '*', GLOB_ONLYDIR );
  206. if ( !is_array( $destination_dirs ) ) {
  207. $destination_dirs = array();
  208. }
  209. $destination_list = array();
  210. foreach( $destination_dirs as $destination_dir ) {
  211. $destination_dir = str_replace( $destinations_root, '', $destination_dir );
  212. $destination_list[$destination_dir] = self::get_info( $destination_dir );
  213. }
  214. // Change some ordering.
  215. $stash_destination = array( 'stash' => $destination_list['stash'] );
  216. unset( $destination_list['stash'] );
  217. $s3_destination = array( 's3' => $destination_list['s3'] );
  218. unset( $destination_list['s3'] );
  219. $destination_list = array_merge( $stash_destination, $s3_destination, $destination_list );
  220. return $destination_list;
  221. } // End get_destinations().
  222. // Handles removing destination from schedules also.
  223. // True on success, else error message.
  224. public static function delete_destination( $destination_id, $confirm = false ) {
  225. if ( $confirm === false ) {
  226. return 'Error #54858597. Not deleted. Confirmation parameter missing.';
  227. }
  228. // Delete destination.
  229. unset( pb_backupbuddy::$options['remote_destinations'][$destination_id] );
  230. // Remove this destination from all schedules using it.
  231. foreach( pb_backupbuddy::$options['schedules'] as $schedule_id => $schedule ) {
  232. $remote_list = '';
  233. $trimmed_destination = false;
  234. $remote_destinations = explode( '|', $schedule['remote_destinations'] );
  235. foreach( $remote_destinations as $remote_destination ) {
  236. if ( $remote_destination == $destination_id ) {
  237. $trimmed_destination = true;
  238. } else {
  239. $remote_list .= $remote_destination . '|';
  240. }
  241. }
  242. if ( $trimmed_destination === true ) {
  243. pb_backupbuddy::$options['schedules'][$schedule_id]['remote_destinations'] = $remote_list;
  244. }
  245. } // end foreach.
  246. pb_backupbuddy::save();
  247. return true;
  248. } // End delete_destination().
  249. }