PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

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