PageRenderTime 253ms CodeModel.GetById 21ms RepoModel.GetById 16ms app.codeStats 1ms

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

https://bitbucket.org/mrmustarde/manhattan-beach
PHP | 457 lines | 216 code | 125 blank | 116 comment | 85 complexity | 2353d2066afe7aaa0906a88904154d6a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Setup the plugin defaults on activation
  4. */
  5. function hmbkp_activate() {
  6. // Run deactivate on activation in-case it was deactivated manually
  7. hmbkp_deactivate();
  8. }
  9. /**
  10. * Cleanup on plugin deactivation
  11. *
  12. * Removes options and clears all cron schedules
  13. */
  14. function hmbkp_deactivate() {
  15. // Clean up the backups directory
  16. hmbkp_cleanup();
  17. // Remove the plugin data cache
  18. delete_transient( 'hmbkp_plugin_data' );
  19. $schedules = new HMBKP_Schedules;
  20. // Clear schedule crons
  21. foreach ( $schedules->get_schedules() as $schedule )
  22. $schedule->unschedule();
  23. }
  24. /**
  25. * Handles anything that needs to be
  26. * done when the plugin is updated
  27. */
  28. function hmbkp_update() {
  29. // Update from backUpWordPress 0.4.5
  30. if ( get_option( 'bkpwp_max_backups' ) ) {
  31. // Carry over the custom path
  32. if ( $legacy_path = get_option( 'bkpwppath' ) )
  33. update_option( 'hmbkp_path', $legacy_path );
  34. // Options to remove
  35. $legacy_options = array(
  36. 'bkpwp_archive_types',
  37. 'bkpwp_automail_from',
  38. 'bkpwp_domain',
  39. 'bkpwp_domain_path',
  40. 'bkpwp_easy_mode',
  41. 'bkpwp_excludelists',
  42. 'bkpwp_install_user',
  43. 'bkpwp_listmax_backups',
  44. 'bkpwp_max_backups',
  45. 'bkpwp_presets',
  46. 'bkpwp_reccurrences',
  47. 'bkpwp_schedules',
  48. 'bkpwp_calculation',
  49. 'bkpwppath',
  50. 'bkpwp_status_config',
  51. 'bkpwp_status'
  52. );
  53. foreach ( $legacy_options as $option )
  54. delete_option( $option );
  55. global $wp_roles;
  56. $wp_roles->remove_cap( 'administrator','manage_backups' );
  57. $wp_roles->remove_cap( 'administrator','download_backups' );
  58. wp_clear_scheduled_hook( 'bkpwp_schedule_bkpwp_hook' );
  59. }
  60. // Version 1 to 2
  61. if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '2.0' , get_option( 'hmbkp_plugin_version' ), '>' ) ) {
  62. /**
  63. * Setup a backwards compatible schedule
  64. */
  65. $legacy_schedule = new HMBKP_Scheduled_Backup( 'backup' );
  66. // Backup type
  67. if ( ( defined( 'HMBKP_FILES_ONLY' ) && HMBKP_FILES_ONLY ) || get_option( 'hmbkp_files_only' ) )
  68. $legacy_schedule->set_type( 'file' );
  69. elseif ( ( defined( 'HMBKP_DATABASE_ONLY' ) && HMBKP_DATABASE_ONLY ) || get_option( 'hmbkp_database_only' ) )
  70. $legacy_schedule->set_type( 'database' );
  71. else
  72. $legacy_schedule->set_type( 'complete' );
  73. // Daily schedule time
  74. if ( defined( 'HMBKP_DAILY_SCHEDULE_TIME' ) && HMBKP_DAILY_SCHEDULE_TIME )
  75. $legacy_schedule->set_schedule_start_time( strtotime( HMBKP_DAILY_SCHEDULE_TIME ) );
  76. // Backup schedule
  77. $legacy_schedule->set_reoccurrence( str_replace( 'hmbkp_', '', get_option( 'hmbkp_schedule_frequency', 'daily' ) ) );
  78. // Automatic backups disabled?
  79. if ( ( defined( 'HMBKP_DISABLE_AUTOMATIC_BACKUP' ) && HMBKP_DISABLE_AUTOMATIC_BACKUP ) || get_option( 'hmbkp_disable_automatic_backup' ) )
  80. $legacy_schedule->set_reoccurrence( 'manually' );
  81. // Max backups
  82. if ( defined( 'HMBKP_MAX_BACKUPS' ) && is_numeric( HMBKP_MAX_BACKUPS ) )
  83. $legacy_schedule->set_max_backups( (int) HMBKP_MAX_BACKUPS );
  84. else
  85. $legacy_schedule->set_max_backups( (int) get_option( 'hmbkp_max_backups', 10 ) );
  86. // Excludes
  87. if ( get_option( 'hmbkp_excludes' ) )
  88. $legacy_schedule->set_excludes( get_option( 'hmbkp_excludes' ) );
  89. // Backup email
  90. if ( defined( 'HMBKP_EMAIL' ) && is_email( HMBKP_EMAIL ) )
  91. $legacy_schedule->set_service_options( 'HMBKP_Email_Service', array( 'email' => HMBKP_EMAIL ) );
  92. elseif ( is_email( get_option( 'hmbkp_email_address' ) ) )
  93. $legacy_schedule->set_service_options( 'HMBKP_Email_Service', array( 'email' => get_option( 'hmbkp_email_address' ) ) );
  94. // Set the archive filename to what it used to be
  95. $legacy_schedule->set_archive_filename( implode( '-', array( get_bloginfo( 'name' ), 'backup', date( 'Y-m-d-H-i-s', current_time( 'timestamp' ) ) ) ) . '.zip' );
  96. $legacy_schedule->save();
  97. // Remove the legacy options
  98. foreach ( array( 'hmbkp_database_only', 'hmbkp_files_only', 'hmbkp_max_backups', 'hmbkp_email_address', 'hmbkp_email', 'hmbkp_schedule_frequency', 'hmbkp_disable_automatic_backup' ) as $option_name )
  99. delete_option( $option_name );
  100. }
  101. // Every update
  102. if ( get_option( 'hmbkp_plugin_version' ) && version_compare( HMBKP_VERSION, get_option( 'hmbkp_plugin_version' ), '>' ) ) {
  103. hmbkp_deactivate();
  104. // Force .htaccess to be re-written
  105. if ( file_exists( hmbkp_path() . '/.htaccess' ) )
  106. unlink( hmbkp_path() . '/.htaccess' );
  107. // Force index.html to be re-written
  108. if ( file_exists( hmbkp_path() . '/index.html' ) )
  109. unlink( hmbkp_path() . '/index.html' );
  110. }
  111. // Update the stored version
  112. if ( get_option( 'hmbkp_plugin_version' ) !== HMBKP_VERSION )
  113. update_option( 'hmbkp_plugin_version', HMBKP_VERSION );
  114. }
  115. /**
  116. * Setup the default backup schedules
  117. */
  118. function hmbkp_setup_default_schedules() {
  119. $schedules = new HMBKP_Schedules;
  120. if ( $schedules->get_schedules() )
  121. return;
  122. /**
  123. * Schedule a database backup daily and store backups
  124. * for the last 2 weeks
  125. */
  126. $database_daily = new HMBKP_Scheduled_Backup( 'default-1' );
  127. $database_daily->set_type( 'database' );
  128. $database_daily->set_reoccurrence( 'daily' );
  129. $database_daily->set_max_backups( 14 );
  130. $database_daily->save();
  131. /**
  132. * Schedule a complete backup to run weekly and store backups for
  133. * the last 3 months
  134. */
  135. $complete_weekly = new HMBKP_Scheduled_Backup( 'default-2' );
  136. $complete_weekly->set_type( 'complete' );
  137. $complete_weekly->set_reoccurrence( 'weekly' );
  138. $complete_weekly->set_max_backups( 12 );
  139. $complete_weekly->save();
  140. function hmbkp_default_schedules_setup_warning() {
  141. echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has setup your default schedules.', 'hmbkp' ) . '</strong> ' . __( 'By default BackUpWordPress performs a daily backup of your database and a weekly backup of your database &amp; files. You can modify these schedules.', 'hmbkp' ) . '</p></div>';
  142. }
  143. add_action( 'admin_notices', 'hmbkp_default_schedules_setup_warning' );
  144. }
  145. add_action( 'admin_init', 'hmbkp_setup_default_schedules' );
  146. /**
  147. * Return an array of cron schedules
  148. *
  149. * @return array $reccurrences
  150. */
  151. function hmbkp_cron_schedules() {
  152. return array(
  153. 'hourly' => array( 'interval' => 3600, 'display' => __( 'Once Hourly' ) ),
  154. 'twicedaily' => array( 'interval' => 43200, 'display' => __( 'Twice Daily' ) ),
  155. 'daily' => array( 'interval' => 86400, 'display' => __( 'Once Daily' ) ),
  156. 'weekly' => array( 'interval' => 604800, 'display' => __( 'Once Weekly', 'hmbkp' ) ),
  157. 'fortnightly' => array( 'interval' => 1209600, 'display' => __( 'Once Fortnightly', 'hmbkp' ) ),
  158. 'monthly' => array( 'interval' => 2629743.83, 'display' => __( 'Once Monthly', 'hmbkp' ) )
  159. );
  160. }
  161. add_filter( 'cron_schedules', 'hmbkp_cron_schedules' );
  162. /**
  163. * Recursively delete a directory including
  164. * all the files and sub-directories.
  165. *
  166. * @param string $dir
  167. */
  168. function hmbkp_rmdirtree( $dir ) {
  169. if ( strpos( HM_Backup::get_home_path(), $dir ) !== false )
  170. throw new Exception( 'You can only delete directories inside your WordPress installation' );
  171. if ( is_file( $dir ) )
  172. @unlink( $dir );
  173. if ( ! is_dir( $dir ) )
  174. return false;
  175. $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ), RecursiveIteratorIterator::CHILD_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD );
  176. foreach ( $files as $file ) {
  177. if ( $file->isDir() )
  178. @rmdir( $file->getPathname() );
  179. else
  180. @unlink( $file->getPathname() );
  181. }
  182. @rmdir( $dir );
  183. }
  184. /**
  185. * Get the path to the backups directory
  186. *
  187. * Will try to create it if it doesn't exist
  188. * and will fallback to default if a custom dir
  189. * isn't writable.
  190. */
  191. function hmbkp_path() {
  192. global $is_apache;
  193. $path = get_option( 'hmbkp_path' );
  194. // Allow the backups path to be defined
  195. if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH )
  196. $path = HMBKP_PATH;
  197. // If the dir doesn't exist or isn't writable then use the default path instead instead
  198. if ( ( ! $path || ( is_dir( $path ) && ! is_writable( $path ) ) || ( ! is_dir( $path ) && ! is_writable( dirname( $path ) ) ) ) && get_option( 'hmbkp_path' ) !== get_option( 'hmbkp_default_path' ) )
  199. $path = hmbkp_path_default();
  200. // Create the backups directory if it doesn't exist
  201. if ( ! is_dir( $path ) && is_writable( dirname( $path ) ) )
  202. mkdir( $path, 0755 );
  203. // If the path has changed then cache it
  204. if ( get_option( 'hmbkp_path' ) !== $path )
  205. update_option( 'hmbkp_path', $path );
  206. // Protect against directory browsing by including a index.html file
  207. $index = $path . '/index.html';
  208. if ( ! file_exists( $index ) && is_writable( $path ) )
  209. file_put_contents( $index, '' );
  210. $htaccess = $path . '/.htaccess';
  211. // Protect the directory with a .htaccess file on Apache servers
  212. if ( $is_apache && function_exists( 'insert_with_markers' ) && ! file_exists( $htaccess ) && is_writable( $path ) ) {
  213. $contents[] = '# ' . sprintf( __( 'This %s file ensures that other people cannot download your backup files.', 'hmbkp' ), '.htaccess' );
  214. $contents[] = '';
  215. $contents[] = '<IfModule mod_rewrite.c>';
  216. $contents[] = 'RewriteEngine On';
  217. $contents[] = 'RewriteCond %{QUERY_STRING} !key=' . HMBKP_SECURE_KEY;
  218. $contents[] = 'RewriteRule (.*) - [F]';
  219. $contents[] = '</IfModule>';
  220. $contents[] = '';
  221. insert_with_markers( $htaccess, 'BackUpWordPress', $contents );
  222. }
  223. return HM_Backup::conform_dir( $path );
  224. }
  225. /**
  226. * Return the default backup path
  227. *
  228. * @return string path
  229. */
  230. function hmbkp_path_default() {
  231. $path = get_option( 'hmbkp_default_path' );
  232. if ( empty( $path ) ) {
  233. $path = HM_Backup::conform_dir( trailingslashit( WP_CONTENT_DIR ) . substr( HMBKP_SECURE_KEY, 0, 10 ) . '-backups' );
  234. update_option( 'hmbkp_default_path', $path );
  235. }
  236. $upload_dir = wp_upload_dir();
  237. // If the backups dir can't be created in WP_CONTENT_DIR then fallback to uploads
  238. if ( ( ( ! is_dir( $path ) && ! is_writable( dirname( $path ) ) ) || ( is_dir( $path ) && ! is_writable( $path ) ) ) && strpos( $path, $upload_dir['basedir'] ) === false ) {
  239. hmbkp_path_move( $path, $path = HM_Backup::conform_dir( trailingslashit( $upload_dir['basedir'] ) . substr( HMBKP_SECURE_KEY, 0, 10 ) . '-backups' ) );
  240. update_option( 'hmbkp_default_path', $path );
  241. }
  242. return $path;
  243. }
  244. /**
  245. * Move the backup directory and all existing backup files to a new
  246. * location
  247. *
  248. * @param string $from path to move the backups dir from
  249. * @param string $to path to move the backups dir to
  250. * @return void
  251. */
  252. function hmbkp_path_move( $from, $to ) {
  253. if ( ! trim( untrailingslashit( trim( $from ) ) ) || ! trim( untrailingslashit( trim( $to ) ) ) )
  254. return;
  255. // Create the new directory if it doesn't exist
  256. if ( is_writable( dirname( $to ) ) && ! is_dir( $to ) )
  257. mkdir( $to, 0755 );
  258. // Bail if we couldn't
  259. if ( ! is_dir( $to ) || ! is_writable( $to ) )
  260. return false;
  261. update_option( 'hmbkp_path', $to );
  262. // Bail if the old directory doesn't exist
  263. if ( ! is_dir( $from ) )
  264. return false;
  265. // Cleanup before we start moving things
  266. hmbkp_cleanup();
  267. // Move any existing backups
  268. if ( $handle = opendir( $from ) ) {
  269. while ( false !== ( $file = readdir( $handle ) ) )
  270. if ( $file !== '.' && $file !== '..' )
  271. if ( ! @rename( trailingslashit( $from ) . $file, trailingslashit( $to ) . $file ) )
  272. copy( trailingslashit( $from ) . $file, trailingslashit( $to ) . $file );
  273. closedir( $handle );
  274. }
  275. hmbkp_rmdirtree( $from );
  276. }
  277. /**
  278. * Check if a backup is possible with regards to file
  279. * permissions etc.
  280. *
  281. * @return bool
  282. */
  283. function hmbkp_possible() {
  284. if ( ! is_writable( hmbkp_path() ) || ! is_dir( hmbkp_path() ) )
  285. return false;
  286. return true;
  287. }
  288. /**
  289. * Remove any non backup.zip files from the backups dir.
  290. *
  291. * @return void
  292. */
  293. function hmbkp_cleanup() {
  294. $hmbkp_path = hmbkp_path();
  295. if ( ! is_dir( $hmbkp_path ) )
  296. return;
  297. if ( $handle = opendir( $hmbkp_path ) ) :
  298. while ( false !== ( $file = readdir( $handle ) ) )
  299. if ( ! in_array( $file, array( '.', '..', 'index.html' ) ) && pathinfo( $file, PATHINFO_EXTENSION ) !== 'zip' )
  300. hmbkp_rmdirtree( trailingslashit( $hmbkp_path ) . $file );
  301. closedir( $handle );
  302. endif;
  303. }
  304. /**
  305. * Handles changes in the defined Constants
  306. * that users can define to control advanced
  307. * settings
  308. */
  309. function hmbkp_constant_changes() {
  310. // If a custom backup path has been set or changed
  311. if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH && HM_Backup::conform_dir( HMBKP_PATH ) !== ( $from = HM_Backup::conform_dir( get_option( 'hmbkp_path' ) ) ) )
  312. hmbkp_path_move( $from, HMBKP_PATH );
  313. // If a custom backup path has been removed
  314. if ( ( ( defined( 'HMBKP_PATH' ) && ! HMBKP_PATH ) || ! defined( 'HMBKP_PATH' ) && hmbkp_path_default() !== ( $from = HM_Backup::conform_dir( get_option( 'hmbkp_path' ) ) ) ) )
  315. hmbkp_path_move( $from, hmbkp_path_default() );
  316. // If the custom path has changed and the new directory isn't writable
  317. if ( defined( 'HMBKP_PATH' ) && HMBKP_PATH && ! is_writable( HMBKP_PATH ) && get_option( 'hmbkp_path' ) === HMBKP_PATH && is_dir( HMBKP_PATH ) )
  318. hmbkp_path_move( HMBKP_PATH, hmbkp_path_default() );
  319. }
  320. /**
  321. * Get the max email attachment filesize
  322. *
  323. * Can be overridden by defining HMBKP_ATTACHMENT_MAX_FILESIZE
  324. *
  325. * return int the filesize
  326. */
  327. function hmbkp_get_max_attachment_size() {
  328. $max_size = '10mb';
  329. if ( defined( 'HMBKP_ATTACHMENT_MAX_FILESIZE' ) && wp_convert_hr_to_bytes( HMBKP_ATTACHMENT_MAX_FILESIZE ) )
  330. $max_size = HMBKP_ATTACHMENT_MAX_FILESIZE;
  331. return wp_convert_hr_to_bytes( $max_size );
  332. }