Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Mailing_BAO_BouncePattern extends CRM_Mailing_DAO_BouncePattern { | |
18 | ||
19 | /** | |
25606795 | 20 | * Pseudo-constant pattern array. |
7e8c8317 | 21 | * @var array |
6a488035 | 22 | */ |
7e8c8317 | 23 | public static $_patterns = NULL; |
6a488035 TO |
24 | |
25 | /** | |
fe482240 | 26 | * Class constructor. |
6a488035 | 27 | */ |
00be9182 | 28 | public function __construct() { |
6a488035 TO |
29 | parent::__construct(); |
30 | } | |
31 | ||
32 | /** | |
fe482240 | 33 | * Build the static pattern array. |
6a488035 TO |
34 | */ |
35 | public static function buildPatterns() { | |
be2fb01f | 36 | self::$_patterns = []; |
6a488035 TO |
37 | $bp = new CRM_Mailing_BAO_BouncePattern(); |
38 | $bp->find(); | |
39 | ||
40 | while ($bp->fetch()) { | |
41 | self::$_patterns[$bp->bounce_type_id][] = $bp->pattern; | |
42 | } | |
43 | ||
44 | foreach (self::$_patterns as $type => $patterns) { | |
45 | if (count($patterns) == 1) { | |
46 | self::$_patterns[$type] = '{(' . $patterns[0] . ')}im'; | |
47 | } | |
48 | else { | |
49 | self::$_patterns[$type] = '{(' . implode(')|(', $patterns) . ')}im'; | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
54 | /** | |
55 | * Try to match the string to a bounce type. | |
56 | * | |
90c8230e TO |
57 | * @param string $message |
58 | * The message to be matched. | |
6a488035 | 59 | * |
a6c01b45 CW |
60 | * @return array |
61 | * Tuple (bounce_type, bounce_reason) | |
6a488035 TO |
62 | */ |
63 | public static function &match(&$message) { | |
64 | // clean up $message and replace all white space by a single space, CRM-4767 | |
65 | $message = preg_replace('/\s+/', ' ', $message); | |
66 | ||
67 | if (self::$_patterns == NULL) { | |
68 | self::buildPatterns(); | |
69 | } | |
70 | ||
71 | foreach (self::$_patterns as $type => $re) { | |
72 | if (preg_match($re, $message, $matches)) { | |
be2fb01f | 73 | $bounce = [ |
6a488035 TO |
74 | 'bounce_type_id' => $type, |
75 | 'bounce_reason' => $message, | |
be2fb01f | 76 | ]; |
6a488035 TO |
77 | return $bounce; |
78 | } | |
79 | } | |
80 | ||
be2fb01f | 81 | $bounce = [ |
6a488035 TO |
82 | 'bounce_type_id' => NULL, |
83 | 'bounce_reason' => $message, | |
be2fb01f | 84 | ]; |
6a488035 TO |
85 | |
86 | return $bounce; | |
87 | } | |
96025800 | 88 | |
6a488035 | 89 | } |