更换编辑器
This commit is contained in:
1
public/static/plugins/NKeditor/php/default/cache.tmp
Normal file
1
public/static/plugins/NKeditor/php/default/cache.tmp
Normal file
@@ -0,0 +1 @@
|
||||
1508678800
|
||||
116
public/static/plugins/NKeditor/php/default/db/SimpleDB.php
Normal file
116
public/static/plugins/NKeditor/php/default/db/SimpleDB.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/****************************************************
|
||||
* NKeditor PHP
|
||||
* 本PHP程序是演示程序,建议不要直接在实际项目中使用。
|
||||
* 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
|
||||
* **************************************************
|
||||
* 简易数据库, 单表 100w 条数据,查询一页数据在 0.015 秒左右
|
||||
* 缺陷,无法排序,如果要排序的话,那么不适合使用 SimpleDB, 请使用 mysql 或者 mongdb
|
||||
* User: yangjian
|
||||
* Date: 17-10-14
|
||||
* Time: 下午5:15
|
||||
*/
|
||||
|
||||
class SimpleDB {
|
||||
|
||||
/**
|
||||
* 文件资源
|
||||
* @var null|resource
|
||||
*/
|
||||
private $handler = null;
|
||||
|
||||
/**
|
||||
* 初始化,打开文件
|
||||
* SimpleDB constructor.
|
||||
* @param $dbname
|
||||
*/
|
||||
public function __construct($dbname)
|
||||
{
|
||||
$dataDir = __DIR__."/data/";
|
||||
if (!file_exists($dataDir)) {
|
||||
mkdir($dataDir);
|
||||
}
|
||||
$this->handler = fopen($dataDir.$dbname.'.db', 'a+');
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入一行数据
|
||||
* @return bool
|
||||
*/
|
||||
public function putLine($data) {
|
||||
|
||||
if ($this->handler != null) {
|
||||
fwrite($this->handler, $this->seralize($data));
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取数据列表
|
||||
* @param $key
|
||||
* @return array|null
|
||||
*/
|
||||
public function getDataList($page, $pagesize) {
|
||||
|
||||
if($page <= 0) {
|
||||
$page = 1;
|
||||
}
|
||||
$offset = ($page - 1) * $pagesize;
|
||||
//循环读取数据
|
||||
$datas = [];
|
||||
$counter = 0;
|
||||
while (!feof($this->handler)) {
|
||||
if ($counter < $offset) {
|
||||
fgets($this->handler); //移动指针到下一行
|
||||
$counter++;
|
||||
continue;
|
||||
}
|
||||
if (count($datas) == $pagesize) {
|
||||
break;
|
||||
}
|
||||
$line = fgets($this->handler);
|
||||
if (!empty($line)) {
|
||||
$datas[] = $this->unseralize($line);
|
||||
}
|
||||
}
|
||||
|
||||
return $datas;
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化数据
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function seralize($data) {
|
||||
|
||||
$break = "\n"; //换行符
|
||||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
||||
$break = "\r\n";
|
||||
}
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE).$break;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
private function unseralize($data) {
|
||||
|
||||
return json_decode($data, true);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭文件
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->handler != null) {
|
||||
fclose($this->handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
public/static/plugins/NKeditor/php/default/db/test.php
Normal file
18
public/static/plugins/NKeditor/php/default/db/test.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: yangjian
|
||||
* Date: 17-10-14
|
||||
* Time: 下午5:28
|
||||
*/
|
||||
require_once "SimpleDB.php";
|
||||
|
||||
$db = new SimpleDB("test");
|
||||
$t = 100000000;
|
||||
for($i = 0; $i < 1000000; $i++) {
|
||||
$db->putLine($t+$i);
|
||||
}
|
||||
printf("数据插入完毕!\n");
|
||||
$items = $db->getDataList(2, 10);
|
||||
print_r($items);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/****************************************************
|
||||
* NKeditor PHP
|
||||
* 本PHP程序是演示程序,建议不要直接在实际项目中使用。
|
||||
* 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
|
||||
* **************************************************
|
||||
* 获取图片服务器上已上传的图片列表
|
||||
* @author yangjian<yangjian102621@gmail.com>
|
||||
*/
|
||||
error_reporting(0);
|
||||
require_once '../JsonResult.php';
|
||||
require_once '../functions.php';
|
||||
require_once "db/SimpleDB.php";
|
||||
|
||||
usleep(500000);
|
||||
$page = intval($_GET["page"]);
|
||||
$fileType = trim($_GET['fileType']);
|
||||
$pagesize = 15;
|
||||
//读取文件数据
|
||||
$db = new SimpleDB($fileType);
|
||||
$files = $db->getDataList($page, $pagesize);
|
||||
$result = new JsonResult();
|
||||
if (!empty($files)) {
|
||||
$result->setCode(JsonResult::CODE_SUCCESS);
|
||||
$result->setData($files);
|
||||
} else {
|
||||
$result->setCode(JsonResult::CODE_FAIL);
|
||||
}
|
||||
$result->output();
|
||||
77
public/static/plugins/NKeditor/php/default/import.php
Normal file
77
public/static/plugins/NKeditor/php/default/import.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* 从老版本的 kindeditor 升级到 NKeditor,你可能需要把之前的文件初始化插入数据库。
|
||||
* 考虑到你可能更改了之前的老版本的文件上传代码,比如存储路径的风格,这里就只个给出一个参考思路:
|
||||
* 1. 遍历你的附件存储根目录, 并根据不同的文件类型 image, flash, file, media 分别创建4个数据库;
|
||||
* 2. 分别递归遍历这4个文件夹,把数据文件的 url 插入到各自的 SimpleDB 数据库.
|
||||
* ----------------------
|
||||
* 重建文件索引, 请使用命令行运行 php import.php
|
||||
* @author yangjian
|
||||
*/
|
||||
error_reporting(0);
|
||||
require_once "db/SimpleDB.php";
|
||||
require_once "../functions.php";
|
||||
|
||||
// 文件上传的根目录,请根据自己的实际情况修改
|
||||
$root = $basePath = dirname(dirname(__DIR__)) . "/uploads/";
|
||||
// 图片上传的根url,请根据实际项目修改
|
||||
$baseUrl = "/editor/nkeditor/uploads/";
|
||||
|
||||
//如果数据库已经存在,则先删除
|
||||
$datadir = __DIR__.'/db/data';
|
||||
file_exists($datadir) && deldir($datadir);
|
||||
|
||||
chdir($root);
|
||||
$dirs = glob("*");
|
||||
foreach ($dirs as $dir) {
|
||||
$db = new SimpleDB($dir);
|
||||
walkDir($root.$dir, $db, $dir);
|
||||
}
|
||||
|
||||
tprint("数据导入完毕!");
|
||||
|
||||
/**
|
||||
* 遍历目录,建立路径索引
|
||||
* @param $dir
|
||||
* @param SimpleDB $db
|
||||
* @param $fileType
|
||||
*/
|
||||
function walkDir($dir, $db, $fileType) {
|
||||
$handler = opendir($dir);
|
||||
global $root;
|
||||
global $baseUrl;
|
||||
while (($filename = readdir($handler)) !== false) {
|
||||
if ($filename != '.' && $filename != '..') {
|
||||
$filePath = $dir.'/'.$filename;
|
||||
if (is_dir($filePath)) {
|
||||
walkDir($filePath, $db, $fileType);
|
||||
continue;
|
||||
}
|
||||
$filesize = filesize($filePath);
|
||||
//如果是图片则获取尺寸
|
||||
if ($fileType == "image") {
|
||||
$size = getimagesize($filePath);
|
||||
}
|
||||
$fileUrl = $baseUrl.str_replace('\\', '/', str_replace($root, '', $filePath));
|
||||
$data = [
|
||||
"thumbURL" => $fileUrl,
|
||||
"oriURL" => $fileUrl,
|
||||
"filesize" => $filesize,
|
||||
"width" => intval($size[0]),
|
||||
"height" => intval($size[1])
|
||||
];
|
||||
$db->putLine($data);
|
||||
tprint("添加路径: {$fileUrl}");
|
||||
}
|
||||
}
|
||||
|
||||
closedir($handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 终端打印函数
|
||||
* @param $message
|
||||
*/
|
||||
function tprint($message) {
|
||||
printf("\033[32m\033[1m{$message}\033[0m\n");
|
||||
}
|
||||
152
public/static/plugins/NKeditor/php/default/upload_json.php
Normal file
152
public/static/plugins/NKeditor/php/default/upload_json.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/****************************************************
|
||||
* NKeditor PHP
|
||||
* 本PHP程序是演示程序,建议不要直接在实际项目中使用。
|
||||
* 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
|
||||
* **************************************************
|
||||
* @author yangjian<yangjian102621@gmail.com>
|
||||
* 文件上传程序
|
||||
*/
|
||||
error_reporting(0);
|
||||
require_once '../JsonResult.php';
|
||||
require_once '../functions.php';
|
||||
require_once "db/SimpleDB.php";
|
||||
|
||||
$fileType = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
|
||||
//文件保存目录路径
|
||||
$basePath = BASE_PATH."{$fileType}/".UPLOAD_PREFIX;
|
||||
//文件保存目录URL
|
||||
$baseUrl = BASE_URL . "{$fileType}/".UPLOAD_PREFIX;
|
||||
//定义允许上传的文件扩展名
|
||||
$allowExtesions = array(
|
||||
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
|
||||
'flash' => array('swf', 'flv'),
|
||||
'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
|
||||
'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
|
||||
);
|
||||
//最大文件大小 2MB
|
||||
$maxSize = 2*1024*1024;
|
||||
if (!file_exists($basePath)) {
|
||||
mkdirs($basePath);
|
||||
}
|
||||
//PHP上传失败
|
||||
if (!empty($_FILES['imgFile']['error'])) {
|
||||
switch($_FILES['imgFile']['error']){
|
||||
case '1':
|
||||
$error = '超过php.ini允许的大小。';
|
||||
break;
|
||||
case '2':
|
||||
$error = '超过表单允许的大小。';
|
||||
break;
|
||||
case '3':
|
||||
$error = '图片只有部分被上传。';
|
||||
break;
|
||||
case '4':
|
||||
$error = '请选择图片。';
|
||||
break;
|
||||
case '6':
|
||||
$error = '找不到临时目录。';
|
||||
break;
|
||||
case '7':
|
||||
$error = '写文件到硬盘出错。';
|
||||
break;
|
||||
case '8':
|
||||
$error = 'File upload stopped by extension。';
|
||||
break;
|
||||
case '999':
|
||||
default:
|
||||
$error = '未知错误。';
|
||||
}
|
||||
alert($error);
|
||||
}
|
||||
|
||||
//base64 文件上传
|
||||
$base64 = trim($_POST['base64']);
|
||||
if ($base64) {
|
||||
$imgData = $_POST['img_base64_data'];
|
||||
|
||||
$json = new JsonResult();
|
||||
if ($imgData && preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgData, $match)){
|
||||
$type = $match[2];
|
||||
$filename = date("YmdHis") . '_' . rand(10000, 99999) . '.png';
|
||||
if (file_put_contents($basePath.$filename, base64_decode(str_replace($match[1], '', $imgData)))){
|
||||
$json->setCode(JsonResult::CODE_SUCCESS);
|
||||
$json->setData(array('url' => $baseUrl.$filename));
|
||||
$json->output();
|
||||
}
|
||||
}
|
||||
$json->setCode(JsonResult::CODE_FAIL);
|
||||
$json->setMessage("涂鸦保存失败.");
|
||||
$json->output();
|
||||
}
|
||||
|
||||
// input 文件上传
|
||||
if (empty($_FILES) == false) {
|
||||
//原文件名
|
||||
$filename = $_FILES['imgFile']['name'];
|
||||
//服务器上临时文件名
|
||||
$tmpName = $_FILES['imgFile']['tmp_name'];
|
||||
//文件大小
|
||||
$filesize = $_FILES['imgFile']['size'];
|
||||
//检查文件名
|
||||
if (!$filename) {
|
||||
alert("请选择文件。");
|
||||
}
|
||||
//检查目录
|
||||
if (@is_dir($basePath) === false) {
|
||||
alert("上传目录不存在。");
|
||||
}
|
||||
//检查目录写权限
|
||||
if (@is_writable($basePath) === false) {
|
||||
alert("上传目录没有写权限。");
|
||||
}
|
||||
//检查是否已上传
|
||||
if (@is_uploaded_file($tmpName) === false) {
|
||||
alert("上传失败。");
|
||||
}
|
||||
//检查文件大小
|
||||
if ($filesize > $maxSize) {
|
||||
alert("上传文件大小超过限制。");
|
||||
}
|
||||
|
||||
//获得文件扩展名
|
||||
$extesion = getFileExt($filename);
|
||||
//检查扩展名
|
||||
if (in_array($extesion, $allowExtesions[$fileType]) === false) {
|
||||
alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $allowExtesions[$fileType]) . "格式。");
|
||||
}
|
||||
//新文件名
|
||||
$newFileName = genNewFilename($filename);
|
||||
//移动文件
|
||||
$filePath = $basePath . $newFileName;
|
||||
if (move_uploaded_file($tmpName, $filePath) === false) {
|
||||
alert("上传文件失败。");
|
||||
}
|
||||
@chmod($filePath, 0644);
|
||||
$fileUrl = $baseUrl . $newFileName;
|
||||
|
||||
$json = new JsonResult(JsonResult::CODE_SUCCESS, "上传成功");
|
||||
$json->setData(array('url' => $fileUrl));
|
||||
|
||||
//保存文件地址到数据库
|
||||
$db = new SimpleDB($fileType);
|
||||
//过滤掉非图片文件
|
||||
if ($fileType == "image") {
|
||||
$size = getimagesize($filePath);
|
||||
}
|
||||
$data = [
|
||||
"thumbURL" => $fileUrl,
|
||||
"oriURL" => $fileUrl,
|
||||
"filesize" => $filesize,
|
||||
"width" => intval($size[0]),
|
||||
"height" => intval($size[1])
|
||||
];
|
||||
$db->putLine($data);
|
||||
|
||||
$json->output();
|
||||
}
|
||||
|
||||
function alert($msg) {
|
||||
$json = new JsonResult(JsonResult::CODE_FAIL, $msg);
|
||||
$json->output();
|
||||
}
|
||||
Reference in New Issue
Block a user