DiskKeyCache.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * A KeyCache which streams to and from disk.
  11. * @package Swift
  12. * @subpackage KeyCache
  13. * @author Chris Corbyn
  14. */
  15. class Swift_KeyCache_DiskKeyCache implements Swift_KeyCache
  16. {
  17. /** Signal to place pointer at start of file */
  18. const POSITION_START = 0;
  19. /** Signal to place pointer at end of file */
  20. const POSITION_END = 1;
  21. /** Signal to leave pointer in whatever position it currently is */
  22. const POSITION_CURRENT = 2;
  23. /**
  24. * An InputStream for cloning.
  25. * @var Swift_KeyCache_KeyCacheInputStream
  26. * @access private
  27. */
  28. private $_stream;
  29. /**
  30. * A path to write to.
  31. * @var string
  32. * @access private
  33. */
  34. private $_path;
  35. /**
  36. * Stored keys.
  37. * @var array
  38. * @access private
  39. */
  40. private $_keys = array();
  41. /**
  42. * Will be true if magic_quotes_runtime is turned on.
  43. * @var boolean
  44. * @access private
  45. */
  46. private $_quotes = false;
  47. /**
  48. * Create a new DiskKeyCache with the given $stream for cloning to make
  49. * InputByteStreams, and the given $path to save to.
  50. * @param Swift_KeyCache_KeyCacheInputStream $stream
  51. * @param string $path to save to
  52. */
  53. public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path)
  54. {
  55. $this->_stream = $stream;
  56. $this->_path = $path;
  57. if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1)
  58. {
  59. $this->_quotes = true;
  60. }
  61. }
  62. /**
  63. * Set a string into the cache under $itemKey for the namespace $nsKey.
  64. * @param string $nsKey
  65. * @param string $itemKey
  66. * @param string $string
  67. * @param int $mode
  68. * @throws Swift_IoException
  69. * @see MODE_WRITE, MODE_APPEND
  70. */
  71. public function setString($nsKey, $itemKey, $string, $mode)
  72. {
  73. $this->_prepareCache($nsKey);
  74. switch ($mode)
  75. {
  76. case self::MODE_WRITE:
  77. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  78. break;
  79. case self::MODE_APPEND:
  80. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
  81. break;
  82. default:
  83. throw new Swift_SwiftException(
  84. 'Invalid mode [' . $mode . '] used to set nsKey='.
  85. $nsKey . ', itemKey=' . $itemKey
  86. );
  87. break;
  88. }
  89. fwrite($fp, $string);
  90. $this->_freeHandle($nsKey, $itemKey);
  91. }
  92. /**
  93. * Set a ByteStream into the cache under $itemKey for the namespace $nsKey.
  94. * @param string $nsKey
  95. * @param string $itemKey
  96. * @param Swift_OutputByteStream $os
  97. * @param int $mode
  98. * @see MODE_WRITE, MODE_APPEND
  99. * @throws Swift_IoException
  100. */
  101. public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os,
  102. $mode)
  103. {
  104. $this->_prepareCache($nsKey);
  105. switch ($mode)
  106. {
  107. case self::MODE_WRITE:
  108. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  109. break;
  110. case self::MODE_APPEND:
  111. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
  112. break;
  113. default:
  114. throw new Swift_SwiftException(
  115. 'Invalid mode [' . $mode . '] used to set nsKey='.
  116. $nsKey . ', itemKey=' . $itemKey
  117. );
  118. break;
  119. }
  120. while (false !== $bytes = $os->read(8192))
  121. {
  122. fwrite($fp, $bytes);
  123. }
  124. $this->_freeHandle($nsKey, $itemKey);
  125. }
  126. /**
  127. * Provides a ByteStream which when written to, writes data to $itemKey.
  128. * NOTE: The stream will always write in append mode.
  129. * @param string $nsKey
  130. * @param string $itemKey
  131. * @return Swift_InputByteStream
  132. */
  133. public function getInputByteStream($nsKey, $itemKey,
  134. Swift_InputByteStream $writeThrough = null)
  135. {
  136. $is = clone $this->_stream;
  137. $is->setKeyCache($this);
  138. $is->setNsKey($nsKey);
  139. $is->setItemKey($itemKey);
  140. if (isset($writeThrough))
  141. {
  142. $is->setWriteThroughStream($writeThrough);
  143. }
  144. return $is;
  145. }
  146. /**
  147. * Get data back out of the cache as a string.
  148. * @param string $nsKey
  149. * @param string $itemKey
  150. * @return string
  151. * @throws Swift_IoException
  152. */
  153. public function getString($nsKey, $itemKey)
  154. {
  155. $this->_prepareCache($nsKey);
  156. if ($this->hasKey($nsKey, $itemKey))
  157. {
  158. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  159. if ($this->_quotes)
  160. {
  161. ini_set('magic_quotes_runtime', 0);
  162. }
  163. $str = '';
  164. while (!feof($fp) && false !== $bytes = fread($fp, 8192))
  165. {
  166. $str .= $bytes;
  167. }
  168. if ($this->_quotes)
  169. {
  170. ini_set('magic_quotes_runtime', 1);
  171. }
  172. $this->_freeHandle($nsKey, $itemKey);
  173. return $str;
  174. }
  175. }
  176. /**
  177. * Get data back out of the cache as a ByteStream.
  178. * @param string $nsKey
  179. * @param string $itemKey
  180. * @param Swift_InputByteStream $is to write the data to
  181. */
  182. public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
  183. {
  184. if ($this->hasKey($nsKey, $itemKey))
  185. {
  186. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  187. if ($this->_quotes)
  188. {
  189. ini_set('magic_quotes_runtime', 0);
  190. }
  191. while (!feof($fp) && false !== $bytes = fread($fp, 8192))
  192. {
  193. $is->write($bytes);
  194. }
  195. if ($this->_quotes)
  196. {
  197. ini_set('magic_quotes_runtime', 1);
  198. }
  199. $this->_freeHandle($nsKey, $itemKey);
  200. }
  201. }
  202. /**
  203. * Check if the given $itemKey exists in the namespace $nsKey.
  204. * @param string $nsKey
  205. * @param string $itemKey
  206. * @return boolean
  207. */
  208. public function hasKey($nsKey, $itemKey)
  209. {
  210. return is_file($this->_path . '/' . $nsKey . '/' . $itemKey);
  211. }
  212. /**
  213. * Clear data for $itemKey in the namespace $nsKey if it exists.
  214. * @param string $nsKey
  215. * @param string $itemKey
  216. */
  217. public function clearKey($nsKey, $itemKey)
  218. {
  219. if ($this->hasKey($nsKey, $itemKey))
  220. {
  221. $this->_freeHandle($nsKey, $itemKey);
  222. unlink($this->_path . '/' . $nsKey . '/' . $itemKey);
  223. }
  224. }
  225. /**
  226. * Clear all data in the namespace $nsKey if it exists.
  227. * @param string $nsKey
  228. */
  229. public function clearAll($nsKey)
  230. {
  231. if (array_key_exists($nsKey, $this->_keys))
  232. {
  233. foreach ($this->_keys[$nsKey] as $itemKey=>$null)
  234. {
  235. $this->clearKey($nsKey, $itemKey);
  236. }
  237. if (is_dir($this->_path . '/' . $nsKey))
  238. {
  239. rmdir($this->_path . '/' . $nsKey);
  240. }
  241. unset($this->_keys[$nsKey]);
  242. }
  243. }
  244. // -- Private methods
  245. /**
  246. * Initialize the namespace of $nsKey if needed.
  247. * @param string $nsKey
  248. * @access private
  249. */
  250. private function _prepareCache($nsKey)
  251. {
  252. $cacheDir = $this->_path . '/' . $nsKey;
  253. if (!is_dir($cacheDir))
  254. {
  255. if (!mkdir($cacheDir))
  256. {
  257. throw new Swift_IoException('Failed to create cache directory ' . $cacheDir);
  258. }
  259. $this->_keys[$nsKey] = array();
  260. }
  261. }
  262. /**
  263. * Get a file handle on the cache item.
  264. * @param string $nsKey
  265. * @param string $itemKey
  266. * @param int $position
  267. * @return resource
  268. * @access private
  269. */
  270. private function _getHandle($nsKey, $itemKey, $position)
  271. {
  272. if (!isset($this->_keys[$nsKey][$itemKey]))
  273. {
  274. $openMode = $this->hasKey($nsKey, $itemKey)
  275. ? 'r+b'
  276. : 'w+b'
  277. ;
  278. $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, $openMode);
  279. $this->_keys[$nsKey][$itemKey] = $fp;
  280. }
  281. if (self::POSITION_START == $position)
  282. {
  283. fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
  284. }
  285. elseif (self::POSITION_END == $position)
  286. {
  287. fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
  288. }
  289. return $this->_keys[$nsKey][$itemKey];
  290. }
  291. private function _freeHandle($nsKey, $itemKey)
  292. {
  293. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_CURRENT);
  294. fclose($fp);
  295. $this->_keys[$nsKey][$itemKey] = null;
  296. }
  297. /**
  298. * Destructor.
  299. */
  300. public function __destruct()
  301. {
  302. foreach ($this->_keys as $nsKey=>$null)
  303. {
  304. $this->clearAll($nsKey);
  305. }
  306. }
  307. }