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

Single-file version of online code editor aceditor

Jul 25, 2016 am 08:47 AM

* 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


    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1666
    14
    PHP Tutorial
    1273
    29
    C# Tutorial
    1252
    24
    Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

    In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

    PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

    PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

    PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

    PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

    PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

    PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

    PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

    PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

    PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

    See all articles