class.wsdlcache.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. The NuSOAP project home is:
  4. http://sourceforge.net/projects/nusoap/
  5. The primary support for NuSOAP is the mailing list:
  6. nusoap-general@lists.sourceforge.net
  7. */
  8. /**
  9. * caches instances of the wsdl class
  10. *
  11. * @author Scott Nichol <snichol@users.sourceforge.net>
  12. * @author Ingo Fischer <ingo@apollon.de>
  13. * @version $Id: class.wsdlcache.php,v 1.7 2007/04/17 16:34:03 snichol Exp $
  14. * @access public
  15. */
  16. class nusoap_wsdlcache
  17. {
  18. /**
  19. * @var resource
  20. * @access private
  21. */
  22. var $fplock;
  23. /**
  24. * @var integer
  25. * @access private
  26. */
  27. var $cache_lifetime;
  28. /**
  29. * @var string
  30. * @access private
  31. */
  32. var $cache_dir;
  33. /**
  34. * @var string
  35. * @access public
  36. */
  37. var $debug_str = '';
  38. /**
  39. * constructor
  40. *
  41. * @param string $cache_dir directory for cache-files
  42. * @param integer $cache_lifetime lifetime for caching-files in seconds or 0 for unlimited
  43. * @access public
  44. */
  45. function __construct($cache_dir='.', $cache_lifetime=0) {
  46. $this->fplock = array();
  47. $this->cache_dir = $cache_dir != '' ? $cache_dir : '.';
  48. $this->cache_lifetime = $cache_lifetime;
  49. }
  50. /**
  51. * creates the filename used to cache a wsdl instance
  52. *
  53. * @param string $wsdl The URL of the wsdl instance
  54. * @return string The filename used to cache the instance
  55. * @access private
  56. */
  57. function createFilename($wsdl) {
  58. return $this->cache_dir.'/wsdlcache-' . md5($wsdl);
  59. }
  60. /**
  61. * adds debug data to the class level debug string
  62. *
  63. * @param string $string debug data
  64. * @access private
  65. */
  66. function debug($string){
  67. $this->debug_str .= get_class($this).": $string\n";
  68. }
  69. /**
  70. * gets a wsdl instance from the cache
  71. *
  72. * @param string $wsdl The URL of the wsdl instance
  73. * @return object wsdl The cached wsdl instance, null if the instance is not in the cache
  74. * @access public
  75. */
  76. function get($wsdl) {
  77. $filename = $this->createFilename($wsdl);
  78. if ($this->obtainMutex($filename, "r")) {
  79. // check for expired WSDL that must be removed from the cache
  80. if ($this->cache_lifetime > 0) {
  81. if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) {
  82. unlink($filename);
  83. $this->debug("Expired $wsdl ($filename) from cache");
  84. $this->releaseMutex($filename);
  85. return null;
  86. }
  87. }
  88. // see what there is to return
  89. if (!file_exists($filename)) {
  90. $this->debug("$wsdl ($filename) not in cache (1)");
  91. $this->releaseMutex($filename);
  92. return null;
  93. }
  94. $fp = @fopen($filename, "r");
  95. if ($fp) {
  96. $s = implode("", @file($filename));
  97. fclose($fp);
  98. $this->debug("Got $wsdl ($filename) from cache");
  99. } else {
  100. $s = null;
  101. $this->debug("$wsdl ($filename) not in cache (2)");
  102. }
  103. $this->releaseMutex($filename);
  104. return (!is_null($s)) ? unserialize($s) : null;
  105. } else {
  106. $this->debug("Unable to obtain mutex for $filename in get");
  107. }
  108. return null;
  109. }
  110. /**
  111. * obtains the local mutex
  112. *
  113. * @param string $filename The Filename of the Cache to lock
  114. * @param string $mode The open-mode ("r" or "w") or the file - affects lock-mode
  115. * @return boolean Lock successfully obtained ?!
  116. * @access private
  117. */
  118. function obtainMutex($filename, $mode) {
  119. if (isset($this->fplock[md5($filename)])) {
  120. $this->debug("Lock for $filename already exists");
  121. return false;
  122. }
  123. $this->fplock[md5($filename)] = fopen($filename.".lock", "w");
  124. if ($mode == "r") {
  125. return flock($this->fplock[md5($filename)], LOCK_SH);
  126. } else {
  127. return flock($this->fplock[md5($filename)], LOCK_EX);
  128. }
  129. }
  130. /**
  131. * adds a wsdl instance to the cache
  132. *
  133. * @param object wsdl $wsdl_instance The wsdl instance to add
  134. * @return boolean WSDL successfully cached
  135. * @access public
  136. */
  137. function put($wsdl_instance) {
  138. $filename = $this->createFilename($wsdl_instance->wsdl);
  139. $s = serialize($wsdl_instance);
  140. if ($this->obtainMutex($filename, "w")) {
  141. $fp = fopen($filename, "w");
  142. if (! $fp) {
  143. $this->debug("Cannot write $wsdl_instance->wsdl ($filename) in cache");
  144. $this->releaseMutex($filename);
  145. return false;
  146. }
  147. fputs($fp, $s);
  148. fclose($fp);
  149. $this->debug("Put $wsdl_instance->wsdl ($filename) in cache");
  150. $this->releaseMutex($filename);
  151. return true;
  152. } else {
  153. $this->debug("Unable to obtain mutex for $filename in put");
  154. }
  155. return false;
  156. }
  157. /**
  158. * releases the local mutex
  159. *
  160. * @param string $filename The Filename of the Cache to lock
  161. * @return boolean Lock successfully released
  162. * @access private
  163. */
  164. function releaseMutex($filename) {
  165. $ret = flock($this->fplock[md5($filename)], LOCK_UN);
  166. fclose($this->fplock[md5($filename)]);
  167. unset($this->fplock[md5($filename)]);
  168. if (! $ret) {
  169. $this->debug("Not able to release lock for $filename");
  170. }
  171. return $ret;
  172. }
  173. /**
  174. * removes a wsdl instance from the cache
  175. *
  176. * @param string $wsdl The URL of the wsdl instance
  177. * @return boolean Whether there was an instance to remove
  178. * @access public
  179. */
  180. function remove($wsdl) {
  181. $filename = $this->createFilename($wsdl);
  182. if (!file_exists($filename)) {
  183. $this->debug("$wsdl ($filename) not in cache to be removed");
  184. return false;
  185. }
  186. // ignore errors obtaining mutex
  187. $this->obtainMutex($filename, "w");
  188. $ret = unlink($filename);
  189. $this->debug("Removed ($ret) $wsdl ($filename) from cache");
  190. $this->releaseMutex($filename);
  191. return $ret;
  192. }
  193. }
  194. /**
  195. * For backward compatibility
  196. */
  197. class wsdlcache extends nusoap_wsdlcache {
  198. }
  199. ?>