TYPO3  7.6
DiskKeyCache.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
19  const POSITION_START = 0;
20 
22  const POSITION_END = 1;
23 
25  const POSITION_CURRENT = 2;
26 
32  private $_stream;
33 
39  private $_path;
40 
46  private $_keys = array();
47 
53  private $_quotes = false;
54 
62  public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path)
63  {
64  $this->_stream = $stream;
65  $this->_path = $path;
66 
67  if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1) {
68  $this->_quotes = true;
69  }
70  }
71 
84  public function setString($nsKey, $itemKey, $string, $mode)
85  {
86  $this->_prepareCache($nsKey);
87  switch ($mode) {
88  case self::MODE_WRITE:
89  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
90  break;
91  case self::MODE_APPEND:
92  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
93  break;
94  default:
95  throw new Swift_SwiftException(
96  'Invalid mode ['.$mode.'] used to set nsKey='.
97  $nsKey.', itemKey='.$itemKey
98  );
99  break;
100  }
101  fwrite($fp, $string);
102  $this->_freeHandle($nsKey, $itemKey);
103  }
104 
117  public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
118  {
119  $this->_prepareCache($nsKey);
120  switch ($mode) {
121  case self::MODE_WRITE:
122  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
123  break;
124  case self::MODE_APPEND:
125  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
126  break;
127  default:
128  throw new Swift_SwiftException(
129  'Invalid mode ['.$mode.'] used to set nsKey='.
130  $nsKey.', itemKey='.$itemKey
131  );
132  break;
133  }
134  while (false !== $bytes = $os->read(8192)) {
135  fwrite($fp, $bytes);
136  }
137  $this->_freeHandle($nsKey, $itemKey);
138  }
139 
151  public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
152  {
153  $is = clone $this->_stream;
154  $is->setKeyCache($this);
155  $is->setNsKey($nsKey);
156  $is->setItemKey($itemKey);
157  if (isset($writeThrough)) {
158  $is->setWriteThroughStream($writeThrough);
159  }
160 
161  return $is;
162  }
163 
174  public function getString($nsKey, $itemKey)
175  {
176  $this->_prepareCache($nsKey);
177  if ($this->hasKey($nsKey, $itemKey)) {
178  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
179  if ($this->_quotes) {
180  ini_set('magic_quotes_runtime', 0);
181  }
182  $str = '';
183  while (!feof($fp) && false !== $bytes = fread($fp, 8192)) {
184  $str .= $bytes;
185  }
186  if ($this->_quotes) {
187  ini_set('magic_quotes_runtime', 1);
188  }
189  $this->_freeHandle($nsKey, $itemKey);
190 
191  return $str;
192  }
193  }
194 
202  public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
203  {
204  if ($this->hasKey($nsKey, $itemKey)) {
205  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
206  if ($this->_quotes) {
207  ini_set('magic_quotes_runtime', 0);
208  }
209  while (!feof($fp) && false !== $bytes = fread($fp, 8192)) {
210  $is->write($bytes);
211  }
212  if ($this->_quotes) {
213  ini_set('magic_quotes_runtime', 1);
214  }
215  $this->_freeHandle($nsKey, $itemKey);
216  }
217  }
218 
227  public function hasKey($nsKey, $itemKey)
228  {
229  return is_file($this->_path.'/'.$nsKey.'/'.$itemKey);
230  }
231 
238  public function clearKey($nsKey, $itemKey)
239  {
240  if ($this->hasKey($nsKey, $itemKey)) {
241  $this->_freeHandle($nsKey, $itemKey);
242  unlink($this->_path.'/'.$nsKey.'/'.$itemKey);
243  }
244  }
245 
251  public function clearAll($nsKey)
252  {
253  if (array_key_exists($nsKey, $this->_keys)) {
254  foreach ($this->_keys[$nsKey] as $itemKey => $null) {
255  $this->clearKey($nsKey, $itemKey);
256  }
257  if (is_dir($this->_path.'/'.$nsKey)) {
258  rmdir($this->_path.'/'.$nsKey);
259  }
260  unset($this->_keys[$nsKey]);
261  }
262  }
263 
269  private function _prepareCache($nsKey)
270  {
271  $cacheDir = $this->_path.'/'.$nsKey;
272  if (!is_dir($cacheDir)) {
273  if (!mkdir($cacheDir)) {
274  throw new Swift_IoException('Failed to create cache directory '.$cacheDir);
275  }
276  $this->_keys[$nsKey] = array();
277  }
278  }
279 
289  private function _getHandle($nsKey, $itemKey, $position)
290  {
291  if (!isset($this->_keys[$nsKey][$itemKey])) {
292  $openMode = $this->hasKey($nsKey, $itemKey)
293  ? 'r+b'
294  : 'w+b'
295  ;
296  $fp = fopen($this->_path.'/'.$nsKey.'/'.$itemKey, $openMode);
297  $this->_keys[$nsKey][$itemKey] = $fp;
298  }
299  if (self::POSITION_START == $position) {
300  fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
301  } elseif (self::POSITION_END == $position) {
302  fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
303  }
304 
305  return $this->_keys[$nsKey][$itemKey];
306  }
307 
308  private function _freeHandle($nsKey, $itemKey)
309  {
310  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_CURRENT);
311  fclose($fp);
312  $this->_keys[$nsKey][$itemKey] = null;
313  }
314 
318  public function __destruct()
319  {
320  foreach ($this->_keys as $nsKey => $null) {
321  $this->clearAll($nsKey);
322  }
323  }
324 }