当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-16 10:55:53 浏览: 评论:0 

这篇文章主要介绍了php单文件版在线代码编辑器,个人感觉相当不错,分享给大家,需要的朋友可以参考下

密码加密方式:

* md5(自设密码+$ace) //$ace为cdn镜像地址

使用方法:

* 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问

* 2.第一次访问提示设置密码,设置密码并牢记

* 3.使用第一次设置的密码登录后,默认编辑的是本php文件,

* 4.本文件是编辑器核心文件,请不要随意修改

* 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果

* 6.保存动作执行后请务必等待保存成功信息返回

* 7.重置操作会修改本程序的文件名,以防他人猜测路径

* 8.刷新功能仅是刷新本程序文件,不能刷新其他

建议在 chrome 浏览器中使用本编辑器,代码如下:

  1. <?php 
  2. session_start(); 
  3. $curr_file = __FILE__; //默认编辑当前文件 
  4. $curr_file_path = str_replace(dirname(__FILE__), '', __FILE__); 
  5. $pwd = "57574d98bc6ebe77b07e59d87065cd9e"; //密码初始化默认值为 false 
  6. $ace = 'ace.js'; //编辑器核心js 
  7. $tip['core'] = 'alertify.core.min.css'; 
  8. $tip['css'] = 'alertify.default.min.css'; 
  9. $tip['js'] = 'alertify.min.js'; 
  10. $jquery = 'jquery.min.js'; 
  11. if ( false !== $pwd ) { 
  12.     define('DEFAULT_PWD', $pwd); 
  13. } 
  14. //文件后缀名对应的语法解析器 
  15. $lng = array( 
  16.     'as' => 'actionscript', 'js' => 'javascript', 
  17.     'php' => 'php', 'css' => 'css', 'html' => 'html', 
  18.     'htm' => 'html', 'ini' => 'ini', 'json' => 'json', 
  19.     'jsp' => 'jsp', 'txt' => 'text', 'sql' => 'mysql', 
  20.     'xml' => 'xml', 'yaml' => 'yaml', 'py' => 'python', 
  21.     'md' => 'markdown', 'htaccess' => 'apache_conf', 
  22.     'bat' => 'batchfile', 'go' => 'golang', 
  23. ); 
  24. //判断用户是否登录 
  25. function is_logged() { 
  26.     $flag = false; 
  27.     if ( isset($_SESSION['pwd']) && defined('DEFAULT_PWD') ) { 
  28.         if ( $_SESSION['pwd'] === DEFAULT_PWD ) { 
  29.             $flag = true; 
  30.         } 
  31.     } 
  32.     return $flag; 
  33. } 
  34. //重新载入到本页面 
  35. function reload() { 
  36.     $file = pathinfo(__FILE__, PATHINFO_BASENAME); 
  37.     die(header("Location: {$file}")); 
  38. } 
  39. //判断请求是否是ajax请求 
  40. function is_ajax() { 
  41.     $flag = false; 
  42.     if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) { 
  43.         $flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; 
  44.     } 
  45.     return $flag; 
  46. } 
  47. //销毁SESSION和COOKIE 
  48. function exterminate() { 
  49.     $_SESSION = array(); 
  50.     foreach ( $_COOKIE as $key ) { 
  51.         setcookie($key, null); 
  52.     } 
  53.     session_destroy(); 
  54.     $_COOKIE = array(); 
  55.     return true; 
  56. } 
  57. //获取一个目录下的文件列表 
  58. function list_dir($path, $type = 'array') { 
  59.     $flag = false; 
  60.     $lst = array('dir'=>array(), 'file'=>array()); 
  61.     $base = !is_dir($path) ? dirname($path) : $path; 
  62.     $tmp = scandir($base); 
  63.     foreach ( $tmp as $k=>$v ) { 
  64.         //过滤掉上级目录,本级目录和程序自身文件名 
  65.         if ( !in_array($v, array('.', '..')) ) { 
  66.             $file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v; 
  67.             if ( $full_path == __FILE__ ) { 
  68.                 continue; //屏蔽自身文件不在列表出现 
  69.             } 
  70.             $file = str_replace(dirname(__FILE__), '', $file); 
  71.             $file = str_replace("\\", '/', $file); //过滤win下的路径 
  72.             $file = str_replace('//', '/', $file); //过滤双斜杠 
  73.             if ( is_dir($full_path) ) { 
  74.                 if ( 'html' === $type ) { 
  75.                     $v = '<li class="dir" path="'.$file 
  76.                     .'" onclick="load();"><span>'.$v.'</span></li>'; 
  77.                 } 
  78.                 array_push($lst['dir'], $v); 
  79.             } else { 
  80.                 if ( 'html' === $type ) { 
  81.                     $v = '<li class="file" path="'.$file 
  82.                     .'" onclick="load()"><span>'.$v.'</span></li>'; 
  83.                 } 
  84.                 array_push($lst['file'], $v); 
  85.             } 
  86.         } 
  87.     } 
  88.     $lst = array_merge($lst['dir'], $lst['file']); 
  89.     $lst = array_filter($lst); 
  90.     $flag = $lst; 
  91.     if ( 'html' === $type ) { 
  92.         $flag = '<ul>'. implode('', $lst) .'</ul>'; 
  93.     } 
  94.     return $flag; 
  95. } 
  96. //递归删除一个非空目录 
  97. function deldir($dir) { 
  98.     $dh = opendir($dir); 
  99.     while ( $file = readdir($dh) ) { 
  100.         if ( $file != '.' && $file != '..' ) { 
  101.             $fullpath = $dir.'/'.$file; 
  102.             if ( !is_dir($fullpath) ) { 
  103.                 unlink($fullpath); 
  104.             } else { 
  105.                 deldir($fullpath); 
  106.             } 
  107.         } 
  108.     } 
  109.     return rmdir($dir); 
  110. } 
  111. //退出登录 
  112. if ( isset($_GET['logout']) ) { 
  113.     if ( exterminate() ) { 
  114.         reload(); 
  115.     } 
  116. } 
  117. //ajax输出文件内容 
  118. if ( is_logged() && is_ajax() && isset($_POST['file']) ) { 
  119.     $file = dirname(__FILE__).$_POST['file']; 
  120.     $ext = pathinfo($file, PATHINFO_EXTENSION); 
  121.     $mode = isset($lng[$ext]) ? $lng[$ext] : false; 
  122.     die(json_encode(array( 
  123.         'file' => $file, 'html' => file_get_contents($file), 
  124.         'mode' => $mode,     
  125.     ))); 
  126. } 
  127. //ajax输出目录列表 
  128. if ( is_logged() && is_ajax() && isset($_POST['dir']) ) { 
  129.     $dir = dirname(__FILE__).$_POST['dir']; 
  130.     $list_dir = list_dir($dir, 'html'); 
  131.     die(json_encode(array( 
  132.         'dir' => $dir, 'html' => $list_dir, 
  133.     ))); 
  134. } 
  135. //ajax保存文件 
  136. if ( is_logged() && is_ajax() && isset($_POST['action']) ) { 
  137.     $arr = array('result'=>'error', 'msg'=>'文件保存失败!'); 
  138.     $content = $_POST['content']; 
  139.     if ( 'save_file' === $_POST['action'] ) { 
  140.         if ( isset($_POST['file_path']) ) { 
  141.             $file = dirname(__FILE__).$_POST['file_path']; 
  142.         } else { 
  143.             $file = __FILE__; 
  144.         } 
  145.         file_put_contents($file, $content); 
  146.         $arr['result'] = 'success'; 
  147.         $arr['msg'] = '保存成功!'; 
  148.     } 
  149.     die(json_encode($arr)); 
  150. } 
  151. //ajax删除文件或文件夹 
  152. if ( is_logged() && is_ajax() && isset($_POST['del']) ) { 
  153.     $path = dirname(__FILE__).$_POST['del']; 
  154.     $arr = array('result'=>'error', 'msg'=>'删除操作失败!'); 
  155.     if ( $_POST['del'] && $path ) { 
  156.         $flag = is_dir($path) ? deldir($path) : unlink($path); 
  157.         if ( $flag ) { 
  158.            $arr['msg'] = '删除操作成功!'; 
  159.            $arr['result'] = 'success'; 
  160.         } 
  161.     } 
  162.     die(json_encode($arr)); 
  163. } 
  164. //ajax新建文件或文件夹 
  165. if ( is_logged() && is_ajax() && isset($_POST['create']) ) { 
  166.     $flag = false; 
  167.     $arr = array('result'=>'error', 'msg'=>'操作失败!'); 
  168.     if ( isset($_POST['target']) ) { 
  169.         $target = dirname(__FILE__).$_POST['target']; 
  170.         $target = is_dir($target) ? $target : dirname($target); 
  171.     } 
  172.     if ( $_POST['create'] && $target ) { 
  173.         $base_name = pathinfo($_POST['create'], PATHINFO_BASENAME); 
  174.         $exp = explode('.', $base_name); 
  175.         $full_path = $target.'/'.$base_name; 
  176.         $new_path = str_replace(dirname(__FILE__), '', $full_path); 
  177.         if ( count($exp) > 1 && isset($lng[array_pop($exp)]) ) { 
  178.             file_put_contents($full_path, ''); 
  179.             $arr['result'] = 'success'; 
  180.             $arr['msg'] = '新建文件成功!'; 
  181.             $arr['type'] = 'file'; 
  182.         } else { 
  183.             mkdir($full_path, 0777, true); 
  184.             $arr['result'] = 'success'; 
  185.             $arr['msg'] = '新建目录成功!'; 
  186.             $arr['type'] = 'dir'; 
  187.         } 
  188.         if ( $base_name && $new_path ) { 
  189.             $arr['new_name'] = $base_name; 
  190.             $arr['new_path'] = $new_path; 
  191.         } 
  192.     } 
  193.     die(json_encode($arr)); 
  194. } 
  195. //ajax重命名文件或文件夹 
  196. if ( is_logged() && is_ajax() && isset($_POST['rename']) ) { 
  197.     $arr = array('result'=>'error', 'msg'=>'重命名操作失败!'); 
  198.     if ( isset($_POST['target']) ) { 
  199.         $target = dirname(__FILE__).$_POST['target']; 
  200.     } 
  201.     if ( $_POST['rename'] ) { 
  202.         $base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME); 
  203.         if ( $base_name ) { 
  204.             $rename = dirname($target).'/'.$base_name; 
  205.             $new_path = str_replace(dirname(__FILE__), '', $rename); 
  206.         } 
  207.     } 
  208.     if ( $rename && $target && rename($target, $rename) ) { 
  209.        $arr['new_name'] = $base_name; 
  210.        $arr['new_path'] = $new_path; 
  211.        $arr['msg'] = '重命名操作成功!'; 
  212.        $arr['result'] = 'success'; 
  213.     } 
  214.     if ( $target == __FILE__ ) { 
  215.         $arr['redirect'] = $new_path; 
  216.     } 
  217.     die(json_encode($arr)); 
  218. } 
  219. //获取代码文件内容 
  220. $code = file_get_contents($curr_file); 
  221. $tree = '<ul id="dir_tree"> 
  222.     <li class="dir" path="/" onclick="load()">ROOT'.list_dir($curr_file, 'html').'</li> 
  223. </ul>'; 
  224. //登陆和设置密码共用模版 
  225. $first = <<<HTMLSTR 
  226. <!DOCTYPE html> 
  227. <html><head><meta charset="UTF-8"> 
  228. <title>【标题】</title> 
  229. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
  230. <style type="text/css" media="screen"> 
  231. body { 
  232.     overflow: hidden; background-color: #2D2D2D; color: #CCCCCC; font-size: 12px; margin: 0; 
  233.     font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace; 
  234. } 
  235. form { display: none; position: absolute; } 
  236. form h5 { font-size: 14px; font-weight: normal; margin: 0; line-height: 2em; } 
  237. form input { 
  238.     color: #fff; border: 1px solid #369; border-radius: 3px; background: #333; height: 22px; 
  239.     line-height: 1.6em; width: 125px; margin-right: 5px; vertical-align: middle; 
  240. } 
  241. form button { 
  242.     line-height: 1.6em; border: 1px solid #369; border-radius: 3px; 
  243.     background: #369; color: #fff; vertical-align: middle; 
  244. } 
  245. </style> 
  246. <link rel="stylesheet" href="{$tip['core']}" /> 
  247. <link rel="stylesheet" href="{$tip['css']}" /> 
  248. </head> 
  249. <body> 
  250.     <form method="post"> 
  251.         <input name="pwd" type="password" /><button type="submit">【动作】</button> 
  252.     </form> 
  253.     <script src="{$jquery}" type="text/javascript" charset="utf-8"></script> 
  254.     <script src="{$ace}" type="text/javascript" charset="utf-8"></script> 
  255.     <script src="{$tip['js']}" type="text/javascript"></script> 
  256.     <script type="text/javascript"> 
  257.     var editor = false; 
  258.     $(function(){ 
  259.         $('form').prepend('<h5>'+ document.title +'</h5>'); 
  260.         $('form').css({ 
  261.             left: ($(window).width()-$('form').width())/2, 
  262.             top: ($(window).height()-$('form').height())/2 
  263.         }); 
  264.         $('form').show(); 
  265.     }); 
  266.     </script> 
  267. </body></html> 
  268. HTMLSTR; 
  269. //判断是否第一次登录 
  270. if ( false === $pwd && emptyempty($_POST) ) { 
  271.     die(str_replace( 
  272.         array('【标题】', '【动作】'), 
  273.         array('第一次使用,请先设置密码!', '设置'), 
  274.         $first 
  275.     )); 
  276. } 
  277. //第一次设置登录密码 
  278. if ( false === $pwd && !emptyempty($_POST) ) { 
  279.     if ( isset($_POST['pwd']) && strlen($_POST['pwd']) ) { 
  280.         $pwd = $_SESSION['pwd'] = md5($_POST['pwd'].$ace); 
  281.         $code = preg_replace('#\$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1); 
  282.         file_put_contents($curr_file, $code); 
  283.     } else { 
  284.         reload(); 
  285.     } 
  286. } 
  287. //用户登录验证 
  288. if ( false !== $pwd && !emptyempty($_POST) ) { 
  289.     $tmp = md5($_POST['pwd'].$ace); 
  290.     if ( $tmp && $pwd && $tmp === $pwd ) { 
  291.         $_SESSION['pwd'] = $pwd; 
  292.         reload(); 
  293.     } 
  294. } 
  295. //处理一下html实体 
  296. $code = htmlspecialchars($code); 
  297. $dir_icon = str_replace(array("\r\n", "\r", "\n"), '', 
  298. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAANCAYAAACgu+4kAAAAGXRFWHRTb2Z0d2 
  299. FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wR 
  300. sCjQEcIY3DiiJUYiqRhp5Mra/92YSUVVgLSW49B7H+NApRh75XkHfFoCG+02tyflUeQTw2y9UYYP8cCStc9SM 
  301. PeVA/Sy6Dw555q3au1z+EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiHZgVqPzLO+8seai6E2jed42bCL06tNyEH 
  302. AX9kv3jh3HqH7BctFWLMOmAbcg05mHK5+sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6 
  303. MtRED4bwsHLnUzxEeKac3+GeP6eo8yevhjC3F1qC4CDAAl3HwuyNAIdwAAAABJRU5ErkJggg=='); 
  304. $file_icon = str_replace(array("\r\n", "\r", "\n"), '', 
  305. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2 
  306. FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS1JREFUeNqMU01KxkAMTaez7aYbNwreQdBzeopS6EXEW+jug7Z 
  307. C6X+/iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFK/U+0hwJAAMjmcm 
  308. DsOA4zge6Pseu67DpmlEqK5rLMvyRkDJor6uq2SGktu2FfdpmpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ 
  309. qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5nh/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF 
  310. 0iC8QmPibEtT4hftrhHI95JqJT/HC2JOt0to+zN6MVsZ/oZKqwmyCTA33DkbN1sws0i+Pega6v0kd42H9JB/8 
  311. LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII='); 
  312. $loading = str_replace(array("\r\n", "\r", "\n"), '', 
  313. 'data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP+TMsZiALlcAKNOAOp4ANVqAP+PFv///wAAAAAAAA 
  314. AAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAIACwAAAAAFAAUAAAEUxDJSau9iBDMteb 
  315. TMEjehgTBJYqkiaLWOlZvGs8WDO6UIPCHw8TnAwWDEuKPcxQml0Ynj2cwYACAS7VqwWItWyuiUJB4s2AxmWxG 
  316. g9bl6YQtl0cAACH5BAUKAAgALAEAAQASABIAAAROEMkpx6A4W5upENUmEQT2feFIltMJYivbvhnZ3Z1h4FMQI 
  317. Dodz+cL7nDEn5CH8DGZhcLtcMBEoxkqlXKVIgAAibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAA 
  318. ROEMkphaA4W5upMdUmDQP2feFIltMJYivbvhnZ3V1R4BNBIDodz+cL7nDEn5CH8DGZAMAtEMBEoxkqlXKVIg4 
  319. HibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpjaE4W5tpKdUmCQL2feFIltMJYivbvhnZ 
  320. 3R0A4NMwIDodz+cL7nDEn5CH8DGZh8ONQMBEoxkqlXKVIgIBibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAA 
  321. QASABIAAAROEMkpS6E4W5spANUmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodz+cL7nDEn5CH8DGZgcBtMMBEox 
  322. kqlXKVIggEibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpAaA4W5vpOdUmFQX2feFIltM 
  323. JYivbvhnZ3V0Q4JNhIDodz+cL7nDEn5CH8DGZBMJNIMBEoxkqlXKVIgYDibbK9YLBYvLtHH5K0J0IACH5BAUK 
  324. AAgALAEAAQASABIAAAROEMkpz6E4W5tpCNUmAQD2feFIltMJYivbvhnZ3R1B4FNRIDodz+cL7nDEn5CH8DGZg 
  325. 8HNYMBEoxkqlXKVIgQCibbK9YLBYvLtHH5K0J0IACH5BAkKAAgALAEAAQASABIAAAROEMkpQ6A4W5spIdUmHQ 
  326. f2feFIltMJYivbvhnZ3d0w4BMAIDodz+cL7nDEn5CH8DGZAsGtUMBEoxkqlXKVIgwGibbK9YLBYvLtHH5K0J0 
  327. IADs='); 
  328. //编辑器模版 
  329. $html = <<<HTMLSTR 
  330. <!DOCTYPE html> 
  331. <html><head><meta charset="UTF-8"> 
  332. <title>ACE代码编辑器</title> 
  333. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
  334. <style type="text/css" media="screen"> 
  335. a { text-decoration: none; } 
  336. body { 
  337.     overflow: hidden; background-color: #2D2D2D; font-size: 12px; 
  338.     font-family: 'Consolas', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace; 
  339.     scrollbar-arrow-color: #ccc; scrollbar-base-color: #333; 
  340.     scrollbar-dark-shadow-color: #00ffff; scrollbar-track-color: #272822; 
  341.     scrollbar-highlight-color: #272822; scrollbar-3d-light-color: #272822; 
  342.     scrollbar-face-color: #2D2D2D; scrollbar-shadow-color: #333; 
  343. } 
  344. ::-webkit-scrollbar { width:5px; height:6px; background-color:#444; } 
  345. ::-webkit-scrollbar:hover { background-color:#444; } 
  346. ::-webkit-scrollbar-thumb:hover { min-height:5px; min-width:5px; background-color: #AAA; } 
  347. ::-webkit-scrollbar-thumb:active { -webkit-border-radius:20px; background-color: #AAA; } 
  348. ::-webkit-scrollbar-thumb { 
  349.     min-height:5px; min-width:5px; -webkit-border-radius:20px; 
  350.     ::-webkit-border-radius:1px; background-color: #AAA; 
  351. } 
  352. body > pre { color: #666; } 
  353. #sider { margin: 0; position: absolute; top:  25px; bottom: 0; left: 0; right: 85%; } 
  354. #editor { margin: 0; position: absolute; top: 0; bottom: 0; left: 15%; right: 0; } 
  355. #dir_tree { margin:0; padding: 0; height: 100%; overflow: auto; position: relative; left: 5px; } 
  356. #dir_tree, #dir_tree ul, #dir_tree li { margin: 0; padding: 0; list-style: none inside; } 
  357. #dir_tree ul { padding-left: 20px; position: relative; } 
  358. #dir_tree li { text-indent: 2em; line-height: 1.6em; cursor: default; color: #ccc; } 
  359. #dir_tree li.hover > span, #dir_tree li:hover > span { color: #66D9EF; } 
  360. #dir_tree li#on > span { color: red; } 
  361. #dir_tree li.dir { background: url({$dir_icon}) no-repeat 3px 3px; } 
  362. #dir_tree li.file { background: url({$file_icon}) no-repeat 3px 0; } 
  363. #dir_tree li.loading { background: url({$loading}) no-repeat 3px 0; } 
  364. #logout { position: absolute; top: 0; left: 0; } 
  365. #logout a { display: inline-block; color: #aaa; line-height: 25px; padding: 0 4px; } 
  366. #logout a:hover { background: #000; color: #ddd; } 
  367. #contextmenu { position: absolute; top: 0; left: 0; background: #fff; color: #333; border: 1px solid #000; padding: 1px; } 
  368. #contextmenu span { display: block; line-height: 24px; text-indent: 20px; width: 80px; cursor: default; } 
  369. #contextmenu span:hover { background-color: #369; color: #fff; } 
  370. #alertify .alertify-message, #alertify .alertify-message { 
  371.     text-align: left !important; text-indent: 0; font-weight: bold; font-size: 16px; 
  372. } 
  373. #alertify .alertify-dialog, #alertify .alertify-dialog { 
  374.     font-family: 'Consolas'; padding: 10px !important; color: #333 !important; 
  375. } 
  376. #alertify .alertify-button { 
  377.     border-radius: 3px !important; font-weight: normal !important; 
  378.     font-size: 14px !important; padding: 3px 15px !important; 
  379. } 
  380. .alertify-buttons { text-align: right !important; } 
  381. </style> 
  382. <link rel="stylesheet" href="{$tip['core']}" /> 
  383. <link rel="stylesheet" href="{$tip['css']}" /> 
  384. </head><body> 
  385. <div id="logout"> 
  386.     <a href="javascript:void(0);">保存</a> 
  387.     <a href="javascript:void(0);">刷新</a> 
  388.     <a href="javascript:void(0);">重置</a> 
  389.     <a href="?logout">退出</a> 
  390. </div> 
  391. <div id="sider">{$tree}</div><pre id="editor">{$code}</pre> 
  392. <script src="{$jquery}" type="text/javascript" charset="utf-8"></script> 
  393. <script src="{$ace}" type="text/javascript" charset="utf-8"></script> 
  394. <script src="{$tip['js']}" type="text/javascript"></script> 
  395. <script type="text/javascript"> 
  396. var load = false; 
  397. var curr_file = false; 
  398. window.location.hash = ''; 
  399. alertify.set({delay: 1000}); //n秒后自动消失 
  400. alertify.set({labels: {ok:'确定',cancel:'取消'}}); 
  401. var editor = false; 
  402. $(function(){ 
  403.     //实例化代码编辑器 
  404.     editor = ace.edit("editor"); 
  405.     //设置编辑器的语法和高亮 
  406.     editor.setTheme("ace/theme/monokai"); 
  407.     editor.getSession().setMode("ace/mode/php"); 
  408.     //设置编辑器自动换行 
  409.     editor.getSession().setWrapLimitRange(null, null); 
  410.     editor.getSession().setUseWrapMode(true); 
  411.     //不显示垂直衬线 
  412.     editor.renderer.setShowPrintMargin(false); 
  413.     //editor.setReadOnly(true); //设置编辑器为只读 
  414.     //editor.gotoLine(325); //跳转到指定行 
  415.     //使编辑器获得输入焦点 
  416.     editor.focus(); 
  417.     //绑定组合按键 
  418.     var commands = editor.commands; 
  419.     commands.addCommand({ 
  420.         name: "save", 
  421.         bindKey: {win: "Ctrl-S", mac: "Command-S"}, 
  422.         exec: save_file 
  423.     }); 
  424.     //保存动作 
  425.     function save_file() { 
  426.         if ( false == editor ) { return false; } 
  427.         var obj = { 
  428.             content: editor.getValue(), 
  429.             action: 'save_file' 
  430.         }; 
  431.         if ( false !== curr_file ) { 
  432.             obj.file_path = curr_file; 
  433.         } 
  434.         alertify.log('正在保存...'); 
  435.         $.post(window.location.href, obj, function(data){ 
  436.             if ( data.msg && 'success' == data.result ) { 
  437.                 alertify.success(data.msg); 
  438.             } else { 
  439.                 alertify.error(data.msg); 
  440.             } 
  441.         }, 'json'); 
  442.     } 
  443.     //加载目录列表或文件 
  444.     load = function(ele) { 
  445.         var curr = $(event.srcElement); 
  446.         if ( ele ) { curr = ele; } 
  447.         if ( curr.is('span') ) { curr = curr.parent('li'); } 
  448.         $('#dir_tree #on').removeAttr('id'); 
  449.         curr.attr('id', 'on'); 
  450.         var type = curr.attr('class'); 
  451.         var path = curr.attr('path'); 
  452.         window.location.hash = path; 
  453.         if ( 'file' === type ) { 
  454.             alertify.log('正在加载...'); 
  455.             curr.addClass('loading'); 
  456.             $.post(window.location.href, {file:path}, function(data){ 
  457.                 curr.removeClass('loading'); 
  458.                 if ( data.mode ) { 
  459.                     editor.getSession().setMode("ace/mode/"+data.mode); 
  460.                 } 
  461.                 //注意,空文件应当允许编辑 
  462.                 if ( true || data.html ) { 
  463.                     curr.attr('disabled', 'disabled'); 
  464.                     curr_file = path; //当前编辑的文件路径 
  465.                     //动态赋值编辑器中的内容 
  466.                     editor.session.doc.setValue(data.html); 
  467.                     editor.renderer.scrollToRow(0); //滚动到第一行 
  468.                     editor.focus(); //编辑器获得焦点 
  469.                     setTimeout(function(){ 
  470.                         editor.gotoLine(0); 
  471.                     }, 800); 
  472.                 } 
  473.             }, 'json'); 
  474.             event.stopPropagation(); 
  475.             event.preventDefault(); 
  476.             return false; 
  477.         } 
  478.         if ( 'dir' === type ) { 
  479.             if ( curr.attr('loaded') ) { 
  480.                 curr.children('ul').toggle(); 
  481.                 event.stopPropagation(); 
  482.                 event.preventDefault(); 
  483.                 return false; 
  484.             } else { 
  485.                 curr.attr('loaded', 'yes'); 
  486.             } 
  487.             alertify.log('正在加载...'); 
  488.             curr.addClass('loading'); 
  489.             $.post(window.location.href, {dir:path}, function(data){ 
  490.                 curr.find('ul').remove(); 
  491.                 curr.removeClass('loading'); 
  492.                 if ( data.html ) { 
  493.                     curr.append(data.html); 
  494.                 } 
  495.             }, 'json'); 
  496.         } 
  497.         return false; 
  498.     } 
  499.     //绑定右键菜单 
  500.     $('#sider').bind('contextmenu', function(e){ 
  501.         var path = false; 
  502.         var target = $(event.srcElement); 
  503.         if ( target.is('span') ) { 
  504.             target = target.parent('li'); 
  505.         } 
  506.         if ( target.attr('path') ) { 
  507.             path = target.attr('path'); 
  508.         } else { 
  509.             return false; 
  510.         } 
  511.         target.addClass('hover'); 
  512.         var right_menu = $('#contextmenu'); 
  513.         if ( !right_menu.get(0) ) { 
  514.             var timer = false; 
  515.             right_menu = $('<div id="contextmenu"></div>'); 
  516.             right_menu.hover(function(){ 
  517.                 if ( timer ) { clearTimeout(timer); } 
  518.             }, function(){ 
  519.                 timer = setTimeout(function(){ 
  520.                     hide_menu(right_menu); 
  521.                 }, 500); 
  522.             }); 
  523.             $('body').append(right_menu); 
  524.         } 
  525.         if ( path ) { 
  526.             right_menu.html(''); 
  527.             var menu = $('<span>新建</span><span>浏览</span><span>重命名</span><span>删除</span>'); 
  528.             right_menu.append(menu); 
  529.             menu_area(right_menu, {left: e.pageX, top: e.pageY}); 
  530.             right_menu.find('span').click(function(){ 
  531.                 switch ( $(this).text() ) { 
  532.                     case '新建' : create_new(target, path); break; 
  533.                     case '浏览' : preview(target, path); break; 
  534.                     case '重命名' : re_name(target, path); break; 
  535.                     case '删除' : del_file(target, path); break; 
  536.                 } 
  537.                 hide_menu(right_menu); 
  538.             }); 
  539.         } 
  540.         path ? right_menu.show() : hide_menu(right_menu); 
  541.         return false; 
  542.     }); 
  543.     //隐藏右键菜单 
  544.     function hide_menu(menu) { 
  545.         $('#sider li.hover').removeClass('hover'); 
  546.         if ( menu ) { 
  547.             menu.hide(); 
  548.         } 
  549.     } 
  550.     //右键菜单区域 
  551.     function menu_area(menu, cfg) { 
  552.         if ( menu && cfg ) { 
  553.             var w = $('#sider').width() - menu.width(); 
  554.             var h = $('#sider').height() - menu.height(); 
  555.             if ( cfg.left > w ) { cfg.left = w; } 
  556.             if ( cfg.top > h ) { cfg.top = h; } 
  557.             menu.css(cfg); 
  558.         } 
  559.     } 
  560.     //保存按钮 
  561.     $('#logout>a:contains("保存")').click(function(){ 
  562.         save_file(); 
  563.         return false; 
  564.     }); 
  565.     //刷新按钮 
  566.     $('#logout>a:contains("刷新")').click(function(){ 
  567.         window.location.href = window.location.pathname; 
  568.         return false; 
  569.     }); 
  570.     //重置按钮 
  571.     $('#logout>a:contains("重置")').click(function(){ 
  572.         alertify.confirm('是否修改 {$curr_file_path} 程序文件名?', function (e) { 
  573.             if ( !e ) { return 'cancel'; } 
  574.             re_name($('<a>'), '{$curr_file_path}'); 
  575.         }); 
  576.         return false; 
  577.     }); 
  578.     //新建操作 
  579.     function create_new(obj, path) { 
  580.         if ( !obj || !path ) { return false; } 
  581.         alertify.prompt('请输入新建文件或文件夹名:', function (e, str) { 
  582.             if ( !e || !str ) { return false; } 
  583.             alertify.log('正在操作中...'); 
  584.             $('#dir_tree #on').removeAttr('loaded').removeAttr('id'); 
  585.             $.post(window.location.href, {create:str,target:path}, function(data){ 
  586.                 if ( data.msg && 'success' == data.result ) { 
  587.                     alertify.success(data.msg); 
  588.                     if ( obj.attr('class') == 'dir' ) { 
  589.                         load(obj); //重新加载子节点 
  590.                     } else { 
  591.                         load(obj.parent().parent()); 
  592.                     } 
  593.                 } else { 
  594.                     alertify.error(data.msg); 
  595.                 } 
  596.             }, 'json'); 
  597.         }); 
  598.     } 
  599.     //浏览操作 
  600.     function preview(obj, path) { 
  601.         if ( !obj || !path ) { return false; } 
  602.         window.open(path, '_blank'); 
  603.     } 
  604.     //重命名 
  605.     function re_name(obj, path) { 
  606.         if ( !obj || !path ) { return false; } 
  607.         alertify.prompt('重命名 '+path+' 为:', function (e, str) { 
  608.             if ( !e || !str ) { return false; } 
  609.             alertify.log('正在操作中...'); 
  610.             $.post(window.location.href, {rename:str,target:path}, function(data){ 
  611.                 if ( data.msg && 'success' == data.result ) { 
  612.                     alertify.success(data.msg); 
  613.                     if ( data.redirect ) { 
  614.                         window.location.href = data.redirect; 
  615.                     } 
  616.                     if ( data.new_name ) { 
  617.                         obj.children('span').first().text(data.new_name); 
  618.                         obj.attr('path', data.new_path); 
  619.                     } 
  620.                 } else { 
  621.                     alertify.error(data.msg); 
  622.                 } 
  623.             }, 'json'); 
  624.         }); 
  625.     } 
  626.     //删除文件动作 
  627.     function del_file(obj, path) { 
  628.         if ( !obj || !path ) { return false; } 
  629.         alertify.confirm('您确定要删除:'+path+' 吗?', function (e) { 
  630.             if ( !e ) { return 'cancel'; } 
  631.             alertify.log('正在删除中...'); 
  632.             $.post(window.location.href, {del:path}, function(data){ 
  633.                 if ( data.msg && 'success' == data.result ) { 
  634.                     alertify.success(data.msg); 
  635.                     obj.remove(); 
  636.                 } else { 
  637.                     alertify.error(data.msg); 
  638.                 } 
  639.             }, 'json'); 
  640.         }); 
  641.     } 
  642. }); 
  643. </script> 
  644. </body></html> 
  645. HTMLSTR; 
  646. //判断是否已经登录 
  647. if ( !is_logged() ) { 
  648.     die(str_replace( 
  649.         array('【标题】', '【动作】'), 
  650.         array('请输入您第一次设置的密码!', '登录'), 
  651.         $first 
  652.     )); 
  653. } else { 
  654.     echo $html; 
  655. } 

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

Tags: php线代码编辑器

分享到: