Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

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