/** * Utility function recursively scan a folder and build an array of it's contents * * @since 1.0.2 * @see * * @param string $base Base directory to start. * @return array $data array of files/folder found */ function snapshot_utility_scandir($base='') { if ((!$base) || (!strlen($base))) return array(); if (!file_exists($base)) { return array(); } $data = array_diff(scandir($base), array('.', '..')); $subs = array(); foreach($data as $key => $value) : if ( is_dir($base . '/' . $value) ) : unset($data[$key]); $subs[] = snapshot_utility_scandir($base . '/' . $value); elseif ( is_file($base . '/' . $value) ) : $data[$key] = $base . '/' . $value; endif; endforeach; if (count($subs)) { foreach ( $subs as $sub ) { $data = array_merge($data, $sub); } } return $data; }
function snapshot_gather_item_files($item) { global $wpdb, $site_id; $item_files = array(); $home_path = apply_filters('snapshot_home_path', get_home_path()); if (!isset($item['files-option']) || !count($item['files-option'])) { return $item_files; } if ($item['files-option'] == "none") { if (isset($item['files-sections']) && count($item['files-sections'])) { unset($item['files-sections']); $item['files-sections'] = array(); } } else { if ($item['files-option'] == "all") { if (is_main_site($item['blog-id'])) { $files_sections = array('themes', 'plugins', 'media'); } else { $files_sections = array('media'); } } else { if ($item['files-option'] == "selected") { $files_sections = $item['files-sections']; } } } if (!isset($files_sections) || !count($files_sections)) { return $item_files; } //global $is_IIS; //echo "is_IIS[". $is_IIS ."]<br />"; //echo "iis7_supports_permalinks[". iis7_supports_permalinks() ."]<br />"; //echo "files_sections<pre>"; print_r($files_sections); echo "</pre>"; foreach ($files_sections as $file_section) { switch ($file_section) { case 'media': $_path = $home_path . snapshot_utility_get_blog_upload_path($item['blog-id']); $_path = str_replace('\\', '/', $_path); //echo "_path[". $_path ."]<br />"; $item_files['media'] = snapshot_utility_scandir($_path); //echo "media files<pre>"; print_r($item_files['media']); echo "</pre>"; //die(); break; case 'plugins': $_path = trailingslashit(WP_CONTENT_DIR) . 'plugins'; $_path = str_replace('\\', '/', $_path); $item_files['plugins'] = snapshot_utility_scandir($_path); break; /* case 'mu-plugins': $_path = trailingslashit(WP_CONTENT_DIR) . 'mu-plugins'; $_path = str_replace('\\', '/', $_path); $item_files['mu-plugins'] = snapshot_utility_scandir($_path); break; */ /* case 'mu-plugins': $_path = trailingslashit(WP_CONTENT_DIR) . 'mu-plugins'; $_path = str_replace('\\', '/', $_path); $item_files['mu-plugins'] = snapshot_utility_scandir($_path); break; */ case 'themes': $_path = trailingslashit(WP_CONTENT_DIR) . 'themes'; $_path = str_replace('\\', '/', $_path); $item_files['themes'] = snapshot_utility_scandir($_path); break; case 'config': $wp_config_file = trailingslashit($home_path) . "wp-config.php"; //$wp_config_file = str_replace('\\', '/', $wp_config_file); if (file_exists($wp_config_file)) { if (!isset($item_files['files'])) { $item_files['files'] = array(); } $item_files['files'][] = $wp_config_file; } break; case 'htaccess': $wp_htaccess_file = trailingslashit($home_path) . ".htaccess"; //$wp_htaccess_file = str_replace('\\', '/', $wp_htaccess_file); if (file_exists($wp_htaccess_file)) { if (!isset($item_files['files'])) { $item_files['files'] = array(); } $item_files['files'][] = $wp_htaccess_file; } $web_config_file = trailingslashit($home_path) . "web.config"; //$web_config_file = str_replace('\\', '/', $web_config_file); if (file_exists($web_config_file)) { if (!isset($item_files['files'])) { $item_files['files'] = array(); } $item_files['files'][] = $web_config_file; } break; default: break; } } //echo "item_files<pre>"; print_r($item_files); echo "</pre>"; //die(); if (!count($item_files)) { return $item_files; } // Exclude files. $item_ignore_files = array(); // With WP 3.5 fresh installs we have a slight issue. In prior versions of WP the main site upload folder and // related sub-site were seperate. Main site was typically /wp-content/uploads/ while sub-sites were // /wp-content/blogs.dir/X/files/ // But in 3.5 when doing a fresh install, not upgrade, the sub-site upload path is beneath the main site. // main site /wp-content/uploads/ and sub-site wp-content/uploads/sites/X // So we have this added fun to try and exclude the sub-site from the main site's media. ug. $blog_id = intval($item['blog-id']); if (is_multisite() && is_main_site($blog_id)) { $main_site_upload_path = snapshot_utility_get_blog_upload_path($blog_id); $sql_str = $wpdb->prepare("SELECT blog_id FROM " . $wpdb->base_prefix . "blogs WHERE blog_id != %d AND site_id=%d LIMIT 5", $blog_id, $site_id); $blog_ids = $wpdb->get_col($sql_str); if (!empty($blog_ids)) { foreach ($blog_ids as $blog_id_tmp) { $sub_site_upload_path = snapshot_utility_get_blog_upload_path($blog_id_tmp); if (!empty($sub_site_upload_path)) { if ($sub_site_upload_path !== $main_site_upload_path && substr($sub_site_upload_path, 0, strlen($main_site_upload_path)) == $main_site_upload_path) { $item_ignore_files[] = dirname($sub_site_upload_path); } break; } } } } //We auto exclude the snapshot tree. Plus any entered exclude entries from the form. $item_ignore_files[] = trailingslashit($this->_settings['backupBaseFolderFull']); $item_ignore_files[] = trailingslashit($this->_settings['SNAPSHOT_PLUGIN_BASE_DIR']); // Then we add any global excludes if (isset($this->config_data['config']['filesIgnore']) && count($this->config_data['config']['filesIgnore'])) { $item_ignore_files = array_merge($item_ignore_files, $this->config_data['config']['filesIgnore']); } // Then item excludes if (isset($item['files-ignore']) && count($item['files-ignore'])) { $item_ignore_files = array_merge($item_ignore_files, $item['files-ignore']); } $item_section_files = array(); // Need to exclude the user ignore patterns as well as our Snapshot base folder. No backup of the backups foreach ($item_files as $item_set_key => $item_set_files) { if (!is_array($item_set_files) || !count($item_set_files)) { continue; } foreach ($item_set_files as $item_set_files_key => $item_set_files_file) { // We spin through all the files. They will fall into one of three sections... // If the file is not readable we ignore if (!is_readable($item_set_files_file)) { if (!isset($item_section_files['error'][$item_set_key])) { $item_section_files['error'][$item_set_key] = array(); } $item_section_files['error'][$item_set_key][] = $item_set_files_file; } else { $EXCLUDE_THIS_FILE = false; foreach ($item_ignore_files as $item_ignore_file) { // Make sure we don't have any blank entries. $item_ignore_file = trim($item_ignore_file); if (empty($item_ignore_file)) { continue; } //echo "item_set_files_file<pre>"; print_r($item_set_files_file); echo "</pre>"; //echo "item_ignore_file[". $item_ignore_file ."]<br />"; $stristr_ret = stristr($item_set_files_file, $item_ignore_file); if ($stristr_ret !== false) { $EXCLUDE_THIS_FILE = true; break; } } if ($EXCLUDE_THIS_FILE == false) { // If file is valid we keep it if (!isset($item_section_files['included'][$item_set_key])) { $item_section_files['included'][$item_set_key] = array(); } $item_section_files['included'][$item_set_key][] = $item_set_files_file; } else { if (!isset($item_section_files['excluded']['pattern'])) { $item_section_files['excluded']['pattern'] = array(); } $item_section_files['excluded']['pattern'][] = $item_set_files_file; } } } } //echo "item_section_files<pre>"; print_r($item_section_files); echo "</pre>"; //die(); return $item_section_files; }