PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/backupwordpress/functions/interface.php

https://bitbucket.org/mrmustarde/manhattan-beach
PHP | 293 lines | 138 code | 86 blank | 69 comment | 41 complexity | f6215eff60fa66137941c00d2add93d4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Displays a row in the manage backups table
  4. *
  5. * @param string $file
  6. */
  7. function hmbkp_get_backup_row( $file, HMBKP_Scheduled_Backup $schedule ) {
  8. $encoded_file = urlencode( base64_encode( $file ) );
  9. $offset = get_option( 'gmt_offset' ) * 3600; ?>
  10. <tr class="hmbkp_manage_backups_row<?php if ( file_exists( hmbkp_path() . '/.backup_complete' ) ) : ?> completed<?php unlink( hmbkp_path() . '/.backup_complete' ); endif; ?>">
  11. <th scope="row">
  12. <?php esc_html_e( date_i18n( get_option( 'date_format' ) . ' - ' . get_option( 'time_format' ), @filemtime( $file ) + $offset ) ); ?>
  13. </th>
  14. <td class="code">
  15. <?php esc_html_e( size_format( @filesize( $file ) ) ); ?>
  16. </td>
  17. <td><?php esc_html_e( hmbkp_human_get_type( $file, $schedule ) ); ?></td>
  18. <td>
  19. <a href="<?php echo wp_nonce_url( admin_url( 'tools.php?page=' . HMBKP_PLUGIN_SLUG . '&amp;hmbkp_download_backup=' . $encoded_file . '&amp;hmbkp_schedule_id=' . $schedule->get_id() ), 'hmbkp-download_backup' ); ?>"><?php _e( 'Download', 'hmbkp' ); ?></a> |
  20. <a href="<?php echo wp_nonce_url( admin_url( 'tools.php?page=' . HMBKP_PLUGIN_SLUG . '&amp;hmbkp_delete_backup=' . $encoded_file . '&amp;hmbkp_schedule_id=' . $schedule->get_id() ), 'hmbkp-delete_backup' ); ?>" class="delete-action"><?php _e( 'Delete', 'hmbkp' ); ?></a>
  21. </td>
  22. </tr>
  23. <?php }
  24. /**
  25. * Displays admin notices for various error / warning
  26. * conditions
  27. *
  28. * @return void
  29. */
  30. function hmbkp_admin_notices() {
  31. // If the backups directory doesn't exist and can't be automatically created
  32. if ( ! is_dir( hmbkp_path() ) ) :
  33. function hmbkp_path_exists_warning() {
  34. $php_user = exec( 'whoami' );
  35. $php_group = reset( explode( ' ', exec( 'groups' ) ) );
  36. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress is almost ready.', 'hmbkp' ) . '</strong> ' . sprintf( __( 'The backups directory can\'t be created because your %1$s directory isn\'t writable, run %2$s or %3$s or create the folder yourself.', 'hmbkp' ), '<code>wp-content</code>', '<code>chown ' . esc_html( $php_user ) . ':' . esc_html( $php_group ) . ' ' . esc_html( dirname( hmbkp_path() ) ) . '</code>', '<code>chmod 777 ' . esc_html( dirname( hmbkp_path() ) ) . '</code>' ) . '</p></div>';
  37. }
  38. add_action( 'admin_notices', 'hmbkp_path_exists_warning' );
  39. endif;
  40. // If the backups directory exists but isn't writable
  41. if ( is_dir( hmbkp_path() ) && ! is_writable( hmbkp_path() ) ) :
  42. function hmbkp_writable_path_warning() {
  43. $php_user = exec( 'whoami' );
  44. $php_group = reset( explode( ' ', exec( 'groups' ) ) );
  45. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress is almost ready.', 'hmbkp' ) . '</strong> ' . sprintf( __( 'Your backups directory isn\'t writable, run %1$s or %2$s or set the permissions yourself.', 'hmbkp' ), '<code>chown -R ' . esc_html( $php_user ) . ':' . esc_html( $php_group ) . ' ' . esc_html( hmbkp_path() ) . '</code>', '<code>chmod -R 777 ' . esc_html( hmbkp_path() ) . '</code>' ) . '</p></div>';
  46. }
  47. add_action( 'admin_notices', 'hmbkp_writable_path_warning' );
  48. endif;
  49. // If safe mode is active
  50. if ( HM_Backup::is_safe_mode_active() ) :
  51. function hmbkp_safe_mode_warning() {
  52. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has detected a problem.', 'hmbkp' ) . '</strong> ' . sprintf( __( '%1$s is running in %2$s, please contact your host and ask them to disable it. BackUpWordPress may not work correctly whilst %3$s is on.', 'hmbkp' ), '<code>PHP</code>', sprintf( '<a href="%1$s">%2$s</a>', __( 'http://php.net/manual/en/features.safe-mode.php', 'hmbkp' ), __( 'Safe Mode', 'hmbkp' ) ), '<code>' . __( 'Safe Mode', 'hmbkp' ) . '</code>' ) . '</p></div>';
  53. }
  54. add_action( 'admin_notices', 'hmbkp_safe_mode_warning' );
  55. endif;
  56. // If a custom backups directory is defined and it doesn't exist and can't be created
  57. if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH && ! is_dir( HMBKP_PATH ) ) :
  58. function hmbkp_custom_path_exists_warning() {
  59. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has detected a problem.', 'hmbkp' ) . '</strong> ' . sprintf( __( 'Your custom backups directory %1$s doesn\'t exist and can\'t be created, your backups will be saved to %2$s instead.', 'hmbkp' ), '<code>' . esc_html( HMBKP_PATH ) . '</code>', '<code>' . esc_html( hmbkp_path() ) . '</code>' ) . '</p></div>';
  60. }
  61. add_action( 'admin_notices', 'hmbkp_custom_path_exists_warning' );
  62. endif;
  63. // If a custom backups directory is defined and exists but isn't writable
  64. if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH && is_dir( HMBKP_PATH ) && ! is_writable( HMBKP_PATH ) ) :
  65. function hmbkp_custom_path_writable_notice() {
  66. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has detected a problem.', 'hmbkp' ) . '</strong> ' . sprintf( __( 'Your custom backups directory %1$s isn\'t writable, new backups will be saved to %2$s instead.', 'hmbkp' ), '<code>' . esc_html( HMBKP_PATH ) . '</code>', '<code>' . esc_html( hmbkp_path() ) . '</code>' ) . '</p></div>';
  67. }
  68. add_action( 'admin_notices', 'hmbkp_custom_path_writable_notice' );
  69. endif;
  70. // If there are any errors reported in the backup
  71. if ( hmbkp_backup_errors_message() ) :
  72. function hmbkp_backup_errors_notice() {
  73. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress detected issues with your last backup.', 'hmbkp' ) . '</strong><a href="' . add_query_arg( 'action', 'hmbkp_dismiss_error' ) . '" style="float: right;" class="button">Dismiss</a></p>' . hmbkp_backup_errors_message() . '</div>';
  74. }
  75. add_action( 'admin_notices', 'hmbkp_backup_errors_notice' );
  76. endif;
  77. }
  78. add_action( 'admin_head', 'hmbkp_admin_notices' );
  79. /**
  80. * Hook in an change the plugin description when BackUpWordPress is activated
  81. *
  82. * @param array $plugins
  83. * @return $plugins
  84. */
  85. function hmbkp_plugin_row( $plugins ) {
  86. if ( isset( $plugins[HMBKP_PLUGIN_SLUG . '/plugin.php'] ) )
  87. $plugins[HMBKP_PLUGIN_SLUG . '/plugin.php']['Description'] = str_replace( 'Once activated you\'ll find me under <strong>Tools &rarr; Backups</strong>', 'Find me under <strong><a href="' . admin_url( 'tools.php?page=' . HMBKP_PLUGIN_SLUG ) . '">Tools &rarr; Backups</a></strong>', $plugins[HMBKP_PLUGIN_SLUG . '/plugin.php']['Description'] );
  88. return $plugins;
  89. }
  90. add_filter( 'all_plugins', 'hmbkp_plugin_row', 10 );
  91. /**
  92. * Parse the json string of errors and
  93. * output as a human readable message
  94. *
  95. * @access public
  96. * @return null
  97. */
  98. function hmbkp_backup_errors_message() {
  99. $message = '';
  100. foreach ( (array) json_decode( hmbkp_backup_errors() ) as $key => $errors )
  101. foreach ( $errors as $error )
  102. $message .= '<p><strong>' . esc_html( $key ) . '</strong>: <code>' . implode( ':', array_map( 'esc_html', (array) $error ) ) . '</code></p>';
  103. return $message;
  104. }
  105. /**
  106. * Display a html list of files
  107. *
  108. * @param HMBKP_Scheduled_Backup $schedule
  109. * @param mixed $excludes (default: null)
  110. * @param string $file_method (default: 'get_included_files')
  111. * @return void
  112. */
  113. function hmbkp_file_list( HMBKP_Scheduled_Backup $schedule, $excludes = null, $file_method = 'get_included_files' ) {
  114. if ( ! is_null( $excludes ) )
  115. $schedule->set_excludes( $excludes );
  116. $exclude_string = $schedule->exclude_string( 'regex' ); ?>
  117. <ul class="hmbkp_file_list code">
  118. <?php foreach( $schedule->get_files() as $file ) :
  119. if ( ! is_null( $excludes ) && strpos( $file, str_ireplace( $schedule->get_root(), '', $schedule->get_path() ) ) !== false )
  120. continue;
  121. // Skip dot files, they should only exist on versions of PHP between 5.2.11 -> 5.3
  122. if ( method_exists( $file, 'isDot' ) && $file->isDot() )
  123. continue;
  124. // Show only unreadable files
  125. if ( $file_method === 'get_unreadable_files' && @realpath( $file->getPathname() ) && $file->isReadable() )
  126. continue;
  127. // Skip unreadable files
  128. elseif ( $file_method !== 'get_unreadable_files' && ( ! @realpath( $file->getPathname() ) || ! $file->isReadable() ) )
  129. continue;
  130. // Show only included files
  131. if ( $file_method === 'get_included_files' )
  132. if ( $exclude_string && preg_match( '(' . $exclude_string . ')', str_ireplace( trailingslashit( $schedule->get_root() ), '', HM_Backup::conform_dir( $file->getPathname() ) ) ) )
  133. continue;
  134. // Show only excluded files
  135. if ( $file_method === 'get_excluded_files' )
  136. if ( ! $exclude_string || ! preg_match( '(' . $exclude_string . ')', str_ireplace( trailingslashit( $schedule->get_root() ), '', HM_Backup::conform_dir( $file->getPathname() ) ) ) )
  137. continue;
  138. if ( @realpath( $file->getPathname() ) && ! $file->isReadable() && $file->isDir() ) { ?>
  139. <li title="<?php echo esc_attr( HM_Backup::conform_dir( trailingslashit( $file->getPathName() ) ) ); ?>"><?php echo esc_html( ltrim( trailingslashit( str_ireplace( HM_Backup::conform_dir( trailingslashit( $schedule->get_root() ) ), '', HM_Backup::conform_dir( $file->getPathName() ) ) ), '/' ) ); ?></li>
  140. <?php } else { ?>
  141. <li title="<?php echo esc_attr( HM_Backup::conform_dir( $file->getPathName() ) ); ?>"><?php echo esc_html( ltrim( str_ireplace( HM_Backup::conform_dir( trailingslashit( $schedule->get_root() ) ), '', HM_Backup::conform_dir( $file->getPathName() ) ), '/' ) ); ?></li>
  142. <?php }
  143. endforeach; ?>
  144. </ul>
  145. <?php }
  146. /**
  147. * Get the human readable backup type in.
  148. *
  149. * @access public
  150. * @param string $type
  151. * @param HMBKP_Scheduled_Backup $schedule (default: null)
  152. * @return string
  153. */
  154. function hmbkp_human_get_type( $type, HMBKP_Scheduled_Backup $schedule = null ) {
  155. if ( strpos( $type, 'complete' ) !== false )
  156. return __( 'Database and Files', 'hmbkp' );
  157. if ( strpos( $type, 'file' ) !== false )
  158. return __( 'Files', 'hmbkp' );
  159. if ( strpos( $type, 'database' ) !== false )
  160. return __( 'Database', 'hmbkp' );
  161. if ( ! is_null( $schedule ) )
  162. return hmbkp_human_get_type( $schedule->get_type() );
  163. return __( 'Legacy', 'hmbkp' );
  164. }
  165. /**
  166. * Display the row of actions for a schedule
  167. *
  168. * @access public
  169. * @param HMBKP_Scheduled_Backup $schedule
  170. * @return void
  171. */
  172. function hmbkp_schedule_actions( HMBKP_Scheduled_Backup $schedule ) {
  173. // Start output buffering
  174. ob_start(); ?>
  175. <span class="hmbkp-status"><?php echo $schedule->get_status() ? $schedule->get_status() : __( 'Starting Backup', 'hmbkp' ); ?> <a href="<?php echo add_query_arg( array( 'action' => 'hmbkp_cancel', 'hmbkp_schedule_id' => $schedule->get_id() ), HMBKP_ADMIN_URL ); ?>"><?php _e( 'cancel', 'hmbkp' ); ?></a></span>
  176. <div class="hmbkp-schedule-actions row-actions">
  177. <a class="fancybox" href="<?php echo add_query_arg( array( 'action' => 'hmbkp_edit_schedule_load', 'hmbkp_schedule_id' => $schedule->get_id() ), admin_url( 'admin-ajax.php' ) ); ?>"><?php _e( 'Settings', 'hmbkp' ); ?></a> |
  178. <?php if ( $schedule->get_type() !== 'database' ) { ?>
  179. <a class="fancybox" href="<?php echo add_query_arg( array( 'action' => 'hmbkp_edit_schedule_excludes_load', 'hmbkp_schedule_id' => $schedule->get_id() ), admin_url( 'admin-ajax.php' ) ); ?>"><?php _e( 'Excludes', 'hmbkp' ); ?></a> |
  180. <?php } ?>
  181. <a class="hmbkp-run" href="<?php echo add_query_arg( array( 'action' => 'hmbkp_run_schedule', 'hmbkp_schedule_id' => $schedule->get_id() ), admin_url( 'admin-ajax.php' ) ); ?>"><?php _e( 'Run now', 'hmbkp' ); ?></a> |
  182. <a class="delete-action" href="<?php echo wp_nonce_url( add_query_arg( array( 'action' => 'hmbkp_delete_schedule', 'hmbkp_schedule_id' => $schedule->get_id() ), HMBKP_ADMIN_URL ), 'hmbkp-delete_schedule' ); ?>"><?php _e( 'Delete', 'hmbkp' ); ?></a>
  183. <?php // capture output
  184. $output = ob_get_clean();
  185. echo apply_filters( 'hmbkp_schedule_actions_menu', $output, $schedule ); ?>
  186. </div>
  187. <?php }
  188. /**
  189. * Load the backup errors file
  190. *
  191. * @return string
  192. */
  193. function hmbkp_backup_errors() {
  194. if ( ! file_exists( hmbkp_path() . '/.backup_errors' ) )
  195. return '';
  196. return file_get_contents( hmbkp_path() . '/.backup_errors' );
  197. }
  198. /**
  199. * Load the backup warnings file
  200. *
  201. * @return string
  202. */
  203. function hmbkp_backup_warnings() {
  204. if ( ! file_exists( hmbkp_path() . '/.backup_warnings' ) )
  205. return '';
  206. return file_get_contents( hmbkp_path() . '/.backup_warnings' );
  207. }