栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 = <<

    【标题】


    body {
        overflow: hidden; background-color: #2D2D2D; color: #CCCCCC; font-size: 12px; margin: 0;
        font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
    }
    form { display: none; position: absolute; }
    form h5 { font-size: 14px; font-weight: normal; margin: 0; line-height: 2em; }
    form input {
        color: #fff; border: 1px solid #369; border-radius: 3px; background: #333; height: 22px;
        line-height: 1.6em; width: 125px; margin-right: 5px; vertical-align: middle;
    }
    form button {
        line-height: 1.6em; border: 1px solid #369; border-radius: 3px;
        background: #369; color: #fff; vertical-align: middle;
    }





       
       
       
       
       

    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("rn", "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("rn", "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("rn", "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代码编辑器


    a { text-decoration: none; }
    body {
        overflow: hidden; background-color: #2D2D2D; font-size: 12px;
        font-family: 'Consolas', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
        scrollbar-arrow-color: #ccc; scrollbar-base-color: #333;
        scrollbar-dark-shadow-color: #00ffff; scrollbar-track-color: #272822;
        scrollbar-highlight-color: #272822; scrollbar-3d-light-color: #272822;
        scrollbar-face-color: #2D2D2D; scrollbar-shadow-color: #333;
    }
    ::-webkit-scrollbar { width:5px; height:6px; background-color:#444; }
    ::-webkit-scrollbar:hover { background-color:#444; }
    ::-webkit-scrollbar-thumb:hover { min-height:5px; min-width:5px; background-color: #AAA; }
    ::-webkit-scrollbar-thumb:active { -webkit-border-radius:20px; background-color: #AAA; }
    ::-webkit-scrollbar-thumb {
        min-height:5px; min-width:5px; -webkit-border-radius:20px;
        ::-webkit-border-radius:1px; background-color: #AAA;
    }
    body > pre { color: #666; }
    #sider { margin: 0; position: absolute; top:  25px; bottom: 0; left: 0; right: 85%; }
    #editor { margin: 0; position: absolute; top: 0; bottom: 0; left: 15%; right: 0; }
    #dir_tree { margin:0; padding: 0; height: 100%; overflow: auto; position: relative; left: 5px; }
    #dir_tree, #dir_tree ul, #dir_tree li { margin: 0; padding: 0; list-style: none inside; }
    #dir_tree ul { padding-left: 20px; position: relative; }
    #dir_tree li { text-indent: 2em; line-height: 1.6em; cursor: default; color: #ccc; }
    #dir_tree li.hover > span, #dir_tree li:hover > span { color: #66D9EF; }
    #dir_tree li#on > span { color: red; }
    #dir_tree li.dir { background: url({$dir_icon}) no-repeat 3px 3px; }
    #dir_tree li.file { background: url({$file_icon}) no-repeat 3px 0; }
    #dir_tree li.loading { background: url({$loading}) no-repeat 3px 0; }
    #logout { position: absolute; top: 0; left: 0; }
    #logout a { display: inline-block; color: #aaa; line-height: 25px; padding: 0 4px; }
    #logout a:hover { background: #000; color: #ddd; }
    #contextmenu { position: absolute; top: 0; left: 0; background: #fff; color: #333; border: 1px solid #000; padding: 1px; }
    #contextmenu span { display: block; line-height: 24px; text-indent: 20px; width: 80px; cursor: default; }
    #contextmenu span:hover { background-color: #369; color: #fff; }
    #alertify .alertify-message, #alertify .alertify-message {
        text-align: left !important; text-indent: 0; font-weight: bold; font-size: 16px;
    }
    #alertify .alertify-dialog, #alertify .alertify-dialog {
        font-family: 'Consolas'; padding: 10px !important; color: #333 !important;
    }
    #alertify .alertify-button {
        border-radius: 3px !important; font-weight: normal !important;
        font-size: 14px !important; padding: 3px 15px !important;
    }
    .alertify-buttons { text-align: right !important; }





        保存
        刷新
        重置
        退出

    {$tree}
    {$code}






    HTMLSTR;
    //判断是否已经登录
    if ( !is_logged() ) {
        die(str_replace(
            array('【标题】', '【动作】'),
            array('请输入您第一次设置的密码!', '登录'),
            $first
        ));
    } else {
        echo $html;
    }

    以上就是本文所述的全部内容了,希望大家能够喜欢。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/46063.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号