| 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.1 |
| 7 |
Author: Simon @ WPsites |
| 8 |
Author URI: http://www.wpsites.co.uk |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 13 |
|
| 14 |
|
| 15 |
if ( !class_exists( 'wpide' ) ) : |
| 16 |
class wpide |
| 17 |
|
| 18 |
{ |
| 19 |
|
| 20 |
public $site_url, $plugin_url; |
| 21 |
|
| 22 |
/** |
| 23 |
* The main WPide loader (PHP4 compatable) |
| 24 |
* |
| 25 |
* @uses wpide::__construct() Setup the globals needed |
| 26 |
*/ |
| 27 |
public function wpide() { |
| 28 |
$this->__construct(); |
| 29 |
} |
| 30 |
|
| 31 |
function __construct() { |
| 32 |
|
| 33 |
//add WPide to the menu |
| 34 |
add_action( 'admin_menu', array( $this, 'add_my_menu_page' ) ); |
| 35 |
|
| 36 |
//hook for processing incoming image saves |
| 37 |
if ( isset($_GET['wpide_save_image']) ){ |
| 38 |
|
| 39 |
//force local file method for testing - you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets' |
| 40 |
$this->override_fs_method('direct'); |
| 41 |
|
| 42 |
add_action('admin_init', array( $this, 'wpide_save_image') ); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
//only include this plugin if on theme editor, plugin editor or an ajax call |
| 48 |
if ( (isset($_GET['page']) && $_GET['page'] === 'wpide') || |
| 49 |
preg_match('#admin-ajax\.php$#', $_SERVER['PHP_SELF']) ){ |
| 50 |
|
| 51 |
|
| 52 |
// force local file method until I've worked out how to implement the other methods |
| 53 |
// main problem being password wouldn't/isn't saved between requests |
| 54 |
// you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets' |
| 55 |
$this->override_fs_method('direct'); |
| 56 |
|
| 57 |
// Uncomment any of these calls to add the functionality that you need. |
| 58 |
add_action('admin_init', array( $this, 'add_admin_js' ) ); |
| 59 |
add_action('admin_init', array( $this, 'add_admin_styles' ) ); |
| 60 |
|
| 61 |
//setup jqueryFiletree list callback |
| 62 |
add_action('wp_ajax_jqueryFileTree', array( $this, 'jqueryFileTree_get_list' ) ); |
| 63 |
//setup ajax function to get file contents for editing |
| 64 |
add_action('wp_ajax_wpide_get_file', array( $this, 'wpide_get_file' ) ); |
| 65 |
//setup ajax function to save file contents and do automatic backup if needed |
| 66 |
add_action('wp_ajax_wpide_save_file', array( $this, 'wpide_save_file' ) ); |
| 67 |
//setup ajax function to create new item (folder, file etc) |
| 68 |
add_action('wp_ajax_wpide_create_new', array( $this, 'wpide_create_new' ) ); |
| 69 |
|
| 70 |
//setup ajax function to create new item (folder, file etc) |
| 71 |
add_action('wp_ajax_wpide_image_edit_key', array( $this, 'wpide_image_edit_key' ) ); |
| 72 |
|
| 73 |
//setup ajax function for startup to get some debug info, checking permissions etc |
| 74 |
add_action('wp_ajax_wpide_startup_check', array( $this, 'wpide_startup_check' ) ); |
| 75 |
|
| 76 |
//add a warning when navigating away from WPide |
| 77 |
//it has to go after WordPress scripts otherwise WP clears the binding |
| 78 |
add_action('admin_print_footer_scripts', array( $this, 'add_admin_nav_warning' ), 99 ); |
| 79 |
|
| 80 |
// Add body class to collapse the wp sidebar nav |
| 81 |
add_filter('admin_body_class', array( $this, 'hide_wp_sidebar_nav' ), 11); |
| 82 |
|
| 83 |
//hide the update nag |
| 84 |
add_action('admin_menu', array( $this, 'hide_wp_update_nag' )); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
$this->site_url = get_bloginfo('url'); |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public function override_fs_method($method = 'direct'){ |
| 99 |
|
| 100 |
|
| 101 |
if ( defined('FS_METHOD') ){ |
| 102 |
|
| 103 |
define('WPIDE_FS_METHOD_FORCED_ELSEWHERE', FS_METHOD); //make a note of the forced method |
| 104 |
|
| 105 |
}else{ |
| 106 |
|
| 107 |
define('FS_METHOD', $method); //force direct |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
public function hide_wp_sidebar_nav($classes) { |
| 115 |
|
| 116 |
return str_replace("auto-fold", "", $classes) . ' folded'; |
| 117 |
} |
| 118 |
|
| 119 |
public function hide_wp_update_nag() { |
| 120 |
remove_action( 'admin_notices', 'update_nag', 3 ); |
| 121 |
} |
| 122 |
|
| 123 |
public static function add_admin_nav_warning() |
| 124 |
{ |
| 125 |
?> |
| 126 |
<script type="text/javascript"> |
| 127 |
|
| 128 |
jQuery(document).ready(function($) { |
| 129 |
window.onbeforeunload = function() { |
| 130 |
return 'You are attempting to navigate away from WPide. Make sure you have saved any changes made to your files otherwise they will be forgotten.' ; |
| 131 |
} |
| 132 |
}); |
| 133 |
|
| 134 |
</script> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static function add_admin_js(){ |
| 144 |
|
| 145 |
$plugin_path = plugin_dir_url( __FILE__ ); |
| 146 |
//include file tree |
| 147 |
wp_enqueue_script('jquery-file-tree', plugins_url("jqueryFileTree.js", __FILE__ ) ); |
| 148 |
//include ace |
| 149 |
wp_enqueue_script('ace', plugins_url("ace-0.2.0/src/ace.js", __FILE__ ) ); |
| 150 |
//include ace modes for css, javascript & php |
| 151 |
wp_enqueue_script('ace-mode-css', $plugin_path . 'ace-0.2.0/src/mode-css.js'); |
| 152 |
wp_enqueue_script('ace-mode-javascript', $plugin_path . 'ace-0.2.0/src/mode-javascript.js'); |
| 153 |
wp_enqueue_script('ace-mode-php', $plugin_path . 'ace-0.2.0/src/mode-php.js'); |
| 154 |
//include ace theme |
| 155 |
wp_enqueue_script('ace-theme', plugins_url("ace-0.2.0/src/theme-dawn.js", __FILE__ ) );//monokai is nice |
| 156 |
// wordpress-completion tags |
| 157 |
wp_enqueue_script('wpide-wordpress-completion', plugins_url("js/autocomplete/wordpress.js", __FILE__ ) ); |
| 158 |
// php-completion tags |
| 159 |
wp_enqueue_script('wpide-php-completion', plugins_url("js/autocomplete/php.js", __FILE__ ) ); |
| 160 |
// load editor |
| 161 |
wp_enqueue_script('wpide-load-editor', plugins_url("js/load-editor.js", __FILE__ ) ); |
| 162 |
// load autocomplete dropdown |
| 163 |
wp_enqueue_script('wpide-dd', plugins_url("js/jquery.dd.js", __FILE__ ) ); |
| 164 |
|
| 165 |
// load jquery ui |
| 166 |
wp_enqueue_script('jquery-ui', plugins_url("js/jquery-ui-1.9.2.custom.min.js", __FILE__ ), array('jquery'), '1.9.2'); |
| 167 |
|
| 168 |
// load color picker |
| 169 |
wp_enqueue_script('ImageColorPicker', plugins_url("js/ImageColorPicker.js", __FILE__ ), array('jquery'), '0.3'); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
public static function add_admin_styles(){ |
| 176 |
|
| 177 |
//main wpide styles |
| 178 |
wp_register_style( 'wpide_style', plugins_url('wpide.css', __FILE__) ); |
| 179 |
wp_enqueue_style( 'wpide_style' ); |
| 180 |
//filetree styles |
| 181 |
wp_register_style( 'wpide_filetree_style', plugins_url('jqueryFileTree.css', __FILE__) ); |
| 182 |
wp_enqueue_style( 'wpide_filetree_style' ); |
| 183 |
//autocomplete dropdown styles |
| 184 |
wp_register_style( 'wpide_dd_style', plugins_url('dd.css', __FILE__) ); |
| 185 |
wp_enqueue_style( 'wpide_dd_style' ); |
| 186 |
|
| 187 |
//jquery ui styles |
| 188 |
wp_register_style( 'wpide_jqueryui_style', plugins_url('css/flick/jquery-ui-1.8.20.custom.css', __FILE__) ); |
| 189 |
wp_enqueue_style( 'wpide_jqueryui_style' ); |
| 190 |
|
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static function jqueryFileTree_get_list() { |
| 197 |
//check the user has the permissions |
| 198 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 199 |
if ( !current_user_can('edit_themes') ) |
| 200 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 201 |
|
| 202 |
//setup wp_filesystem api |
| 203 |
global $wp_filesystem; |
| 204 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 205 |
$form_fields = null; // for now, but at some point the login info should be passed in here |
| 206 |
if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields) ) ) { |
| 207 |
// no credentials yet, just produced a form for the user to fill in |
| 208 |
return true; // stop the normal page form from displaying |
| 209 |
} |
| 210 |
|
| 211 |
if ( ! WP_Filesystem($creds) ) |
| 212 |
return false; |
| 213 |
|
| 214 |
$_POST['dir'] = urldecode($_POST['dir']); |
| 215 |
$root = apply_filters( 'wpide_filesystem_root', WP_CONTENT_DIR ); |
| 216 |
|
| 217 |
if( $wp_filesystem->exists($root . $_POST['dir']) ) { |
| 218 |
//$files = scandir($root . $_POST['dir']); |
| 219 |
//print_r($files); |
| 220 |
$files = $wp_filesystem->dirlist($root . $_POST['dir']); |
| 221 |
//print_r($files); |
| 222 |
|
| 223 |
echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">"; |
| 224 |
if( count($files) > 0 ) { |
| 225 |
|
| 226 |
// All dirs |
| 227 |
foreach( $files as $file => $file_info ) { |
| 228 |
if( $file != '.' && $file != '..' && $file_info['type']=='d' ) { |
| 229 |
echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "/\">" . htmlentities($file) . "</a></li>"; |
| 230 |
} |
| 231 |
} |
| 232 |
// All files |
| 233 |
foreach( $files as $file => $file_info ) { |
| 234 |
if( $file != '.' && $file != '..' && $file_info['type']!='d') { |
| 235 |
$ext = preg_replace('/^.*\./', '', $file); |
| 236 |
echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>"; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
//output toolbar for creating new file, folder etc |
| 241 |
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>"; |
| 242 |
echo "</ul>"; |
| 243 |
} |
| 244 |
|
| 245 |
die(); // this is required to return a proper result |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
public static function wpide_get_file() { |
| 250 |
//check the user has the permissions |
| 251 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 252 |
if ( !current_user_can('edit_themes') ) |
| 253 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 254 |
|
| 255 |
//setup wp_filesystem api |
| 256 |
global $wp_filesystem; |
| 257 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 258 |
$form_fields = null; // for now, but at some point the login info should be passed in here |
| 259 |
if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields) ) ) { |
| 260 |
// no credentials yet, just produced a form for the user to fill in |
| 261 |
return true; // stop the normal page form from displaying |
| 262 |
} |
| 263 |
if ( ! WP_Filesystem($creds) ) |
| 264 |
return false; |
| 265 |
|
| 266 |
|
| 267 |
$root = apply_filters( 'wpide_filesystem_root', WP_CONTENT_DIR ); |
| 268 |
$file_name = $root . stripslashes($_POST['filename']); |
| 269 |
echo $wp_filesystem->get_contents($file_name); |
| 270 |
die(); // this is required to return a proper result |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public static function wpide_image_edit_key() { |
| 276 |
|
| 277 |
//check the user has the permissions |
| 278 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 279 |
if ( !current_user_can('edit_themes') ) |
| 280 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 281 |
|
| 282 |
//create a nonce based on the image path |
| 283 |
echo wp_create_nonce( 'wpide_image_edit' . $_POST['file'] ); |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
public static function wpide_create_new() { |
| 288 |
//check the user has the permissions |
| 289 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 290 |
if ( !current_user_can('edit_themes') ) |
| 291 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 292 |
|
| 293 |
//setup wp_filesystem api |
| 294 |
global $wp_filesystem; |
| 295 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 296 |
$form_fields = null; // for now, but at some point the login info should be passed in here |
| 297 |
if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields) ) ) { |
| 298 |
// no credentials yet, just produced a form for the user to fill in |
| 299 |
return true; // stop the normal page form from displaying |
| 300 |
} |
| 301 |
if ( ! WP_Filesystem($creds) ) |
| 302 |
return false; |
| 303 |
|
| 304 |
$root = apply_filters( 'wpide_filesystem_root', WP_CONTENT_DIR ); |
| 305 |
|
| 306 |
//check all required vars are passed |
| 307 |
if (strlen($_POST['path'])>0 && strlen($_POST['type'])>0 && strlen($_POST['file'])>0){ |
| 308 |
|
| 309 |
|
| 310 |
$filename = sanitize_file_name( $_POST['file'] ); |
| 311 |
$path = $_POST['path']; |
| 312 |
|
| 313 |
if ($_POST['type'] == "directory"){ |
| 314 |
|
| 315 |
$write_result = $wp_filesystem->mkdir($root . $path . $filename, FS_CHMOD_DIR); |
| 316 |
|
| 317 |
if ($write_result){ |
| 318 |
die("1"); //created |
| 319 |
}else{ |
| 320 |
echo "Problem creating directory" . $root . $path . $filename; |
| 321 |
} |
| 322 |
|
| 323 |
}else if ($_POST['type'] == "file"){ |
| 324 |
|
| 325 |
//write the file |
| 326 |
$write_result = $wp_filesystem->put_contents( |
| 327 |
$root . $path . $filename, |
| 328 |
'', |
| 329 |
FS_CHMOD_FILE // predefined mode settings for WP files |
| 330 |
); |
| 331 |
|
| 332 |
if ($write_result){ |
| 333 |
die("1"); //created |
| 334 |
}else{ |
| 335 |
echo "Problem creating file " . $root . $path . $filename; |
| 336 |
} |
| 337 |
|
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
//print_r($_POST); |
| 342 |
|
| 343 |
|
| 344 |
} |
| 345 |
echo "0"; |
| 346 |
die(); // this is required to return a proper result |
| 347 |
} |
| 348 |
|
| 349 |
public static function wpide_save_file() { |
| 350 |
//check the user has the permissions |
| 351 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 352 |
if ( !current_user_can('edit_themes') ) |
| 353 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 354 |
|
| 355 |
//check file syntax of PHP files by parsing the PHP |
| 356 |
if ( preg_match("#\.php$#i", $_POST['filename']) ){ |
| 357 |
|
| 358 |
require('PHP-Parser/lib/bootstrap.php'); |
| 359 |
ini_set('xdebug.max_nesting_level', 2000); |
| 360 |
|
| 361 |
$code = stripslashes($_POST['content']); |
| 362 |
|
| 363 |
$parser = new PHPParser_Parser(new PHPParser_Lexer); |
| 364 |
|
| 365 |
try { |
| 366 |
$stmts = $parser->parse($code); |
| 367 |
} catch (PHPParser_Error $e) { |
| 368 |
echo 'Parse Error: ', $e->getMessage(); |
| 369 |
die(); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
//setup wp_filesystem api |
| 374 |
global $wp_filesystem; |
| 375 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 376 |
$form_fields = null; // for now, but at some point the login info should be passed in here |
| 377 |
if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields) ) ) { |
| 378 |
// no credentials yet, just produced a form for the user to fill in |
| 379 |
return true; // stop the normal page form from displaying |
| 380 |
} |
| 381 |
if ( ! WP_Filesystem($creds) ) |
| 382 |
echo "Cannot initialise the WP file system API"; |
| 383 |
|
| 384 |
//save a copy of the file and create a backup just in case |
| 385 |
$root = apply_filters( 'wpide_filesystem_root', WP_CONTENT_DIR ); |
| 386 |
$file_name = $root . stripslashes($_POST['filename']); |
| 387 |
|
| 388 |
//set backup filename |
| 389 |
$backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH"); |
| 390 |
//create backup directory if not there |
| 391 |
$new_file_info = pathinfo($backup_path); |
| 392 |
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 |
| 393 |
|
| 394 |
//do backup |
| 395 |
$wp_filesystem->copy( $file_name, $backup_path ); |
| 396 |
|
| 397 |
//save file |
| 398 |
if( $wp_filesystem->put_contents( $file_name, stripslashes($_POST['content'])) ) { |
| 399 |
$result = "success"; |
| 400 |
} |
| 401 |
|
| 402 |
die($result); // this is required to return a proper result |
| 403 |
} |
| 404 |
|
| 405 |
public static function wpide_save_image() { |
| 406 |
|
| 407 |
$filennonce = split("::", $_POST["opt"]); //file::nonce |
| 408 |
|
| 409 |
//check the user has a valid nonce |
| 410 |
//we are checking two variations of the nonce, one as-is and another that we have removed a trailing zero from |
| 411 |
//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 |
| 412 |
if (! wp_verify_nonce( $filennonce[1], 'wpide_image_edit' . $filennonce[0]) && |
| 413 |
! wp_verify_nonce( rtrim($filennonce[1], "0") , 'wpide_image_edit' . $filennonce[0])) { |
| 414 |
die('Security check'); //die because both checks failed |
| 415 |
} |
| 416 |
//check the user has the permissions |
| 417 |
if ( !current_user_can('edit_themes') ) |
| 418 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 419 |
|
| 420 |
|
| 421 |
$_POST['content'] = base64_decode($_POST["data"]); //image content |
| 422 |
$_POST['filename'] = $filennonce[0]; //filename |
| 423 |
|
| 424 |
//setup wp_filesystem api |
| 425 |
global $wp_filesystem; |
| 426 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 427 |
$form_fields = null; // for now, but at some point the login info should be passed in here |
| 428 |
if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields) ) ) { |
| 429 |
// no credentials yet, just produced a form for the user to fill in |
| 430 |
return true; // stop the normal page form from displaying |
| 431 |
} |
| 432 |
if ( ! WP_Filesystem($creds) ) |
| 433 |
echo "Cannot initialise the WP file system API"; |
| 434 |
|
| 435 |
//save a copy of the file and create a backup just in case |
| 436 |
$root = apply_filters( 'wpide_filesystem_root', WP_CONTENT_DIR ); |
| 437 |
$file_name = $root . stripslashes($_POST['filename']); |
| 438 |
|
| 439 |
//set backup filename |
| 440 |
$backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH"); |
| 441 |
//create backup directory if not there |
| 442 |
$new_file_info = pathinfo($backup_path); |
| 443 |
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 |
| 444 |
|
| 445 |
//do backup |
| 446 |
$wp_filesystem->move( $file_name, $backup_path ); |
| 447 |
|
| 448 |
|
| 449 |
//save file |
| 450 |
if( $wp_filesystem->put_contents( $file_name, $_POST['content']) ) { |
| 451 |
$result = "success"; |
| 452 |
} |
| 453 |
|
| 454 |
if ($result == "success"){ |
| 455 |
wp_die('<p>'.__('<strong>Image saved.</strong> <br />You may <a href="JavaScript:window.close();">close this window / tab</a>.').'</p>'); |
| 456 |
}else{ |
| 457 |
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>'); |
| 458 |
} |
| 459 |
//print_r($_POST); |
| 460 |
|
| 461 |
|
| 462 |
//return; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
public static function wpide_startup_check() { |
| 467 |
global $wp_filesystem, $wp_version; |
| 468 |
|
| 469 |
echo "\n\n\n\nWPIDE STARTUP CHECKS \n"; |
| 470 |
echo "___________________ \n\n"; |
| 471 |
|
| 472 |
//WordPress version |
| 473 |
if ($wp_version > 3){ |
| 474 |
echo "WordPress version = " . $wp_version . "\n\n"; |
| 475 |
}else{ |
| 476 |
echo "WordPress version = " . $wp_version . " (which is too old to run WPide) \n\n"; |
| 477 |
} |
| 478 |
|
| 479 |
//check the user has the permissions |
| 480 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 481 |
if ( !current_user_can('edit_themes') ) |
| 482 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 483 |
|
| 484 |
if ( defined( 'WPIDE_FS_METHOD_FORCED_ELSEWHERE' ) ){ |
| 485 |
echo "WordPress filesystem API has been forced to use the " . WPIDE_FS_METHOD_FORCED . " method by another plugin/WordPress. \n\n"; |
| 486 |
} |
| 487 |
|
| 488 |
//setup wp_filesystem api |
| 489 |
$wpide_filesystem_before = $wp_filesystem; |
| 490 |
|
| 491 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 492 |
$form_fields = null; // for now, but at some point the login info should be passed in here |
| 493 |
ob_start(); |
| 494 |
if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields) ) ) { |
| 495 |
// if we get here, then we don't have credentials yet, |
| 496 |
// but have just produced a form for the user to fill in, |
| 497 |
// so stop processing for now |
| 498 |
//return true; // stop the normal page form from displaying |
| 499 |
} |
| 500 |
ob_end_clean(); |
| 501 |
if ( ! WP_Filesystem($creds) ) { |
| 502 |
|
| 503 |
echo "There has been a problem initialising the filesystem API \n\n"; |
| 504 |
echo "Filesystem API before this plugin ran: \n\n" . print_r($wpide_filesystem_before, true); |
| 505 |
echo "Filesystem API now: \n\n" . print_r($wp_filesystem, true); |
| 506 |
|
| 507 |
} |
| 508 |
unset($wpide_filesystem_before); |
| 509 |
|
| 510 |
|
| 511 |
$root = apply_filters( 'wpide_filesystem_root', WP_CONTENT_DIR ); |
| 512 |
if ( isset($wp_filesystem) ){ |
| 513 |
|
| 514 |
//Running webservers user and group |
| 515 |
echo "Web server user/group = " . getenv('APACHE_RUN_USER') . ":" . getenv('APACHE_RUN_GROUP') . "\n"; |
| 516 |
//wp-content user and group |
| 517 |
echo "wp-content owner/group = " . $wp_filesystem->owner( $root ) . ":" . $wp_filesystem->group( $root ) . "\n\n"; |
| 518 |
|
| 519 |
|
| 520 |
//check we can list wp-content files |
| 521 |
if( $wp_filesystem->exists( $root ) ){ |
| 522 |
|
| 523 |
$files = $wp_filesystem->dirlist( $root ); |
| 524 |
if ( count($files) > 0){ |
| 525 |
echo "wp-content folder exists and contains ". count($files) ." files \n"; |
| 526 |
}else{ |
| 527 |
echo "wp-content folder exists but we cannot read it's contents \n"; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
// $wp_filesystem->owner() $wp_filesystem->group() $wp_filesystem->is_writable() $wp_filesystem->is_readable() |
| 532 |
echo "\nUsing the ".$wp_filesystem->method." method of the WP filesystem API\n"; |
| 533 |
|
| 534 |
//wp-content editable? |
| 535 |
echo "The wp-content folder ". ( $wp_filesystem->is_readable( $root )==1 ? "IS":"IS NOT" ) ." readable and ". ( $wp_filesystem->is_writable( $root )==1 ? "IS":"IS NOT" ) ." writable by this method \n"; |
| 536 |
|
| 537 |
|
| 538 |
//plugins folder editable |
| 539 |
echo "The wp-content/plugins folder ". ( $wp_filesystem->is_readable( $root."/plugins" )==1 ? "IS":"IS NOT" ) ." readable and ". ( $wp_filesystem->is_writable( $root."/plugins" )==1 ? "IS":"IS NOT" ) ." writable by this method \n"; |
| 540 |
|
| 541 |
|
| 542 |
//themes folder editable |
| 543 |
echo "The wp-content/themes folder ". ( $wp_filesystem->is_readable( $root."/themes" )==1 ? "IS":"IS NOT" ) ." readable and ". ( $wp_filesystem->is_writable( $root."/themes" )==1 ? "IS":"IS NOT" ) ." writable by this method \n"; |
| 544 |
|
| 545 |
} |
| 546 |
|
| 547 |
echo "___________________ \n\n\n\n"; |
| 548 |
|
| 549 |
echo " If the file tree to the right is empty there is a possibility that your server permissions are not compatible with this plugin. \n The startup information above may shed some light on things. \n Paste that information into the support forum for further assistance."; |
| 550 |
|
| 551 |
|
| 552 |
die(); |
| 553 |
|
| 554 |
//set backup filename |
| 555 |
$backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH"); |
| 556 |
//create backup directory if not there |
| 557 |
$new_file_info = pathinfo($backup_path); |
| 558 |
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 |
| 559 |
|
| 560 |
//do backup |
| 561 |
$wp_filesystem->move( $file_name, $backup_path ); |
| 562 |
|
| 563 |
|
| 564 |
//save file |
| 565 |
if( $wp_filesystem->put_contents( $file_name, $_POST['content']) ) { |
| 566 |
$result = "success"; |
| 567 |
} |
| 568 |
|
| 569 |
if ($result == "success"){ |
| 570 |
wp_die('<p>'.__('<strong>Image saved.</strong> <br />You may <a href="JavaScript:window.close();">close this window / tab</a>.').'</p>'); |
| 571 |
}else{ |
| 572 |
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>'); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
//return; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
public function add_my_menu_page() { |
| 583 |
//add_menu_page("wpide", "wpide","edit_themes", "wpidesettings", array( &$this, 'my_menu_page') ); |
| 584 |
add_menu_page('WPide', 'WPide', 'edit_themes', "wpide", array( &$this, 'my_menu_page' )); |
| 585 |
} |
| 586 |
|
| 587 |
public function my_menu_page() { |
| 588 |
if ( !current_user_can('edit_themes') ) |
| 589 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 590 |
|
| 591 |
$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) |
| 592 |
if (is_ssl()) $app_url = str_replace("http:", "https:", $app_url); |
| 593 |
|
| 594 |
?> |
| 595 |
<script> |
| 596 |
|
| 597 |
var wpide_app_path = "<?php echo plugin_dir_url( __FILE__ ); ?>"; |
| 598 |
//dont think this is needed any more.. var wpide_file_root_url = "<?php echo apply_filters("wpide_file_root_url", WP_CONTENT_URL );?>"; |
| 599 |
|
| 600 |
function the_filetree() { |
| 601 |
jQuery('#wpide_file_browser').fileTree({ script: ajaxurl }, function(parent, file) { |
| 602 |
|
| 603 |
if ( jQuery(parent).hasClass("create_new") ){ //create new file/folder |
| 604 |
//to create a new item we need to know the name of it so show input |
| 605 |
|
| 606 |
var item = eval('('+file+')'); |
| 607 |
|
| 608 |
//hide all inputs just incase one is selected |
| 609 |
jQuery(".new_item_inputs").hide(); |
| 610 |
//show the input form for this |
| 611 |
jQuery("div.new_" + item.type).show(); |
| 612 |
jQuery("div.new_" + item.type + " input[name='new_" + item.type + "']").focus(); |
| 613 |
jQuery("div.new_" + item.type + " input[name='new_" + item.type + "']").attr("rel", file); |
| 614 |
|
| 615 |
|
| 616 |
}else if ( jQuery(".wpide_tab[rel='"+file+"']").length > 0) { //focus existing tab |
| 617 |
jQuery(".wpide_tab[sessionrel='"+ jQuery(".wpide_tab[rel='"+file+"']").attr("sessionrel") +"']").click();//focus the already open tab |
| 618 |
}else{ //open file |
| 619 |
|
| 620 |
var image_patern =new RegExp("(\.jpg|\.gif|\.png|\.bmp)"); |
| 621 |
if ( image_patern.test(file) ){ |
| 622 |
//it's an image so open it for editing |
| 623 |
|
| 624 |
//using modal+iframe |
| 625 |
if ("lets not" == "use the modal for now"){ |
| 626 |
|
| 627 |
var NewDialog = jQuery('<div id="MenuDialog">\ |
| 628 |
<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>\ |
| 629 |
</div>'); |
| 630 |
NewDialog.dialog({ |
| 631 |
modal: true, |
| 632 |
title: "title", |
| 633 |
show: 'clip', |
| 634 |
hide: 'clip', |
| 635 |
width:'800', |
| 636 |
height:'600' |
| 637 |
}); |
| 638 |
|
| 639 |
}else{ //open in new tab/window |
| 640 |
|
| 641 |
var data = { action: 'wpide_image_edit_key', file: file, _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val() }; |
| 642 |
var image_data = ''; |
| 643 |
jQuery.ajaxSetup({async:false}); //we need to wait until we get the response before opening the window |
| 644 |
jQuery.post(ajaxurl, data, function(response) { |
| 645 |
|
| 646 |
//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 |
| 647 |
image_data = file+'::'+response; |
| 648 |
|
| 649 |
}); |
| 650 |
|
| 651 |
jQuery.ajaxSetup({async:true});//enable async again |
| 652 |
|
| 653 |
|
| 654 |
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" ) ;?>'); |
| 655 |
|
| 656 |
} |
| 657 |
|
| 658 |
}else{ |
| 659 |
jQuery(parent).addClass('wait'); |
| 660 |
|
| 661 |
wpide_set_file_contents(file, function(){ |
| 662 |
|
| 663 |
//once file loaded remove the wait class/indicator |
| 664 |
jQuery(parent).removeClass('wait'); |
| 665 |
|
| 666 |
}); |
| 667 |
|
| 668 |
jQuery('#filename').val(file); |
| 669 |
} |
| 670 |
|
| 671 |
} |
| 672 |
|
| 673 |
}); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
jQuery(document).ready(function($) { |
| 679 |
|
| 680 |
$("#fancyeditordiv").css("height", ($('body').height()-120) + 'px' ); |
| 681 |
|
| 682 |
// Handler for .ready() called. |
| 683 |
the_filetree() ; |
| 684 |
|
| 685 |
//inialise the color assist |
| 686 |
$("#wpide_color_assist img").ImageColorPicker({ |
| 687 |
afterColorSelected: function(event, color){ |
| 688 |
jQuery("#wpide_color_assist_input").val(color); |
| 689 |
} |
| 690 |
}); |
| 691 |
$("#wpide_color_assist").hide(); //hide it until it's needed |
| 692 |
|
| 693 |
$("#wpide_color_assist_send").click(function(e){ |
| 694 |
e.preventDefault(); |
| 695 |
editor.insert( jQuery("#wpide_color_assist_input").val().replace('#', '') ); |
| 696 |
|
| 697 |
$("#wpide_color_assist").hide(); //hide it until it's needed again |
| 698 |
}); |
| 699 |
|
| 700 |
$(".close_color_picker a").click(function(e){ |
| 701 |
e.preventDefault(); |
| 702 |
$("#wpide_color_assist").hide(); //hide it until it's needed again |
| 703 |
}); |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
}); |
| 709 |
</script> |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
<div id="poststuff" class="metabox-holder has-right-sidebar"> |
| 714 |
|
| 715 |
<div id="side-info-column" class="inner-sidebar"> |
| 716 |
|
| 717 |
<div id="wpide_info"> |
| 718 |
<div id="wpide_info_content"></div> |
| 719 |
</div> |
| 720 |
<br style="clear:both;" /> |
| 721 |
<div id="wpide_color_assist"> |
| 722 |
<div class="close_color_picker"><a href="close-color-picker">x</a></div> |
| 723 |
<h3>Colour Assist</h3> |
| 724 |
<img src='<?php echo plugins_url("images/color-wheel.png", __FILE__ ); ?>' /> |
| 725 |
<input type="button" class="button" id="wpide_color_assist_send" value="< Send to editor" /> |
| 726 |
<input type="text" id="wpide_color_assist_input" name="wpide_color_assist_input" value="" /> |
| 727 |
|
| 728 |
</div> |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
<div id="submitdiv" class="postbox "> |
| 733 |
<h3 class="hndle"><span>Files</span></h3> |
| 734 |
<div class="inside"> |
| 735 |
<div class="submitbox" id="submitpost"> |
| 736 |
<div id="minor-publishing"> |
| 737 |
</div> |
| 738 |
<div id="major-publishing-actions"> |
| 739 |
<div id="wpide_file_browser"></div> |
| 740 |
<br style="clear:both;" /> |
| 741 |
<div class="new_file new_item_inputs"> |
| 742 |
<label for="new_folder">File name</label><input class="has_data" name="new_file" type="text" rel="" value="" placeholder="Filename.ext" /> |
| 743 |
<a href="#" id="wpide_create_new_file" class="button-primary">CREATE</a> |
| 744 |
</div> |
| 745 |
<div class="new_directory new_item_inputs"> |
| 746 |
<label for="new_directory">Directory name</label><input class="has_data" name="new_directory" type="text" rel="" value="" placeholder="Filename.ext" /> |
| 747 |
<a href="#" id="wpide_create_new_directory" class="button-primary">CREATE</a> |
| 748 |
</div> |
| 749 |
<div class="clear"></div> |
| 750 |
</div> |
| 751 |
</div> |
| 752 |
</div> |
| 753 |
</div> |
| 754 |
|
| 755 |
|
| 756 |
</div> |
| 757 |
|
| 758 |
<div id="post-body"> |
| 759 |
<div id="wpide_toolbar" class="quicktags-toolbar"> |
| 760 |
<div id="wpide_toolbar_tabs"> </div> |
| 761 |
<div id="dialog_window_minimized_container"></div> |
| 762 |
</div> |
| 763 |
|
| 764 |
<div id="wpide_toolbar_buttons"> |
| 765 |
<div id="wpide_message" class="error highlight"></div> |
| 766 |
<a href="#"></a> <a href="#"></a> </div> |
| 767 |
|
| 768 |
|
| 769 |
<div id='fancyeditordiv'></div> |
| 770 |
|
| 771 |
<form id="wpide_save_container" action="" method="get"> |
| 772 |
<a href="#" id="wpide_save" alt="Keyboard shortcut to save [Ctrl/Cmd + S]" title="Keyboard shortcut to save [Ctrl/Cmd + S]" class="button-primary">SAVE |
| 773 |
FILE</a> |
| 774 |
<input type="hidden" id="filename" name="filename" value="" /> |
| 775 |
<?php |
| 776 |
if ( function_exists('wp_nonce_field') ) |
| 777 |
wp_nonce_field('plugin-name-action_wpidenonce'); |
| 778 |
?> |
| 779 |
</form> |
| 780 |
</div> |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
</div> |
| 785 |
|
| 786 |
<?php |
| 787 |
} |
| 788 |
|
| 789 |
} |
| 790 |
|
| 791 |
$wpide = new wpide(); |
| 792 |
|
| 793 |
endif; // class_exists check |
| 794 |
|
| 795 |
?> |
| 796 |
|