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 / mdb2.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  13.5 KB  |  395 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: Mail :: Queue :: MDB2 Container                              |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2008 Lorenzo Alberton                             |
  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. // | Author: Lorenzo Alberton <l.alberton at quipo.it>                    |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: mdb2.php,v 1.11 2008/05/03 15:32:26 quipo Exp $
  20.  
  21. /**
  22.  * Storage driver for fetching mail queue data from a PEAR::MDB2 database
  23.  *
  24.  * This storage driver can use all databases which are supported
  25.  * by the PEAR MDB2 abstraction layer.
  26.  *
  27.  * @author   Lorenzo Alberton <l.alberton at quipo.it>
  28.  * @package  Mail_Queue
  29.  * @version  $Id: mdb2.php,v 1.11 2008/05/03 15:32:26 quipo Exp $
  30.  */
  31. require_once 'MDB2.php';
  32. require_once 'Mail/Queue/Container.php';
  33.  
  34. /**
  35.  * Mail_Queue_Container_mdb2
  36.  */
  37. class Mail_Queue_Container_mdb2 extends Mail_Queue_Container
  38. {
  39.     // {{{ class vars
  40.  
  41.     /**
  42.      * Reference to the current database connection.
  43.      * @var object PEAR::MDB2 instance
  44.      */
  45.     var $db = null;
  46.  
  47.     /**
  48.      * Table for sql database
  49.      * @var  string
  50.      */
  51.     var $mail_table = 'mail_queue';
  52.  
  53.     /**
  54.      * @var string  the name of the sequence for this table
  55.      */
  56.     var $sequence = null;
  57.  
  58.     // }}}
  59.     // {{{ Mail_Queue_Container_mdb2()
  60.  
  61.     /**
  62.      * Constructor
  63.      *
  64.      * Mail_Queue_Container_mdb2()
  65.      *
  66.      * @param mixed $options    An associative array of connection option.
  67.      *
  68.      * @access public
  69.      */
  70.     function Mail_Queue_Container_mdb2($options)
  71.     {
  72.         if (!is_array($options) || !isset($options['dsn'])) {
  73.             return new Mail_Queue_Error(MAILQUEUE_ERROR_NO_OPTIONS,
  74.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  75.                 'No dns specified!');
  76.         }
  77.         if (isset($options['mail_table'])) {
  78.             $this->mail_table = $options['mail_table'];
  79.         }
  80.         $this->sequence = (isset($options['sequence']) ? $options['sequence'] : $this->mail_table);
  81.  
  82.         if (!empty($options['pearErrorMode'])) {
  83.             $this->pearErrorMode = $options['pearErrorMode'];
  84.         }
  85.         $res = $this->_connect($options['dsn']);
  86.         if (PEAR::isError($res)) {
  87.             return $res;
  88.         }
  89.         $this->setOption();
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ _connect()
  94.  
  95.     /**
  96.      * Connect to database by using the given DSN string
  97.      *
  98.      * @param mixed &$db DSN string | array | MDB2 object
  99.      *
  100.      * @return boolean|PEAR_Error on error
  101.      * @access private
  102.      */
  103.     function _connect(&$db)
  104.     {
  105.         if (is_object($db) && is_a($db, 'MDB2_Driver_Common')) {
  106.             $this->db = &$db;
  107.         } elseif (is_string($db) || is_array($db)) {
  108.             include_once 'MDB2.php';
  109.             $this->db =& MDB2::connect($db);
  110.         } elseif (is_object($db) && MDB2::isError($db)) {
  111.             return new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_CONNECT,
  112.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  113.                 'MDB2::connect failed: '. MDB2::errorMessage($this->db));
  114.         } else {
  115.             return new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_CONNECT,
  116.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  117.                 'The given dsn was not valid in file '. __FILE__ . ' at line ' . __LINE__);
  118.         }
  119.         if (PEAR::isError($this->db)) {
  120.             return $this->db;
  121.         }
  122.         return true;
  123.     }
  124.  
  125.     // }}}
  126.     // {{{ _checkConnection()
  127.  
  128.     /**
  129.      * Check if there's a valid db connection
  130.      *
  131.      * @return boolean|PEAR_Error on error
  132.      */
  133.     function _checkConnection() {
  134.         if (!is_object($this->db) || !is_a($this->db, 'MDB2_Driver_Common')) {
  135.             $msg = 'MDB2::connect failed';
  136.             if (PEAR::isError($this->db)) {
  137.                 $msg .= ': '.$this->db->getMessage();
  138.             }
  139.             return new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_CONNECT,
  140.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__, $msg);
  141.         }
  142.         return true;
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ _preload()
  147.  
  148.     /**
  149.      * Preload mail to queue.
  150.      *
  151.      * @return mixed  True on success else Mail_Queue_Error object.
  152.      * @access private
  153.      */
  154.     function _preload()
  155.     {
  156.         $res = $this->_checkConnection();
  157.         if (PEAR::isError($res)) {
  158.             return $res;
  159.         }
  160.         $query = 'SELECT * FROM ' . $this->mail_table
  161.                 .' WHERE sent_time IS NULL AND try_sent < '. $this->try
  162.                 .' AND time_to_send <= '.$this->db->quote(date('Y-m-d H:i:s'), 'timestamp')
  163.                 .' ORDER BY time_to_send';
  164.         $this->db->setLimit($this->limit, $this->offset);
  165.         $res = $this->db->query($query);
  166.         if (PEAR::isError($res)) {
  167.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  168.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  169.                 'MDB2::query failed - "'.$query.'" - '.MDB2::errorMessage($res));
  170.         }
  171.  
  172.         $this->_last_item = 0;
  173.         $this->queue_data = array(); //reset buffer
  174.         while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  175.             //var_dump($row['headers']);
  176.             if (!is_array($row)) {
  177.                 return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  178.                     $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  179.                     'MDB2::query failed - "'.$query.'" - '.MDB2::errorMessage($res));
  180.             }
  181.             $this->queue_data[$this->_last_item] = new Mail_Queue_Body(
  182.                 $row['id'],
  183.                 $row['create_time'],
  184.                 $row['time_to_send'],
  185.                 $row['sent_time'],
  186.                 $row['id_user'],
  187.                 $row['ip'],
  188.                 $row['sender'],
  189.                 $this->_isSerialized($row['recipient']) ? unserialize($row['recipient']) : $row['recipient'],
  190.                 unserialize($row['headers']),
  191.                 unserialize($row['body']),
  192.                 $row['delete_after_send'],
  193.                 $row['try_sent']
  194.             );
  195.             $this->_last_item++;
  196.         }
  197.  
  198.         return true;
  199.     }
  200.  
  201.     // }}}
  202.     // {{{ put()
  203.  
  204.     /**
  205.      * Put new mail in queue and save in database.
  206.      *
  207.      * Mail_Queue_Container::put()
  208.      *
  209.      * @param string $time_to_send  When mail have to be send
  210.      * @param integer $id_user  Sender id
  211.      * @param string $ip  Sender ip
  212.      * @param string $from  Sender e-mail
  213.      * @param string $to  Reciepient e-mail
  214.      * @param string $hdrs  Mail headers (in RFC)
  215.      * @param string $body  Mail body (in RFC)
  216.      * @param bool $delete_after_send  Delete or not mail from db after send
  217.      *
  218.      * @return mixed  ID of the record where this mail has been put
  219.      *                or Mail_Queue_Error on error
  220.      * @access public
  221.      **/
  222.     function put($time_to_send, $id_user, $ip, $sender,
  223.                 $recipient, $headers, $body, $delete_after_send=true)
  224.     {
  225.         $res = $this->_checkConnection();
  226.         if (PEAR::isError($res)) {
  227.             return $res;
  228.         }
  229.         $id = $this->db->nextID($this->sequence);
  230.         if (empty($id) || PEAR::isError($id)) {
  231.             return new Mail_Queue_Error(MAILQUEUE_ERROR,
  232.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  233.                 'Cannot create id in: '.$this->sequence);
  234.         }
  235.         $query = 'INSERT INTO '. $this->mail_table
  236.                 .' (id, create_time, time_to_send, id_user, ip'
  237.                 .', sender, recipient, headers, body, delete_after_send) VALUES ('
  238.                 .       $this->db->quote($id, 'integer')
  239.                 .', ' . $this->db->quote(date('Y-m-d H:i:s'), 'timestamp')
  240.                 .', ' . $this->db->quote($time_to_send, 'timestamp')
  241.                 .', ' . $this->db->quote($id_user, 'integer')
  242.                 .', ' . $this->db->quote($ip, 'text')
  243.                 .', ' . $this->db->quote($sender, 'text')
  244.                 .', ' . $this->db->quote($recipient, 'text')
  245.                 .', ' . $this->db->quote($headers, 'text')   //clob
  246.                 .', ' . $this->db->quote($body, 'text')      //clob
  247.                 .', ' . ($delete_after_send ? 1 : 0)
  248.                 .')';
  249.         $res = $this->db->query($query);
  250.         if (PEAR::isError($res)) {
  251.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  252.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  253.                 'MDB2::query failed - "'.$query.'" - '.$res->getMessage());
  254.         }
  255.         return $id;
  256.     }
  257.  
  258.     // }}}
  259.     // {{{ countSend()
  260.  
  261.     /**
  262.      * Check how many times mail was sent.
  263.      *
  264.      * @param object   Mail_Queue_Body
  265.      * @return mixed  Integer or Mail_Queue_Error class if error.
  266.      * @access public
  267.      */
  268.     function countSend($mail)
  269.     {
  270.         $res = $this->_checkConnection();
  271.         if (PEAR::isError($res)) {
  272.             return $res;
  273.         }
  274.         if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
  275.             return new Mail_Queue_Error(MAILQUEUE_ERROR_UNEXPECTED, __FILE__, __LINE__);
  276.         }
  277.         $count = $mail->_try();
  278.         $query = 'UPDATE ' . $this->mail_table
  279.                 .' SET try_sent = ' . $this->db->quote($count, 'integer')
  280.                 .' WHERE id = '     . $this->db->quote($mail->getId(), 'integer');
  281.         $res = $this->db->query($query);
  282.         if (PEAR::isError($res)) {
  283.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  284.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  285.                 'MDB2::query failed - "'.$query.'" - '.MDB2::errorMessage($res));
  286.         }
  287.         return $count;
  288.     }
  289.  
  290.     // }}}
  291.     // {{{ setAsSent()
  292.  
  293.     /**
  294.      * Set mail as already sent.
  295.      *
  296.      * @param object Mail_Queue_Body object
  297.      * @return bool
  298.      * @access public
  299.      */
  300.     function setAsSent($mail)
  301.     {
  302.         $res = $this->_checkConnection();
  303.         if (PEAR::isError($res)) {
  304.             return $res;
  305.         }
  306.         if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
  307.             return new Mail_Queue_Error(MAILQUEUE_ERROR_UNEXPECTED,
  308.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  309.                'Expected: Mail_Queue_Body class');
  310.         }
  311.         $query = 'UPDATE ' . $this->mail_table
  312.                 .' SET sent_time = '.$this->db->quote(date('Y-m-d H:i:s'), 'timestamp')
  313.                 .' WHERE id = '. $this->db->quote($mail->getId(), 'integer');
  314.  
  315.         $res = $this->db->query($query);
  316.         if (PEAR::isError($res)) {
  317.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  318.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  319.                 'MDB2::query failed - "'.$query.'" - '.MDB2::errorMessage($res));
  320.         }
  321.         return true;
  322.     }
  323.  
  324.     // }}}
  325.     // {{{ getMailById()
  326.  
  327.     /**
  328.      * Return mail by id $id (bypass mail_queue)
  329.      *
  330.      * @param integer $id  Mail ID
  331.      * @return mixed  Mail object or false on error.
  332.      * @access public
  333.      */
  334.     function getMailById($id)
  335.     {
  336.         $res = $this->_checkConnection();
  337.         if (PEAR::isError($res)) {
  338.             return $res;
  339.         }
  340.         $query = 'SELECT * FROM ' . $this->mail_table
  341.                 .' WHERE id = '   . (int)$id;
  342.         $row = $this->db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
  343.         if (PEAR::isError($row) || !is_array($row)) {
  344.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  345.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  346.                 'MDB2: query failed - "'.$query.'" - '.$row->getMessage());
  347.         }
  348.         return new Mail_Queue_Body(
  349.             $row['id'],
  350.             $row['create_time'],
  351.             $row['time_to_send'],
  352.             $row['sent_time'],
  353.             $row['id_user'],
  354.             $row['ip'],
  355.             $row['sender'],
  356.             $this->_isSerialized($row['recipient']) ? unserialize($row['recipient']) : $row['recipient'],
  357.             unserialize($row['headers']),
  358.             unserialize($row['body']),
  359.             $row['delete_after_send'],
  360.             $row['try_sent']
  361.         );
  362.     }
  363.  
  364.     // }}}
  365.     // {{{ deleteMail()
  366.  
  367.     /**
  368.      * Remove from queue mail with $id identifier.
  369.      *
  370.      * @param integer $id  Mail ID
  371.      * @return bool  True on success else Mail_Queue_Error class
  372.      *
  373.      * @access public
  374.      */
  375.     function deleteMail($id)
  376.     {
  377.         $res = $this->_checkConnection();
  378.         if (PEAR::isError($res)) {
  379.             return $res;
  380.         }
  381.         $query = 'DELETE FROM ' . $this->mail_table
  382.                 .' WHERE id = ' . $this->db->quote($id, 'text');
  383.         $res = $this->db->query($query);
  384.  
  385.         if (PEAR::isError($res)) {
  386.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  387.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  388.                 'MDB2::query failed - "'.$query.'" - '.MDB2::errorMessage($res));
  389.         }
  390.         return true;
  391.     }
  392.  
  393.     // }}}
  394. }
  395. ?>