From 017778f20d9d98c8c8e023cde0c41a6c88a274c8 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Mon, 6 Feb 2012 22:01:54 +0000
Subject: [PATCH] [gh-289] Merging from testing into staging -c 1433

git-svn-id: file:///tmp/wdn_thm_drupal/branches/drupal-7.x/staging@1445 20a16fea-79d4-4915-8869-1ea9d5ebf173
---
 .../all/modules/unl/unl_cas/unl_cas.admin.inc | 45 ++++++++++++++++---
 1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/sites/all/modules/unl/unl_cas/unl_cas.admin.inc b/sites/all/modules/unl/unl_cas/unl_cas.admin.inc
index 425172c5..3a5347b5 100644
--- a/sites/all/modules/unl/unl_cas/unl_cas.admin.inc
+++ b/sites/all/modules/unl/unl_cas/unl_cas.admin.inc
@@ -29,13 +29,48 @@ function unl_cas_user_import($form, &$form_state) {
     $matches = array();
     $search = $form_state['values']['name'];
     // TODO Use directory for now, use LDAP query once this app is granted access
-    $results = json_decode(file_get_contents('http://directory.unl.edu/service.php?q='.urlencode($search).'&format=json&method=getLikeMatches'));
+    try {
+      $ldap = new Unl_Ldap(unl_cas_get_setting('ldap_uri'));
+      $ldap->bind(unl_cas_get_setting('ldap_dn'), unl_cas_get_setting('ldap_password'));
+      
+      $searchFields = array('uid', 'mail', 'cn', 'givenName', 'sn', 'eduPersonNickname');
+      $filter = '(&';
+      foreach (preg_split('/\s+/', $search) as $searchTerm) {
+        $searchTerm = str_replace(array('"', ',', '*'), '', $searchTerm);
+        $filter .= '(|';
+        foreach ($searchFields as $searchField) {
+          $filter .= '(' . $searchField . '=*' . $searchTerm . '*)';
+        }
+        $filter .= ')';
+      }
+      $filter .= '(|(ou=people)(ou=guests)))';
+      
+      // Convert the uid to a string to match with peoplefinder results.
+      $results = $ldap->search('dc=unl,dc=edu', $filter);
+      foreach ($results as $key => $result) {
+        $results[$key]['uid'] = $result['uid'][0];
+      }
+    } catch (Exception $e) {
+      $results = json_decode(file_get_contents('http://directory.unl.edu/service.php?q='.urlencode($search).'&format=json&method=getLikeMatches'), TRUE);
+      
+      // Convert each key to lowercase to mimic the LDAP results.
+      foreach ($results as $index => $result) {
+        foreach ($result as $key => $value) {
+          $results[$index][strtolower($key)] = $value;
+        }
+      }
+    }
+    
+    
+    $ldap = new Unl_Ldap('ldap://ldap.unl.edu/');
     foreach ($results as $result) {
-      $displayName = $result->displayName->{0};
-      $uid = $result->uid;
-      $mail = $result->mail;
+      if (!$result['uid']) {
+        continue;
+      }
+      $displayName = $result['displayname'][0];
+      $uid = $result['uid'];
       $affiliations = array();
-      foreach ($result->eduPersonPrimaryAffiliation as $affiliation) {
+      foreach ($result['edupersonaffiliation'] as $affiliation) {
         $affiliations[] = $affiliation;
       }
       $affiliations = implode(', ', $affiliations);
-- 
GitLab