]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LdapSearch.php
gettext msg unification
[SourceForge/phpwiki.git] / lib / plugin / LdapSearch.php
1 <?php // -*-php-*- rcs_id('$Id: LdapSearch.php,v 1.3 2004-12-20 16:05:14 rurban Exp $');
2 /**
3  Copyright 2004 John Lines
4
5  This file is part of PhpWiki.
6
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.
11
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.
16
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
20  */
21
22 /**
23  * WikiPlugin which searches an LDAP directory.
24  *
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
28  *
29  * Usage Samples:
30   <?plugin LdapSearch?>
31   <?plugin LdapSearch
32            host="localhost"
33            port=389
34            basedn=""
35             filter="(cn=*)"
36            attributes=""  
37   ?>
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" ?>
45
46  * @author John Lines
47  */
48
49 // Constants are defined before the class.
50 // if (!defined('THE_END'))
51 //    define('THE_END', "!");
52
53 class WikiPlugin_LdapSearch
54 extends WikiPlugin
55 {
56     function getName () {
57         return _("LdapSearch");
58     }
59     function getDescription () {
60         return _("Search an LDAP directory");
61
62     }
63     function getVersion() {
64         return preg_replace("/[Revision: $]/", '',
65                             "\$Revision: 1.3 $");
66     }
67     function getDefaultArguments() {
68         return array('host'     => "localhost", // change to LDAP_AUTH_HOST
69                      'port'     => 389,         // ditto
70                      'basedn'   => "",          // LDAP_BASE_DN
71                      'filter'   => "(cn=*)",
72                      'attributes' => "");
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         extract($this->getArgs($argstr, $request));
79
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"));
84         }
85         $bind = ldap_bind($connect);
86         $attr_array = array("");                // for now - 
87         if (!$attributes) {
88             $res = ldap_search($connect, $basedn, $filter);
89         } else {
90             $attr_array = split (" ",$attributes);
91             $res = ldap_search($connect, $basedn, $filter,$attr_array);
92         }
93         $entries = ldap_get_entries($connect, $res);
94  
95         // If we were given attributes then we return them in the order given
96         if ( $attributes ) {
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;
105                     }
106                 }
107             }
108
109             // Now print the headers
110             $row = HTML::tr(); 
111             for ($i=0; $i < count($attr_array) ; $i++) {
112                 $row->pushContent(HTML::th(array('colspan' => $attrcols[$i]), $attr_array[$i]));
113             }
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"]) {
122                     case 0:
123                         $row->pushContent(HTML::td("")); $nc++;
124                         break;
125                     default:
126                         for ($iii=0; $iii < $entries[$i][$data]["count"]; $iii++) {
127                             $row->pushContent(HTML::td($entries[$i][$data][$iii])); $nc++;
128                         }
129                     }
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(""));
133                     }
134                 }
135                 $html->pushContent($row);
136             }
137         } else {
138             // $i = entries
139             // $ii = attributes for entry
140             // $iii = values per attribute
141             for ($i = 0; $i < $entries["count"]; $i++) {
142                 $row = HTML::tr();
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.":&nbsp;&nbsp;".$entries[$i][$data][$iii]."<br>";
147                         if ( ! $attributes ) {
148                             $row->pushContent(HTML::td($data));
149                         }
150                         $row->pushContent(HTML::td($entries[$i][$data][$iii]));
151                     }
152                 }               
153                 $html->pushContent($row);
154             }
155         }
156         
157         // THE_END); // ??
158         return $html;
159     }
160 };
161
162 // $Log: not supported by cvs2svn $
163 // Revision 1.2  2004/10/04 23:39:34  rurban
164 // just aesthetics
165 //
166 // Revision 1.1  2004/09/23 12:28:12  rurban
167 // initial checkin from http://phpwiki.org/LdapSearchPlugin
168 //   by John Lines
169 //
170
171 // For emacs users
172 // Local Variables:
173 // mode: php
174 // tab-width: 8
175 // c-basic-offset: 4
176 // c-hanging-comment-ender-p: nil
177 // indent-tabs-mode: nil
178 // End:
179 ?>