Нет описания

ModelValidator.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Util\Helpers;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. class ModelValidator extends \MailPoetVendor\Sudzy\Engine {
  7. public $validators;
  8. const EMAIL_MIN_LENGTH = 6;
  9. const EMAIL_MAX_LENGTH = 150;
  10. const ROLE_EMAILS = [
  11. 'abuse',
  12. 'compliance',
  13. 'devnull',
  14. 'dns',
  15. 'ftp',
  16. 'hostmaster',
  17. 'inoc',
  18. 'ispfeedback',
  19. 'ispsupport',
  20. 'list-request',
  21. 'list',
  22. 'maildaemon',
  23. 'noc',
  24. 'no-reply',
  25. 'noreply',
  26. 'nospam',
  27. 'null',
  28. 'phish',
  29. 'phishing',
  30. 'postmaster',
  31. 'privacy',
  32. 'registrar',
  33. 'root',
  34. 'security',
  35. 'spam',
  36. 'sysadmin',
  37. 'undisclosed-recipients',
  38. 'unsubscribe',
  39. 'usenet',
  40. 'uucp',
  41. 'webmaster',
  42. 'www',
  43. ];
  44. public function __construct() {
  45. parent::__construct();
  46. $this->validators = [
  47. 'validEmail' => 'validateEmail',
  48. 'validRenderedNewsletterBody' => 'validateRenderedNewsletterBody',
  49. ];
  50. $this->setupValidators();
  51. }
  52. private function setupValidators() {
  53. $_this = $this;
  54. foreach ($this->validators as $validator => $action) {
  55. $this->addValidator($validator, function($params) use ($action, $_this) {
  56. $callback = [$_this, $action];
  57. if (is_callable($callback)) {
  58. return call_user_func($callback, $params);
  59. }
  60. });
  61. }
  62. }
  63. public function validateEmail($email) {
  64. $permittedLength = (strlen($email) >= self::EMAIL_MIN_LENGTH && strlen($email) <= self::EMAIL_MAX_LENGTH);
  65. $validEmail = WPFunctions::get()->isEmail($email) !== false && parent::_isEmail($email, null);
  66. return ($permittedLength && $validEmail);
  67. }
  68. public function validateNonRoleEmail($email) {
  69. if (!$this->validateEmail($email)) return false;
  70. $firstPart = strtolower(substr($email, 0, (int)strpos($email, '@')));
  71. return array_search($firstPart, self::ROLE_EMAILS) === false;
  72. }
  73. public function validateRenderedNewsletterBody($newsletterBody) {
  74. if (is_serialized($newsletterBody)) {
  75. $newsletterBody = unserialize($newsletterBody);
  76. } else if (Helpers::isJson($newsletterBody)) {
  77. $newsletterBody = json_decode($newsletterBody, true);
  78. }
  79. return (is_null($newsletterBody) || (is_array($newsletterBody) && !empty($newsletterBody['html']) && !empty($newsletterBody['text'])));
  80. }
  81. public function validateIPAddress(string $ip): bool {
  82. return (bool)filter_var($ip, FILTER_VALIDATE_IP);
  83. }
  84. }