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