<?php
/**
* Ueditor
* @author yupoxiong<i@yufuping.com>
*/
namespace tools;
class Ueditor
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function server($action)
{
date_default_timezone_set("Asia/Chongqing");
header("Content-Type: text/html; charset=utf-8");
$ue_config = $this->config;
switch ($action) {
case 'config':
$result = $ue_config;
break;
case 'uploadimage':
$fieldName = $ue_config['imageFieldName'];
$result = $this->upFile($fieldName);
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $ue_config['scrawlPathFormat'],
"maxSize" => $ue_config['scrawlMaxSize'],
"allowFiles" => $ue_config['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $ue_config['scrawlFieldName'];
$base64 = "base64";
$result = $this->upBase64($config, $fieldName);
break;
case 'uploadvideo':
$fieldName = $ue_config['videoFieldName'];
$result = $this->upFile($fieldName);
break;
case 'uploadfile':
$fieldName = $ue_config['fileFieldName'];
$result = $this->upFile($fieldName);
break;
case 'listimage':
$allowFiles = $ue_config['imageManagerAllowFiles'];
$listSize = $ue_config['imageManagerListSize'];
$path = $ue_config['imageManagerListPath'];
$get = $_GET;
$result = $this->fileList($allowFiles, $listSize, $get);
break;
case 'listfile':
$allowFiles = $ue_config['fileManagerAllowFiles'];
$listSize = $ue_config['fileManagerListSize'];
$path = $ue_config['fileManagerListPath'];
$get = $_GET;
$result = $this->fileList($allowFiles, $listSize, $get);
break;
case 'catchimage':
$config = array(
"pathFormat" => $ue_config['catcherPathFormat'],
"maxSize" => $ue_config['catcherMaxSize'],
"allowFiles" => $ue_config['catcherAllowFiles'],
"oriName" => "remote.png"
);
$fieldName = $ue_config['catcherFieldName'];
$list = array();
isset($_POST[$fieldName]) ? $source = $_POST[$fieldName] : $source = $_GET[$fieldName];
foreach ($source as $imgUrl) {
$info = json_decode($this->saveRemote($config, $imgUrl), true);
array_push($list, array(
"state" => $info["state"],
"url" => $info["url"],
"size" => $info["size"],
"title" => htmlspecialchars($info["title"]),
"original" => htmlspecialchars($info["original"]),
"source" => htmlspecialchars($imgUrl)
));
}
$result = array(
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
);
break;
default:
$result = ['state' => '请求地址出错'];
break;
}
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
return json(['state' => 'callback参数不合法']);
}
} else {
return json($result);
}
}
private function upFile($fieldName)
{
$file = request()->file($fieldName);
$info = $file->move(ROOT_PATH . 'public/uploads/ueditor');
if ($info) $fname = '/uploads/ueditor/' . str_replace('\\', '/', $info->getSaveName());
$imgArr = explode(',', 'jpg,gif,png,jpeg,bmp,ttf,tif');
$imgExt = strtolower($info->getExtension());
$isImg = in_array($imgExt, $imgArr);
if ($isImg) $thumbnail = 1;
$water = 1;
}
$data = array(
'state' => 'SUCCESS',
'url' => $fname,
'title' => $info->getFilename(),
'original' => $info->getFilename(),
'type' => '.' . $info->getExtension(),
'size' => $info->getSize(),
);
} else {
$data = array(
'state' => $info->getError(),
);
}
return $data;
}
private function fileList($allowFiles, $listSize, $get)
{
$dirname = '/uploads/ueditor/';
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
$size = isset($get['size']) ? htmlspecialchars($get['size']) : $listSize;
$start = isset($get['start']) ? htmlspecialchars($get['start']) : 0;
$end = $start + $size;
$path = $dirname;
$files = $this->getFiles($path, $allowFiles);
if (!count($files)) {
return json_encode(array(
"state" => "no match file",
"list" => array(),
"start" => $start,
"total" => count($files)
));
}
$len = count($files);
for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
$result = (array(
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($files)
));
return $result;
}
private function getFiles($path, $allowFiles, &$files = array())
{
if (!is_dir($path)) return null;
if (substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $path . $file;
if (is_dir($path2)) {
$this->getFiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
$files[] = array(
'url' => substr($path2, 1),
'mtime' => filemtime($path2)
);
}
}
}
}
return $files;
}
private function saveRemote($config, $fieldName)
{
$imgUrl = htmlspecialchars($fieldName);
$imgUrl = str_replace("&", "&", $imgUrl);
if (strpos($imgUrl, "http") !== 0) {
$data = array(
'state' => '链接不是http链接',
);
return json_encode($data);
}
$heads = get_headers($imgUrl);
if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
$data = array(
'state' => '链接不可用',
);
return ($data);
}
$fileType = strtolower(strrchr($imgUrl, '.'));
if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
$data = array(
'state' => '链接contentType不正确',
);
return ($data);
}
ob_start();
$context = stream_context_create(
array('http' => array(
'follow_location' => false ))
);
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
$dirname = ROOT_PATH . 'public/uploads/ueditor/remote/';
$file['oriName'] = $m ? $m[1] : "";
$file['filesize'] = strlen($img);
$file['ext'] = strtolower(strrchr($config['oriName'], '.'));
$file['name'] = uniqid() . $file['ext'];
$file['fullName'] = $dirname . $file['name'];
$fullName = $file['fullName'];
if ($file['filesize'] >= ($config["maxSize"])) {
$data = array(
'state' => '文件大小超出网站限制',
);
return ($data);
}
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$data = array(
'state' => '目录创建失败',
);
return json_encode($data);
} else if (!is_writeable($dirname)) {
$data = array(
'state' => '目录没有写权限',
);
return ($data);
}
if (!(file_put_contents($fullName, $img) && file_exists($fullName))) { $data = array(
'state' => '写入文件内容错误',
);
return json_encode($data);
} else { $data = array(
'state' => 'SUCCESS',
'url' => substr($file['fullName'], 1),
'title' => $file['name'],
'original' => $file['oriName'],
'type' => $file['ext'],
'size' => $file['filesize'],
);
}
return ($data);
}
private function upBase64($config, $fieldName)
{
$base64Data = $_POST[$fieldName];
$img = base64_decode($base64Data);
$dirname = ROOT_PATH . 'uploads/ueditor/scrawl/';
$file['filesize'] = strlen($img);
$file['oriName'] = $config['oriName'];
$file['ext'] = strtolower(strrchr($config['oriName'], '.'));
$file['name'] = uniqid() . $file['ext'];
$file['fullName'] = $dirname . $file['name'];
$fullName = $file['fullName'];
if ($file['filesize'] >= ($config["maxSize"])) {
$data = array(
'state' => '文件大小超出网站限制',
);
return ($data);
}
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$data = array(
'state' => '目录创建失败',
);
return json_encode($data);
} else if (!is_writeable($dirname)) {
$data = array(
'state' => '目录没有写权限',
);
return ($data);
}
if (!(file_put_contents($fullName, $img) && file_exists($fullName))) { $data = array(
'state' => '写入文件内容错误',
);
} else { $data = array(
'state' => 'SUCCESS',
'url' => substr($file['fullName'], 1),
'title' => $file['name'],
'original' => $file['oriName'],
'type' => $file['ext'],
'size' => $file['filesize'],
);
}
return ($data);
}
}