当前位置:Gxlcms > PHP教程 > 单文件版在线代码编辑器aceditor

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

时间:2021-07-01 10:21:17 帮助过:62人阅读

* 单文件版在线代码编辑器 editor.php 版本: v1.21
* 非常方便地在线编辑您网站上的任意文本文件,对于维护网站,和在线写代码非常好用
* 密码加密方式:
* md5(自设密码+$ace) //$ace为cdn镜像地址
*
* 使用方法:
* 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问
* 2.第一次访问提示设置密码,设置密码并牢记
* 3.使用第一次设置的密码登录后,默认编辑的是本php文件,
* 4.本文件是编辑器核心文件,请不要随意修改
* 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果
* 6.保存动作执行后请务必等待保存成功信息返回
* 7.重置操作会修改本程序的文件名,以防他人猜测路径
* 8.刷新功能仅是刷新本程序文件,不能刷新其他
*
* 建议在 chrome 浏览器中使用本编辑器

项目详细见
http://git.oschina.net/ymk18/aceditor
  1. /**
  2. * 单文件版在线代码编辑器 editor.php 版本: v1.21
  3. *
  4. * 密码加密方式:
  5. * md5(自设密码+$ace) //$ace为cdn镜像地址
  6. *
  7. * 使用方法:
  8. * 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问
  9. * 2.第一次访问提示设置密码,设置密码并牢记
  10. * 3.使用第一次设置的密码登录后,默认编辑的是本php文件,
  11. * 4.本文件是编辑器核心文件,请不要随意修改
  12. * 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果
  13. * 6.保存动作执行后请务必等待保存成功信息返回
  14. * 7.重置操作会修改本程序的文件名,以防他人猜测路径
  15. * 8.刷新功能仅是刷新本程序文件,不能刷新其他
  16. *
  17. * 建议在 chrome 浏览器中使用本编辑器
  18. */
  19. session_start();
  20. $curr_file = __FILE__; //默认编辑当前文件
  21. $curr_file_path = str_replace(dirname(__FILE__), '', __FILE__);
  22. $pwd = false; //密码初始化默认值为 false
  23. $ace = 'http://cdn.staticfile.org/ace/1.1.3/ace.js'; //编辑器核心js
  24. $tip['core'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.core.min.css';
  25. $tip['css'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.default.min.css';
  26. $tip['js'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.min.js';
  27. $jquery = 'http://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js';
  28. if ( false !== $pwd ) {
  29. define('DEFAULT_PWD', $pwd);
  30. }
  31. //文件后缀名对应的语法解析器
  32. $lng = array(
  33. 'as' => 'actionscript', 'js' => 'javascript',
  34. 'php' => 'php', 'css' => 'css', 'html' => 'html',
  35. 'htm' => 'html', 'ini' => 'ini', 'json' => 'json',
  36. 'jsp' => 'jsp', 'txt' => 'text', 'sql' => 'mysql',
  37. 'xml' => 'xml', 'yaml' => 'yaml', 'py' => 'python',
  38. 'md' => 'markdown', 'htaccess' => 'apache_conf',
  39. 'bat' => 'batchfile', 'go' => 'golang',
  40. );
  41. //判断用户是否登录
  42. function is_logged() {
  43. $flag = false;
  44. if ( isset($_SESSION['pwd']) && defined('DEFAULT_PWD') ) {
  45. if ( $_SESSION['pwd'] === DEFAULT_PWD ) {
  46. $flag = true;
  47. }
  48. }
  49. return $flag;
  50. }
  51. //重新载入到本页面
  52. function reload() {
  53. $file = pathinfo(__FILE__, PATHINFO_BASENAME);
  54. die(header("Location: {$file}"));
  55. }
  56. //判断请求是否是ajax请求
  57. function is_ajax() {
  58. $flag = false;
  59. if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
  60. $flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  61. }
  62. return $flag;
  63. }
  64. //销毁SESSION和COOKIE
  65. function exterminate() {
  66. $_SESSION = array();
  67. foreach ( $_COOKIE as $key ) {
  68. setcookie($key, null);
  69. }
  70. session_destroy();
  71. $_COOKIE = array();
  72. return true;
  73. }
  74. //获取一个目录下的文件列表
  75. function list_dir($path, $type = 'array') {
  76. $flag = false;
  77. $lst = array('dir'=>array(), 'file'=>array());
  78. $base = !is_dir($path) ? dirname($path) : $path;
  79. $tmp = scandir($base);
  80. foreach ( $tmp as $k=>$v ) {
  81. //过滤掉上级目录,本级目录和程序自身文件名
  82. if ( !in_array($v, array('.', '..')) ) {
  83. $file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
  84. if ( $full_path == __FILE__ ) {
  85. continue; //屏蔽自身文件不在列表出现
  86. }
  87. $file = str_replace(dirname(__FILE__), '', $file);
  88. $file = str_replace("\\", '/', $file); //过滤win下的路径
  89. $file = str_replace('//', '/', $file); //过滤双斜杠
  90. if ( is_dir($full_path) ) {
  91. if ( 'html' === $type ) {
  92. $v = '
  93. '.$v.'
  94. ';
  95. }
  96. array_push($lst['dir'], $v);
  97. } else {
  98. if ( 'html' === $type ) {
  99. $v = '
  100. '.$v.'
  101. ';
  102. }
  103. array_push($lst['file'], $v);
  104. }
  105. }
  106. }
  107. $lst = array_merge($lst['dir'], $lst['file']);
  108. $lst = array_filter($lst);
  109. $flag = $lst;
  110. if ( 'html' === $type ) {
  111. $flag = '
      '. implode('', $lst) .'
    ';
  112. }
  113. return $flag;
  114. }
  115. //递归删除一个非空目录
  116. function deldir($dir) {
  117. $dh = opendir($dir);
  118. while ( $file = readdir($dh) ) {
  119. if ( $file != '.' && $file != '..' ) {
  120. $fullpath = $dir.'/'.$file;
  121. if ( !is_dir($fullpath) ) {
  122. unlink($fullpath);
  123. } else {
  124. deldir($fullpath);
  125. }
  126. }
  127. }
  128. return rmdir($dir);
  129. }
  130. //退出登录
  131. if ( isset($_GET['logout']) ) {
  132. if ( exterminate() ) {
  133. reload();
  134. }
  135. }
  136. //ajax输出文件内容
  137. if ( is_logged() && is_ajax() && isset($_POST['file']) ) {
  138. $file = dirname(__FILE__).$_POST['file'];
  139. $ext = pathinfo($file, PATHINFO_EXTENSION);
  140. $mode = isset($lng[$ext]) ? $lng[$ext] : false;
  141. die(json_encode(array(
  142. 'file' => $file, 'html' => file_get_contents($file),
  143. 'mode' => $mode,
  144. )));
  145. }
  146. //ajax输出目录列表
  147. if ( is_logged() && is_ajax() && isset($_POST['dir']) ) {
  148. $dir = dirname(__FILE__).$_POST['dir'];
  149. $list_dir = list_dir($dir, 'html');
  150. die(json_encode(array(
  151. 'dir' => $dir, 'html' => $list_dir,
  152. )));
  153. }
  154. //ajax保存文件
  155. if ( is_logged() && is_ajax() && isset($_POST['action']) ) {
  156. $arr = array('result'=>'error', 'msg'=>'文件保存失败!');
  157. $content = $_POST['content'];
  158. if ( 'save_file' === $_POST['action'] ) {
  159. if ( isset($_POST['file_path']) ) {
  160. $file = dirname(__FILE__).$_POST['file_path'];
  161. } else {
  162. $file = __FILE__;
  163. }
  164. file_put_contents($file, $content);
  165. $arr['result'] = 'success';
  166. $arr['msg'] = '保存成功!';
  167. }
  168. die(json_encode($arr));
  169. }
  170. //ajax删除文件或文件夹
  171. if ( is_logged() && is_ajax() && isset($_POST['del']) ) {
  172. $path = dirname(__FILE__).$_POST['del'];
  173. $arr = array('result'=>'error', 'msg'=>'删除操作失败!');
  174. if ( $_POST['del'] && $path ) {
  175. $flag = is_dir($path) ? deldir($path) : unlink($path);
  176. if ( $flag ) {
  177. $arr['msg'] = '删除操作成功!';
  178. $arr['result'] = 'success';
  179. }
  180. }
  181. die(json_encode($arr));
  182. }
  183. //ajax新建文件或文件夹
  184. if ( is_logged() && is_ajax() && isset($_POST['create']) ) {
  185. $flag = false;
  186. $arr = array('result'=>'error', 'msg'=>'操作失败!');
  187. if ( isset($_POST['target']) ) {
  188. $target = dirname(__FILE__).$_POST['target'];
  189. $target = is_dir($target) ? $target : dirname($target);
  190. }
  191. if ( $_POST['create'] && $target ) {
  192. $base_name = pathinfo($_POST['create'], PATHINFO_BASENAME);
  193. $exp = explode('.', $base_name);
  194. $full_path = $target.'/'.$base_name;
  195. $new_path = str_replace(dirname(__FILE__), '', $full_path);
  196. if ( count($exp) > 1 && isset($lng[array_pop($exp)]) ) {
  197. file_put_contents($full_path, '');
  198. $arr['result'] = 'success';
  199. $arr['msg'] = '新建文件成功!';
  200. $arr['type'] = 'file';
  201. } else {
  202. mkdir($full_path, 0777, true);
  203. $arr['result'] = 'success';
  204. $arr['msg'] = '新建目录成功!';
  205. $arr['type'] = 'dir';
  206. }
  207. if ( $base_name && $new_path ) {
  208. $arr['new_name'] = $base_name;
  209. $arr['new_path'] = $new_path;
  210. }
  211. }
  212. die(json_encode($arr));
  213. }
  214. //ajax重命名文件或文件夹
  215. if ( is_logged() && is_ajax() && isset($_POST['rename']) ) {
  216. $arr = array('result'=>'error', 'msg'=>'重命名操作失败!');
  217. if ( isset($_POST['target']) ) {
  218. $target = dirname(__FILE__).$_POST['target'];
  219. }
  220. if ( $_POST['rename'] ) {
  221. $base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME);
  222. if ( $base_name ) {
  223. $rename = dirname($target).'/'.$base_name;
  224. $new_path = str_replace(dirname(__FILE__), '', $rename);
  225. }
  226. }
  227. if ( $rename && $target && rename($target, $rename) ) {
  228. $arr['new_name'] = $base_name;
  229. $arr['new_path'] = $new_path;
  230. $arr['msg'] = '重命名操作成功!';
  231. $arr['result'] = 'success';
  232. }
  233. if ( $target == __FILE__ ) {
  234. $arr['redirect'] = $new_path;
  235. }
  236. die(json_encode($arr));
  237. }
  238. //获取代码文件内容
  239. $code = file_get_contents($curr_file);
  240. $tree = '
    • ROOT'.list_dir($curr_file, 'html').'
    ';
  241. //登陆和设置密码共用模版
  242. $first = <<
  243. 【标题】
  244. HTMLSTR;
  245. //判断是否第一次登录
  246. if ( false === $pwd && empty($_POST) ) {
  247. die(str_replace(
  248. array('【标题】', '【动作】'),
  249. array('第一次使用,请先设置密码!', '设置'),
  250. $first
  251. ));
  252. }
  253. //第一次设置登录密码
  254. if ( false === $pwd && !empty($_POST) ) {
  255. if ( isset($_POST['pwd']) && strlen($_POST['pwd']) ) {
  256. $pwd = $_SESSION['pwd'] = md5($_POST['pwd'].$ace);
  257. $code = preg_replace('#\$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1);
  258. file_put_contents($curr_file, $code);
  259. } else {
  260. reload();
  261. }
  262. }
  263. //用户登录验证
  264. if ( false !== $pwd && !empty($_POST) ) {
  265. $tmp = md5($_POST['pwd'].$ace);
  266. if ( $tmp && $pwd && $tmp === $pwd ) {
  267. $_SESSION['pwd'] = $pwd;
  268. reload();
  269. }
  270. }
  271. //处理一下html实体
  272. $code = htmlspecialchars($code);
  273. $dir_icon = str_replace(array("\r\n", "\r", "\n"), '',
  274. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAANCAYAAACgu+4kAAAAGXRFWHRTb2Z0d2
  275. FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wR
  276. sCjQEcIY3DiiJUYiqRhp5Mra/92YSUVVgLSW49B7H+NApRh75XkHfFoCG+02tyflUeQTw2y9UYYP8cCStc9SM
  277. PeVA/Sy6Dw555q3au1z+EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiHZgVqPzLO+8seai6E2jed42bCL06tNyEH
  278. AX9kv3jh3HqH7BctFWLMOmAbcg05mHK5+sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
  279. MtRED4bwsHLnUzxEeKac3+GeP6eo8yevhjC3F1qC4CDAAl3HwuyNAIdwAAAABJRU5ErkJggg==');
  280. $file_icon = str_replace(array("\r\n", "\r", "\n"), '',
  281. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
  282. FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS1JREFUeNqMU01KxkAMTaez7aYbNwreQdBzeopS6EXEW+jug7Z
  283. C6X+/iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFK/U+0hwJAAMjmcm
  284. DsOA4zge6Pseu67DpmlEqK5rLMvyRkDJor6uq2SGktu2FfdpmpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ
  285. qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5nh/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF
  286. 0iC8QmPibEtT4hftrhHI95JqJT/HC2JOt0to+zN6MVsZ/oZKqwmyCTA33DkbN1sws0i+Pega6v0kd42H9JB/8
  287. LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII=');
  288. $loading = str_replace(array("\r\n", "\r", "\n"), '',
  289. 'data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP+TMsZiALlcAKNOAOp4ANVqAP+PFv///wAAAAAAAA
  290. AAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAIACwAAAAAFAAUAAAEUxDJSau9iBDMteb
  291. TMEjehgTBJYqkiaLWOlZvGs8WDO6UIPCHw8TnAwWDEuKPcxQml0Ynj2cwYACAS7VqwWItWyuiUJB4s2AxmWxG
  292. g9bl6YQtl0cAACH5BAUKAAgALAEAAQASABIAAAROEMkpx6A4W5upENUmEQT2feFIltMJYivbvhnZ3Z1h4FMQI
  293. Dodz+cL7nDEn5CH8DGZhcLtcMBEoxkqlXKVIgAAibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAA
  294. ROEMkphaA4W5upMdUmDQP2feFIltMJYivbvhnZ3V1R4BNBIDodz+cL7nDEn5CH8DGZAMAtEMBEoxkqlXKVIg4
  295. HibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpjaE4W5tpKdUmCQL2feFIltMJYivbvhnZ
  296. 3R0A4NMwIDodz+cL7nDEn5CH8DGZh8ONQMBEoxkqlXKVIgIBibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAA
  297. QASABIAAAROEMkpS6E4W5spANUmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodz+cL7nDEn5CH8DGZgcBtMMBEox
  298. kqlXKVIggEibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpAaA4W5vpOdUmFQX2feFIltM
  299. JYivbvhnZ3V0Q4JNhIDodz+cL7nDEn5CH8DGZBMJNIMBEoxkqlXKVIgYDibbK9YLBYvLtHH5K0J0IACH5BAUK
  300. AAgALAEAAQASABIAAAROEMkpz6E4W5tpCNUmAQD2feFIltMJYivbvhnZ3R1B4FNRIDodz+cL7nDEn5CH8DGZg
  301. 8HNYMBEoxkqlXKVIgQCibbK9YLBYvLtHH5K0J0IACH5BAkKAAgALAEAAQASABIAAAROEMkpQ6A4W5spIdUmHQ
  302. f2feFIltMJYivbvhnZ3d0w4BMAIDodz+cL7nDEn5CH8DGZAsGtUMBEoxkqlXKVIgwGibbK9YLBYvLtHH5K0J0
  303. IADs=');
  304. //编辑器模版
  305. $html = <<
  306. ACE代码编辑器
  307. 保存
  308. 刷新
  309. 重置
  310. 退出
  311. {$tree}
    {$code}
  312. HTMLSTR;
  313. //判断是否已经登录
  314. if ( !is_logged() ) {
  315. die(str_replace(
  316. array('【标题】', '【动作】'),
  317. array('请输入您第一次设置的密码!', '登录'),
  318. $first
  319. ));
  320. } else {
  321. echo $html;
  322. }

人气教程排行