Просмотр файла cron/get_course.php

Размер файла: 3.65Kb
<?
include (dirname(__FILE__)."/../includes/common.php");

function cErr($msg){
	global $BILLING_EMAIL; 
	@mail($BILLING_EMAIL,"Ошибка при обновлении курсов",$msg,"From: ".$SETTINGS['site_name']." CRON<$BILLING_EMAIL>");
}



/* CURRENCY */

$val1_row = mysql_fetch_object(mysql_query("select * from currencies where code='RUR'"));
$time = time();

/* GLOBAL */
$charCode = "";
$xmlTag = "";
$nominal = "";

function startElement($parser, $name, $attrs) {
	global $xmlTag;
	$xmlTag = $name;
}

function endElement($parser, $name) {
}

function characterData($parser, $data) {
   global $xmlTag;
   global $charCode;
   global $val1_row;
   global $time;
   global $nominal;

   if(strtoupper($xmlTag)=="CHARCODE" && trim($data)!=""){
		$charCode = trim($data);
   }
   if(strtoupper($xmlTag)=="NOMINAL" && trim($data)!=""){
		$nominal = trim($data);
   }
   
   if(strtoupper($xmlTag)=="VALUE"){
		$tmp_row = mysql_fetch_object(mysql_query("select * from currencies where code='".strtoupper($charCode)."'"));
		if($tmp_row->id!=""){
			$value = str_replace(",",".",$data)+0;
			if($value > 0){
				mysql_query("INSERT INTO `courses` (`val1_id` ,`val1_number` , `val2_id` ,`val2_number` , `course` , `back_course` , `date`) VALUES ('".($tmp_row->id)."',$nominal,'".$val1_row->id."',1, $value, '".round(1/$value,6)."', '".$time."');");
			}
		}
   }
}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen("http://www.cbr.ru/scripts/XML_daily.asp","r"))) {
   die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
       die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($xml_parser)),
                   xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);


/* start LVL course */
function startLVLElement($parser, $name, $attrs) {
	global $xmlTag;
	$xmlTag = $name;
}

function endLVLElement($parser, $name) {
}

function characterLVLData($parser, $data) {
   global $xmlTag;
   global $charCode;
   global $val1_row;
   global $time;

   if(strtoupper($xmlTag)=="ID" && trim($data)!=""){
		$charCode = trim($data);
   }
   
   if(strtoupper($xmlTag)=="RATE"&&strtoupper($charCode)=="USD"&&trim($data)!=""){
		if(!($data > 0)){
			exit;
		}
		$tmp_row = mysql_fetch_object(mysql_query("select * from currencies where code='LVL'"));
		if($tmp_row->id!=""){
			$value = str_replace(",",".",round(GetCourse("USD")/$data,4))+0;
			if($value > 0){
				mysql_query("INSERT INTO `courses` (`val1_id` ,`val1_number` , `val2_id` ,`val2_number` , `course` , `back_course` , `date`) VALUES ('".($tmp_row->id)."',1,'".$val1_row->id."',1, $value, '".round(1/$value,6)."', '".$time."');");
			}
		}
   }
}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startLVLElement", "endLVLElement");
xml_set_character_data_handler($xml_parser, "characterLVLData");
if (!($fp = fopen("http://www.bank.lv/ValutuKursi/XML/xml.cfm","r"))) {
   die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
       die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($xml_parser)),
                   xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);
/* end LVL course */

exit;
?>