PageRenderTime 135ms CodeModel.GetById 33ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/include/Mail/Queue/Container/db.php

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