--- cas-module.bak/cas.module	2007-02-23 02:53:36.000000000 +0100
+++ modules/cas/cas.module	2007-02-23 02:52:12.000000000 +0100
@@ -8,6 +8,40 @@
 
 require_once('CAS/CAS.php');
 
+/*
+** LDAPAuth interfacing - BEGIN 
+*/
+
+/**
+* LDAP Auxiliary functions
+*/
+
+function _get_ldap_config_name ($user_name)
+{
+  include_once('modules/ldap_integration/ldapauth.module');
+  $user_found = false;
+  $result = db_query("SELECT name FROM {ldapauth} WHERE status = '%d' ORDER BY sid", 1);
+  while ($row = db_fetch_object($result)) {
+    // cycle thru the authentication schemes - first successful one wins
+    // instantiate ldap
+    _ldapauth_init($row->name); 
+    $ldap_user_entry = _ldapauth_user_lookup($user_name);
+    if ($ldap_user_entry)
+    {
+      $user_found = true;
+      break;
+    }
+  }
+  if ($user_found)
+    return $row->name;
+  else
+    return false;
+}
+
+/*
+** LDAPAuth interfacing - END
+*/
+
 /**
 * Implementation of hook_init
 * Traps a page load to see if authentication is required.
@@ -20,7 +54,8 @@
     //do nothing because user is already logged into Drupal
   }
   elseif ( _cas_force_login()){
-     $user_register = variable_get('user_register',1);
+     //$user_register = variable_get('user_register',1);
+     $user_register = 1;
      $cas_authmap = variable_get('cas_authmap',0);
      phpCAS::setDebug();
      $server_version   = (string)variable_get('cas_version', '2.0');
@@ -28,6 +63,7 @@
      $server_port      = (int)variable_get('cas_port', '443');
      $server_uri       = (string)variable_get('cas_uri', '');
      $cas_domain       = (string)variable_get('cas_domain','');
+     $cas_useldap      = variable_get('cas_useldap',0);
      $start_session    = (boolean)FALSE; // Drupal takes care of its own session
      cas_save_page();
      phpCAS::client($server_version, $server_cas_server, $server_port, $server_uri, $start_session);
@@ -70,6 +106,29 @@
            $user_default['authname_cas'] = $cas_name;
          if ($cas_domain)
            $user_default['mail'] = $cas_name . '@' . $cas_domain;
+
+         /*
+         ** LDAPAuth interfacing - BEGIN
+         */
+
+         if ($cas_useldap)
+         {
+           if ($ldap_config_name = _get_ldap_config_name($cas_name))
+           {
+             global $ldap;
+             _ldapauth_init($ldap_config_name);
+             $ldap->connect();
+             $cas_ldap_email_attribute = (string)variable_get('cas_ldap_email_attribute','mail');
+             $ldap_entries = $ldap->search($ldap->getOption('basedn'), $ldap->getOption('user_attr').'='.$cas_name, array($cas_ldap_email_attribute));
+             if ($ldap_entries['count']==1)
+               $user_default['mail'] = $ldap_entries[0][$cas_ldap_email_attribute][0];
+           }
+         }
+
+         /*
+         ** LDAPAuth interfacing - END
+         */
+
          $user = user_save("", $user_default);
          watchdog("user", "new user: $user->name (CAS)", l(t("edit user"), "admin/user/edit/$user->uid"));
          if(($user->uid) && ($user->uid > 0) && $cas_authmap)
@@ -82,6 +141,25 @@
 
    if($user->uid && $user->uid > 0) { // final check to make sure we have a good user
    {
+      /*
+      ** LDAPAuth interfacing - BEGIN
+      */
+
+      if (variable_get('cas_useldap_groups',''))
+      {
+        if ($ldap_config_name = _get_ldap_config_name($user->name))
+        {
+          _ldapauth_init($ldap_config_name);
+          include_once('modules/ldap_integration/ldapgroups.module');
+          $user->ldap_authentified = true;
+          ldapgroups_user_login($user);
+        }
+      }
+
+      /*
+      ** LDAPAuth interfacing - END
+      */
+
       drupal_set_message("Logged in via CAS as " . $user->name . ".");
       // We can't count on the menu because we're changing login states.
       cas_login_page();
@@ -120,7 +198,7 @@
   if ($may_cache) {
     $items[] = array(
       'path' => 'admin/user/cas',
-      'title' => t('cas settings'),
+      'title' => t('CAS settings'),
       'description' => 'Configure central authentication services',
       'callback' => 'drupal_get_form',
       'callback arguments' => 'cas_admin_settings',
@@ -160,6 +238,10 @@
   $form['pages'] = array('#type' => 'fieldset', '#title' => t('Redirection settings'),'#collapsible' => true,    '#collapsed' => true);
   $form['pages']['cas_access'] = array('#type' => 'radios', '#title' => t('Require Cas Login for '), '#default_value' => variable_get('cas_access',0), '#options' => array(t('specific pages'), t('all pages')));
   $form['pages']['cas_pages'] = array('#type' => 'textarea',  '#title' => t('Specific pages'),  '#default_value' => variable_get('cas_pages',''), '#cols' => 40,	'#rows' => 5, '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."));
+  $form['ldap'] = array('#type' => 'fieldset', '#title' => t('LDAP settings'),'#collapsible' => true,    '#collapsed' => true);
+  $form['ldap']['cas_useldap'] = array('#type' => 'checkbox', '#title' => t('Should we extract the user email from an LDAP directory?'), '#default_value' => variable_get('cas_useldap', 0), '#description' => t('Activate this option if you want to extract the user email from an LDAP directory. <strong>Ldapauth module must be enabled and configured</strong>.'));
+  $form['ldap']['cas_ldap_email_attribute'] = array('#type' => 'textfield',  '#title' => t('Email attribute'), '#default_value' => variable_get('cas_ldap_email_attribute', 'mail'), '#size' => 30,'#maxlength' => 55, '#description' => t('LDAP entry attribute containing the email address.'));
+  $form['ldap']['cas_useldap_groups'] = array('#type' => 'checkbox', '#title' => t('Should we extract user groups from an LDAP directory?'), '#default_value' => variable_get('cas_useldap_groups', 0), '#description' => t('Activate this option if you want to extract the user groups from an LDAP directory. <strong>Ldapgroups module must be enabled and configured</strong>.'));
   return system_settings_form($form);
 }
 /**
@@ -224,7 +306,7 @@
           // NOTE: special care needs to be taken because on pages with forms,
           // such as node and comment submission pages, the $edit variable
           // might already be set.
-          $output .= l('Login','cas');
+          $output .= l(t('Login'),'cas');
           $output .= "</div>\n";
           $block['subject'] = t('User Login');
           $block['content'] = $output;
@@ -268,10 +350,40 @@
   switch ($form_id){
     case 'user_edit':
       //make the email field hidden and force the value to the default.
-      if (variable_get('cas_hide_email',0) && variable_get('cas_domain',''))
+      if (variable_get('cas_hide_email',0))
       {
-        $form['account']['mail']['#type']='hidden';
-        $form['account']['mail']['#value']= $form['account']['name']['#default_value'].'@'.variable_get('cas_domain','');
+        if (variable_get('cas_domain',''))
+        {
+          $form['account']['mail']['#type']='hidden';
+          $form['account']['mail']['#value']= $form['account']['name']['#default_value'].'@'.variable_get('cas_domain','');
+        }
+ 
+        /*
+        ** LDAPAuth interfacing - BEGIN
+        */ 
+  
+        if (variable_get('cas_useldap',''))
+        {
+          global $ldap, $user;
+          if ($ldap_config_name = _get_ldap_config_name($user->name))
+          {
+            _ldapauth_init($ldap_config_name);
+            $ldap->connect();
+            $cas_ldap_email_attribute = (string)variable_get('cas_ldap_email_attribute','mail');
+            $ldap_entries = $ldap->search($ldap->getOption('basedn'), $ldap->getOption('user_attr').'='.$user->name, array($cas_ldap_email_attribute));
+            if ($ldap_entries['count']==1 && isset($ldap_entries[0][$cas_ldap_email_attribute][0]))
+            if (trim($ldap_entries[0][$cas_ldap_email_attribute][0])!='')
+            {
+              $form['account']['mail']['#type']='hidden';
+              $form['account']['mail']['#value']= $ldap_entries[0][$cas_ldap_email_attribute][0];
+            }
+          }
+        }
+
+        /*
+        ** LDAPAuth interfacing - END
+        */
+
       }
 
       //Remove the password fields from the form.
