PluginProbe
WPIDE – File Manager & Code Editor / 2.0.2
WPIDE – File Manager & Code Editor v2.0.2
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / WPide.php

WPide.php in WPIDE – File Manager & Code Editor 2.0.2, at WPide.php

533 lines 18.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WPide
4 Plugin URI: https://github.com/WPsites/WPide
5 Description: WordPress code editor with auto completion of both WordPress and PHP functions with reference, syntax highlighting, line numbers, tabbed editing, automatic backup.
6 Version: 2.0.2
7 Author: Simon Dunton
8 Author URI: http://www.wpsites.co.uk
9 */
10
11
12
13 class WPide2
14
15 {
16
17 public $site_url, $plugin_url;
18
19 function __construct() {
20
21 //add WPide to the menu
22 add_action( 'admin_menu', array( &$this, 'add_my_menu_page' ) );
23
24 //hook for processing incoming image saves
25 if ( isset($_GET['wpide_save_image']) ){
26
27 //force local file method for testing - you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets'
28 define('FS_METHOD', 'direct');
29
30 add_action('admin_init', array($this, 'wpide_save_image'));
31
32 }
33
34 //only include this plugin if on theme editor, plugin editor or an ajax call
35 if ( $_SERVER['PHP_SELF'] === '/wp-admin/admin-ajax.php' ||
36 $_GET['page'] === 'wpide' ){
37
38 //force local file method for testing - you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets'
39 define('FS_METHOD', 'direct');
40
41 // Uncomment any of these calls to add the functionality that you need.
42 //add_action('admin_head', 'WPide2::add_admin_head');
43 add_action('admin_init', 'WPide2::add_admin_js');
44 add_action('admin_init', 'WPide2::add_admin_styles');
45
46 //setup jqueryFiletree list callback
47 add_action('wp_ajax_jqueryFileTree', 'WPide2::jqueryFileTree_get_list');
48 //setup ajax function to get file contents for editing
49 add_action('wp_ajax_wpide_get_file', 'WPide2::wpide_get_file' );
50 //setup ajax function to save file contents and do automatic backup if needed
51 add_action('wp_ajax_wpide_save_file', 'WPide2::wpide_save_file' );
52 //setup ajax function to create new item (folder, file etc)
53 add_action('wp_ajax_wpide_create_new', 'WPide2::wpide_create_new' );
54
55 //setup ajax function to create new item (folder, file etc)
56 add_action('wp_ajax_wpide_image_edit_key', 'WPide2::wpide_image_edit_key' );
57
58
59
60
61 }
62
63
64
65
66
67 $WPide->site_url = get_bloginfo('url');
68
69
70 }
71
72
73
74 public static function add_admin_head()
75 {
76
77 }
78
79
80
81
82
83
84 public static function add_admin_js(){
85
86 $plugin_path = plugin_dir_url( __FILE__ );
87 //include file tree
88 wp_enqueue_script('jquery-file-tree', plugins_url("jqueryFileTree.js", __FILE__ ) );
89 //include ace
90 wp_enqueue_script('ace', plugins_url("ace-0.2.0/src/ace.js", __FILE__ ) );
91 //include ace modes for css, javascript & php
92 wp_enqueue_script('ace-mode-css', $plugin_path . 'ace-0.2.0/src/mode-css.js');
93 wp_enqueue_script('ace-mode-javascript', $plugin_path . 'ace-0.2.0/src/mode-javascript.js');
94 wp_enqueue_script('ace-mode-php', $plugin_path . 'ace-0.2.0/src/mode-php.js');
95 //include ace theme
96 wp_enqueue_script('ace-theme', plugins_url("ace-0.2.0/src/theme-dawn.js", __FILE__ ) );//monokai is nice
97 // wordpress-completion tags
98 wp_enqueue_script('wpide-wordpress-completion', plugins_url("js/autocomplete.wordpress.js", __FILE__ ) );
99 // php-completion tags
100 wp_enqueue_script('wpide-php-completion', plugins_url("js/autocomplete.php.js", __FILE__ ) );
101 // load editor
102 wp_enqueue_script('wpide-load-editor', plugins_url("js/load-editor.js", __FILE__ ) );
103 // load autocomplete dropdown
104 wp_enqueue_script('wpide-dd', plugins_url("js/jquery.dd.js", __FILE__ ) );
105
106 // load jquery ui
107 wp_enqueue_script('jquery-ui', plugins_url("js/jquery-ui-1.8.20.custom.min.js", __FILE__ ), array('jquery'), '1.8.20');
108
109
110
111
112 }
113
114 public static function add_admin_styles(){
115
116 //main wpide styles
117 wp_register_style( 'wpide_style', plugins_url('wpide.css', __FILE__) );
118 wp_enqueue_style( 'wpide_style' );
119 //filetree styles
120 wp_register_style( 'wpide_filetree_style', plugins_url('jqueryFileTree.css', __FILE__) );
121 wp_enqueue_style( 'wpide_filetree_style' );
122 //autocomplete dropdown styles
123 wp_register_style( 'wpide_dd_style', plugins_url('dd.css', __FILE__) );
124 wp_enqueue_style( 'wpide_dd_style' );
125
126 //jquery ui styles
127 wp_register_style( 'wpide_jqueryui_style', plugins_url('css/flick/jquery-ui-1.8.20.custom.css', __FILE__) );
128 wp_enqueue_style( 'wpide_jqueryui_style' );
129
130
131 }
132
133
134
135 public static function jqueryFileTree_get_list() {
136 //check the user has the permissions
137 check_admin_referer('plugin-name-action_wpidenonce');
138 if ( !current_user_can('edit_themes') )
139 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
140
141 //setup wp_filesystem api
142 global $wp_filesystem;
143 if ( ! WP_Filesystem($creds) )
144 return false;
145
146 $_POST['dir'] = urldecode($_POST['dir']);
147 $root = WP_CONTENT_DIR;
148
149 if( $wp_filesystem->exists($root . $_POST['dir']) ) {
150 //$files = scandir($root . $_POST['dir']);
151 //print_r($files);
152 $files = $wp_filesystem->dirlist($root . $_POST['dir']);
153 //print_r($files);
154
155 echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
156 if( count($files) > 0 ) {
157
158 // All dirs
159 foreach( $files as $file => $file_info ) {
160 if( $file != '.' && $file != '..' && $file_info['type']=='d' ) {
161 echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "/\">" . htmlentities($file) . "</a></li>";
162 }
163 }
164 // All files
165 foreach( $files as $file => $file_info ) {
166 if( $file != '.' && $file != '..' && $file_info['type']!='d') {
167 $ext = preg_replace('/^.*\./', '', $file);
168 echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>";
169 }
170 }
171 }
172 //output toolbar for creating new file, folder etc
173 echo "<li class=\"create_new\"><a class='new_directory' title='Create a new directory here.' href=\"#\" rel=\"{type: 'directory', path: '" . htmlentities($_POST['dir']) . "'}\"></a> <a class='new_file' title='Create a new file here.' href=\"#\" rel=\"{type: 'file', path: '" . htmlentities($_POST['dir']) . "'}\"></a><br style='clear:both;' /></li>";
174 echo "</ul>";
175 }
176
177 die(); // this is required to return a proper result
178 }
179
180
181 public static function wpide_get_file() {
182 //check the user has the permissions
183 check_admin_referer('plugin-name-action_wpidenonce');
184 if ( !current_user_can('edit_themes') )
185 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
186
187 //setup wp_filesystem api
188 global $wp_filesystem;
189 if ( ! WP_Filesystem($creds) )
190 return false;
191
192
193 $root = WP_CONTENT_DIR;
194 $file_name = $root . stripslashes($_POST['filename']);
195 echo $wp_filesystem->get_contents($file_name);
196 die(); // this is required to return a proper result
197 }
198
199
200
201 public static function wpide_image_edit_key() {
202
203 //check the user has the permissions
204 check_admin_referer('plugin-name-action_wpidenonce');
205 if ( !current_user_can('edit_themes') )
206 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
207
208 //create a nonce based on the image path
209 echo wp_create_nonce( 'wpide_image_edit' . $_POST['file'] );
210
211 }
212
213 public static function wpide_create_new() {
214 //check the user has the permissions
215 check_admin_referer('plugin-name-action_wpidenonce');
216 if ( !current_user_can('edit_themes') )
217 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
218
219 //setup wp_filesystem api
220 global $wp_filesystem;
221 if ( ! WP_Filesystem($creds) )
222 return false;
223
224 $root = WP_CONTENT_DIR;
225
226 //check all required vars are passed
227 if (strlen($_POST['path'])>0 && strlen($_POST['type'])>0 && strlen($_POST['file'])>0){
228
229
230 $filename = sanitize_file_name( $_POST['file'] );
231 $path = $_POST['path'];
232
233 if ($_POST['type'] == "directory"){
234
235 $write_result = $wp_filesystem->mkdir($root . $path . $filename, FS_CHMOD_DIR);
236
237 if ($write_result){
238 die("1"); //created
239 }else{
240 echo "Problem creating directory" . $root . $path . $filename;
241 }
242
243 }else if ($_POST['type'] == "file"){
244
245 $write_result = $wp_filesystem->put_contents(
246 $root . $path . $filename,
247 ' ',
248 FS_CHMOD_FILE // predefined mode settings for WP files
249 );
250
251 if ($write_result){
252 die("1"); //created
253 }else{
254 echo "Problem creating file " . $root . $path . $filename;
255 }
256
257 }
258
259
260 //print_r($_POST);
261
262
263 }
264 echo "0";
265 die(); // this is required to return a proper result
266 }
267
268 public static function wpide_save_file() {
269 //check the user has the permissions
270 check_admin_referer('plugin-name-action_wpidenonce');
271 if ( !current_user_can('edit_themes') )
272 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
273
274 //setup wp_filesystem api
275 global $wp_filesystem;
276 if ( ! WP_Filesystem($creds) )
277 echo "Cannot initialise the WP file system API";
278
279 //save a copy of the file and create a backup just in case
280 $root = WP_CONTENT_DIR;
281 $file_name = $root . stripslashes($_POST['filename']);
282
283 //set backup filename
284 $backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH");
285 //create backup directory if not there
286 $new_file_info = pathinfo($backup_path);
287 if (!$wp_filesystem->is_dir($new_file_info['dirname'])) wp_mkdir_p( $new_file_info['dirname'] ); //should use the filesytem api here but there isn't a comparable command right now
288
289 //do backup
290 $wp_filesystem->copy( $file_name, $backup_path );
291
292 //save file
293 if( $wp_filesystem->put_contents( $file_name, stripslashes($_POST['content'])) ) {
294 $result = "success";
295 }
296
297 die($result); // this is required to return a proper result
298 }
299
300 public static function wpide_save_image() {
301
302 $filennonce = split("::", $_POST["opt"]); //file::nonce
303
304 //check the user has a valid nonce
305 //we are checking two variations of the nonce, one as-is and another that we have removed a trailing zero from
306 //this is to get around some sort of bug where a nonce generated on another page has a trailing zero and a nonce generated/checked here doesn't have the zero
307 if (! wp_verify_nonce( $filennonce[1], 'wpide_image_edit' . $filennonce[0]) &&
308 ! wp_verify_nonce( rtrim($filennonce[1], "0") , 'wpide_image_edit' . $filennonce[0])) {
309 die('Security check'); //die because both checks failed
310 }
311 //check the user has the permissions
312 if ( !current_user_can('edit_themes') )
313 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
314
315
316 $_POST['content'] = base64_decode($_POST["data"]); //image content
317 $_POST['filename'] = $filennonce[0]; //filename
318
319 //setup wp_filesystem api
320 global $wp_filesystem;
321
322 if ( ! WP_Filesystem($creds) )
323 echo "Cannot initialise the WP file system API";
324
325 //save a copy of the file and create a backup just in case
326 $root = WP_CONTENT_DIR;
327 $file_name = $root . stripslashes($_POST['filename']);
328
329 //set backup filename
330 $backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH");
331 //create backup directory if not there
332 $new_file_info = pathinfo($backup_path);
333 if (!$wp_filesystem->is_dir($new_file_info['dirname'])) wp_mkdir_p( $new_file_info['dirname'] ); //should use the filesytem api here but there isn't a comparable command right now
334
335 //do backup
336 $wp_filesystem->move( $file_name, $backup_path );
337
338
339 //save file
340 if( $wp_filesystem->put_contents( $file_name, $_POST['content']) ) {
341 $result = "success";
342 }
343
344 if ($result == "success"){
345 wp_die('<p>'.__('<strong>Image saved.</strong> <br />You may <a href="JavaScript:window.close();">close this window / tab</a>.').'</p>');
346 }else{
347 wp_die('<p>'.__('<strong>Problem saving image.</strong> <br /><a href="JavaScript:window.close();">Close this window / tab</a> and try editing the image again.').'</p>');
348 }
349 //print_r($_POST);
350
351
352 //return;
353 }
354
355
356 public function add_my_menu_page() {
357 //add_menu_page("wpide", "wpide","edit_themes", "wpidesettings", array( &$this, 'my_menu_page') );
358 add_menu_page('WPide', 'WPide', 'edit_themes', "wpide", array( &$this, 'my_menu_page' ));
359 }
360
361 public function my_menu_page() {
362 if ( !current_user_can('edit_themes') )
363 wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>');
364
365 $app_url = get_bloginfo('url'); //need to make this https if we are currently looking on the site using https (even though https for admin might not be forced it can still cause issues)
366 if (is_ssl()) $app_url = str_replace("http:", "https:", $app_url);
367
368 ?>
369 <script>
370
371 var wpide_app_path = "<?php echo plugin_dir_url( __FILE__ ); ?>";
372
373 function the_filetree() {
374 jQuery('#wpide_file_browser').fileTree({ script: ajaxurl }, function(parent, file) {
375
376 if ( jQuery(parent).hasClass("create_new") ){ //create new file/folder
377 //to create a new item we need to know the name of it so show input
378
379 var item = eval('('+file+')');
380
381 //hide all inputs just incase one is selected
382 jQuery(".new_item_inputs").hide();
383 //show the input form for this
384 jQuery("div.new_" + item.type).show();
385 jQuery("div.new_" + item.type + " input[name='new_" + item.type + "']").focus();
386 jQuery("div.new_" + item.type + " input[name='new_" + item.type + "']").attr("rel", file);
387
388
389 }else if ( jQuery(".wpide_tab[rel='"+file+"']").length > 0) { //focus existing tab
390 jQuery(".wpide_tab[sessionrel='"+ jQuery(".wpide_tab[rel='"+file+"']").attr("sessionrel") +"']").click();//focus the already open tab
391 }else{ //open file
392
393 var image_patern =new RegExp("(\.jpg|\.gif|\.png|\.bmp)");
394 if ( image_patern.test(file) ){
395 //it's an image so open it for editing
396
397 //using modal+iframe
398 if ("lets not" == "use the modal for now"){
399
400 var NewDialog = jQuery('<div id="MenuDialog">\
401 <iframe src="http://www.sumopaint.com/app/?key=ebcdaezjeojbfgih&target=<?php echo get_bloginfo('url') . "?action=wpide_image_save";?>&url=<?php echo get_bloginfo('url') . "/wp-content";?>' + file + '&title=Edit image&service=Save back to WPide" width="100%" height="600px"> </iframe>\
402 </div>');
403 NewDialog.dialog({
404 modal: true,
405 title: "title",
406 show: 'clip',
407 hide: 'clip',
408 width:'800',
409 height:'600'
410 });
411
412 }else{ //open in new tab/window
413
414 var data = { action: 'wpide_image_edit_key', file: file, _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val() };
415 var image_data = '';
416 jQuery.ajaxSetup({async:false}); //we need to wait until we get the response before opening the window
417 jQuery.post(ajaxurl, data, function(response) {
418
419 //with the response (which is a nonce), build the json data to pass to the image editor. The edit key (nonce) is only valid to edit this image
420 image_data = file+'::'+response;
421
422 });
423
424
425 window.open('http://www.sumopaint.com/app/?key=ebcdaezjeojbfgih&url=<?php echo $app_url. "/wp-content";?>' + file + '&opt=' + image_data + '&title=Edit image&service=Save back to WPide&target=<?php echo urlencode( $app_url . "/wp-admin/admin.php?wpide_save_image=yes" ) ;?>');
426
427 }
428
429 }else{
430 jQuery(parent).addClass('wait');
431
432 wpide_set_file_contents(file, function(){
433
434 //once file loaded remove the wait class/indicator
435 jQuery(parent).removeClass('wait');
436
437 });
438
439 jQuery('#filename').val(file);
440 }
441
442 }
443
444 });
445 }
446
447 jQuery(document).ready(function($) {
448 // Handler for .ready() called.
449 the_filetree() ;
450
451
452
453
454
455
456 });
457 </script>
458
459 <?php
460 $url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce');
461 if ( ! WP_Filesystem($creds) ) {
462 request_filesystem_credentials($url, '', true, false, null);
463 return;
464 }
465 ?>
466
467 <div id="poststuff" class="metabox-holder has-right-sidebar">
468
469 <div id="side-info-column" class="inner-sidebar">
470
471 <div id="wpide_info"><div id="wpide_info_content"></div> </div>
472
473 <div id="submitdiv" class="postbox ">
474 <h3 class="hndle"><span>Files</span></h3>
475 <div class="inside">
476 <div class="submitbox" id="submitpost">
477 <div id="minor-publishing">
478 </div>
479 <div id="major-publishing-actions">
480 <div id="wpide_file_browser"></div>
481 <br style="clear:both;" />
482 <div class="new_file new_item_inputs">
483 <label for="new_folder">File name</label><input class="has_data" name="new_file" type="text" rel="" value="" placeholder="Filename.ext" />
484 <a href="#" id="wpide_create_new_file" class="button-primary">CREATE</a>
485 </div>
486 <div class="new_directory new_item_inputs">
487 <label for="new_directory">Directory name</label><input class="has_data" name="new_directory" type="text" rel="" value="" placeholder="Filename.ext" />
488 <a href="#" id="wpide_create_new_directory" class="button-primary">CREATE</a>
489 </div>
490 <div class="clear"></div>
491 </div>
492 </div>
493 </div>
494 </div>
495
496
497 </div>
498
499 <div id="post-body">
500 <div id="wpide_toolbar" class="quicktags-toolbar">
501 <div id="wpide_toolbar_tabs"> </div>
502 <div id="dialog_window_minimized_container"></div>
503 </div>
504
505 <div id="wpide_toolbar_buttons">
506 <div id="wpide_message" class="error highlight"></div>
507 <a href="#"></a> <a href="#"></a> </div>
508
509
510 <div id='fancyeditordiv'></div>
511
512 <form id="wpide_save_container" action="" method="get">
513 <a href="#" id="wpide_save" class="button-primary">SAVE
514 FILE</a>
515 <input type="hidden" id="filename" name="filename" value="" />
516 <?php
517 if ( function_exists('wp_nonce_field') )
518 wp_nonce_field('plugin-name-action_wpidenonce');
519 ?>
520 </form>
521 </div>
522
523
524
525 </div>
526
527 <?php
528 }
529
530 }
531 add_action("init", create_function('', 'new WPide2();'));
532 ?>
533