Skip to content

Commit 767e6bc

Browse files
author
Dan Bradley
committed
Remove physics-specific code from ldap lookup.
1 parent 7150d7b commit 767e6bc

File tree

1 file changed

+14
-133
lines changed

1 file changed

+14
-133
lines changed

people_ldap.php

+14-133
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
11
<?php
22

3-
function fixLdapCap($str) {
4-
$result = "";
5-
foreach( explode(" ",trim($str)) as $words ) {
6-
$ampwords = "";
7-
foreach( explode("&",$words) as $word ) {
8-
if( $word == "UW-ICECUBE" ) $word = "UW-IceCube";
9-
# only change cap if the word is all uppercase
10-
if( preg_match("/^[[:upper:]]*$/",$word) ) {
11-
$word = ucfirst(strtolower($word));
12-
}
13-
if( $ampwords ) $ampwords .= "&";
14-
$ampwords .= $word;
15-
}
16-
if( $result ) $result .= " ";
17-
$result .= $ampwords;
18-
}
19-
return $result;
20-
}
21-
22-
function fixLdapRoom($str) {
23-
$str = fixLdapCap($str);
24-
$str = preg_replace("/, Thomas C\$/","",$str);
25-
return $str;
26-
}
27-
283
function getLdapInfo($first,$middle,$last,$email,$netid) {
294

305
if( !$email && $netid ) {
316
$email = $netid . "@wisc.edu";
327
}
338

34-
$phys_ldap = "ldap.physics.wisc.edu";
359
$wisc_ldap = "ldap.services.wisc.edu";
3610

37-
$phys_info = "";
3811
$wisc_info = "";
3912
if( $email ) {
40-
$phys_info = ldapSearch("mail=$email",$phys_ldap);
4113
$wisc_info = ldapSearch("(&(mail=$email)(datasource=Payroll))",$wisc_ldap);
4214
if( !$wisc_info ) $wisc_info = ldapSearch("(&(mail=$email)(datasource=Student))",$wisc_ldap);
4315
}
@@ -46,57 +18,39 @@ function getLdapInfo($first,$middle,$last,$email,$netid) {
4618
$first = str_replace(".","",$first);
4719
$middle = str_replace(".","",$middle);
4820
$last = str_replace(".","",$last);
49-
if( !$phys_info ) $phys_info = ldapSearch("cn=$first $middle $last",$phys_ldap);
5021
if( !$wisc_info ) $wisc_info = ldapSearch("(&(cn=$first $middle $last)(datasource=Payroll))",$wisc_ldap);
5122
if( !$wisc_info ) $wisc_info = ldapSearch("(&(cn=$first $middle $last)(datasource=Student))",$wisc_ldap);
5223
}
5324

5425
$results = array();
5526

56-
# preferentially use wisc ldap title over physics ldap title, because Michelle Holland needs the official campus title in the gradvise committee listing
57-
if( $wisc_info && array_key_exists("title",$wisc_info) ) $results["title"] = fixLdapCap($wisc_info["title"][0]);
58-
else if( $phys_info && array_key_exists("title",$phys_info) ) $results["title"] = fixLdapCap($phys_info["title"][0]);
59-
6027
if( $wisc_info && array_key_exists("ou",$wisc_info) ) {
61-
$results["department"] = fixLdapCap($wisc_info["ou"][0]);
62-
if( $results["department"] == "GRADUATE SCHOOL" && isset($wisc_info["wiscedualldepartments"]) ) {
63-
# this happens sometimes for students with a fellowship; see if there is a better department to list
64-
foreach( $wisc_info["wiscedualldepartments"] as $department ) {
65-
if( $department != "GRADUATE SCHOOL" ) {
66-
$results["department"] = $department;
67-
break;
68-
}
69-
}
28+
$results["department"] = $wisc_info["ou"][0];
29+
if( array_key_exists("wiscedualldepartments",$wisc_info) ) {
30+
$results["all_departments"] = $wisc_info["wiscedualldepartments"];
7031
}
7132
}
72-
else if( $phys_info ) $results["department"] = "Physics";
7333

74-
if( $phys_info && array_key_exists("cn",$phys_info) ) $results["cn"] = $phys_info["cn"][0];
75-
else if( $wisc_info && array_key_exists("cn",$wisc_info) ) $results["cn"] = $wisc_info["cn"][0];
34+
if( $wisc_info && array_key_exists("cn",$wisc_info) ) $results["cn"] = $wisc_info["cn"][0];
7635

77-
if( $phys_info && array_key_exists("sn",$phys_info) ) $results["sn"] = $phys_info["sn"][0];
78-
else if( $wisc_info && array_key_exists("sn",$wisc_info) ) $results["sn"] = $wisc_info["sn"][0];
36+
if( $wisc_info && array_key_exists("sn",$wisc_info) ) $results["sn"] = $wisc_info["sn"][0];
7937

80-
if( $phys_info && array_key_exists("givenname",$phys_info) ) $results["givenname"] = $phys_info["givenname"][0];
81-
else if( $wisc_info && array_key_exists("givenname",$wisc_info) ) $results["givenname"] = $wisc_info["givenname"][0];
38+
if( $wisc_info && array_key_exists("givenname",$wisc_info) ) $results["givenname"] = $wisc_info["givenname"][0];
8239

83-
if( $phys_info && array_key_exists("mail",$phys_info) ) $results["mail"] = $phys_info["mail"][0];
84-
else if( $wisc_info && array_key_exists("mail",$wisc_info) ) $results["mail"] = $wisc_info["mail"][0];
40+
if( $wisc_info && array_key_exists("mail",$wisc_info) ) $results["mail"] = $wisc_info["mail"][0];
8541

86-
if( $phys_info && array_key_exists("telephonenumber",$phys_info) ) $results["telephonenumber"] = $phys_info["telephonenumber"][0];
87-
else if( $wisc_info && array_key_exists("telephonenumber",$wisc_info) ) $results["telephonenumber"] = $wisc_info["telephonenumber"][0];
42+
if( $wisc_info && array_key_exists("telephonenumber",$wisc_info) ) $results["telephonenumber"] = $wisc_info["telephonenumber"][0];
8843

89-
if( $phys_info && array_key_exists("roomnumber",$phys_info) ) $results["roomnumber"] = $phys_info["roomnumber"][0];
90-
else if( $wisc_info && array_key_exists("physicaldeliveryofficename",$wisc_info) ) $results["roomnumber"] = fixLdapRoom($wisc_info["physicaldeliveryofficename"][0]);
44+
if( $wisc_info && array_key_exists("physicaldeliveryofficename",$wisc_info) ) $results["roomnumber"] = $wisc_info["physicaldeliveryofficename"][0];
9145

9246
return $results;
9347
}
9448

95-
function ldapSearch($search,$server="ldap.physics.wisc.edu") {
49+
function ldapSearch($search,$server) {
9650
$ldap=ldap_connect($server);
9751

9852
if (!$ldap) {
99-
echo "Unable to connect to LDAP server\n";
53+
error_log("Unable to connect to LDAP server $server");
10054
return "";
10155
}
10256

@@ -109,7 +63,7 @@ function ldapSearch($search,$server="ldap.physics.wisc.edu") {
10963
}
11064
$sr=ldap_search($ldap, $base_dn, $search);
11165
if ( ldap_errno( $ldap ) != 0 ) {
112-
echo "Error querying LDAP server for $search\n";
66+
error_log("Error querying LDAP server $server for $search");
11367
ldap_close($ldap);
11468
return "";
11569
}
@@ -119,12 +73,12 @@ function ldapSearch($search,$server="ldap.physics.wisc.edu") {
11973
$info = ldap_get_entries($ldap, $sr);
12074

12175
if ( ldap_errno( $ldap) != 0 ) {
122-
echo "Error extracting ldap entries for $search\n";
76+
error_log("Error extracting ldap $server entries for $search");
12377
ldap_close($ldap);
12478
return "";
12579
}
12680
if ( $expected_count != $info["count"] ) {
127-
echo "Unexpected number of results in LDAP query $search\n";
81+
error_log("Unexpected number of results in LDAP $server query $search");
12882
ldap_close($ldap);
12983
return "";
13084
}
@@ -138,76 +92,3 @@ function ldapSearch($search,$server="ldap.physics.wisc.edu") {
13892

13993
return $info[0];
14094
}
141-
142-
function updateLdapInfo($person_id=null) {
143-
$dbh = connectDB();
144-
$sql = "SELECT ID,NAME,FIRST_NAME,MIDDLE_NAME,LAST_NAME,EMAIL,NETID FROM people WHERE LAST_LDAP_UPDATE < DATE_SUB(NOW(),INTERVAL 30 DAY)";
145-
if( $person_id ) $sql .= " AND ID = :ID";
146-
$stmt = $dbh->prepare($sql);
147-
if( $person_id ) $stmt->bindParam(":ID",$person_id);
148-
$stmt->execute();
149-
150-
$sql = "UPDATE people SET DEPARTMENT = :DEPARTMENT, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
151-
$update_dept_stmt = $dbh->prepare($sql);
152-
153-
$sql = "UPDATE people SET TITLE = :TITLE, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
154-
$update_title_stmt = $dbh->prepare($sql);
155-
156-
$sql = "UPDATE people SET NAME = :NAME, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
157-
$update_name_stmt = $dbh->prepare($sql);
158-
159-
$sql = "UPDATE people SET FIRST_NAME = :FIRST_NAME, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
160-
$update_first_name_stmt = $dbh->prepare($sql);
161-
162-
$sql = "UPDATE people SET LAST_NAME = :LAST_NAME, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
163-
$update_last_name_stmt = $dbh->prepare($sql);
164-
165-
$sql = "UPDATE people SET NETID = :NETID, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
166-
$update_netid_stmt = $dbh->prepare($sql);
167-
168-
$sql = "UPDATE people SET EMAIL = :EMAIL, LAST_LDAP_UPDATE = NOW() WHERE ID = :ID";
169-
$update_email_stmt = $dbh->prepare($sql);
170-
171-
while( ($row=$stmt->fetch()) ) {
172-
$ldap_info = getLdapInfo($row["FIRST_NAME"],$row["MIDDLE_NAME"],$row["LAST_NAME"],$row["EMAIL"],$row["NETID"]);
173-
if( !$ldap_info ) continue;
174-
175-
if( array_key_exists("department",$ldap_info) ) {
176-
$update_dept_stmt->bindValue(":ID",$row["ID"]);
177-
$update_dept_stmt->bindValue(":DEPARTMENT",$ldap_info["department"]);
178-
$update_dept_stmt->execute();
179-
}
180-
181-
if( array_key_exists("title",$ldap_info) ) {
182-
$update_title_stmt->bindValue(":ID",$row["ID"]);
183-
$update_title_stmt->bindValue(":TITLE",$ldap_info["title"]);
184-
$update_title_stmt->execute();
185-
}
186-
187-
# The primary source for name and email is InfoAccess, so only fill that in here if it is missing.
188-
189-
if( !$row["NAME"] && array_key_exists("cn",$ldap_info) ) {
190-
$update_name_stmt->bindValue(":ID",$row["ID"]);
191-
$update_name_stmt->bindValue(":NAME",$ldap_info["cn"]);
192-
$update_name_stmt->execute();
193-
}
194-
195-
if( !$row["FIRST_NAME"] && array_key_exists("givenname",$ldap_info) ) {
196-
$update_first_name_stmt->bindValue(":ID",$row["ID"]);
197-
$update_first_name_stmt->bindValue(":FIRST_NAME",$ldap_info["givenname"]);
198-
$update_first_name_stmt->execute();
199-
}
200-
201-
if( !$row["LAST_NAME"] && array_key_exists("sn",$ldap_info) ) {
202-
$update_last_name_stmt->bindValue(":ID",$row["ID"]);
203-
$update_last_name_stmt->bindValue(":LAST_NAME",$ldap_info["sn"]);
204-
$update_last_name_stmt->execute();
205-
}
206-
207-
if( !$row["EMAIL"] && array_key_exists("mail",$ldap_info) ) {
208-
$update_email_stmt->bindValue(":ID",$row["ID"]);
209-
$update_email_stmt->bindValue(":EMAIL",$ldap_info["mail"]);
210-
$update_email_stmt->execute();
211-
}
212-
}
213-
}

0 commit comments

Comments
 (0)