Home > Backend Development > PHP Tutorial > Single-file version of online code editor aceditor

Single-file version of online code editor aceditor

WBOY
Release: 2016-07-25 08:47:33
Original
2099 people have browsed it
* Single-file online code editor editor.php version: v1.21
* It is very convenient to edit any text file on your website online. It is very useful for maintaining the website and writing code online
* Password encryption method:
* md5 (self-set password + $ace) //$ace is the cdn mirror address
*
* Instructions for use:
* 1. Confirm that the $pwd variable value is false, upload this file to the PHP space and access it
* 2. First You are prompted to set a password for each visit, set the password and remember it
* 3. After logging in with the password you set for the first time, this php file will be edited by default,
* 4. This file is the core file of the editor, please do not modify it at will
* 5. Please use the Ctrl + S key combination to save the edited file and wait for the execution result
* 6. After the save action is executed, please be sure to wait for the successful save message to return
* 7. The reset operation will modify the file name of this program to prevent others Guess the path
* 8. The refresh function only refreshes this program file and cannot refresh other
*
* It is recommended to use this editor in the chrome browser

See the project details
http://git.oschina.net/ymk18/aceditor Single-file version of online code editor aceditor
  1. /**
  2. * Single-file online code editor editor.php Version: v1.21
  3. *
  4. * Password encryption method:
  5. * md5 (self-set password + $ace) //$ace is the cdn mirror address
  6. *
  7. * How to use :
  8. * 1. Confirm that the $pwd variable value is false, upload this file to the PHP space and access it
  9. * 2. You will be prompted to set a password for the first time, set the password and remember it
  10. * 3. After logging in with the password you set for the first time , this php file is edited by default,
  11. * 4. This file is the core file of the editor, please do not modify it at will
  12. * 5. Please use the Ctrl + S key combination to save the edited file and wait for the execution result
  13. * 6. Save the action After execution, please be sure to wait for the successful save message to return
  14. * 7. The reset operation will modify the file name of this program to prevent others from guessing the path
  15. * 8. The refresh function only refreshes this program file and cannot refresh other ones
  16. *
  17. * Suggestions Use this editor in chrome browser
  18. */
  19. session_start();
  20. $curr_file = __FILE__; //Edit the current file by default
  21. $curr_file_path = str_replace(dirname(__FILE__), '', __FILE__) ;
  22. $pwd = false; //The default value of password initialization is false
  23. $ace = 'http://cdn.staticfile.org/ace/1.1.3/ace.js'; //Editor core 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. //The syntax parser corresponding to the file extension name
  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. //Determine whether the user is logged in
  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. //Reload Enter this page
  52. function reload() {
  53. $file = pathinfo(__FILE__, PATHINFO_BASENAME);
  54. die(header("Location: {$file}"));
  55. }
  56. //Determine whether the request is an ajax request
  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. //Destroy SESSION and 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. //Get a list of files in a directory
  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. //Filter out the superior directory, this level directory and the program’s own file name
  82. if ( !in_array($v, array(' .', '..')) ) {
  83. $file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
  84. if ( $full_path == __FILE__ ) {
  85. continue; // Shield itself The file does not appear in the list
  86. }
  87. $file = str_replace(dirname(__FILE__), '', $file);
  88. $file = str_replace("\", '/', $file); //Filter the path under win
  89. $file = str_replace('//', '/', $file); //Filter double slashes
  90. if ( is_dir($full_path) ) {
  91. if ( 'html' === $type ) {
  92. $v = '
  93. '.$v.' }
  94. array_push($lst['dir'], $v);
  95. } else {
  96. if ( 'html' === $type ) {
  97. $v = '
  98. '.$v.'
  99. ';
  100. }
  101. array_push($lst[ 'file'], $v);
  102. }
  103. }
  104. }
  105. $lst = array_merge($lst['dir'], $lst['file']);
  106. $lst = array_filter($lst);
  107. $ flag = $lst;
  108. if ( 'html' === $type ) {
  109. $flag = '
      '. implode('', $lst) .'
    ';
  110. }
  111. return $flag;
  112. }
  113. //Recursively delete a non-empty directory
  114. function deldir($dir) {
  115. $dh = opendir($dir);
  116. while ( $file = readdir($dh) ) {
  117. if ( $file != '. ' && $file != '..' ) {
  118. $fullpath = $dir.'/'.$file;
  119. if ( !is_dir($fullpath) ) {
  120. unlink($fullpath);
  121. } else {
  122. deldir ($fullpath);
  123. }
  124. }
  125. }
  126. return rmdir($dir);
  127. }
  128. //Log out
  129. if ( isset($_GET['logout']) ) {
  130. if ( exterminate() ) {
  131. reload();
  132. }
  133. }
  134. //ajax output file content
  135. if ( is_logged() && is_ajax() && isset($_POST['file']) ) {
  136. $file = dirname(__FILE__).$ _POST['file'];
  137. $ext = pathinfo($file, PATHINFO_EXTENSION);
  138. $mode = isset($lng[$ext]) ? $lng[$ext] : false;
  139. die(json_encode(array(
  140. 'file' => $file, 'html' => file_get_contents($file),
  141. 'mode' => $mode,
  142. )));
  143. }
  144. //ajax output directory list
  145. if ( is_logged () && is_ajax() && isset($_POST['dir']) ) {
  146. $dir = dirname(__FILE__).$_POST['dir'];
  147. $list_dir = list_dir($dir, 'html');
  148. die(json_encode(array(
  149. 'dir' => $dir, 'html' => $list_dir,
  150. )));
  151. }
  152. //ajax save file
  153. if ( is_logged() && is_ajax() && isset($_POST['action']) ) {
  154. $arr = array('result'=>'error', 'msg'=>'File saving failed! ');
  155. $content = $_POST['content'];
  156. if ( 'save_file' === $_POST['action'] ) {
  157. if ( isset($_POST['file_path']) ) {
  158. $ file = dirname(__FILE__).$_POST['file_path'];
  159. } else {
  160. $file = __FILE__;
  161. }
  162. file_put_contents($file, $content);
  163. $arr['result'] = 'success';
  164. $arr['msg'] = 'Save successfully! ';
  165. }
  166. die(json_encode($arr));
  167. }
  168. //ajax delete file or folder
  169. if ( is_logged() && is_ajax() && isset($_POST['del']) ) {
  170. $path = dirname(__FILE__).$_POST['del'];
  171. $arr = array('result'=>'error', 'msg'=>'Delete operation failed!');
  172. if ( $ _POST['del'] && $path ) {
  173. $flag = is_dir($path) ? deldir($path) : unlink($path);
  174. if ( $flag ) {
  175. $arr['msg'] = ' The deletion operation was successful! ';
  176. $arr['result'] = 'success';
  177. }
  178. }
  179. die(json_encode($arr));
  180. }
  181. //ajax creates a new file or folder
  182. if ( is_logged() && is_ajax( ) && isset($_POST['create']) ) {
  183. $flag = false;
  184. $arr = array('result'=>'error', 'msg'=>'Operation failed!');
  185. if ( isset($_POST['target']) ) {
  186. $target = dirname(__FILE__).$_POST['target'];
  187. $target = is_dir($target) ? $target : dirname($target);
  188. }
  189. if ( $_POST['create'] && $target ) {
  190. $base_name = pathinfo($_POST['create'], PATHINFO_BASENAME);
  191. $exp = explode('.', $base_name);
  192. $ full_path = $target.'/'.$base_name;
  193. $new_path = str_replace(dirname(__FILE__), '', $full_path);
  194. if ( count($exp) > 1 && isset($lng[array_pop($ exp)]) ) {
  195. file_put_contents($full_path, '');
  196. $arr['result'] = 'success';
  197. $arr['msg'] = 'New file successfully! ';
  198. $arr['type'] = 'file';
  199. } else {
  200. mkdir($full_path, 0777, true);
  201. $arr['result'] = 'success';
  202. $arr['msg' ] = 'Create new directory successfully! ';
  203. $arr['type'] = 'dir';
  204. }
  205. if ( $base_name && $new_path ) {
  206. $arr['new_name'] = $base_name;
  207. $arr['new_path'] = $new_path ;
  208. }
  209. }
  210. die(json_encode($arr));
  211. }
  212. //ajax rename file or folder
  213. if ( is_logged() && is_ajax() && isset($_POST['rename']) ) {
  214. $arr = array('result'=>'error', 'msg'=>'Rename operation failed!');
  215. if ( isset($_POST['target']) ) {
  216. $target = dirname(__FILE__).$_POST['target'];
  217. }
  218. if ( $_POST['rename'] ) {
  219. $base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME);
  220. if ( $base_name ) {
  221. $rename = dirname($target).'/'.$base_name;
  222. $new_path = str_replace(dirname(__FILE__), '', $rename);
  223. }
  224. }
  225. if ( $rename && $target && rename($target, $rename) ) {
  226. $arr['new_name'] = $base_name;
  227. $arr['new_path'] = $new_path;
  228. $arr['msg'] = 'Rename operation successful!';
  229. $arr['result'] = 'success';
  230. }
  231. if ( $target == __FILE__ ) {
  232. $arr['redirect'] = $new_path;
  233. }
  234. die(json_encode($arr));
  235. }
  236. //获取代码文件内容
  237. $code = file_get_contents($curr_file);
  238. $tree = '
    • ROOT'.list_dir($curr_file, 'html').'
    ';
  239. //登陆和设置密码共用模版
  240. $first = <<
  241. 【标题】
  242. HTMLSTR;
  243. //判断是否第一次登录
  244. if ( false === $pwd && empty($_POST) ) {
  245. die(str_replace(
  246. array('【标题】', '【动作】'),
  247. array('第一次使用,请先设置密码!', 'Settings'),
  248. $first
  249. ));
  250. }
  251. //Set the login password for the first time
  252. if ( false === $pwd && !empty($_POST) ) {
  253. if ( isset($ _POST['pwd']) && strlen($_POST['pwd']) ) {
  254. $pwd = $_SESSION['pwd'] = md5($_POST['pwd'].$ace);
  255. $code = preg_replace('#$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1);
  256. file_put_contents($curr_file, $code);
  257. } else {
  258. reload( );
  259. }
  260. }
  261. //User login verification
  262. if ( false !== $pwd && !empty($_POST) ) {
  263. $tmp = md5($_POST['pwd'].$ace);
  264. if ( $tmp && $pwd && $tmp === $pwd ) {
  265. $_SESSION['pwd'] = $pwd;
  266. reload();
  267. }
  268. }
  269. //Process the html entity
  270. $code = htmlspecialchars($code);
  271. $dir_icon = str_replace(array("rn", "r", "n"), '',
  272. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAANCAYAAACgu+4kAAAAGXRFWHRTb2Z0d2
  273. FyZQBBZG9iZSBJbWF nZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wR
  274. sCjQEcIY3DiiJUYiqRhp5Mra/92YSUVVgLSW49B7H +NApRh75XkHfFoCG+02tyflUeQTw2y9UYYP8cCStc9SM
  275. PeVA/Sy6Dw555q3au1z+EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiHZgVqPzLO+8seai6E2jed42bCL06tNyEH
  276. AX9kv3 jh3HqH7BctFWLMOmAbcg05mHK5+sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
  277. MtRED4bwsHLnUzxEeKac3+GeP6eo8yevhjC3F1qC4CDAAl3HwuyNAIdwAAAABJRU5ErkJg gg==');
  278. $file_icon = str_replace(array("rn", "r", "n"), '',
  279. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
  280. FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS1JREFUeNqMU01KxkAMTaez7aYbNwreQdBzeopS6EXEW+ju g7Z
  281. C6X+/iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFK/U+0hwJAAMjmcm
  282. DsOA4zge6Pseu67DpmlEqK5rLMvyRkDJor6uq2SGktu2Ffdp mpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ
  283. qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5n h/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF
  284. 0iC8QmPibEtT4hftrhHI95Jq JT/HC2JOt0to+zN6MVsZ/oZKqwmyCTA33DkbN1sws0i+Pega6v0kd42H9JB/8
  285. LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII= ');
  286. $loading = str_replace(array("rn", "r", "n"), '',
  287. 'data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP+TMsZiALLlcAKNOAOp4ANVqAP+PFv///wAAAAAAAAA
  288. AAAAAAAAAAAAAAAAAACH/ C05FVFNDQVBFMI4WAWEAAAh+ CAS7VQWWITWYUUUJB4S2AXMWXG
  289. G9BL6YQTL0CAACH5BAUKAAGAAGAALAEAQASAAAAAAROEMKPX6A4W5UPENUMEQT2FILTMJYIVBVHNZ3Z1H4FMQI
  290. DODZ+CL7ND EN5CH8DGZHCLTCMBEOXKQLXKVIGAAIBBK9YLBYVLTH5K0J0IACH5BAUKAGAL AEAAQASABIAAAAAAA4W5upMDQP2FILTMJYIVBVHNZ3V1R4BNBIDO DZ+CL7NDEN5CH8DGZAMAMBEOXKQLXKVIG4
  291. Hibbk9ylbyVlth5k0J0IACH5Baukaagalaeaaaaaaaaaaaaaaaemkpjae4W5TPKQL2fefiltMJYVHNZ
  292. 3
  293. 3
  294. 3
  295. 3
  296. 3
  297. 3 R0A4NMWIDODODZ+CL7NDEN5CH8DGZH8ONQMBEOXKQLXKVIGIGIGIBBK9YLBYVLTHH5K0J0IACH5BAUKAAGAAAAAAAAAAAAAAAAAROEMKPS6E4W5SPANUMGQB2FEFILTMJY IVBVHNZ3D1X4JMGIDODZ+CL7NDEN5CH8DGZGCBTMBEOX
  298. kqlxkviggeibbk9ylbyVLTH5K0J0IACH5Baukaagalaeaaaaaaaaaaaaa4W5VPODUMFQX 2Fefiltmijyivbvhnz3V0Q4JNHIDODZ+CL7nden5CH8DGZBMJNIMBEOXKQLXKVIGYDIBBK9YLBYHVLTH5K0J0IAGAAGAAGAAAAAAAAAAAAAAAAAAAAAAAAAROEMKPZ6E4E4 W5TPCNUMAQD2FEFILTMJYIVBVHNZ3R1B4FNRIDODZ+Cl7NDEN5CH8DGZGZHNYMBEOXKQLXKQCIGQCIGQCIGQCIBYVLTH5K0J0KKKAAQAQASABIAAA AROEMKPQ6A4W5SPIDUMHQ
  299. F2FEFILTMJYIVBVHNZ3D0W4BMAIDODZ+CL7NDEN5CH8DGZASGTUMKQLXKVIGIBK9ylby5k0J0J0
  300. iads = ');
  301. //// /Editor template
  302. $html = <<
  303. ACE code Editor
  304. 保存
  305. 刷新
  306. 重置
  307. 退出
  • {$tree}
    {$code}
  • HTMLSTR;
  • //Judgement Have you 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;
  • }
  • Copy code


    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template