PageRenderTime 222ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/public_html/lists/admin/commonlib/lib/maillib.php

https://github.com/samtuke/phplist
PHP | 182 lines | 145 code | 13 blank | 24 comment | 8 complexity | 7097f651ad10e4037e85ed16ed1421ce MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * library with mail functions
  4. *
  5. * this file is shared between the webbler and PHPlist via commonlib
  6. *
  7. * @package Common
  8. * @subpackage maillib
  9. */
  10. require_once dirname(__FILE__).'/accesscheck.php';
  11. function HTML2Text($text) {
  12. # strip HTML, and turn links into the full URL
  13. $text = preg_replace("/\r/","",$text);
  14. #$text = preg_replace("/\n/","###NL###",$text);
  15. $text = preg_replace("/<script[^>]*>(.*?)<\/script\s*>/is","",$text);
  16. $text = preg_replace("/<style[^>]*>(.*?)<\/style\s*>/is","",$text);
  17. # would prefer to use < and > but the strip tags below would erase that.
  18. # $text = preg_replace("/<a href=\"(.*?)\"[^>]*>(.*?)<\/a>/is","\\2\n{\\1}",$text,100);
  19. # $text = preg_replace("/<a href=\"(.*?)\"[^>]*>(.*?)<\/a>/is","[URLTEXT]\\2[/URLTEXT][LINK]\\1[/LINK]",$text,100);
  20. $text = preg_replace("/<a[^>]*href=[\"\'](.*)[\"\'][^>]*>(.*)<\/a>/Umis","[URLTEXT]\\2[ENDURLTEXT][LINK]\\1[ENDLINK]\n",$text);
  21. $text = preg_replace("/<b>(.*?)<\/b\s*>/is","*\\1*",$text);
  22. $text = preg_replace("/<h[\d]>(.*?)<\/h[\d]\s*>/is","**\\1**\n",$text);
  23. # $text = preg_replace("/\s+/"," ",$text);
  24. $text = preg_replace("/<i>(.*?)<\/i\s*>/is","/\\1/",$text);
  25. $text = preg_replace("/<\/tr\s*?>/i","<\/tr>\n\n",$text);
  26. $text = preg_replace("/<\/p\s*?>/i","<\/p>\n\n",$text);
  27. $text = preg_replace("/<br[^>]*?>/i","<br>\n",$text);
  28. $text = preg_replace("/<br[^>]*?\/>/i","<br\/>\n",$text);
  29. $text = preg_replace("/<table/i","\n\n<table",$text);
  30. $text = strip_tags($text);
  31. # find all URLs and replace them back
  32. preg_match_all('~\[URLTEXT\](.*)\[ENDURLTEXT\]\[LINK\](.*)\[ENDLINK\]~Umis', $text, $links);
  33. foreach ($links[0] as $matchindex => $fullmatch) {
  34. $linktext = $links[1][$matchindex];
  35. $linkurl = $links[2][$matchindex];
  36. # check if the text linked is a repetition of the URL
  37. if (trim($linktext) == trim($linkurl) ||
  38. 'http://'.trim($linktext) == trim($linkurl)) {
  39. $linkreplace = $linkurl;
  40. } else {
  41. ## if link is an anchor only, take it out
  42. if (strpos($linkurl,'#') !== false) {
  43. $linkreplace = $linktext;
  44. } else {
  45. $linkreplace = $linktext.' <'.$linkurl.'>';
  46. }
  47. }
  48. # $text = preg_replace('~'.preg_quote($fullmatch).'~',$linkreplace,$text);
  49. $text = str_replace($fullmatch,$linkreplace,$text);
  50. }
  51. $text = preg_replace("/<a href=[\"\'](.*?)[\"\'][^>]*>(.*?)<\/a>/is","[URLTEXT]\\2[ENDURLTEXT][LINK]\\1[ENDLINK]",$text,500);
  52. $text = replaceChars($text);
  53. $text = preg_replace("/###NL###/","\n",$text);
  54. $text = preg_replace("/\n /","\n",$text);
  55. $text = preg_replace("/\t/"," ",$text);
  56. # reduce whitespace
  57. while (preg_match("/ /",$text)) {
  58. $text = preg_replace("/ /"," ",$text);
  59. }
  60. while (preg_match("/\n\s*\n\s*\n/",$text)) {
  61. $text = preg_replace("/\n\s*\n\s*\n/","\n\n",$text);
  62. }
  63. $text = wordwrap($text,70);
  64. return $text;
  65. }
  66. function replaceChars($text) {
  67. // $document should contain an HTML document.
  68. // This will remove HTML tags, javascript sections
  69. // and white space. It will also convert some
  70. // common HTML entities to their text equivalent.
  71. $search = array ("'&(quot|#34);'i", // Replace html entities
  72. "'&(amp|#38);'i",
  73. "'&(lt|#60);'i",
  74. "'&(gt|#62);'i",
  75. "'&(nbsp|#160);'i",
  76. "'&(iexcl|#161);'i",
  77. "'&(cent|#162);'i",
  78. "'&(pound|#163);'i",
  79. "'&(copy|#169);'i",
  80. "'&rsquo;'i",
  81. "'&ndash;'i",
  82. // "'&#(\d+);'e" // evaluate as php, deprecated in 5.5 and up
  83. );
  84. $replace = array ("\"",
  85. "&",
  86. "<",
  87. ">",
  88. " ",
  89. chr(161),
  90. chr(162),
  91. chr(163),
  92. chr(169),
  93. "'",
  94. "-",
  95. // "chr(\\1)"
  96. );
  97. $text = preg_replace ($search, $replace, $text);
  98. # eze
  99. # $text = html_entity_decode ( $text , ENT_QUOTES , $GLOBALS['strCharSet'] );
  100. $text = html_entity_decode ( $text , ENT_QUOTES , 'UTF-8' );
  101. return $text;
  102. }
  103. function addAbsoluteResources($text,$url) {
  104. $parts = parse_url($url);
  105. $tags = array('src\s*=\s*','href\s*=\s*','action\s*=\s*',
  106. 'background\s*=\s*','@import\s+','@import\s+url\(');
  107. foreach ($tags as $tag) {
  108. # preg_match_all('/'.preg_quote($tag).'"([^"|\#]*)"/Uim', $text, $foundtags);
  109. # we're only handling nicely formatted src="something" and not src=something, ie quotes are required
  110. # bit of a nightmare to not handle it with quotes.
  111. preg_match_all('/('.$tag.')"([^"|\#]*)"/Uim', $text, $foundtags);
  112. for ($i=0; $i< count($foundtags[0]); $i++) {
  113. $match = $foundtags[2][$i];
  114. $tagmatch = $foundtags[1][$i];
  115. # print "$match<br/>";
  116. if (preg_match("#^(http|javascript|https|ftp|mailto):#i",$match)) {
  117. # scheme exists, leave it alone
  118. } elseif (preg_match("#\[.*\]#U",$match)) {
  119. # placeholders used, leave alone as well
  120. } elseif (preg_match("/^\//",$match)) {
  121. # starts with /
  122. $text = preg_replace('#'.preg_quote($foundtags[0][$i]).'#im',$tagmatch.'"'.$parts["scheme"].'://'.$parts["host"].$match.'"',$text,1);
  123. } else {
  124. $path = '';
  125. if (isset($parts['path'])) {
  126. $path = $parts["path"];
  127. }
  128. if (!preg_match('#/$#',$path)) {
  129. $pathparts = explode('/',$path);
  130. array_pop($pathparts);
  131. $path = join('/',$pathparts);
  132. $path .= '/';
  133. }
  134. $text = preg_replace('#'.preg_quote($foundtags[0][$i]).'#im',
  135. $tagmatch.'"'.$parts["scheme"].'://'.$parts["host"].$path.$match.'"',$text,1);
  136. }
  137. }
  138. }
  139. # $text = preg_replace('#PHPSESSID=[^\s]+
  140. return $text;
  141. }
  142. /**
  143. * Retrieve a URL and send the contents as an HTML email
  144. *
  145. * @param string $sTo
  146. * @param string $sFrom
  147. * @param string $sSubject
  148. * @param string $sUrl
  149. * @return boolean Succes / failure
  150. */
  151. function mailURL( $sTo, $sFrom, $sSubject, $sUrl ) {
  152. if ( $sHtmlBody = file_get_contents($sUrl) ) {
  153. $sHtmlBody = addAbsoluteResources( $sHtmlBody, sprintf( 'http://%s/', $_SERVER['HTTP_HOST']) );
  154. $sTextBody = HTML2Text( $sHtmlBody );
  155. return htmlEmail( $sTo, $sFrom, $sSubject, $sTextBody, '', $sFrom, $sHtmlBody);
  156. } else {
  157. Error( "URL $sUrl could not be opened" );
  158. return false;
  159. }
  160. }
  161. ?>