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 / creole.php next >
Encoding:
PHP Script  |  2008-07-02  |  12.3 KB  |  374 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: Mail :: Queue :: CREOLE Container                            |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2007 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. // | Author: Randy Syring <randy at rcs-comp dot com>                     |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: creole.php,v 1.1 2007/01/31 14:49:27 quipo Exp $
  20.  
  21. /**
  22.  * Storage driver for fetching mail queue data from a CREOLE database
  23.  *
  24.  * This storage driver can use all databases which are supported
  25.  * by the CREOLE abstraction layer.
  26.  *
  27.  * @author   Randy Syring <randy at rcs-comp dot com>
  28.  * @package  Mail_Queue
  29.  * @version  $Id: creole.php,v 1.1 2007/01/31 14:49:27 quipo Exp $
  30.  */
  31. require_once 'creole/Creole.php';
  32. require_once 'Mail/Queue/Container.php';
  33.  
  34. /**
  35.  * Mail_Queue_Container_creole
  36.  */
  37. class Mail_Queue_Container_creole 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;
  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.     var $constructor_error = null;
  59.  
  60.     // }}}
  61.     // {{{ Mail_Queue_Container_Creole()
  62.  
  63.     /**
  64.      * Constructor
  65.      *
  66.      * Mail_Queue_Container_creole()
  67.      *
  68.      * @param mixed $options    An associative array of connection option.
  69.      *
  70.      * @access public
  71.      */
  72.     function Mail_Queue_Container_creole($options)
  73.     {
  74.         if (!is_array($options) || (!isset($options['dsn']) && !isset( $options['connection']))) {
  75.             $this->constructor_error =  new Mail_Queue_Error(MAILQUEUE_ERROR_NO_OPTIONS,
  76.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  77.                 'No dns specified!');
  78.             return;
  79.         }
  80.         if (isset($options['mail_table'])) {
  81.             $this->mail_table = $options['mail_table'];
  82.         }
  83.  
  84.         if (!empty($options['pearErrorMode'])) {
  85.             $this->pearErrorMode = $options['pearErrorMode'];
  86.         }
  87.         try {
  88.             $this->db = isset($options['connection']) ? $options['connection'] : Creole::getConnection($options['dsn']);
  89.         } catch (SQLException $e) {
  90.             $this->constructor_error = new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_CONNECT,
  91.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  92.                 'CREOLE::connect failed: '. $e->getMessage());
  93.             return;
  94.         }
  95.         $this->setOption();
  96.     }
  97.  
  98.     // }}}
  99.     // {{{ _preload()
  100.  
  101.     /**
  102.      * Preload mail to queue.
  103.      *
  104.      * @return mixed  True on success else Mail_Queue_Error object.
  105.      * @access private
  106.      */
  107.     function _preload()
  108.     {
  109.         $sql = "SELECT * FROM {$this->mail_table} WHERE sent_time IS NULL
  110.                             AND try_sent < ?
  111.                             AND ? > time_to_send
  112.                             ORDER BY time_to_send";
  113.         try {
  114.             $stmt = $this->db->prepareStatement( $sql );
  115.             $stmt->setLimit($this->limit);
  116.  
  117.             $stmt->setString( 1, $this->try );
  118.             $stmt->setTimeStamp( 2, time() );
  119.             $res = $stmt->executeQuery();
  120.  
  121.             $this->_last_item = 0;
  122.             $this->queue_data = array(); //reset buffer
  123.             while ($res->next()) {
  124.                 $row = $res->getRow();
  125.                 $this->queue_data[$this->_last_item] = new Mail_Queue_Body(
  126.                     $row['id'],
  127.                     $row['create_time'],
  128.                     $row['time_to_send'],
  129.                     $row['sent_time'],
  130.                     $row['id_user'],
  131.                     $row['ip'],
  132.                     $row['sender'],
  133.                     $this->_isSerialized($row['recipient']) ? unserialize($row['recipient']) : $row['recipient'],
  134.                     unserialize($row['headers']),
  135.                     unserialize($row['body']),
  136.                     $row['delete_after_send'],
  137.                     $row['try_sent']
  138.                 );
  139.                 $this->_last_item++;
  140.             }
  141.         } catch (SQLException $e ) {
  142.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  143.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  144.                 'CREOLE::query failed - "'.$sql.'" - '.$e->getMessage());
  145.         }
  146.  
  147.         return true;
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ put()
  152.  
  153.     /**
  154.      * Put new mail in queue and save in database.
  155.      *
  156.      * Mail_Queue_Container::put()
  157.      *
  158.      * @param string $time_to_send  When mail have to be send
  159.      * @param integer $id_user  Sender id
  160.      * @param string $ip  Sender ip
  161.      * @param string $from  Sender e-mail
  162.      * @param string $to  Reciepient e-mail
  163.      * @param string $hdrs  Mail headers (in RFC)
  164.      * @param string $body  Mail body (in RFC)
  165.      * @param bool $delete_after_send  Delete or not mail from db after send
  166.      *
  167.      * @return mixed  ID of the record where this mail has been put
  168.      *                or Mail_Queue_Error on error
  169.      * @access public
  170.      */
  171.     function put($time_to_send, $id_user, $ip, $sender,
  172.                 $recipient, $headers, $body, $delete_after_send=true)
  173.     {
  174.         $query = 'INSERT INTO '. $this->mail_table
  175.                 .' (create_time, time_to_send, id_user, ip'
  176.                 .', sender, recipient, headers, body, delete_after_send) VALUES ('
  177.                 .str_repeat('? ,', 8)
  178.                 .'?)';
  179.         try {
  180.             $idgen = $this->db->getIdGenerator();
  181.             $stmt = $this->db->prepareStatement($query);
  182.             $stmt->setTimestamp(1, time());
  183.             $stmt->setTimestamp(2, $time_to_send);
  184.             $stmt->setInt(3, $id_user);
  185.             $stmt->setString(4, $ip );
  186.             $stmt->setString(5, $sender );
  187.             $stmt->setString(6, $recipient);
  188.             $stmt->setString(7, $headers);
  189.             $stmt->setString(8, $body);
  190.             $stmt->setInt(9, $delete_after_send ? 1 : 0 );
  191.             $stmt->executeUpdate();
  192.         } catch (SQLException $e ) {
  193.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  194.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  195.                 'CREOLE::query failed - "'.$query.'" - '.$e->getMessage());
  196.         }
  197.         return $idgen->getId();
  198.     }
  199.  
  200.     // }}}
  201.     // {{{ countSend()
  202.  
  203.     /**
  204.      * Check how many times mail was sent.
  205.      *
  206.      * @param object   Mail_Queue_Body
  207.      * @return mixed  Integer or Mail_Queue_Error class if error.
  208.      * @access public
  209.      */
  210.     function countSend($mail)
  211.     {
  212.         if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
  213.             return new Mail_Queue_Error('Expected: Mail_Queue_Body class',
  214.                 __FILE__, __LINE__);
  215.         }
  216.         $count = $mail->_try();
  217.         $sql = 'UPDATE '. $this->mail_table
  218.                 .' SET try_sent = ?'
  219.                 .' WHERE id = ?';
  220.         try {
  221.             $stmt = $this->db->prepareStatement( $sql );
  222.             $stmt->setInt( 1, $count );
  223.             $stmt->setInt( 2, $mail->getId() );
  224.             $stmt->executeUpdate();
  225.         } catch (SQLException $e ) {
  226.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  227.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  228.                 'CREOLE::query failed - "'.$sql.'" - '.$e->getMessage());
  229.         }
  230.         return $count;
  231.     }
  232.  
  233.     // }}}
  234.     // {{{ setAsSent()
  235.  
  236.     /**
  237.      * Set mail as already sent.
  238.      *
  239.      * @param object Mail_Queue_Body object
  240.      * @return bool
  241.      * @access public
  242.      */
  243.     function setAsSent($mail)
  244.     {
  245.         if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
  246.             return new Mail_Queue_Error(MAILQUEUE_ERROR_UNEXPECTED,
  247.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  248.                'Expected: Mail_Queue_Body class');
  249.         }
  250.  
  251.         $sql = 'UPDATE '. $this->mail_table
  252.                 .' SET sent_time = ?'
  253.                 .' WHERE id = ?';
  254.         try {
  255.             $stmt = $this->db->prepareStatement( $sql );
  256.             $stmt->setTimeStamp( 1, time() );
  257.             $stmt->setInt( 2, $mail->getId() );
  258.             $stmt->executeUpdate();
  259.         } catch (SQLException $e ) {
  260.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  261.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  262.                 'CREOLE::query failed - "'.$sql.'" - '.$e->getMessage());
  263.         }
  264.         return true;
  265.     }
  266.  
  267.     // }}}
  268.     // {{{ getMailById()
  269.  
  270.     /**
  271.      * Return mail by id $id (bypass mail_queue)
  272.      *
  273.      * @param integer $id  Mail ID
  274.      * @return mixed  Mail object or false on error.
  275.      * @access public
  276.      */
  277.     function getMailById($id)
  278.     {
  279.         $sql = 'SELECT * FROM '. $this->mail_table
  280.                 .' WHERE id = ?';
  281.  
  282.         try {
  283.             $stmt = $this->db->prepareStatement( $sql );
  284.             $stmt->setInt( 1, $mail->getId() );
  285.             $res = $stmt->executeQuery();
  286.  
  287.             $res->next();
  288.             $row = $res->getRow();
  289.         } catch (SQLException $e ) {
  290.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  291.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  292.                 'CREOLE::query failed - "'.$sql.'" - '.$e->getMessage());
  293.         }
  294.  
  295.         return new Mail_Queue_Body(
  296.             $row['id'],
  297.             $row['create_time'],
  298.             $row['time_to_send'],
  299.             $row['sent_time'],
  300.             $row['id_user'],
  301.             $row['ip'],
  302.             $row['sender'],
  303.             $this->_isSerialized($row['recipient']) ? unserialize($row['recipient']) : $row['recipient'],
  304.             unserialize($row['headers']),
  305.             unserialize($row['body']),
  306.             $row['delete_after_send'],
  307.             $row['try_sent']
  308.         );
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ deleteMail()
  313.  
  314.     /**
  315.      * Remove from queue mail with $id identifier.
  316.      *
  317.      * @param integer $id  Mail ID
  318.      * @return bool  True on success else Mail_Queue_Error class
  319.      *
  320.      * @access public
  321.      */
  322.     function deleteMail($id)
  323.     {
  324.         $sql = 'DELETE FROM '. $this->mail_table
  325.                 .' WHERE id = ?';
  326.  
  327.         try {
  328.             $stmt = $this->db->prepareStatement( $sql );
  329.             $stmt->setInt( 1, $id );
  330.             $stmt->executeUpdate();
  331.         } catch (SQLException $e ) {
  332.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  333.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  334.                 'CREOLE::query failed - "'.$sql.'" - '.$e->getMessage());
  335.         }
  336.         return true;
  337.     }
  338.     
  339.     // }}}
  340.     // {{{ queueSize()
  341.  
  342.     /**
  343.      * Ge the size of the queue.
  344.      *
  345.      * @return int|Mail_Queue_Error Queue size on success, Mail_Queue_Error on failure
  346.      *
  347.      * @access private
  348.      */
  349.     function queueSize()
  350.     {
  351.         $sql = "SELECT COUNT(*) as count FROM {$this->mail_table} WHERE sent_time IS NULL
  352.                             AND try_sent < ?
  353.                             AND ? > time_to_send
  354.                             ORDER BY time_to_send";
  355.         try {
  356.             $stmt = $this->db->prepareStatement( $sql );
  357.             $stmt->setString( 1, $this->try );
  358.             $stmt->setTimeStamp( 2, time() );
  359.             $res = $stmt->executeQuery();
  360.  
  361.             $res->next();
  362.             $result = $res->getRow();
  363.         } catch ( SQLException $e ) {
  364.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  365.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  366.                 'CREOLE::query failed - "'.$sql.'" - '.$e->getMessage());
  367.         }
  368.  
  369.         return $result['count'];
  370.     }
  371.  
  372.     // }}}
  373. }
  374. ?>