| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | class CRM_Mailing_BAO_BouncePattern extends CRM_Mailing_DAO_BouncePattern { |
| 18 | |
| 19 | /** |
| 20 | * Pseudo-constant pattern array. |
| 21 | * @var array |
| 22 | */ |
| 23 | public static $_patterns = NULL; |
| 24 | |
| 25 | /** |
| 26 | * Class constructor. |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | parent::__construct(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Build the static pattern array. |
| 34 | */ |
| 35 | public static function buildPatterns() { |
| 36 | self::$_patterns = []; |
| 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 | * |
| 57 | * @param string $message |
| 58 | * The message to be matched. |
| 59 | * |
| 60 | * @return array |
| 61 | * Tuple (bounce_type, bounce_reason) |
| 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)) { |
| 73 | $bounce = [ |
| 74 | 'bounce_type_id' => $type, |
| 75 | 'bounce_reason' => $message, |
| 76 | ]; |
| 77 | return $bounce; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $bounce = [ |
| 82 | 'bounce_type_id' => NULL, |
| 83 | 'bounce_reason' => $message, |
| 84 | ]; |
| 85 | |
| 86 | return $bounce; |
| 87 | } |
| 88 | |
| 89 | } |