<?php
date_default_timezone_set('Asia/Shanghai');
$currencydate = date('Y-m-d');
if($_GET['currencydate'] != ''){
	$currencydate = $_GET['currencydate'];
}
$currency = json_decode(file_get_contents('data/Currency.json'), true);




if(!file_exists('currency/'.str_replace('-','',$currencydate)) || date('H') == 10  || $_GET['debug'] == 1 ){
	
	$superrich1965 =  json_decode(file_get_contents("https://superrichrate2.ztidev.com/sprrate/api/v1/rates/locations/33"));
	foreach ($superrich1965 as $r){
		if($r->currencyCode == 'CNY'){
			$rmbtobath =  $r->buy;
			break;
		}
	}

	$bankofchina = file_get_contents("https://www.bankofchina.com/sourcedb/whpj/enindex.html");
	$bankofchina = explode('">USD',$bankofchina);
	$bankofchina = explode('</td>',$bankofchina[1]);
        
        $bankofchina2 = explode('">',$bankofchina[3]);
	$bankofchina2 = $bankofchina2[1]/100;
        
	$bankofchina = explode('">',$bankofchina[1]);
	$bankofchina = $bankofchina[1]/100;
        
        
       
        
        
        $myr = file_get_contents("https://www.bankofchina.com/sourcedb/myr/");
	$myr = explode('<td>CNY',$myr);
	$myr = explode('</tr>',$myr[1]);
	$myr = explode('<td>',$myr[0]);        
	$myr = explode('</td>',$myr[2]);
        $myr = $myr[0];
        $myr = $myr/100;
        $ar['MYR'] = $myr;
        foreach ($currency as $key => $ru){
                if('MYR' == $ru['currency'] && $myr > 0){					
                        $currency[$key]['exchangerate'] = round($myr,4);
                }
        }
	
	
	$hsbc = file_get_contents("https://www.hsbc.com.vn/foreign-exchange/rate/");
	$hsbc = explode('<td width="593">',$hsbc);
	$hsbc = explode('</td>',$hsbc[1]);
	$hsbc = str_replace(',','',$hsbc[0]); 
	$hsbc = str_replace('.','',$hsbc); 
	
	$currencytoday = file_get_contents('http://www.floatrates.com/historical-exchange-rates.html?currency_date='.$currencydate.'&base_currency_code=CNY&format_type=html');
	$currencytoday = explode('bk-currency-rates',$currencytoday);
	$currencytoday = explode('</table>',$currencytoday[1]);
	$currencytoday =  $currencytoday[0];
	
	$currencytoday = explode('<tr>',$currencytoday);
	
	
	
	foreach ($currencytoday as $r){
              
		if(strpos($r,'USD') !==false  ){
			$r = explode('align="right">',$r);
			$r = explode('</td>',$r[2]);
			$r = $r[0]; 
			if($bankofchina > 0){
				$r = $bankofchina;
			}
			$ar['USD'] = $r;			
			foreach ($currency as $key => $ru){
				if('USD' == $ru['currency'] && $r > 0){
					$currency[$key]['exchangerate'] = round($r,4);
				}
			}
                        
                        if($bankofchina2 > 0){
				$r2 = $bankofchina2;
			}
                        $ar['USD2'] = $r2;			
			foreach ($currency as $key => $ru){
				if('USD2' == $ru['currency'] && $r2 > 0){
					$currency[$key]['exchangerate'] = round($r2,4);
				}
			}
		}
                
		if(strpos($r,'VND') !==false ){
			$r = explode('align="right">',$r);
			$r = explode('</td>',$r[2]);
			$r = $r[0];
			$ar['VND'] = $r;
			foreach ($currency as $key => $ru){
				if('VND' == $ru['currency'] && $r > 0){
					//$currency[$key]['exchangerate'] = $r;
					$ar['VND'] = $currency[$key]['exchangerate'];
				}
			}
		}
		
		if(strpos($r,'THB') !==false ){
			$r = explode('align="right">',$r);
			$r = explode('</td>',$r[2]);
			$r = $r[0];
			if($rmbtobath > 0){
				$r = 1/$rmbtobath;
			}
			$ar['THB'] = $r;
			foreach ($currency as $key => $ru){
				if('Baht' == $ru['currency'] && $r > 0){					
					$currency[$key]['exchangerate'] = round($r,4);
				}
			}
		}
                
               
                
                
	}
        
        $currencytoday = file_get_contents('http://www.floatrates.com/historical-exchange-rates.html?currency_date='.$currencydate.'&base_currency_code=HKD&format_type=html');
	$currencytoday = explode('bk-currency-rates',$currencytoday);
	$currencytoday = explode('</table>',$currencytoday[1]);
	$currencytoday =  $currencytoday[0];
	
	$currencytoday = explode('<tr>',$currencytoday);
        foreach ($currencytoday as $r){
             
                if(strpos($r,'USD') !==false ){
			$r = explode('align="right">',$r);
			$r = explode('</td>',$r[2]);
			$r = $r[0];
			
			$ar['HKD'] = $r;
			foreach ($currency as $key => $ru){
				if('HKD' == $ru['currency'] && $r > 0){					
					$currency[$key]['exchangerate'] = round($r,4);
				}
			}
		}
                
                if(strpos($r,'VND') !==false ){
			$r = explode('align="right">',$r);
			$r = explode('</td>',$r[1]);
			$r = $r[0];
			$r = str_replace(',','',$r);
                        $r = round($r,1);
			$ar['HKDVND'] = $r;
			foreach ($currency as $key => $ru){
				if('HKDVND' == $ru['currency'] && $r > 0){					
					$currency[$key]['exchangerate'] = $r;
				}
			}
		}
                
                 if(strpos($r,'CNY') !==false ){
			$r = explode('align="right">',$r);
			$r = explode('</td>',$r[1]);
			$r = $r[0];
			$r = str_replace(',','',$r);
                        $r = round($r,3);
			$ar['HKDRMB'] = $r;
			foreach ($currency as $key => $ru){
				if('HKDRMB' == $ru['currency'] && $r > 0){					
					$currency[$key]['exchangerate'] = $r;
				}
			}
		}
                
        }
        
     
        
        
	$ar['USDVND'] = $hsbc;
	foreach ($currency as $key => $ru){
		if('USDVND' == $ru['currency'] && $hsbc > 0){					
			$currency[$key]['exchangerate'] = $hsbc;
		}
	}
	
	file_put_contents('currency/'.str_replace('-','',$currencydate), json_encode($ar) );
	file_put_contents('data/Currency.json',json_encode(array_values($currency)));
        echo 'China Now is:'.date('Y-m-d H:i:s').'<br>';
    echo 'updated';
}else{
    echo 'China Now is:'.date('Y-m-d H:i:s').'<br>';
    echo 'Has been update at '.date('Y-m-d H:i:s',filemtime('currency/'.str_replace('-','',$currencydate))).'';
}


?>