|
|
- $imgUrl = htmlspecialchars($this->fileField);
- $trueimgUrl = $imgUrl = str_replace("& a m p", "&", $imgUrl); //处理图片参数问题
- //http开头验证
- if (strpos($imgUrl, "http") !== 0) {
- $this->stateInfo = $this->getStateInfo("ERROR_HTTP_LINK");
- return;
- }
- //获取请求头并检测死链
- $heads = get_headers($imgUrl, 1);
- if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
- $this->stateInfo = $this->getStateInfo("ERROR_DEAD_LINK");
- return;
- }
- //格式验证(扩展名验证和Content-Type验证)
- $fileType = strtolower(strrchr($imgUrl, '.'));
- /*需要修复下6b7.png?imageView&thumbnail=550x0 不能抓取的问题*/
- if(strpos($fileType,'?')){
- $fileType = substr($fileType,0,strpos($fileType,'?'));
- $trueimgUrl = substr($imgUrl,0,strpos($imgUrl,'?'));
- }
- if (!in_array($fileType, $this->config['allowFiles']) || !isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) {
- $this->stateInfo = $this->getStateInfo("ERROR_HTTP_CONTENTTYPE");
- return;
- }
- //打开输出缓冲区并获取远程图片
- ob_start();
- $context = stream_context_create(
- array('http' => array(
- 'follow_location' => false // don't follow redirects
- ))
- );
- readfile($trueimgUrl, false, $context);
- $img = ob_get_contents();
- ob_end_clean();
- preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $trueimgUrl, $m);
- $this->oriName = $m ? $m[1]:"";
- $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;
- }
- //检查文件内容是否真的是图片--远程抓取图片本地化化报错屏蔽
- /* if (substr(mime_content_type($this->filePath), 0, 5) != 'image') {
- $this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
- return;
- }*/
- //创建目录失败
- 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];
- }
复制代码- private function action_crawler(){
- set_time_limit(0);
- pc_base::load_app_class('uploader', '', 0);
- pc_base::load_sys_class('attachment','',0);
- $module = trim($_GET['module']);
- $catid = intval($_GET['catid']);
- $siteid = $this->get_siteid();
- $site_setting = get_site_setting($siteid);
- $site_allowext = $site_setting['upload_allowext'];
- $attachment = new attachment($module,$catid,$siteid);
- $attachment->set_userid($this->userid);
- $CONFIG = $this->config();
- /* 上传配置 */
- $config = array(
- "pathFormat" => $CONFIG['catcherPathFormat'],
- "maxSize" => $CONFIG['catcherMaxSize'],
- "allowFiles" => $CONFIG['catcherAllowFiles'],
- "oriName" => "remote.png"
- );
- $fieldName = $CONFIG['catcherFieldName'];
- /* 抓取远程图片 */
- $list = array();
- if (isset($_POST[$fieldName])) {
- $source = $_POST[$fieldName];
- } else {
- $source = $_GET[$fieldName];
- }
- foreach ($source as $imgUrl) {
- $item = new uploader($imgUrl, $config, "remote");
- $info = $item->getFileInfo();
- array_push($list, array(
- "state" => $info["state"],
- "url" => $info["url"],
- "size" => $info["size"],
- "title" => htmlspecialchars($info["title"]),
- "original" => htmlspecialchars($info["original"]),
- "source" => $imgUrl, //不能使用htmlspecialchars 否则图片参数会匹配不上。
- ));
- /* 返回数据 */
- if($info['state']=='SUCCESS'){
- $uploadedfile =array(
- 'filename'=>$info['original'],
- 'filepath'=>str_replace('/uploadfile/','',$info['url']),
- 'filesize'=>$info['size'],
- 'fileext'=>$info['type'],
- 'fn'=>1);
- $attachment->add($uploadedfile);
- }
- }
- /* 返回抓取数据 */
- return json_encode(array(
- 'state'=> count($list) ? 'SUCCESS':'ERROR',
- 'list'=> $list
- ));
- }
复制代码
|
|