LDAPquery.php 1.16 KB
Newer Older
jlopez's avatar
jlopez committed
1
2
3
4
5
6
<?php

/**
 * Connection to ldap
 */

remy's avatar
remy committed
7
require_once(__DIR__.'/../inc/conf/Conf.php');
jlopez's avatar
jlopez committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

class LDAPquery {

    var $ldaph = NULL;

    public function __construct() {
        $this->openConnection();
    }

    public function openConnection() {
        $this->ldaph = ldap_connect(Conf::$LDAP_HOSTNAME);
        if(!$this->ldaph) {
            die("Impossible de se connecter au serveur LDAP ".Conf::$LDAP_HOSTNAME);
        }
    }

    public function bind() {
        ldap_set_option($this->ldaph, LDAP_OPT_PROTOCOL_VERSION, 3);
        $r = ldap_bind($this->ldaph, Conf::$LDAP_ADMIN, Conf::$LDAP_PP);
        return $r;
    }

    public function verifyPass($ul, $up) {
        $r = $this->bind();
        if($r) {
            $sr=ldap_search($this->ldaph, Conf::$LDAP_USER, "(cn=*)");
            $info = ldap_get_entries($this->ldaph, $sr);
            for ($i=0; $i<$info["count"]; $i++) {

                $login = $info[$i]["cn"][0];

                if($login == $ul) {
                    if($info[$i]["userpassword"][0] == Conf::ldapEncodePass($up)) {
                        return 1;
                    };
                }
            }
        }

        return 0;
    }

}