]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LdapSearch.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / LdapSearch.php
1 <?php // -*-php-*- // $Id$
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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   <<LdapSearch>>
32   <<LdapSearch
33            host="localhost"
34            port=389
35            basedn=""
36             filter="(cn=*)"
37            attributes=""
38   >>
39   <<LdapSearch host=ldap.example.com filter="(ou=web-team)"
40                       attributes="sn cn telephonenumber" >>
41   <<LdapSearch host="ldap.itd.umich.edu" basedn="" filter="(sn=jensen)" attributes="cn drink" >>
42   <<LdapSearch host=ldap.example.com attributes="cn sn telephonenumber" >>
43   <<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     function getDefaultArguments() {
60         return array('host'         => "",                 // default: LDAP_AUTH_HOST
61                      'port'         => 389,                // ignored if host = full uri
62                      'basedn'         => "",                // LDAP_BASE_DN
63                      'filter'   => "(cn=*)",
64                      'attributes' => "",
65                      'user'     => '',
66                      'password' => '',
67                      'options'   => "",
68                      );
69     }
70
71     // I ought to require the ldap extension, but fail sanely, if I cant get it.
72     // - however at the moment this seems to work as is
73     function run($dbi, $argstr, $request) {
74         if (!function_exists('ldap_connect')) {
75             if (!loadPhpExtension('ldap'))
76                 return $this->error(_("Missing ldap extension"));
77         }
78         $args = $this->getArgs($argstr, $request);
79         extract($args);
80         //include_once("lib/WikiUser/LDAP.php");
81         if (!$host) {
82             if (defined('LDAP_AUTH_HOST')) {
83                 $host = LDAP_AUTH_HOST;
84                 if (strstr(LDAP_AUTH_HOST, '://'))
85                     $port = null;
86             } else {
87                 $host = 'localhost';
88             }
89         } else {
90             if (strstr($host, '://'))
91                 $port = null;
92         }
93         $html = HTML();
94         if (is_null($port))
95             $connect = ldap_connect($host);
96         else
97             $connect = ldap_connect($host, $port);
98         if (!$connect)
99             return $this->error(_("Failed to connect to LDAP host"));
100         if (!$options and defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
101             if (!empty($GLOBALS['LDAP_SET_OPTION'])) {
102                 $options = $GLOBALS['LDAP_SET_OPTION'];
103             }
104         }
105         if ($options) {
106             foreach ($options as $key => $value) {
107                 if (!ldap_set_option($connect, $key, $value))
108                     $this->error(_("Failed to set LDAP $key $value"));
109             }
110         }
111
112         // special convenience: if host = LDAP_AUTH_HOST
113         // then take user and password from config.ini also
114         if ($user) {
115             if ($password)
116                 // required for Windows Active Directory Server
117                 $bind = ldap_bind($connect, $user, $password);
118             else
119                 $bind = ldap_bind($connect, $user);
120         } elseif (defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
121             if (LDAP_AUTH_USER)
122                 if (LDAP_AUTH_PASSWORD)
123                     // Windows Active Directory Server is strict
124                     $r = ldap_bind($connect, LDAP_AUTH_USER, LDAP_AUTH_PASSWORD);
125                 else
126                     $r = ldap_bind($connect, LDAP_AUTH_USER);
127             else // anonymous bind
128                 $bind = ldap_bind($connect);
129         } else { // other anonymous bind
130             $bind = ldap_bind($connect);
131         }
132         if (!$bind) return $this->error(_("Failed to bind LDAP host"));
133         if (!$basedn) $basedn = LDAP_BASE_DN;
134         $attr_array = array("");
135         if (!$attributes) {
136             $res = ldap_search($connect, $basedn, $filter);
137         } else {
138             $attr_array = explode(" ", $attributes);
139             $res = ldap_search($connect, $basedn, $filter, $attr_array);
140         }
141         $entries = ldap_get_entries($connect, $res);
142
143         // If we were given attributes then we return them in the order given
144         // else take all
145         if ( !$attributes ) {
146             $attr_array = array();
147             for ($ii=0; $ii < $entries[0]["count"]; $ii++) {
148                     $data = $entries[0][$ii];
149                     $attr_array[] = $data;
150             }
151         }
152         for ($i=0; $i < count($attr_array) ; $i++) { $attrcols[$i] = 0; }
153         // Work out how many columns we need for each attribute. objectclass has more
154         for ($i=0; $i<$entries[0]["count"]; $i++) {
155                 $data = $entries[0][$i];
156                 $datalen = $entries[0][$data]["count"];
157                 if ($attrcols[$i] < $datalen) {
158                     $attrcols[$i] = $datalen;
159                 }
160         }
161         // Print the headers
162         $row = HTML::tr();
163         for ($i=0; $i < count($attr_array) ; $i++) {
164             // span subcolumns, like objectclass
165             if ($attrcols[$i] > 1)
166                 $row->pushContent(HTML::th(array('colspan' => $attrcols[$i]), $attr_array[$i]));
167             else
168                 $row->pushContent(HTML::th(array(), $attr_array[$i]));
169         }
170         $html->pushContent($row);
171
172         // Print the data rows
173         for ($currow = 0; $currow < $entries["count"]; $currow++) {
174             $row = HTML::tr(); $nc=0;
175             // columns
176             for ($i=0; $i < count($attr_array); $i++){
177                     $colname = $attr_array[$i];
178                 $data = @$entries[$currow][$colname];
179                 if ($data and $data["count"] > 0) {
180                     // subcolumns, e.g. for objectclass
181                     for ($iii=0; $iii < $data["count"]; $iii++) {
182                       $row->pushContent(HTML::td($data[$iii])); $nc++;
183                     }
184                 } else {
185                     $row->pushContent(HTML::td("")); $nc++;
186                 }
187                 // Make up some blank cells if required to pad this row
188                 /*for ( $j=0 ; $j < ($attrcols[$ii] - $nc); $j++ ) {
189                     $row->pushContent(HTML::td(""));
190                 }*/
191             }
192             $html->pushContent($row);
193         }
194         return HTML::table(array('cellpadding' => 1,'cellspacing' => 1, 'border' => 1), $html);
195     }
196 };
197
198 // Local Variables:
199 // mode: php
200 // tab-width: 8
201 // c-basic-offset: 4
202 // c-hanging-comment-ender-p: nil
203 // indent-tabs-mode: nil
204 // End:
205 ?>