self::files(), 'db' => self::database(), 'scanned' => time(), ); set_transient( self::CACHE_KEY, $info, 15 * MINUTE_IN_SECONDS ); return $info; } /** * Uncompressed bytes of files a backup with these options will hold. * * @param array $options Sanitised backup options. * @return int */ public static function expected_files( array $options ) { $info = self::get(); $groups = $info['files']['groups']; $map = array( 'uploads' => 'skip_media', 'themes' => 'skip_themes', 'plugins' => 'skip_plugins', 'mu-plugins' => 'skip_mu_plugins', 'other' => 'skip_other', ); $total = 0; foreach ( $map as $group => $key ) { if ( empty( $options[ $key ] ) ) { $total += (int) $groups[ $group ]['bytes']; } } if ( empty( $options['skip_themes'] ) && ! empty( $options['active_theme_only'] ) ) { $total -= (int) $info['files']['inactive']['themes']['bytes']; } if ( empty( $options['skip_plugins'] ) && ! empty( $options['active_plugins_only'] ) ) { $total -= (int) $info['files']['inactive']['plugins']['bytes']; } return max( 0, $total ); } /** * File sizes per group, plus per-theme and per-plugin sizes. * * @return array */ private static function files() { $root = rtrim( str_replace( '\\', '/', WP_CONTENT_DIR ), '/' ); $dir = PathGuard::backups_dir(); $walk = new FilesSource( is_wp_error( $dir ) ? $root . '/sitezip-backups-none' : $dir ); $groups = array(); foreach ( array( 'uploads', 'themes', 'plugins', 'mu-plugins', 'other' ) as $group ) { $groups[ $group ] = array( 'bytes' => 0, 'count' => 0, ); } $items = array( 'themes' => array(), 'plugins' => array(), ); // What the backup leaves out, measured so the totals add up to // what is really on disk. $skipped = array(); foreach ( array( 'backups', 'cache', 'logs', 'dev', 'server' ) as $reason ) { $skipped[ $reason ] = array( 'bytes' => 0, 'count' => 0, 'names' => array(), ); } // Things the backup cannot reach: shown as warnings. $symlinks = array(); $link_size = array( 'bytes' => 0, 'count' => 0, ); $unreadable = array( 'count' => 0, 'names' => array(), ); $partial = false; $stop = microtime( true ) + self::SCAN_SECONDS; // Each entry: relative dir, and the reason it is skipped ('' = backed up). $stack = array( array( '', '' ) ); while ( $stack ) { if ( microtime( true ) > $stop ) { $partial = true; break; } list( $rel_dir, $dir_reason ) = array_pop( $stack ); $abs_dir = '' === $rel_dir ? $root : $root . '/' . $rel_dir; $entries = @scandir( $abs_dir ); if ( false === $entries ) { if ( '' === $dir_reason ) { $unreadable['count']++; $unreadable['names'][] = $rel_dir . '/'; } continue; } foreach ( $entries as $name ) { if ( '.' === $name || '..' === $name ) { continue; } $rel = '' === $rel_dir ? $name : $rel_dir . '/' . $name; $path = $abs_dir . '/' . $name; $reason = '' !== $dir_reason ? $dir_reason : $walk->exclusion_reason( $rel, $path ); if ( is_link( $path ) ) { if ( '' === $reason ) { // A link whose target is gone is marked, so the user knows why it is empty. $symlinks[] = $rel . ( is_dir( $path ) ? '/' : '' ) . ( file_exists( $path ) ? '' : ' ' . __( '(broken link: its target no longer exists)', 'sitezip' ) ); // Measure what the link points to, so the panel can show its size. $linked = self::measure_link( $path, $stop ); $link_size['bytes'] += $linked['bytes']; $link_size['count'] += $linked['count']; $partial = $partial || $linked['partial']; } continue; } // Name each left-out file or folder where the reason starts, // not every file inside a left-out folder. if ( '' !== $reason && '' === $dir_reason && isset( $skipped[ $reason ] ) && count( $skipped[ $reason ]['names'] ) < 20 ) { $skipped[ $reason ]['names'][] = $rel . ( is_dir( $path ) ? '/' : '' ); } if ( is_dir( $path ) ) { $stack[] = array( $rel, $reason ); continue; } $size = (int) @filesize( $path ); if ( '' !== $reason ) { if ( isset( $skipped[ $reason ] ) ) { $skipped[ $reason ]['bytes'] += $size; $skipped[ $reason ]['count']++; } continue; } if ( ! is_readable( $path ) ) { $unreadable['count']++; $unreadable['names'][] = $rel; continue; } $group = FilesSource::group( $rel ); $groups[ $group ]['bytes'] += $size; $groups[ $group ]['count']++; $parts = explode( '/', $rel, 3 ); if ( isset( $items[ $parts[0] ], $parts[1] ) && 'index.php' !== $parts[1] ) { if ( ! isset( $items[ $parts[0] ][ $parts[1] ] ) ) { $items[ $parts[0] ][ $parts[1] ] = 0; } $items[ $parts[0] ][ $parts[1] ] += $size; } } } // Media stored outside wp-content (a custom UPLOADS path) is not // in any backup, because only wp-content is archived. $uploads = wp_upload_dir( null, false ); $uploads_dir = rtrim( str_replace( '\\', '/', (string) $uploads['basedir'] ), '/' ); $uploads_outside = ( '' !== $uploads_dir && 0 !== strpos( $uploads_dir . '/', $root . '/' ) ) ? $uploads_dir : ''; $active = array( 'themes' => FilesSource::active_themes(), 'plugins' => FilesSource::active_plugins(), ); $inactive = array(); foreach ( $items as $group => $sizes ) { $inactive[ $group ] = array( 'bytes' => 0, 'count' => 0, 'names' => array(), ); foreach ( $sizes as $name => $bytes ) { if ( ! in_array( $name, $active[ $group ], true ) ) { $inactive[ $group ]['bytes'] += $bytes; $inactive[ $group ]['count']++; $inactive[ $group ]['names'][] = $name; } } } return array( 'groups' => $groups, 'inactive' => $inactive, 'skipped' => $skipped, 'symlinks' => array_slice( $symlinks, 0, 20 ), 'symlink_count' => count( $symlinks ), 'symlink_bytes' => $link_size['bytes'], 'symlink_files' => $link_size['count'], 'unreadable' => array( 'count' => $unreadable['count'], 'names' => array_slice( $unreadable['names'], 0, 20 ), ), 'uploads_outside' => $uploads_outside, 'partial' => $partial, ); } /** * Database size and counts of rows that can be left out. * * @return array */ private static function database() { global $wpdb; $like = $wpdb->esc_like( $wpdb->base_prefix ) . '%'; $bytes = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = DATABASE() AND table_name LIKE %s', $like ) ); // phpcs:disable WordPress.DB.DirectDatabaseQuery $revisions = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'revision'" ); $spam = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_approved IN ('spam','trash')" ); $trans = $wpdb->get_row( "SELECT COUNT(*) AS n, COALESCE(SUM(LENGTH(option_value)),0) AS b FROM {$wpdb->options} WHERE option_name LIKE '\\_transient\\_%' OR option_name LIKE '\\_site\\_transient\\_%'", ARRAY_A ); $rev_b = (int) $wpdb->get_var( "SELECT COALESCE(SUM(LENGTH(post_content)),0) FROM {$wpdb->posts} WHERE post_type = 'revision'" ); // phpcs:enable WordPress.DB.DirectDatabaseQuery return array( 'bytes' => $bytes, 'revisions' => $revisions, 'revisions_bytes' => $rev_b, 'spam' => $spam, 'transients' => (int) $trans['n'], 'transients_bytes' => (int) $trans['b'], ); } /** * Size and file count of what a symlink points to. Links found inside * the target are not followed, so a loop cannot run forever. * * @param string $path Symlink path. * @param float $stop Stop-by timestamp. * @return array Keys: bytes, count, partial. */ private static function measure_link( $path, $stop ) { $out = array( 'bytes' => 0, 'count' => 0, 'partial' => false, ); if ( ! is_dir( $path ) ) { if ( is_file( $path ) ) { $out['bytes'] = (int) @filesize( $path ); $out['count'] = 1; } return $out; } try { $files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $path, \FilesystemIterator::SKIP_DOTS ), \RecursiveIteratorIterator::LEAVES_ONLY, \RecursiveIteratorIterator::CATCH_GET_CHILD ); foreach ( $files as $file ) { if ( microtime( true ) > $stop ) { $out['partial'] = true; break; } if ( $file->isFile() && ! $file->isLink() ) { $out['bytes'] += (int) $file->getSize(); $out['count']++; } } } catch ( \Exception $e ) { $out['partial'] = true; } return $out; } }