Single-file version of the online code editor Aceditor

Source: Internet
Author: User
Tags save file
* Single-file version of the online Code Editor editor.php version: v1.21
* Very easy to edit any text file on your website online, it is very useful for maintaining website and writing code online.
* 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
*
* It is recommended to use this editor in Chrome browser

See the project in detail
Http://git.oschina.net/ymk18/aceditor
  1. /**
  2. * Single-file version of the online Code Editor editor.php version: v1.21
  3. *
  4. * Password encryption Method:
  5. * MD5 (password + $ace)//$ace for CDN Mirror Address
  6. *
  7. * How to use:
  8. * 1. Confirm that the $PWD variable value is false, upload this file to PHP space and access
  9. * 2. First access prompt set password, set password and remember
  10. * 3. After logging in with the password for the first time, this PHP file is edited by default,
  11. * 4. This file is the editor core file, please do not modify it arbitrarily
  12. * 5. Save the edited file by using Ctrl + S key combination, waiting for the result to be executed
  13. * 6. Be sure to wait for the save success message to return after the Save action is executed
  14. * 7. The reset operation modifies the file name of the program to prevent others guessing the path
  15. * 8. Refresh function is only to refresh the program files, cannot refresh other
  16. *
  17. * It is recommended to 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; Password initialization default value 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. Syntax parser for file suffix names
  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 if the user is logged on
  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 to this page
  52. function Reload () {
  53. $file = PathInfo (__file__, pathinfo_basename);
  54. Die (The header ("Location: {$file}"));
  55. }
  56. Determine if 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. Destroying session and Cookies
  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 under 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 parent directory, the 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 Mask itself file does not appear in 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 Slash
  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. Recursively delete a non-empty directory
  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. Sign Out
  131. if (Isset ($_get[' logout ')) {
  132. if (exterminate ()) {
  133. Reload ();
  134. }
  135. }
  136. Ajax Output File contents
  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 Output Directory List
  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 Save File
  155. if (is_logged () && is_ajax () && isset ($_post[' action ')) {
  156. $arr = Array (' Result ' = ' error ', ' msg ' = ' = ' File save failed! ');
  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 '] = ' saved successfully! ';
  167. }
  168. Die (Json_encode ($arr));
  169. }
  170. Ajax delete a file or folder
  171. if (is_logged () && is_ajax () && isset ($_post[' del ')) {
  172. $path = DirName (__file__). $_post[' del ';
  173. $arr = Array (' Result ' = ' error ', ' msg ' = ' = ' delete operation failed! ');
  174. if ($_post[' del '] && $path) {
  175. $flag = Is_dir ($path)? Deldir ($path): unlink ($path);
  176. if ($flag) {
  177. $arr [' msg '] = ' delete operation succeeded! ';
  178. $arr [' result '] = ' success ';
  179. }
  180. }
  181. Die (Json_encode ($arr));
  182. }
  183. Ajax new file or folder
  184. if (is_logged () && is_ajax () && isset ($_post[' create ')) {
  185. $flag = false;
  186. $arr = Array (' Result ' = ' error ', ' msg ' = ' = ' operation failed!) ');
  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 '] = ' new file succeeded! ';
  200. $arr [' type '] = ' file ';
  201. } else {
  202. mkdir ($full _path, 0777, true);
  203. $arr [' result '] = ' success ';
  204. $arr [' msg '] = ' new directory successful! ';
  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 renaming a file or folder
  215. if (is_logged () && is_ajax () && isset ($_post[' rename ')) {
  216. $arr = Array (' Result ' = ' error ', ' msg ' = ' = ' rename operation failed! ');
  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 '] = ' rename operation succeeded! ';
  231. $arr [' result '] = ' success ';
  232. }
  233. if ($target = = __file__) {
  234. $arr [' redirect '] = $new _path;
  235. }
  236. Die (Json_encode ($arr));
  237. }
  238. Get the contents of a code file
  239. $code = file_get_contents ($curr _file);
  240. $tree = '
    • ROOT '. List_dir ($curr _file, ' html '). '
    ';
  241. Login and Set Password common template
  242. $first = <<<>
  243. Title
  244. HTMLSTR;
  245. Determine if you are logged in for the first time
  246. if (false = = = $pwd && empty ($_post)) {
  247. Die (Str_replace (
  248. Array (' title ' ', ' ' action '),
  249. Array (' First use, please set the password first! ', ' Set '),
  250. $first
  251. ));
  252. }
  253. Set the login password for the first time
  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. User Login Verification
  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. Working with HTML entities
  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. Editor templates
  305. $html = <<<>
  306. Ace Code Editor
  307. Save
  308. Refresh
  309. Reset
  310. Exit
  311. {$tree}
    {$code}
  312. HTMLSTR;
  313. Determine if you are logged in
  314. if (!is_logged ()) {
  315. Die (Str_replace (
  316. Array (' title ' ', ' ' action '),
  317. Array (' Please enter the password you set for the first time! ', ' login '),
  318. $first
  319. ));
  320. } else {
  321. Echo $html;
  322. }
Copy Code
  • Related Article

    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.