Make WordPress Core

Ticket #11974: plugin-editor.php

File plugin-editor.php, 8.6 KB (added by cwgservices, 17 years ago)

plugin editor with patch for unitialized variable $file

Line 
1<?php
2/**
3 * Edit plugin editor administration panel.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/** WordPress Administration Bootstrap */
10require_once('admin.php');
11
12if ( !current_user_can('edit_plugins') )
13 wp_die('<p>'.__('You do not have sufficient permissions to edit plugins for this blog.').'</p>');
14
15$title = __("Edit Plugins");
16$parent_file = 'plugins.php';
17
18wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'plugin'));
19
20wp_admin_css( 'theme-editor' );
21
22$plugins = get_plugins();
23
24if ( isset($_REQUEST['file']) )
25 $plugin = stripslashes($_REQUEST['file']);
26
27if ( empty($plugin) ) {
28 $plugin = array_keys($plugins);
29 $plugin = $plugin[0];
30}
31
32$plugin_files = get_plugin_files($plugin);
33
34// cwg patch to skip test of uninitialized variable
35/*
36if ( empty($file) )
37 $file = $plugin_files[0];
38else
39 $file = stripslashes($file);
40*/
41 $file = $plugin_files[0];
42// end cwg patch to skip test of uninitialized variable
43
44$file = validate_file_to_edit($file, $plugin_files);
45$real_file = WP_PLUGIN_DIR . '/' . $file;
46$scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
47
48switch ( $action ) {
49
50case 'update':
51
52 check_admin_referer('edit-plugin_' . $file);
53
54 $newcontent = stripslashes($_POST['newcontent']);
55 if ( is_writeable($real_file) ) {
56 $f = fopen($real_file, 'w+');
57 fwrite($f, $newcontent);
58 fclose($f);
59
60 // Deactivate so we can test it.
61 if ( is_plugin_active($file) || isset($_POST['phperror']) ) {
62 if ( is_plugin_active($file) )
63 deactivate_plugins($file, true);
64 wp_redirect(add_query_arg('_wpnonce', wp_create_nonce('edit-plugin-test_' . $file), "plugin-editor.php?file=$file&liveupdate=1&scrollto=$scrollto"));
65 exit;
66 }
67 wp_redirect("plugin-editor.php?file=$file&a=te&scrollto=$scrollto");
68 } else {
69 wp_redirect("plugin-editor.php?file=$file&scrollto=$scrollto");
70 }
71 exit;
72
73break;
74
75default:
76
77 if ( isset($_GET['liveupdate']) ) {
78 check_admin_referer('edit-plugin-test_' . $file);
79
80 $error = validate_plugin($file);
81 if ( is_wp_error($error) )
82 wp_die( $error );
83
84 if ( ! is_plugin_active($file) )
85 activate_plugin($file, "plugin-editor.php?file=$file&phperror=1"); // we'll override this later if the plugin can be included without fatal error
86
87 wp_redirect("plugin-editor.php?file=$file&a=te&scrollto=$scrollto");
88 exit;
89 }
90
91 // List of allowable extensions
92 $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
93 $editable_extensions = (array) apply_filters('editable_extensions', $editable_extensions);
94
95 if ( ! is_file($real_file) ) {
96 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
97 } else {
98 // Get the extension of the file
99 if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
100 $ext = strtolower($matches[1]);
101 // If extension is not in the acceptable list, skip it
102 if ( !in_array( $ext, $editable_extensions) )
103 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
104 }
105 }
106
107 require_once('admin-header.php');
108
109 update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
110
111 $content = file_get_contents( $real_file );
112
113 if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
114 $functions = wp_doc_link_parse( $content );
115
116 if ( !empty($functions) ) {
117 $docs_select = '<select name="docs-list" id="docs-list">';
118 $docs_select .= '<option value="">' . __( 'Function Name...' ) . '</option>';
119 foreach ( $functions as $function) {
120 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . htmlspecialchars( $function ) . '()</option>';
121 }
122 $docs_select .= '</select>';
123 }
124 }
125
126 $content = htmlspecialchars( $content );
127 $codepress_lang = codepress_get_lang($real_file);
128
129 ?>
130<?php if (isset($_GET['a'])) : ?>
131 <div id="message" class="updated fade"><p><?php _e('File edited successfully.') ?></p></div>
132<?php elseif (isset($_GET['phperror'])) : ?>
133 <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>
134 <?php
135 if ( wp_verify_nonce($_GET['_error_nonce'], 'plugin-activation-error_' . $file) ) { ?>
136 <iframe style="border:0" width="100%" height="70px" src="<?php bloginfo('wpurl'); ?>/wp-admin/plugins.php?action=error_scrape&amp;plugin=<?php echo esc_attr($file); ?>&amp;_wpnonce=<?php echo esc_attr($_GET['_error_nonce']); ?>"></iframe>
137 <?php } ?>
138</div>
139<?php endif; ?>
140<div class="wrap">
141<?php screen_icon(); ?>
142<h2><?php echo esc_html( $title ); ?></h2>
143
144<div class="fileedit-sub">
145<div class="alignleft">
146<big><?php
147 if ( is_plugin_active($plugin) ) {
148 if ( is_writeable($real_file) )
149 echo sprintf(__('Editing <strong>%s</strong> (active)'), $file);
150 else
151 echo sprintf(__('Browsing <strong>%s</strong> (active)'), $file);
152 } else {
153 if ( is_writeable($real_file) )
154 echo sprintf(__('Editing <strong>%s</strong> (inactive)'), $file);
155 else
156 echo sprintf(__('Browsing <strong>%s</strong> (inactive)'), $file);
157 }
158 ?></big>
159</div>
160<div class="alignright">
161 <form action="plugin-editor.php" method="post">
162 <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
163 <select name="plugin" id="plugin">
164<?php
165 foreach ( $plugins as $plugin_key => $a_plugin ) {
166 $plugin_name = $a_plugin['Name'];
167 if ( $plugin_key == $plugin )
168 $selected = " selected='selected'";
169 else
170 $selected = '';
171 $plugin_name = esc_attr($plugin_name);
172 $plugin_key = esc_attr($plugin_key);
173 echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
174 }
175?>
176 </select>
177 <input type="submit" name="Submit" value="<?php esc_attr_e('Select') ?>" class="button" />
178 </form>
179</div>
180<br class="clear" />
181</div>
182
183<div id="templateside">
184 <h3><?php _e('Plugin Files'); ?></h3>
185
186 <ul>
187<?php
188foreach ( $plugin_files as $plugin_file ) :
189 // Get the extension of the file
190 if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
191 $ext = strtolower($matches[1]);
192 // If extension is not in the acceptable list, skip it
193 if ( !in_array( $ext, $editable_extensions ) )
194 continue;
195 } else {
196 // No extension found
197 continue;
198 }
199?>
200 <li<?php echo $file == $plugin_file ? ' class="highlight"' : ''; ?>><a href="plugin-editor.php?file=<?php echo $plugin_file; ?>&amp;plugin=<?php echo $plugin; ?>"><?php echo $plugin_file ?></a></li>
201<?php endforeach; ?>
202 </ul>
203</div>
204<form name="template" id="template" action="plugin-editor.php" method="post">
205 <?php wp_nonce_field('edit-plugin_' . $file) ?>
206 <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1" class="codepress <?php echo $codepress_lang ?>"><?php echo $content ?></textarea>
207 <input type="hidden" name="action" value="update" />
208 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
209 <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
210 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
211 </div>
212 <?php if ( !empty( $docs_select ) ) : ?>
213 <div id="documentation"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Lookup' ) ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'http://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( $wp_version ) ?>&amp;redirect=true'); }" /></div>
214 <?php endif; ?>
215<?php if ( is_writeable($real_file) ) : ?>
216 <?php if ( in_array($file, (array) get_option('active_plugins')) ) { ?>
217 <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>
218 <?php } ?>
219 <p class="submit">
220 <?php
221 if ( isset($_GET['phperror']) )
222 echo "<input type='hidden' name='phperror' value='1' /><input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File and Attempt to Reactivate') . "' tabindex='2' />";
223 else
224 echo "<input type='submit' name='submit' class='button-primary' value='" . esc_attr__('Update File') . "' tabindex='2' />";
225 ?>
226 </p>
227<?php else : ?>
228 <p><em><?php _e('You need to make this file writable before you can save your changes. See <a href="http://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.'); ?></em></p>
229<?php endif; ?>
230</form>
231<br class="clear" />
232</div>
233<script type="text/javascript">
234/* <![CDATA[ */
235jQuery(document).ready(function($){
236 $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
237 $('#newcontent').scrollTop( $('#scrollto').val() );
238});
239/* ]]> */
240</script>
241<?php
242 break;
243}
244include("admin-footer.php");