CRM-15578 - Mailing.submit API - Accept "approval_note"
[civicrm-core.git] / CRM / Mailing / BAO / BouncePattern.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Mailing_BAO_BouncePattern extends CRM_Mailing_DAO_BouncePattern {
36
37 /**
38 * Pseudo-constant pattern array
39 */
40 static $_patterns = NULL;
41
42 /**
43 * class constructor
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Build the static pattern array
51 *
52 * @return void
53 * @access public
54 * @static
55 */
56 public static function buildPatterns() {
57 self::$_patterns = array();
58 $bp = new CRM_Mailing_BAO_BouncePattern();
59 $bp->find();
60
61 while ($bp->fetch()) {
62 self::$_patterns[$bp->bounce_type_id][] = $bp->pattern;
63 }
64
65 foreach (self::$_patterns as $type => $patterns) {
66 if (count($patterns) == 1) {
67 self::$_patterns[$type] = '{(' . $patterns[0] . ')}im';
68 }
69 else {
70 self::$_patterns[$type] = '{(' . implode(')|(', $patterns) . ')}im';
71 }
72 }
73 }
74
75 /**
76 * Try to match the string to a bounce type.
77 *
78 * @param string $message The message to be matched
79 *
80 * @return array Tuple (bounce_type, bounce_reason)
81 * @access public
82 * @static
83 */
84 public static function &match(&$message) {
85 // clean up $message and replace all white space by a single space, CRM-4767
86 $message = preg_replace('/\s+/', ' ', $message);
87
88 if (self::$_patterns == NULL) {
89 self::buildPatterns();
90 }
91
92 foreach (self::$_patterns as $type => $re) {
93 if (preg_match($re, $message, $matches)) {
94 $bounce = array(
95 'bounce_type_id' => $type,
96 'bounce_reason' => $message,
97 );
98 return $bounce;
99 }
100 }
101
102 $bounce = array(
103 'bounce_type_id' => NULL,
104 'bounce_reason' => $message,
105 );
106
107 return $bounce;
108 }
109}
110