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 / db.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.6 KB  |  361 lines

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