--- httpauth/httpauth.module	2008-06-27 15:51:57.000000000 +0300
+++ httpauth/httpauth.module	2008-09-13 01:07:13.000000000 +0300
@@ -74,6 +74,12 @@
   
   $form['httpauth_pages'] = array('#type' => 'textarea', '#title' => t('Promote HTTP authentication on pages'), '#default_value' => variable_get('httpauth_pages', ''), '#description' => t('On which pages to promote HTTP authentication, if an anonymous user stumbles upon an access denied page. Enter one page per line as a Drupal path. The * character is a wildcard.'));
   
+  $form['login'] = array('#type' => 'fieldset', '#title' => t('Alternative login function'), '#description' => t('In case Drupal uses external authentication, an alternative login function can be defined to take over the user_authenticate(). This function will not be used to authenticate a user with the UID=1.'), '#collapsible' => TRUE, '#collapsed' => TRUE);
+  $form['login']['httpauth_login_function'] = array('#type' => 'textfield', '#title' => t('Alternative login function'), '#default_value' => variable_get('httpauth_login_function', ''), '#description' => t('An alternative function to user_authenticate() to be called for authentication. The function should take and return the same variables as the user_authenticate() function. Leave empty if unsure.'));
+  $form['login']['httpauth_login_module'] = array('#type' => 'textfield', '#title' => t('A module providing login function'), '#default_value' => variable_get('httpauth_login_module', ''), '#description' => t('Name of the module which contains the login function. The module will be included before executing the login function.'));
+  $form['login']['httpauth_login_module_includes'] = array('#type' => 'textarea', '#title' => t('Module\'s include files'), '#default_value' => variable_get('httpauth_login_module_includes', ''), '#description' => t('Additional login module\'s include files which need to be loaded before executing the login function. One path related to module per line.'));
+  $form['login']['httpauth_login_includes'] = array('#type' => 'textarea', '#title' => t('Include files'), '#default_value' => variable_get('httpauth_login_includes', 'includes/common.inc'), '#description' => t('Include files which are not yet loaded at httpauth_boot(), but are needed by the login function. One path per line. Most probably "includes/common.inc" will be needed as it contains drupal_get_path() function which is used to load a custom module with the authentication function.'));
+  
   $form = system_settings_form($form);
   $form['#submit'][] = 'httpauth_settings_form_submit';
   
@@ -143,7 +149,23 @@
       httpauth_unauthorized();
       exit;
     }
-    user_authenticate(array('name' => $name, 'pass' => $pass));
+
+    $is_admin = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s' AND uid = '1'", $name));
+    foreach (array_filter(array_map('rtrim', explode("\n", variable_get('httpauth_login_includes', '')))) as $path) {
+      require_once($path);
+    }
+    if ($module = variable_get('httpauth_login_module', '')) {
+      foreach (array_filter(array_map('rtrim', explode("\n", variable_get('httpauth_login_module_includes', '')))) as $path) {
+        include_once(drupal_get_path('module', $module) .'/'. $path);
+      }
+      include_once(drupal_get_path('module', $module) .'/'. $module .'.module');
+    }
+    if (!$is_admin && ($login_function = variable_get('httpauth_login_function', '')) && function_exists($login_function)) {
+      $login_function(array('name' => $name, 'pass' => $pass));
+    }
+    else {
+      user_authenticate(array('name' => $name, 'pass' => $pass));
+    }
 
     // Was authentication successful?
     if ($user->uid) {
