PHP single-file version of the online Code Editor _php instance

Source: Internet
Author: User
Password encryption Method:
* MD5 (password + $ace)//$ace for CDN Mirror Address

How to use:

* 1. Confirm that the $PWD variable value is false, upload this file to PHP space and access
* 2. First access prompt set password, set password and remember
* 3. After logging in with the password for the first time, this PHP file is edited by default,
* 4. This file is the editor core file, please do not modify it arbitrarily
* 5. Save the edited file by using Ctrl + S key combination, waiting for the result to be executed
* 6. Be sure to wait for the save success message to return after the Save action is executed
* 7. The reset operation modifies the file name of the program to prevent others guessing the path
* 8. Refresh function is only to refresh the program files, cannot refresh other

We recommend that you use this editor in your Chrome browser

Copy CodeThe code is as follows:
<?php
Session_Start ();
$curr _file = __file__; Edit the current file by default
$curr _file_path = Str_replace (dirname (__file__), ", __file__);
$pwd = "57574d98bc6ebe77b07e59d87065cd9e"; Password initialization default value is False
$ace = ' ace.js '; Editor Core 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);
}
Syntax parser for file suffix names
$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 if the user is logged on
function is_logged () {
$flag = false;
if (Isset ($_session[' pwd ") && defined (' Default_pwd ')) {
if ($_session[' pwd '] = = = Default_pwd) {
$flag = true;
}
}
return $flag;
}
Reload to this page
function Reload () {
$file = PathInfo (__file__, pathinfo_basename);
Die (The header ("Location: {$file}"));
}
Determine if 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;
}
Destroying session and Cookies
function exterminate () {
$_session = Array ();
foreach ($_cookie as $key) {
Setcookie ($key, NULL);
}
Session_destroy ();
$_cookie = Array ();
return true;
}
Get a list of files under 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 parent directory, the 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 Mask itself file does not appear in list
}
$file = Str_replace (dirname (__file__), ", $file);
$file = str_replace ("\ \", '/', $file); Filter the path under Win
$file = Str_replace ('//', '/', $file); Filter Double Slash
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;
    }
    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);
    }
    Sign Out
    if (Isset ($_get[' logout ')) {
    if (exterminate ()) {
    Reload ();
    }
    }
    Ajax Output File contents
    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 save 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 '] = ' saved successfully! ';
    }
    Die (Json_encode ($arr));
    }
    Ajax delete a 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 '] = ' delete operation succeeded! ';
    $arr [' result '] = ' success ';
    }
    }
    Die (Json_encode ($arr));
    }
    Ajax 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 succeeded! ';
    $arr [' type '] = ' file ';
    } else {
    mkdir ($full _path, 0777, true);
    $arr [' result '] = ' success ';
    $arr [' msg '] = ' new directory successful! ';
    $arr [' type '] = ' dir ';
    }
    if ($base _name && $new _path) {
    $arr [' new_name '] = $base _name;
    $arr [' new_path '] = $new _path;
    }
    }
    Die (Json_encode ($arr));
    }
    Ajax renaming a 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 succeeded! ';
    $arr [' result '] = ' success ';
    }
    if ($target = = __file__) {
    $arr [' redirect '] = $new _path;
    }
    Die (Json_encode ($arr));
    }
    Get the contents of a code file
    $code = file_get_contents ($curr _file);
    $tree = '

      • ROOT '. List_dir ($curr _file, ' html '). '

    ';
    Login and Set Password common template
    $first = << <>


    <title>Title</title>












    HTMLSTR;
    Determine if you are logged in for the first time
    if (false = = = $pwd && empty ($_post)) {
    Die (Str_replace (
    Array (' title ' ', ' ' action '),
    Array (' First use, please set the password first! ', ' Set '),
    $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 ();
    }
    }
    Working with HTML entities
    $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= ');
    Editor templates
    $html = << <>


    <title>Ace Code Editor</title>






    Save
    Refresh
    Reset
    Exit

    {$tree}
    {$code}






    HTMLSTR;
    Determine if you are 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;
    }

    The above is the whole content of this article, I hope you can enjoy.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.