您好,欢迎访问一九零五行业门户网

php单文件版在线代码编辑器_php实例

密码加密方式:
 * 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 = ''.$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 = ''. implode('', $lst) .'';
    }
    return $flag;
}
//递归删除一个非空目录
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);
}
//退出登录
if ( isset($_get['logout']) ) {
    if ( exterminate() ) {
        reload();
    }
}
//ajax输出文件内容
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输出目录列表
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保存文件
if ( is_logged() && is_ajax() && isset($_post['action']) ) {
    $arr = array('result'=>'error', 'msg'=>'文件保存失败!');
    $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'] = '保存成功!';
    }
    die(json_encode($arr));
}
//ajax删除文件或文件夹
if ( is_logged() && is_ajax() && isset($_post['del']) ) {
    $path = dirname(__file__).$_post['del'];
    $arr = array('result'=>'error', 'msg'=>'删除操作失败!');
    if ( $_post['del'] && $path ) {
        $flag = is_dir($path) ? deldir($path) : unlink($path);
        if ( $flag ) {
           $arr['msg'] = '删除操作成功!';
           $arr['result'] = 'success';
        }
    }
    die(json_encode($arr));
}
//ajax新建文件或文件夹
if ( is_logged() && is_ajax() && isset($_post['create']) ) {
    $flag = false;
    $arr = array('result'=>'error', 'msg'=>'操作失败!');
    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'] = '新建文件成功!';
            $arr['type'] = 'file';
        } else {
            mkdir($full_path, 0777, true);
            $arr['result'] = 'success';
            $arr['msg'] = '新建目录成功!';
            $arr['type'] = 'dir';
        }
        if ( $base_name && $new_path ) {
            $arr['new_name'] = $base_name;
            $arr['new_path'] = $new_path;
        }
    }
    die(json_encode($arr));
}
//ajax重命名文件或文件夹
if ( is_logged() && is_ajax() && isset($_post['rename']) ) {
    $arr = array('result'=>'error', 'msg'=>'重命名操作失败!');
    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'] = '重命名操作成功!';
       $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('第一次使用,请先设置密码!', '设置'),
        $first
    ));
}
//第一次设置登录密码
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();
    }
}
//用户登录验证
if ( false !== $pwd && !empty($_post) ) {
    $tmp = md5($_post['pwd'].$ace);
    if ( $tmp && $pwd && $tmp === $pwd ) {
        $_session['pwd'] = $pwd;
        reload();
    }
}
//处理一下html实体
$code = htmlspecialchars($code);
$dir_icon = str_replace(array(\r\n, \r, \n), '',
'data:image/jpg;base64,ivborw0kggoaaaansuheugaaabaaaaancayaaacgu+4kaaaagxrfwhrtb2z0d2
fyzqbbzg9izsbjbwfnzvjlywr5ccllpaaaaqvjrefuenqkkk1uwjaqhd84bsnp1fuxlctu0h3xpsox4qrd9wr
scjqeciy3diijuyiqrhp5mra/92ysuvvglsw49b7h+naprh75xkhffocg+02tyflueqtw2y9uyyp8ccstc9sm
peva/sy6dw555q3au1z+ehbyk1cgo7osndafnt0x5sckydha0wpihzgvqpzlo+8seai6e2jed42bcl06tnyeh
ax9kv3jh3hqh7bctfwlmomabcg05mhk5+sqpd1hyijn47zcducshgehtzxtwqs9wtcaqmjrorjdlxqb9s1tu6
mtred4bwshlnuzxeekac3+gep6eo8yevhjc3f1qc4cdaal3hwuynaidwaaaabjru5erkjggg==');
$file_icon = str_replace(array(\r\n, \r, \n), '',
'data:image/jpg;base64,ivborw0kggoaaaansuheugaaaa8aaaaqcayaaadjviueaaaagxrfwhrtb2z0d2
fyzqbbzg9izsbjbwfnzvjlywr5ccllpaaaas1jrefuenqmu01kxkamtaez7aybnwreqdbzeops6exew+jug7z
c6x+/iulosr6xiofhjkpee5mujgbwt7gjppb3xagfibjs5doylf/btl0pkefngdbzpgnrfk/u+0hwjaamjmcm
dsoa4zge6pseu67dpmleqk5rlmvyrkdjor6uq2sgktu2ffdpmpanqqosasyno/kthabjkocoxcaskcbkwsyuq
qcene1fqhz3fmkxzjnj2srinl33qbnizwj5nh/l8npqohvtjwytyffm/d6oo2hge8ffwseuz1pejhroutmsrf
0ic8qmpibett4hftrhhi95jqjt/hc2jot0to+zn6mvsz/ozkqwmycta33dkbn1sws0i+pega6v0kd42h9jb/8
ljl5i6pnbgaeaa9mp7qwonloaaaaasuvork5cyii=');
$loading = str_replace(array(\r\n, \r, \n), '',
'data:image/gif;base64,r0lgodlhfaaualmiaph2ap+tmsziallcaknoaop4anvqap+pfv///waaaaaaaa
aaaaaaaaaaaaaaaaaaach/c05fvfndqvbfmi4waweaaaah+qqfcgaiacwaaaaafaauaaaeuxdjsau9ibdmteb
tmejehgtbjyqkialwolzvgs8wdo6uipchw8tnawwdeukpcxqml0ynj2cwyacas7vqwwitwyuiujb4s2axmwxg
g9bl6yqtl0caach5baukaagalaeaaqasabiaaaroemkpx6a4w5upenumeqt2fefiltmjyivbvhnz3z1h4fmqi
dodz+cl7nden5ch8dgzhcltcmbeoxkqlxkvigaaibbk9ylbyvlthh5k0j0iach5baukaagalaeaaqasabiaaa
roemkphaa4w5upmdumdqp2fefiltmjyivbvhnz3v1r4bnbidodz+cl7nden5ch8dgzamatembeoxkqlxkvig4
hibbk9ylbyvlthh5k0j0iach5baukaagalaeaaqasabiaaaroemkpjae4w5tpkdumcql2fefiltmjyivbvhnz
3r0a4nmwidodz+cl7nden5ch8dgzh8onqmbeoxkqlxkvigibibbk9ylbyvlthh5k0j0iach5baukaagalaeaa
qasabiaaaroemkps6e4w5spanumgqb2fefiltmjyivbvhnz3d1x4jmgidodz+cl7nden5ch8dgzgcbtmmbeox
kqlxkviggeibbk9ylbyvlthh5k0j0iach5baukaagalaeaaqasabiaaaroemkpaaa4w5vpodumfqx2fefiltm
jyivbvhnz3v0q4jnhidodz+cl7nden5ch8dgzbmjnimbeoxkqlxkvigydibbk9ylbyvlthh5k0j0iach5bauk
aagalaeaaqasabiaaaroemkpz6e4w5tpcnumaqd2fefiltmjyivbvhnz3r1b4fnridodz+cl7nden5ch8dgzg
8hnymbeoxkqlxkvigqcibbk9ylbyvlthh5k0j0iach5bakkaagalaeaaqasabiaaaroemkpq6a4w5spidumhq
f2fefiltmjyivbvhnz3d0w4bmaidodz+cl7nden5ch8dgzasgtumbeoxkqlxkvigwgibbk9ylbyvlthh5k0j0
iads=');
//编辑器模版
$html =
ace代码编辑器
保存
    刷新
    重置
    退出
{$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('正在删除中...');
            $.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;
//判断是否已经登录
if ( !is_logged() ) {
    die(str_replace(
        array('【标题】', '【动作】'),
        array('请输入您第一次设置的密码!', '登录'),
        $first
    ));
} else {
    echo $html;
}
以上就是本文所述的全部内容了,希望大家能够喜欢。
其它类似信息

推荐信息