PageRenderTime 63ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/inc/class-jobtype-file.php

https://github.com/90rdon/bric
PHP | 541 lines | 500 code | 21 blank | 20 comment | 51 complexity | a68078824defd56acd7db349c646ad96 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. */
  5. class BackWPup_JobType_File extends BackWPup_JobTypes {
  6. private $folers_to_backup = array();
  7. /**
  8. *
  9. */
  10. public function __construct() {
  11. $this->info[ 'ID' ] = 'FILE';
  12. $this->info[ 'name' ] = __( 'Files', 'backwpup' );
  13. $this->info[ 'description' ] = __( 'File backup', 'backwpup' );
  14. $this->info[ 'URI' ] = translate( BackWPup::get_plugin_data( 'PluginURI' ), 'backwpup' );
  15. $this->info[ 'author' ] = BackWPup::get_plugin_data( 'Author' );
  16. $this->info[ 'authorURI' ] = translate( BackWPup::get_plugin_data( 'AuthorURI' ), 'backwpup' );
  17. $this->info[ 'version' ] = BackWPup::get_plugin_data( 'Version' );
  18. }
  19. /**
  20. *
  21. */
  22. public function admin_print_scripts() {
  23. if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
  24. wp_enqueue_script( 'backwpupjobtypefile', BackWPup::get_plugin_data( 'URL' ) . '/assets/js/page_edit_jobtype_file.js', array( 'jquery' ), time(), TRUE );
  25. } else {
  26. wp_enqueue_script( 'backwpupjobtypefile', BackWPup::get_plugin_data( 'URL' ) . '/assets/js/page_edit_jobtype_file.min.js', array( 'jquery' ), BackWPup::get_plugin_data( 'Version' ), TRUE );
  27. }
  28. }
  29. /**
  30. * @return bool
  31. */
  32. public function creates_file() {
  33. return TRUE;
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function option_defaults() {
  39. return array(
  40. 'backupexcludethumbs' => FALSE, 'backupspecialfiles' => TRUE,
  41. 'backuproot' => TRUE, 'backupcontent' => TRUE, 'backupplugins' => TRUE, 'backupthemes' => TRUE, 'backupuploads' => TRUE,
  42. 'backuprootexcludedirs' => array(), 'backupcontentexcludedirs' => array( 'cache', 'upgrade', 'w3tc' ), 'backuppluginsexcludedirs' => array( 'backwpup', 'backwpup-pro' ), 'backupthemesexcludedirs' => array(), 'backupuploadsexcludedirs' => array(),
  43. 'fileexclude' => '.tmp,.svn,.git,desktop.ini,.DS_Store', 'dirinclude' => ''
  44. );
  45. }
  46. /**
  47. * @param $main
  48. */
  49. public function edit_tab( $main ) {
  50. @set_time_limit( 300 );
  51. ?>
  52. <h3 class="title"><?php _e( 'Folders to backup', 'backwpup' ) ?></h3>
  53. <p></p>
  54. <table class="form-table">
  55. <tr>
  56. <th scope="row"><label for="idbackuproot"><?php _e( 'Backup root folder', 'backwpup' ); ?></label></th>
  57. <td>
  58. <?php
  59. $folder = realpath( ABSPATH );
  60. if ( $folder ) {
  61. $folder = untrailingslashit( str_replace( '\\', '/', $folder ) );
  62. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder, FALSE ), 2 ) . ')' : '';
  63. }
  64. ?>
  65. <input class="checkbox"
  66. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backuproot' ), TRUE, TRUE );?>
  67. name="backuproot" id="idbackuproot" value="1" /> <code title="<?php echo sprintf( __( 'Path as set by user (symlink?): %s', 'backwpup' ), esc_attr( ABSPATH ) ); ?>"><?php echo esc_attr( $folder ); ?></code><?php echo $folder_size; ?>
  68. <fieldset id="backuprootexcludedirs" style="padding-left:15px; margin:2px;">
  69. <legend><strong><?php _e( 'Exclude:', 'backwpup' ); ?></strong></legend>
  70. <?php
  71. if ( $folder && $dir = @opendir( $folder ) ) {
  72. while ( ( $file = readdir( $dir ) ) !== FALSE ) {
  73. if ( ! in_array( $file, array( '.', '..' ) ) && is_dir( $folder . '/' . $file ) && ! in_array( trailingslashit( $folder . '/' . $file ), $this->get_exclude_dirs( $folder ) ) ) {
  74. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder . '/' . $file ), 2 ) . ')' : '';
  75. echo '<nobr><label for="idrootexcludedirs-'.sanitize_file_name( $file ).'"><input class="checkbox" type="checkbox"' . checked( in_array( $file, BackWPup_Option::get( $main, 'backuprootexcludedirs' ) ), TRUE, FALSE ) . ' name="backuprootexcludedirs[]" id="idrootexcludedirs-'.sanitize_file_name( $file ).'" value="' . $file . '" /> ' . esc_attr( $file ) . $folder_size . '</label><br /></nobr>';
  76. }
  77. }
  78. @closedir( $dir );
  79. }
  80. ?>
  81. </fieldset>
  82. </td>
  83. </tr>
  84. <tr>
  85. <th scope="row"><label for="idbackupcontent"><?php _e( 'Backup content folder', 'backwpup' ); ?></label></th>
  86. <td>
  87. <?php
  88. $folder = realpath( WP_CONTENT_DIR );
  89. if ( $folder ) {
  90. $folder = untrailingslashit( str_replace( '\\', '/', $folder ) );
  91. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder, FALSE ), 2 ) . ')' : '';
  92. }
  93. ?>
  94. <input class="checkbox"
  95. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backupcontent' ), TRUE, TRUE );?>
  96. name="backupcontent" id="idbackupcontent" value="1" /> <code title="<?php echo sprintf( __( 'Path as set by user (symlink?): %s', 'backwpup' ), esc_attr( WP_CONTENT_DIR ) ); ?>"><?php echo esc_attr( $folder ); ?></code><?php echo $folder_size; ?>
  97. <fieldset id="backupcontentexcludedirs" style="padding-left:15px; margin:2px;">
  98. <legend><strong><?php _e( 'Exclude:', 'backwpup' ); ?></strong></legend>
  99. <?php
  100. if ( $folder && $dir = @opendir( $folder ) ) {
  101. while ( ( $file = readdir( $dir ) ) !== FALSE ) {
  102. if ( ! in_array( $file, array( '.', '..' ) ) && is_dir( $folder . '/' . $file ) && ! in_array( trailingslashit( $folder . '/' . $file ), $this->get_exclude_dirs( $folder ) ) ) {
  103. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder . '/' . $file ), 2 ) . ')' : '';
  104. echo '<nobr><label for="idcontentexcludedirs-'.sanitize_file_name( $file ).'"><input class="checkbox" type="checkbox"' . checked( in_array( $file, BackWPup_Option::get( $main, 'backupcontentexcludedirs' ) ), TRUE, FALSE ) . ' name="backupcontentexcludedirs[]" id="idcontentexcludedirs-'.sanitize_file_name( $file ).'" value="' . $file . '" /> ' . esc_attr( $file ) . $folder_size . '</label><br /></nobr>';
  105. }
  106. }
  107. @closedir( $dir );
  108. }
  109. ?>
  110. </fieldset>
  111. </td>
  112. </tr>
  113. <tr>
  114. <th scope="row"><label for="idbackupplugins"><?php _e( 'Backup plugins', 'backwpup' ); ?></label></th>
  115. <td>
  116. <?php
  117. $folder = realpath( WP_PLUGIN_DIR );
  118. if ( $folder ) {
  119. $folder = untrailingslashit( str_replace( '\\', '/', $folder ) );
  120. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder, FALSE ), 2 ) . ')' : '';
  121. }
  122. ?>
  123. <input class="checkbox"
  124. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backupplugins' ), TRUE, TRUE );?>
  125. name="backupplugins" id="idbackupplugins" value="1" /> <code title="<?php echo sprintf( __( 'Path as set by user (symlink?): %s', 'backwpup' ), esc_attr( WP_PLUGIN_DIR ) ); ?>"><?php echo esc_attr( $folder ); ?></code><?php echo $folder_size; ?>
  126. <fieldset id="backuppluginsexcludedirs" style="padding-left:15px; margin:2px;">
  127. <legend><strong><?php _e( 'Exclude:', 'backwpup' ); ?></strong></legend>
  128. <?php
  129. if ( $folder && $dir = @opendir( $folder ) ) {
  130. while ( ( $file = readdir( $dir ) ) !== FALSE ) {
  131. if ( ! in_array( $file, array( '.', '..' ) ) && is_dir( $folder . '/' . $file ) && ! in_array( trailingslashit( $folder . '/' . $file ), $this->get_exclude_dirs( $folder ) ) ) {
  132. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder . '/' . $file ), 2 ) . ')' : '';
  133. echo '<nobr><label for="idpluginexcludedirs-'.sanitize_file_name( $file ).'"><input class="checkbox" type="checkbox"' . checked( in_array( $file, BackWPup_Option::get( $main, 'backuppluginsexcludedirs' ) ), TRUE, FALSE ) . ' name="backuppluginsexcludedirs[]" id="idpluginexcludedirs-'.sanitize_file_name( $file ).'" value="' . $file . '" /> ' . esc_attr( $file ) . $folder_size . '</label><br /></nobr>';
  134. }
  135. }
  136. @closedir( $dir );
  137. }
  138. ?>
  139. </fieldset>
  140. </td>
  141. </tr>
  142. <tr>
  143. <th scope="row"><label for="idbackupthemes"><?php _e( 'Backup themes', 'backwpup' ); ?></label></th>
  144. <td>
  145. <?php
  146. $folder = realpath( get_theme_root() );
  147. if ( $folder ) {
  148. $folder = untrailingslashit( str_replace( '\\', '/', $folder ) );
  149. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder, FALSE ), 2 ) . ')' : '';
  150. }
  151. ?>
  152. <input class="checkbox"
  153. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backupthemes' ), TRUE, TRUE );?>
  154. name="backupthemes" id="idbackupthemes" value="1" /> <code title="<?php echo sprintf( __( 'Path as set by user (symlink?): %s', 'backwpup' ), esc_attr( get_theme_root() ) ); ?>"><?php echo esc_attr( $folder ); ?></code><?php echo $folder_size; ?>
  155. <fieldset id="backupthemesexcludedirs" style="padding-left:15px; margin:2px;">
  156. <legend><strong><?php _e( 'Exclude:', 'backwpup' ); ?></strong></legend>
  157. <?php
  158. if ( $folder && $dir = @opendir( $folder ) ) {
  159. while ( ( $file = readdir( $dir ) ) !== FALSE ) {
  160. if ( ! in_array( $file, array( '.', '..' ) ) && is_dir( $folder . '/' . $file ) && ! in_array( trailingslashit( $folder . '/' . $file ), $this->get_exclude_dirs( $folder ) ) ) {
  161. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder . '/' . $file ), 2 ) . ')' : '';
  162. echo '<nobr><label for="idthemesexcludedirs-'.sanitize_file_name( $file ).'"><input class="checkbox" type="checkbox"' . checked( in_array( $file, BackWPup_Option::get( $main, 'backupthemesexcludedirs' ) ), TRUE, FALSE ) . ' name="backupthemesexcludedirs[]" id="idthemesexcludedirs-'.sanitize_file_name( $file ).'" value="' . $file . '" /> ' . esc_attr( $file ) . $folder_size . '</label><br /></nobr>';
  163. }
  164. }
  165. @closedir( $dir );
  166. }
  167. ?>
  168. </fieldset>
  169. </td>
  170. </tr>
  171. <tr>
  172. <th scope="row"><label for="idbackupuploads"><?php _e( 'Backup uploads folder', 'backwpup' ); ?></label></th>
  173. <td>
  174. <?php
  175. $folder = realpath( BackWPup_File::get_upload_dir() );
  176. if ( $folder ) {
  177. $folder = untrailingslashit( str_replace( '\\', '/', $folder ) );
  178. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder, FALSE ), 2 ) . ')' : '';
  179. }
  180. ?>
  181. <input class="checkbox"
  182. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backupuploads' ), TRUE, TRUE );?>
  183. name="backupuploads" id="idbackupuploads" value="1" /> <code title="<?php echo sprintf( __( 'Path as set by user (symlink?): %s', 'backwpup' ), esc_attr( BackWPup_File::get_upload_dir() ) ); ?>"><?php echo esc_html( $folder ); ?></code><?php echo $folder_size; ?>
  184. <fieldset id="backupuploadsexcludedirs" style="padding-left:15px; margin:2px;">
  185. <legend><strong><?php _e( 'Exclude:', 'backwpup' ); ?></strong></legend>
  186. <?php
  187. if ( $folder && $dir = @opendir( $folder ) ) {
  188. while ( ( $file = readdir( $dir ) ) !== FALSE ) {
  189. if ( ! in_array( $file, array( '.', '..' ) ) && is_dir( $folder . '/' . $file ) && ! in_array( trailingslashit( $folder . '/' . $file ), $this->get_exclude_dirs( $folder ) ) ) {
  190. $folder_size = ( get_site_option( 'backwpup_cfg_showfoldersize') ) ? ' (' . size_format( BackWPup_File::get_folder_size( $folder . '/' . $file ), 2 ) . ')' : '';
  191. echo '<nobr><label for="iduploadexcludedirs-'.sanitize_file_name( $file ).'"><input class="checkbox" type="checkbox"' . checked( in_array( $file, BackWPup_Option::get( $main, 'backupuploadsexcludedirs' ) ), TRUE, FALSE ) . ' name="backupuploadsexcludedirs[]" id="iduploadexcludedirs-'.sanitize_file_name( $file ).'" value="' . $file . '" /> ' . esc_attr( $file ) . $folder_size . '</label><br /></nobr>';
  192. }
  193. }
  194. @closedir( $dir );
  195. }
  196. ?>
  197. </fieldset>
  198. </td>
  199. </tr>
  200. <tr>
  201. <th scope="row"><label for="dirinclude"><?php _e( 'Extra folders to backup', 'backwpup' ); ?></label></th>
  202. <td>
  203. <textarea name="dirinclude" id="dirinclude" class="text code help-tip" rows="7" cols="50" title="<?php esc_attr_e( 'Separate folder names with a line-break or a comma. Folders must be set with their absolute path!', 'backwpup' )?>"><?php echo BackWPup_Option::get( $main, 'dirinclude' ); ?></textarea>
  204. </td>
  205. </tr>
  206. </table>
  207. <h3 class="title"><?php _e( 'Exclude from backup', 'backwpup' ) ?></h3>
  208. <p></p>
  209. <table class="form-table">
  210. <tr>
  211. <th scope="row"><?php _e( 'Thumbnails in uploads', 'backwpup' ); ?></th>
  212. <td>
  213. <label for="idbackupexcludethumbs"><input class="checkbox"
  214. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backupexcludethumbs' ), TRUE, TRUE );?>
  215. name="backupexcludethumbs" id="idbackupexcludethumbs" value="1" /> <?php _e( 'Don\'t backup thumbnails from the site\'s uploads folder.', 'backwpup' ); BackWPup_Help::add_tab( __( 'All images with -???x???. will be excluded. Use a plugin like Regenerate Thumbnails to rebuild them after a restore.', 'backwpup' ) );?></label>
  216. </td>
  217. </tr>
  218. <tr>
  219. <th scope="row"><label for="idfileexclude"><?php _e( 'Exclude files/folders from backup', 'backwpup' ); ?></label></th>
  220. <td>
  221. <textarea name="fileexclude" id="idfileexclude" class="text code help-tip" rows="7" cols="50" title="<?php esc_attr_e( 'Separate file / folder name parts with a line-break or a comma. For example /logs/,.log,.tmp', 'backwpup' ); ?>"><?php echo BackWPup_Option::get( $main, 'fileexclude' ); ?></textarea>
  222. </td>
  223. </tr>
  224. </table>
  225. <h3 class="title"><?php _e( 'Special option', 'backwpup' ) ?></h3>
  226. <p></p>
  227. <table class="form-table">
  228. <tr>
  229. <th scope="row"><?php _e( 'Include special files', 'backwpup' ); ?></th>
  230. <td>
  231. <label for="idbackupspecialfiles"><input class="checkbox" id="idbackupspecialfiles"
  232. type="checkbox"<?php checked( BackWPup_Option::get( $main, 'backupspecialfiles' ), TRUE, TRUE );?>
  233. name="backupspecialfiles" value="1" /> <?php _e( 'Backup wp-config.php, robots.txt, .htaccess, .htpasswd and favicon.ico from root.', 'backwpup' ); BackWPup_Help::add_tab( __( 'If the WordPress root folder is not included in this backup job, check this option to additionally include wp-config.php, robots.txt, .htaccess, .htpasswd and favicon.ico into the backup. Your wp-config.php will be included even if you placed it in the parent directory of your root folder.', 'backwpup' ) ); ?></label>
  234. </td>
  235. </tr>
  236. </table>
  237. <?php
  238. }
  239. /**
  240. * @param $id
  241. */
  242. public function edit_form_post_save( $id ) {
  243. $fileexclude = explode( ',', stripslashes( str_replace( array( "\r\n", "\r" ), ',', $_POST[ 'fileexclude' ] ) ) );
  244. foreach ( $fileexclude as $key => $value ) {
  245. $fileexclude[ $key ] = str_replace( '//', '/', str_replace( '\\', '/', trim( $value ) ) );
  246. if ( empty( $fileexclude[ $key ] ) )
  247. unset( $fileexclude[ $key ] );
  248. }
  249. sort( $fileexclude );
  250. BackWPup_Option::update( $id, 'fileexclude', implode( ',', $fileexclude ) );
  251. $dirinclude = explode( ',', stripslashes( str_replace( array( "\r\n", "\r" ), ',', $_POST[ 'dirinclude' ] ) ) );
  252. foreach ( $dirinclude as $key => $value ) {
  253. $dirinclude[ $key ] = trailingslashit( str_replace( '//', '/', str_replace( '\\', '/', trim( $value ) ) ) );
  254. if ( $dirinclude[ $key ] == '/' || empty( $dirinclude[ $key ] ) || ! is_dir( $dirinclude[ $key ] ) )
  255. unset( $dirinclude[ $key ] );
  256. }
  257. sort( $dirinclude );
  258. BackWPup_Option::update( $id, 'dirinclude', implode( ',', $dirinclude ) );
  259. BackWPup_Option::update( $id, 'backupexcludethumbs', ( isset( $_POST[ 'backupexcludethumbs' ] ) && $_POST[ 'backupexcludethumbs' ] == 1 ) ? TRUE : FALSE );
  260. BackWPup_Option::update( $id, 'backupspecialfiles', ( isset( $_POST[ 'backupspecialfiles' ] ) && $_POST[ 'backupspecialfiles' ] == 1 ) ? TRUE : FALSE );
  261. BackWPup_Option::update( $id, 'backuproot', ( isset( $_POST[ 'backuproot' ] ) && $_POST[ 'backuproot' ] == 1 ) ? TRUE : FALSE );
  262. if ( ! isset( $_POST[ 'backuprootexcludedirs' ] ) || ! is_array( $_POST[ 'backuprootexcludedirs' ] ) )
  263. $_POST[ 'backuprootexcludedirs' ] = array();
  264. sort( $_POST[ 'backuprootexcludedirs' ] );
  265. BackWPup_Option::update( $id, 'backuprootexcludedirs', $_POST[ 'backuprootexcludedirs' ] );
  266. BackWPup_Option::update( $id, 'backupcontent', ( isset( $_POST[ 'backupcontent' ] ) && $_POST[ 'backupcontent' ] == 1 ) ? TRUE : FALSE );
  267. if ( ! isset( $_POST[ 'backupcontentexcludedirs' ] ) || ! is_array( $_POST[ 'backupcontentexcludedirs' ] ) )
  268. $_POST[ 'backupcontentexcludedirs' ] = array();
  269. sort( $_POST[ 'backupcontentexcludedirs' ] );
  270. BackWPup_Option::update( $id, 'backupcontentexcludedirs', $_POST[ 'backupcontentexcludedirs' ] );
  271. BackWPup_Option::update( $id, 'backupplugins', ( isset( $_POST[ 'backupplugins' ] ) && $_POST[ 'backupplugins' ] == 1 ) ? TRUE : FALSE );
  272. if ( ! isset( $_POST[ 'backuppluginsexcludedirs' ] ) || ! is_array( $_POST[ 'backuppluginsexcludedirs' ] ) )
  273. $_POST[ 'backuppluginsexcludedirs' ] = array();
  274. sort( $_POST[ 'backuppluginsexcludedirs' ] );
  275. BackWPup_Option::update( $id, 'backuppluginsexcludedirs', $_POST[ 'backuppluginsexcludedirs' ] );
  276. BackWPup_Option::update( $id, 'backupthemes', ( isset( $_POST[ 'backupthemes' ] ) && $_POST[ 'backupthemes' ] == 1 ) ? TRUE : FALSE );
  277. if ( ! isset( $_POST[ 'backupthemesexcludedirs' ] ) || ! is_array( $_POST[ 'backupthemesexcludedirs' ] ) )
  278. $_POST[ 'backupthemesexcludedirs' ] = array();
  279. sort( $_POST[ 'backupthemesexcludedirs' ] );
  280. BackWPup_Option::update( $id, 'backupthemesexcludedirs', $_POST[ 'backupthemesexcludedirs' ] );
  281. BackWPup_Option::update( $id, 'backupuploads', ( isset( $_POST[ 'backupuploads' ] ) && $_POST[ 'backupuploads' ] == 1 ) ? TRUE : FALSE );
  282. if ( ! isset( $_POST[ 'backupuploadsexcludedirs' ] ) || ! is_array( $_POST[ 'backupuploadsexcludedirs' ] ) )
  283. $_POST[ 'backupuploadsexcludedirs' ] = array();
  284. sort( $_POST[ 'backupuploadsexcludedirs' ] );
  285. BackWPup_Option::update( $id, 'backupuploadsexcludedirs', $_POST[ 'backupuploadsexcludedirs' ] );
  286. }
  287. /**
  288. * @param $job_object
  289. * @return bool
  290. */
  291. public function job_run( &$job_object ) {
  292. $job_object->log( sprintf( __( '%d. Trying to make a list of folders to back up&#160;&hellip;', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ][ 'STEP_TRY' ] ) );
  293. $job_object->substeps_todo = 7;
  294. $job_object->temp[ 'folders_to_backup' ]=array();
  295. //Folder lists for blog folders
  296. $abs_path = realpath( ABSPATH );
  297. if ( $abs_path && ! empty( $job_object->job[ 'backuproot'] ) && $job_object->substeps_done == 0 ) {
  298. $abs_path = trailingslashit( str_replace( '\\', '/', $abs_path ) );
  299. $excludes = $this->get_exclude_dirs( $abs_path );
  300. foreach( $job_object->job[ 'backuprootexcludedirs' ] as $folder )
  301. $excludes[] = trailingslashit( $abs_path . $folder );
  302. $this->get_folder_list( $job_object, $abs_path, $excludes );
  303. }
  304. $job_object->substeps_done = 1;
  305. $job_object->update_working_data();
  306. $wp_content_dir = realpath( WP_CONTENT_DIR );
  307. if ( $wp_content_dir && ! empty( $job_object->job[ 'backupcontent'] ) && $job_object->substeps_done == 1 ) {
  308. $wp_content_dir = trailingslashit( str_replace( '\\', '/', $wp_content_dir ) );
  309. $excludes = $this->get_exclude_dirs( $wp_content_dir );
  310. foreach( $job_object->job[ 'backupcontentexcludedirs' ] as $folder )
  311. $excludes[] = trailingslashit( $wp_content_dir . $folder );
  312. $this->get_folder_list( $job_object, $wp_content_dir, $excludes );
  313. }
  314. $job_object->substeps_done = 2;
  315. $job_object->update_working_data();
  316. $wp_plugin_dir = realpath( WP_PLUGIN_DIR );
  317. if ( $wp_plugin_dir && ! empty( $job_object->job[ 'backupplugins'] ) && $job_object->substeps_done == 2 ) {
  318. $wp_plugin_dir = trailingslashit( str_replace( '\\', '/', $wp_plugin_dir ) );
  319. $excludes = $this->get_exclude_dirs( $wp_plugin_dir );
  320. foreach( $job_object->job[ 'backuppluginsexcludedirs' ] as $folder )
  321. $excludes[] = trailingslashit( $wp_plugin_dir . $folder );
  322. $this->get_folder_list( $job_object, $wp_plugin_dir, $excludes );
  323. }
  324. $job_object->substeps_done = 3;
  325. $job_object->update_working_data();
  326. $theme_root = realpath( get_theme_root() );
  327. if ( $theme_root && ! empty( $job_object->job[ 'backupthemes'] ) && $job_object->substeps_done == 3 ) {
  328. $theme_root = trailingslashit( str_replace( '\\', '/', $theme_root ) );
  329. $excludes = $this->get_exclude_dirs( $theme_root );
  330. foreach( $job_object->job[ 'backupthemesexcludedirs' ] as $folder )
  331. $excludes[] = trailingslashit( $theme_root . $folder );
  332. $this->get_folder_list( $job_object, $theme_root, $excludes );
  333. }
  334. $job_object->substeps_done = 4;
  335. $job_object->update_working_data();
  336. $upload_dir = realpath( BackWPup_File::get_upload_dir() );
  337. if ( $upload_dir && ! empty( $job_object->job[ 'backupuploads'] ) && $job_object->substeps_done == 4 ) {
  338. $upload_dir = trailingslashit( str_replace( '\\', '/', $upload_dir ) );
  339. $excludes = $this->get_exclude_dirs( $upload_dir );
  340. foreach( $job_object->job[ 'backupuploadsexcludedirs' ] as $folder )
  341. $excludes[] = trailingslashit( $upload_dir . $folder );
  342. $this->get_folder_list( $job_object, $upload_dir, $excludes );
  343. }
  344. $job_object->substeps_done = 5;
  345. $job_object->update_working_data();
  346. //include dirs
  347. if ( $job_object->job[ 'dirinclude' ] && $job_object->substeps_done == 5 ) {
  348. $dirinclude = explode( ',', $job_object->job[ 'dirinclude' ] );
  349. $dirinclude = array_unique( $dirinclude );
  350. //Crate file list for includes
  351. foreach ( $dirinclude as $dirincludevalue ) {
  352. if ( is_dir( $dirincludevalue ) )
  353. $this->get_folder_list( $job_object, $dirincludevalue );
  354. }
  355. }
  356. $job_object->substeps_done = 6;
  357. $job_object->update_working_data();
  358. //save folder list
  359. $this->folers_to_backup = array_unique( $this->folers_to_backup );
  360. sort( $this->folers_to_backup );
  361. $job_object->data_storage( 'folder', $this->folers_to_backup );
  362. $job_object->count_folder = count( $this->folers_to_backup );
  363. $job_object->update_working_data();
  364. //add extra files if selected
  365. if ( ! empty( $job_object->job[ 'backupspecialfiles'] ) ) {
  366. if ( is_readable( ABSPATH . 'wp-config.php' ) && empty( $job_object->job[ 'backuproot' ] ) ) {
  367. $job_object->additional_files_to_backup[ ] = str_replace( '\\', '/', ABSPATH . 'wp-config.php' );
  368. $job_object->count_files ++;
  369. $job_object->count_filesize = $job_object->count_filesize + @filesize( ABSPATH . 'wp-config.php' );
  370. $job_object->log( sprintf( __( 'Added "%s" to backup file list', 'backwpup' ), 'wp-config.php' ) );
  371. }
  372. elseif ( BackWPup_File::is_in_open_basedir( dirname( ABSPATH ) . '/wp-config.php' ) ) {
  373. if ( is_readable( dirname( ABSPATH ) . '/wp-config.php' ) && ! is_readable( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
  374. $job_object->additional_files_to_backup[ ] = str_replace( '\\', '/', dirname( ABSPATH ) . '/wp-config.php' );
  375. $job_object->count_files ++;
  376. $job_object->count_filesize = $job_object->count_filesize + @filesize( dirname( ABSPATH ) . '/wp-config.php' );
  377. $job_object->log( sprintf( __( 'Added "%s" to backup file list', 'backwpup' ), 'wp-config.php' ) );
  378. }
  379. }
  380. if ( is_readable( ABSPATH . '.htaccess' ) && empty( $job_object->job[ 'backuproot' ] ) ) {
  381. $job_object->additional_files_to_backup[ ] = str_replace( '\\', '/', ABSPATH . '.htaccess' );
  382. $job_object->count_files ++;
  383. $job_object->count_filesize = $job_object->count_filesize + @filesize( ABSPATH . '.htaccess' );
  384. $job_object->log( sprintf( __( 'Added "%s" to backup file list', 'backwpup' ), '.htaccess' ) );
  385. }
  386. if ( is_readable( ABSPATH . '.htpasswd' ) && empty( $job_object->job[ 'backuproot' ] ) ) {
  387. $job_object->additional_files_to_backup[ ] = str_replace( '\\', '/', ABSPATH . '.htpasswd' );
  388. $job_object->count_files ++;
  389. $job_object->count_filesize = $job_object->count_filesize + @filesize( ABSPATH . '.htpasswd' );
  390. $job_object->log( sprintf( __( 'Added "%s" to backup file list', 'backwpup' ), '.htpasswd' ) );
  391. }
  392. if ( is_readable( ABSPATH . 'robots.txt' ) && empty( $job_object->job[ 'backuproot' ] ) ) {
  393. $job_object->additional_files_to_backup[ ] = str_replace( '\\', '/', ABSPATH . 'robots.txt' );
  394. $job_object->count_files ++;
  395. $job_object->count_filesize = $job_object->count_filesize + @filesize( ABSPATH . 'robots.txt' );
  396. $job_object->log( sprintf( __( 'Added "%s" to backup file list', 'backwpup' ), 'robots.txt' ) );
  397. }
  398. if ( is_readable( ABSPATH . 'favicon.ico' ) && empty( $job_object->job[ 'backuproot' ] ) ) {
  399. $job_object->additional_files_to_backup[ ] = str_replace( '\\', '/', ABSPATH . 'favicon.ico' );
  400. $job_object->count_files ++;
  401. $job_object->count_filesize = $job_object->count_filesize + @filesize( ABSPATH . 'favicon.ico' );
  402. $job_object->log( sprintf( __( 'Added "%s" to backup file list', 'backwpup' ), 'favicon.ico' ) );
  403. }
  404. }
  405. if ( $job_object->count_folder == 0 && count( $job_object->additional_files_to_backup ) == 0 )
  406. $job_object->log( __( 'No files/folder for the backup.', 'backwpup' ), E_USER_WARNING );
  407. elseif ( $job_object->count_folder > 1 )
  408. $job_object->log( sprintf( __( '%1$d folders to backup.', 'backwpup' ), $job_object->count_folder ) );
  409. $job_object->substeps_done = 7;
  410. return TRUE;
  411. }
  412. /**
  413. *
  414. * Helper function for folder_list()
  415. *
  416. * @param $job_object BackWPup_Job
  417. * @param string $folder
  418. * @param array $excludedirs
  419. *
  420. * @return bool
  421. */
  422. private function get_folder_list( &$job_object, $folder, $excludedirs = array() ) {
  423. $folder = trailingslashit( $folder );
  424. if ( $dir = opendir( $folder ) ) {
  425. //add folder to folder list
  426. $this->folers_to_backup[] = $folder;
  427. //scan folder
  428. while ( FALSE !== ( $file = readdir( $dir ) ) ) {
  429. if ( in_array( $file, array( '.', '..' ) ) )
  430. continue;
  431. foreach ( $job_object->exclude_from_backup as $exclusion ) { //exclude files
  432. $exclusion = trim( $exclusion );
  433. if ( FALSE !== stripos( $folder . $file, trim( $exclusion ) ) && ! empty( $exclusion ) )
  434. continue 2;
  435. }
  436. if ( is_dir( $folder . $file ) ) {
  437. if ( in_array( trailingslashit( $folder . $file ), $excludedirs ) )
  438. continue;
  439. if ( ! is_readable( $folder . $file ) ) {
  440. $job_object->log( sprintf( __( 'Folder "%s" is not readable!', 'backwpup' ), $folder . $file ), E_USER_WARNING );
  441. continue;
  442. }
  443. $this->get_folder_list( $job_object, trailingslashit( $folder . $file ), $excludedirs );
  444. }
  445. }
  446. closedir( $dir );
  447. }
  448. return TRUE;
  449. }
  450. /**
  451. *
  452. * Get folder to exclude from a given folder for file backups
  453. *
  454. * @param $folder string folder to check for excludes
  455. *
  456. * @return array of folder to exclude
  457. */
  458. private function get_exclude_dirs( $folder ) {
  459. $folder = trailingslashit( str_replace( '\\', '/', realpath( $folder ) ) );
  460. $excludedir = array();
  461. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', realpath( BackWPup::get_plugin_data( 'TEMP' ) ) ) ); //exclude temp
  462. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', realpath( get_site_option( 'backwpup_cfg_logfolder' ) ) ) ); //exclude log folder
  463. if ( FALSE !== strpos( trailingslashit( str_replace( '\\', '/', realpath( ABSPATH ) ) ), $folder ) && trailingslashit( str_replace( '\\', '/', realpath( ABSPATH ) ) ) != $folder )
  464. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', realpath( ABSPATH ) ) );
  465. if ( FALSE !== strpos( trailingslashit( str_replace( '\\', '/', realpath( WP_CONTENT_DIR ) ) ), $folder ) && trailingslashit( str_replace( '\\', '/', realpath( WP_CONTENT_DIR ) ) ) != $folder )
  466. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', realpath( WP_CONTENT_DIR ) ) );
  467. if ( FALSE !== strpos( trailingslashit( str_replace( '\\', '/', realpath( WP_PLUGIN_DIR ) ) ), $folder ) && trailingslashit( str_replace( '\\', '/', realpath( WP_PLUGIN_DIR ) ) ) != $folder )
  468. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', realpath( WP_PLUGIN_DIR ) ) );
  469. if ( FALSE !== strpos( trailingslashit( str_replace( '\\', '/', realpath( get_theme_root() ) ) ), $folder ) && trailingslashit( str_replace( '\\', '/', realpath( get_theme_root() ) ) ) != $folder )
  470. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', realpath( get_theme_root() ) ) );
  471. if ( FALSE !== strpos( trailingslashit( realpath( BackWPup_File::get_upload_dir() ) ), $folder ) && trailingslashit( realpath( BackWPup_File::get_upload_dir() ) ) != $folder )
  472. $excludedir[ ] = trailingslashit( realpath( BackWPup_File::get_upload_dir() ) );
  473. //Exclude Backup dirs
  474. $jobids = BackWPup_Option::get_job_ids();
  475. foreach ( $jobids as $id ) {
  476. $backupdir = realpath( BackWPup_Option::get( $id, 'backupdir' ) );
  477. if ( ! empty( $backupdir ) && $backupdir != '/' )
  478. $excludedir[ ] = trailingslashit( str_replace( '\\', '/', $backupdir ) );
  479. }
  480. return array_unique( $excludedir );
  481. }
  482. }