]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LdapSearch.php
Improve LdapSearch
[SourceForge/phpwiki.git] / lib / plugin / LdapSearch.php
1 <?php // -*-php-*- rcs_id('$Id: LdapSearch.php,v 1.4 2007-01-21 23:23:49 rurban Exp $');
2 /**
3  Copyright 2004 John Lines
4  Copyright 2007 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 /**
24  * WikiPlugin which searches an LDAP directory.
25  *
26  * Uses the config.ini constants as defaults.
27  * See http://phpwiki.org/LdapSearchPlugin
28  * TODO: Return a pagelist on certain attributes
29  *
30  * Usage Samples:
31   <?plugin LdapSearch?>
32   <?plugin LdapSearch
33            host="localhost"
34            port=389
35            basedn=""
36            filter="(cn=*)"
37            attributes=""  
38   ?>
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" ?>
46
47  * @author John Lines
48  */
49
50 class WikiPlugin_LdapSearch
51 extends WikiPlugin
52 {
53     function getName () {
54         return _("LdapSearch");
55     }
56     function getDescription () {
57         return _("Search an LDAP directory");
58
59     }
60     function getVersion() {
61         return preg_replace("/[Revision: $]/", '',
62                             "\$Revision: 1.4 $");
63     }
64     function getDefaultArguments() {
65         return array('host'     => "",          // default: LDAP_AUTH_HOST
66                      'port'     => 389,         // ignored if host = full uri
67                      'basedn'   => "",          // LDAP_BASE_DN
68                      'filter'   => "(cn=*)",
69                      'attributes' => "",
70                      'user'     => '',
71                      'password' => '',
72                      'options'   => "",
73                      );
74     }
75
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"));
82         }
83         $args = $this->getArgs($argstr, $request);
84         extract($args);
85         if (!$host) {
86             if (defined('LDAP_AUTH_HOST')) {
87                 $host = LDAP_AUTH_HOST;
88                 if (strstr(LDAP_AUTH_HOST, '://'))
89                     $port = null;
90             } else {
91                 $host = 'localhost';
92             }
93         } else {
94             if (strstr($host, '://'))
95                 $port = null;
96         }
97         $html = HTML();
98         if (is_null($port))
99             $connect = ldap_connect($host);
100         else
101             $connect = ldap_connect($host, $port);
102         if (!$connect)
103             return $this->error(_("Failed to connect to LDAP host"));
104         if (!$options and defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
105             if (!empty($GLOBALS['LDAP_SET_OPTION'])) {
106                 $options = $GLOBALS['LDAP_SET_OPTION'];
107             }
108         }
109         if ($options) {
110             foreach ($options as $key => $value) {
111                 if (!ldap_set_option($connect, $key, $value))
112                     $this->error(_("Failed to set LDAP $key $value"));
113             }
114         }
115         $html->pushContent(HTML::table(array('cellpadding' => 1,'cellspacing' => 1, 'border' => 1)));
116         // special convenience: if host = LDAP_AUTH_HOST
117         // then take user and password from config.ini also
118         if ($user) {
119             if ($password)
120                 // required for Windows Active Directory Server
121                 $bind = ldap_bind($connect, $user, $password);
122             else
123                 $bind = ldap_bind($connect, $user);
124         } elseif (defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
125             if (LDAP_AUTH_USER)
126                 if (LDAP_AUTH_PASSWORD)
127                     // Windows Active Directory Server is strict
128                     $r = ldap_bind($connect, LDAP_AUTH_USER, LDAP_AUTH_PASSWORD); 
129                 else
130                     $r = ldap_bind($connect, LDAP_AUTH_USER); 
131             else // anonymous bind
132                 $bind = ldap_bind($connect);
133         } else { // other anonymous bind
134             $bind = ldap_bind($connect);
135         }
136         if (!$bind) return $this->error(_("Failed to bind LDAP host"));
137         $attr_array = array("");
138         if (!$attributes) {
139             $res = ldap_search($connect, $basedn, $filter);
140         } else {
141             $attr_array = split (" ",$attributes);
142             $res = ldap_search($connect, $basedn, $filter, $attr_array);
143         }
144         $entries = ldap_get_entries($connect, $res);
145  
146         // If we were given attributes then we return them in the order given
147         if ( $attributes ) {
148             for ($i=0; $i < count($attr_array) ; $i++) { $attrcols[$i] = 0; }
149             // Work out how many columns we need for each attribute.
150             for ($i = 0; $i < $entries["count"]; $i++) {
151                 for ($ii=0; $ii<$entries[$i]["count"]; $ii++){
152                     $data = $entries[$i][$ii];
153                     $datalen = $entries[$i][$data]["count"];
154                     if ($attrcols[$ii] < $datalen ) {
155                         $attrcols[$ii] = $datalen;
156                     }
157                 }
158             }
159
160             // Now print the headers
161             $row = HTML::tr(); 
162             for ($i=0; $i < count($attr_array) ; $i++) {
163                 $row->pushContent(HTML::th(array('colspan' => $attrcols[$i]), $attr_array[$i]));
164             }
165             $html->pushContent($row);
166             for ($i = 0; $i<$entries["count"]; $i++) {
167                 // start a new row for every data value.
168                 $row = HTML::tr(); $nc=0;
169                 for ($ii=0; $ii < $entries[$i]["count"]; $ii++){
170                     $data = $entries[$i][$ii];
171                     // 3 possible cases for the values of each attribute.
172                     switch ($entries[$i][$data]["count"]) {
173                     case 0:
174                         $row->pushContent(HTML::td("")); $nc++;
175                         break;
176                     default:
177                         for ($iii=0; $iii < $entries[$i][$data]["count"]; $iii++) {
178                             $row->pushContent(HTML::td($entries[$i][$data][$iii])); $nc++;
179                         }
180                     }
181                     // Make up some blank cells if required to pad this row
182                     for ( $j=0 ; $j < ($attrcols[$ii] - $nc); $j++ ) {
183                         $row->pushContent(HTML::td(""));
184                     }
185                 }
186                 $html->pushContent($row);
187             }
188         } else {
189             // $i = entries
190             // $ii = attributes for entry
191             // $iii = values per attribute
192             for ($i = 0; $i < $entries["count"]; $i++) {
193                 $row = HTML::tr();
194                 for ($ii=0; $ii < $entries[$i]["count"]; $ii++){
195                     $data = $entries[$i][$ii];
196                     for ($iii=0; $iii < $entries[$i][$data]["count"]; $iii++) {
197                         //echo $data.":&nbsp;&nbsp;".$entries[$i][$data][$iii]."<br>";
198                         if ( ! $attributes ) {
199                             $row->pushContent(HTML::td($data));
200                         }
201                         $row->pushContent(HTML::td($entries[$i][$data][$iii]));
202                     }
203                 }               
204                 $html->pushContent($row);
205             }
206         }
207         return $html;
208     }
209 };
210
211 // $Log: not supported by cvs2svn $
212 // Revision 1.3  2004/12/20 16:05:14  rurban
213 // gettext msg unification
214 //
215 // Revision 1.2  2004/10/04 23:39:34  rurban
216 // just aesthetics
217 //
218 // Revision 1.1  2004/09/23 12:28:12  rurban
219 // initial checkin from http://phpwiki.org/LdapSearchPlugin
220 //   by John Lines
221 //
222
223 // For emacs users
224 // Local Variables:
225 // mode: php
226 // tab-width: 8
227 // c-basic-offset: 4
228 // c-hanging-comment-ender-p: nil
229 // indent-tabs-mode: nil
230 // End:
231 ?>