DiskKeyCache.php 9.0KB

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