ѡ̳

 找回密码
 ע
搜索
查看: 23|回复: 0
打印 上一主题 下一主题

修复bug,ueditor远程图片本地化时带参数的图片出错的问题。--完成

[复制链接]

789

主题

1158

帖子

4197

积分

Ա

Rank: 9Rank: 9Rank: 9

积分
4197
跳转到指定楼层
¥
发表于 2017-11-26 22:38:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. $imgUrl =  htmlspecialchars($this->fileField);

  2.       $trueimgUrl =  $imgUrl =  str_replace("& a m p", "&", $imgUrl); //处理图片参数问题

  3.         //http开头验证
  4.         if (strpos($imgUrl, "http") !== 0) {
  5.             $this->stateInfo = $this->getStateInfo("ERROR_HTTP_LINK");
  6.             return;
  7.         }
  8.         //获取请求头并检测死链
  9.         $heads = get_headers($imgUrl, 1);
  10.         if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
  11.             $this->stateInfo = $this->getStateInfo("ERROR_DEAD_LINK");
  12.             return;
  13.         }
  14.         //格式验证(扩展名验证和Content-Type验证)
  15.         $fileType = strtolower(strrchr($imgUrl, '.'));
  16.         /*需要修复下6b7.png?imageView&thumbnail=550x0 不能抓取的问题*/
  17.         if(strpos($fileType,'?')){
  18.             $fileType = substr($fileType,0,strpos($fileType,'?'));
  19.             $trueimgUrl =  substr($imgUrl,0,strpos($imgUrl,'?'));
  20.          }

  21.         if (!in_array($fileType, $this->config['allowFiles']) || !isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) {
  22.             $this->stateInfo = $this->getStateInfo("ERROR_HTTP_CONTENTTYPE");
  23.             return;
  24.         }

  25.         //打开输出缓冲区并获取远程图片
  26.         ob_start();
  27.         $context = stream_context_create(
  28.             array('http' => array(
  29.                 'follow_location' => false // don't follow redirects
  30.             ))
  31.         );
  32.         readfile($trueimgUrl, false, $context);
  33.         $img = ob_get_contents();
  34.         ob_end_clean();
  35.         preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $trueimgUrl, $m);

  36.         $this->oriName = $m ? $m[1]:"";
  37.         $this->fileSize = strlen($img);
  38.         $this->fileType = $this->getFileExt();
  39.         $this->fullName = $this->getFullName();
  40.         $this->filePath = $this->getFilePath();
  41.         $this->fileName = $this->getFileName();
  42.         $dirname = dirname($this->filePath);

  43.         //检查文件大小是否超出限制
  44.         if (!$this->checkSize()) {
  45.             $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
  46.             return;
  47.         }

  48.         //检查文件内容是否真的是图片--远程抓取图片本地化化报错屏蔽

  49.        /* if (substr(mime_content_type($this->filePath), 0, 5) != 'image') {
  50.             $this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
  51.             return;
  52.         }*/

  53.         //创建目录失败
  54.         if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
  55.             $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
  56.             return;
  57.         } else if (!is_writeable($dirname)) {
  58.             $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
  59.             return;
  60.         }

  61.         //移动文件
  62.         if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败
  63.             $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
  64.         } else { //移动成功
  65.             $this->stateInfo = $this->stateMap[0];
  66.         }
复制代码
  1. private  function action_crawler(){

  2.                 set_time_limit(0);
  3.                 pc_base::load_app_class('uploader', '', 0);
  4.                 pc_base::load_sys_class('attachment','',0);
  5.                 $module = trim($_GET['module']);
  6.                 $catid = intval($_GET['catid']);
  7.                 $siteid = $this->get_siteid();
  8.                 $site_setting = get_site_setting($siteid);
  9.                 $site_allowext = $site_setting['upload_allowext'];
  10.                 $attachment = new attachment($module,$catid,$siteid);
  11.                 $attachment->set_userid($this->userid);
  12.                 $CONFIG = $this->config();
  13.                 /* 上传配置 */
  14.                 $config = array(
  15.                         "pathFormat" => $CONFIG['catcherPathFormat'],
  16.                         "maxSize" => $CONFIG['catcherMaxSize'],
  17.                         "allowFiles" => $CONFIG['catcherAllowFiles'],
  18.                         "oriName" => "remote.png"
  19.                 );
  20.                 $fieldName = $CONFIG['catcherFieldName'];

  21.                 /* 抓取远程图片 */
  22.                 $list = array();
  23.                 if (isset($_POST[$fieldName])) {
  24.                         $source = $_POST[$fieldName];
  25.                 } else {
  26.                         $source = $_GET[$fieldName];
  27.                 }


  28.                 foreach ($source as $imgUrl) {
  29.                         $item = new uploader($imgUrl, $config, "remote");
  30.                         $info = $item->getFileInfo();
  31.                         array_push($list, array(
  32.                                 "state" => $info["state"],
  33.                                 "url" => $info["url"],
  34.                                 "size" => $info["size"],
  35.                                 "title" => htmlspecialchars($info["title"]),
  36.                                 "original" => htmlspecialchars($info["original"]),
  37.                                 "source" => $imgUrl, //不能使用htmlspecialchars 否则图片参数会匹配不上。
  38.                         ));

  39.                         /* 返回数据 */
  40.                  if($info['state']=='SUCCESS'){

  41.                                 $uploadedfile =array(
  42.                                         'filename'=>$info['original'],
  43.                                         'filepath'=>str_replace('/uploadfile/','',$info['url']),

  44.                                         'filesize'=>$info['size'],
  45.                                         'fileext'=>$info['type'],
  46.                                         'fn'=>1);

  47.                                 $attachment->add($uploadedfile);
  48.                         }




  49.                 }

  50.                 /* 返回抓取数据 */
  51.                 return json_encode(array(
  52.                         'state'=> count($list) ? 'SUCCESS':'ERROR',
  53.                         'list'=> $list
  54.                 ));

  55.     }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | ע

本版积分规则

QQ|Archiver|ֻ|С|ѡ̳

GMT+8, 2026-5-2 07:19 , Processed in 0.085419 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表
0.1023s