PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Mailing/Event/BAO/Bounce.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 304 lines | 171 code | 40 blank | 93 comment | 18 complexity | 6f14e97dcf1f4ae09f2e14d313ff7fe1 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. class CRM_Mailing_Event_BAO_Bounce extends CRM_Mailing_Event_DAO_Bounce {
  33. /**
  34. * Class constructor.
  35. */
  36. public function __construct() {
  37. parent::__construct();
  38. }
  39. /**
  40. * Create a new bounce event, update the email address if necessary
  41. *
  42. * @param $params
  43. *
  44. * @return bool|null
  45. */
  46. public static function create(&$params) {
  47. $q = CRM_Mailing_Event_BAO_Queue::verify($params['job_id'],
  48. $params['event_queue_id'],
  49. $params['hash']
  50. );
  51. $success = NULL;
  52. if (!$q) {
  53. return $success;
  54. }
  55. $transaction = new CRM_Core_Transaction();
  56. $bounce = new CRM_Mailing_Event_BAO_Bounce();
  57. $bounce->time_stamp = date('YmdHis');
  58. $action = empty($params['id']) ? 'create' : 'edit';
  59. CRM_Utils_Hook::pre($action, 'MailingEventBounce', CRM_Utils_Array::value('id', $params), $params);
  60. // if we dont have a valid bounce type, we should set it
  61. // to bounce_type_id 11 which is Syntax error. this allows such email
  62. // addresses to be bounce a few more time before being put on hold
  63. // CRM-4814
  64. // we changed this behavior since this bounce type might be due to some issue
  65. // with the connection or smtp server etc
  66. if (empty($params['bounce_type_id'])) {
  67. $params['bounce_type_id'] = 11;
  68. if (empty($params['bounce_reason'])) {
  69. $params['bounce_reason'] = ts('Unknown bounce type: Could not parse bounce email');
  70. }
  71. }
  72. // replace any invalid unicode characters with replacement characters
  73. $params['bounce_reason'] = mb_convert_encoding($params['bounce_reason'], 'UTF-8', 'UTF-8');
  74. // dev/mail#37 Replace 4-byte utf8 characaters with the unicode replacement character
  75. // while CiviCRM does not support utf8mb4 for MySQL
  76. $params['bounce_reason'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $params['bounce_reason']);
  77. // CRM-11989
  78. $params['bounce_reason'] = mb_strcut($params['bounce_reason'], 0, 254);
  79. $bounce->copyValues($params);
  80. $bounce->save();
  81. CRM_Utils_Hook::post($action, 'MailingEventBounce', $bounce->id, $bounce);
  82. if ($q->email_id) {
  83. self::putEmailOnHold($q->email_id);
  84. }
  85. $transaction->commit();
  86. return TRUE;
  87. }
  88. /**
  89. * Get row count for the event selector.
  90. *
  91. * @param int $mailing_id
  92. * ID of the mailing.
  93. * @param int $job_id
  94. * Optional ID of a job to filter on.
  95. * @param bool $is_distinct
  96. * Group by queue ID?.
  97. *
  98. * @param string|null $toDate
  99. *
  100. * @return int
  101. * Number of rows in result set
  102. */
  103. public static function getTotalCount($mailing_id, $job_id = NULL, $is_distinct = FALSE, $toDate = NULL) {
  104. $dao = new CRM_Core_DAO();
  105. $bounce = self::getTableName();
  106. $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
  107. $mailing = CRM_Mailing_BAO_Mailing::getTableName();
  108. $job = CRM_Mailing_BAO_MailingJob::getTableName();
  109. $query = "
  110. SELECT COUNT($bounce.id) as bounce
  111. FROM $bounce
  112. INNER JOIN $queue
  113. ON $bounce.event_queue_id = $queue.id
  114. INNER JOIN $job
  115. ON $queue.job_id = $job.id
  116. INNER JOIN $mailing
  117. ON $job.mailing_id = $mailing.id
  118. WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
  119. if (!empty($toDate)) {
  120. $query .= " AND $bounce.time_stamp <= $toDate";
  121. }
  122. if (!empty($job_id)) {
  123. $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
  124. }
  125. if ($is_distinct) {
  126. $query .= " GROUP BY $queue.id ";
  127. }
  128. // query was missing
  129. $dao->query($query);
  130. if ($dao->fetch()) {
  131. return $dao->bounce;
  132. }
  133. return NULL;
  134. }
  135. /**
  136. * Get rows for the event browser.
  137. *
  138. * @param int $mailing_id
  139. * ID of the mailing.
  140. * @param int $job_id
  141. * Optional ID of the job.
  142. * @param bool $is_distinct
  143. * Group by queue id?.
  144. * @param int $offset
  145. * Offset.
  146. * @param int $rowCount
  147. * Number of rows.
  148. * @param array $sort
  149. * Sort array.
  150. *
  151. * @return array
  152. * Result set
  153. */
  154. public static function &getRows(
  155. $mailing_id, $job_id = NULL,
  156. $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
  157. ) {
  158. $dao = new CRM_Core_DAO();
  159. $bounce = self::getTableName();
  160. $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
  161. $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
  162. $mailing = CRM_Mailing_BAO_Mailing::getTableName();
  163. $job = CRM_Mailing_BAO_MailingJob::getTableName();
  164. $contact = CRM_Contact_BAO_Contact::getTableName();
  165. $email = CRM_Core_BAO_Email::getTableName();
  166. $query = "
  167. SELECT $contact.display_name as display_name,
  168. $contact.id as contact_id,
  169. $email.email as email,
  170. $bounce.time_stamp as date,
  171. $bounce.bounce_reason as reason,
  172. $bounceType.name as bounce_type
  173. FROM $contact
  174. INNER JOIN $queue
  175. ON $queue.contact_id = $contact.id
  176. INNER JOIN $email
  177. ON $queue.email_id = $email.id
  178. INNER JOIN $bounce
  179. ON $bounce.event_queue_id = $queue.id
  180. LEFT JOIN $bounceType
  181. ON $bounce.bounce_type_id = $bounceType.id
  182. INNER JOIN $job
  183. ON $queue.job_id = $job.id
  184. AND $job.is_test = 0
  185. INNER JOIN $mailing
  186. ON $job.mailing_id = $mailing.id
  187. WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer');
  188. if (!empty($job_id)) {
  189. $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer');
  190. }
  191. if ($is_distinct) {
  192. $query .= " GROUP BY $queue.id, $bounce.time_stamp, $bounce.bounce_reason, $bounceType.name ";
  193. }
  194. $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC";
  195. if ($sort) {
  196. if (is_string($sort)) {
  197. $sort = CRM_Utils_Type::escape($sort, 'String');
  198. $orderBy = $sort;
  199. }
  200. else {
  201. $orderBy = trim($sort->orderBy());
  202. }
  203. }
  204. $query .= " ORDER BY {$orderBy} ";
  205. if ($offset || $rowCount) {
  206. //Added "||$rowCount" to avoid displaying all records on first page
  207. $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer');
  208. }
  209. $dao->query($query);
  210. $results = array();
  211. while ($dao->fetch()) {
  212. $url = CRM_Utils_System::url('civicrm/contact/view',
  213. "reset=1&cid={$dao->contact_id}"
  214. );
  215. $results[] = array(
  216. 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
  217. 'email' => $dao->email,
  218. // FIXME: translate this
  219. 'type' => (empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type
  220. ),
  221. 'reason' => $dao->reason,
  222. 'date' => CRM_Utils_Date::customFormat($dao->date),
  223. );
  224. }
  225. return $results;
  226. }
  227. /**
  228. * Put the email on hold if it has met the threshold.
  229. *
  230. * @param int $email_id
  231. */
  232. protected static function putEmailOnHold($email_id) {
  233. $bounceTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
  234. $bounceType = CRM_Mailing_DAO_BounceType::getTableName();
  235. $emailTable = CRM_Core_BAO_Email::getTableName();
  236. $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
  237. // might want to put distinct inside the count
  238. $query = "SELECT count($bounceTable.id) as bounces,
  239. $bounceType.hold_threshold as threshold
  240. FROM $bounceTable
  241. INNER JOIN $bounceType
  242. ON $bounceTable.bounce_type_id = $bounceType.id
  243. INNER JOIN $queueTable
  244. ON $bounceTable.event_queue_id = $queueTable.id
  245. INNER JOIN $emailTable
  246. ON $queueTable.email_id = $emailTable.id
  247. WHERE $emailTable.id = $email_id
  248. AND ($emailTable.reset_date IS NULL
  249. OR $bounceTable.time_stamp >= $emailTable.reset_date)
  250. GROUP BY $bounceTable.bounce_type_id
  251. ORDER BY threshold, bounces desc";
  252. $dao = CRM_Core_DAO::executeQuery($query);
  253. while ($dao->fetch()) {
  254. if ($dao->bounces >= $dao->threshold) {
  255. $email = new CRM_Core_BAO_Email();
  256. $email->id = $email_id;
  257. $email->on_hold = TRUE;
  258. $email->hold_date = date('YmdHis');
  259. $email->save();
  260. break;
  261. }
  262. }
  263. }
  264. }