DiskKeyCache.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $this->_quotes = ini_get('magic_quotes_runtime');
  58. }
  59. /**
  60. * Set a string into the cache under $itemKey for the namespace $nsKey.
  61. * @param string $nsKey
  62. * @param string $itemKey
  63. * @param string $string
  64. * @param int $mode
  65. * @throws Swift_IoException
  66. * @see MODE_WRITE, MODE_APPEND
  67. */
  68. public function setString($nsKey, $itemKey, $string, $mode)
  69. {
  70. $this->_prepareCache($nsKey);
  71. switch ($mode)
  72. {
  73. case self::MODE_WRITE:
  74. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  75. break;
  76. case self::MODE_APPEND:
  77. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
  78. break;
  79. default:
  80. throw new Swift_SwiftException(
  81. 'Invalid mode [' . $mode . '] used to set nsKey='.
  82. $nsKey . ', itemKey=' . $itemKey
  83. );
  84. break;
  85. }
  86. fwrite($fp, $string);
  87. $this->_freeHandle($nsKey, $itemKey);
  88. }
  89. /**
  90. * Set a ByteStream into the cache under $itemKey for the namespace $nsKey.
  91. * @param string $nsKey
  92. * @param string $itemKey
  93. * @param Swift_OutputByteStream $os
  94. * @param int $mode
  95. * @see MODE_WRITE, MODE_APPEND
  96. * @throws Swift_IoException
  97. */
  98. public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os,
  99. $mode)
  100. {
  101. $this->_prepareCache($nsKey);
  102. switch ($mode)
  103. {
  104. case self::MODE_WRITE:
  105. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  106. break;
  107. case self::MODE_APPEND:
  108. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
  109. break;
  110. default:
  111. throw new Swift_SwiftException(
  112. 'Invalid mode [' . $mode . '] used to set nsKey='.
  113. $nsKey . ', itemKey=' . $itemKey
  114. );
  115. break;
  116. }
  117. while (false !== $bytes = $os->read(8192))
  118. {
  119. fwrite($fp, $bytes);
  120. }
  121. $this->_freeHandle($nsKey, $itemKey);
  122. }
  123. /**
  124. * Provides a ByteStream which when written to, writes data to $itemKey.
  125. * NOTE: The stream will always write in append mode.
  126. * @param string $nsKey
  127. * @param string $itemKey
  128. * @return Swift_InputByteStream
  129. */
  130. public function getInputByteStream($nsKey, $itemKey,
  131. Swift_InputByteStream $writeThrough = null)
  132. {
  133. $is = clone $this->_stream;
  134. $is->setKeyCache($this);
  135. $is->setNsKey($nsKey);
  136. $is->setItemKey($itemKey);
  137. if (isset($writeThrough))
  138. {
  139. $is->setWriteThroughStream($writeThrough);
  140. }
  141. return $is;
  142. }
  143. /**
  144. * Get data back out of the cache as a string.
  145. * @param string $nsKey
  146. * @param string $itemKey
  147. * @return string
  148. * @throws Swift_IoException
  149. */
  150. public function getString($nsKey, $itemKey)
  151. {
  152. $this->_prepareCache($nsKey);
  153. if ($this->hasKey($nsKey, $itemKey))
  154. {
  155. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  156. if ($this->_quotes)
  157. {
  158. ini_set('magic_quotes_runtime', 0);
  159. }
  160. $str = '';
  161. while (!feof($fp) && false !== $bytes = fread($fp, 8192))
  162. {
  163. $str .= $bytes;
  164. }
  165. if ($this->_quotes)
  166. {
  167. ini_set('magic_quotes_runtime', 1);
  168. }
  169. $this->_freeHandle($nsKey, $itemKey);
  170. return $str;
  171. }
  172. }
  173. /**
  174. * Get data back out of the cache as a ByteStream.
  175. * @param string $nsKey
  176. * @param string $itemKey
  177. * @param Swift_InputByteStream $is to write the data to
  178. */
  179. public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
  180. {
  181. if ($this->hasKey($nsKey, $itemKey))
  182. {
  183. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
  184. if ($this->_quotes)
  185. {
  186. ini_set('magic_quotes_runtime', 0);
  187. }
  188. while (!feof($fp) && false !== $bytes = fread($fp, 8192))
  189. {
  190. $is->write($bytes);
  191. }
  192. if ($this->_quotes)
  193. {
  194. ini_set('magic_quotes_runtime', 1);
  195. }
  196. $this->_freeHandle($nsKey, $itemKey);
  197. }
  198. }
  199. /**
  200. * Check if the given $itemKey exists in the namespace $nsKey.
  201. * @param string $nsKey
  202. * @param string $itemKey
  203. * @return boolean
  204. */
  205. public function hasKey($nsKey, $itemKey)
  206. {
  207. return is_file($this->_path . '/' . $nsKey . '/' . $itemKey);
  208. }
  209. /**
  210. * Clear data for $itemKey in the namespace $nsKey if it exists.
  211. * @param string $nsKey
  212. * @param string $itemKey
  213. */
  214. public function clearKey($nsKey, $itemKey)
  215. {
  216. if ($this->hasKey($nsKey, $itemKey))
  217. {
  218. $this->_freeHandle($nsKey, $itemKey);
  219. unlink($this->_path . '/' . $nsKey . '/' . $itemKey);
  220. }
  221. }
  222. /**
  223. * Clear all data in the namespace $nsKey if it exists.
  224. * @param string $nsKey
  225. */
  226. public function clearAll($nsKey)
  227. {
  228. if (array_key_exists($nsKey, $this->_keys))
  229. {
  230. foreach ($this->_keys[$nsKey] as $itemKey=>$null)
  231. {
  232. $this->clearKey($nsKey, $itemKey);
  233. }
  234. rmdir($this->_path . '/' . $nsKey);
  235. unset($this->_keys[$nsKey]);
  236. }
  237. }
  238. // -- Private methods
  239. /**
  240. * Initialize the namespace of $nsKey if needed.
  241. * @param string $nsKey
  242. * @access private
  243. */
  244. private function _prepareCache($nsKey)
  245. {
  246. $cacheDir = $this->_path . '/' . $nsKey;
  247. if (!is_dir($cacheDir))
  248. {
  249. if (!mkdir($cacheDir))
  250. {
  251. throw new Swift_IoException('Failed to create cache directory ' . $cacheDir);
  252. }
  253. $this->_keys[$nsKey] = array();
  254. }
  255. }
  256. /**
  257. * Get a file handle on the cache item.
  258. * @param string $nsKey
  259. * @param string $itemKey
  260. * @param int $position
  261. * @return resource
  262. * @access private
  263. */
  264. private function _getHandle($nsKey, $itemKey, $position)
  265. {
  266. if (!isset($this->_keys[$nsKey][$itemKey]))
  267. {
  268. $openMode = $this->hasKey($nsKey, $itemKey)
  269. ? 'r+b'
  270. : 'w+b'
  271. ;
  272. $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, $openMode);
  273. $this->_keys[$nsKey][$itemKey] = $fp;
  274. }
  275. if (self::POSITION_START == $position)
  276. {
  277. fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
  278. }
  279. elseif (self::POSITION_END == $position)
  280. {
  281. fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
  282. }
  283. return $this->_keys[$nsKey][$itemKey];
  284. }
  285. private function _freeHandle($nsKey, $itemKey)
  286. {
  287. $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_CURRENT);
  288. fclose($fp);
  289. $this->_keys[$nsKey][$itemKey] = null;
  290. }
  291. /**
  292. * Destructor.
  293. */
  294. public function __destruct()
  295. {
  296. foreach ($this->_keys as $nsKey=>$null)
  297. {
  298. $this->clearAll($nsKey);
  299. }
  300. }
  301. }