PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/Espo/Core/Mail/FiltersMatcher.php

https://gitlab.com/johanlindberg/irvato-crm
PHP | 120 lines | 81 code | 12 blank | 27 comment | 21 complexity | 7095190b06ee7002b18df8b5194e5c13 MD5 | raw file
  1. <?php
  2. /************************************************************************
  3. * This file is part of EspoCRM.
  4. *
  5. * EspoCRM - Open Source CRM application.
  6. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  7. * Website: http://www.espocrm.com
  8. *
  9. * EspoCRM is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * EspoCRM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  21. *
  22. * The interactive user interfaces in modified source and object code versions
  23. * of this program must display Appropriate Legal Notices, as required under
  24. * Section 5 of the GNU General Public License version 3.
  25. *
  26. * In accordance with Section 7(b) of the GNU General Public License version 3,
  27. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  28. ************************************************************************/
  29. namespace Espo\Core\Mail;
  30. use \Espo\Entities\Email;
  31. class FiltersMatcher
  32. {
  33. public function __construct()
  34. {
  35. }
  36. public function match(Email $email, $subject, $skipBody = false)
  37. {
  38. if (is_array($subject) || $subject instanceof \Traversable) {
  39. $filterList = $subject;
  40. } else {
  41. $filterList = [$subject];
  42. }
  43. foreach ($filterList as $filter) {
  44. if ($filter->get('from')) {
  45. if ($this->matchString(strtolower($filter->get('from')), strtolower($email->get('from')))) {
  46. return true;
  47. }
  48. }
  49. if ($filter->get('to')) {
  50. if ($email->get('to')) {
  51. $toArr = explode(';', $email->get('to'));
  52. foreach ($toArr as $to) {
  53. if ($this->matchString(strtolower($filter->get('to')), strtolower($to))) {
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. if ($filter->get('subject')) {
  60. if ($this->matchString($filter->get('subject'), $email->get('name'))) {
  61. return true;
  62. }
  63. }
  64. }
  65. if (!$skipBody) {
  66. if ($this->matchBody($email, $filterList)) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. public function matchBody(Email $email, $subject)
  73. {
  74. if (is_array($subject) || $subject instanceof \Traversable) {
  75. $filterList = $subject;
  76. } else {
  77. $filterList = [$subject];
  78. }
  79. foreach ($filterList as $filter) {
  80. if ($filter->get('bodyContains')) {
  81. $phraseList = $filter->get('bodyContains');
  82. $body = $email->get('body');
  83. $bodyPlain = $email->get('bodyPlain');
  84. foreach ($phraseList as $phrase) {
  85. if (stripos($bodyPlain, $phrase) !== false) {
  86. return true;
  87. }
  88. if (stripos($body, $phrase) !== false) {
  89. return true;
  90. }
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. protected function matchString($pattern, $value)
  97. {
  98. if ($pattern == $value) {
  99. return true;
  100. }
  101. $pattern = preg_quote($pattern, '#');
  102. $pattern = str_replace('\*', '.*', $pattern).'\z';
  103. if (preg_match('#^'.$pattern.'#', $value)) {
  104. return true;
  105. }
  106. return false;
  107. }
  108. }