forked from opendocman/opendocman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldap.inc
188 lines (159 loc) · 9.61 KB
/
ldap.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
ldap.inc - Not Working - just a rough draft of ldap integration
Copyright (C) 2002-2010 Stephen Lawrence Jr.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
function draw_ldap_info($ldap_request)
{
$ds=ldap_connect($GLOBALS['CONFIG']['ldap_server']);
// ** IF Connected, send request. Takes the user input, puts asterisks around it for a larger query return. ** //
if ($ds)
{
$cn=" ".$ldap_request." ";
$cn = preg_replace ("/ /", "*", $cn);
$ldap_no_aster= preg_replace ("/\*/", " ", $ldap_request);
// ** $filter is the field it searches in the LDAP database. Compares against Employee Number first. **//
$filter="(|(employeenumber=$ldap_no_aster))";
// ** $justthese are the fields you want returned **//
$justthese = array("cn", "mail", "uid", "telephonenumber", "postaladdress", "ou", "title");
$r=ldap_bind($ds); #bind to directory
// ** ldap_search(connection, "Base DN", search fields, fields returned,0,[max returns]) **//
$sr=@ldap_search($ds,$GLOBALS['CONFIG']['ldap_basedn'], $filter,$justthese,0,300);
switch(ldap_count_entries($ds,$sr))
{
//** Only one entry returned, go directly to Detailed View **//
case 1:
$info = ldap_get_entries($ds, $sr); #get the result
print"<table cellpadding=1 cellspacing=1 align='center'>";
for ($i=0; $i<$info["count"]; $i++)
{ #loop though ldap search result
print "<tr><td class=text><b>Name: </b></td><td class=text>";
eval("print \$info[\$i]['cn'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Title: </b></td><td class=text>";
eval("print \$info[\$i]['title'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Login ID: </b></td><td class=text>";
eval("print \$info[\$i]['uid'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Address: </b></td><td class=text>";
eval("print \$info[\$i]['postaladdress'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Department: </b></td><td class=text>";
eval("print \$info[\$i]['ou'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Telephone: </b></td><td class=text>";
eval("print \$info[\$i]['telephonenumber'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>E-Mail: </b></td><td class=text><a href=\"mailto:";
eval("print \$info[\$i]['mail'][0];"); #print email address for hyperlink
print "\"><font color='blue'>";
eval("print \$info[\$i]['mail'][0];"); #print email address for viewing
print "</font></a></td></tr>";
}
print "</table>";
break;
//** No returns on Employee ID. Check against Common Name, Email, UCD Login ID, employeenumber **//
case 0:
$cn=" ".$ldap_request." ";
$cn = preg_replace ("/ /", "*", $cn);
$filter="(|(cn=$cn)(mail=$ldap_request)(uid=$ldap_no_aster)(employeenumber=$ldap_no_aster))";
$justthese = array("cn", "mail", "uid", "telephonenumber", "postaladdress", "ou", "title", "employeenumber");
$r=ldap_bind($ds); #bind to directory
$sr=@ldap_search($ds,"ou=University of California Davis,o=University of California,c=US", $filter,$justthese,0,300);
switch(true)
{
// ** If only one result, return in detail mode **//
case ldap_count_entries($ds,$sr)==1:
$info = ldap_get_entries($ds, $sr); #get the result
print"<table cellpadding=1 cellspacing=1 align='center'>";
for ($i=0; $i<$info["count"]; $i++)
{ #loop though ldap search result
print "<tr><td class=text><b>Name: </b></td><td class=text>";
eval("print \$info[\$i]['cn'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Title: </b></td><td class=text>";
eval("print \$info[\$i]['title'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Login ID: </b></td><td class=text>";
eval("print \$info[\$i]['uid'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Address: </b></td><td class=text>";
eval("print \$info[\$i]['postaladdress'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Department: </b></td><td class=text>";
eval("print \$info[\$i]['ou'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>Telephone: </b></td><td class=text>";
eval("print \$info[\$i]['telephonenumber'][0];"); #print attribute value
print "</td></tr>";
print "<tr><td class=text><b>E-Mail: </b></td><td class=text><a href=\"mailto:";
eval("print \$info[\$i]['mail'][0];"); #print email address for hyperlink
print "\"><font color='blue'>";
eval("print \$info[\$i]['mail'][0];"); #print email address for viewing
print "</font></a></td></tr>";
}
print "</table>";
break;
// ** If more than one result, sort results by lastname,firstname and return as LIST **//
case ldap_count_entries($ds,$sr)>1:
// SORTING
function aSortBySecondIndex($multiArray, $secondIndex)
{
while (list($firstIndex, ) = each($multiArray))
$indexMap[$firstIndex] = $multiArray[$firstIndex][$secondIndex];
asort($indexMap);
while (list($firstIndex, ) = each($indexMap))
if (is_numeric($firstIndex))
$sortedArray[] = $multiArray[$firstIndex];
else $sortedArray[$firstIndex] = $multiArray[$firstIndex];
return $sortedArray;
}
$info = ldap_get_entries($ds, $sr); #get the result
$info = aSortBySecondIndex($info,"sn");
// ** Data sorted, return as list with "Details" hyperlink to see individuals **//
print"
<table width=500 align='center' callpadding=1 cellspacing=1>
<tr><td class='text'><b>Name</b></td><td class='text'><b>UCD ID</b></td><td class='text'><b>Email</b></td><td></td></tr>";
for ($i=0; $i<$info["count"]; $i++)
{ #loop though ldap search result
print"<tr>";
$detail=$info[$i]['employeenumber'][0];
print "<td class='text'>";
eval("print \$info[\$i]['cn'][0];"); #print attribute value
print " </td>";
print "<td class='text'>";
eval("print \$info[\$i]['uid'][0];"); #print attribute value
print " </td>";
print "<td class='text'>";
eval("print \$info[\$i]['mail'][0];"); #print attribute value
print " </td>";
//print "<td class='text'><a href='ucd_directory.php?ldap_request=$detail'><font color='blue'>[Detail]</font></a></td></tr>";
print "</tr>";
}
print "</table><BR><BR>";
break;
// ** if there has been a search, but no matches... **//
case $ldap_request:
print"<div class='text' align='center'>No matches were found.</div>";
break;
}
}
ldap_close($ds);
} else
{
echo "<h4>Unable to connect to LDAP
server</h4>";
}
}