DiskKeyCache.php 9.1KB

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