home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Services / ExchangeRates / Common.php next >
Encoding:
PHP Script  |  2008-07-02  |  4.6 KB  |  121 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Marshall Roch <marshall@exclupen.com>                        |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.1.1.1 2003/09/13 15:03:53 mroch Exp $
  20.  
  21. /**
  22.  * @author Marshall Roch <marshall@exclupen.com>
  23.  * @copyright Copyright 2003 Marshall Roch
  24.  * @license http://www.php.net/license/2_02.txt PHP License 2.0
  25.  * @package Services_ExchangeRates
  26.  */
  27.  
  28. /**
  29.  * Cache_Lite is needed to cache the feeds
  30.  */
  31. require_once 'Cache/Lite.php';
  32.  
  33. /**
  34.  * Common functions for data retrieval
  35.  *
  36.  * Provides base functions to retrieve and cache data feeds in different
  37.  * formats.
  38.  *
  39.  * @package Services_ExchangeRates
  40.  */
  41. class Services_ExchangeRates_Common {
  42.  
  43.    /**
  44.     * Retrieves data from cache, if it's there.  If it is, but it's expired, 
  45.     * it performs a conditional GET to see if the data is updated.  If it 
  46.     * isn't, it down updates the modification time of the cache file and 
  47.     * returns the data.  If the cache is not there, or the remote file has been
  48.     * modified, it is downloaded and cached.
  49.     *
  50.     * @param string URL of remote file to retrieve
  51.     * @param int Length of time to cache file locally before asking the server
  52.     *            if it changed.
  53.     * @return string File contents
  54.     */
  55.     function retrieveFile($url, $cacheLength, $cacheDir) {
  56.         
  57.         $cacheID = md5($url);
  58.                
  59.         $cache = new Cache_Lite(array("cacheDir" => $cacheDir,
  60.                                       "lifeTime" => $cacheLength));
  61.                                              
  62.         if ($data = $cache->get($cacheID)) {
  63.             return $data;
  64.         } else {
  65.             // we need to perform a request, so include HTTP_Request
  66.             include_once 'HTTP/Request.php';
  67.             
  68.             // HTTP_Request has moronic redirect "handling", turn that off (Alexey Borzov)
  69.             $req =& new HTTP_Request($url, array('allowRedirects' => false));
  70.             
  71.             // if $cache->get($cacheID) found the file, but it was expired, 
  72.             // $cache->_file will exist 
  73.             if (isset($cache->_file) && file_exists($cache->_file)) {
  74.                 $req->addHeader('If-Modified-Since', gmdate("D, d M Y H:i:s", filemtime($cache->_file)) ." GMT");
  75.             }
  76.             
  77.             $req->sendRequest();
  78.             
  79.             if (!($req->getResponseCode() == 304)) {
  80.                 // data is changed, so save it to cache
  81.                 $data = $req->getResponseBody();
  82.                 $cache->save($data, $cacheID);
  83.                 return $data;
  84.             } else {
  85.                 // retrieve the data, since the first time we did this failed
  86.                 if ($data = $cache->get($cacheID, 'default', true)) {
  87.                     return $data;
  88.                 }
  89.             }
  90.         }
  91.         
  92.         Services_ExchangeRates::raiseError("Unable to retrieve file ${url} (unknown reason)", SERVICES_EXCHANGERATES_ERROR_RETRIEVAL_FAILED);
  93.         return false;
  94.         
  95.     }
  96.     
  97.    /**
  98.     * Downloads XML file or returns it from cache
  99.     *
  100.     * @param string URL of XML file
  101.     * @param int Length of time to cache
  102.     * @return object XML_Tree object
  103.     */
  104.     function retrieveXML($url, $cacheLength, $cacheDir) {
  105.         include_once 'XML/Tree.php';
  106.         
  107.         if ($data = $this->retrieveFile($url, $cacheLength, $cacheDir)) {
  108.         
  109.             $tree = new XML_Tree();
  110.             $root =& $tree->getTreeFromString($data);
  111.         
  112.             return $root;
  113.         }
  114.         
  115.         return false;
  116.     }
  117.  
  118. }
  119.  
  120. ?>
  121.