<?php
class DecodeThisPF{
	public $vin, $baseurl, $cref, $rID;
	function __construct(){
		//$this->cref = curl_init();
		//curl_setopt($this->cref, CURLOPT_RETURNTRANSFER,1);
	}
	function read($vin,$id){
		$this->rID = $id;
		$tArr = NULL;

		$this->baseurl = 'http://www.decodethis.com/webservices/api.ashx?vin='.$vin.'&apikey=737faa81-d5ec-4abf-aa55-b704199137be&format=JSONP';
		$this->vin = $vin;
		//curl_setopt($this->cref, CURLOPT_URL, $this->baseurl);
		//$fres = curl_exec($this->cref);
		file_get_contents($this->baseurl);
		
		$fres = substr($fres,1,strrpos($fres,")")-1);
		
		$iobrack = strpos($fres,"[");
		$fresArr = json_decode($fres,true);
		
		$isArr=false;
		if(json_last_error()==0){
			if(trim($fresArr["decode"]["status"])=="SUCCESS"){
				if(!$iobrack===false) { if($iobrack<70) $isArr=true; }
				if($isArr){
					$tArr = $this->fetchMultiDetails($fresArr["decode"]["vehicle"][0]["id"]);
				}else{
					$tArr = $this->fetchDetails($fresArr["decode"]["vehicle"]["id"]);
				}
			}
		}
		//curl_close($this->cref);
		return $tArr;
	}
	function fetchDetails($id){
		$ss=false;
		
		//curl_setopt($this->cref, CURLOPT_URL, $this->baseurl.'&detail='.$id);
		$dtrs = file_get_contents($this->baseurl.'&detail='.$id);//curl_exec($this->cref);
		
		$dtrs = substr($dtrs,1,strrpos($dtrs,")")-1);
		$dtrsArr = json_decode($dtrs,true);
		
		if(json_last_error()==0){
			$ss = $this->returnDetails($dtrsArr["decode"]["vehicle"], $dtrsArr["decode"]["vehicle"]["id"]);
		}
		return $ss;
	}
	function fetchMultiDetails($id){
		$ss=false;
		//curl_setopt($this->cref, CURLOPT_URL, $this->baseurl.'&detail='.$id);
		//$dtrs = curl_exec($this->cref);
		$dtrs = file_get_contents($this->baseurl.'&detail='.$id);
		
		$dtrs = substr($dtrs,1,strrpos($dtrs,")")-1);
		$dtrsArr = json_decode($dtrs,true);
		
		if(json_last_error()==0){
			$vehs = $dtrsArr["decode"]["vehicle"];
			for($i=0; $i<sizeof($vehs); $i++){
				$ss = $this->returnDetails($vehs[$i], $vehs[$i]["id"]);
			}
		}
		return $ss;
	}
	
	function returnDetails($veh, $did){
		$NODATA = "No data"; $ss = false;
		$equip = $veh["Equip"];
		
		$make = $veh["make"];
		$model = $veh["model"];
		$body = $veh["body"];
		$trim = $veh["trim"];
		$year = $veh["year"];
		
		for($i=0; $i<sizeof($equip); $i++){
			$equipA = $equip[$i];
			switch($equipA["name"]){
				case "Engine Type":
				$val = trim($equipA["value"]);
				if($val==$NODATA){ $capacity = ""; $enginetype = ""; }
				else{
					$pOL = strpos($equipA["value"],"L");
					$enginetype = trim(substr($equipA["value"],$pOL+1));
					$capacity = trim(substr($equipA["value"],0,$pOL));
				}
				break;
				
				case "Standard Seating":
				$val = trim($equipA["value"]);
				if($val==$NODATA){ $seats = ""; }
				else{
					$str = trim($equipA["value"]);
					if(ctype_digit($str)) $seats = $str;
				}
				break;
				
				case "Optional Seating":
				$val = trim($equipA["value"]);
				if($val!=$NODATA){
					if(ctype_digit($val)){
						if($seats==""){ $seats = $val; }
						else{
							if($seats!=$val){
								if($seat<=$val){ $seats = $seats."|".$val; }
								else{ $seats = $val."|".$seats; }
							}
						}
					}
				}
				break;
			}
		}
		$res = mysql_query("update vinprefixes set dtseats='{$seats}', dtcapacity='{$capacity}' where id='{$this->rID}'");
		
		return array("seats"=>$seats, "capacity"=>$capacity);
	}
}
?>