前不久客戶反饋說,在秀米網(wǎng)上編輯好文章,發(fā)布到我們后臺,前臺圖片無法顯示,最先想到的是圖片域名限制。發(fā)郵件給秀米,秀米給出的建議是類似微信后臺做圖片本地化。 開發(fā)組內(nèi)部討論后,看到了這篇文章,UEditor編輯器如何關(guān)閉抓取遠(yuǎn)程圖片本地化功能,so easy?按文章說明設(shè)置,測試,失! 百度看了下,大都是這樣解答,說明應(yīng)該是有人實(shí)現(xiàn)了,秀米編輯器核心代碼也正是ueditor核心代碼,那就只能自己搗鼓了。 具體搗鼓過程如下: 注意:搗鼓前先備份下,以備修改錯誤導(dǎo)致其他問題
打開ueditor.config.js,在配置項(xiàng)中加入
,catchRemoteImageEnable:true 打開ueditor.config.js,搜索catchremoteimage,在加入console.warn(url),看看上傳地址。前臺測試后看到/Skin/public/ueditor/php/controller.php?action=catchimage 打開php/controller.php 可以看到
case 'catchimage':
$result = include("action_crawler.php");
break;接著打開統(tǒng)計(jì)目錄下action_crawler.php include("Uploader.class.php");
$item = new Uploader($imgUrl, $config, "remote");看到這兩段代碼。繼續(xù)打開統(tǒng)計(jì)目錄下Uploader.class.php //構(gòu)造函數(shù)如下
public function __construct($fileField, $config, $type = "upload")
{
$this->fileField = $fileField;
$this->config = $config;
$this->type = $type;
if ($type == "remote") {
$this->saveRemote();
} else if($type == "base64") {
$this->upBase64();
} else {
$this->upFile();
}
$this->stateMap['ERROR_TYPE_NOT_ALLOWED'] = iconv('unicode', 'utf-8', $this->stateMap['ERROR_TYPE_NOT_ALLOWED']);
}搜索saveRemote,主要修復(fù)fileType與oriName兩塊。 /**
* 拉取遠(yuǎn)程圖片
* @return mixed
*/
private function saveRemote()
{
$imgUrl = htmlspecialchars($this->fileField);
$imgUrl = str_replace("&", "&", $imgUrl);
//http開頭驗(yàn)證
if (strpos($imgUrl, "http") !== 0) {
$this->stateInfo = $this->getStateInfo("ERROR_HTTP_LINK");
return;
}
preg_match('/(^https*:\/\/[^:\/]+)/', $imgUrl, $matches);
$host_with_protocol = count($matches) > 1 ? $matches[1] : '';
// 判斷是否是合法 url
if (!filter_var($host_with_protocol, FILTER_VALIDATE_URL)) {
$this->stateInfo = $this->getStateInfo("INVALID_URL");
return;
}
preg_match('/^https*:\/\/(.+)/', $host_with_protocol, $matches);
$host_without_protocol = count($matches) > 1 ? $matches[1] : '';
// 此時(shí)提取出來的可能是 ip 也有可能是域名,先獲取 ip
$ip = gethostbyname($host_without_protocol);
// 判斷是否是私有 ip
if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$this->stateInfo = $this->getStateInfo("INVALID_IP");
return;
}
//獲取請求頭并檢測死鏈
$heads = get_headers($imgUrl, 1);
if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
$this->stateInfo = $this->getStateInfo("ERROR_DEAD_LINK");
return;
}
//格式驗(yàn)證(擴(kuò)展名驗(yàn)證和Content-Type驗(yàn)證)
$fileType = strtolower(strrchr($imgUrl, '.'));
//13sai 20170712 秀米網(wǎng)鏈接接如下http://img.xiumi.us/xmi/ua/h4qG/i/b8f2af6986e8dba51615a9d85cc82f3b-sz_1952250.JPG?x-oss-process=style/xm ,我們完善下
$fileType = (strpos($fileType, '?') > 0)? strtolower(substr($fileType,0,strpos($fileType,'?'))) : strtolower($fileType);
//echo $fileType;die();
if (!in_array($fileType, $this->config['allowFiles']) || !isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) { $this->stateInfo = $this->getStateInfo("ERROR_HTTP_CONTENTTYPE");
return;
}
//打開輸出緩沖區(qū)并獲取遠(yuǎn)程圖片
ob_start(); $context = stream_context_create(
array('http' => array( 'follow_location' => false // don't follow redirects
))
);
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
//13sai 20170712 此處正則有問題,修改如下
//preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
preg_match("/\/[A-za-z0-9-]+.".$fileType."/", strtolower($imgUrl), $m);
//var_dump($m);die();
$this->oriName = $m ? ltrim($m[0],'/'):"";
//$this->oriName = $m ? $m[1]:"";
//echo $this->oriName;
//die();
$this->fileSize = strlen($img);
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//檢查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//創(chuàng)建目錄失敗
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}
//移動文件
if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移動失敗
$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
} else { //移動成功
$this->stateInfo = $this->stateMap[0];
}
}打開php/config.json,修改大小、格式、存儲路徑等參數(shù)
/* 抓取遠(yuǎn)程圖片配置 */ "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherActionName": "catchimage", /* 執(zhí)行抓取遠(yuǎn)程圖片的action名稱 */ "catcherFieldName": "source", /* 提交的圖片列表表單名稱 */ "catcherPathFormat": "/Upload/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "catcherUrlPrefix": "", /* 圖片訪問路徑前綴 */ "catcherMaxSize": 20480000, /* 上傳大小限制,單位B */ "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取圖片格式顯示 */5.保存修改上傳,測試成功!
|