1 <?php // -*-php-*- rcs_id('$Id: LdapSearch.php,v 1.6 2007-08-25 18:06:46 rurban Exp $');
3 Copyright 2004 John Lines
4 Copyright 2007 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * WikiPlugin which searches an LDAP directory.
26 * Uses the config.ini constants as defaults.
27 * See http://phpwiki.org/LdapSearchPlugin
28 * TODO: Return a pagelist on certain attributes
39 <?plugin LdapSearch host=ldap.example.com filter="(ou=web-team)"
40 attributes="sn cn telephonenumber" ?>
41 <?plugin LdapSearch host="ldap.itd.umich.edu" basedn="" filter="(sn=jensen)" attributes="cn drink" ?>
42 <?plugin LdapSearch host=ldap.example.com attributes="cn sn telephonenumber" ?>
43 <?plugin LdapSearch host=bugs.debian.org port=10101 basedn="dc=current,dc=bugs,dc=debian,dc=org"
44 filter="(debbugsPackage=phpwiki)"
45 attributes="debbugsSeverity debbugsState debbugsTitle" ?>
50 class WikiPlugin_LdapSearch
54 return _("LdapSearch");
56 function getDescription () {
57 return _("Search an LDAP directory");
60 function getVersion() {
61 return preg_replace("/[Revision: $]/", '',
64 function getDefaultArguments() {
65 return array('host' => "", // default: LDAP_AUTH_HOST
66 'port' => 389, // ignored if host = full uri
67 'basedn' => "", // LDAP_BASE_DN
76 // I ought to require the ldap extension, but fail sanely, if I cant get it.
77 // - however at the moment this seems to work as is
78 function run($dbi, $argstr, $request) {
79 if (!function_exists('ldap_connect')) {
80 if (!loadPhpExtension('ldap'))
81 return $this->error(_("Missing ldap extension"));
83 $args = $this->getArgs($argstr, $request);
85 //include_once("lib/WikiUser/LDAP.php");
87 if (defined('LDAP_AUTH_HOST')) {
88 $host = LDAP_AUTH_HOST;
89 if (strstr(LDAP_AUTH_HOST, '://'))
95 if (strstr($host, '://'))
100 $connect = ldap_connect($host);
102 $connect = ldap_connect($host, $port);
104 return $this->error(_("Failed to connect to LDAP host"));
105 if (!$options and defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
106 if (!empty($GLOBALS['LDAP_SET_OPTION'])) {
107 $options = $GLOBALS['LDAP_SET_OPTION'];
111 foreach ($options as $key => $value) {
112 if (!ldap_set_option($connect, $key, $value))
113 $this->error(_("Failed to set LDAP $key $value"));
117 // special convenience: if host = LDAP_AUTH_HOST
118 // then take user and password from config.ini also
121 // required for Windows Active Directory Server
122 $bind = ldap_bind($connect, $user, $password);
124 $bind = ldap_bind($connect, $user);
125 } elseif (defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
127 if (LDAP_AUTH_PASSWORD)
128 // Windows Active Directory Server is strict
129 $r = ldap_bind($connect, LDAP_AUTH_USER, LDAP_AUTH_PASSWORD);
131 $r = ldap_bind($connect, LDAP_AUTH_USER);
132 else // anonymous bind
133 $bind = ldap_bind($connect);
134 } else { // other anonymous bind
135 $bind = ldap_bind($connect);
137 if (!$bind) return $this->error(_("Failed to bind LDAP host"));
138 if (!$basedn) $basedn = LDAP_BASE_DN;
139 $attr_array = array("");
141 $res = ldap_search($connect, $basedn, $filter);
143 $attr_array = split (" ",$attributes);
144 $res = ldap_search($connect, $basedn, $filter, $attr_array);
146 $entries = ldap_get_entries($connect, $res);
148 // If we were given attributes then we return them in the order given
150 if ( !$attributes ) {
151 $attr_array = array();
152 for ($i = 0; $i < $entries["count"]; $i++) {
154 for ($ii=0; $ii < $entries[$i]["count"]; $ii++){
155 $data = $entries[$i][$ii];
156 $attr_array[] = $data;
160 for ($i=0; $i < count($attr_array) ; $i++) { $attrcols[$i] = 0; }
161 // Work out how many columns we need for each attribute. objectclass has more
162 for ($i = 0; $i < $entries["count"]; $i++) {
163 for ($ii=0; $ii<$entries[$i]["count"]; $ii++){
164 $data = $entries[$i][$ii];
165 $datalen = $entries[$i][$data]["count"];
166 if ($attrcols[$ii] < $datalen) {
167 $attrcols[$ii] = $datalen;
173 for ($i=0; $i < count($attr_array) ; $i++) {
174 if ($attrcols[$i] > 1)
175 $row->pushContent(HTML::th(array('colspan' => $attrcols[$i]), $attr_array[$i]));
177 $row->pushContent(HTML::th(array(), $attr_array[$i]));
179 $html->pushContent($row);
181 // Print the data rows
182 for ($i = 0; $i < $entries["count"]; $i++) {
183 $row = HTML::tr(); $nc=0;
184 for ($ii=0; $ii < count($attr_array); $ii++){
185 $data = @$entries[$i][$attr_array[$ii]];
186 if ($data and $data["count"] > 0) {
187 for ($iii=0; $iii < $data["count"]; $iii++) {
188 $row->pushContent(HTML::td($data[$iii])); $nc++;
191 $row->pushContent(HTML::td("")); $nc++;
194 // Make up some blank cells if required to pad this row
195 for ( $j=0 ; $j < ($attrcols[$ii] - $nc); $j++ ) {
196 $row->pushContent(HTML::td(""));
199 $html->pushContent($row);
201 return HTML::table(array('cellpadding' => 1,'cellspacing' => 1, 'border' => 1), $html);
205 // $Log: not supported by cvs2svn $
206 // Revision 1.5 2007/01/22 23:50:00 rurban
207 // Improve Table for no attributes
209 // Revision 1.4 2007/01/21 23:23:49 rurban
210 // Improve LdapSearch
212 // Revision 1.3 2004/12/20 16:05:14 rurban
213 // gettext msg unification
215 // Revision 1.2 2004/10/04 23:39:34 rurban
218 // Revision 1.1 2004/09/23 12:28:12 rurban
219 // initial checkin from http://phpwiki.org/LdapSearchPlugin
228 // c-hanging-comment-ender-p: nil
229 // indent-tabs-mode: nil