View source
<?php
namespace Drupal\ueditor\Uploader;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
abstract class Upload {
protected $fileField;
protected $file;
protected $base64;
protected $config;
protected $oriName;
protected $fileName;
protected $fullName;
protected $filePath;
protected $fileSize;
protected $fileType;
protected $stateInfo;
protected $stateMap = array(
"SUCCESS",
"文件大小超出 upload_max_filesize 限制",
"文件大小超出 MAX_FILE_SIZE 限制",
"文件未被完整上传",
"没有文件被上传",
"上传文件为空",
"ERROR_TMP_FILE" => "临时文件错误",
"ERROR_TMP_FILE_NOT_FOUND" => "找不到临时文件",
"ERROR_SIZE_EXCEED" => "文件大小超出网站限制",
"ERROR_TYPE_NOT_ALLOWED" => "文件类型不允许",
"ERROR_CREATE_DIR" => "目录创建失败",
"ERROR_DIR_NOT_WRITEABLE" => "目录没有写权限",
"ERROR_FILE_MOVE" => "文件保存时出错",
"ERROR_FILE_NOT_FOUND" => "找不到上传文件",
"ERROR_WRITE_CONTENT" => "写入文件内容错误",
"ERROR_UNKNOWN" => "未知错误",
"ERROR_WATERMARK_ADD" => "添加水印出错",
"ERROR_WATERMARK_TEXT_RGB" => "水印文字颜色格式不正确",
"ERROR_WATERMARK_NOT_FOUND" => "需要添加水印的图片不存在",
"ERROR_WATERMARK_SIZE" => "水印太大或图片太小",
"ERROR_DEAD_LINK" => "链接不可用",
"ERROR_HTTP_LINK" => "链接不是http链接",
"ERROR_HTTP_CONTENTTYPE" => "链接contentType不正确",
);
abstract function doUpload();
public function __construct($config, $request) {
$this->config = $config;
$this->request = $request;
$this->fileField = $this->config['fieldName'];
$this->global_settings = \Drupal::config('ueditor.settings')
->get('ueditor_global_settings');
}
public function upload() {
$this
->doUpload();
return $this
->getFileInfo();
}
protected function savefileToDrupal($imagedata, $savePath, $managed = FALSE, $replace = FileSystemInterface::EXISTS_RENAME) {
if ($managed) {
$local = file_save_data($imagedata, 'public://' . $savePath, $replace);
}
else {
try {
$local = \Drupal::service('file_system')
->saveData($imagedata, 'public://' . $savePath, $replace);
} catch (\Drupal\Core\File\Exception\FileException $e) {
}
}
return $local;
}
protected function addWatermark($savePath) {
$watermark_type = $this->global_settings['ueditor_watermark_type'];
$watermark_place = $this->global_settings['ueditor_watermark_place'];
if ($watermark_type == 'image') {
$watermark_path = $this->global_settings['ueditor_watermark_path'];
$watermark_alpha = $this->global_settings['ueditor_watermark_alpha'];
$success = $this
->imageWaterMark($savePath, $watermark_place, $watermark_path, $watermark_alpha);
}
else {
global $base_url;
$watermark_textcontent = $this->global_settings['ueditor_watermark_textcontent'];
$watermark_textfontsize = $this->global_settings['ueditor_watermark_textfontsize'];
$watermark_textcolor = $this->global_settings['ueditor_watermark_textcolor'];
$success = $this
->imageWaterMark($savePath, $watermark_place, '', '', $watermark_textcontent, $watermark_textfontsize, $watermark_textcolor);
}
return $success;
}
protected function getStateInfo($errCode) {
return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode];
}
protected function getFileExt() {
return strtolower(strrchr($this->oriName, '.'));
}
protected function getFullName() {
$t = time();
$d = explode('-', date("Y-y-m-d-H-i-s"));
$format = $this->config["pathFormat"];
$format = str_replace("{yyyy}", $d[0], $format);
$format = str_replace("{yy}", $d[1], $format);
$format = str_replace("{mm}", $d[2], $format);
$format = str_replace("{dd}", $d[3], $format);
$format = str_replace("{hh}", $d[4], $format);
$format = str_replace("{ii}", $d[5], $format);
$format = str_replace("{ss}", $d[6], $format);
$format = str_replace("{time}", $t, $format);
$oriName = substr($this->oriName, 0, strrpos($this->oriName, '.'));
$oriName = preg_replace("/[\\|\\?\"\\<\\>\\/\\*\\\\]+/", '', $oriName);
$format = str_replace("{filename}", $oriName, $format);
$randNum = rand(1, 10000000000) . rand(1, 10000000000);
if (preg_match("/\\{rand\\:([\\d]*)\\}/i", $format, $matches)) {
$format = preg_replace("/\\{rand\\:[\\d]*\\}/i", substr($randNum, 0, $matches[1]), $format);
}
if (strpos($format, '{transliteration_filename}')) {
if (module_exists('transliteration') && function_exists('transliteration_clean_filename')) {
$format = str_replace("{transliteration_filename}", transliteration_clean_filename($oriName), $format);
}
}
$ext = $this
->getFileExt();
return $format . $ext;
}
protected function getFileName() {
return substr($this->filePath, strrpos($this->filePath, '/') + 1);
}
protected function getFilePath() {
$site_path = \Drupal::service('site.path');
$fullname = ueditor_get_savepath($this->fullName);
$uploadPath = Settings::get('file_public_path', $site_path . '/files');
$rootPath = strtr(DRUPAL_ROOT, '\\', '/');
if (substr($fullname, 0, 1) != '/') {
$fullname = '/' . $fullname;
}
return $rootPath . '/' . $uploadPath . $fullname;
}
protected function checkType() {
return in_array($this
->getFileExt(), $this->config["allowFiles"]);
}
protected function checkSize($type) {
return $this->fileSize <= $this->global_settings['ueditor_' . $type . '_maxsize'] * 1000;
}
public function getFileInfo() {
return array(
"state" => $this->stateInfo,
"url" => $this->fullName,
"title" => $this->fileName,
"original" => $this->oriName,
"type" => $this->fileType,
"size" => $this->fileSize,
);
}
protected function imageWaterMark($groundImage, $waterPos = 0, $waterImage = '', $wateralpha = '', $waterText = '', $fontSize = '', $textColor = '') {
$isWaterImage = FALSE;
if (!empty($waterImage) && file_exists($waterImage)) {
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];
$water_h = $water_info[1];
switch ($water_info[2]) {
case 1:
$water_im = imagecreatefromgif($waterImage);
break;
case 2:
$water_im = imagecreatefromjpeg($waterImage);
break;
case 3:
$water_im = imagecreatefrompng($waterImage);
break;
default:
return "ERROR_TYPE_NOT_ALLOWED";
}
}
if (!empty($groundImage) && file_exists($groundImage)) {
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0];
$ground_h = $ground_info[1];
switch ($ground_info[2]) {
case 1:
$ground_im = imagecreatefromgif($groundImage);
break;
case 2:
$ground_im = imagecreatefromjpeg($groundImage);
break;
case 3:
$ground_im = imagecreatefrompng($groundImage);
break;
default:
return "ERROR_TYPE_NOT_ALLOWED";
}
}
else {
return "ERROR_WATERMARK_NOT_FOUND";
}
if ($isWaterImage) {
$w = $water_w;
$h = $water_h;
$label = "图片的";
}
else {
$fontface = drupal_get_path('module', 'ueditor') . '/fonts/fzcyjt.ttf';
$temp = imagettfbbox(ceil($fontSize * 1.2), 0, $fontface, $waterText);
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
$label = "文字区域";
}
if ($ground_w < $w || $ground_h < $h) {
return "ERROR_WATERMARK_SIZE";
}
switch ($waterPos) {
case 0:
$posX = rand(0, $ground_w - $w);
$posY = rand(0, $ground_h - $h);
break;
case 1:
$posX = 10;
$posY = $h + 10;
break;
case 2:
$posX = ($ground_w - $w) / 2;
$posY = $h + 10;
break;
case 3:
$posX = $ground_w - $w;
$posY = $h + 10;
break;
case 4:
$posX = 10;
$posY = ($ground_h - $h) / 2;
break;
case 5:
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6:
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7:
$posX = 10;
$posY = $ground_h - $h;
break;
case 8:
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9:
$posX = $ground_w - $w - 10;
$posY = $ground_h - $h - 10;
break;
default:
$posX = rand(0, $ground_w - $w);
$posY = rand(0, $ground_h - $h);
break;
}
imagealphablending($ground_im, true);
if ($isWaterImage) {
imagecopymerge($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h, $wateralpha);
}
else {
if (!empty($textColor) && strlen($textColor) == 7) {
$R = hexdec(substr($textColor, 1, 2));
$G = hexdec(substr($textColor, 3, 2));
$B = hexdec(substr($textColor, 5));
}
else {
return "ERROR_WATERMARK_TEXT_RGB";
}
imagettftext($ground_im, $fontSize, 0, $posX, $posY, imagecolorallocate($ground_im, $R, $G, $B), $fontface, $waterText);
}
@unlink($groundImage);
switch ($ground_info[2]) {
case 1:
imagegif($ground_im, $groundImage);
break;
case 2:
imagejpeg($ground_im, $groundImage);
break;
case 3:
imagepng($ground_im, $groundImage);
break;
default:
return "ERROR_WATERMARK_ADD";
}
if (isset($water_info)) {
unset($water_info);
}
if (isset($water_im)) {
imagedestroy($water_im);
}
unset($ground_info);
imagedestroy($ground_im);
return TRUE;
}
}