Esempio n. 1
0
 /**
  * Retrieves data from cache, if it's there.  If it is, but it's expired,
  * it performs a conditional GET to see if the data is updated.  If it
  * isn't, it down updates the modification time of the cache file and
  * returns the data.  If the cache is not there, or the remote file has been
  * modified, it is downloaded and cached.
  *
  * @param string URL of remote file to retrieve
  * @param int Length of time to cache file locally before asking the server
  *            if it changed.
  * @return string File contents
  */
 function retrieveFile($url, $cacheLength, $cacheDir)
 {
     $cacheID = md5($url);
     $cache = new Cache_Lite(array("cacheDir" => $cacheDir, "lifeTime" => $cacheLength));
     if ($data = $cache->get($cacheID)) {
         return $data;
     } else {
         // we need to perform a request, so include HTTP_Request
         include_once 'HTTP/Request.php';
         // HTTP_Request has moronic redirect "handling", turn that off (Alexey Borzov)
         $req = new HTTP_Request($url, array('allowRedirects' => false));
         // if $cache->get($cacheID) found the file, but it was expired,
         // $cache->_file will exist
         if (isset($cache->_file) && file_exists($cache->_file)) {
             $req->addHeader('If-Modified-Since', gmdate("D, d M Y H:i:s", filemtime($cache->_file)) . " GMT");
         }
         $req->sendRequest();
         if (!($req->getResponseCode() == 304)) {
             // data is changed, so save it to cache
             $data = $req->getResponseBody();
             $cache->save($data, $cacheID);
             return $data;
         } else {
             // retrieve the data, since the first time we did this failed
             if ($data = $cache->get($cacheID, 'default', true)) {
                 return $data;
             }
         }
     }
     Services_ExchangeRates::raiseError("Unable to retrieve file {$url} (unknown reason)", SERVICES_EXCHANGERATES_ERROR_RETRIEVAL_FAILED);
     return false;
 }
Esempio n. 2
0
 function _retrieveFile($url, $cacheLength, $cacheDir)
 {
     $cacheID = md5($url);
     $cache = new Cache_Lite(array("cacheDir" => $cacheDir, "lifeTime" => $cacheLength));
     if ($data = $cache->get($cacheID)) {
         return $data;
     } else {
         $fp = fopen($url, 'r');
         $data = stream_get_contents($fp);
         if (strlen($data) > 10) {
             // data is changed, so save it to cache
             $cache->save($data, $cacheID);
             return $data;
         } else {
             // retrieve the data, since the first time we did this failed
             if ($data = $cache->get($cacheID, 'default', true)) {
                 return $data;
             }
         }
     }
     Services_ExchangeRates::raiseError("Unable to retrieve file {$url} (unknown reason)", SERVICES_EXCHANGERATES_ERROR_RETRIEVAL_FAILED);
     return false;
 }
Esempio n. 3
0
 *
 * @package Services_ExchangeRates
 */
/**
 * Requires Services_ExchangeRates to function
 */
require_once 'Services/ExchangeRates.php';
/**
 * Creates new instance of currency converter
 *
 * @param string Choose where the exchange rates are coming from. In this case,
 *               it's the European Central Bank.
 * @param string Choose where the currency rates are coming from. In this case,
 *               it's the United Nations.
 */
$conv = new Services_ExchangeRates('ECB', 'UN');
?>

<html>
<head>
<title>Currency Converter - PEAR::Services_ExchangeRates Example</title>
</head>
<body>

<h1>Currency Converter</h1>

<?php 
if (!empty($_POST['amount'])) {
    echo "<h1>";
    echo $conv->format($_POST['amount']) . ' ' . $_POST['from'];
    echo " = ";
Esempio n. 4
0
 /**
  * Set to debug mode
  *
  * When an error is found, the script will stop and the message will be displayed
  * (in debug mode only).
  */
 function setToDebug()
 {
     self::$_pearErrorMode = SERVICES_EXCHANGERATES_ERROR_DIE;
 }
Esempio n. 5
0
loader_import('saf.Date');
loader_import('saf.GUI.Pager');
$q = db_query($sql);
if (!$q->execute()) {
    $total = 0;
    $invoices = array();
} else {
    $total = $q->rows();
    $invoices = $q->fetch($cgi->offset, 20);
}
$q->free();
$pg = new Pager($cgi->offset, 20, $total);
$pg->setUrl(site_current() . '?client=%s&status=%s', $cgi->client, $cgi->status);
$pg->update();
$dc = appconf('default_currency');
$exch = new Services_ExchangeRates('ECB', 'UN', 'UN', array('roundToDecimal' => 2, 'roundAutomatically' => true, 'thousandsSeparator' => '', 'decimalCharacter' => '.', 'cacheDirectory' => 'inc/app/siteinvoice/data/rates/', 'cacheLengthRates' => 86400, 'cacheLengthCurrencies' => 2592000, 'cacheLengthCountries' => 2592000));
$subtotal = 0;
$taxes = 0;
$total = 0;
if ($cgi->status == 'unpaid') {
    $today = date('Y-m-d');
    $thirty = Date::subtract($today, '30 day') . ' 00:00:00';
    $forty_five = Date::subtract($today, '45 day') . ' 00:00:00';
    $sixty = Date::subtract($today, '60 day') . ' 00:00:00';
    $ninety = Date::subtract($today, '90 day') . ' 00:00:00';
    foreach (array_keys($invoices) as $k) {
        if ($invoices[$k]->sent_on < $ninety) {
            $invoices[$k]->range = 90;
        } elseif ($invoices[$k]->sent_on < $sixty) {
            $invoices[$k]->range = 60;
        } elseif ($invoices[$k]->sent_on < $forty_five) {
Esempio n. 6
0
 /**
  * Downloads exchange rates in terms of the PLN from the National Bank of
  * Poland (NBP). This information is updated daily, and is cached by default for 1 hour.
  *
  * Returns a multi-dimensional array containing:
  * 'rates' => associative array of currency codes to exchange rates
  * 'source' => URL of feed
  * 'date' => date feed last updated, pulled from the feed (more reliable than file mod time)
  *
  * @link http://www.nbp.pl/Kursy/RatesA.html English HTML version
  * @link http://www.nbp.pl/Kursy/KursyA.html Polish HTML version (with link to XML)
  *
  * @param int Length of time to cache (in seconds)
  * @return array 
  */
 function retrieve($cacheLength, $cacheDir)
 {
     $return['rates'] = array('PLN' => 1.0);
     // retrieve XML address
     $htmlpage = $this->retrieveFile($this->feedHTMLUrl, $cacheLength, $cacheDir);
     // Example line is:
     // <div class="file"><a href="xml/a055z020319.xml">powysza tabela w formacie .xml</a></div>
     if (!preg_match('#href="(xml/a\\d+z\\d+\\.xml)"#', $htmlpage, $match)) {
         Services_ExchangeRates::raiseError("Retrieved url " . $this->feedHTMLUrl . " has no link to XML page", SERVICES_EXCHANGERATES_RETRIEVAL_FAILED);
         return false;
     }
     $this->feedXMLUrl = $this->feedDir . $match[1];
     $return['source'] = $this->_feedXMLUrl;
     // retrieve the feed from the server or cache
     $root = $this->retrieveXML($this->feedXMLUrl, $cacheLength, $cacheDir);
     // get down to array of exchange rates
     foreach ($root->children as $rateinfo) {
         if ($rateinfo->name == 'pozycja') {
             $rateinfo->children[4]->content = strtr($rateinfo->children[4]->content, ',', '.');
             $value = $rateinfo->children[2]->content / $rateinfo->children[4]->content;
             // currency rate
             $return['rates'][$rateinfo->children[3]->content] = $value;
         } elseif ($rateinfo->name == 'data_publikacji') {
             // set date published
             $return['date'] = $rateinfo->content;
         }
     }
     return $return;
 }