3 |
$Header$ |
$Header$ |
4 |
------------------------------------------------------------------------ |
------------------------------------------------------------------------ |
5 |
copyright (C) 2003 mose . mose@ludap.org |
copyright (C) 2003 mose . mose@ludap.org |
|
copyright (C) 2001, 2002 mose, makina corpus - mose@makina-corpus.org |
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
18 |
the Free Software Foundation, Inc., |
the Free Software Foundation, Inc., |
19 |
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
------------------------------------------------------------------------ |
------------------------------------------------------------------------ |
21 |
|
That file is adapted from ludap work at makina corpus avalaible under GPL |
22 |
|
copyright (C) 2001, 2002 mose, makina corpus - mose@makina-corpus.org |
23 |
*/ |
*/ |
24 |
|
|
25 |
/** |
/** |
29 |
class ludap { |
class ludap { |
30 |
|
|
31 |
var $ver = '0.5'; |
var $ver = '0.5'; |
32 |
|
var $conn = ''; |
33 |
|
var $message = ''; |
34 |
|
var $auth = false; |
35 |
|
var $admin = false; |
36 |
|
|
37 |
var $conf = array(); |
var $conf = array(); |
38 |
|
|
39 |
|
var $logfile = ".ludap.log"; |
40 |
|
|
41 |
|
var $schema_user_file = 'user.schema'; |
42 |
|
var $schema_user = array(); |
43 |
|
|
44 |
|
var $schema_group_file = 'group.schema'; |
45 |
|
var $schema_group = array(); |
46 |
|
|
47 |
|
var $schema_mail = array(); |
48 |
|
|
49 |
function ludap($conf_file) { |
function ludap($C, $conf_file) { |
50 |
$confludap = new conf($conf_file); |
$this->conf = $C->get($conf_file); |
51 |
$this->conf = $confludap->readconf(); |
|
52 |
|
$schema_user =& $C->get($this->schema_user_file); |
53 |
|
foreach ($schema_user as $att=>$att_activated) { |
54 |
|
if ($att_activated) { |
55 |
|
$this->schema_user = array_merge($this->schema_user, array($att => $C->get($this->schema_user_file,$att))); |
56 |
|
} |
57 |
|
} |
58 |
|
|
59 |
|
$schema_group =& $C->get($this->schema_group_file); |
60 |
|
foreach ($schema_group as $att=>$att_activated) { |
61 |
|
if ($att_activated) { |
62 |
|
$this->schema_group = array_merge($this->schema_group, array($att => $C->get($this->schema_group_file,$att))); |
63 |
|
} |
64 |
|
} |
65 |
|
|
66 |
} |
} |
67 |
|
|
68 |
function check_ldap_ability() { |
function check_ldap_ability() { |
69 |
if (!function_exists("ldap_connect")) { |
if (!function_exists("ldap_connect")) { |
70 |
if (!extension_loaded('ldap')) { |
if (!extension_loaded('ldap')) { |
80 |
} |
} |
81 |
} |
} |
82 |
|
|
83 |
|
function rawlog($type) { |
84 |
|
global $S; |
85 |
|
$file = $this->logfile; |
86 |
|
$S->assign('type',$type); |
87 |
|
$log = $S->fetch('logline.tpl'); |
88 |
|
$fp = @fopen($this->conf['logdir']."/$file", "a+"); |
89 |
|
if ($fp) { |
90 |
|
while (!flock($fp,2) and ($i < 30)) { sleep(2); $i++; } |
91 |
|
@fputs($fp,"$log\n"); |
92 |
|
flock($fp,3); |
93 |
|
@fclose($fp); |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
|
function showerror() { |
98 |
|
global $S; |
99 |
|
$S->assign('errno',ldap_errno($this->conn)); |
100 |
|
$S->assign('error',ldap_error($this->conn)); |
101 |
|
$S->assign('errmsg',ldap_err2str(ldap_errno($this->conn))); |
102 |
|
return $S->fetch('errorline.tpl'); |
103 |
|
} |
104 |
|
|
105 |
|
function conn() { |
106 |
|
$this->check_ldap_ability(); |
107 |
|
$conn = @ldap_connect($this->conf['host']); |
108 |
|
if (is_resource($conn)) { |
109 |
|
$this->conn = $conn; |
110 |
|
return true; |
111 |
|
} else { |
112 |
|
return false; |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
|
function logout() { |
117 |
|
if (isset($_SESSION['me'])) { |
118 |
|
$this->rawlog("Logout - ".@$_SESSION['me']); |
119 |
|
session_unset(); |
120 |
|
session_destroy(); |
121 |
|
$this->message.= "<br/>Votre session a été detruite."; |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
function auth($me, $login=false , $pass=false) { |
126 |
|
if (!$me) { |
127 |
|
$this->logout(); |
128 |
|
} |
129 |
|
if (!$me && $login && $pass) { |
130 |
|
$binddn = $this->conf['baseattr']."=".utf8_encode($login).", ".$this->conf['basedn']; |
131 |
|
if (@ldap_bind($this->conn, $binddn, $pass)) { |
132 |
|
$i = ldap_search($this->conn, $this->conf['groupdn'], $this->conf['groupattr']."= $binddn", array($this->conf['membattr'])); |
133 |
|
$ix = ldap_get_entries($this->conn, $i); |
134 |
|
for ($x=0; $x<$ix["count"]; $x++) { |
135 |
|
$gps[] = $ix["$x"]["{$this->conf['membattr']}"][0]; |
136 |
|
} |
137 |
|
$_SESSION['me'] = $login; |
138 |
|
$_SESSION['pass'] = $pass; |
139 |
|
$_SESSION['gps'] = $gps; |
140 |
|
if (in_array($this->conf['admingrp'],$gps)) { |
141 |
|
$this->admin = true; |
142 |
|
$_SESSION['admin'] = $this->admin; |
143 |
|
} |
144 |
|
$this->rawlog("Login - $login"); |
145 |
|
return true; |
146 |
|
} else { |
147 |
|
$this->message.= "<br />".$this->showerror(); |
148 |
|
return false; |
149 |
|
} |
150 |
|
} elseif ($me) { |
151 |
|
$binddn = $this->conf['baseattr']."=".utf8_encode($me).", ".$this->conf['basedn']; |
152 |
|
if (@ldap_bind($this->conn, $binddn, $_SESSION['pass'])) { |
153 |
|
return true; |
154 |
|
} else { |
155 |
|
return false; |
156 |
|
} |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
function domenu($list,$it) { |
function domenu($list,$it) { |
161 |
foreach ($list as $k=>$l) { |
foreach ($list as $k=>$l) { |
162 |
if ($it == $k) { |
if ($it == $k) { |
168 |
return "$back"; |
return "$back"; |
169 |
} |
} |
170 |
|
|
171 |
function get_gid($id,$attrs='') { |
function get_gid($id, $attrs) { |
|
global $conn, $conf; |
|
172 |
if (is_array($attrs)) { |
if (is_array($attrs)) { |
173 |
$i = ldap_search($conn,$conf['ludap']['groupdn'],$conf['ludap']['membattr']."=$id",$attrs); |
$i = ldap_search($this->conn,$this->conf['groupdn'],"(".$this->conf['membattr']."=$id)", $attrs); |
174 |
} else { |
} else { |
175 |
$i = ldap_search($conn,$conf['ludap']['groupdn'],$conf['ludap']['membattr']."=$id"); |
$i = ldap_search($this->conn,$this->conf['groupdn'],"(".$this->conf['membattr']."=$id)"); |
176 |
} |
} |
177 |
$info = ldap_get_entries($conn,$i); |
$info = ldap_get_entries($this->conn,$i); |
178 |
foreach ($conf[group] as $clabel=>$cbuff) { |
|
179 |
if ($cbuff[2] == 'multi') { |
foreach ($this->schema_group as $clabel=>$cbuff) { |
180 |
if ($info[0][$clabel][count]) { |
if ($cbuff['TYPE'] == 'multi') { |
181 |
|
if ($info[0][$clabel]['count']) { |
182 |
array_shift($info[0][$clabel]); |
array_shift($info[0][$clabel]); |
183 |
foreach ($info[0][$clabel] as $icl) { |
foreach ($info[0][$clabel] as $icl) { |
184 |
if ($cbuff[1] == 'dn') { |
if ($cbuff['TYPE'] == 'dn') { |
185 |
if (ereg("{$conf['ludap']['baseattr']}=([^,]+),{$conf['ludap']['basedn']}",$icl,$match)) { |
if (ereg("{$this->conf['baseattr']}=([^,]+),{$this->conf['basedn']}",$icl,$match)) { |
186 |
$fres[$clabel][] = array($conf['ludap']['baseattr'], $conf['ludap']['basedn'], utf8_decode($match[1])); |
$fres[$clabel][] = array($this->conf['baseattr'], $this->conf['basedn'], utf8_decode($match[1])); |
187 |
} |
} |
188 |
} else { |
} else { |
189 |
$fres[$clabel][] = utf8_decode($icl); |
$fres[$clabel][] = utf8_decode($icl); |
190 |
} |
} |
191 |
} |
} |
192 |
} |
} |
193 |
} elseif ($cbuff[1] == 'image') { |
} elseif ($cbuff['TYPE'] == 'file') { |
194 |
$ei = ldap_first_entry($conn, $i); |
$ei = ldap_first_entry($this->conn, $i); |
195 |
$tmpimg = @ldap_get_values_len($conn,$ei,$clabel); |
$tmpimg = @ldap_get_values_len($this->conn,$ei,$clabel); |
196 |
if ($tmpimg) { |
if ($tmpimg) { |
197 |
$fp = fopen("tmp/$id.jpg", "w"); |
$fp = fopen("tmp/$id.jpg", "w"); |
198 |
fwrite($fp, $tmpimg[0]); |
fwrite($fp, $tmpimg[0]); |
202 |
/* |
/* |
203 |
echo "<pre>"; print_r($tmpimg); die("</pre>"); |
echo "<pre>"; print_r($tmpimg); die("</pre>"); |
204 |
*/ |
*/ |
205 |
} elseif ($cbuff[1] == 'dn') { |
} elseif ($cbuff['TYPE'] == 'dn') { |
206 |
$fres[$clabel] = strtok(utf8_decode($info[0][$clabel][0]),','); |
$fres["$clabel"] = strtok(utf8_decode($info[0]["$clabel"][0]),','); |
207 |
} else { |
} else { |
208 |
$fres[$clabel] = utf8_decode($info[0][$clabel][0]); |
if (isset($info[0]["$clabel"])) { |
209 |
|
$fres["$clabel"] = utf8_decode($info[0]["$clabel"][0]); |
210 |
|
} |
211 |
} |
} |
212 |
} |
} |
213 |
return $fres; |
return $fres; |
214 |
} |
} |
215 |
|
|
216 |
function get_id($id,$attrs='') { |
function get_id($id,$attrs='') { |
217 |
global $conn, $conf; |
global $conn; |
218 |
if (is_array($attrs)) { |
if (is_array($attrs)) { |
219 |
$i = ldap_search($conn,$conf['ludap']['basedn'],$conf['ludap']['baseattr']."=$id",$attrs); |
$i = ldap_search($this->conn,$this->conf['basedn'],$this->conf['baseattr']."=$id",$attrs); |
220 |
} else { |
} else { |
221 |
$i = ldap_search($conn,$conf['ludap']['basedn'],$conf['ludap']['baseattr']."=$id"); |
$i = ldap_search($this->conn,$this->conf['basedn'],$this->conf['baseattr']."=$id"); |
222 |
} |
} |
223 |
$info = ldap_get_entries($conn,$i); |
$info = ldap_get_entries($this->conn,$i); |
224 |
foreach ($conf[human] as $clabel=>$cbuff) { |
foreach ($this->schema_user as $clabel=>$cbuff) { |
225 |
if ($cbuff[2] == 'multi') { |
if ($cbuff['TYPE'] == 'multi') { |
226 |
if ($info[0][$clabel][count]) { |
if ($info[0][$clabel][count]) { |
227 |
array_shift($info[0][$clabel]); |
array_shift($info[0][$clabel]); |
228 |
foreach ($info[0][$clabel] as $icl) { |
foreach ($info[0][$clabel] as $icl) { |
229 |
$fres[$clabel][] = utf8_decode($icl); |
$fres["$clabel"][] = utf8_decode($icl); |
230 |
} |
} |
231 |
} |
} |
232 |
} elseif ($cbuff[1] == 'image') { |
} elseif ($cbuff['TYPE'] == 'file') { |
233 |
$ei = ldap_first_entry($conn, $i); |
$ei = ldap_first_entry($this->conn, $i); |
234 |
$tmpimg = @ldap_get_values_len($conn,$ei,$clabel); |
$tmpimg = @ldap_get_values_len($this->conn,$ei,$clabel); |
235 |
if ($tmpimg) { |
if ($tmpimg) { |
236 |
$fp = fopen("tmp/$id.jpg", "w"); |
$fp = fopen("tmp/$id.jpg", "w"); |
237 |
fwrite($fp, $tmpimg[0]); |
fwrite($fp, $tmpimg[0]); |
238 |
fclose($fp); |
fclose($fp); |
239 |
$fres[$clabel] = "tmp/$id.jpg"; |
$fres["$clabel"] = "tmp/$id.jpg"; |
240 |
} |
} |
241 |
/* |
/* |
242 |
echo "<pre>"; print_r($tmpimg); die("</pre>"); |
echo "<pre>"; print_r($tmpimg); die("</pre>"); |
243 |
*/ |
*/ |
244 |
} else { |
} else { |
245 |
$fres[$clabel] = utf8_decode($info[0][$clabel][0]); |
if (isset($info[0]["$clabel"])) { |
246 |
|
$fres["$clabel"] = utf8_decode($info[0][$clabel][0]); |
247 |
|
} |
248 |
} |
} |
249 |
} |
} |
250 |
return $fres; |
return $fres; |
253 |
// ROU 20030303 récupère la liste des adresses mails |
// ROU 20030303 récupère la liste des adresses mails |
254 |
function get_mails($id='') { |
function get_mails($id='') { |
255 |
global $conn, $conf; |
global $conn, $conf; |
256 |
$cn = $conf['ludap']['membattr']; |
// inhibit for now |
257 |
|
return array(); |
258 |
|
$cn = $this->conf['membattr']; |
259 |
if ($id) { |
if ($id) { |
260 |
$limit = "{$conf['ludap']['membattr']}={$id}*"; |
$limit = "({$this->conf['membattr']}={$id}*)"; |
261 |
} else { |
} else { |
262 |
$limit = "($cn=*)"; |
$limit = "($cn=*)"; |
263 |
} |
} |
264 |
$i = ldap_search($conn,$conf['ludap']['maildn'],$conf['ludap']['membattr']."=$id",array($conf['ludap']['mailattr'])); |
$i = ldap_search($conn,$this->conf['maildn'],$this->conf['membattr']."=$id",array($conf['ludap']['mailattr'])); |
265 |
$info = ldap_get_entries($conn,$i); |
$info = ldap_get_entries($conn,$i); |
266 |
for ($x=0; $x<$info["count"]; $x++) { |
for ($x=0; $x<$info["count"]; $x++) { |
267 |
for ($y=0; $y<$info[$x][$conf['ludap']['mailattr']]['count']; $y++) { |
for ($y=0; $y<$info[$x][$this->conf['mailattr']]['count']; $y++) { |
268 |
$f_mails[] = utf8_decode($info[$x][$conf['ludap']['mailattr']][$y]); |
$f_mails[] = utf8_decode($info[$x][$this->conf['mailattr']][$y]); |
269 |
} |
} |
270 |
} |
} |
271 |
return $f_mails; |
return $f_mails; |
273 |
// ROU 20030303 |
// ROU 20030303 |
274 |
|
|
275 |
function get_groups($id='') { |
function get_groups($id='') { |
|
global $conn, $conf; |
|
|
$cn = $conf['ludap']['membattr']; |
|
276 |
if ($id) { |
if ($id) { |
277 |
$limit = $conf['ludap']['groupattr']."=".$id.", ".$conf['ludap']['basedn']; |
$limit = "(".$this->conf['groupattr'].'= '.$this->conf['baseattr']."=".$id.", ".$this->conf['basedn'].")"; |
278 |
} else { |
} else { |
279 |
$limit = "($cn=*)"; |
$limit = "(".$this->conf['membattr']."=*)"; |
280 |
} |
} |
281 |
$i = ldap_search($conn,$conf['ludap']['groupdn'],$limit,array($cn)); |
$i = ldap_search($this->conn, $this->conf['groupdn'], $limit, array($this->conf['membattr'])); |
282 |
$info = ldap_get_entries($conn,$i); |
$info = ldap_get_entries($this->conn, $i); |
283 |
for ($x=0; $x<$info["count"]; $x++) { |
for ($x=0; $x<$info["count"]; $x++) { |
284 |
$f_gps[] = utf8_decode($info[$x][$cn][0]); |
$f_gps[] = utf8_decode($info[$x]["{$this->conf['membattr']}"][0]); |
285 |
} |
} |
286 |
return $f_gps; |
return $f_gps; |
287 |
} |
} |
296 |
|
|
297 |
function increment($attr) { |
function increment($attr) { |
298 |
global $conn, $conf; |
global $conn, $conf; |
299 |
$i = ldap_search($conn,$conf['ludap']['basedn'],$conf['ludap']['baseattr']."=*",array($attr)); |
$i = ldap_search($conn,$this->conf['basedn'],$this->conf['baseattr']."=*",array($attr)); |
300 |
$info = ldap_get_entries($conn,$i); |
$info = ldap_get_entries($conn,$i); |
301 |
$max = 0; |
$max = 0; |
302 |
for ($x=0; $x<$info["count"]; $x++) { |
for ($x=0; $x<$info["count"]; $x++) { |
310 |
|
|
311 |
function write_id($fes) { |
function write_id($fes) { |
312 |
global $conn, $conf; |
global $conn, $conf; |
313 |
$in[objectclass] = split(',',$conf[ludap][baseobj]); |
$in['objectclass'] = split(',',$this->conf['baseobj']); |
314 |
foreach ($conf[human] as $clabel=>$cbuff) { |
foreach ($this->schema_user as $clabel=>$cbuff) { |
315 |
list($cl,$ntxt,$ntype,$nfunc) = $cbuff; |
list($cl,$ntxt,$ntype,$nfunc) = $cbuff; |
316 |
if ($ntype == 'primary') { |
if ($ntype == 'primary') { |
317 |
$xres = get_id($fes[$clabel],array($clabel)); |
$xres = get_id($fes[$clabel],array($clabel)); |
333 |
} |
} |
334 |
} |
} |
335 |
if (!$output) { |
if (!$output) { |
336 |
$r = @ldap_add($conn, $conf['ludap']['baseattr'].'='.$fes[id].','.$conf['ludap']['basedn'], $in); |
$r = @ldap_add($conn, $this->conf['baseattr'].'='.$fes[id].','.$this->conf['basedn'], $in); |
337 |
if ($r) { |
if ($r) { |
338 |
return "$id CREATED."; |
return "$id CREATED."; |
339 |
} else { |
} else { |
403 |
} |
} |
404 |
if (!$output) { |
if (!$output) { |
405 |
if (is_array($in)) { |
if (is_array($in)) { |
406 |
$r = ldap_mod_replace($conn,"{$conf['ludap']['baseattr']}={$fes['id']},{$conf['ludap']['basedn']}", $in); |
$r = ldap_mod_replace($conn,"{$this->conf['baseattr']}={$fes['id']},{$this->conf['basedn']}", $in); |
407 |
if ($r) { |
if ($r) { |
408 |
$outgood[] = "$dn MODIFIED. (replace pass)"; |
$outgood[] = "$dn MODIFIED. (replace pass)"; |
409 |
} else { |
} else { |
415 |
} |
} |
416 |
} |
} |
417 |
if (is_array($indel)) { |
if (is_array($indel)) { |
418 |
$r = ldap_mod_del($conn,"{$conf['ludap']['baseattr']}={$fes['id']},{$conf['ludap']['basedn']}", $indel); |
$r = ldap_mod_del($conn,"{$this->conf['baseattr']}={$fes['id']},{$this->conf['basedn']}", $indel); |
419 |
if ($r) { |
if ($r) { |
420 |
$outgood[] = "$dn MODIFIED. (del pass)"; |
$outgood[] = "$dn MODIFIED. (del pass)"; |
421 |
} else { |
} else { |
427 |
} |
} |
428 |
} |
} |
429 |
if (is_array($inadd)) { |
if (is_array($inadd)) { |
430 |
$r = ldap_mod_add($conn,"{$conf['ludap']['baseattr']}={$fes['id']},{$conf['ludap']['basedn']}", $inadd); |
$r = ldap_mod_add($conn,"{$this->conf['baseattr']}={$fes['id']},{$this->conf['basedn']}", $inadd); |
431 |
if ($r) { |
if ($r) { |
432 |
$outgood[] = "$dn MODIFIED. (add pass)"; |
$outgood[] = "$dn MODIFIED. (add pass)"; |
433 |
} else { |
} else { |