Index: cas.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cas/cas.module,v
retrieving revision 1.74.2.1
diff -u -p -w -r1.74.2.1 cas.module
--- cas.module	16 Jul 2010 00:36:58 -0000	1.74.2.1
+++ cas.module	7 Sep 2010 19:14:33 -0000
@@ -123,8 +123,22 @@ function cas_login_check() {
   $cas_force_login = _cas_force_login(); 
   
   if ($cas_force_login || $cas_check_first) {
-    // Calls phpCAS library.
-    require_once ('CAS/CAS.php');
+
+    // Gets path to CAS library, performs a basic sanity check.
+    if (module_exists('libraries')) {
+      $filename = libraries_get_path('CAS');
+    }
+    else {
+      $filename = variable_get('cas_library_dir', 'CAS');
+    }
+    $filename .= '/CAS.php';
+    if (file_exists($filename)) {
+      require_once $filename;
+    }
+    else {
+      drupal_set_message(t('Error loading CAS library.'));
+      return;
+    }
 
     // Variable set
     $cas_user_register = variable_get('cas_user_register', 1);
@@ -400,10 +414,135 @@ function cas_menu() {
 }
 
 /**
+ * Returns the version of phpCAS installed on the server.
+ *
+ * @param $dir
+ *   A string containing the directory the CAS.php file resides in.
+ * @param &$error
+ *   Returns a human readable message as to why the version could not be
+ *   retrieved, empty string if there are no errors.
+ *
+ * @return
+ *   A string containing the version, FALSE on errors.
+ */
+function cas_library_version_get($dir, &$error = NULL) {
+  $error = '';
+
+  // Gets path to library directory, path to the CAS.php file.
+  $dirname  = rtrim($dir, '/');
+  $filename = $dirname .'/CAS.php';
+
+  // Validates file and returns version.
+  if (empty($dir)) {
+    $error = t('Directory required.');
+  }
+  elseif (!is_dir($dirname)) {
+    $error = t(
+      'The directory %dirname does not exist.',
+      array('%dirname' => $dirname)
+    );
+  }
+  elseif (!file_exists($filename)) {
+    $error = t('The <em>CAS.php</em> file was not found.');
+  }
+  elseif (!is_readable($filename)) {
+    $error = t('The <em>CAS.php</em> file is not readable.');
+  }
+  else {
+    require_once $filename;
+    if (!class_exists('phpCAS')) {
+      $error = t('The <em>CAS.php</em> file was loaded, but the <em>phpCAS</em> class was not found.');
+    }
+    elseif (!defined('PHPCAS_VERSION')) {
+      $error = t('The phpCAS library was loaded, but the version could not be determined.');
+    }
+    return PHPCAS_VERSION;
+  }
+
+  // If we get to this point, there was an error.
+  return FALSE;
+}
+
+/**
+ * Checks that the library is installed in the location specified by loading the
+ * class and extracting the version.
+ *
+ * @param $form_element
+ *   The form element containing the "library" fieldset.
+ * @param $form_state
+ *   An array containing the form's state information.
+ *
+ * @return
+ *   The modified form element containing the "library" fieldset.
+ */
+function cas_library_version_check($form_element, $form_state) {
+  if ($version = cas_library_version_get($form_state['values']['cas_library_dir'], $error)) {
+    $form_element['library']['cas_library_version'] = array(
+      '#title' => t('Library version'),
+      '#value' => '<pre>'. check_plain($version) .'</pre>',
+      '#description' => t('The phpCAS library was loaded and returned this version information.'),
+    );
+  }
+  else {
+    $form_element['library'] = array(
+      '#value' => '<p class="error">'. $error .'</p>',
+    );
+  }
+  $form_element['library']['cas_library_version']['#type'] = 'item';
+  return $form_element;
+}
+
+/**
+ * Validates the CAS library version.
+ *
+ * @param $element
+ *   An array containing the element being validated.
+ *
+ * @return
+ *   A boolean flagging whether or not the element is valid.
+ */
+function cas_library_dir_validate($element) {
+  if ($version = cas_library_version_get($element['#value'])) {
+    return TRUE;
+  }
+  else {
+    form_set_error($element['#parents'][0], t('CAS library directory not valid.'));
+    return FALSE;
+  }
+}
+
+/**
+ * Validates the cas_admin_settings form.
+ */
+function cas_admin_settings_validate(&$form, &$form_state) {
+  if (!module_exists('libraries')) {
+    form_set_value($form['library']['cas_library_dir'], rtrim($form_state['values']['cas_library_dir'], '/'), $form_state);
+  }
+}
+
+/**
  * Provides settings pages.
  */
 function cas_admin_settings() {
   
+  // Only displays path settings if Libraries API is not installed.
+  if (!module_exists('libraries')) {
+    $form['library'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('CAS library settings'),
+      '#collapsible' => TRUE,
+      '#after_build' => array('cas_library_version_check'),
+    );
+    $form['library']['cas_library_dir'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Library directory'),
+      '#default_value' => variable_get('cas_library_dir', 'CAS'),
+      '#required' => TRUE,
+      '#description' => t('Specify the path to the directory the CAS.php file resides in.'),
+      '#element_validate' => array('cas_library_dir_validate'),
+    );
+  }
+
   $form['server'] = array(
     '#type' => 'fieldset',
     '#title' => t('CAS server settings'),
