1 <?php // -*-php-*- rcs_id('$Id: LdapSearch.php,v 1.3 2004-12-20 16:05:14 rurban Exp $');
3 Copyright 2004 John Lines
5 This file is part of PhpWiki.
7 PhpWiki is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 PhpWiki is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with PhpWiki; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * WikiPlugin which searches an LDAP directory.
25 * Note that for this version the attributes are required.
26 * TODO: use the config.ini constants as defaults
27 * See http://phpwiki.org/LdapSearchPlugin
38 <?plugin LdapSearch host=ldap.example.com filter="(ou=web-team)"
39 attributes="sn cn telephonenumber" ?>
40 <?plugin LdapSearch host="ldap.itd.umich.edu" basedn="" filter="(sn=jensen)" attributes="cn drink" ?>
41 <?plugin LdapSearch host=ldap.example.com attributes="cn sn telephonenumber" ?>
42 <?plugin LdapSearch host=bugs.debian.org port=10101 basedn="dc=current,dc=bugs,dc=debian,dc=org"
43 filter="(debbugsPackage=phpwiki)"
44 attributes="debbugsSeverity debbugsState debbugsTitle" ?>
49 // Constants are defined before the class.
50 // if (!defined('THE_END'))
51 // define('THE_END', "!");
53 class WikiPlugin_LdapSearch
57 return _("LdapSearch");
59 function getDescription () {
60 return _("Search an LDAP directory");
63 function getVersion() {
64 return preg_replace("/[Revision: $]/", '',
67 function getDefaultArguments() {
68 return array('host' => "localhost", // change to LDAP_AUTH_HOST
69 'port' => 389, // ditto
70 'basedn' => "", // LDAP_BASE_DN
75 // I ought to require the ldap extension, but fail sanely, if I cant get it.
76 // - however at the moment this seems to work as is
77 function run($dbi, $argstr, $request) {
78 extract($this->getArgs($argstr, $request));
80 $html = HTML::table(array('cellpadding' => 1,'cellspacing' => 1, 'border' => 1));
81 $connect = ldap_connect($host, $port);
82 if (!ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3)) {
83 $this->error(_("Failed to set LDAP protocol version to 3"));
85 $bind = ldap_bind($connect);
86 $attr_array = array(""); // for now -
88 $res = ldap_search($connect, $basedn, $filter);
90 $attr_array = split (" ",$attributes);
91 $res = ldap_search($connect, $basedn, $filter,$attr_array);
93 $entries = ldap_get_entries($connect, $res);
95 // If we were given attributes then we return them in the order given
97 for ($i=0; $i < count($attr_array) ; $i++) { $attrcols[$i] = 0; }
98 // Work out how many columns we need for each attribute.
99 for ($i = 0; $i < $entries["count"]; $i++) {
100 for ($ii=0; $ii<$entries[$i]["count"]; $ii++){
101 $data = $entries[$i][$ii];
102 $datalen = $entries[$i][$data]["count"];
103 if ($attrcols[$ii] < $datalen ) {
104 $attrcols[$ii] = $datalen;
109 // Now print the headers
111 for ($i=0; $i < count($attr_array) ; $i++) {
112 $row->pushContent(HTML::th(array('colspan' => $attrcols[$i]), $attr_array[$i]));
114 $html->pushContent($row);
115 for ($i = 0; $i<$entries["count"]; $i++) {
116 // start a new row for every data value.
117 $row = HTML::tr(); $nc=0;
118 for ($ii=0; $ii < $entries[$i]["count"]; $ii++){
119 $data = $entries[$i][$ii];
120 // 3 possible cases for the values of each attribute.
121 switch ($entries[$i][$data]["count"]) {
123 $row->pushContent(HTML::td("")); $nc++;
126 for ($iii=0; $iii < $entries[$i][$data]["count"]; $iii++) {
127 $row->pushContent(HTML::td($entries[$i][$data][$iii])); $nc++;
130 // Make up some blank cells if required to pad this row
131 for ( $j=0 ; $j < ($attrcols[$ii] - $nc); $j++ ) {
132 $row->pushContent(HTML::td(""));
135 $html->pushContent($row);
139 // $ii = attributes for entry
140 // $iii = values per attribute
141 for ($i = 0; $i < $entries["count"]; $i++) {
143 for ($ii=0; $ii < $entries[$i]["count"]; $ii++){
144 $data = $entries[$i][$ii];
145 for ($iii=0; $iii < $entries[$i][$data]["count"]; $iii++) {
146 //echo $data.": ".$entries[$i][$data][$iii]."<br>";
147 if ( ! $attributes ) {
148 $row->pushContent(HTML::td($data));
150 $row->pushContent(HTML::td($entries[$i][$data][$iii]));
153 $html->pushContent($row);
162 // $Log: not supported by cvs2svn $
163 // Revision 1.2 2004/10/04 23:39:34 rurban
166 // Revision 1.1 2004/09/23 12:28:12 rurban
167 // initial checkin from http://phpwiki.org/LdapSearchPlugin
176 // c-hanging-comment-ender-p: nil
177 // indent-tabs-mode: nil