email-import-helper.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2020 Johannes Keßler
  7. *
  8. * Development starting from 2011: Johannes Keßler
  9. * https://www.bananas-playground.net/projekt/insipid/
  10. *
  11. * creator:
  12. * Luke Reeves <luke@neuro-tech.net>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  26. *
  27. */
  28. /**
  29. * a static helper class for email importer
  30. */
  31. class EmailImportHelper {
  32. /**
  33. * extract from given string (eg. email body) any links we want to add
  34. * should be in the right format. See documentation
  35. * return an array with links and the infos about them
  36. *
  37. * @param string $string
  38. * @return array $ret
  39. */
  40. static function extractEmailLinks($string) {
  41. $ret = array();
  42. #this matches a valid URL. An URL with | is still valid...
  43. $urlpattern = '#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#';
  44. preg_match_all($urlpattern, $string, $matches);
  45. if(isset($matches[0]) && !empty($matches[0])) {
  46. foreach($matches[0] as $match) {
  47. $ret[md5($match)] = $match;
  48. }
  49. }
  50. return $ret;
  51. }
  52. /**
  53. * Check if given from is in the valid EMAIL_REPLY_BACK_VALID
  54. * @param $replyto
  55. * @return bool
  56. */
  57. static function canSendReplyTo($replyto) {
  58. if(defined("EMAIL_REPORT_BACK") && EMAIL_REPORT_BACK === true
  59. && defined("EMAIL_REPLY_BACK_VALID") && !empty(EMAIL_REPLY_BACK_VALID)) {
  60. if(strstr($replyto,EMAIL_REPLY_BACK_VALID)) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. /**
  67. * check if given email header identify the email as a autoreply
  68. * if so, you should do nothing with this email
  69. *
  70. * based an some code from:
  71. * https://github.com/r-a-y/bp-reply-by-email
  72. * https://arp242.net/autoreply.html
  73. * https://github.com/jpmckinney/multi_mail/wiki/Detecting-autoresponders
  74. * https://github.com/Exim/exim/wiki/EximAutoReply#Router-1
  75. * https://stackoverflow.com/questions/6317714/apache-camel-mail-to-identify-auto-generated-messages/6383675#6383675
  76. *
  77. * this does not really quality check the values. It just checks if some headers are set
  78. *
  79. * @param $headers complete email headers as an array
  80. * @return bool
  81. */
  82. static function isAutoReplyMessage($headers) {
  83. if(empty($headers)) {
  84. return true;
  85. }
  86. foreach($headers as $k=>$v) {
  87. $headers[strtolower($k)] = $v;
  88. }
  89. if(isset($headers['auto-submitted'])
  90. || isset($headers['x-autoreply'])
  91. || isset($headers['x-autorespond'])
  92. || isset($headers['x-auto-response-suppress'])
  93. || isset($headers['list-id'])
  94. || isset($headers['list-subscribe'])
  95. || isset($headers['list-archive'])
  96. || isset($headers['list-help'])
  97. || isset($headers['list-post'])
  98. || isset($headers['mailing-list'])
  99. || isset($headers['x-mailing-list'])
  100. || isset($headers['list-unsubscribe'])
  101. || isset($headers['list-owner'])
  102. || isset($headers['auto-submitted'])
  103. || isset($headers['x-autoreply-from'])
  104. || isset($headers['x-mail-autoreply'])
  105. || isset($headers['x-mc-system'])
  106. || isset($headers['x-fc-machinegenerated'])
  107. || isset($headers['x-facebook-notify'])
  108. || isset($headers['x-autogenerated'])
  109. || isset($headers['feedback-id'])
  110. || isset($headers['x-msfbl'])
  111. || isset($headers['x-loop'])
  112. || isset($headers['x-cron-env'])
  113. || isset($headers['x-ebay-mailtracker'])
  114. || (isset($headers['precedence']) && $headers['precedence'] == "list")
  115. || (isset($headers['x-precedence']) && $headers['x-precedence'] == "list")
  116. || (isset($headers['precedence']) && $headers['precedence'] == "auto_reply")
  117. || (isset($headers['x-precedence']) && $headers['x-precedence'] == "auto_reply")
  118. || (isset($headers['x-post-messageclass']) && $headers['x-post-messageclass'] == "9; Autoresponder")
  119. || (isset($headers['delivered-to']) && $headers['delivered-to'] == "Autoresponder")
  120. || (isset($headers['x-spam-flag']) && $headers['x-spam-flag'] == "yes")
  121. || (isset($headers['x-spam-status']) && $headers['x-spam-status'] == "yes")
  122. ) {
  123. return true;
  124. }
  125. $_autoreplymail = array(
  126. "reply", "mailer-daemon", "delivery", "owner-", "request-", "bounce", "-confirm", "-errors", "donotreply",
  127. "postmaster", "daemon", "listserv", "majordom", "mailman", "mdaemon", "root"
  128. );
  129. foreach ($_autoreplymail as $entry) {
  130. if(
  131. (isset($headers['from']) && stripos($headers['from'],$entry))
  132. || (isset($headers['reply-to']) && stripos($headers['reply-to'],$entry))
  133. || (isset($headers['return-path']) &&stripos($headers['return-path'],$entry))
  134. ) {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. }