<?php
class TopAction extends FuncAction
{
    protected function _initialize()
    {
        Input::noGPC();
        if (false === ($setting = S('setting'))) {
            $setting = D('setting')->setting_cache();
        }		
        C($setting);
        $this->assign('async_sendmail', session('async_sendmail'));
    }
    
	
	protected function _request($name, $trim='', $val=''){
return I($name,$val,$trim);
}

protected function _get($name, $trim='', $val=''){
return I($name,$val,$trim);
}
	
    protected function _404($url = '')
    {
    	   $this->display(TMPL_PATH . '404.html');
    	   header("HTTP/1.0 404 Not Found");  
    	   exit;
    }
    
    public function getItem($num_iid,$activityId){
 	$appkey = trim(C('yh_taobao_appkey'));
    	$appsecret = trim(C('yh_taobao_appsecret'));
    	if (! empty($appkey) && ! empty($appsecret) && !empty($num_iid)) {
    		vendor('taobao.TopSdk');
    		$c = new TopClient();
    		$c->appkey = $appkey;
    		$c->secretKey = $appsecret;
    		$req = new TbkItemInfoGetRequest();
    		$req->setFields("num_iid,user_type,title,seller_id,volume,nick,pict_url,reserve_price,zk_final_price,item_url");
    		$req->setPlatform("1");
    		$req->setNumIids($num_iid);
    		$resp = $c->execute($req);
    		$resparr = xmlToArray($resp);
    		$contents = $resparr['results']['n_tbk_item'];
		}
        if ($contents) {
			$info = array();
			$info['title'] = $contents['title'];
			$info['volume'] = $contents['volume'];
			$info['price'] = $contents['zk_final_price'];
			$info['pic_url'] = $contents['pict_url'];
			$info['pic_url'] = str_replace('_320x320.jpg', "", $info['pic_url']);
			$info['sellerId'] = $contents['seller_id'];
			$info['nick'] = $contents['nick'];
            if ($contents['user_type'] == "1") {
                $info['shop_type'] = "B";
            } else {
                $info['shop_type'] = "C";
            }
			$info['num_iid'] = $num_iid;
            $info['coupon_start_time'] = date('Y-m-d H:i', time());
            $info['coupon_end_time'] = date('Y-m-d H:i', time() + 86400 * 7);
			$descUrl = 'http://hws.m.taobao.com/cache/mtop.wdetail.getItemDescx/4.1/?data=%7B%22item_num_id%22%3A%22' . $num_iid . '%22%7D';
//          $source = $this->_curl($descUrl);
//          $source = $yhxia_https->results;
//          if (!$source) {
//              $source = file_get_contents($descUrl);
//          }
//          $result_data = json_decode($source, true);
//          $dinfo = array();
//          $num = $result_data['data']['images'];
//          for ($i = 0; $i < count($num); $i++) {
//              $images = $i + 1;
//              $desc[$images] = $num[$i];
//              $desc[$images] = '<img class="lazy" src=' . $desc[$images] . '>';
//          }
//          $info['desc'] = $desc[1] . '' . $desc[2] . '' . $desc[3] . '' . $desc[4] . '' . $desc[5] . '' . $desc[6] . '' . $desc[7] . '' . $desc[8] . '' . $desc[9] . '' . $desc[10] . '' . $desc[11] . '' . $desc[12] . '' . $desc[13] . '' . $desc[14] . '' . $desc[15] . '' . $desc[16] . '' . $desc[17] . '' . $desc[18] . '' . $desc[19] . '' . $desc[20] . '' . $desc[21] . '' . $desc[22] . '' . $desc[23] . '' . $desc[24] . '' . $desc[25] . '' . $desc[26] . '' . $desc[27] . '' . $desc[28] . '' . $desc[29] . '' . $desc[30];
//			
            return $info;
        } else {
            return false;
        }
    }
    
    protected function _curl($url, $data = array(), $is_post = false, $cookie = null, $referer = null)
    {
        set_time_limit(30);
        $curl = curl_init();
        if($data && $is_post == false){
            $url .= '?' . http_build_query($data);
        }
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //
		curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //
		curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if($is_post){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        if($cookie){
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
       curl_setopt($curl, CURLOPT_REFERER, C('yh_site_url'));
       curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5' );  
        curl_setopt($curl, CURLOPT_TIMEOUT,30);
        $result = curl_exec($curl);
		//var_dump(curl_error($curl));
        curl_close($curl);
        if (empty($result)) {
            $result = false;
        }
        
        return $result;
    }
    
    public function http($url)
    {
        set_time_limit(0);
        $result = file_get_contents($url);
        if ($result === false) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
            curl_close($curl);
        }
        if (empty($result)) {
            $result = false;
        }
        return $result;
    }
    public function get_id($url)
    {
        $id = 0;
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            if (isset($params['id'])) {
                $id = $params['id'];
            } elseif (isset($params['item_id'])) {
                $id = $params['item_id'];
            } elseif (isset($params['default_item_id'])) {
                $id = $params['default_item_id'];
            } elseif (isset($params['amp;id'])) {
                $id = $params['amp;id'];
            } elseif (isset($params['amp;item_id'])) {
                $id = $params['amp;item_id'];
            } elseif (isset($params['amp;default_item_id'])) {
                $id = $params['amp;default_item_id'];
            }
        }
        return $id;
    }
	
	
protected function getRobot(){
if(empty($_SERVER['HTTP_USER_AGENT']))
 {
  return false;
 }
 $searchEngineBot = array(
  'googlebot'=>'google',
  'mediapartners-google'=>'google',
  'baiduspider'=>'baidu',
  'msnbot'=>'msn',
  'dotbot'=>'dotbot',
  'youdaobot'=>'yodao',
  'yahoo! slurp'=>'yahoo',
  'yahoo! slurp china'=>'yahoo',
  'iaskspider'=>'iask',
  'sogou web spider'=>'sogou',
  'sogou push spider'=>'sogou',
  'sosospider'=>'soso',
  'bingbot'=>'bing',
  'yandexbot'=>'yan',
  'ahrefsbot'=>'ahrefs',
  '360Spider'=>'360',
  'sitemapx'=>'sitemapx',
  'spider'=>'other',
  'alibaba.security'=>'alibaba',
  'bot'=>'bot',
  'crawler'=>'other',
 );
 $spider = strtolower($_SERVER['HTTP_USER_AGENT']);
 foreach ($searchEngineBot as $key => $value)
 { 
  if (strpos($spider, $key)!== false)
  {
   return $value;
  }
 }
 return false;
}

protected function taobaodetail($id){
    	$appkey = trim(C('yh_taobao_appkey'));
    	$appsecret = trim(C('yh_taobao_appsecret'));
    	if (!empty($appkey) && !empty($appsecret) && !empty($id)) {
    		import('TopSdk', VENDOR_PATH.'taobao', '.php');
    		$c = new \TopClient();
    		$c->appkey = $appkey;
    		$c->secretKey = $appsecret;
    		$req = new \TbkItemInfoGetRequest();
    		$req->setFields("free_shipment,ratesum,shop_dsr,is_prepay,num_iid,user_type,title,seller_id,volume,pict_url,reserve_price,zk_final_price,item_url,provcity,nick");
    		$req->setPlatform("1");
    		$req->setNumIids($id);
    		$resp = $c->execute($req);
    		$resparr = xmlToArray($resp);
    		$newitem = $resparr['results']['n_tbk_item'];
    		if($newitem) return $newitem;
		}
		return false;
}    
    /*
	 * 验证是否存在openid
	 */
	protected function verificat(){
		$openid = $_SESSION['user']['openid'];
		$json=array(                                 
			'status'=>'out',                             
			'msg'=>'登录超时'                                
		);  
	  	if(!$openid){
	  		exit(json_encode($json));
	  	}
	}  
     
}