* Single-file online code editor editor.php version: v1.21 * It is very convenient to edit any text file on your website online. It is very useful for maintaining the website and writing code online * Password encryption method: * md5 (self-set password + $ace) //$ace is the cdn mirror address * * Instructions for use: * 1. Confirm that the $pwd variable value is false, upload this file to the PHP space and access it * 2. First You are prompted to set a password for each visit, set the password and remember it * 3. After logging in with the password you set for the first time, this php file will be edited by default, * 4. This file is the core file of the editor, please do not modify it at will * 5. Please use the Ctrl + S key combination to save the edited file and wait for the execution result * 6. After the save action is executed, please be sure to wait for the successful save message to return * 7. The reset operation will modify the file name of this program to prevent others Guess the path * 8. The refresh function only refreshes this program file and cannot refresh other * * It is recommended to use this editor in the chrome browser See the project details http://git.oschina.net/ymk18/aceditor
/**
* Single-file online code editor editor.php Version: v1.21
*
* Password encryption method:
* md5 (self-set password + $ace) //$ace is the cdn mirror address
*
* How to use :
* 1. Confirm that the $pwd variable value is false, upload this file to the PHP space and access it
* 2. You will be prompted to set a password for the first time, set the password and remember it
* 3. After logging in with the password you set for the first time , this php file is edited by default,
* 4. This file is the core file of the editor, please do not modify it at will
* 5. Please use the Ctrl + S key combination to save the edited file and wait for the execution result
* 6. Save the action After execution, please be sure to wait for the successful save message to return
* 7. The reset operation will modify the file name of this program to prevent others from guessing the path
* 8. The refresh function only refreshes this program file and cannot refresh other ones
*
* Suggestions Use this editor in chrome browser
*/
session_start();
$curr_file = __FILE__; //Edit the current file by default
$curr_file_path = str_replace(dirname(__FILE__), '', __FILE__) ;
$pwd = false; //The default value of password initialization is false
$ace = 'http://cdn.staticfile.org/ace/1.1.3/ace.js'; //Editor core js
$tip ['core'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.core.min.css';
$tip['css'] = 'http://cdn. staticfile.org/alertify.js/0.3.11/alertify.default.min.css';
$tip['js'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify .min.js';
$jquery = 'http://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js';
if ( false !== $pwd ) {
define ('DEFAULT_PWD', $pwd);
}
//The syntax parser corresponding to the file extension name
$lng = array(
'as' => 'actionscript', 'js' => 'javascript',
'php' => 'php', 'css' => 'css', 'html' => 'html',
'htm' => 'html', 'ini' => 'ini ', 'json' => 'json',
'jsp' => 'jsp', 'txt' => 'text', 'sql' => 'mysql',
'xml' => 'xml', 'yaml' => 'yaml', 'py' => 'python',
'md' => 'markdown', 'htaccess' => 'apache_conf',
'bat' = > 'batchfile', 'go' => 'golang',
);
//Determine whether the user is logged in
function is_logged() {
$flag = false;
if ( isset($_SESSION['pwd' ]) && defined('DEFAULT_PWD') ) {
if ( $_SESSION['pwd'] === DEFAULT_PWD ) {
$flag = true;
}
}
return $flag;
}
//Reload Enter this page
function reload() {
$file = pathinfo(__FILE__, PATHINFO_BASENAME);
die(header("Location: {$file}"));
}
//Determine whether the request is an ajax request
function is_ajax() {
$flag = false;
if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
$flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}
return $flag;
}
//Destroy SESSION and COOKIE
function exterminate() {
$_SESSION = array();
foreach ( $_COOKIE as $key ) {
setcookie($key, null);
}
session_destroy();
$_COOKIE = array();
return true;
}
//Get a list of files in a directory
function list_dir($path, $type = 'array') {
$flag = false;
$lst = array('dir'=>array(), 'file'=>array());
$base = !is_dir($path) ? dirname($path) : $path;
$tmp = scandir($base);
foreach ( $tmp as $k=>$v ) {
//Filter out the superior directory, this level directory and the program’s own file name
if ( !in_array($v, array(' .', '..')) ) {
$file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
if ( $full_path == __FILE__ ) {
continue; // Shield itself The file does not appear in the list
}
$file = str_replace(dirname(__FILE__), '', $file);
$file = str_replace("\", '/', $file); //Filter the path under win
$file = str_replace('//', '/', $file); //Filter double slashes
if ( is_dir($full_path) ) {
if ( 'html' === $type ) {
$v = '
'.$v.' > ;'; }
array_push($lst['dir'], $v);
} else {
if ( 'html' === $type ) {
$v = '
'.$v.' '; }
array_push($lst[ 'file'], $v);
}
}
}
$lst = array_merge($lst['dir'], $lst['file']);
$lst = array_filter($lst);
$ flag = $lst;
if ( 'html' === $type ) {
$flag = '';
}
return $flag;
}
//Recursively delete a non-empty directory
function deldir($dir) {
$dh = opendir($dir);
while ( $file = readdir($dh) ) {
if ( $file != '. ' && $file != '..' ) {
$fullpath = $dir.'/'.$file;
if ( !is_dir($fullpath) ) {
unlink($fullpath);
} else {
deldir ($fullpath);
}
}
}
return rmdir($dir);
}
//Log out
if ( isset($_GET['logout']) ) {
if ( exterminate() ) {
reload();
}
}
//ajax output file content
if ( is_logged() && is_ajax() && isset($_POST['file']) ) {
$file = dirname(__FILE__).$ _POST['file'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$mode = isset($lng[$ext]) ? $lng[$ext] : false;
die(json_encode(array(
'file' => $file, 'html' => file_get_contents($file),
'mode' => $mode,
)));
}
//ajax output directory list
if ( is_logged () && is_ajax() && isset($_POST['dir']) ) {
$dir = dirname(__FILE__).$_POST['dir'];
$list_dir = list_dir($dir, 'html');
die(json_encode(array(
'dir' => $dir, 'html' => $list_dir,
)));
}
//ajax save file
if ( is_logged() && is_ajax() && isset($_POST['action']) ) {
$arr = array('result'=>'error', 'msg'=>'File saving failed! ');
$content = $_POST['content'];
if ( 'save_file' === $_POST['action'] ) {
if ( isset($_POST['file_path']) ) {
$ file = dirname(__FILE__).$_POST['file_path'];
} else {
$file = __FILE__;
}
file_put_contents($file, $content);
$arr['result'] = 'success';
$arr['msg'] = 'Save successfully! ';
}
die(json_encode($arr));
}
//ajax delete file or folder
if ( is_logged() && is_ajax() && isset($_POST['del']) ) {
$path = dirname(__FILE__).$_POST['del'];
$arr = array('result'=>'error', 'msg'=>'Delete operation failed!');
if ( $ _POST['del'] && $path ) {
$flag = is_dir($path) ? deldir($path) : unlink($path);
if ( $flag ) {
$arr['msg'] = ' The deletion operation was successful! ';
$arr['result'] = 'success';
}
}
die(json_encode($arr));
}
//ajax creates a new file or folder
if ( is_logged() && is_ajax( ) && isset($_POST['create']) ) {
$flag = false;
$arr = array('result'=>'error', 'msg'=>'Operation failed!');
if ( isset($_POST['target']) ) {
$target = dirname(__FILE__).$_POST['target'];
$target = is_dir($target) ? $target : dirname($target);
}
if ( $_POST['create'] && $target ) {
$base_name = pathinfo($_POST['create'], PATHINFO_BASENAME);
$exp = explode('.', $base_name);
$ full_path = $target.'/'.$base_name;
$new_path = str_replace(dirname(__FILE__), '', $full_path);
if ( count($exp) > 1 && isset($lng[array_pop($ exp)]) ) {
file_put_contents($full_path, '');
$arr['result'] = 'success';
$arr['msg'] = 'New file successfully! ';
$arr['type'] = 'file';
} else {
mkdir($full_path, 0777, true);
$arr['result'] = 'success';
$arr['msg' ] = 'Create new directory successfully! ';
$arr['type'] = 'dir';
}
if ( $base_name && $new_path ) {
$arr['new_name'] = $base_name;
$arr['new_path'] = $new_path ;
}
}
die(json_encode($arr));
}
//ajax rename file or folder
if ( is_logged() && is_ajax() && isset($_POST['rename']) ) {
$arr = array('result'=>'error', 'msg'=>'Rename operation failed!');
if ( isset($_POST['target']) ) {
$target = dirname(__FILE__).$_POST['target'];
}
if ( $_POST['rename'] ) {
$base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME);
if ( $base_name ) {
$rename = dirname($target).'/'.$base_name;
$new_path = str_replace(dirname(__FILE__), '', $rename);
}
}
if ( $rename && $target && rename($target, $rename) ) {
$arr['new_name'] = $base_name;
$arr['new_path'] = $new_path;
$arr['msg'] = 'Rename operation successful!';
$arr['result'] = 'success';
}
if ( $target == __FILE__ ) {
$arr['redirect'] = $new_path;
}
die(json_encode($arr));
}
//获取代码文件内容
$code = file_get_contents($curr_file);
$tree = '
ROOT'.list_dir($curr_file, 'html').'
';
//登陆和设置密码共用模版
$first =
【标题】
HTMLSTR;
//判断是否第一次登录
if ( false === $pwd && empty($_POST) ) {
die(str_replace(
array('【标题】', '【动作】'),
array('第一次使用,请先设置密码!', 'Settings'),
$first
));
}
//Set the login password for the first time
if ( false === $pwd && !empty($_POST) ) {
if ( isset($ _POST['pwd']) && strlen($_POST['pwd']) ) {
$pwd = $_SESSION['pwd'] = md5($_POST['pwd'].$ace);
$code = preg_replace('#$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1);
file_put_contents($curr_file, $code);
} else {
reload( );
}
}
//User login verification
if ( false !== $pwd && !empty($_POST) ) {
$tmp = md5($_POST['pwd'].$ace);
if ( $tmp && $pwd && $tmp === $pwd ) {
$_SESSION['pwd'] = $pwd;
reload();
}
}
//Process the html entity
$code = htmlspecialchars($code);
$dir_icon = str_replace(array("rn", "r", "n"), '',
'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAANCAYAAACgu+4kAAAAGXRFWHRTb2Z0d2
FyZQBBZG9iZSBJbWF nZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wR
sCjQEcIY3DiiJUYiqRhp5Mra/92YSUVVgLSW49B7H +NApRh75XkHfFoCG+02tyflUeQTw2y9UYYP8cCStc9SM
PeVA/Sy6Dw555q3au1z+EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiHZgVqPzLO+8seai6E2jed42bCL06tNyEH
AX9kv3 jh3HqH7BctFWLMOmAbcg05mHK5+sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
MtRED4bwsHLnUzxEeKac3+GeP6eo8yevhjC3F1qC4CDAAl3HwuyNAIdwAAAABJRU5ErkJg gg==');
$file_icon = str_replace(array("rn", "r", "n"), '',
'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS1JREFUeNqMU01KxkAMTaez7aYbNwreQdBzeopS6EXEW+ju g7Z
C6X+/iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGN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fefiltmijyivbvhnz3V0Q4JNHIDODZ+CL7nden5CH8DGZBMJNIMBEOXKQLXKVIGYDIBBK9YLBYHVLTH5K0J0IAGAAGAAGAAAAAAAAAAAAAAAAAAAAAAAAAROEMKPZ6E4E4 W5TPCNUMAQD2FEFILTMJYIVBVHNZ3R1B4FNRIDODZ+Cl7NDEN5CH8DGZGZHNYMBEOXKQLXKQCIGQCIGQCIGQCIBYVLTH5K0J0KKKAAQAQASABIAAA AROEMKPQ6A4W5SPIDUMHQ
F2FEFILTMJYIVBVHNZ3D0W4BMAIDODZ+CL7NDEN5CH8DGZASGTUMKQLXKVIGIBK9ylby5k0J0J0
iads = ');
//// /Editor template
$html =
ACE code Editor
{$tree}
{$code}
');
right_menu.hover(function(){
if ( timer ) { clearTimeout(timer); }
}, function(){
timer = setTimeout(function(){
hide_menu(right_menu);
}, 500);
});
$('body').append(right_menu);
}
if ( path ) {
right_menu.html('');
var menu = $('新建 浏览 重命名 删除 ');
right_menu.append(menu);
menu_area(right_menu, {left: e.pageX, top: e.pageY});
right_menu.find('span').click(function(){
switch ( $(this).text() ) {
case '新建' : create_new(target, path); break;
case '浏览' : preview(target, path); break;
case '重命名' : re_name(target, path); break;
case '删除' : del_file(target, path); break;
}
hide_menu(right_menu);
});
}
path ? right_menu.show() : hide_menu(right_menu);
return false;
});
//隐藏右键菜单
function hide_menu(menu) {
$('#sider li.hover').removeClass('hover');
if ( menu ) {
menu.hide();
}
}
//右键菜单区域
function menu_area(menu, cfg) {
if ( menu && cfg ) {
var w = $('#sider').width() - menu.width();
var h = $('#sider').height() - menu.height();
if ( cfg.left > w ) { cfg.left = w; }
if ( cfg.top > h ) { cfg.top = h; }
menu.css(cfg);
}
}
//保存按钮
$('#logout>a:contains("保存")').click(function(){
save_file();
return false;
});
//刷新按钮
$('#logout>a:contains("刷新")').click(function(){
window.location.href = window.location.pathname;
return false;
});
//重置按钮
$('#logout>a:contains("重置")').click(function(){
alertify.confirm('是否修改 {$curr_file_path} 程序文件名?', function (e) {
if ( !e ) { return 'cancel'; }
re_name($(''), '{$curr_file_path}'); });
return false;
});
//新建操作
function create_new(obj, path) {
if ( !obj || !path ) { return false; }
alertify.prompt('请输入新建文件或文件夹名:', function (e, str) {
if ( !e || !str ) { return false; }
alertify.log('正在操作中...');
$('#dir_tree #on').removeAttr('loaded').removeAttr('id');
$.post(window.location.href, {create:str,target:path}, function(data){
if ( data.msg && 'success' == data.result ) {
alertify.success(data.msg);
if ( obj.attr('class') == 'dir' ) {
load(obj); //重新加载子节点
} else {
load(obj.parent().parent());
}
} else {
alertify.error(data.msg);
}
}, 'json');
});
}
//浏览操作
function preview(obj, path) {
if ( !obj || !path ) { return false; }
window.open(path, '_blank');
}
//重命名
function re_name(obj, path) {
if ( !obj || !path ) { return false; }
alertify.prompt('重命名 '+path+' 为:', function (e, str) {
if ( !e || !str ) { return false; }
alertify.log('正在操作中...');
$.post(window.location.href, {rename:str,target:path}, function(data){
if ( data.msg && 'success' == data.result ) {
alertify.success(data.msg);
if ( data.redirect ) {
window.location.href = data.redirect;
}
if ( data.new_name ) {
obj.children('span').first().text(data.new_name);
obj.attr('path', data.new_path);
}
} else {
alertify.error(data.msg);
}
}, 'json');
});
}
//删除文件动作
function del_file(obj, path) {
if ( !obj || !path ) { return false; }
alertify.confirm('您确定要删除:'+path+' 吗?', function (e) {
if ( !e ) { return 'cancel'; }
alertify.log('Deleting...');
$.post(window.location.href, {del:path }, function(data){
if ( data.msg && 'success' == data.result ) {
alertify.success(data.msg);
obj.remove();
} else {
alertify.error( data.msg);
}
}, 'json');
});
}
});
HTMLSTR;
//Judgement Have you logged in
if ( !is_logged() ) {
die(str_replace(
array('[Title]', '[Action]'),
array('Please enter the password you set for the first time!', ' Login'),
$first
));
} else {
echo $html;
}
Copy code