* 單一檔案版線上程式碼編輯器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
-
/**
- * 單檔版線上程式碼編輯器editor.php 版本: v1.21
- *
- * 密碼加密方式:
- * md5(自設密碼$ace) //$ace為cdn鏡像位址
- *
- * 使用方法:
- * 1.確認$pwd 變數值為false, 上傳本檔案到PHP空間並存取
- * 2.第一次存取提示設定密碼,設定密碼並牢記
- * 3.使用第一次設定的密碼登入後,預設編輯的是本php檔案,
- * 4.本檔案是編輯器核心檔案,請不要隨意修改
- * 5.儲存編輯的檔案請用Ctrl S 按鍵組合,等待執行結果
- * 6.儲存動作執行後請務必等待儲存成功資訊回傳
- * 7.重設操作會修改本程式的檔案名,以防他人猜測路徑
- * 8.刷新功能僅是刷新本程式文件,不能刷新其他
- *
- * 建議在chrome 瀏覽器中使用本編輯器
- */
- session_start();
- $curr_file = __FILE__; //預設編輯目前檔案
- $curr_file_path = str_replace(dirname(__FILE__), '', __FILE__);
- $pwd = false; //密碼初始化預設值為false
- $ace = 'http://cdn.staticfile.org/ace /1.1.3/ace.js'; //編輯器核心js
- $tip['core'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.core. min.css';
- $tip['css'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.default.min.css';
- $tip[ 'js'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.min.js';
- $jquery = 'http://cdn.staticfile.org/jquery/ 2.1.1-rc2/jquery.min.js';
-
- if ( false !== $pwd ) {
- define('DEFAULT_PWD', $pwd);
- }
-
- //檔案後綴名對應的語法解析器
- $lng = array(
- 'as' => 'actionscript', 'js' => 'javascript',
- 'php' => ' php', 'css' => 'css', 'html' => 'html',
- 'htm' => 'html', 'ini' => 'ini', 'json' => 'json' ,
- 'jsp' => 'jsp', 'txt' => 'text', 'sql' => 'mysql',
- 'xml' => 'xml', 'yaml' => 'yaml ', 'py' => 'python',
- 'md' => 'markdown', 'htaccess' => 'apache_conf',
- 'bat' => 'batchfile', 'go' => ' golang',
- );
-
- //判斷使用者是否登入
- function is_logged() {
- $flag = false;
- if ( isset($_SESSION['pwd'] ) && defined('DEFAULT_PWD') ) {
- if ( $_SESSION['pwd'] === DEFAULT_PWD ) {
- $flag = true;
- }
- }
- $flag = true;
- }
- }
- return $laglag; ;
- }
-
- //重新載入至本頁
- function reload() {
- $file = pathinfo(__FILE__, PATHINFO_BASENAME);
- die(header("Location: {$file}"));
- }
-
- //判斷請求是否為ajax請求
- function is_ajax() {
- $flag = false;
- if ( isset($ _SERVER['HTTP_X_REQUESTED_WITH']) ) {
- $flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
- }
- return $flag;
- //銷毀SESSION和COOKIE
- function exterminate() {
- $_SESSION = array();
- foreach ( $_COOKIE as $key ) {
- setcookie($key, null);
- }
- session_destroy();
- $_COOKIE = array();
- return true;
- }
-
- //取得目錄下的檔案清單
- }
-
- //取得一個目錄下的檔案清單
- function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function🎜>function list_dir($path, $type = 'array') {
- $flag = false;
- $lst = array('dir'=>array(), 'file'=>array());
- $base = !is_dir($path) ? dirname($path) : $path;
- $tmp = scandir($base);
- foreach ( $tmp as $k=>$v ) {
- //過濾掉上級目錄,本級目錄與程式本身檔名
- if ( !in_array($v, array('.', '..')) ) {
- $file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
- if ( $full_path == __FILE__ ) {
- continue; //屏蔽自身檔案不在列表中出現
- } $file = str_replace(dirname(__FILE__), '', $file);
- $file = str_replace("\", '/', $file); //過濾win下的路徑
- $file = str_replace(' //', '/', $file); //過濾雙斜線
- if ( is_dir($full_path) ) {
- if ( 'html' === $type ) {
- $v = '
- '.$v.'
'; - }
- array_push($lst['dir'], $v);
- } else {
- if ( 'html' === $type ) {
- $v = ' .'" onclick="load()">'.$v.'
';- }
- array_push($lst['file'], $v);
- }
- }
- }
- $lst = array_merge($lst['dir'], $lst['file ']);
- $lst = array_filter($lst);
- $flag = $lst;
- if ( 'html' === $type ) {
- $flag = ''; } return $flag;}
-
- //遞歸刪除一個非空白目錄
- function deldir($dir) {
- $dh = opendir($dir);
- while ( $file = readdir($dh) ) {
- if ( $file != '.' && $file != '..' ) {
- $fullpath = $dir.'/'.$file;
- if ( !is_dir($fullpath ) ) {
- unlink($fullpath);
- } else {
- deldir($fullpath);
- }
- }
- }
- return rmdir($dir); 🎜>}
-
- //登出登入
- if ( isset($_GET['logout']) ) {
- if (exterminate() ) {
- reload();
- }
- }
-
- //ajax輸出檔案內容
- if ( is_logged() && is_ajax() && isset($_POST['file']) ) {
- $file = dirname( __FILE__).$_POST['file'];
- $ext = pathinfo($file, PATHINFO_EXTENSION);
- $mode = isset($lng[$ext]) ? $lng[$ext] : false;
- die(json_encode(array(
- 'file' => $file, 'html' => file_get_contents($file),
- 'mode' => $mode,
- )));
- }
-
- //ajax輸出目錄清單
- if ( is_logged() && is_ajax() && isset($_POST['dir']) ) {
- $dir = dirname(__FILE__) .$_POST['dir'];
- $list_dir = list_dir($dir, 'html');
- die(json_encode(array(
- 'dir' => $dir, 'html' => $list_dir,
- )));
- }
-
- //ajax儲存檔案
- if ( is_logged() && is_ajax() && isset($_POST['action']) ) {
- $arr = array('result'=>'error', 'msg'=>'檔案儲存失敗! ');
- $content = $_POST['content'];
- if ( 'save_file' === $_POST['action'] ) {
- if (isset($_POST['file_path' ]) ) {
- $file = dirname(__FILE__).$_POST['file_path'];
- } else {
- $file = __FILE__;
- }
- file_put_contents($file, $ content);
- $arr['result'] = 'success';
- $arr['msg'] = '保存成功! ';
- }
- die(json_encode($arr));
- }
-
- //ajax刪除檔案或資料夾
- if ( is_logged() && is_ajax() && isset() && isset ($_POST['del']) ) {
- $path = dirname(__FILE__).$_POST['del'];
- $arr = array('result'=>'error', 'msg' =>'刪除操作失敗! ');
- if ( $_POST['del'] && $path ) {
- $flag = is_dir($path) ? deldir($path) : unlink($path) ;
- if ( $flag ) {
- $arr['msg'] = '刪除操作成功! ';
- $arr['result'] = 'success';
- }
- }
- die(json_encode($arr));
- }
-
- //ajax新檔案或資料夾
- if ( is_logged() && is_ajax() && isset($_POST['create']) ) {
- $flag = false;
- $arr = array('result'= >'error', 'msg'=>'操作失敗! '];
- $target = is_dir($target) ? $target : dirname($target);
- }
- if ( $_POST['create'] && $target ) {
- $base_name = pathinfo($_POST['create'], PATHINFO_BASENAME);
- $exp = explode('.', $base_name);
- $full_path = $target.'/'.$base_name;
- $full_path = $target.'/'.$base_name;
- $ new_path = str_replace(dirname(__FILE__), '', $full_path);
- if ( count($exp) > 1 && isset($lng[array_pop($exp)]) ) {
- file_put_contents($full_path , '');
- $arr['result'] = 'success';
- $arr['msg'] = '新建文件成功! ';
- $arr['type'] = 'file';
- } else {
- mkdir($full_path, 0777, true);
- $arr['result'] = 'success' ;
- $arr['msg'] = '新建目錄成功! ';
- $arr['type'] = 'dir';
- }
- if ( $base_name && $new_path ) {
- $arr['new_name'] = $base_name;
- $arr['new_path'] = $new_path;
- }
- }
- die(json_encode($arr));
- }
-
- //ajax重命名檔案或資料夾
- if ( is_logged() && is_ajax() && isset($_POST['rename']) ) {
- $arr = array('result'=>'error', 'msg'=>'重新命名操作失敗! ( $_POST['rename'] ) {
- $base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME);
- if ( $base_name ) {
- $rename = dirname($target). '/'.$base_name;
- $new_path = str_replace(dirname(__FILE__), '', $rename);
- }
- }
- if ( $rename && $target && rename($target , $rename) ) {
- $arr['new_name'] = $base_name;
- $arr['new_path'] = $new_path;
- $arr['msg'] = '重命名操作成功!';
- $arr['result'] = '成功';
- }
- if ( $target == __FILE__ ) {
- $arr['redirect'] = $new_path;
- }
- die(json_encode($arr));
- }
-
- //取得程式碼檔案內容
- $code = file_get_contents($curr_file);
- $tree = '
-
- ROOT'.list_dir($curr_file, 'html').' li>
- ';
-
- //登陸與設定密碼消耗模版
- $first =
- 【標題】
-
-
-
-
-
-
-
-
-
-
- HTMLSTR;
-
- //判斷是否第一次登入
- if ( false === $pwd &&empty($_POST) ) {
- die(str_replace(
- array('【標題】', '【動作】'),
- array('第一次使用,請先設定密碼!', '設定'),
- $first
- ));
- }
-
- //第一次設定登入密碼
- if ( false === $pwd && !empty ($_POST) ) {
- if ( isset($_POST['pwd']) && strlen($_POST['pwd']) ) {
- $pwd = $_SESSION['pwd'] = md5( $_POST['pwd'].$ace);
- $code = preg_replace('#$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1) ;
- file_put_contents($curr_file, $code);
- } else {
- reload();
- }
- }
-
- //使用者登入驗證
- if ( false !== $pwd && !empty($_POST) ) {
- $tmp = md5($_POST['pwd'].$ace);
- if ( $tmp && $pwd && $tmp == = $pwd ) {
- $_SESSION['pwd'] = $pwd;
- reload();
- }
- }
-
- //處理一下html實體
- $ code = htmlspecialchars($code);
-
- $dir_icon = str_replace(array("rn", "r", "n"), '',
- 'data:image/jpg;base64,iVBORw0KGAAAFANSUhEUgGAAABAB 4kAAAAGXRFWHRTb2Z0d2
- FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wRPeVA/Sy6Dw555q3au1z EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiLOHyqPwemAiLO h3HqH7BctFWLMOmAbcg05mHK5 sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
- MtRED4bwsHLnUzxEeKac3wuP63838E)ExExSacxB]
-
- $file_icon = str_replace(array(" rn", "r", "n"), '',
- 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
- FyZQBBZG9iZCJGbKJGGJ ez7aYbNwreQdBzeopS6EXEW jug7Z
- C6X /iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFKRFPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFRFK/Umhw kDJor6uq2SGktu2FfdpmpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ
- qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5nh/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF
- 0iC8QmPibi4inft 33DkbN1sws0i Pega6v0kd42H9JB/8
- LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII=');
-
- $loading = str_replace(array("rn" n"), '',
- 'data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP TMsZiALlcAKNOAOp4ANVqAP PFv///wAAAAAAAA
- AAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh QQFCgAIACwAAAAAFAAUAAAEUxDJSau9iBDMteb
- TMEjehgTBJYqkiaLWOlZvGs8WDO6UIPCHw8TnAwWDEuKPcxQml0Ynj2cwYACAS7VqwWItWyuiUJB4s2AxmWxG
- g9bl6YQtl0cAACH5BAUKAAgALAEAAQASABIAAAROEMkpx6A4W5upENUmEQT2feFIltMJYivbvhnZ3Z1h4FMQI
- Dodz cL7nDEn5CH8DGZhcLtcMBEoxkqlXKVIgAAibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAA
- ROEMkphaA4W5upMdUmDQP2feFIltMJYivbvhnZ3V1R4BNBIDodz cL7nDEn5CH8DGZAMAtEMBEoxkqlXKVIg4
- HibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpjaE4W5tpKdUmCQL2feFIltMJYivbvhnZ qlXKVIgIBibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAA
- QASABIAAAROEMkpS6E4W5spANUmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodzmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodz tHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpAaA4W5vpOdUmFQX2feFIltM
- JYivbvhnZ3V0Q4JNhIDodz AQASABIAAAROEMkpz6E4W5tpCNUmAQD2feFIltMJYivbvhnZ3R1B4FNRIDodz cL7nDEn5CH8DGZg
- 8HNYMBEoxkqlXKVIgQCiASAK9YLBYvL75KJLBYv f2feFIltMJYivbvhnZ3d0w4BMAIDodz cL7nDEn5CH8DGZAsGtUMBEoxkqlXKVIgwGibbK9YLBYvLtHH5K0J0
- IADs=');
-
- //編輯器模版
- $html =
- ACE程式碼編輯器
{$tree} {$code} -
|