PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/domit/php_text_cache.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 192 lines | 94 code | 25 blank | 73 comment | 25 complexity | 884f68b13a4a31208efd59f684453704 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. if (!defined('PHP_TEXT_CACHE_INCLUDE_PATH')) {
  13. define('PHP_TEXT_CACHE_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  14. }
  15. require_once(PHP_TEXT_CACHE_INCLUDE_PATH . 'php_http_connector.php');
  16. /**
  17. * A simple caching class for saving/retrieving local copies of url data
  18. *
  19. * @package php_text_cache
  20. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  21. */
  22. class php_text_cache extends php_http_connector {
  23. /** @var string The directory in which cached files are stored */
  24. var $cacheDir;
  25. /** @var int The amount time of time to wait before a cached file should be updated */
  26. var $cacheTime;
  27. /** @var boolean True if an HTTP client should be used to establish the connection */
  28. var $doUseHTTPClient;
  29. /** @var int Time in seconds to disconnect after attempting an http connection */
  30. var $httpTimeout;
  31. /**
  32. * Constructor
  33. * @param string Directory in which to store the cache files
  34. * @param int Expiry time for cache file (-1 signifies no expiry limit)
  35. * @param int Time in seconds to disconnect after attempting an http connection
  36. */
  37. function php_text_cache($cacheDir = './', $cacheTime = -1, $timeout = 0) {
  38. $this->cacheDir = $cacheDir;
  39. $this->cacheTime = $cacheTime;
  40. $this->timeout = $timeout;
  41. } //php_text_cache
  42. /**
  43. * Specifies the default timeout value for connecting to a host
  44. * @param int The number of seconds to timeout when attempting to connect to a server
  45. */
  46. function setTimeout($timeout) {
  47. $this->timeout = $timeout;
  48. } //setTimeout
  49. /**
  50. * Gets data from an url, or its cache file
  51. *
  52. * @param string The url of the data
  53. * @return string The data at the specified url
  54. */
  55. function getData($url) {
  56. $cacheFile = $this->getCacheFileName($url);
  57. if (is_file($cacheFile)) {
  58. $fileStats = stat($cacheFile);
  59. $lastChangeTime = $fileStats[9]; //mtime
  60. $currTime = time();
  61. if (($this->cacheTime != -1) && ($currTime - $lastChangeTime) > $this->cacheTime) { //get data from url
  62. return $this->fromURL($url, $cacheFile);
  63. }
  64. else { //get data from file
  65. return $this->fromCache($cacheFile);
  66. }
  67. }
  68. else {
  69. return $this->fromURL($url, $cacheFile);
  70. }
  71. } //getData
  72. /**
  73. * Given an url, returns the path to the cache file
  74. *
  75. * Uses an md5 hash of the url. This can be
  76. * overridden if a different approach is required
  77. *
  78. * @param string The url of the data
  79. * @return string The cache file name
  80. */
  81. function getCacheFileName($url) {
  82. return ($this->cacheDir . md5($url));
  83. } //getCacheFileName
  84. /**
  85. * Establishes a connection, given an url
  86. * @param string The url of the data
  87. */
  88. function establishConnection($url) {
  89. require_once(PHP_TEXT_CACHE_INCLUDE_PATH . 'php_http_client_generic.php');
  90. $host = php_http_connection::formatHost($url);
  91. $host = substr($host, 0, strpos($host, '/'));
  92. $this->setConnection($host, '/', 80, $this->timeout);
  93. } //establishConnection
  94. /**
  95. * Specifies whether an HTTP client should be used to establish a connection
  96. * @param boolean True if an HTTP client is to be used to establish the connection
  97. */
  98. function useHTTPClient($truthVal) {
  99. // fixes bug identified here: sarahk.pcpropertymanager.com/blog/using-domit-rss/225/
  100. //$this->doUseHTTPClient = truthVal;
  101. $this->doUseHTTPClient = $truthVal;
  102. } //useHTTPClient
  103. /**
  104. * Gets data from an url and caches a copy of the data
  105. * @param string The url for the data
  106. * @param string The cache file path
  107. * @return string The contents of the url
  108. */
  109. function fromURL($url, $cacheFile) {
  110. $fileContents = '';
  111. if ($this->httpConnection != null) {
  112. $response =& $this->httpConnection->get($url);
  113. if ($response != null) {
  114. $fileContents = $response->getResponse();
  115. }
  116. }
  117. else if ($this->doUseHTTPClient) {
  118. $this->establishConnection($url);
  119. $response =& $this->httpConnection->get($url);
  120. if ($response != null) {
  121. $fileContents = $response->getResponse();
  122. }
  123. }
  124. else {
  125. $fileContents = $this->fromFile($url);
  126. }
  127. //if file is empty, might need to establish an
  128. //http connection to get the data
  129. if (($fileContents == '') && !$this->doUseHTTPClient) {
  130. $this->establishConnection($url);
  131. $response =& $this->httpConnection->get($url);
  132. if ($response != null) {
  133. $fileContents = $response->getResponse();
  134. }
  135. }
  136. if ($fileContents != '') {
  137. require_once(PHP_TEXT_CACHE_INCLUDE_PATH . 'php_file_utilities.php');
  138. php_file_utilities::putDataToFile($cacheFile, $fileContents, 'w');
  139. }
  140. return $fileContents;
  141. } //fromURL
  142. /**
  143. * Get text from cache file
  144. * @param string The file path
  145. * @return string The text contained in the file, or an empty string
  146. */
  147. function fromCache($cacheFile) {
  148. return $this->fromFile($cacheFile);
  149. } //fromCache
  150. /**
  151. * Get text from an url or file
  152. * @param string The url or file path
  153. * @return string The text contained in the url or file, or an empty string
  154. */
  155. function fromFile($filename) {
  156. if (function_exists('file_get_contents')) {
  157. return @file_get_contents($filename);
  158. }
  159. else {
  160. require_once(PHP_TEXT_CACHE_INCLUDE_PATH . 'php_file_utilities.php');
  161. $fileContents =& php_file_utilities::getDataFromFile($filename, 'r');
  162. return $fileContents;
  163. }
  164. return '';
  165. } //fromFile
  166. } //php_text_cache
  167. ?>