You are here

function email_verify_enable_module in Email Verify 7.2

Same name and namespace in other branches
  1. 7 email_verify.admin.inc \email_verify_enable_module()

Checks the system for the capability to use this module.

@todo This function works, but it needs some thought and potential rework, now that it is not in email_verify.install.

1 call to email_verify_enable_module()
email_verify_admin_settings_submit in ./email_verify.admin.inc
Form submit function.

File

./email_verify.admin.inc, line 177
This is for the administrative settings for this module.

Code

function email_verify_enable_module() {
  $debugging_mode = variable_get('email_verify_debug_mode', FALSE);
  $date_time_format = variable_get('email_verify_debug_mode_date_format', 'long');
  $debugging_text = array();
  if ($debugging_mode) {
    $debugging_text[] = t('Beginning the system capability checks for the Email Verify module (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }

  // Check that fsockopen() works on port 25.
  // @see: http://drupal.org/node/147883
  //
  // The following is duplicated code from email_verify_check(). The reason it
  // exists is because in its original use case, it was being called on install,
  // and the code was not being reliable loaded in all cases. That situation no
  // longer exists, but before the code is just wholesale replaced by that
  // function call, it needs to be compared and tested, as some of it was
  // modified to work in this situation.
  //
  // If a previous enable found port 25 closed or fsockopen() disabled, don't
  // test it again. Testing can cause a long delay on module enable. Completely
  // uninstall and then re-install this module to re-test.
  if ($debugging_mode) {
    $debugging_text[] = t('Checking to see if the system capability checks have already been run (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  if (variable_get('email_verify_skip_mailbox', FALSE)) {
    if ($debugging_mode) {
      $debugging_text[] = t('The system capability checks have already been run, so the system capability check is stopping (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
    return $debugging_text;
  }
  if ($debugging_mode) {
    $debugging_text[] = t('The system capability checks have not been run, so the system capability check is continuing (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }

  // Check if fsockopen() is disabled.
  if ($debugging_mode) {
    $debugging_text[] = t('Checking to see if fsockopen() is enabled or disabled (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  if (!function_exists('fsockopen')) {
    if ($debugging_mode) {
      $debugging_text[] = t('The fsockopen() function does not exist, so the system capability check is stopping (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
    $message = t('Email Verify will test email domains but not mailboxes because the fsockopen() function has been disabled.');
    variable_set('email_verify_skip_mailbox', TRUE);
    drupal_set_message($message, 'warning');
    return $debugging_text;
  }
  if ($debugging_mode) {
    $debugging_text[] = t('The fsockopen() function exists, so the system capability check is continuing (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  $host = variable_get('email_verify_test_options_host_name', 'drupal.org');
  if ($debugging_mode) {
    $debugging_text[] = t('The host that will be used for checking the system capability is "%host" (!date_time).', array(
      '%host' => $host,
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  if ($debugging_mode) {
    $debugging_text[] = t('Checking to see if Microsoft Windows compatible functions are needed (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    if ($debugging_mode) {
      $debugging_text[] = t('Microsoft Windows was detected, so the compatible functions are being loaded (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
    module_load_include('inc', 'email_verify', 'windows_compat');
  }
  else {
    if ($debugging_mode) {
      $debugging_text[] = t('Microsoft Windows was not detected, so the compatible functions are not being loaded (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
  }

  // What SMTP servers should we contact?
  if ($debugging_mode) {
    $debugging_text[] = t('Retrieving any MX records corresponding to the specified host "%host" (!date_time).', array(
      '%host' => $host,
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  $mx_hosts = array();
  if (!getmxrr($host, $mx_hosts)) {

    // When there is no MX record, the host itself should be used.
    if ($debugging_mode) {
      $debugging_text[] = t('No MX records were found, so the host itself will be used to check the system capability (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
    $mx_hosts[] = $host;
  }
  else {
    if ($debugging_mode) {
      $debugging_text[] = t('MX records were found, so they will be used to check the system capability (!date_time): !mx_hosts', array(
        '!date_time' => format_date(time(), $date_time_format),
        '!mx_hosts' => '<pre>' . print_r($mx_hosts, TRUE) . '</pre>',
      ));
    }
  }
  $timeout = variable_get('email_verify_test_options_timeout', 15);
  if ($debugging_mode) {
    $debugging_text[] = t('The timeout setting for checking the system capability is "%timeout" seconds (!date_time).', array(
      '%timeout' => $timeout,
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }

  // Try to connect to one SMTP server.
  if ($debugging_mode) {
    $debugging_text[] = t('Checking the host(s) to see if a connection can be made to any of them (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  foreach ($mx_hosts as $smtp) {
    $connect = @fsockopen($smtp, 25, $errno, $errstr, $timeout);
    if ($debugging_mode) {
      if ($connect === FALSE) {
        $debugging_text[] = t('The attempt to connect to host "%smtp" failed. If provided, the error number was "%errno", and the error string was "%errstr" (!date_time).', array(
          '%smtp' => $smtp,
          '%errno' => $errno,
          '%errstr' => $errstr,
          '!date_time' => format_date(time(), $date_time_format),
        ));
      }
      else {
        $debugging_text[] = t('The attempt to connect to host "%smtp" succeeded. If provided, the error number was "%errno", and the error string was "%errstr" (!date_time).', array(
          '%smtp' => $smtp,
          '%errno' => $errno,
          '%errstr' => $errstr,
          '!date_time' => format_date(time(), $date_time_format),
        ));
      }
    }
    if (!$connect) {
      if ($debugging_mode) {
        $debugging_text[] = t('The system could not connect to "%smtp" and is continuing to the next host in the list (!date_time).', array(
          '%smtp' => $smtp,
          '!date_time' => format_date(time(), $date_time_format),
        ));
      }
      if ($connect === FALSE && $errno === 0) {
        if ($debugging_mode) {
          $debugging_text[] = t('The result of fsockopen() was FALSE, and the error number was 0, which indicates a potential problem initializing the socket. This is the error string: "%errstr" (!date_time).', array(
            '%errstr' => $errstr,
            '!date_time' => format_date(time(), $date_time_format),
          ));
        }
        watchdog('email_verify', 'There was a potential problem initializing the socket when attempting to check an email address.', array(), WATCHDOG_WARNING);
      }
      continue;
    }
    if (preg_match("/^220/", fgets($connect, 1024))) {

      // OK, we have a SMTP connection.
      if ($debugging_mode) {
        $debugging_text[] = t('A connection was made to "%smtp", and so the system capability check is continuing (!date_time).', array(
          '%smtp' => $smtp,
          '!date_time' => format_date(time(), $date_time_format),
        ));
      }
      break;
    }
  }
  if (!$connect) {
    if ($debugging_mode) {
      $debugging_text[] = t('No connection could be made to any host. Domains will be checked for validity, but mailboxes cannot be checked (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
    variable_set('email_verify_skip_mailbox', TRUE);
    $message = t("Email Verify will test email domains but not mailboxes because port 25 is closed on your host's firewall for security.");
    watchdog('email_verify', $message, array(), WATCHDOG_WARNING);
    drupal_set_message($message, 'warning');
  }
  else {
    if ($debugging_mode) {
      $debugging_text[] = t('A connection was made to a host, so the system has passed the capability check (!date_time).', array(
        '!date_time' => format_date(time(), $date_time_format),
      ));
    }
  }
  if ($debugging_mode) {
    $debugging_text[] = t('Ending the system capability checks for the Email Verify module (!date_time).', array(
      '!date_time' => format_date(time(), $date_time_format),
    ));
  }
  return $debugging_text;
}