File indexing completed on 2024-12-22 05:36:51

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mail
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * @category   Zend
0026  * @package    Zend_Mail
0027  * @subpackage Storage
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 abstract class Zend_Mail_Storage_Abstract implements Countable, ArrayAccess, SeekableIterator
0032 {
0033     /**
0034      * class capabilities with default values
0035      * @var array
0036      */
0037     protected $_has = array('uniqueid'  => true,
0038                             'delete'    => false,
0039                             'create'    => false,
0040                             'top'       => false,
0041                             'fetchPart' => true,
0042                             'flags'     => false);
0043 
0044     /**
0045      * current iteration position
0046      * @var int
0047      */
0048     protected $_iterationPos = 0;
0049 
0050     /**
0051      * maximum iteration position (= message count)
0052      * @var null|int
0053      */
0054     protected $_iterationMax = null;
0055 
0056     /**
0057      * used message class, change it in an extened class to extend the returned message class
0058      * @var string
0059      */
0060     protected $_messageClass = 'Zend_Mail_Message';
0061 
0062     /**
0063      * Getter for has-properties. The standard has properties
0064      * are: hasFolder, hasUniqueid, hasDelete, hasCreate, hasTop
0065      *
0066      * The valid values for the has-properties are:
0067      *   - true if a feature is supported
0068      *   - false if a feature is not supported
0069      *   - null is it's not yet known or it can't be know if a feature is supported
0070      *
0071      * @param  string $var  property name
0072      * @return bool         supported or not
0073      * @throws Zend_Mail_Storage_Exception
0074      */
0075     public function __get($var)
0076     {
0077         if (strpos($var, 'has') === 0) {
0078             $var = strtolower(substr($var, 3));
0079             return isset($this->_has[$var]) ? $this->_has[$var] : null;
0080         }
0081 
0082         /**
0083          * @see Zend_Mail_Storage_Exception
0084          */
0085         // require_once 'Zend/Mail/Storage/Exception.php';
0086         throw new Zend_Mail_Storage_Exception($var . ' not found');
0087     }
0088 
0089 
0090     /**
0091      * Get a full list of features supported by the specific mail lib and the server
0092      *
0093      * @return array list of features as array(featurename => true|false[|null])
0094      */
0095     public function getCapabilities()
0096     {
0097         return $this->_has;
0098     }
0099 
0100 
0101     /**
0102      * Count messages messages in current box/folder
0103      *
0104      * @return int number of messages
0105      * @throws Zend_Mail_Storage_Exception
0106      */
0107     abstract public function countMessages();
0108 
0109 
0110     /**
0111      * Get a list of messages with number and size
0112      *
0113      * @param  int $id  number of message
0114      * @return int|array size of given message of list with all messages as array(num => size)
0115      */
0116     abstract public function getSize($id = 0);
0117 
0118 
0119     /**
0120      * Get a message with headers and body
0121      *
0122      * @param int $id number of message
0123      * @return Zend_Mail_Message
0124      */
0125     abstract public function getMessage($id);
0126 
0127 
0128     /**
0129      * Get raw header of message or part
0130      *
0131      * @param  int               $id       number of message
0132      * @param  null|array|string $part     path to part or null for messsage header
0133      * @param  int               $topLines include this many lines with header (after an empty line)
0134      * @return string raw header
0135      */
0136     abstract public function getRawHeader($id, $part = null, $topLines = 0);
0137 
0138     /**
0139      * Get raw content of message or part
0140      *
0141      * @param  int               $id   number of message
0142      * @param  null|array|string $part path to part or null for messsage content
0143      * @return string raw content
0144      */
0145     abstract public function getRawContent($id, $part = null);
0146 
0147     /**
0148      * Create instance with parameters
0149      *
0150      * @param  array $params mail reader specific parameters
0151      * @throws Zend_Mail_Storage_Exception
0152      */
0153     abstract public function __construct($params);
0154 
0155 
0156     /**
0157      * Destructor calls close() and therefore closes the resource.
0158      */
0159     public function __destruct()
0160     {
0161         $this->close();
0162     }
0163 
0164 
0165     /**
0166      * Close resource for mail lib. If you need to control, when the resource
0167      * is closed. Otherwise the destructor would call this.
0168      *
0169      * @return null
0170      */
0171     abstract public function close();
0172 
0173 
0174     /**
0175      * Keep the resource alive.
0176      *
0177      * @return null
0178      */
0179     abstract public function noop();
0180 
0181     /**
0182      * delete a message from current box/folder
0183      *
0184      * @return null
0185      */
0186     abstract public function removeMessage($id);
0187 
0188     /**
0189      * get unique id for one or all messages
0190      *
0191      * if storage does not support unique ids it's the same as the message number
0192      *
0193      * @param int|null $id message number
0194      * @return array|string message number for given message or all messages as array
0195      * @throws Zend_Mail_Storage_Exception
0196      */
0197     abstract public function getUniqueId($id = null);
0198 
0199     /**
0200      * get a message number from a unique id
0201      *
0202      * I.e. if you have a webmailer that supports deleting messages you should use unique ids
0203      * as parameter and use this method to translate it to message number right before calling removeMessage()
0204      *
0205      * @param string $id unique id
0206      * @return int message number
0207      * @throws Zend_Mail_Storage_Exception
0208      */
0209     abstract public function getNumberByUniqueId($id);
0210 
0211     // interface implementations follows
0212 
0213     /**
0214      * Countable::count()
0215      *
0216      * @return   int
0217      */
0218      public function count()
0219      {
0220         return $this->countMessages();
0221      }
0222 
0223 
0224      /**
0225       * ArrayAccess::offsetExists()
0226       *
0227       * @param    int     $id
0228       * @return   boolean
0229       */
0230      public function offsetExists($id)
0231      {
0232         try {
0233             if ($this->getMessage($id)) {
0234                 return true;
0235             }
0236         } catch(Zend_Mail_Storage_Exception $e) {}
0237 
0238         return false;
0239      }
0240 
0241 
0242      /**
0243       * ArrayAccess::offsetGet()
0244       *
0245       * @param    int $id
0246       * @return   Zend_Mail_Message message object
0247       */
0248      public function offsetGet($id)
0249      {
0250         return $this->getMessage($id);
0251      }
0252 
0253 
0254      /**
0255       * ArrayAccess::offsetSet()
0256       *
0257       * @param    id     $id
0258       * @param    mixed  $value
0259       * @throws   Zend_Mail_Storage_Exception
0260       * @return   void
0261       */
0262      public function offsetSet($id, $value)
0263      {
0264         /**
0265          * @see Zend_Mail_Storage_Exception
0266          */
0267         // require_once 'Zend/Mail/Storage/Exception.php';
0268         throw new Zend_Mail_Storage_Exception('cannot write mail messages via array access');
0269      }
0270 
0271 
0272      /**
0273       * ArrayAccess::offsetUnset()
0274       *
0275       * @param    int   $id
0276       * @return   boolean success
0277       */
0278      public function offsetUnset($id)
0279      {
0280         return $this->removeMessage($id);
0281      }
0282 
0283 
0284      /**
0285       * Iterator::rewind()
0286       *
0287       * Rewind always gets the new count from the storage. Thus if you use
0288       * the interfaces and your scripts take long you should use reset()
0289       * from time to time.
0290       *
0291       * @return   void
0292       */
0293      public function rewind()
0294      {
0295         $this->_iterationMax = $this->countMessages();
0296         $this->_iterationPos = 1;
0297      }
0298 
0299 
0300      /**
0301       * Iterator::current()
0302       *
0303       * @return   Zend_Mail_Message current message
0304       */
0305      public function current()
0306      {
0307         return $this->getMessage($this->_iterationPos);
0308      }
0309 
0310 
0311      /**
0312       * Iterator::key()
0313       *
0314       * @return   int id of current position
0315       */
0316      public function key()
0317      {
0318         return $this->_iterationPos;
0319      }
0320 
0321 
0322      /**
0323       * Iterator::next()
0324       *
0325       * @return   void
0326       */
0327      public function next()
0328      {
0329         ++$this->_iterationPos;
0330      }
0331 
0332 
0333      /**
0334       * Iterator::valid()
0335       *
0336       * @return   boolean
0337       */
0338      public function valid()
0339      {
0340         if ($this->_iterationMax === null) {
0341           $this->_iterationMax = $this->countMessages();
0342         }
0343         return $this->_iterationPos && $this->_iterationPos <= $this->_iterationMax;
0344      }
0345 
0346 
0347      /**
0348       * SeekableIterator::seek()
0349       *
0350       * @param  int $pos
0351       * @return void
0352       * @throws OutOfBoundsException
0353       */
0354      public function seek($pos)
0355      {
0356         if ($this->_iterationMax === null) {
0357           $this->_iterationMax = $this->countMessages();
0358         }
0359 
0360         if ($pos > $this->_iterationMax) {
0361             throw new OutOfBoundsException('this position does not exist');
0362         }
0363         $this->_iterationPos = $pos;
0364      }
0365 
0366 }