+ $filename = "/search?q={$curr_b}+to+{$curr_a}";
+ $site = "https://www.google.com";
+ $contents = file_get_contents($site.$filename);
+ }
+ elseif ($provider == 'BLOOMBERG')
+ {
+ $filename = "/quote/{$curr_b}{$curr_a}:CUR";
+ $site = "www.bloomberg.com";
+ $proto = 'https://';
+ $contents=file_get_contents($proto.$site.$filename);
+ }
+ elseif ($provider == 'EXCHANGE-RATES.ORG')
+ {
+ $filename = "/converter/{$curr_b}/{$curr_a}/1";
+ $site = "exchange-rates.org";
+ $proto = 'https://';
+ $contents=file_get_contents($proto.$site.$filename);
+ }
+ if (empty($contents)) {
+ if (function_exists('curl_init'))
+ { // first check with curl as we can set short timeout;
+ $retry = 1;
+ do {
+ $ch = curl_init();
+ curl_setopt ($ch, CURLOPT_URL, $proto.$site.$filename);
+ curl_setopt ($ch, CURLOPT_COOKIEJAR, VARLIB_PATH."/cookie.txt");
+ curl_setopt ($ch, CURLOPT_HEADER, 0);
+ curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
+ // prevent warning while safe_mode/open_basedir on (redirection doesn't occur at least on ECB page)
+ if (!ini_get('safe_mode') && !ini_get('open_basedir'))
+ curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
+ curl_setopt ($ch, CURLOPT_TIMEOUT, 3);
+ $contents = curl_exec ($ch);
+ curl_close($ch);
+ // due to resolver bug in some curl versions (e.g. 7.15.5)
+ // try again for constant IP.
+ if (isset($site_ip))
+ $site=$site_ip;
+ } while( ($contents == '') && $retry--);
+ } else {
+ $contents = url_get_contents($proto.$site.$filename);
+ }
+ }
+ $val = '';
+ if ($provider == 'ECB')
+ {
+ $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
+ $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
+ preg_match ( $from_mask, $contents, $out );
+ $val_a = isset($out[1]) ? $out[1] : 0;
+ $val_a = str_replace ( ',', '', $val_a );
+ $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
+ preg_match ( $to_mask, $contents, $out );
+ $val_b = isset($out[1]) ? $out[1] : 0;
+ $val_b = str_replace ( ',', '', $val_b );
+ if ($val_b)