首頁  >  文章  >  後端開發  >  單檔版線上程式碼編輯器 aceditor

單檔版線上程式碼編輯器 aceditor

WBOY
WBOY原創
2016-07-25 08:47:332079瀏覽
* 單一檔案版線上程式碼編輯器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 單檔版線上程式碼編輯器 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. $flag = true;
  50. }
  51. }
  52. return $laglag; ;
  53. }
  54. //重新載入至本頁
  55. function reload() {
  56. $file = pathinfo(__FILE__, PATHINFO_BASENAME);
  57. die(header("Location: {$file}"));
  58. }
  59. //判斷請求是否為ajax請求
  60. function is_ajax() {
  61. $flag = false;
  62. if ( isset($ _SERVER['HTTP_X_REQUESTED_WITH']) ) {
  63. $flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  64. }
  65. return $flag;
  66. //銷毀SESSION和COOKIE
  67. function exterminate() {
  68. $_SESSION = array();
  69. foreach ( $_COOKIE as $key ) {
  70. setcookie($key, null);
  71. }
  72. session_destroy();
  73. $_COOKIE = array();
  74. return true;
  75. }
  76. //取得目錄下的檔案清單
  77. }
  78. //取得一個目錄下的檔案清單
  79. function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function list_dir($path, $type = 'array') {
  80. $flag = false;
  81. $lst = array('dir'=>array(), 'file'=>array());
  82. $base = !is_dir($path) ? dirname($path) : $path;
  83. $tmp = scandir($base);
  84. foreach ( $tmp as $k=>$v ) {
  85. //過濾掉上級目錄,本級目錄與程式本身檔名
  86. if ( !in_array($v, array('.', '..')) ) {
  87. $file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
  88. if ( $full_path == __FILE__ ) {
  89. continue; //屏蔽自身檔案不在列表中出現
  90. } $file = str_replace(dirname(__FILE__), '', $file);
  91. $file = str_replace("\", '/', $file); //過濾win下的路徑
  92. $file = str_replace(' //', '/', $file); //過濾雙斜線
  93. if ( is_dir($full_path) ) {
  94. if ( 'html' === $type ) {
  95. $v = '
  96. '.$v.'
  97. ';
  98. }
  99. array_push($lst['dir'], $v);
  100. } else {
  101. if ( 'html' === $type ) {
  102. $v = ' .'" onclick="load()">'.$v.'
  103. ';
  104. }
  105. array_push($lst['file'], $v);
  106. }
  107. }
  108. }
  109. $lst = array_merge($lst['dir'], $lst['file ']);
  110. $lst = array_filter($lst);
  111. $flag = $lst;
  112. if ( 'html' === $type ) {
  113. $flag = '
      '. implode('', $lst) .'
    '; } return $flag;}
  114. //遞歸刪除一個非空白目錄
  115. function deldir($dir) {
  116. $dh = opendir($dir);
  117. while ( $file = readdir($dh) ) {
  118. if ( $file != '.' && $file != '..' ) {
  119. $fullpath = $dir.'/'.$file;
  120. if ( !is_dir($fullpath ) ) {
  121. unlink($fullpath);
  122. } else {
  123. deldir($fullpath);
  124. }
  125. }
  126. }
  127. return rmdir($dir); 🎜>}
  128. //登出登入
  129. if ( isset($_GET['logout']) ) {
  130. if (exterminate() ) {
  131. reload();
  132. }
  133. }
  134. //ajax輸出檔案內容
  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輸出目錄清單
  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儲存檔案
  153. if ( is_logged() && is_ajax() && isset($_POST['action']) ) {
  154. $arr = array('result'=>'error', 'msg'=>'檔案儲存失敗! ');
  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'] = '保存成功! ';
  165. }
  166. die(json_encode($arr));
  167. }
  168. //ajax刪除檔案或資料夾
  169. if ( is_logged() && is_ajax() && isset() && isset ($_POST['del']) ) {
  170. $path = dirname(__FILE__).$_POST['del'];
  171. $arr = array('result'=>'error', 'msg' =>'刪除操作失敗! ');
  172. if ( $_POST['del'] && $path ) {
  173. $flag = is_dir($path) ? deldir($path) : unlink($path) ;
  174. if ( $flag ) {
  175. $arr['msg'] = '刪除操作成功! ';
  176. $arr['result'] = 'success';
  177. }
  178. }
  179. die(json_encode($arr));
  180. }
  181. //ajax新檔案或資料夾
  182. if ( is_logged() && is_ajax() && isset($_POST['create']) ) {
  183. $flag = false;
  184. $arr = array('result'= >'error', 'msg'=>'操作失敗! '];
  185. $target = is_dir($target) ? $target : dirname($target);
  186. }
  187. if ( $_POST['create'] && $target ) {
  188. $base_name = pathinfo($_POST['create'], PATHINFO_BASENAME);
  189. $exp = explode('.', $base_name);
  190. $full_path = $target.'/'.$base_name;
  191. $full_path = $target.'/'.$base_name;
  192. $ new_path = str_replace(dirname(__FILE__), '', $full_path);
  193. if ( count($exp) > 1 && isset($lng[array_pop($exp)]) ) {
  194. file_put_contents($full_path , '');
  195. $arr['result'] = 'success';
  196. $arr['msg'] = '新建文件成功! ';
  197. $arr['type'] = 'file';
  198. } else {
  199. mkdir($full_path, 0777, true);
  200. $arr['result'] = 'success' ;
  201. $arr['msg'] = '新建目錄成功! ';
  202. $arr['type'] = 'dir';
  203. }
  204. if ( $base_name && $new_path ) {
  205. $arr['new_name'] = $base_name;
  206. $arr['new_path'] = $new_path;
  207. }
  208. }
  209. die(json_encode($arr));
  210. }
  211. //ajax重命名檔案或資料夾
  212. if ( is_logged() && is_ajax() && isset($_POST['rename']) ) {
  213. $arr = array('result'=>'error', 'msg'=>'重新命名操作失敗! ( $_POST['rename'] ) {
  214. $base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME);
  215. if ( $base_name ) {
  216. $rename = dirname($target). '/'.$base_name;
  217. $new_path = str_replace(dirname(__FILE__), '', $rename);
  218. }
  219. }
  220. if ( $rename && $target && rename($target , $rename) ) {
  221. $arr['new_name'] = $base_name;
  222. $arr['new_path'] = $new_path;
  223. $arr['msg'] = '重命名操作成功!';
  224. $arr['result'] = '成功';
  225. }
  226. if ( $target == __FILE__ ) {
  227. $arr['redirect'] = $new_path;
  228. }
  229. die(json_encode($arr));
  230. }
  231. //取得程式碼檔案內容
  232. $code = file_get_contents($curr_file);
  233. $tree = '
  234. ROOT'.list_dir($curr_file, 'html').' li>
  235. ';
  236. //登陸與設定密碼消耗模版
  237. $first =
  238. 【標題】
  239. HTMLSTR;
  240. //判斷是否第一次登入
  241. if ( false === $pwd &&empty($_POST) ) {
  242. die(str_replace(
  243. array('【標題】', '【動作】'),
  244. array('第一次使用,請先設定密碼!', '設定'),
  245. $first
  246. ));
  247. }
  248. //第一次設定登入密碼
  249. if ( false === $pwd && !empty ($_POST) ) {
  250. if ( isset($_POST['pwd']) && strlen($_POST['pwd']) ) {
  251. $pwd = $_SESSION['pwd'] = md5( $_POST['pwd'].$ace);
  252. $code = preg_replace('#$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1) ;
  253. file_put_contents($curr_file, $code);
  254. } else {
  255. reload();
  256. }
  257. }
  258. //使用者登入驗證
  259. if ( false !== $pwd && !empty($_POST) ) {
  260. $tmp = md5($_POST['pwd'].$ace);
  261. if ( $tmp && $pwd && $tmp == = $pwd ) {
  262. $_SESSION['pwd'] = $pwd;
  263. reload();
  264. }
  265. }
  266. //處理一下html實體
  267. $ code = htmlspecialchars($code);
  268. $dir_icon = str_replace(array("rn", "r", "n"), '',
  269. 'data:image/jpg;base64,iVBORw0KGAAAFANSUhEUgGAAABAB 4kAAAAGXRFWHRTb2Z0d2
  270. FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wRPeVA/Sy6Dw555q3au1z EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiLOHyqPwemAiLO h3HqH7BctFWLMOmAbcg05mHK5 sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
  271. MtRED4bwsHLnUzxEeKac3wuP63838E)ExExSacxB]
  272. $file_icon = str_replace(array(" rn", "r", "n"), '',
  273. 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
  274. FyZQBBZG9iZCJGbKJGGJ ez7aYbNwreQdBzeopS6EXEW jug7Z
  275. C6X /iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFKRFPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFRFK/Umhw kDJor6uq2SGktu2FfdpmpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ
  276. qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5nh/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF
  277. 0iC8QmPib​​i4i​​nft 33DkbN1sws0i Pega6v0kd42H9JB/8
  278. LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII=');
  279. $loading = str_replace(array("rn" n"), '',
  280. 'data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP TMsZiALlcAKNOAOp4ANVqAP PFv///wAAAAAAAA
  281. AAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh QQFCgAIACwAAAAAFAAUAAAEUxDJSau9iBDMteb
  282. TMEjehgTBJYqkiaLWOlZvGs8WDO6UIPCHw8TnAwWDEuKPcxQml0Ynj2cwYACAS7VqwWItWyuiUJB4s2AxmWxG
  283. g9bl6YQtl0cAACH5BAUKAAgALAEAAQASABIAAAROEMkpx6A4W5upENUmEQT2feFIltMJYivbvhnZ3Z1h4FMQI
  284. Dodz cL7nDEn5CH8DGZhcLtcMBEoxkqlXKVIgAAibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAA
  285. ROEMkphaA4W5upMdUmDQP2feFIltMJYivbvhnZ3V1R4BNBIDodz cL7nDEn5CH8DGZAMAtEMBEoxkqlXKVIg4
  286. HibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpjaE4W5tpKdUmCQL2feFIltMJYivbvhnZ qlXKVIgIBibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAA
  287. QASABIAAAROEMkpS6E4W5spANUmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodzmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodz tHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpAaA4W5vpOdUmFQX2feFIltM
  288. JYivbvhnZ3V0Q4JNhIDodz AQASABIAAAROEMkpz6E4W5tpCNUmAQD2feFIltMJYivbvhnZ3R1B4FNRIDodz cL7nDEn5CH8DGZg
  289. 8HNYMBEoxkqlXKVIgQCiASAK9YLBYvL75KJLBYv f2feFIltMJYivbvhnZ3d0w4BMAIDodz cL7nDEn5CH8DGZAsGtUMBEoxkqlXKVIgwGibbK9YLBYvLtHH5K0J0
  290. IADs=');
  291. //編輯器模版
  292. $html =
  293. ACE程式碼編輯器
  294. 保存
  295. 刷新
  296. 重置
  297. 退出
  • {$tree}
    {$code}