From 917ddcf5124b98086782cd58737061d87df6941e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 15 May 2015 14:19:43 -0500
Subject: [PATCH] Add support to disable email domain verification

---
 include/class.config.php                         |  6 ++++++
 include/class.staff.php                          |  2 +-
 include/class.validator.php                      | 12 +++++++++---
 include/i18n/en_US/help/tips/settings.email.yaml |  8 ++++++++
 include/staff/settings-emails.inc.php            |  9 +++++++++
 5 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/include/class.config.php b/include/class.config.php
index a33f231d6..870f2f6dd 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -165,6 +165,7 @@ class OsticketConfig extends Config {
         'default_help_topic' => 0,
         'help_topic_sort_mode' => 'a',
         'client_verify_email' => 1,
+        'verify_email_addrs' => 1,
     );
 
     function OsticketConfig($section=null) {
@@ -615,6 +616,10 @@ class OsticketConfig extends Config {
          return $this->get('admin_email');
     }
 
+    function verifyEmailAddrs() {
+        return (bool) $this->get('verify_email_addrs');
+    }
+
     function getReplySeparator() {
         return $this->get('reply_separator');
     }
@@ -1000,6 +1005,7 @@ class OsticketConfig extends Config {
             'alert_email_id'=>$vars['alert_email_id'],
             'default_smtp_id'=>$vars['default_smtp_id'],
             'admin_email'=>$vars['admin_email'],
+            'verify_email_addrs'=>isset($vars['verify_email_addrs']) ? 1 : 0,
             'enable_auto_cron'=>isset($vars['enable_auto_cron'])?1:0,
             'enable_mail_polling'=>isset($vars['enable_mail_polling'])?1:0,
             'strip_quoted_reply'=>isset($vars['strip_quoted_reply'])?1:0,
diff --git a/include/class.staff.php b/include/class.staff.php
index 49df39127..e937871c1 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -753,7 +753,7 @@ implements EmailContact {
         elseif(($uid=Staff::getIdByUsername($vars['username'])) && $uid!=$id)
             $errors['username']=__('Username already in use');
 
-        if(!$vars['email'] || !Validator::is_email($vars['email']))
+        if(!$vars['email'] || !Validator::is_valid_email($vars['email']))
             $errors['email']=__('Valid email is required');
         elseif(Email::getIdByEmail($vars['email']))
             $errors['email']=__('Already in use system email');
diff --git a/include/class.validator.php b/include/class.validator.php
index fcb350126..88075ab45 100644
--- a/include/class.validator.php
+++ b/include/class.validator.php
@@ -156,14 +156,20 @@ class Validator {
                 return false;
         }
 
-        if ($verify && !checkdnsrr($m->host, 'MX'))
-            return false;
+        // According to RFC2821, the domain (A record) can be treated as an
+        // MX if no MX records exist for the domain. Also, include a
+        // full-stop trailing char so that the default domain of the server
+        // is not added automatically
+        if ($verify and !count(dns_get_record($m->host.'.', DNS_MX)))
+            return 0 < count(dns_get_record($m->host.'.', DNS_A|DNS_AAAA));
 
         return true;
     }
 
     function is_valid_email($email) {
-        return self::is_email($email, false, true);
+        global $cfg;
+        // Default to FALSE for installation
+        return self::is_email($email, false, $cfg && $cfg->verifyEmailAddrs());
     }
 
     function is_phone($phone) {
diff --git a/include/i18n/en_US/help/tips/settings.email.yaml b/include/i18n/en_US/help/tips/settings.email.yaml
index ddad061bf..9b257975f 100644
--- a/include/i18n/en_US/help/tips/settings.email.yaml
+++ b/include/i18n/en_US/help/tips/settings.email.yaml
@@ -123,3 +123,11 @@ default_mta:
     content: >
         <span class="doc-desc-title">Default MTA</span> takes care of
         email delivery process for outgoing emails without SMTP setting.
+
+verify_email_addrs:
+    title: Verify Email Addresses
+    content: >
+        Enable this option to check if the email address has a mail
+        exchanger (MX) in the domain's DNS. This is useful to detect
+        incorrectly typed email addresses. This is perfomed in addition to
+        checking the email address wellformedness.
diff --git a/include/staff/settings-emails.inc.php b/include/staff/settings-emails.inc.php
index b10facabe..a20dfd661 100644
--- a/include/staff/settings-emails.inc.php
+++ b/include/staff/settings-emails.inc.php
@@ -82,6 +82,15 @@ if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin() || !$config)
                 <i class="help-tip icon-question-sign" href="#admins_email_address"></i>
             </td>
         </tr>
+        <tr>
+            <td width="180" class="required"><?php echo __("Verify Email Addresses");?>:</td>
+            <td>
+                <input type="checkbox" name="verify_email_addrs" <?php
+                    if ($config['verify_email_addrs']) echo 'checked="checked"'; ?>>
+                <?php echo __('Verify email address domain'); ?>
+                <i class="help-tip icon-question-sign" href="#verify_email_addrs"></i>
+            </td>
+        </tr>
         <tr><th colspan=2><em><strong><?php echo __('Incoming Emails'); ?>:</strong>&nbsp;
             </em></th>
         <tr>
-- 
GitLab