You are here

function simple_ldap_init in Simple LDAP 7.2

File

./simple_ldap.module, line 45
Main simple_ldap module file.

Code

function simple_ldap_init() {
  global $conf;

  // LDAP server connection parameters
  $host = variable_get('simple_ldap_host', '');
  $port = variable_get('simple_ldap_port', '389');
  $starttls = variable_get('simple_ldap_starttls', FALSE);
  $binddn = variable_get('simple_ldap_binddn', NULL);
  $bindpw = variable_get('simple_ldap_bindpw', NULL);
  if (empty($host) || empty($port)) {
    return;
  }
  if ($ldap = ldap_connect($host, (int) $port)) {
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

    // TODO: Support StartTLS
    if (@ldap_bind($ldap, $binddn, $bindpw)) {
      ldap_unbind($ldap);
      variable_set('simple_ldap_last_saw_ldap', time());
      return;
    }
  }
  $last_good = variable_get('simple_ldap_last_saw_ldap', 0);
  $last_notify = variable_get('simple_ldap_last_down_notify', 0);
  $notify_frequency = variable_get('simple_ldap_ldap_down_notify_frequency', '30');

  // Send notifications
  $recipients = trim(implode(',', explode("\n", variable_get('simple_ldap_ldap_down_notify_list', NULL))));
  if (!empty($recipients)) {

    // Send if we've just seen it go down, or if it's still down and
    // it's been some number of minutes since the last cry for help.
    if ($last_good > $last_notify || $notify_frequency && time() > $last_notify + $notify_frequency * 60) {
      drupal_mail('simple_ldap', 'ldap_down', $recipients, 'en');
      variable_set('simple_ldap_last_down_notify', time());
    }
  }

  // Optionally, put the site into maintenance mode.
  if (variable_get('simple_ldap_ldap_down_maintenance_mode', TRUE)) {

    // Set $conf directly so the site will automatically come back when LDAP
    // is restored.
    $conf['maintenance_mode'] = 1;
    $conf['maintenance_mode_message'] = variable_get('simple_ldap_ldap_down_message', t('The site is experiencing technical difficulties.'));
  }

  // Tell the admins LDAP is down.
  if (user_access('access site in maintenance mode')) {
    drupal_set_message(t('LDAP OFFLINE: Cannot connect to any LDAP servers. Check <a href="@ldap_config">the LDAP configuration</a>', array(
      '@ldap_config' => url('admin/config/people/simple_ldap/server'),
    )), 'error');
  }
}