<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[13167] trunk/wp-admin: Detect plugin output on activation,
  as well as fatal errors.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13167">13167</a></dd>
<dt>Author</dt> <dd>markjaquith</dd>
<dt>Date</dt> <dd>2010-02-16 03:44:20 +0000 (Tue, 16 Feb 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Detect plugin output on activation, as well as fatal errors. props miqrogroove. fixes <a href="http://trac.wordpress.org/ticket/12089">#12089</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludespluginphp">trunk/wp-admin/includes/plugin.php</a></li>
<li><a href="#trunkwpadminpluginsphp">trunk/wp-admin/plugins.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludespluginphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/plugin.php (13166 => 13167)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/plugin.php        2010-02-16 00:21:06 UTC (rev 13166)
+++ trunk/wp-admin/includes/plugin.php        2010-02-16 03:44:20 UTC (rev 13167)
</span><span class="lines">@@ -358,7 +358,11 @@
</span><span class="cx">                 if ( !empty($redirect) )
</span><span class="cx">                         wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
</span><span class="cx">                 ob_start();
</span><del>-                @include(WP_PLUGIN_DIR . '/' . $plugin);
</del><ins>+                include(WP_PLUGIN_DIR . '/' . $plugin);
+                if ( ob_get_length() &gt; 0 ) {
+                        $output = ob_get_clean();
+                        return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
+                }
</ins><span class="cx">                 do_action( 'activate_plugin', trim( $plugin) );
</span><span class="cx">                 if ( $network_wide ) {
</span><span class="cx">                         $current[$plugin] = time();
</span></span></pre></div>
<a id="trunkwpadminpluginsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/plugins.php (13166 => 13167)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/plugins.php        2010-02-16 00:21:06 UTC (rev 13166)
+++ trunk/wp-admin/plugins.php        2010-02-16 03:44:20 UTC (rev 13167)
</span><span class="lines">@@ -50,8 +50,15 @@
</span><span class="cx">                         check_admin_referer('activate-plugin_' . $plugin);
</span><span class="cx"> 
</span><span class="cx">                         $result = activate_plugin($plugin, 'plugins.php?error=true&amp;plugin=' . $plugin, $network_wide);
</span><del>-                        if ( is_wp_error( $result ) )
-                                wp_die($result);
</del><ins>+                        if ( is_wp_error( $result ) ) {
+                                if ('unexpected_output' == $result-&gt;get_error_code()) {
+                                        $redirect = 'plugins.php?error=true&amp;charsout=' . strlen($result-&gt;get_error_data()) . '&amp;plugin=' . $plugin;
+                                        wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect));
+                                        exit;
+                                } else {
+                                        wp_die($result);
+                                }
+                        }
</ins><span class="cx"> 
</span><span class="cx">                         $recent = (array)get_option('recently_activated');
</span><span class="cx">                         if ( isset($recent[ $plugin ]) ) {
</span><span class="lines">@@ -140,10 +147,12 @@
</span><span class="cx">                         if ( is_wp_error($valid) )
</span><span class="cx">                                 wp_die($valid);
</span><span class="cx"> 
</span><del>-                        if ( defined('E_RECOVERABLE_ERROR') )
-                                error_reporting(E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
-                        else
-                                error_reporting(E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING);
</del><ins>+                        if ( ! WP_DEBUG ) {
+                                if ( defined('E_RECOVERABLE_ERROR') )
+                                        error_reporting(E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
+                                else
+                                        error_reporting(E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING);
+                        }
</ins><span class="cx"> 
</span><span class="cx">                         @ini_set('display_errors', true); //Ensure that Fatal errors are displayed.
</span><span class="cx">                         include(WP_PLUGIN_DIR . '/' . $plugin);
</span><span class="lines">@@ -157,7 +166,10 @@
</span><span class="cx">                         check_admin_referer('deactivate-plugin_' . $plugin);
</span><span class="cx">                         deactivate_plugins($plugin);
</span><span class="cx">                         update_option('recently_activated', array($plugin =&gt; time()) + (array)get_option('recently_activated'));
</span><del>-                        wp_redirect(&quot;plugins.php?deactivate=true&amp;plugin_status=$status&amp;paged=$page&quot;);
</del><ins>+                        if (headers_sent())
+                                echo &quot;&lt;meta http-equiv='refresh' content='&quot; . esc_attr( &quot;0;url=plugins.php?deactivate=true&amp;plugin_status=$status&amp;paged=$page&quot; ) . &quot;' /&gt;&quot;;
+                        else
+                                wp_redirect(&quot;plugins.php?deactivate=true&amp;plugin_status=$status&amp;paged=$page&quot;);
</ins><span class="cx">                         exit;
</span><span class="cx">                         break;
</span><span class="cx">                 case 'deactivate-selected':
</span><span class="lines">@@ -302,8 +314,15 @@
</span><span class="cx">                 echo '&lt;div id=&quot;message&quot; class=&quot;error&quot;&gt;&lt;p&gt;' . sprintf(__('The plugin &lt;code&gt;%s&lt;/code&gt; has been &lt;strong&gt;deactivated&lt;/strong&gt; due to an error: %s'), esc_html($plugin_file), $error-&gt;get_error_message()) . '&lt;/p&gt;&lt;/div&gt;';
</span><span class="cx"> ?&gt;
</span><span class="cx"> 
</span><del>-&lt;?php if ( isset($_GET['error']) ) : ?&gt;
-        &lt;div id=&quot;message&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php _e('Plugin could not be activated because it triggered a &lt;strong&gt;fatal error&lt;/strong&gt;.') ?&gt;&lt;/p&gt;
</del><ins>+&lt;?php if ( isset($_GET['error']) ) :
+
+        if (isset($_GET['charsout']))
+                $errmsg = sprintf(__('Plugin could not be activated because it generated %d characters of &lt;strong&gt;unexpected output&lt;/strong&gt;.'), $_GET['charsout']);
+        else
+                $errmsg = __('Plugin could not be activated because it triggered a &lt;strong&gt;fatal error&lt;/strong&gt;.');
+
+        ?&gt;
+        &lt;div id=&quot;message&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php echo $errmsg; ?&gt;&lt;/p&gt;
</ins><span class="cx">         &lt;?php
</span><span class="cx">                 if ( wp_verify_nonce($_GET['_error_nonce'], 'plugin-activation-error_' . $plugin) ) { ?&gt;
</span><span class="cx">         &lt;iframe style=&quot;border:0&quot; width=&quot;100%&quot; height=&quot;70px&quot; src=&quot;&lt;?php echo admin_url('plugins.php?action=error_scrape&amp;amp;plugin=' . esc_attr($plugin) . '&amp;amp;_wpnonce=' . esc_attr($_GET['_error_nonce'])); ?&gt;&quot;&gt;&lt;/iframe&gt;
</span></span></pre>
</div>
</div>

</body>
</html>