/simpus/vendor/swiftmailer/swiftmailer/lib/classes/Swift/KeyCache/DiskKeyCache.php

https://gitlab.com/isdzulqor/Slis-Dev · PHP · 324 lines · 169 code · 25 blank · 130 comment · 26 complexity · cf4009256544caaa64b3467f6c289c56 MD5 · raw file

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