XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
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 
11 //@require 'Swift/KeyCache.php';
12 //@require 'Swift/KeyCacheInputStream.php';
13 //@require 'Swift/InputByteStream.php';
14 //@require 'Swift/OutputByteStrean.php';
15 //@require 'Swift/SwiftException.php';
16 //@require 'Swift/IoException.php';
17 
25 {
26 
28  const POSITION_START = 0;
29 
31  const POSITION_END = 1;
32 
38  private $_stream;
39 
45  private $_path;
46 
52  private $_keys = array();
53 
59  private $_quotes = false;
60 
68  {
69  $this->_stream = $stream;
70  $this->_path = $path;
71  $this->_quotes = get_magic_quotes_runtime();
72  }
73 
83  public function setString($nsKey, $itemKey, $string, $mode)
84  {
85  $this->_prepareCache($nsKey);
86  switch ($mode)
87  {
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  }
103 
113  public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os,
114  $mode)
115  {
116  $this->_prepareCache($nsKey);
117  switch ($mode)
118  {
119  case self::MODE_WRITE:
120  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
121  break;
122  case self::MODE_APPEND:
123  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
124  break;
125  default:
126  throw new Swift_SwiftException(
127  'Invalid mode [' . $mode . '] used to set nsKey='.
128  $nsKey . ', itemKey=' . $itemKey
129  );
130  break;
131  }
132  while (false !== $bytes = $os->read(8192))
133  {
134  fwrite($fp, $bytes);
135  }
136  }
137 
145  public function getInputByteStream($nsKey, $itemKey,
146  Swift_InputByteStream $writeThrough = null)
147  {
148  $is = clone $this->_stream;
149  $is->setKeyCache($this);
150  $is->setNsKey($nsKey);
151  $is->setItemKey($itemKey);
152  if (isset($writeThrough))
153  {
154  $is->setWriteThroughStream($writeThrough);
155  }
156  return $is;
157  }
158 
166  public function getString($nsKey, $itemKey)
167  {
168  $this->_prepareCache($nsKey);
169  if ($this->hasKey($nsKey, $itemKey))
170  {
171  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
172  if ($this->_quotes)
173  {
174  set_magic_quotes_runtime(0);
175  }
176  $str = '';
177  while (!feof($fp) && false !== $bytes = fread($fp, 8192))
178  {
179  $str .= $bytes;
180  }
181  if ($this->_quotes)
182  {
183  set_magic_quotes_runtime(1);
184  }
185  return $str;
186  }
187  }
188 
195  public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
196  {
197  if ($this->hasKey($nsKey, $itemKey))
198  {
199  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
200  if ($this->_quotes)
201  {
202  set_magic_quotes_runtime(0);
203  }
204  while (!feof($fp) && false !== $bytes = fread($fp, 8192))
205  {
206  $is->write($bytes);
207  }
208  if ($this->_quotes)
209  {
210  set_magic_quotes_runtime(1);
211  }
212  }
213  }
214 
221  public function hasKey($nsKey, $itemKey)
222  {
223  return is_file($this->_path . '/' . $nsKey . '/' . $itemKey);
224  }
225 
231  public function clearKey($nsKey, $itemKey)
232  {
233  if ($this->hasKey($nsKey, $itemKey))
234  {
235  $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
236  fclose($fp);
237  unlink($this->_path . '/' . $nsKey . '/' . $itemKey);
238  }
239  unset($this->_keys[$nsKey][$itemKey]);
240  }
241 
246  public function clearAll($nsKey)
247  {
248  if (array_key_exists($nsKey, $this->_keys))
249  {
250  foreach ($this->_keys[$nsKey] as $itemKey=>$null)
251  {
252  $this->clearKey($nsKey, $itemKey);
253  }
254  rmdir($this->_path . '/' . $nsKey);
255  unset($this->_keys[$nsKey]);
256  }
257  }
258 
259  // -- Private methods
260 
266  private function _prepareCache($nsKey)
267  {
268  $cacheDir = $this->_path . '/' . $nsKey;
269  if (!is_dir($cacheDir))
270  {
271  if (!mkdir($cacheDir))
272  {
273  throw new Swift_IoException('Failed to create cache directory ' . $cacheDir);
274  }
275  $this->_keys[$nsKey] = array();
276  }
277  }
278 
287  private function _getHandle($nsKey, $itemKey, $position)
288  {
289  if (!isset($this->_keys[$nsKey]) || !array_key_exists($itemKey, $this->_keys[$nsKey]))
290  {
291  $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, 'w+b');
292  $this->_keys[$nsKey][$itemKey] = $fp;
293  }
294  if (self::POSITION_START == $position)
295  {
296  fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
297  }
298  else
299  {
300  fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
301  }
302  return $this->_keys[$nsKey][$itemKey];
303  }
304 
308  public function __destruct()
309  {
310  foreach ($this->_keys as $nsKey=>$null)
311  {
312  $this->clearAll($nsKey);
313  }
314  }
315 
316 }