<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class arcinfoModel{
	function Run(&$atts, &$refObj, &$fields){
		if(!$_GET['archives_id']){echo "Error:{openzc:arcinfo}该标签适用于文章详情页，<a href='http://www.openzc.com/' target='_blank'>查看说明</a>！";return false;}
		global $blog_class;
		$attlist = "field=,desclen=,imgsizer=,bgcolor=,separator=";
		
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE); 
		$strlen = empty($strlen) ? 200 : $strlen;
		
		if(isset($GLOBALS["archives_info"]) && !$desclen && empty($separator)){
			$archives=$GLOBALS["archives_info"];
		}else{
			$archives=$blog_class->get_archives_info($_GET['archives_id'],$separator);
			if($desclen){
				$archives['archives_description']=$this->getDesview($archives['archives_description'],$desclen);
			}
			$GLOBALS["archives_info"]=$archives;
		}
		if($field=="tag"){
			$tags=explode(",",$archives['archives_tag']);
			foreach($tags as $k => $v){
				$taglist[$k]['sort_index']=$k+1;
				$taglist[$k]['tag_name']=$v;
				$taglist[$k]['tag_link']=zen_href_link(FILENAME_ADVANCED_SEARCH_RESULT,"channel=blog_tag&keyword=".$v);
			}
			return $taglist;
		}
		//**匹配描述中的图片，图片过大或者远程图片裁剪后缓存至本地**//
		if($field=="archives_body" && $imgsizer){
			global $ImageSizer;
			$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
			preg_match_all($pattern,$archives['archives_body'],$match);
			$imgsizer=explode(",",$imgsizer);
			$imgdir=IMGCACHE_DIR.$imgsizer[0]."-".$imgsizer[1]."/";
			foreach($match[1] as $k => $v){
				$img=$v;
				$imgname=getCacheImgname(basename($img));
				if(strstr($img,"http") && !strstr($img,$_SERVER['HTTP_HOST'])){
					if(!is_dir(IMGCACHE_DIR."remote")){mkdir(IMGCACHE_DIR."remote");}
					$img=file_get_contents($img);
					$cachefile = fopen(IMGCACHE_DIR."remote/".$imgname, "w") or die("Unable to open file!");
					fwrite($cachefile, $img);
					fclose($cachefile);
					$img=str_replace(ROOT_PATH,"",IMGCACHE_DIR."remote/".$imgname);
				}
				if(is_file("./images/".$img)){
					$img="/images/".$img;
				}else if(is_file("./".$img)){
					$img="/".$img;
				}
				$image_new=$imgdir.$imgname;
				if(!is_dir($imgdir)){mkdir($imgdir);}
				if(getCacheImgcheck($image_new)==true){
					$ImageSizer->Run(".".$img);
					$ImageSizer->resize($imgsizer[0],$imgsizer[1],$bgcolor);
					$ImageSizer->save($image_new);
				}
				$img=DIR_WS_CATALOG.str_replace(ROOT_PATH,"",$imgdir).$imgname;
				$archives[$field]=str_replace($v,$img,$archives[$field]);
			}
		}
		if($field=="archives_image" && $imgsizer){
			global $ImageSizer;
			if($imgsizer){
				$imgsizer=explode(",",$imgsizer);
				$imgname=getCacheImgname(basename($archives[$field]));
				$imgdir=IMGCACHE_DIR.$imgsizer[0]."-".$imgsizer[1]."/";
				$image_new=$imgdir.$imgname;
				if(!is_dir($imgdir)){
					mkdir($imgdir);
				}
				if(getCacheImgcheck($image_new)==true){
					$ImageSizer->Run(".".$archives[$field]);
					$ImageSizer->resize($imgsizer[0],$imgsizer[1],$bgcolor);
					$ImageSizer->save($image_new);
				}
				$archives[$field]=DIR_WS_CATALOG.str_replace(ROOT_PATH,"",$imgdir).$imgname;
			}
		}
		
		return $archives[$field];
		
    }
	
	private function getDesview($str,$len){
		$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern,$str,$match);
		foreach($match[0] as $k => $v){
			$str=str_replace($v,"",$str);
		}
		if($len){
			$str=substr($str,0,$len);
		}
		return $str;
	}
}

  
?>