PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/mail/helper.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 168 lines | 65 code | 20 blank | 83 comment | 11 complexity | d5da0faa669cd0489d8a4712930f27f4 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: helper.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4. * @package Joomla.Framework
  5. * @subpackage Mail
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * E-Mail helper class, provides static methods to perform various tasks relevant
  18. * to the Joomla e-mail routines.
  19. *
  20. * TODO: Test these methods as the regex work is first run and not tested thoroughly
  21. *
  22. * @static
  23. * @author Louis Landry <louis.landry@joomla.org>
  24. * @package Joomla.Framework
  25. * @subpackage Mail
  26. * @since 1.5
  27. */
  28. class JMailHelper
  29. {
  30. /**
  31. * Cleans single line inputs.
  32. *
  33. * @static
  34. * @param string $value String to be cleaned.
  35. * @return string Cleaned string.
  36. */
  37. function cleanLine( $value ) {
  38. return trim( preg_replace( '/(%0A|%0D|\n+|\r+)/i', '', $value ) );
  39. }
  40. /**
  41. * Cleans multi-line inputs.
  42. *
  43. * @static
  44. * @param string $value Multi-line string to be cleaned.
  45. * @return string Cleaned multi-line string.
  46. */
  47. function cleanText( $value ) {
  48. return trim( preg_replace( '/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i', '', $value ) );
  49. }
  50. /**
  51. * Cleans any injected headers from the E-Mail body.
  52. *
  53. * @static
  54. * @param string $body E-Mail body string.
  55. * @return string Cleaned E-Mail body string.
  56. * @since 1.5
  57. */
  58. function cleanBody($body) {
  59. // Strip all E-Mail headers from a string
  60. return preg_replace("/((From:|To:|Cc:|Bcc:|Subject:|Content-type:) ([\S]+))/", "", $body);
  61. }
  62. /**
  63. * Cleans any injected headers from the subject string.
  64. *
  65. * @static
  66. * @param string $subject E-Mail subject string.
  67. * @return string Cleaned E-Mail subject string.
  68. * @since 1.5
  69. */
  70. function cleanSubject($subject) {
  71. return preg_replace("/((From:|To:|Cc:|Bcc:|Content-type:) ([\S]+))/", "", $subject);
  72. }
  73. /**
  74. * Verifies that an e-mail address does not have any extra headers injected into it.
  75. *
  76. * @static
  77. * @param string $address E-Mail address.
  78. * @return string|false E-Mail address string or boolean false if injected headers are present.
  79. * @since 1.5
  80. */
  81. function cleanAddress($address)
  82. {
  83. if (preg_match("[\s;,]", $address)) {
  84. return false;
  85. }
  86. return $address;
  87. }
  88. /**
  89. * Verifies that the string is in a proper e-mail address format.
  90. *
  91. * @static
  92. * @param string $email String to be verified.
  93. * @return boolean True if string has the correct format; false otherwise.
  94. * @since 1.5
  95. */
  96. function isEmailAddress($email)
  97. {
  98. // Split the email into a local and domain
  99. $atIndex = strrpos($email, "@");
  100. $domain = substr($email, $atIndex+1);
  101. $local = substr($email, 0, $atIndex);
  102. // Check Length of domain
  103. $domainLen = strlen($domain);
  104. if ($domainLen < 1 || $domainLen > 255) {
  105. return false;
  106. }
  107. // Check the local address
  108. // We're a bit more conservative about what constitutes a "legal" address, that is, A-Za-z0-9!#$%&\'*+/=?^_`{|}~-
  109. $allowed = 'A-Za-z0-9!#&*+=?_-';
  110. $regex = "/^[$allowed][\.$allowed]{0,63}$/";
  111. if ( ! preg_match($regex, $local) ) {
  112. return false;
  113. }
  114. // No problem if the domain looks like an IP address, ish
  115. $regex = '/^[0-9\.]+$/';
  116. if ( preg_match($regex, $domain)) {
  117. return true;
  118. }
  119. // Check Lengths
  120. $localLen = strlen($local);
  121. if ($localLen < 1 || $localLen > 64) {
  122. return false;
  123. }
  124. // Check the domain
  125. $domain_array = explode(".", $domain);
  126. $regex = '/^[A-Za-z0-9-]{0,63}$/';
  127. foreach ($domain_array as $domain ) {
  128. // Must be something
  129. if ( ! $domain ) {
  130. return false;
  131. }
  132. // Check for invalid characters
  133. if ( ! preg_match($regex, $domain) ) {
  134. return false;
  135. }
  136. // Check for a dash at the beginning of the domain
  137. if ( strpos($domain, '-' ) === 0 ) {
  138. return false;
  139. }
  140. // Check for a dash at the end of the domain
  141. $length = strlen($domain) -1;
  142. if ( strpos($domain, '-', $length ) === $length ) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. }