get_plugin_version(); // add WPide to the menu add_action('admin_menu', array($this, 'add_my_menu_page')); // hook for processing incoming image saves if (is_admin() && isset($_GET['wpide_save_image'])) { // force local file method for testing - you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets' $this->override_fs_method('direct'); add_action('admin_init', array($this, 'wpide_save_image')); } add_action('admin_init', array($this, 'setup_hooks')); $this->site_url = get_bloginfo('url'); } public function override_fs_method($method = 'direct') { if (defined('FS_METHOD')) { define('WPIDE_FS_METHOD_FORCED_ELSEWHERE', FS_METHOD); //make a note of the forced method } else { define('FS_METHOD', $method); //force direct } } function is_plugin_page() { $current_screen = get_current_screen(); if ($current_screen->id === $this->menu_hook) { return true; } else { return false; } } // get plugin version from header function get_plugin_version() { $plugin_data = get_file_data(__FILE__, array('version' => 'Version'), 'plugin'); $this->version = $plugin_data['version']; return $plugin_data['version']; } // get_plugin_version public function setup_hooks() { // force local file method until I've worked out how to implement the other methods // main problem being password wouldn't/isn't saved between requests // you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets' $this->override_fs_method('direct'); // Will only enqueue on WPide page add_action('admin_print_scripts-' . $this->menu_hook, array($this, 'add_admin_js')); add_action('admin_print_styles-' . $this->menu_hook, array($this, 'add_admin_styles')); add_action('admin_print_footer_scripts', array($this, 'print_find_dialog')); //setup jqueryFiletree list callback add_action('wp_ajax_jqueryFileTree', array($this, 'jqueryFileTree_get_list')); //setup ajax function to get file contents for editing add_action('wp_ajax_wpide_get_file', array($this, 'wpide_get_file')); //setup ajax function to save file contents and do automatic backup if needed add_action('wp_ajax_wpide_save_file', array($this, 'wpide_save_file')); //setup ajax function to rename file/folder add_action('wp_ajax_wpide_rename_file', array($this, 'wpide_rename_file')); //setup ajax function to delete file/folder add_action('wp_ajax_wpide_delete_file', array($this, 'wpide_delete_file')); //setup ajax function to handle upload add_action('wp_ajax_wpide_upload_file', array($this, 'wpide_upload_file')); //setup ajax function to handle download add_action('wp_ajax_wpide_download_file', array($this, 'wpide_download_file')); //setup ajax function to unzip file add_action('wp_ajax_wpide_unzip_file', array($this, 'wpide_unzip_file')); //setup ajax function to zip file add_action('wp_ajax_wpide_zip_file', array($this, 'wpide_zip_file')); //setup ajax function to create new item (folder, file etc) add_action('wp_ajax_wpide_create_new', array($this, 'wpide_create_new')); //setup ajax function to create new item (folder, file etc) add_action('wp_ajax_wpide_image_edit_key', array($this, 'wpide_image_edit_key')); //setup ajax function for startup to get some debug info, checking permissions etc add_action('wp_ajax_wpide_startup_check', array($this, 'wpide_startup_check')); //add a warning when navigating away from WPide //it has to go after WordPress scripts otherwise WP clears the binding // This has been implemented in load-editor.js // todo: // add_action('admin_print_footer_scripts', array( $this, 'add_admin_nav_warning' ), 99); // Add body class to collapse the wp sidebar nav add_filter('admin_body_class', array($this, 'hide_wp_sidebar_nav'), 11); //hide the update nag add_action('admin_menu', array($this, 'hide_wp_update_nag')); add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links') ); add_filter('admin_footer_text', array($this, 'admin_footer_text')); } // add settings link to plugins page function plugin_action_links($links) { $settings_link = 'Manage Files'; array_unshift($links, $settings_link); return $links; } // plugin_action_links // additional powered by text in admin footer; only on WPide page function admin_footer_text($text) { if (!$this->is_plugin_page()) { return $text; } $text = 'WPide v' . $this->version . ' by XplodedThemes. Please rate the plugin ★★★★★ to help us spread the word. Thank you!'; return $text; } // admin_footer_text public function hide_wp_sidebar_nav($classes) { global $hook_suffix; if (!$this->is_plugin_page()) { return $classes; } if (apply_filters('wpide_sidebar_folded', $hook_suffix === $this->menu_hook)) { return str_replace("auto-fold", "", $classes) . ' folded'; } } public function hide_wp_update_nag() { if (!$this->is_plugin_page()) { return; } remove_action('admin_notices', 'update_nag', 3); } public function add_admin_nav_warning() { ?> You do not have sufficient permissions to edit files for this site.

'); //setup wp_filesystem api global $wp_filesystem; $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields))) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) return false; $_POST['dir'] = urldecode($_POST['dir']); $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); if ($wp_filesystem->exists($root . $_POST['dir'])) { $files = $wp_filesystem->dirlist($root . $_POST['dir']); echo ""; } die(); // this is required to return a proper result } public function wpide_get_file() { //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('edit_themes')) wp_die('

You do not have sufficient permissions to edit files for this site.

'); //setup wp_filesystem api global $wp_filesystem; $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields))) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) return false; $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); echo $wp_filesystem->get_contents($file_name); die(); // this is required to return a proper result } public function wpide_image_edit_key() { //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('edit_themes')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } //create a nonce based on the image path echo wp_create_nonce('wpide_image_edit' . $_POST['file']); } public function wpide_create_new() { //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('edit_themes')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } //setup wp_filesystem api global $wp_filesystem; $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields))) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) return false; $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); //check all required vars are passed if (strlen($_POST['path']) > 0 && strlen($_POST['type']) > 0 && strlen($_POST['file']) > 0) { $filename = $_POST['file']; $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0)); $filename = preg_replace("#\x{00a0}#siu", ' ', $filename); $filename = str_replace($special_chars, '', $filename); $filename = str_replace(array('%20', '+'), '-', $filename); $filename = preg_replace('/[\r\n\t -]+/', '-', $filename); $path = $_POST['path']; if ($_POST['type'] == "directory") { $write_result = $wp_filesystem->mkdir($root . $path . $filename, FS_CHMOD_DIR); if ($write_result) { die("1"); //created } else { echo "Problem creating directory" . $root . $path . $filename; } } else if ($_POST['type'] == "file") { //write the file $write_result = $wp_filesystem->put_contents( $root . $path . $filename, '', FS_CHMOD_FILE // predefined mode settings for WP files ); if ($write_result) { die("1"); //created } else { echo "Problem creating file " . $root . $path . $filename; } } //print_r($_POST); } echo "0"; die(); // this is required to return a proper result } public function wpide_save_file() { //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('edit_themes')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $is_php = false; /* * Check file syntax of PHP files by parsing the PHP * If a site is running low on memory this PHP parser library could well tip memory usage over the edge * Especially if you are editing a large PHP file. * Might be worth either making this syntax check optional or it only running if memory is available. * Symptoms: no response on file save, and errors in your log like "Fatal error: Allowed memory size of 8388608 bytes exhausted…" */ if (preg_match("#\.php$#i", $_POST['filename'])) { $is_php = true; require('PHP-Parser/lib/bootstrap.php'); ini_set('xdebug.max_nesting_level', 2000); $code = stripslashes($_POST['content']); $parser = new PHPParser_Parser(new PHPParser_Lexer); try { $stmts = $parser->parse($code); } catch (PHPParser_Error $e) { echo 'Parse Error: ', $e->getMessage(); die(); } } //setup wp_filesystem api global $wp_filesystem; $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields))) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; //save a copy of the file and create a backup just in case $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); //set backup filename $backup_path = 'backups' . preg_replace("#\.php$#i", "_" . date("Y-m-d-H") . ".php", $_POST['filename']); $backup_path_full = plugin_dir_path(__FILE__) . $backup_path; //create backup directory if not there $new_file_info = pathinfo($backup_path_full); 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 if ($is_php) { //create the backup file adding some php to the file to enable direct restore global $current_user; get_currentuserinfo(); $user_md5 = md5(serialize($current_user)); $restore_php = '#s", "", file_get_contents("' . $backup_path_full . '") ) ) ){ echo "Your file has been restored, overwritting the recently edited file! \n\n The active editor still contains the broken or unwanted code. If you no longer need that content then close the tab and start fresh with the restored file."; } }else{ echo "-1"; } die(); /* end WPide restore code */ ?>'; file_put_contents($backup_path_full, $restore_php . file_get_contents($file_name)); } else { //do normal backup $wp_filesystem->copy($file_name, $backup_path_full); } //save file if ($wp_filesystem->put_contents($file_name, stripslashes($_POST['content']))) { //lets create an extra long nonce to make it less crackable global $current_user; get_currentuserinfo(); $user_md5 = md5(serialize($current_user)); $result = "\"" . $backup_path . ":::" . $user_md5 . "\""; } die($result); // this is required to return a proper result } public function wpide_rename_file() { global $wp_filesystem; //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('manage_options')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here $creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields); if (false === $creds) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); $new_name = dirname($file_name) . '/' . stripslashes($_POST['newname']); if (!$wp_filesystem->exists($file_name)) { echo 'The target file doesn\'t exist!'; exit; } if ($wp_filesystem->exists($new_name)) { echo 'The destination file exists!'; exit; } // Move instead of rename $renamed = $wp_filesystem->move($file_name, $new_name); if (!$renamed) { echo 'The file could not be renamed!'; } exit; } public function wpide_delete_file() { global $wp_filesystem; //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('manage_options')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here $creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields); if (false === $creds) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); if (!$wp_filesystem->exists($file_name)) { echo 'The file doesn\'t exist!'; exit; } $deleted = $wp_filesystem->delete($file_name); if (!$deleted) { echo 'The file couldn\'t be deleted.'; } exit; } public function wpide_upload_file() { global $wp_filesystem; //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('manage_options')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here $creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields); if (false === $creds) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $destination_folder = $root . stripslashes($_POST['destination']); foreach ($_FILES as $file) { if (!is_uploaded_file($file['tmp_name'])) { continue; } $destination = $destination_folder . $file['name']; if ($wp_filesystem->exists($destination)) { exit($file['name'] . ' already exists!'); } if (!$wp_filesystem->move($file['tmp_name'], $destination)) { exit($file['name'] . ' could not be moved.'); } if (!$wp_filesystem->chmod($destination)) { exit($file['name'] . ' could not be chmod.'); } } exit; } public function wpide_download_file() { global $wp_filesystem; //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('manage_options')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here $creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields); if (false === $creds) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); if (!$wp_filesystem->exists($file_name)) { echo 'The file doesn\'t exist!'; exit; } header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: filename="' . basename($file_name) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); echo $wp_filesystem->get_contents($file_name); exit; } public function wpide_zip_file() { global $wp_filesystem; //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('manage_options')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here $creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields); if (false === $creds) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; if (!$wp_filesystem->exists($file_name)) { echo 'Error: target file does not exist!'; exit; } $ext = '.zip'; switch (apply_filters('wpide_compression_method', 'zip')) { case 'gz': $ext = '.tar.gz'; break; case 'tar': $ext = '.tar'; break; case 'b2z': $ext = '.b2z'; break; case 'zip': $ext = '.zip'; break; } // Unzip a file to its current directory. if ($wp_filesystem->is_dir($file_name)) { $output_path = dirname($file_name) . '/' . basename($file_name) . $ext; } else { $output_path = $file_name; $output_path = strstr($file_name, '.', true) . $ext; } $zipped = self::do_zip_file($file_name, $output_path); if (is_wp_error($zipped)) { printf('%s: %s', $zipped->get_error_code(), $zipped->get_error_message()); exit; } exit; } protected function do_zip_file($file, $to) { // Unzip can use a lot of memory, but not this much hopefully @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT)); $method = apply_filters('wpide_compression_method', 'zip'); switch ($method) { case 'gz': case 'tar': if (class_exists('PharData') && apply_filters('unzip_file_use_phardata', true)) { exit('yes'); return self::_zip_archive_phardata($file, $to); } else { exit('figure it out'); } /* if ( $method === 'gz' ) { $gz = gzopen( $to ); } */ break; case 'b2z': exit('B2Z!'); case 'zip': default: if ($method !== 'zip') { trigger_error(sprintf('"%s" is not a valid compression mechanism.', $method)); } if (class_exists('ZipArchive') && apply_filters('unzip_file_use_ziparchive', true)) { return self::_zip_file_ziparchive($file, $to); } else { // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. return self::_zip_file_pclzip($file, $to); } break; } } protected static function _zip_file_ziparchive($file, $to) { $z = new ZipArchive; $opened = $z->open($to, ZipArchive::CREATE); if ($opened !== true) { switch ($opened) { case ZipArchive::ER_EXISTS: return new WP_Error( 'ZipArchive Error', 'File already exists', ZipArchive::ER_EXISTS ); case ZipArchive::ER_INCONS: return new WP_Error( 'ZipArchive Error', 'Archive inconsistent', ZipArchive::ER_INCONS ); case ZipArchive::ER_INVAL: return new WP_Error( 'ZipArchive Error', 'Invalid argument', ZipArchive::ER_INVAL ); case ZipArchive::ER_MEMORY: return new WP_Error( 'ZipArchive Error', 'Malloc failure', ZipArchive::ER_MEMORY ); case ZipArchive::ER_NOENT: return new WP_Error( 'ZipArchive Error', 'No such file.', ZipArchive::ER_NOENT ); case ZipArchive::ER_NOZIP: return new WP_Error( 'ZipArchive Error', 'Not a zip archive.', ZipArchive::ER_NOZIP ); case ZipArchive::ER_OPEN: return new WP_Error( 'ZipArchive Error', 'Can\'t open file.', ZipArchive::ER_OPEN ); case ZipArchive::ER_READ: return new WP_Error( 'ZipArchive Error', 'Read Error', ZipArchive::ER_READ ); case ZipArchive::ER_SEEK: return new WP_Error( 'ZipArchive Error', 'Seek Error', ZipArchive::ER_SEEK ); default: return new WP_Error( 'ZipArchive Error', 'Unknown Error', $opened ); break; } } if (is_dir($file)) { $base = dirname($file); $file = untrailingslashit($file); $z = self::_zip_folder_ziparchive($base, $file, $to, $z); if (is_wp_error($z)) return $z; } else { $z->addFile($file, basename($file)); } $z->close(); return true; } protected static function _zip_folder_ziparchive($zip_base, $folder, $to, $z) { $handle = opendir($folder); while (1) { $file = readdir($handle); if (false === $file) break; if (($file != '.') && ($file != '..')) { $filePath = "$folder/$file"; $filePathRel = str_replace($zip_base, '', $filePath); if ($filePathRel[0] === '/') $filePathRel = substr($filePathRel, 1); if (is_file($filePath)) { $z->addFile($filePath, $filePathRel); } elseif (is_dir($filePath)) { // Add sub-directory. $z->addEmptyDir($filePathRel); self::_zip_folder_ziparchive($zip_base, $filePath, $to, $z); } } } closedir($handle); return $z; } protected static function _zip_file_pclzip($file, $to) { require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); $pz = new PclZip($to); $created = $pz->create($file, PCLZIP_OPT_REMOVE_PATH, dirname($file)); if (!$created) { return new WP_Error('PclZip Error', $pz->errorInfo(true)); } return true; } protected static function _zip_file_phardata($file, $to) { $p = new PharData($to); if (is_dir($file)) { $p->buildFromDirectory($file); } else { $p->addFile($file, basename($file)); } return true; } public function wpide_unzip_file() { global $wp_filesystem; //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('manage_options')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here $creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields); if (false === $creds) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; if (!$wp_filesystem->exists($file_name)) { echo 'Error: Extraction path doesn\'t exist!'; exit; } $unzipped = self::do_unzip_file($file_name, dirname($file_name)); if (is_wp_error($unzipped)) { printf('%s: %s', $unzipped->get_error_code(), $unzipped->get_error_message()); exit; } exit; } protected function do_unzip_file($from, $to) { if (!file_exists($from)) { return new WP_Error('file-missing', 'Archive missing.'); } $fp = fopen($from, 'rb'); $bytes = fread($fp, 2); fclose($fp); switch ($bytes) { case "\37\213": // gz case 'BZ': return new WP_Error('unimplemented', 'That method is not yet implemented.'); break; case 'PK': return unzip_file($from, $to); default: return new WP_Error('unknown', 'Unknown archive type'); } } public function wpide_save_image() { $filennonce = split("::", $_POST["opt"]); //file::nonce //check the user has a valid nonce //we are checking two variations of the nonce, one as-is and another that we have removed a trailing zero from //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 if ( !wp_verify_nonce($filennonce[1], 'wpide_image_edit' . $filennonce[0]) && !wp_verify_nonce(rtrim($filennonce[1], "0"), 'wpide_image_edit' . $filennonce[0]) ) { die('Security check'); //die because both checks failed } //check the user has the permissions if (!current_user_can('edit_themes')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $_POST['content'] = base64_decode($_POST["data"]); //image content $_POST['filename'] = $filennonce[0]; //filename //setup wp_filesystem api global $wp_filesystem; $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields))) { // no credentials yet, just produced a form for the user to fill in return true; // stop the normal page form from displaying } if (!WP_Filesystem($creds)) echo "Cannot initialise the WP file system API"; //save a copy of the file and create a backup just in case $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); $file_name = $root . stripslashes($_POST['filename']); //set backup filename $backup_path = 'backups' . preg_replace("#\.php$#i", "_" . date("Y-m-d-H") . ".php", $_POST['filename']); $backup_path = plugin_dir_path(__FILE__) . $backup_path; //create backup directory if not there $new_file_info = pathinfo($backup_path); 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 //do backup $wp_filesystem->move($file_name, $backup_path); //save file if ($wp_filesystem->put_contents($file_name, $_POST['content'])) { $result = "success"; } if ($result == "success") { wp_die('

' . __('Image saved.
You may close this window / tab.') . '

'); } else { wp_die('

' . __('Problem saving image.
Close this window / tab and try editing the image again.') . '

'); } //print_r($_POST); } public function wpide_startup_check() { global $wp_filesystem, $wp_version; echo "\n\n\n\nWPIDE STARTUP CHECKS \n"; echo "___________________ \n\n"; // WordPress version if ($wp_version > 5) { echo "WordPress version = " . $wp_version . "\n\n"; } else { echo "WordPress version = " . $wp_version . " (which is too old to run WPide) \n\n"; } //check the user has the permissions check_admin_referer('plugin-name-action_wpidenonce'); if (!current_user_can('edit_themes')) wp_die('

' . __('You do not have sufficient permissions to edit templates for this site. SORRY') . '

'); if (defined('WPIDE_FS_METHOD_FORCED_ELSEWHERE')) { echo "WordPress filesystem API has been forced to use the " . WPIDE_FS_METHOD_FORCED_ELSEWHERE . " method by another plugin/WordPress. \n\n"; } //setup wp_filesystem api $wpide_filesystem_before = $wp_filesystem; $url = wp_nonce_url('admin.php?page=wpide', 'plugin-name-action_wpidenonce'); $form_fields = null; // for now, but at some point the login info should be passed in here ob_start(); if (false === ($creds = request_filesystem_credentials($url, FS_METHOD, false, false, $form_fields))) { // if we get here, then we don't have credentials yet, // but have just produced a form for the user to fill in, // so stop processing for now //return true; // stop the normal page form from displaying } ob_end_clean(); if (!WP_Filesystem($creds)) { echo "There has been a problem initialising the filesystem API \n\n"; echo "Filesystem API before this plugin ran: \n\n" . print_r($wpide_filesystem_before, true); echo "Filesystem API now: \n\n" . print_r($wp_filesystem, true); } unset($wpide_filesystem_before); $root = apply_filters('wpide_filesystem_root', WP_CONTENT_DIR); if (isset($wp_filesystem)) { //Running webservers user and group echo "Web server user/group = " . getenv('APACHE_RUN_USER') . ":" . getenv('APACHE_RUN_GROUP') . "\n"; //wp-content user and group echo "wp-content owner/group = " . $wp_filesystem->owner($root) . ":" . $wp_filesystem->group($root) . "\n\n"; //check we can list wp-content files if ($wp_filesystem->exists($root)) { $files = $wp_filesystem->dirlist($root); if (count($files) > 0) { echo "wp-content folder exists and contains " . count($files) . " files \n"; } else { echo "wp-content folder exists but we cannot read it's contents \n"; } } // $wp_filesystem->owner() $wp_filesystem->group() $wp_filesystem->is_writable() $wp_filesystem->is_readable() echo "\nUsing the " . $wp_filesystem->method . " method of the WP filesystem API\n"; //wp-content editable? 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"; //plugins folder editable 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"; //themes folder editable 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"; } echo "___________________ \n\n\n\n"; 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."; die(); } public function add_my_menu_page() { $this->menu_hook = add_menu_page('WPide', 'WPide', 'edit_themes', "wpide", array($this, 'my_menu_page'), 'dashicons-editor-code'); } public function my_menu_page() { if (!current_user_can('edit_themes')) { wp_die('

You do not have sufficient permissions to edit files for this site.

'); } $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) if (is_ssl()) $app_url = str_replace("http:", "https:", $app_url); ?>

Colour Assist

Files


CREATE
CREATE
SAVE FILE
is_plugin_page()) { return; } ?>