. */ require_once("./IRundaily.php"); class CurrenciesRundaily implements IRundaily { private $messages; public function __construct() { $this->messages = array(); } public function run() { $rates = $this->downloadRates(); if( count($rates) > 1 ) { $this->processRates($rates); }else { $this->addMessage("Failed fetching currencies from EC website."); } } public function isEnabled() { global $setup; return $setup['EventDatePartTypeFix']!="" && is_before_ex("EventDatePartTypeFix"); } public function getTitle() { return "Update currencies"; } public function getMessages() { return $this->messages; } private function addMessage($message) { $this->messages[] = $message; } private function downloadRates() { $rates = array(); $fp = urlopen("ec.europa.eu", "/budg/inforeuro/api/monthly-rates/csv?year=" . date("Y") . "&month=" . date("n") . "&pointSeparator=true&lang=EN", "HTTP/1.0"); if( $fp != null ) { while( !feof($fp) ) { $buf = fgets($fp, 1024); $cols = split(";", $buf); if( count($cols) >= 5 && strlen($cols[2]) == 2 ) { $rates[$cols[2]] = str_replace(",", ".", $cols[5]); } } $rates['EU'] = 1; }else { $this->addMessage("Failed downloading currencies from ec.europa.eu"); } return $rates; } private function processRates($rates) { $query = "SELECT `id`, `ISO_2`, `rate` FROM `currencies`"; if( ($res=doquery($query)) && mysql_num_rows($res)>0 ) { while( $currency = mysql_fetch_assoc($res) ) { if( array_key_exists($currency['ISO_2'], $rates) ) { $rate = number_format(1 / $rates[$currency['ISO_2']], 10); if( $rate != $currency['rate'] ) { $query = "UPDATE `currencies` SET `rate` = " . $rate . " WHERE `id` = " . $currency['id']; if( doquery($query) ) { $this->addMessage("Updated currency " . $currency['ISO_2'] . " to " . $rate); }else { $this->addMessage("Failed updating currency " . $currency['ISO_2'] . " to " . $rate); } } } } }else { $this->addMessage("Failed fetching current currencies from database"); } } } ?>