[ Index ] |
|
Code source de Joomla 1.0.13 |
1 <?php 2 /** 3 * PHP Text Cache is a simple caching class for for saving/retrieving local copies of url data 4 * @package php_text_cache 5 * @version 0.3-pre 6 * @copyright (C) 2004 John Heinstein. All rights reserved 7 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 8 * @author John Heinstein <johnkarl@nbnet.nb.ca> 9 * @link http://www.engageinteractive.com/php_text_cache/ PHP Text Cache Home Page 10 * PHP Text Cache is Free Software 11 **/ 12 13 if (!defined('PHP_TEXT_CACHE_INCLUDE_PATH')) { 14 define('PHP_TEXT_CACHE_INCLUDE_PATH', (dirname(__FILE__) . "/")); 15 } 16 17 require_once (PHP_TEXT_CACHE_INCLUDE_PATH . 'php_http_connector.php'); 18 19 /** 20 * A simple caching class for saving/retrieving local copies of url data 21 * 22 * @package php_text_cache 23 * @author John Heinstein <johnkarl@nbnet.nb.ca> 24 */ 25 class php_text_cache extends php_http_connector { 26 /** @var string The directory in which cached files are stored */ 27 var $cacheDir; 28 /** @var int The amount time of time to wait before a cached file should be updated */ 29 var $cacheTime; 30 /** @var boolean True if an HTTP client should be used to establish the connection */ 31 var $doUseHTTPClient; 32 /** @var int Time in seconds to disconnect after attempting an http connection */ 33 var $httpTimeout; 34 35 /** 36 * Constructor 37 * @param string Directory in which to store the cache files 38 * @param int Expiry time for cache file (-1 signifies no expiry limit) 39 * @param int Time in seconds to disconnect after attempting an http connection 40 */ 41 function php_text_cache($cacheDir = './', $cacheTime = -1, $timeout = 0) { 42 $this->cacheDir = $cacheDir; 43 $this->cacheTime = $cacheTime; 44 $this->timeout = $timeout; 45 } //php_text_cache 46 47 /** 48 * Specifies the default timeout value for connecting to a host 49 * @param int The number of seconds to timeout when attempting to connect to a server 50 */ 51 function setTimeout($timeout) { 52 $this->timeout = $timeout; 53 } //setTimeout 54 55 /** 56 * Gets data from an url, or its cache file 57 * 58 * @param string The url of the data 59 * @return string The data at the specified url 60 */ 61 function getData($url) { 62 $cacheFile = $this->getCacheFileName($url); 63 64 if (is_file($cacheFile)) { 65 $fileStats = stat($cacheFile); 66 $lastChangeTime = $fileStats[9]; //mtime 67 $currTime = time(); 68 69 if (($this->cacheTime != -1) && ($currTime - $lastChangeTime) > $this->cacheTime) { //get data from url 70 return $this->fromURL($url, $cacheFile); 71 } 72 else { //get data from file 73 return $this->fromCache($cacheFile); 74 } 75 } 76 else { 77 return $this->fromURL($url, $cacheFile); 78 } 79 } //getData 80 81 /** 82 * Given an url, returns the path to the cache file 83 * 84 * Uses an md5 hash of the url. This can be 85 * overridden if a different approach is required 86 * 87 * @param string The url of the data 88 * @return string The cache file name 89 */ 90 function getCacheFileName($url) { 91 return ($this->cacheDir . md5($url)); 92 } //getCacheFileName 93 94 /** 95 * Establishes a connection, given an url 96 * @param string The url of the data 97 */ 98 function establishConnection($url) { 99 require_once (PHP_TEXT_CACHE_INCLUDE_PATH . 'php_http_client_generic.php'); 100 101 $host = php_http_connection::formatHost($url); 102 $host = substr($host, 0, strpos($host, '/')); 103 104 $this->setConnection($host, '/', 80, $this->timeout); 105 } //establishConnection 106 107 /** 108 * Specifies whether an HTTP client should be used to establish a connection 109 * @param boolean True if an HTTP client is to be used to establish the connection 110 */ 111 function useHTTPClient($truthVal) { 112 // fixes bug identified here: sarahk.pcpropertymanager.com/blog/using-domit-rss/225/ 113 //$this->doUseHTTPClient = truthVal; 114 $this->doUseHTTPClient = $truthVal; 115 } //useHTTPClient 116 117 /** 118 * Gets data from an url and caches a copy of the data 119 * @param string The url for the data 120 * @param string The cache file path 121 * @return string The contents of the url 122 */ 123 function fromURL($url, $cacheFile) { 124 $fileContents = ''; 125 126 if ($this->httpConnection != null) { 127 $response =& $this->httpConnection->get($url); 128 129 if ($response != null) { 130 $fileContents = $response->getResponse(); 131 } 132 } 133 else if ($this->doUseHTTPClient) { 134 $this->establishConnection($url); 135 $response =& $this->httpConnection->get($url); 136 137 if ($response != null) { 138 $fileContents = $response->getResponse(); 139 } 140 } 141 else { 142 $fileContents = $this->fromFile($url); 143 } 144 145 //if file is empty, might need to establish an 146 //http connection to get the data 147 if (($fileContents == '') && !$this->doUseHTTPClient) { 148 $this->establishConnection($url); 149 $response =& $this->httpConnection->get($url); 150 151 if ($response != null) { 152 $fileContents = $response->getResponse(); 153 } 154 } 155 156 if ($fileContents != '') { 157 require_once (PHP_TEXT_CACHE_INCLUDE_PATH . 'php_file_utilities.php'); 158 php_file_utilities::putDataToFile($cacheFile, $fileContents, 'w'); 159 } 160 161 return $fileContents; 162 } //fromURL 163 164 /** 165 * Get text from cache file 166 * @param string The file path 167 * @return string The text contained in the file, or an empty string 168 */ 169 function fromCache($cacheFile) { 170 return $this->fromFile($cacheFile); 171 } //fromCache 172 173 /** 174 * Get text from an url or file 175 * @param string The url or file path 176 * @return string The text contained in the url or file, or an empty string 177 */ 178 function fromFile($filename) { 179 if (function_exists('file_get_contents')) { 180 return @file_get_contents($filename); 181 } 182 else { 183 require_once (PHP_TEXT_CACHE_INCLUDE_PATH . 'php_file_utilities.php'); 184 $fileContents =& php_file_utilities::getDataFromFile($filename, 'r'); 185 return $fileContents; 186 } 187 188 return ''; 189 } //fromFile 190 } //php_text_cache 191 192 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 14:43:32 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |