/[drupal]/contributions/modules/ldap_lookup/ldap_lookup.class
ViewVC logotype

Contents of /contributions/modules/ldap_lookup/ldap_lookup.class

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download)
Thu Oct 4 10:07:03 2007 UTC (2 years, 1 month ago) by kibble
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +1 -0 lines
Version checks only
1 <?php
2 // $Id: ldap_lookup.class,v 1.2 2007/09/26 09:57:27 kibble Exp $
3
4 class ldap_lookup_class {
5
6 public $connection; /** object */
7 public $binding; /** object */
8 private $name; /** string */
9 private $server; /** string */
10 private $port; /** integer */
11 private $basedn; /** string */
12 private $groupdn; /** string */
13 public $binddn; /** string */
14 private $bindpw; /** string */
15 private $use_tls; /** bool */
16 public $user_attr; /** string */
17 private $email_attr; /** string */
18
19 function __construct($name, $server, $port, $basedn, $groupdn, $binddn, $bindpw, $use_tls, $user_attr, $email_attr) {
20
21 $this->name = $name;
22 $this->server = $server;
23 $this->port = $port;
24 $this->basedn = $basedn;
25 $this->binddn = $binddn;
26 $this->groupdn = $groupdn;
27 $this->bindpw = $bindpw;
28 $this->use_tls = $use_tls;
29 $this->user_attr = $user_attr;
30 $this->email_attr = $email_attr;
31
32 }
33
34 function __destruct() {
35 $this->disconnect();
36 }
37
38 function debug($errmsg) {
39 if (variable_get('ldap_lookup_debugging', LDAP_ALL_OFF) == 1) {
40 watchdog('ldap', $errmeg);
41 }
42 if (variable_get('ldap_lookup_debugging', LDAP_ALL_OFF) == 2) {
43 drupal_set_message($errmeg);
44 }
45 drupal_set_message($errmeg);
46 return;
47 }
48
49 function void_error_handler($p1, $p2, $p3, $p4, $p5) {
50 /** Do nothing */
51 $this->debug('There was an error [binding|searching]<br />' . $p1 . '<br />' . $p2 . '<br />' . $p3 . '<br />' . $p4 . '<br />' . print_r($p5, true));
52 return;
53 }
54
55 function set_bindings($binddn, $bindpw) {
56
57 if ($binddn) {
58 $this->binddn = $binddn;
59 }
60
61 if ($bindpw) {
62 $this->bindpw = $bindpw;
63 }
64
65 return;
66
67 }
68
69 function disconnect() {
70
71 if ($this->connection) {
72 ldap_unbind($this->connection);
73 $this->connection = NULL;
74 }
75
76 }
77
78 function connect() {
79
80 $this->disconnect();
81
82 if (!$this->connection = ldap_connect($this->server, $this->port)) {
83 watchdog('ldap', 'LDAP Connect failure to ' . $this->server . ':' . $this->port);
84 return(FALSE);
85 }
86
87 ldap_set_option($this->connection, LDAP_OPT_REFERRALS, 0);
88 ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, 3);
89
90 if ($this->tls) {
91
92 $vers = -1;
93 $vers = ldap_get_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $vers);
94 if ($vers == -1) {
95 watchdog('ldap', 'Could not get LDAP protocol version.');
96 }
97
98 if ($vers != 3) {
99 watchdog('ldap', 'Could not start TLS, only supported by LDAP v3.');
100 } else if (!function_exists('ldap_start_tls')) {
101 watchdog('ldap', 'Could not start TLS. It does not seem to be supported by this PHP setup.');
102 } else if (!ldap_start_tls($this->connection)) {
103 watchdog('ldap', t("Could not start TLS. (Error %errno: %error).", array('%errno' => ldap_errno($this->connection), '%error' => ldap_error($this->connection))));
104 }
105
106 }
107
108 ob_start();
109 set_error_handler(array('ldap_lookup_class', 'void_error_handler'));
110 $this->binding = @ldap_bind($this->connection, $this->binddn, $this->bindpw);
111 restore_error_handler();
112 ob_end_clean();
113
114 if (!$this->binding) {
115 watchdog('ldap', t('LDAP Bind failure for user %user. Error %errno: %error', array('%user' => $this->binddn, '%errno' => ldap_errno($this->connection), '%error' => ldap_error($this->connection))));
116 return(FALSE);
117 } else {
118 return(TRUE);
119 }
120
121 }
122
123 function retrieve($user_dn) {
124
125 set_error_handler(array('ldap_lookup_class', 'void_error_handler'));
126 $result = ldap_read($this->connection, $user_dn, 'objectClass=*');
127 $entries = ldap_get_entries($this->connection, $result);
128 restore_error_handler();
129
130 return($entries[0]);
131
132 }
133
134 function search($base_dn, $filter, $attributes = array()) {
135
136 set_error_handler(array('ldap_lookup_class', 'void_error_handler'));
137 $x = @ldap_search($this->connection, $base_dn, $filter, $attributes);
138 restore_error_handler();
139
140 if ($x && ldap_count_entries($this->connection, $x)) {
141 return(ldap_get_entries($this->connection, $x));
142 } else {
143 return(array());
144 }
145
146 }
147
148 function modify($user, $attributes) {
149
150 $possible_base_dns = explode("\r\n", $this->basedn);
151 foreach ($possible_base_dns as $base_dn) {
152
153 $sr = @ldap_search($this->connection, $base_dn, "(" . $this->user_attr . "=" . $user . ")");
154
155 if ($sr) {
156 $ent = ldap_get_entries($this->connection, $sr);
157
158 if ($ent) {
159
160 set_error_handler(array('ldap_lookup_class', 'void_error_handler'));
161
162 $real_attr = array();
163 foreach ($attributes as $key => $cur_val) {
164 if ($attributes[$key][0] == "") {
165 @ldap_mod_del($this->connection, $ent[0]["dn"], array($key => array()));
166 } else {
167 $real_attr[$key][0] = $attributes[$key][0];
168 }
169 }
170
171 @ldap_modify($this->connection, $ent[0]["dn"], $real_attr);
172 restore_error_handler();
173
174 unset($real_attr);
175
176 }
177
178 }
179
180 }
181
182 }
183
184 }
185
186 ?>

  ViewVC Help
Powered by ViewVC 1.1.2