<?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 proinfoModel{
	function Run(&$atts, &$refObj, &$fields){
		if(!$_GET['products_id']){echo "Error:{openzc:proinfo}该标签适用于商品详情页，<a href='http://www.openzc.com/' target='_blank'>查看说明</a>！";return false;}
		global $product_class,$category_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["products_info"]) && !$desclen && empty($separator)){
			$products=$GLOBALS["products_info"];
		}else{
			$products=$product_class->get_products_info($_GET['products_id'],$separator);
			if($desclen){
				$products['products_description_preview']=$this->getDesview($products['products_description_preview'],$desclen);
			}
			$GLOBALS["products_info"]=$products;
		}
		
		//**匹配描述中的图片，图片过大或者远程图片裁剪后缓存至本地**//
		if($field=="products_description" && $imgsizer){
			global $ImageSizer;
			$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
			preg_match_all($pattern,$products['products_description'],$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;
				$products[$field]=str_replace($v,$img,$products[$field]);
			}
		}
		if($field=="products_image" && $imgsizer){
			global $ImageSizer;
			$remote=$product_class->checkRemoteimg($products[$field]);
			if($imgsizer && $remote==false){
				$imgsizer=explode(",",$imgsizer);
				$imgname=getCacheImgname(basename($products[$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("./".$products[$field]);
					$ImageSizer->resize($imgsizer[0],$imgsizer[1],$bgcolor);
					$ImageSizer->save($image_new);
				}
				$products[$field]=DIR_WS_CATALOG.str_replace(ROOT_PATH,"",$imgdir).$imgname;
			}

		}
		
		return $products[$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;
	}
}

  
?>