<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[25540] trunk/src/wp-admin/includes: Upgrader: Perform a MD5 file verification check on the files during upgrade.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/25540">25540</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2013-09-21 06:48:20 +0000 (Sat, 21 Sep 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Upgrader: Perform a MD5 file verification check on the files during upgrade. This ensures that both a Partial upgrade build can be used, and that all the files were copied into place correctly.
Props pento for initial patch. Fixes <a href="http://core.trac.wordpress.org/ticket/18201">#18201</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpupgraderphp">trunk/src/wp-admin/includes/class-wp-upgrader.php</a></li>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunksrcwpadminincludesupdatecorephp">trunk/src/wp-admin/includes/update-core.php</a></li>
<li><a href="#trunksrcwpadminincludesupdatephp">trunk/src/wp-admin/includes/update.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpupgraderphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/class-wp-upgrader.php (25539 => 25540)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/class-wp-upgrader.php 2013-09-21 05:52:06 UTC (rev 25539)
+++ trunk/src/wp-admin/includes/class-wp-upgrader.php 2013-09-21 06:48:20 UTC (rev 25540)
</span><span class="lines">@@ -1111,11 +1111,18 @@
</span><span class="cx">
</span><span class="cx"> $wp_dir = trailingslashit($wp_filesystem->abspath());
</span><span class="cx">
</span><ins>+ // Pre-cache the checksums for the versions we care about
+ get_core_checksums( array( $wp_version, $current->version ) );
+
+ $no_partial = false;
+ if ( ! $this->check_files() )
+ $no_partial = true;
+
</ins><span class="cx"> // If partial update is returned from the API, use that, unless we're doing a reinstall.
</span><span class="cx"> // If we cross the new_bundled version number, then use the new_bundled zip.
</span><span class="cx"> // Don't though if the constant is set to skip bundled items.
</span><span class="cx"> // If the API returns a no_content zip, go with it. Finally, default to the full zip.
</span><del>- if ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version )
</del><ins>+ if ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && ! $no_partial )
</ins><span class="cx"> $to_download = 'partial';
</span><span class="cx"> elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
</span><span class="cx"> && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) )
</span><span class="lines">@@ -1205,6 +1212,21 @@
</span><span class="cx"> return false;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ function check_files() {
+ global $wp_version;
+
+ $checksums = get_core_checksums( $wp_version );
+
+ if ( empty( $checksums[ $wp_version ] ) || ! is_array( $checksums[ $wp_version ] ) )
+ return false;
+
+ foreach ( $checksums[ $wp_version ] as $file => $checksum ) {
+ if ( md5_file( ABSPATH . $file ) !== $checksum )
+ return false;
+ }
+
+ return true;
+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/file.php (25539 => 25540)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/file.php 2013-09-21 05:52:06 UTC (rev 25539)
+++ trunk/src/wp-admin/includes/file.php 2013-09-21 06:48:20 UTC (rev 25540)
</span><span class="lines">@@ -723,17 +723,9 @@
</span><span class="cx"> $from = trailingslashit($from);
</span><span class="cx"> $to = trailingslashit($to);
</span><span class="cx">
</span><del>- $skip_regex = '';
- foreach ( (array)$skip_list as $key => $skip_file )
- $skip_regex .= preg_quote($skip_file, '!') . '|';
-
- if ( !empty($skip_regex) )
- $skip_regex = '!(' . rtrim($skip_regex, '|') . ')$!i';
-
</del><span class="cx"> foreach ( (array) $dirlist as $filename => $fileinfo ) {
</span><del>- if ( !empty($skip_regex) )
- if ( preg_match($skip_regex, $from . $filename) )
- continue;
</del><ins>+ if ( in_array( $filename, $skip_list ) )
+ continue;
</ins><span class="cx">
</span><span class="cx"> if ( 'f' == $fileinfo['type'] ) {
</span><span class="cx"> if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
</span><span class="lines">@@ -747,7 +739,15 @@
</span><span class="cx"> if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
</span><span class="cx"> return new WP_Error('mkdir_failed', __('Could not create directory.'), $to . $filename);
</span><span class="cx"> }
</span><del>- $result = copy_dir($from . $filename, $to . $filename, $skip_list);
</del><ins>+
+ // generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list
+ $sub_skip_list = array();
+ foreach ( $skip_list as $skip_item ) {
+ if ( 0 === strpos( $skip_item, $filename . '/' ) )
+ $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
+ }
+
+ $result = copy_dir($from . $filename, $to . $filename, $sub_skip_list);
</ins><span class="cx"> if ( is_wp_error($result) )
</span><span class="cx"> return $result;
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunksrcwpadminincludesupdatecorephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/update-core.php (25539 => 25540)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/update-core.php 2013-09-21 05:52:06 UTC (rev 25539)
+++ trunk/src/wp-admin/includes/update-core.php 2013-09-21 06:48:20 UTC (rev 25540)
</span><span class="lines">@@ -688,17 +688,63 @@
</span><span class="cx"> elseif ( !$mysql_compat )
</span><span class="cx"> return new WP_Error( 'mysql_not_compatible', sprintf( __('The update cannot be installed because WordPress %1$s requires MySQL version %2$s or higher. You are running version %3$s.'), $wp_version, $required_mysql_version, $mysql_version ) );
</span><span class="cx">
</span><del>- apply_filters('update_feedback', __('Installing the latest version…'));
</del><ins>+ apply_filters( 'update_feedback', __( 'Preparing to install the latest version…' ) );
</ins><span class="cx">
</span><ins>+ // Don't copy wp-content, we'll deal with that below
+ $skip = array( 'wp-content' );
+
+ // Check to see which files don't really need updating - only available for 3.7 and higher
+ if ( function_exists( 'get_core_checksums' ) ) {
+ $checksums = get_core_checksums( $wp_version );
+ if ( ! empty( $checksums[ $wp_version ] ) && is_array( $checksums[ $wp_version ] ) ) {
+ foreach( $checksums[ $wp_version ] as $file => $checksum ) {
+ if ( md5_file( ABSPATH . $file ) === $checksum )
+ $skip[] = $file;
+ }
+ }
+ }
+
+ apply_filters( 'update_feedback', __( 'Enabling Maintenance mode…' ) );
</ins><span class="cx"> // Create maintenance file to signal that we are upgrading
</span><span class="cx"> $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
</span><span class="cx"> $maintenance_file = $to . '.maintenance';
</span><span class="cx"> $wp_filesystem->delete($maintenance_file);
</span><span class="cx"> $wp_filesystem->put_contents($maintenance_file, $maintenance_string, FS_CHMOD_FILE);
</span><span class="cx">
</span><ins>+ apply_filters( 'update_feedback', __( 'Copying the required files…' ) );
</ins><span class="cx"> // Copy new versions of WP files into place.
</span><del>- $result = _copy_dir($from . $distro, $to, array('wp-content') );
</del><ins>+ $result = _copy_dir( $from . $distro, $to, $skip );
</ins><span class="cx">
</span><ins>+ // Check to make sure everything copied correctly, ignoring the contents of wp-content
+ $skip = array( 'wp-content' );
+ $failed = array();
+ if ( ! empty( $checksums[ $wp_version ] ) && is_array( $checksums[ $wp_version ] ) ) {
+ foreach ( $checksums[ $wp_version ] as $file => $checksum ) {
+ if ( 0 === strpos( $file, 'wp-content' ) )
+ continue;
+
+ if ( md5_file( ABSPATH . $file ) == $checksum )
+ $skip[] = $file;
+ else
+ $failed[] = $file;
+ }
+ }
+
+ // Some files didn't copy properly
+ if ( ! empty( $failed ) ) {
+ $total_size = 0;
+ // Find the local version of the working directory
+ $working_dir_local = str_replace( trailingslashit( $wp_filesystem->wp_content_dir() ), trailingslashit( WP_CONTENT_DIR ), $from . $distro );
+ foreach ( $failed as $file )
+ $total_size += filesize( $working_dir_local . '/' . $file );
+
+ // If we don't have enough free space, it isn't worth trying again
+ if ( $total_size >= disk_free_space( ABSPATH ) )
+ $result = new WP_Error( 'disk_full', __( "There isn't enough free disk space to complete the upgrade." ), $to );
+ else
+ $result = _copy_dir( $from . $distro, $to, $skip );
+ }
+
</ins><span class="cx"> // Custom Content Directory needs updating now.
</span><span class="cx"> // Copy Languages
</span><span class="cx"> if ( !is_wp_error($result) && $wp_filesystem->is_dir($from . $distro . 'wp-content/languages') ) {
</span><span class="lines">@@ -795,6 +841,7 @@
</span><span class="cx"> else
</span><span class="cx"> delete_option('update_core');
</span><span class="cx">
</span><ins>+ apply_filters( 'update_feedback', __( 'Disabling Maintenance mode…' ) );
</ins><span class="cx"> // Remove maintenance file, we're done.
</span><span class="cx"> $wp_filesystem->delete($maintenance_file);
</span><span class="cx">
</span><span class="lines">@@ -808,10 +855,12 @@
</span><span class="cx"> * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
</span><span class="cx"> * Assumes that WP_Filesystem() has already been called and setup.
</span><span class="cx"> *
</span><del>- * This is a temporary function for the 3.1 -> 3.2 upgrade only and will be removed in 3.3
</del><ins>+ * This is a temporary function for the 3.1 -> 3.2 upgrade, as well as for those upgrading to
+ * 3.7+
</ins><span class="cx"> *
</span><span class="cx"> * @ignore
</span><span class="cx"> * @since 3.2.0
</span><ins>+ * @since 3.7.0 Updated not to use a regular expression for the skip list
</ins><span class="cx"> * @see copy_dir()
</span><span class="cx"> *
</span><span class="cx"> * @param string $from source directory
</span><span class="lines">@@ -827,17 +876,9 @@
</span><span class="cx"> $from = trailingslashit($from);
</span><span class="cx"> $to = trailingslashit($to);
</span><span class="cx">
</span><del>- $skip_regex = '';
- foreach ( (array)$skip_list as $key => $skip_file )
- $skip_regex .= preg_quote($skip_file, '!') . '|';
-
- if ( !empty($skip_regex) )
- $skip_regex = '!(' . rtrim($skip_regex, '|') . ')$!i';
-
</del><span class="cx"> foreach ( (array) $dirlist as $filename => $fileinfo ) {
</span><del>- if ( !empty($skip_regex) )
- if ( preg_match($skip_regex, $from . $filename) )
- continue;
</del><ins>+ if ( in_array( $filename, $skip_list ) )
+ continue;
</ins><span class="cx">
</span><span class="cx"> if ( 'f' == $fileinfo['type'] ) {
</span><span class="cx"> if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
</span><span class="lines">@@ -851,7 +892,15 @@
</span><span class="cx"> if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
</span><span class="cx"> return new WP_Error('mkdir_failed', __('Could not create directory.'), $to . $filename);
</span><span class="cx"> }
</span><del>- $result = _copy_dir($from . $filename, $to . $filename, $skip_list);
</del><ins>+
+ // generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list
+ $sub_skip_list = array();
+ foreach ( $skip_list as $skip_item ) {
+ if ( 0 === strpos( $skip_item, $filename . '/' ) )
+ $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
+ }
+
+ $result = _copy_dir($from . $filename, $to . $filename, $sub_skip_list);
</ins><span class="cx"> if ( is_wp_error($result) )
</span><span class="cx"> return $result;
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunksrcwpadminincludesupdatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/update.php (25539 => 25540)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/update.php 2013-09-21 05:52:06 UTC (rev 25539)
+++ trunk/src/wp-admin/includes/update.php 2013-09-21 06:48:20 UTC (rev 25540)
</span><span class="lines">@@ -87,6 +87,68 @@
</span><span class="cx"> return $auto_update;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Gets and caches the checksums for the given versions of WordPress
+ *
+ * @since 3.7.0
+ *
+ * @param $version string|array A single version, or an array of versions to fetch
+ *
+ * @return bool|array False on failure, otherwise the array of checksums, keyed by version
+ */
+function get_core_checksums( $version ) {
+ if ( ! is_array( $version ) )
+ $version = array( $version );
+
+ $return = array();
+
+ // Check to see if we have cached copies available, if we do, no need to request them
+ foreach ( $version as $i => $v ) {
+ if ( $checksums = get_site_transient( "core_checksums_$v" ) ) {
+ unset( $version[ $i ] );
+ $return[ $v ] = $checksums;
+ }
+ }
+
+ // We had cached copies for all of the versions!
+ if ( empty( $version ) )
+ return $return;
+
+ $url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( array( 'version' => $version ), null, '&' );
+
+ if ( wp_http_supports( array( 'ssl' ) ) )
+ $url = set_url_scheme( $url, 'https' );
+
+ $options = array(
+ 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
+ );
+
+ $response = wp_remote_get( $url, $options );
+
+ if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
+ return false;
+
+ $body = trim( wp_remote_retrieve_body( $response ) );
+ $body = json_decode( $body, true );
+
+ if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
+ return false;
+
+ // Cache the checksums for later
+ foreach ( $version as $v ) {
+ set_site_transient( "core_checksums_$v", $body['checksums'][ $v ], HOUR_IN_SECONDS );
+ $return[ $v ] = $body['checksums'][ $v ];
+ }
+
+ // If the API didn't return anything for a version, explicitly set it's return value to false
+ foreach ( $return as $v => $r ) {
+ if ( empty( $r ) )
+ $return[ $v ] = false;
+ }
+
+ return $return;
+}
+
</ins><span class="cx"> function dismiss_core_update( $update ) {
</span><span class="cx"> $dismissed = get_site_option( 'dismissed_update_core' );
</span><span class="cx"> $dismissed[ $update->current . '|' . $update->locale ] = true;
</span></span></pre>
</div>
</div>
</body>
</html>