萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php單文件版在線代碼編輯器

php單文件版在線代碼編輯器

 這篇文章主要介紹了php單文件版在線代碼編輯器,個人感覺相當不錯,分享給大家,需要的朋友可以參考下

   

密碼加密方式:
* md5(自設密碼+$ace) //$ace為cdn鏡像地址

使用方法:

* 1.確認 $pwd 變量值為 false, 上傳本文件到PHP空間並訪問
* 2.第一次訪問提示設置密碼,設置密碼並牢記
* 3.使用第一次設置的密碼登錄後,默認編輯的是本php文件,
* 4.本文件是編輯器核心文件,請不要隨意修改
* 5.保存編輯的文件請用 Ctrl + S 按鍵組合,等待執行結果
* 6.保存動作執行後請務必等待保存成功信息返回
* 7.重置操作會修改本程序的文件名,以防他人猜測路徑
* 8.刷新功能僅是刷新本程序文件,不能刷新其他

建議在 chrome 浏覽器中使用本編輯器

 

代碼如下:
<?php
session_start();
$curr_file = __FILE__; //默認編輯當前文件
$curr_file_path = str_replace(dirname(__FILE__), '', __FILE__);
$pwd = "57574d98bc6ebe77b07e59d87065cd9e"; //密碼初始化默認值為 false
$ace = 'ace.js'; //編輯器核心js
$tip['core'] = 'alertify.core.min.css';
$tip['css'] = 'alertify.default.min.css';
$tip['js'] = 'alertify.min.js';
$jquery = '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;
}
}
return $flag;
}
//重新載入到本頁面
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 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 = '<li class="dir" path="'.$file
.'" onclick="load();"><span>'.$v.'</span></li>';
}
array_push($lst['dir'], $v);
} else {
if ( 'html' === $type ) {
$v = '<li class="file" path="'.$file
.'" onclick="load()"><span>'.$v.'</span></li>';
}
array_push($lst['file'], $v);
}
}
}
$lst = array_merge($lst['dir'], $lst['file']);
$lst = array_filter($lst);
$flag = $lst;
if ( 'html' === $type ) {
$flag = '<ul>'. implode('', $lst) .'</ul>';
}
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($_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'=>'操作失敗!');
if ( isset($_POST['target']) ) {
$target = dirname(__FILE__).$_POST['target'];
$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;
$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'=>'
copyright © 萬盛學電腦網 all rights reserved