1. [代码][PHP]代码
<?php /**
* 单文件版在线代码编辑器 editor.php 版本: v1.21
*
* 密码加密方式:
* md5(自设密码+$ace) //$ace为cdn镜像地址
*
* 使用方法:
* 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问
* 2.第一次访问提示设置密码,设置密码并牢记
* 3.使用第一次设置的密码登录后,默认编辑的是本php文件,
* 4.本文件是编辑器核心文件,请不要随意修改
* 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果
* 6.保存动作执行后请务必等待保存成功信息返回
* 7.重置操作会修改本程序的文件名,以防他人猜测路径
* 8.刷新功能仅是刷新本程序文件,不能刷新其他
*
* 建议在 chrome 浏览器中使用本编辑器
*
*
*
*/
session_start();
$curr_file = __FILE__; //默认编辑当前文件
$curr_file_path = str_replace(dirname(__FILE__), '', __FILE__);
$pwd = "57574d98bc6ebe77b07e59d87065cd9e"; //密码初始化默认值为 false
$ace = 'ace.js'; //编辑器核心js
$tip['core'] = 'alertify.core.min.css';
$tip['css'] = 'alertify.default.min.css';
$tip['js'] = 'alertify.min.js';
$jquery = 'jquery.min.js';
if ( false !== $pwd ) {
define('DEFAULT_PWD', $pwd);
}
//文件后缀名对应的语法解析器
$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',
);
//判断用户是否登录
function is_logged() {
$flag = false;
if ( isset($_SESSION['pwd']) && defined('DEFAULT_PWD') ) {
if ( $_SESSION['pwd'] === DEFAULT_PWD ) {
$flag = true;
}
}
return $flag;
}
//重新载入到本页面
function reload() {
$file = pathinfo(__FILE__, PATHINFO_BASENAME);
die(header("Location: {$file}"));
}
//判断请求是否是ajax请求
function is_ajax() {
$flag = false;
if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
$flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}
return $flag;
}
//销毁SESSION和COOKIE
function exterminate() {
$_SESSION = array();
foreach ( $_COOKIE as $key ) {
setcookie($key, null);
}
session_destroy();
$_COOKIE = array();
return true;
}
//获取一个目录下的文件列表
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 ) {
//过滤掉上级目录,本级目录和程序自身文件名
if ( !in_array($v, array('.', '..')) ) {
$file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
if ( $full_path == __FILE__ ) {
continue; //屏蔽自身文件不在列表出现
}
$file = str_replace(dirname(__FILE__), '', $file);
$file = str_replace("\", '/', $file); //过滤win下的路径
$file = str_replace('//', '/', $file); //过滤双斜杠
if ( is_dir($full_path) ) {
if ( 'html' === $type ) {
$v = '
- '. implode('', $lst) .'
- ROOT'.list_dir($curr_file, 'html').'
{$tree}
{$code}
<script></script><script></script><script></script><script>
var load = false;
var curr_file = false;
window.location.hash = '';
alertify.set({delay: 1000}); //n秒后自动消失
alertify.set({labels: {ok:'确定',cancel:'取消'}});
var editor = false;
$(function(){
//实例化代码编辑器
editor = ace.edit("editor");
//设置编辑器的语法和高亮
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/php");
//设置编辑器自动换行
editor.getSession().setWrapLimitRange(null, null);
editor.getSession().setUseWrapMode(true);
//不显示垂直衬线
editor.renderer.setShowPrintMargin(false);
//editor.setReadOnly(true); //设置编辑器为只读
//editor.gotoLine(325); //跳转到指定行
//使编辑器获得输入焦点
editor.focus();
//绑定组合按键
var commands = editor.commands;
commands.addCommand({
name: "save",
bindKey: {win: "Ctrl-S", mac: "Command-S"},
exec: save_file
});
//保存动作
function save_file() {
if ( false == editor ) { return false; }
var obj = {
content: editor.getValue(),
action: 'save_file'
};
if ( false !== curr_file ) {
obj.file_path = curr_file;
}
alertify.log('正在保存...');
$.post(window.location.href, obj, function(data){
if ( data.msg && 'success' == data.result ) {
alertify.success(data.msg);
} else {
alertify.error(data.msg);
}
}, 'json');
}
//加载目录列表或文件
load = function(ele) {
var curr = $(event.srcElement);
if ( ele ) { curr = ele; }
if ( curr.is('span') ) { curr = curr.parent('li'); }
$('#dir_tree #on').removeAttr('id');
curr.attr('id', 'on');
var type = curr.attr('class');
var path = curr.attr('path');
window.location.hash = path;
if ( 'file' === type ) {
alertify.log('正在加载...');
curr.addClass('loading');
$.post(window.location.href, {file:path}, function(data){
curr.removeClass('loading');
if ( data.mode ) {
editor.getSession().setMode("ace/mode/"+data.mode);
}
//注意,空文件应当允许编辑
if ( true || data.html ) {
curr.attr('disabled', 'disabled');
curr_file = path; //当前编辑的文件路径
//动态赋值编辑器中的内容
editor.session.doc.setValue(data.html);
editor.renderer.scrollToRow(0); //滚动到第一行
editor.focus(); //编辑器获得焦点
setTimeout(function(){
editor.gotoLine(0);
}, 800);
}
}, 'json');
event.stopPropagation();
event.preventDefault();
return false;
}
if ( 'dir' === type ) {
if ( curr.attr('loaded') ) {
curr.children('ul').toggle();
event.stopPropagation();
event.preventDefault();
return false;
} else {
curr.attr('loaded', 'yes');
}
alertify.log('正在加载...');
curr.addClass('loading');
$.post(window.location.href, {dir:path}, function(data){
curr.find('ul').remove();
curr.removeClass('loading');
if ( data.html ) {
curr.append(data.html);
}
}, 'json');
}
return false;
}
//绑定右键菜单
$('#sider').bind('contextmenu', function(e){
var path = false;
var target = $(event.srcElement);
if ( target.is('span') ) {
target = target.parent('li');
}
if ( target.attr('path') ) {
path = target.attr('path');
} else {
return false;
}
target.addClass('hover');
var right_menu = $('#contextmenu');
if ( !right_menu.get(0) ) {
var timer = false;
right_menu = $('<p id="contextmenu">');
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 = $('<span>新建<span>浏览<span>重命名<span>删除');
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($('<a>'), '{$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('正在删除中...');
$.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');
});
}
});
</script>
HTMLSTR;
//判断是否已经登录
if ( !is_logged() ) {
die(str_replace(
array('【标题】', '【动作】'),
array('请输入您第一次设置的密码!', '登录'),
$first
));
} else {
echo $html;
}
2. [文件] ace.rar
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










