home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Mail / Queue / Container.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  8.3 KB  |  326 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: Mail :: Queue :: Container                                   |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Radek Maciaszek <wodzu@tonet.pl>                            |
  17. // |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Container.php,v 1.10 2007/01/20 11:24:17 quipo Exp $
  21.  
  22. /**
  23.  * File Container.php
  24.  *
  25.  * @package Mail_Queue
  26.  */
  27. require_once 'Mail/Queue/Body.php';
  28.  
  29. /**
  30.  * Mail_Queue_Container - base class for MTA queue.
  31.  * Define methods for all storage containers.
  32.  *
  33.  * @version  $Revision: 1.10 $
  34.  * @author   Radek Maciaszek <chief@php.net>
  35.  * @author   Lorenzo Alberton <l dot alberton at quipo dot it>
  36.  * @package  Mail_Queue
  37.  * @access   public
  38.  * @abstract
  39.  */
  40. class Mail_Queue_Container
  41. {
  42.     // {{{ class vars
  43.  
  44.     /**
  45.      * Array for mails in queue
  46.      *
  47.      * @var array
  48.      */
  49.     var $queue_data = array();
  50.  
  51.     /**
  52.      * Key for current mail in queue
  53.      *
  54.      * @var integer
  55.      * @access private
  56.      */
  57.     var $_current_item = 0;
  58.  
  59.     /**
  60.      * Key for last mail in queue
  61.      *
  62.      * @var integer
  63.      * @access private
  64.      */
  65.     var $_last_item = 0;
  66.  
  67.     /**
  68.      * Options
  69.      */
  70.     var $limit;
  71.     var $offset;
  72.     var $try;
  73.     var $force_preload;
  74.     var $buffer_size = 10; //number of mails in the queue
  75.  
  76.     /**
  77.      * Pear error mode (see PEAR doc)
  78.      *
  79.      * @var int $pearErrorMode
  80.      * @access private
  81.      */
  82.     var $pearErrorMode = PEAR_ERROR_RETURN;
  83.  
  84.     // }}}
  85.     // {{{ get()
  86.  
  87.     /**
  88.      * Get next mail from queue. When exclude first time preload all queue
  89.      *
  90.      * @return mixed  MailBody object on success else Mail_Queue_Error
  91.      * @access    public
  92.      */
  93.     function get()
  94.     {
  95.         if (PEAR::isError($err = $this->preload())) {
  96.             return $err;
  97.         }
  98.         if (empty($this->queue_data)) {
  99.             return false;
  100.         }
  101.         if (!isset($this->queue_data[$this->_current_item])) {
  102.             //unlikely...
  103.             return new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_INITIALIZE,
  104.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  105.                 'No item: '.$this->_current_item.' in queue!');
  106.         }
  107.  
  108.         $object = $this->queue_data[$this->_current_item];
  109.         unset($this->queue_data[$this->_current_item]);
  110.         $this->_current_item++;
  111.         return $object;
  112.     }
  113.  
  114.     // }}}
  115.     // {{{ skip()
  116.  
  117.     /**
  118.      * Remove the current (problematic) mail from the buffer, but don't delete
  119.      * it from the db: it might be a temporary issue.
  120.      */
  121.     function skip()
  122.     {
  123.         if (!empty($this->queue_data)) {
  124.             if (isset($this->queue_data[$this->_current_item])) {
  125.                 unset($this->queue_data[$this->_current_item]);
  126.                 $this->_current_item++;
  127.             }
  128.         }
  129.     }
  130.  
  131.     // }}}
  132.     // {{{ put()
  133.  
  134.     /**
  135.      * Put new mail in queue.
  136.      *
  137.      * Mail_Queue_Container::put()
  138.      *
  139.      * @param string $time_to_send  When mail have to be send
  140.      * @param integer $id_user  Sender id
  141.      * @param string $ip  Sender ip
  142.      * @param string $from  Sender e-mail
  143.      * @param string $to  Reciepient e-mail
  144.      * @param string $hdrs  Mail headers (in RFC)
  145.      * @param string $body  Mail body (in RFC)
  146.      * @return bool True on success
  147.      * @access public
  148.      **/
  149. /*
  150.     function put($time_to_send, $id_user, $ip, $from, $to, $hdrs, $body, $delete_after_send)
  151.     {
  152.         $this->_last_item = count($this->queue_data);
  153.         $this->queue_data[$this->_last_item] = new Mail_Queue_Body($id, date("d-m-y G:i:s"),
  154.                     $time_to_send, null, $id_user,
  155.                     $ip, $sender, $recipient, unserialize($headers),
  156.                     unserialize($body), $delete_after_send, 0);
  157.         return true;
  158.     }
  159. */
  160.     // }}}
  161.     // {{{ setOption()
  162.  
  163.     /**
  164.      * Set common option
  165.      *
  166.      * Mail_Queue_Container::setOption()
  167.      *
  168.      * @param integer  $limit  Optional - Number of mails loaded to queue
  169.      * @param integer  $offset Optional - You could also specify offset
  170.      * @param integer  $try  Optional - how many times should system try sent
  171.      *                       each mail
  172.      * @param boolean  $force_preload  Optional - FIXME
  173.      * @return void
  174.      *
  175.      * @access public
  176.      **/
  177.     function setOption($limit = MAILQUEUE_ALL, $offset = MAILQUEUE_START,
  178.                         $try = MAILQUEUE_TRY, $force_preload = false)
  179.     {
  180.         $this->limit = $limit;
  181.         $this->offset = $offset;
  182.         $this->try = $try;
  183.         $this->force_preload = $force_preload;
  184.     }
  185.  
  186.     // }}}
  187.     // {{{ countSend()
  188.  
  189.     /**
  190.      * Check how many times mail was sent.
  191.      *
  192.      * @param object   MailBody
  193.      * @return mixed  Integer or false if error.
  194.      * @access public
  195.      */
  196.     function countSend($mail)
  197.     {
  198.         return false;
  199.     }
  200.  
  201.     // }}}
  202.     // {{{ setAsSent()
  203.  
  204.     /**
  205.      * Set mail as already sent.
  206.      *
  207.      * @param object MailBody object
  208.      * @return bool
  209.      * @access public
  210.      */
  211.     function setAsSent($mail)
  212.     {
  213.         return false;
  214.     }
  215.  
  216.     // }}}
  217.     // {{{ getMailById()
  218.  
  219.     /**
  220.      * Return mail by id $id (bypass mail_queue)
  221.      *
  222.      * @param integer $id  Mail ID
  223.      * @return mixed  Mail object or false on error.
  224.      * @access public
  225.      */
  226.     function getMailById($id)
  227.     {
  228.         return false;
  229.     }
  230.  
  231.     // }}}
  232.     // {{{ deleteMail()
  233.  
  234.     /**
  235.      * Remove from queue mail with $id identifier.
  236.      *
  237.      * @param integer $id  Mail ID
  238.      * @return bool  True on success ale false.
  239.      * @access public
  240.      */
  241.     function deleteMail($id)
  242.     {
  243.         return false;
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ preload()
  248.  
  249.     /**
  250.      * Preload mail to queue.
  251.      * The buffer size can be set in the options.
  252.      *
  253.      * @return mixed  True on success else Mail_Queue_Error object.
  254.      * @access private
  255.      */
  256.     function preload()
  257.     {
  258.         if (!empty($this->queue_data)) {
  259.             return true;
  260.         }
  261.  
  262.         if (!$this->limit) {
  263.             return true;   //limit reached
  264.         }
  265.  
  266.         $bkp_limit = $this->limit;
  267.  
  268.         //set buffer size
  269.         if ($bkp_limit == MAILQUEUE_ALL) {
  270.             $this->limit = $this->buffer_size;
  271.         } else {
  272.             $this->limit = min($this->buffer_size, $this->limit);
  273.         }
  274.  
  275.         if (Mail_Queue::isError($err = $this->_preload())) {
  276.             return $err;
  277.         }
  278.  
  279.         //restore limit
  280.         if ($bkp_limit == MAILQUEUE_ALL) {
  281.             $this->limit = MAILQUEUE_ALL;
  282.         } else {
  283.             $this->limit = $bkp_limit - count($this->queue_data);
  284.         }
  285.  
  286.         //set buffer pointers
  287.         $this->_current_item = 0;
  288.         $this->_last_item = count($this->queue_data)-1;
  289.  
  290.         return true;
  291.     }
  292.  
  293.     // }}}
  294.     // {{{ _isSerialized()
  295.  
  296.     /**
  297.      * Check if the string is a regular string or a serialized array
  298.      *
  299.      * @param string $string
  300.      * @return boolean
  301.      * @access protected
  302.      */
  303.     function _isSerialized($string)
  304.     {
  305.         if (!is_string($string) || strlen($string) < 4) {
  306.             return false;
  307.         }
  308.         // serialized integer?
  309.         if (preg_match('/^i:\d+;$/', $string)) {
  310.             return true;
  311.         }
  312.         // serialized float?
  313.         if (preg_match('/^d:\d(\.\d+)?;$/', $string)) {
  314.             return true;
  315.         }
  316.         // serialized string?
  317.         if (preg_match('/^s:\d+\:\"(.*)\";$/', $string)) {
  318.             return true;
  319.         }
  320.         //serialized array?
  321.         return preg_match('/^a:\d+\:\{(.*)\}$/', $string);
  322.     }
  323.  
  324.     // }}}
  325. }
  326. ?>