<!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>[18618] trunk/wp-admin/includes/class-wp-upgrader.php: First slice of rejecting invalid Plugin and Theme zip uploads (Incompatible file structure, not containing a Plugin/Theme, etc).</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, #logmsg > ol { margin-left: 0; 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/18618">18618</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2011-08-28 15:02:48 +0000 (Sun, 28 Aug 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>First slice of rejecting invalid Plugin and Theme zip uploads (Incompatible file structure, not containing a Plugin/Theme, etc). See <a href="http://core.trac.wordpress.org/ticket/14999">#14999</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesclasswpupgraderphp">trunk/wp-admin/includes/class-wp-upgrader.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesclasswpupgraderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/class-wp-upgrader.php (18617 => 18618)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/class-wp-upgrader.php        2011-08-28 08:42:07 UTC (rev 18617)
+++ trunk/wp-admin/includes/class-wp-upgrader.php        2011-08-28 15:02:48 UTC (rev 18618)
</span><span class="lines">@@ -52,7 +52,7 @@
</span><span class="cx">                 $this->strings['installing_package'] = __('Installing the latest version&#8230;');
</span><span class="cx">                 $this->strings['folder_exists'] = __('Destination folder already exists.');
</span><span class="cx">                 $this->strings['mkdir_failed'] = __('Could not create directory.');
</span><del>-                $this->strings['bad_package'] = __('Incompatible Archive.');
</del><ins>+                $this->strings['incompatible_archive'] = __('The package is corrupt or not in the correct format.');
</ins><span class="cx">
</span><span class="cx">                 $this->strings['maintenance_start'] = __('Enabling Maintenance mode&#8230;');
</span><span class="cx">                 $this->strings['maintenance_end'] = __('Disabling Maintenance mode&#8230;');
</span><span class="lines">@@ -153,6 +153,9 @@
</span><span class="cx">
</span><span class="cx">                 if ( is_wp_error($result) ) {
</span><span class="cx">                         $wp_filesystem->delete($working_dir, true);
</span><ins>+                        if ( 'incompatible_archive' == $result->get_error_code() ) {
+                                return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
+                        }
</ins><span class="cx">                         return $result;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="lines">@@ -190,7 +193,7 @@
</span><span class="cx">                 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
</span><span class="cx">                         $source = trailingslashit($source) . trailingslashit($source_files[0]);
</span><span class="cx">                 elseif ( count($source_files) == 0 )
</span><del>-                        return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files?
</del><ins>+                        return new WP_Error('incompatible_archive', $this->strings['incompatible_archive']); //There are no files?
</ins><span class="cx">                 //else //Its only a single file, The upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
</span><span class="cx">
</span><span class="cx">                 //Hook ability to change the source file location..
</span><span class="lines">@@ -391,6 +394,8 @@
</span><span class="cx">                 $this->init();
</span><span class="cx">                 $this->install_strings();
</span><span class="cx">
</span><ins>+                add_filter('upgrader_source_selection', array(&$this, 'check_package') );
+
</ins><span class="cx">                 $this->run(array(
</span><span class="cx">                                         'package' => $package,
</span><span class="cx">                                         'destination' => WP_PLUGIN_DIR,
</span><span class="lines">@@ -399,6 +404,8 @@
</span><span class="cx">                                         'hook_extra' => array()
</span><span class="cx">                                         ));
</span><span class="cx">
</span><ins>+                remove_filter('upgrader_source_selection', array(&$this, 'check_package') );
+
</ins><span class="cx">                 if ( ! $this->result || is_wp_error($this->result) )
</span><span class="cx">                         return $this->result;
</span><span class="cx">
</span><span class="lines">@@ -533,6 +540,32 @@
</span><span class="cx">                 return $results;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        function check_package($source) {
+                global $wp_filesystem;
+
+                if ( is_wp_error($source) )
+                        return $source;
+
+                $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
+                if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, lets not prevent installation.
+                        return $source;
+
+                // Check the folder contains at least 1 valid plugin.
+                $plugins_found = false;
+                foreach ( glob( $working_directory . '*.php' ) as $file ) {
+                        $info = get_plugin_data($file, false, false);
+                        if ( !empty( $info['Name'] ) ) {
+                                $plugins_found = true;
+                                break;
+                        }
+                }
+
+                if ( ! $plugins_found )
+                        return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'] );
+
+                return $source;
+        }
+
</ins><span class="cx">         //return plugin info.
</span><span class="cx">         function plugin_info() {
</span><span class="cx">                 if ( ! is_array($this->result) )
</span><span class="lines">@@ -635,6 +668,8 @@
</span><span class="cx">                 $this->init();
</span><span class="cx">                 $this->install_strings();
</span><span class="cx">
</span><ins>+                add_filter('upgrader_source_selection', array(&$this, 'check_package') );
+
</ins><span class="cx">                 $options = array(
</span><span class="cx">                                                 'package' => $package,
</span><span class="cx">                                                 'destination' => WP_CONTENT_DIR . '/themes',
</span><span class="lines">@@ -644,6 +679,8 @@
</span><span class="cx">
</span><span class="cx">                 $this->run($options);
</span><span class="cx">
</span><ins>+                remove_filter('upgrader_source_selection', array(&$this, 'check_package') );
+
</ins><span class="cx">                 if ( ! $this->result || is_wp_error($this->result) )
</span><span class="cx">                         return $this->result;
</span><span class="cx">
</span><span class="lines">@@ -686,6 +723,10 @@
</span><span class="cx">
</span><span class="cx">                 $this->run($options);
</span><span class="cx">
</span><ins>+                remove_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
+                remove_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
+                remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
+
</ins><span class="cx">                 if ( ! $this->result || is_wp_error($this->result) )
</span><span class="cx">                         return $this->result;
</span><span class="cx">
</span><span class="lines">@@ -782,6 +823,30 @@
</span><span class="cx">                 return $results;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        function check_package($source) {
+                global $wp_filesystem;
+
+                if ( is_wp_error($source) )
+                        return $source;
+
+                // Check the folder contains a valid theme
+                $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
+                if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, lets not prevent installation.
+                        return $source;
+
+                if ( ! file_exists( $working_directory . 'style.css' ) ) // A proper archive should have a style.css file in the single subdirectory
+                        return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'] );
+
+                $info = get_theme_data( $working_directory . 'style.css' );
+                if ( empty($info['Name']) )
+                        return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'] );
+
+                if ( empty($info['Template']) && ! file_exists( $working_directory . 'index.php' ) ) // If no template is set, it must have at least an index.php to be legit.
+                        return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'] );
+
+                return $source;
+        }
+
</ins><span class="cx">         function current_before($return, $theme) {
</span><span class="cx">
</span><span class="cx">                 if ( is_wp_error($return) )
</span><span class="lines">@@ -797,6 +862,7 @@
</span><span class="cx">
</span><span class="cx">                 return $return;
</span><span class="cx">         }
</span><ins>+
</ins><span class="cx">         function current_after($return, $theme) {
</span><span class="cx">                 if ( is_wp_error($return) )
</span><span class="cx">                         return $return;
</span></span></pre>
</div>
</div>
</body>
</html>