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

/htdocs/wp-content/plugins/backupbuddy/destinations/sftp/_manage.php

https://github.com/akosweb/mmgdev
PHP | 158 lines | 116 code | 31 blank | 11 comment | 22 complexity | 8368c8c8fd66e57251d55a5e72ff2dd4 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. // Authored by Dustin Bolton - Summer 2013.
  3. // Incoming variables: $destination
  4. pb_backupbuddy::$ui->title( 'sFTP' );
  5. require_once( pb_backupbuddy::plugin_path() . '/destinations/sftp/init.php' );
  6. pb_backupbuddy_destination_sftp::_init();
  7. // Delete sftp backups
  8. if ( pb_backupbuddy::_POST( 'bulk_action' ) == 'delete_backup' ) {
  9. pb_backupbuddy::verify_nonce();
  10. $delete_count = 0;
  11. // Connect to server.
  12. $server = $destination['address'];
  13. $port = '22'; // Default sFTP port.
  14. if ( strstr( $server, ':' ) ) { // Handle custom sFTP port.
  15. $server_params = explode( ':', $server );
  16. $server = $server_params[0];
  17. $port = $server_params[1];
  18. }
  19. pb_backupbuddy::status( 'details', 'Connecting to sFTP server...' );
  20. $sftp = new Net_SFTP( $server, $port );
  21. if ( ! $sftp->login( $destination['username'], $destination['password'] ) ) {
  22. pb_backupbuddy::status( 'error', 'Connection to sFTP server FAILED.' );
  23. return false;
  24. } else {
  25. pb_backupbuddy::status( 'details', 'Success connecting to sFTP server.' );
  26. }
  27. // Change to directory.
  28. pb_backupbuddy::status( 'details', 'Attempting to change into directory...' );
  29. if ( true === $sftp->chdir( $destination['path'] ) ) {
  30. pb_backupbuddy::status( 'details', 'Changed into directory.' );
  31. } else {
  32. pb_backupbuddy::status( 'error', 'Unable to change into specified path. Verify the path is correct with valid permissions.' );
  33. return false;
  34. }
  35. // loop through and delete ftp backup files
  36. foreach( (array)pb_backupbuddy::_POST( 'items' ) as $backup ) {
  37. // try to delete backup
  38. if ( true === $sftp->delete( $backup ) ) {
  39. $delete_count++;
  40. } else {
  41. pb_backupbuddy::alert( 'Unable to delete file `' . $destination['path'] . '/' . $backup . '`.' );
  42. }
  43. }
  44. if ( $delete_count > 0 ) {
  45. pb_backupbuddy::alert( sprintf( _n( 'Deleted %d file.', 'Deleted %d files.', $delete_count, 'it-l10n-backupbuddy' ), $delete_count ) );
  46. } else {
  47. pb_backupbuddy::alert( __('No backups were deleted.', 'it-l10n-backupbuddy' ) );
  48. }
  49. }
  50. // Connect to server.
  51. $server = $destination['address'];
  52. $port = '22'; // Default sFTP port.
  53. if ( strstr( $server, ':' ) ) { // Handle custom sFTP port.
  54. $server_params = explode( ':', $server );
  55. $server = $server_params[0];
  56. $port = $server_params[1];
  57. }
  58. pb_backupbuddy::status( 'details', 'Connecting to sFTP server...' );
  59. $sftp = new Net_SFTP( $server, $port );
  60. if ( ! $sftp->login( $destination['username'], $destination['password'] ) ) {
  61. pb_backupbuddy::status( 'error', 'Connection to sFTP server FAILED.' );
  62. return false;
  63. } else {
  64. pb_backupbuddy::status( 'details', 'Success connecting to sFTP server.' );
  65. }
  66. pb_backupbuddy::status( 'details', 'Attempting to create path (if it does not exist)...' );
  67. if ( true === $sftp->mkdir( $destination['path'] ) ) { // Try to make directory.
  68. pb_backupbuddy::status( 'details', 'Directory created.' );
  69. } else {
  70. pb_backupbuddy::status( 'details', 'Directory not created.' );
  71. }
  72. // List files.
  73. $files = $sftp->rawlist( $destination['path'] );
  74. $backups = array();
  75. $backup_list_temp = array();
  76. foreach( $files as $filename => $file ) {
  77. if ( false === stristr( $filename, 'backup' ) ) { // only show backup files
  78. continue;
  79. }
  80. if ( stristr( $filename, '-db-' ) !== false ) {
  81. $backup_type = 'Database';
  82. } elseif( stristr( $filename, '-full-' ) !== false ) {
  83. $backup_type = 'Full';
  84. } else {
  85. $backup_type = 'Unknown';
  86. }
  87. $last_modified = $file['mtime'];
  88. while( isset( $backup_list_temp[$last_modified] ) ) { // Avoid collisions.
  89. $last_modified += 0.1;
  90. }
  91. $backup_list_temp[$last_modified] = array(
  92. $filename,
  93. pb_backupbuddy::$format->date(
  94. pb_backupbuddy::$format->localize_time( $file['mtime'] )
  95. ) . '<br /><span class="description">(' .
  96. pb_backupbuddy::$format->time_ago( $file['mtime'] ) .
  97. ' ago)</span>',
  98. pb_backupbuddy::$format->file_size( $file['size'] ),
  99. $backup_type
  100. );
  101. }
  102. krsort( $backup_list_temp );
  103. $backup_list = array();
  104. foreach( $backup_list_temp as $backup_item ) {
  105. $backup_list[ $backup_item[0] ] = $backup_item; //str_replace( 'db/', '', str_replace( 'full/', '', $backup_item ) );
  106. }
  107. unset( $backup_list_temp );
  108. echo '<h3>', __('Viewing', 'it-l10n-backupbuddy' ), ' `' . $destination['title'] . '` (' . $destination['type'] . ')</h3>';
  109. // Render table listing files.
  110. if ( count( $backup_list ) == 0 ) {
  111. echo '<b>';
  112. _e( 'You have not completed sending any backups to this sFTP destination for this site yet.', 'it-l10n-backupbuddy' );
  113. echo '</b>';
  114. } else {
  115. pb_backupbuddy::$ui->list_table(
  116. $backup_list,
  117. array(
  118. 'action' => pb_backupbuddy::page_url() . '&custom=remoteclient&destination_id=' . htmlentities( pb_backupbuddy::_GET( 'destination_id' ) ) . '&remote_path=' . htmlentities( pb_backupbuddy::_GET( 'remote_path' ) ),
  119. 'columns' => array( 'Backup File', 'Uploaded <img src="' . pb_backupbuddy::plugin_url() . '/images/sort_down.png" style="vertical-align: 0px;" title="Sorted most recent first">', 'File Size', 'Type' ),
  120. //'hover_actions' => array( 'copy' => 'Copy to Local' ),
  121. 'hover_action_column_key' => '0',
  122. 'bulk_actions' => array( 'delete_backup' => 'Delete' ),
  123. 'css' => 'width: 100%;',
  124. )
  125. );
  126. }
  127. ?>