1
1
<?php
2
2
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
-
28
3
function getLdapInfo ($ first ,$ middle ,$ last ,$ email ,$ netid ) {
29
4
30
5
if ( !$ email && $ netid ) {
31
6
$ email = $ netid . "@wisc.edu " ;
32
7
}
33
8
34
- $ phys_ldap = "ldap.physics.wisc.edu " ;
35
9
$ wisc_ldap = "ldap.services.wisc.edu " ;
36
10
37
- $ phys_info = "" ;
38
11
$ wisc_info = "" ;
39
12
if ( $ email ) {
40
- $ phys_info = ldapSearch ("mail= $ email " ,$ phys_ldap );
41
13
$ wisc_info = ldapSearch ("(&(mail= $ email)(datasource=Payroll)) " ,$ wisc_ldap );
42
14
if ( !$ wisc_info ) $ wisc_info = ldapSearch ("(&(mail= $ email)(datasource=Student)) " ,$ wisc_ldap );
43
15
}
@@ -46,57 +18,39 @@ function getLdapInfo($first,$middle,$last,$email,$netid) {
46
18
$ first = str_replace (". " ,"" ,$ first );
47
19
$ middle = str_replace (". " ,"" ,$ middle );
48
20
$ last = str_replace (". " ,"" ,$ last );
49
- if ( !$ phys_info ) $ phys_info = ldapSearch ("cn= $ first $ middle $ last " ,$ phys_ldap );
50
21
if ( !$ wisc_info ) $ wisc_info = ldapSearch ("(&(cn= $ first $ middle $ last)(datasource=Payroll)) " ,$ wisc_ldap );
51
22
if ( !$ wisc_info ) $ wisc_info = ldapSearch ("(&(cn= $ first $ middle $ last)(datasource=Student)) " ,$ wisc_ldap );
52
23
}
53
24
54
25
$ results = array ();
55
26
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
-
60
27
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 " ];
70
31
}
71
32
}
72
- else if ( $ phys_info ) $ results ["department " ] = "Physics " ;
73
33
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 ];
76
35
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 ];
79
37
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 ];
82
39
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 ];
85
41
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 ];
88
43
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 ];
91
45
92
46
return $ results ;
93
47
}
94
48
95
- function ldapSearch ($ search ,$ server= " ldap.physics.wisc.edu " ) {
49
+ function ldapSearch ($ search ,$ server ) {
96
50
$ ldap =ldap_connect ($ server );
97
51
98
52
if (!$ ldap ) {
99
- echo "Unable to connect to LDAP server \n" ;
53
+ error_log ( "Unable to connect to LDAP server $ server " ) ;
100
54
return "" ;
101
55
}
102
56
@@ -109,7 +63,7 @@ function ldapSearch($search,$server="ldap.physics.wisc.edu") {
109
63
}
110
64
$ sr =ldap_search ($ ldap , $ base_dn , $ search );
111
65
if ( ldap_errno ( $ ldap ) != 0 ) {
112
- echo "Error querying LDAP server for $ search\n" ;
66
+ error_log ( "Error querying LDAP server $ server for $ search" ) ;
113
67
ldap_close ($ ldap );
114
68
return "" ;
115
69
}
@@ -119,12 +73,12 @@ function ldapSearch($search,$server="ldap.physics.wisc.edu") {
119
73
$ info = ldap_get_entries ($ ldap , $ sr );
120
74
121
75
if ( ldap_errno ( $ ldap ) != 0 ) {
122
- echo "Error extracting ldap entries for $ search\n" ;
76
+ error_log ( "Error extracting ldap $ server entries for $ search" ) ;
123
77
ldap_close ($ ldap );
124
78
return "" ;
125
79
}
126
80
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" ) ;
128
82
ldap_close ($ ldap );
129
83
return "" ;
130
84
}
@@ -138,76 +92,3 @@ function ldapSearch($search,$server="ldap.physics.wisc.edu") {
138
92
139
93
return $ info [0 ];
140
94
}
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