правильный код
public function refresh($force = false) {
$currencies = array();
if ($force) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'");
} else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' AND date_modified < '" . $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "'");
}
foreach ($query->rows as $result) {
$currencies[$result['code']] = $result['value'];
}
if ($currencies) {
$xml_data = $this->cache->get('currencies');
if (empty($xml_data)) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.cbr.ru/scripts/XML_daily.asp');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl);
curl_close($curl);
if ($content) {
$xml_data = json_decode(
json_encode(
(array)simplexml_load_string($content)
), true
);
file_put_contents(
DIR_CACHE . 'cache.currencies.' . strtotime('tomorrow'),
serialize($xml_data)
);
} else {
$this->log->write('Automatic currency update failed: Link is broken or empty data feed!');
return false;
}
}
$date_modified = date('Y-m-d H:i:s');
if ( isset($xml_data['@attributes']) && isset($xml_data['@attributes']['Date']) ) {
$date_modified = date('Y-m-d H:i:s', strtotime($xml_data['@attributes']['Date']) );
}
if ( isset($xml_data['Valute']) ) {
foreach ($xml_data['Valute'] as $Valute) {
if ( isset($currencies[$Valute['CharCode']]) && isset($Valute['Value'])) {
$value = floatval(
str_replace(',', '.', $Valute['Value'])
);
if ($value) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . 1/($value/$Valute['Nominal']) . "', date_modified = '" . $this->db->escape($date_modified) . "' WHERE code = '" . $this->db->escape($Valute['CharCode']) . "'");
}
unset($currencies[$Valute['CharCode']]);
}
if (empty($currencies)) {
break;
}
}
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '1.00000', date_modified = '" . $this->db->escape($date_modified) . "' WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "'");
$this->cache->delete('currency');
} else {
$this->log->write('Automatic currency update failed: Unable to parse data feed!');
$this->cache->delete('currencies');
}
}
}