PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/include/nusoap/class.wsdlcache.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 251 lines | 107 code | 14 blank | 130 comment | 24 complexity | f8f80fa3fa56b0d0c3169cff60bfc3c2 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. r57813 - 2010-08-19 10:34:44 -0700 (Thu, 19 Aug 2010) - kjing - Author: John Mertic <jmertic@sugarcrm.com>
  5. Bug 39085 - When loading the opposite search panel via ajax on the ListViews, call the index action instead of the ListView action to avoid touching pre-MVC code by accident.
  6. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  7. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  8. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  9. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  10. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  11. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  12. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  13. r13782 - 2006-06-06 10:58:55 -0700 (Tue, 06 Jun 2006) - majed - changes entry point code
  14. r11115 - 2006-01-17 14:54:45 -0800 (Tue, 17 Jan 2006) - majed - add entry point validation
  15. r8846 - 2005-10-31 11:01:12 -0800 (Mon, 31 Oct 2005) - majed - new version of nusoap
  16. r5462 - 2005-05-25 13:50:11 -0700 (Wed, 25 May 2005) - majed - upgraded nusoap to .6.9
  17. r573 - 2004-09-04 13:03:32 -0700 (Sat, 04 Sep 2004) - sugarclint - undoing copyrights added in inadvertantly. --clint
  18. r546 - 2004-09-03 11:49:38 -0700 (Fri, 03 Sep 2004) - sugarmsi - removed echo count
  19. r354 - 2004-08-02 23:00:37 -0700 (Mon, 02 Aug 2004) - sugarjacob - Adding Soap
  20. */
  21. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  22. /*
  23. The NuSOAP project home is:
  24. http://sourceforge.net/projects/nusoap/
  25. The primary support for NuSOAP is the mailing list:
  26. nusoap-general@lists.sourceforge.net
  27. */
  28. /**
  29. * caches instances of the wsdl class
  30. *
  31. * @author Scott Nichol <snichol@users.sourceforge.net>
  32. * @author Ingo Fischer <ingo@apollon.de>
  33. * @access public
  34. */
  35. class nusoap_wsdlcache {
  36. /**
  37. * @var resource
  38. * @access private
  39. */
  40. var $fplock;
  41. /**
  42. * @var integer
  43. * @access private
  44. */
  45. var $cache_lifetime;
  46. /**
  47. * @var string
  48. * @access private
  49. */
  50. var $cache_dir;
  51. /**
  52. * @var string
  53. * @access public
  54. */
  55. var $debug_str = '';
  56. /**
  57. * constructor
  58. *
  59. * @param string $cache_dir directory for cache-files
  60. * @param integer $cache_lifetime lifetime for caching-files in seconds or 0 for unlimited
  61. * @access public
  62. */
  63. function nusoap_wsdlcache($cache_dir='.', $cache_lifetime=0) {
  64. $this->fplock = array();
  65. $this->cache_dir = $cache_dir != '' ? $cache_dir : '.';
  66. $this->cache_lifetime = $cache_lifetime;
  67. }
  68. /**
  69. * creates the filename used to cache a wsdl instance
  70. *
  71. * @param string $wsdl The URL of the wsdl instance
  72. * @return string The filename used to cache the instance
  73. * @access private
  74. */
  75. function createFilename($wsdl) {
  76. return $this->cache_dir.'/wsdlcache-' . md5($wsdl);
  77. }
  78. /**
  79. * adds debug data to the class level debug string
  80. *
  81. * @param string $string debug data
  82. * @access private
  83. */
  84. function debug($string){
  85. $this->debug_str .= get_class($this).": $string\n";
  86. }
  87. /**
  88. * gets a wsdl instance from the cache
  89. *
  90. * @param string $wsdl The URL of the wsdl instance
  91. * @return object wsdl The cached wsdl instance, null if the instance is not in the cache
  92. * @access public
  93. */
  94. function get($wsdl) {
  95. $filename = $this->createFilename($wsdl);
  96. if ($this->obtainMutex($filename, "r")) {
  97. // check for expired WSDL that must be removed from the cache
  98. if ($this->cache_lifetime > 0) {
  99. if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) {
  100. unlink($filename);
  101. $this->debug("Expired $wsdl ($filename) from cache");
  102. $this->releaseMutex($filename);
  103. return null;
  104. }
  105. }
  106. // see what there is to return
  107. if (!file_exists($filename)) {
  108. $this->debug("$wsdl ($filename) not in cache (1)");
  109. $this->releaseMutex($filename);
  110. return null;
  111. }
  112. $fp = @fopen($filename, "r");
  113. if ($fp) {
  114. $s = implode("", @file($filename));
  115. fclose($fp);
  116. $this->debug("Got $wsdl ($filename) from cache");
  117. } else {
  118. $s = null;
  119. $this->debug("$wsdl ($filename) not in cache (2)");
  120. }
  121. $this->releaseMutex($filename);
  122. return (!is_null($s)) ? unserialize($s) : null;
  123. } else {
  124. $this->debug("Unable to obtain mutex for $filename in get");
  125. }
  126. return null;
  127. }
  128. /**
  129. * obtains the local mutex
  130. *
  131. * @param string $filename The Filename of the Cache to lock
  132. * @param string $mode The open-mode ("r" or "w") or the file - affects lock-mode
  133. * @return boolean Lock successfully obtained ?!
  134. * @access private
  135. */
  136. function obtainMutex($filename, $mode) {
  137. if (isset($this->fplock[md5($filename)])) {
  138. $this->debug("Lock for $filename already exists");
  139. return false;
  140. }
  141. $this->fplock[md5($filename)] = fopen($filename.".lock", "w");
  142. if ($mode == "r") {
  143. return flock($this->fplock[md5($filename)], LOCK_SH);
  144. } else {
  145. return flock($this->fplock[md5($filename)], LOCK_EX);
  146. }
  147. }
  148. /**
  149. * adds a wsdl instance to the cache
  150. *
  151. * @param object wsdl $wsdl_instance The wsdl instance to add
  152. * @return boolean WSDL successfully cached
  153. * @access public
  154. */
  155. function put($wsdl_instance) {
  156. $filename = $this->createFilename($wsdl_instance->wsdl);
  157. $s = serialize($wsdl_instance);
  158. if ($this->obtainMutex($filename, "w")) {
  159. $fp = fopen($filename, "w");
  160. if (! $fp) {
  161. $this->debug("Cannot write $wsdl_instance->wsdl ($filename) in cache");
  162. $this->releaseMutex($filename);
  163. return false;
  164. }
  165. fputs($fp, $s);
  166. fclose($fp);
  167. $this->debug("Put $wsdl_instance->wsdl ($filename) in cache");
  168. $this->releaseMutex($filename);
  169. return true;
  170. } else {
  171. $this->debug("Unable to obtain mutex for $filename in put");
  172. }
  173. return false;
  174. }
  175. /**
  176. * releases the local mutex
  177. *
  178. * @param string $filename The Filename of the Cache to lock
  179. * @return boolean Lock successfully released
  180. * @access private
  181. */
  182. function releaseMutex($filename) {
  183. $ret = flock($this->fplock[md5($filename)], LOCK_UN);
  184. fclose($this->fplock[md5($filename)]);
  185. unset($this->fplock[md5($filename)]);
  186. if (! $ret) {
  187. $this->debug("Not able to release lock for $filename");
  188. }
  189. return $ret;
  190. }
  191. /**
  192. * removes a wsdl instance from the cache
  193. *
  194. * @param string $wsdl The URL of the wsdl instance
  195. * @return boolean Whether there was an instance to remove
  196. * @access public
  197. */
  198. function remove($wsdl) {
  199. $filename = $this->createFilename($wsdl);
  200. if (!file_exists($filename)) {
  201. $this->debug("$wsdl ($filename) not in cache to be removed");
  202. return false;
  203. }
  204. // ignore errors obtaining mutex
  205. $this->obtainMutex($filename, "w");
  206. $ret = unlink($filename);
  207. $this->debug("Removed ($ret) $wsdl ($filename) from cache");
  208. $this->releaseMutex($filename);
  209. return $ret;
  210. }
  211. }
  212. /**
  213. * For backward compatibility
  214. */
  215. class wsdlcache extends nusoap_wsdlcache {
  216. }
  217. ?>