123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
-
-
-
-
- class Swift_FileSpool extends Swift_ConfigurableSpool
- {
-
- private $_path;
-
-
-
- private $_retryLimit=10;
-
-
-
- public function __construct($path)
- {
- $this->_path = $path;
-
- if (!file_exists($this->_path))
- {
- if (!mkdir($this->_path, 0777, true))
- {
- throw new Swift_IoException('Unable to create Path ['.$this->_path.']');
- }
- }
- }
-
-
-
- public function isStarted()
- {
- return true;
- }
-
-
-
- public function start()
- {
- }
-
-
-
- public function stop()
- {
- }
-
-
-
- public function setRetryLimit($limit)
- {
- $this->_retryLimit=$limit;
- }
-
-
-
- public function queueMessage(Swift_Mime_Message $message)
- {
- $ser = serialize($message);
- $fileName=$this->_path.'/'.$this->getRandomString(10);
- for ($i = 0; $i < $this->_retryLimit; ++$i)
- {
-
-
- $fp = @fopen($fileName.'.message', 'x');
- if (false !== $fp)
- {
- if (false === fwrite($fp, $ser))
- {
- return false;
- }
-
- return fclose($fp);
- }
- else
- {
-
-
- $fileName.=$this->getRandomString(1);
- }
- }
-
- throw new Swift_IoException('Unable to create a file for enqueuing Message');
- }
-
-
-
- public function recover($timeout=900)
- {
- foreach (new DirectoryIterator($this->_path) as $file)
- {
- $file = $file->getRealPath();
-
- if (substr($file, -16)=='.message.sending')
- {
- $lockedtime=filectime($file);
- if ((time()-$lockedtime)>$timeout)
- {
- rename($file, substr($file, 0, -8));
- }
- }
- }
- }
-
-
-
- public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
- {
- if (!$transport->isStarted())
- {
- $transport->start();
- }
-
- $failedRecipients = (array) $failedRecipients;
- $count = 0;
- $time = time();
- foreach (new DirectoryIterator($this->_path) as $file)
- {
- $file = $file->getRealPath();
-
- if (substr($file, -8) != '.message')
- {
- continue;
- }
-
-
- if (rename($file, $file.'.sending'))
- {
- $message = unserialize(file_get_contents($file.'.sending'));
-
- $count += $transport->send($message, $failedRecipients);
-
- unlink($file.'.sending');
- }
- else
- {
-
- continue;
- }
-
- if ($this->getMessageLimit() && $count >= $this->getMessageLimit())
- {
- break;
- }
-
- if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit())
- {
- break;
- }
- }
-
- return $count;
- }
-
-
-
- protected function getRandomString($count) {
-
- $base="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.";
- $ret='';
- $strlen=strlen($base);
- for ($i=0; $i<$count; ++$i)
- {
- $ret.=$base[((int)rand(0,$strlen-1))];
- }
- return $ret;
- }
- }
|