PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backupbuddy/destinations/local/init.php

https://bitbucket.org/betaimages/chakalos
PHP | 173 lines | 112 code | 39 blank | 22 comment | 18 complexity | 17bc3d8bdb30484edeacc3ecca8d2e8d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // DO NOTE CALL THIS CLASS DIRECTLY. CALL VIA: pb_backupbuddy_destination in bootstrap.php.
  3. class pb_backupbuddy_destination_local { // Change class name end to match destination name.
  4. public static $destination_info = array(
  5. 'name' => 'Local Directory',
  6. 'description' => 'Send files to another directory on this server / hosting account. This is useful for storing copies locally in another location. This is also a possible destination for automated migrations.',
  7. );
  8. // Default settings. Should be public static for auto-merging.
  9. public static $default_settings = array(
  10. 'type' => 'local', // MUST MATCH your destination slug. Required destination field.
  11. 'title' => '', // Required destination field.
  12. 'path' => '', // Local file path for destination.
  13. 'url' => '', // Corresponding web URL for this location.
  14. 'created_at' => 0,
  15. 'temporary' => false,
  16. 'archive_limit' => '0',
  17. );
  18. /* send()
  19. *
  20. * Send one or more files.
  21. *
  22. * @param array $files Array of one or more files to send.
  23. * @return boolean True on success, else false.
  24. */
  25. public static function send( $settings = array(), $files = array() ) {
  26. $limit = $settings['archive_limit'];
  27. $path = $settings['path'];
  28. if ( !file_exists( $settings['path'] ) ) {
  29. pb_backupbuddy::$filesystem->mkdir( $settings['path'] );
  30. }
  31. foreach( $files as $file ) {
  32. pb_backupbuddy::status( 'details', 'Starting send to `' . $path . '`.' );
  33. if ( true !== @copy( $file, $path . '/' . basename( $file ) ) ) {
  34. pb_backupbuddy::status( 'error', 'Unable to copy file `' . $file . '` to local path `' . $path . '`. Please verify the directory exists and permissions permit writing.' );
  35. pb_backupbuddy::$classes['core']->mail_error( $error_message );
  36. return false;
  37. } else {
  38. pb_backupbuddy::status( 'details', 'Send success.' );
  39. }
  40. // Start remote backup limit
  41. if ( $limit > 0 ) {
  42. pb_backupbuddy::status( 'details', 'Archive limit of `' . $limit . '` in settings.' );
  43. pb_backupbuddy::status( 'details', 'path: ' . $path . '*.zip' );
  44. $remote_files = glob( $path . '/*.zip' );
  45. if ( !is_array( $remote_files ) ) {
  46. $remote_files = array();
  47. }
  48. usort( $remote_files, create_function('$a,$b', 'return filemtime($a) - filemtime($b);' ) );
  49. pb_backupbuddy::status( 'details', 'Found `' . count( $remote_files ) . '` backups.' );
  50. // Create array of backups and organize by date
  51. $bkupprefix = pb_backupbuddy::$classes['core']->backup_prefix();
  52. foreach( $remote_files as $file_key => $remote_file ) {
  53. if ( false === stripos( $remote_file, 'backup-' . $bkupprefix . '-' ) ) {
  54. pb_backupbuddy::status( 'details', 'backup-' . $bkupprefix . '-' . 'not in file: ' . $remote_file );
  55. unset( $backups[$file_key] );
  56. }
  57. }
  58. arsort( $remote_files );
  59. pb_backupbuddy::status( 'details', 'Found `' . count( $remote_files ) . '` backups.' );
  60. if ( ( count( $remote_files ) ) > $limit ) {
  61. pb_backupbuddy::status( 'details', 'More archives (' . count( $remote_files ) . ') than limit (' . $limit . ') allows. Trimming...' );
  62. $i = 0;
  63. $delete_fail_count = 0;
  64. foreach( $remote_files as $remote_file ) {
  65. $i++;
  66. if ( $i > $limit ) {
  67. pb_backupbuddy::status ( 'details', 'Trimming excess file `' . $remote_file . '`...' );
  68. if ( !unlink( $remote_file ) ) {
  69. pb_backupbuddy::status( 'details', 'Unable to delete excess local file `' . $remote_file . '`.' );
  70. $delete_fail_count++;
  71. }
  72. }
  73. }
  74. pb_backupbuddy::status( 'details', 'Finished trimming excess backups.' );
  75. if ( $delete_fail_count !== 0 ) {
  76. $error_message = 'Local remote limit could not delete ' . $delete_fail_count . ' backups.';
  77. pb_backupbuddy::status( 'error', $error_message );
  78. pb_backupbuddy::$classes['core']->mail_error( $error_message );
  79. }
  80. }
  81. } else {
  82. pb_backupbuddy::status( 'details', 'No local destination file limit to enforce.' );
  83. } // End remote backup limit
  84. } // end foreach.
  85. return true;
  86. } // End send().
  87. /* test()
  88. *
  89. * Tests ability to write to this remote destination.
  90. * TODO: Should this delete the temporary test directory to clean up after itself?
  91. *
  92. * @param array $settings Destination settings.
  93. * @return bool|string True on success, string error message on failure.
  94. */
  95. public static function test( $settings, $files = array() ) {
  96. $path = rtrim( $settings['path'], '/\\' );
  97. $url = rtrim( $settings['url'], '/\\' );
  98. if ( !file_exists( $path ) ) {
  99. pb_backupbuddy::$filesystem->mkdir( $path );
  100. }
  101. if ( is_writable( $path ) !== true ) {
  102. return __('Failure', 'it-l10n-backupbuddy' ) . '; The path does not allow writing. Please verify write file permissions.';
  103. }
  104. if ( $url != '' ) {
  105. $test_filename = 'migrate_test_' . pb_backupbuddy::random_string( 10 ) . '.php';
  106. $test_file_path = $path . '/' . $test_filename;
  107. $test_file_url = $url . '/' . $test_filename;
  108. // Make file.
  109. file_put_contents( $test_file_path, "<?php die( '1' ); ?>" );
  110. pb_backupbuddy::status( 'details', 'Local test: Veryifing `' . $test_file_url . '` points to `' . $test_file_path . '`.' );
  111. // Test URL points to file.
  112. $response = wp_remote_get( $test_file_url, array(
  113. 'method' => 'GET',
  114. 'timeout' => 45,
  115. 'redirection' => 5,
  116. 'httpversion' => '1.0',
  117. 'blocking' => true,
  118. 'headers' => array(),
  119. 'body' => null,
  120. 'cookies' => array()
  121. )
  122. );
  123. unlink( $test_file_path );
  124. if ( is_wp_error( $response ) ) {
  125. return __( 'Failure. Unable to connect to the provided URL.', 'it-l10n-backupbuddy' );
  126. }
  127. if ( trim( $response['body'] ) != '1' ) {
  128. return __('Failure. The path appears valid but the URL does not correspond to it. Leave the URL blank if not using this destination for migrations.', 'it-l10n-backupbuddy' );
  129. }
  130. }
  131. // Made it this far so success.
  132. return true;
  133. } // End test().
  134. } // End class.