]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LdapSearch.php
No underscore for private functions
[SourceForge/phpwiki.git] / lib / plugin / LdapSearch.php
1 <?php
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  * @author John Lines
47  */
48
49 class WikiPlugin_LdapSearch
50     extends WikiPlugin
51 {
52     function getName()
53     {
54         return _("LdapSearch");
55     }
56
57     function getDescription()
58     {
59         return _("Search an LDAP directory.");
60     }
61
62     function getDefaultArguments()
63     {
64         return array('host' => "", // default: LDAP_AUTH_HOST
65             'port' => 389, // ignored if host = full uri
66             'basedn' => "", // LDAP_BASE_DN
67             'filter' => "(cn=*)",
68             'attributes' => "",
69             'user' => '',
70             'password' => '',
71             'options' => "",
72         );
73     }
74
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     {
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         //include_once("lib/WikiUser/LDAP.php");
86         if (!$host) {
87             if (defined('LDAP_AUTH_HOST')) {
88                 $host = LDAP_AUTH_HOST;
89                 if (strstr(LDAP_AUTH_HOST, '://'))
90                     $port = null;
91             } else {
92                 $host = 'localhost';
93             }
94         } else {
95             if (strstr($host, '://'))
96                 $port = null;
97         }
98         $html = HTML();
99         if (is_null($port))
100             $connect = ldap_connect($host);
101         else
102             $connect = ldap_connect($host, $port);
103         if (!$connect)
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'];
108             }
109         }
110         if ($options) {
111             foreach ($options as $key => $value) {
112                 if (!ldap_set_option($connect, $key, $value))
113                     $this->error(_("Failed to set LDAP $key $value"));
114             }
115         }
116
117         // special convenience: if host = LDAP_AUTH_HOST
118         // then take user and password from config.ini also
119         if ($user) {
120             if ($password)
121                 // required for Windows Active Directory Server
122                 $bind = ldap_bind($connect, $user, $password);
123             else
124                 $bind = ldap_bind($connect, $user);
125         } elseif (defined('LDAP_AUTH_HOST') and $args['host'] == LDAP_AUTH_HOST) {
126             if (LDAP_AUTH_USER)
127                 if (LDAP_AUTH_PASSWORD)
128                     // Windows Active Directory Server is strict
129                     $r = ldap_bind($connect, LDAP_AUTH_USER, LDAP_AUTH_PASSWORD);
130                 else
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);
136         }
137         if (!$bind) return $this->error(_("Failed to bind LDAP host"));
138         if (!$basedn) $basedn = LDAP_BASE_DN;
139         $attr_array = array("");
140         if (!$attributes) {
141             $res = ldap_search($connect, $basedn, $filter);
142         } else {
143             $attr_array = explode(" ", $attributes);
144             $res = ldap_search($connect, $basedn, $filter, $attr_array);
145         }
146         $entries = ldap_get_entries($connect, $res);
147
148         // If we were given attributes then we return them in the order given
149         // else take all
150         if (!$attributes) {
151             $attr_array = array();
152             for ($ii = 0; $ii < $entries[0]["count"]; $ii++) {
153                 $data = $entries[0][$ii];
154                 $attr_array[] = $data;
155             }
156         }
157         for ($i = 0; $i < count($attr_array); $i++) {
158             $attrcols[$i] = 0;
159         }
160         // Work out how many columns we need for each attribute. objectclass has more
161         for ($i = 0; $i < $entries[0]["count"]; $i++) {
162             $data = $entries[0][$i];
163             $datalen = $entries[0][$data]["count"];
164             if ($attrcols[$i] < $datalen) {
165                 $attrcols[$i] = $datalen;
166             }
167         }
168         // Print the headers
169         $row = HTML::tr();
170         for ($i = 0; $i < count($attr_array); $i++) {
171             // span subcolumns, like objectclass
172             if ($attrcols[$i] > 1)
173                 $row->pushContent(HTML::th(array('colspan' => $attrcols[$i]), $attr_array[$i]));
174             else
175                 $row->pushContent(HTML::th(array(), $attr_array[$i]));
176         }
177         $html->pushContent($row);
178
179         // Print the data rows
180         for ($currow = 0; $currow < $entries["count"]; $currow++) {
181             $row = HTML::tr();
182             $nc = 0;
183             // columns
184             for ($i = 0; $i < count($attr_array); $i++) {
185                 $colname = $attr_array[$i];
186                 $data = @$entries[$currow][$colname];
187                 if ($data and $data["count"] > 0) {
188                     // subcolumns, e.g. for objectclass
189                     for ($iii = 0; $iii < $data["count"]; $iii++) {
190                         $row->pushContent(HTML::td($data[$iii]));
191                         $nc++;
192                     }
193                 } else {
194                     $row->pushContent(HTML::td(""));
195                     $nc++;
196                 }
197                 // Make up some blank cells if required to pad this row
198                 /*for ( $j=0 ; $j < ($attrcols[$ii] - $nc); $j++ ) {
199                     $row->pushContent(HTML::td(""));
200                 }*/
201             }
202             $html->pushContent($row);
203         }
204         return HTML::table(array('cellpadding' => 1, 'cellspacing' => 1, 'border' => 1), $html);
205     }
206 }
207
208 // Local Variables:
209 // mode: php
210 // tab-width: 8
211 // c-basic-offset: 4
212 // c-hanging-comment-ender-p: nil
213 // indent-tabs-mode: nil
214 // End: