PageRenderTime 81ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/backupbuddy/destinations/live/live.php

https://gitlab.com/mattswann/launch-housing
PHP | 416 lines | 279 code | 86 blank | 51 comment | 58 complexity | 1fc3d9dbead13911d369553b1a9e7b52 MD5 | raw file
  1. <?php
  2. /* BackupBuddy Stash Live Parent Class
  3. *
  4. * @author Dustin Bolton
  5. * @since 7.0
  6. *
  7. */
  8. class backupbuddy_live {
  9. const STASH_QUOTA_TRANSIENT_NAME = 'backupbuddy_live_stash_quota';
  10. const STASH_QUOTA_TRANSIENT_EXPIRE = 300;
  11. private static $_liveDestinationID = '';
  12. /* getStashQuota()
  13. *
  14. * Retrieves quota information for associated Stash account.
  15. *
  16. */
  17. public static function getStashQuota( $bust_cache = false ) {
  18. if ( ( FALSE === $bust_cache ) && ( FALSE !== ( $quota = get_transient( self::STASH_QUOTA_TRANSIENT_NAME ) ) ) ) {
  19. return $quota;
  20. } else {
  21. require_once( pb_backupbuddy::plugin_path() . '/destinations/live/live_periodic.php' );
  22. require_once( pb_backupbuddy::plugin_path() . '/destinations/stash2/init.php' );
  23. $destination_settings = backupbuddy_live_periodic::get_destination_settings();
  24. $quota = pb_backupbuddy_destination_stash2::get_quota( $destination_settings );
  25. if ( false === $quota ) {
  26. pb_backupbuddy::status( 'error', 'Error #3489348944: Could not get quota for Stash Live.' );
  27. }
  28. set_transient( self::STASH_QUOTA_TRANSIENT_NAME, $quota, self::STASH_QUOTA_TRANSIENT_EXPIRE );
  29. return $quota;
  30. }
  31. }
  32. /* queue_manual_scan()
  33. *
  34. * Queues a directory for file and signature scanning. eg: Used by media upload to look for new files (including thumbnails, etc) for an uploaded image.
  35. *
  36. * @param string $directory Directory to scan. Trailing slash optional. Important: MUST be below/within the ABSPATH or we return false.
  37. */
  38. public static function queue_manual_file_scan( $directory ) {
  39. require_once( 'live_periodic.php' );
  40. // If directory within abspath?
  41. if ( ABSPATH != substr( $directory, 0, strlen( ABSPATH ) ) ) {
  42. pb_backupbuddy::status( 'warning', 'Warning #438943834: Queued filescan directory `' . $directory . '` not found within ABSPATH. Skipping.' );
  43. return false;
  44. }
  45. self::queue_step( 'update_files_list', array( $directory ) );
  46. } // End queue_manual_file_scan().
  47. // $force_use_jump_transient forces use of transient method instead of running now to prevent issues queing from within a periodic function step.
  48. public static function queue_step( $step, $args = array(), $skip_run_now = false, $force_run_now = false ) {
  49. $run_now = false;
  50. $state = backupbuddy_live_periodic::get_stats();
  51. $assume_timed_out_after = backupbuddy_core::adjustedMaxExecutionTime() + backupbuddy_constants::TIMED_OUT_PROCESS_RESUME_WIGGLE_ROOM;
  52. if ( 'daily_init' == $state['step']['function'] ) { // Currently idling so okay to run now.
  53. $run_now = true;
  54. } elseif ( time() - $state['stats']['last_activity'] > $assume_timed_out_after ) { // Probably timed out so okay to run now.
  55. $run_now = true;
  56. }
  57. set_transient( 'backupbuddy_live_jump', array( $step, $args ), 60*60*48 ); // Tells Live process to restart from the beginning (if mid-process) so new settigns apply.
  58. if ( ( true === $force_run_now ) || ( ( true === $run_now ) && ( false === $skip_run_now ) ) ) {
  59. backupbuddy_live_periodic::_set_next_step( $step, $args, $save_now_and_unlock = true );
  60. $schedule_result = backupbuddy_core::schedule_single_event( time(), 'live_periodic', $cronArgs = array() );
  61. if ( true === $schedule_result ) {
  62. pb_backupbuddy::status( 'details', 'Live Periodic chunk step cron event for step `' . $step . '` scheduled.' );
  63. } else {
  64. pb_backupbuddy::status( 'error', 'Next Live Periodic chunk step cron event for step `' . $step . '` FAILED to be scheduled.' );
  65. }
  66. if ( '1' != pb_backupbuddy::$options['skip_spawn_cron_call'] ) {
  67. pb_backupbuddy::status( 'details', 'Spawning cron now.' );
  68. update_option( '_transient_doing_cron', 0 ); // Prevent cron-blocking for next item.
  69. spawn_cron( time() + 150 ); // Adds > 60 seconds to get around once per minute cron running limit.
  70. }
  71. } else { // Already running. Trigger to restart at beginning.
  72. //set_transient( 'backupbuddy_live_jump', array( $step, $args ), 60*60*48 ); // Tells Live process to restart from the beginning (if mid-process) so new settigns apply.
  73. }
  74. } // End queue_step().
  75. public static function update_db_live_activity_time() {
  76. $activity_time_file = backupbuddy_core::getLogDirectory() . 'live/db_activity-' . pb_backupbuddy::$options['log_serial'] . '.txt';
  77. @touch( $activity_time_file );
  78. } // End update_db_live_activity_time().
  79. public static function get_db_live_activity_time() {
  80. $activity_time_file = backupbuddy_core::getLogDirectory() . 'live/db_activity-' . pb_backupbuddy::$options['log_serial'] . '.txt';
  81. if ( ! file_exists( $activity_time_file ) ) {
  82. return -1;
  83. }
  84. if ( false === ( $mtime = @filemtime( $activity_time_file ) ) ) {
  85. return -1;
  86. }
  87. return $mtime;
  88. }
  89. /* calculateTables()
  90. *
  91. * Calculates array of tables Live should back up based on Live additional inclusions/exclusions and global defaults.
  92. *
  93. */
  94. public static function calculateTables() {
  95. $results = self::_calculate_table_includes_excludes_basedump();
  96. // Calculate overall tables which is based on base mode, additional global excludes, additional global includes, and Live-specific excludes.
  97. $tables = backupbuddy_core::calculate_tables( $results[2], $results[0], $results[1] );
  98. return $tables;
  99. } // End calculateTables().
  100. public static function _calculate_table_includes_excludes_basedump() {
  101. $profile = pb_backupbuddy::$options['profiles'][0];
  102. global $wpdb;
  103. $prefix = $wpdb->prefix;
  104. if ( '1' == $profile['backup_nonwp_tables'] ) { // Backup all tables.
  105. $base_dump_mode = 'all';
  106. } elseif ( '2' == $profile['backup_nonwp_tables'] ) { // Backup no tables by default. Relies on listed additional tables.
  107. $base_dump_mode = 'none';
  108. } else { // Only backup matching prefix.
  109. $base_dump_mode = 'prefix';
  110. }
  111. // Calculate Live-specific excludes.
  112. $live_excludes = self::getOption( 'table_excludes', $makeArray = true );
  113. foreach( $live_excludes as &$live_exclude ) {
  114. $live_exclude = str_replace( '{prefix}', $prefix, $live_exclude ); // Populate prefix variable.
  115. }
  116. pb_backupbuddy::status( 'details', 'Live-specific tables to exclude: `' . implode( ', ', $live_excludes ) . '`.' );
  117. // Merge Live-specific excludes with BB global default excludes.
  118. $excludes = array_merge( $live_excludes, backupbuddy_core::get_mysqldump_additional( 'excludes', pb_backupbuddy::$options['profiles'][0] ) );
  119. $includes = backupbuddy_core::get_mysqldump_additional( 'includes', pb_backupbuddy::$options['profiles'][0] );
  120. return array( $includes, $excludes, $base_dump_mode );
  121. } // End _calculate_table_includes_excludes().
  122. /* getLiveDatabaseSnapshotDir()
  123. *
  124. * Has trailing slash.
  125. *
  126. */
  127. public static function getLiveDatabaseSnapshotDir() {
  128. return backupbuddy_core::getTempDirectory() . pb_backupbuddy::$options['log_serial'] . '/live_db_snapshot/';
  129. }
  130. /* getOption()
  131. *
  132. * description
  133. *
  134. */
  135. public static function getOption( $option, $makeArray = false ) {
  136. if ( true !== self::_setLiveID() ) {
  137. if ( true == $makeArray ) {
  138. return array();
  139. } else {
  140. return '';
  141. }
  142. }
  143. if ( ! isset( pb_backupbuddy::$options['remote_destinations'][ self::$_liveDestinationID ][ $option ] ) ) {
  144. if ( true == $makeArray ) {
  145. return array();
  146. } else {
  147. return '';
  148. }
  149. }
  150. $optionValue = pb_backupbuddy::$options['remote_destinations'][ self::$_liveDestinationID ][ $option ];
  151. if ( true === $makeArray ) {
  152. $optionValue = explode( "\n", $optionValue );
  153. $optionValue = array_map( 'trim', $optionValue );
  154. return array_filter( $optionValue ); // Removes empty lines.
  155. } else {
  156. return $optionValue;
  157. }
  158. } // End getOption().
  159. public static function pretty_function( $function ) {
  160. $user_label = '';
  161. if ( ! empty( wp_get_current_user()->user_firstname ) ) {
  162. $user_label = wp_get_current_user()->user_firstname;
  163. } else {
  164. $user_label = wp_get_current_user()->user_login;
  165. }
  166. $functions = array(
  167. 'daily_init' => array(
  168. __( 'Up to date. Watching for changes...', 'it-l10n-backupbuddy' ),
  169. __( 'Up to date. Keeping an eye on that awesome new blog post...', 'it-l10n-backupbuddy' ),
  170. __( 'Up to date. We are all ears, waiting for your next move...', 'it-l10n-backupbuddy' ),
  171. __( 'Up to date. Eating popcorn and keeping movies safe from your Media Gallery...', 'it-l10n-backupbuddy' ),
  172. sprintf( __( 'Up to date. Your move, %s...', 'it-l10n-backupbuddy' ), $user_label )
  173. ),
  174. 'database_snapshot' => __( 'Capturing entire database', 'it-l10n-backupbuddy' ),
  175. 'send_pending_db_snapshots' => __( 'Sending captured database files', 'it-l10n-backupbuddy' ),
  176. 'process_table_deletions' => __( 'Processing deleted tables', 'it-l10n-backupbuddy' ),
  177. 'update_files_list' => __( 'Scanning for new or deleted files', 'it-l10n-backupbuddy' ),
  178. 'update_files_signatures' => __( 'Scanning for file changes', 'it-l10n-backupbuddy' ),
  179. 'process_file_deletions' => __( 'Processing deleted files', 'it-l10n-backupbuddy' ),
  180. 'send_pending_files' => __( 'Sending new & modified files... This may take a while', 'it-l10n-backupbuddy' ),
  181. 'audit_remote_files' => __( 'Auditing backed up files for integrity', 'it-l10n-backupbuddy' ),
  182. 'run_remote_snapshot' => __( 'Creating snapshot', 'it-l10n-backupbuddy' ),
  183. 'wait_on_transfers' => __( 'Waiting for pending file transfers to finish', 'it-l10n-backupbuddy' ),
  184. );
  185. if ( isset( $functions[ $function ] ) ) {
  186. if ( is_array( $functions [ $function ] ) ) {
  187. return $functions[ $function][ array_rand( $functions[ $function ] )];
  188. } else {
  189. return $functions[ $function ];
  190. }
  191. } else {
  192. return __( 'Unknown', 'it-l10n-backupbuddy' );
  193. }
  194. } // End pretty_function().
  195. /* _setLiveID()
  196. *
  197. * description
  198. *
  199. */
  200. private static function _setLiveID() {
  201. if ( '' == self::$_liveDestinationID ) {
  202. foreach( pb_backupbuddy::$options['remote_destinations'] as $destination_id => $destination ) {
  203. if ( 'live' == $destination['type'] ) {
  204. self::$_liveDestinationID = $destination_id;
  205. return true;
  206. }
  207. }
  208. if ( '' == self::$_liveDestinationID ) {
  209. //pb_backupbuddy::status( 'error', 'Warning #382938932: No Live destination was found configured. Set up BackupBuddy Stash Live.' );
  210. return false;
  211. }
  212. }
  213. return true;
  214. } // End _setLiveID().
  215. /* getLiveID()
  216. *
  217. * Returns ID of remote destination or FALSE if not found.
  218. *
  219. */
  220. public static function getLiveID() {
  221. if ( '' == self::$_liveDestinationID ) {
  222. if ( false === self::_setLiveID() ) {
  223. return false;
  224. }
  225. }
  226. return self::$_liveDestinationID;
  227. }
  228. // TODO: $delete param only temporarily needed for server-side transition to new api server based archive trimming.
  229. public static function get_archive_limit_settings_array( $delete = true ) {
  230. $destination_id = backupbuddy_live::getLiveID();
  231. $destination_settings = backupbuddy_live_periodic::get_destination_settings();
  232. $archive_types = array(
  233. 'db',
  234. 'full',
  235. 'plugins',
  236. 'themes',
  237. );
  238. $archive_periods = array(
  239. 'daily',
  240. 'weekly',
  241. 'monthly',
  242. 'yearly',
  243. );
  244. $limits = array();
  245. foreach( $archive_types as $archive_type ) {
  246. $limits[ $archive_type ] = array();
  247. foreach( $archive_periods as $archive_period ) {
  248. if ( '' == $destination_settings[ 'limit_' . $archive_type . '_' . $archive_period ] ) { // For blank values, omit key since it is NOT being limited (unlimited of the type/period combo).
  249. continue;
  250. }
  251. $limits[ $archive_type ][ $archive_period ] = $destination_settings[ 'limit_' . $archive_type . '_' . $archive_period ];
  252. }
  253. }
  254. $return = array(
  255. 'limits' => $limits,
  256. );
  257. if ( true === $delete ) {
  258. $return['delete'] = true; // Whether to actually delete or just dry-run.
  259. }
  260. return $return;
  261. }
  262. public static function send_trim_settings() {
  263. require_once( pb_backupbuddy::plugin_path() . '/destinations/live/live_periodic.php' );
  264. $additionalParams = self::get_archive_limit_settings_array();
  265. $destination_settings = backupbuddy_live_periodic::get_destination_settings();
  266. require_once( pb_backupbuddy::plugin_path() . '/destinations/live/init.php' );
  267. $response = pb_backupbuddy_destination_live::stashAPI( $destination_settings, 'tmtrim-settings', $additionalParams );
  268. if ( ! is_array( $response ) ) {
  269. $error = 'Error #96431277: Error sending settings for trimming archives. Details: `' . $response . '`.';
  270. pb_backupbuddy::status( 'error', $error );
  271. return false;
  272. } else {
  273. pb_backupbuddy::status( 'details', 'Send trimmed remotely stored backup archive settings sent. Results: `' . print_r( $response, true ) . '`.' );
  274. return true;
  275. }
  276. } // End trim_remote_archives().
  277. // Deprecated as of 7.0.5.5 pending verified of new system.
  278. public static function trim_remote_archives( $echo = false ) {
  279. require_once( pb_backupbuddy::plugin_path() . '/destinations/live/live_periodic.php' );
  280. $destination_id = backupbuddy_live::getLiveID();
  281. $destination_settings = backupbuddy_live_periodic::get_destination_settings();
  282. $archive_types = array(
  283. 'db',
  284. 'full',
  285. 'plugins',
  286. 'themes',
  287. );
  288. $archive_periods = array(
  289. 'daily',
  290. 'weekly',
  291. 'monthly',
  292. 'yearly',
  293. );
  294. $limits = array();
  295. foreach( $archive_types as $archive_type ) {
  296. $limits[ $archive_type ] = array();
  297. foreach( $archive_periods as $archive_period ) {
  298. if ( '' == $destination_settings[ 'limit_' . $archive_type . '_' . $archive_period ] ) { // For blank values, omit key since it is NOT being limited (unlimited of the type/period combo).
  299. continue;
  300. }
  301. $limits[ $archive_type ][ $archive_period ] = $destination_settings[ 'limit_' . $archive_type . '_' . $archive_period ];
  302. }
  303. }
  304. $additionalParams = array(
  305. 'delete' => true, // Whether to actually delete or just dry-run.
  306. 'limits' => $limits,
  307. );
  308. require_once( pb_backupbuddy::plugin_path() . '/destinations/live/init.php' );
  309. $response = pb_backupbuddy_destination_live::stashAPI( $destination_settings, 'tmtrim', $additionalParams );
  310. if ( ! is_array( $response ) ) {
  311. $error = 'Error #96431277: Error trimming archives. Details: `' . $response . '`.';
  312. pb_backupbuddy::status( 'error', $error );
  313. if ( true === $echo ) {
  314. echo 'Archive trim error details:<pre>';
  315. print_r( $response );
  316. echo '</pre>';
  317. }
  318. return false;
  319. } else {
  320. pb_backupbuddy::status( 'details', 'Trimmed remotely stored backup archives. Results: `' . print_r( $response, true ) . '`.' );
  321. if ( true === $echo ) {
  322. echo 'NOTE: Type/period combinations where the value is left blank indicate no limiting (unlimited backup storage of this type) and are omitted from being sent in the limit list.<br><br>';
  323. echo 'Archive trim success response:<pre>';
  324. print_r( $response );
  325. echo '</pre>';
  326. }
  327. return true;
  328. }
  329. } // End trim_remote_archives().
  330. } // end class backupbuddy_live.