Ticket #3254: prevent_plugin_fatal_errors.002.diff

File prevent_plugin_fatal_errors.002.diff, 3.4 kB (added by markjaquith, 2 years ago)

Part II. Protects against fatal errors when editing an active plugin

  • wp-admin/plugin-editor.php

    old new  
    3030                $f = fopen($real_file, 'w+'); 
    3131                fwrite($f, $newcontent); 
    3232                fclose($f); 
     33 
     34                // Deactivate so we can test it. 
     35                $current = get_option('active_plugins'); 
     36                if ( in_array($file, $current) ) { 
     37                        array_splice($current, array_search( $file, $current), 1 ); // Array-fu! 
     38                        update_option('active_plugins', $current); 
     39                        wp_redirect(add_query_arg('_wpnonce', wp_create_nonce('edit-plugin-test_' . $file), "plugin-editor.php?file=$file&liveupdate=1")); 
     40                        exit(); 
     41                } 
    3342                wp_redirect("plugin-editor.php?file=$file&a=te"); 
    3443        } else { 
    3544                wp_redirect("plugin-editor.php?file=$file"); 
     
    4453        if ( !current_user_can('edit_plugins') ) 
    4554                wp_die('<p>'.__('You do not have sufficient permissions to edit plugins for this blog.').'</p>'); 
    4655 
     56        if ( $_GET['liveupdate'] ) { 
     57                check_admin_referer('edit-plugin-test_' . $file); 
     58                $current = get_option('active_plugins'); 
     59                $plugin = $file; 
     60                if ( validate_file($plugin) ) 
     61                        wp_die(__('Invalid plugin.')); 
     62                if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) ) 
     63                        wp_die(__('Plugin file does not exist.')); 
     64                if (!in_array($plugin, $current)) { 
     65                        wp_redirect("plugin-editor.php?file=$file&phperror=1"); // we'll override this later if the plugin can be included without fatal error 
     66                        @include(ABSPATH . PLUGINDIR . '/' . $plugin); 
     67                        $current[] = $plugin; 
     68                        sort($current); 
     69                        update_option('active_plugins', $current); 
     70                } 
     71                wp_redirect("plugin-editor.php?file=$file&a=te"); 
     72        } 
     73 
    4774        require_once('admin-header.php'); 
    4875 
    4976        update_recently_edited(PLUGINDIR . "/$file"); 
     
    6087        ?> 
    6188<?php if (isset($_GET['a'])) : ?> 
    6289 <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div> 
     90<?php elseif (isset($_GET['phperror'])) : ?> 
     91 <div id="message" class="updated fade"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p></div> 
    6392<?php endif; ?> 
    6493 <div class="wrap"> 
    6594        <?php 
    66         if (is_writeable($real_file)) { 
    67                 echo '<h2>' . sprintf(__('Editing <strong>%s</strong>'), $file) . '</h2>'; 
     95        if ( in_array($file, (array) get_option('active_plugins')) ) { 
     96                if (is_writeable($real_file)) { 
     97                        echo '<h2>' . sprintf(__('Editing <strong>%s</strong> (active)'), $file) . '</h2>'; 
     98                } else { 
     99                echo '<h2>' . sprintf(__('Browsing <strong>%s</strong> (active)'), $file) . '</h2>'; 
     100                } 
    68101        } else { 
    69                 echo '<h2>' . sprintf(__('Browsing <strong>%s</strong>'), $file) . '</h2>'; 
     102                if (is_writeable($real_file)) { 
     103                        echo '<h2>' . sprintf(__('Editing <strong>%s</strong> (inactive)'), $file) . '</h2>'; 
     104                } else { 
     105                echo '<h2>' . sprintf(__('Browsing <strong>%s</strong> (inactive)'), $file) . '</h2>'; 
     106                } 
    70107        } 
    71108        ?> 
    72109        <div id="templateside"> 
     
    90127                <input type="hidden" name="file" value="<?php echo $file ?>" /> 
    91128                </div> 
    92129<?php if ( is_writeable($real_file) ) : ?> 
     130        <?php if ( in_array($file, (array) get_option('active_plugins')) ) { ?> 
     131                <p><?php _e('<strong>Warning:</strong> Making changes to active plugins is not recommended.  If your changes cause a fatal error, the plugin will be automatically deactivated.'); ?></p> 
     132        <?php } ?> 
    93133        <p class="submit"> 
    94134        <?php 
    95135                echo "<input type='submit' name='submit' value='        " . __('Update File &raquo;') . "' tabindex='2' />";