[go: up one dir, main page]

File: ldap.php

package info (click to toggle)
sork-accounts 2.1.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 532 kB
  • ctags: 79
  • sloc: sh: 553; php: 360; makefile: 114; perl: 30; xml: 16
file content (156 lines) | stat: -rw-r--r-- 5,273 bytes parent folder | download
1
2
3
4
5
6
7
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * $Horde: accounts/lib/Driver/ldap.php,v 1.5.2.3 2003/02/25 23:33:43 ericr Exp $
 *
 *
 * Copyright 2001-2003 Eric Rostetter <eric.rostetter@physics.utexas.edu>
 *
 * See the enclosed file LICENSE for license information (BSD). If you
 * did not receive this file, see http://www.horde.org/bsdl.php.
 *
 * The ldap class attempts to return user information stored in an ldap
 * directory service.
 *
 * NOTE: This code is probably terrible.  I don't run ldap. I wrote
 * this simply as a template for others to start with.  If you don't
 * like the code, please fix it!  (however, it does work!)
 *
 * @author   Eric Jon Rostetter <eric.rostetter@physics.utexas.edu>
 * @version  $Revision: 1.5.2.3 $
 * @since    Accounts 2.1
 * @package  accounts
 */
class Accounts_Driver_ldap extends Accounts_Driver {

    /** Pointer to the ldap connection. */
    var $_ds;

    /** Hash containing connection parameters. */
    var $_params;

    /**
     * Constructs a new ldap Accounts_Driver object.
     *
     * @param array  $params    A hash containing connection parameters.
     */
    function Accounts_Driver_ldap($params = array())
    {
        $this->_params['host']       = array_key_exists('host', $params)       ? $params['host'] : 'localhost';
        $this->_params['port']       = array_key_exists('port', $params)       ? $params['port'] : 389;
        $this->_params['basedn']     = array_key_exists('basedn', $params)     ? $params['basedn'] : '';
        $this->_params['attr']       = array_key_exists('attr', $params)       ? $params['attr'] : 'uid';
    }



    /**
     * Find the user's fullname
     *
     * @param   $username     The user to find the full name for.
     *
     * @return  mixed         User's Fullname (string) or false (error).
     */
    function getFullname($username) {

        // connect to the ldap server
        $this->_ds = ldap_connect($this->_params['host'],
                                  $this->_params['port']);
        if (!$this->_ds) {
           return PEAR::raiseError(_("Could not connect to ldap server"));
        }

        // bind as anonymous
        $result = @ldap_bind($this->_ds);
        if (!$result) {
          return PEAR::raiseError(_("Could not bind to ldap server"));
        }

        // Get the fullname
        $searchResult = ldap_search($this->_ds, $this->_params['basedn'],
                        $this->_params['attr'] . '=' . $username);
        $information = ldap_get_entries($this->_ds, $searchResult);
        // FIXME: Need to check for errors here if nothing is returned above
        if ($information[0]['cn;lang-es'][0] != '') {
            $name = $information[0]['cn;lang-es'][0];
        } else {
            $name = $information[0]['cn'][0];
        }

        // disconnect from the ldap server
        @ldap_close($this->_ds);

        return (empty($name) ? false : $name);
    }

    /**
     * Find the user's home directory
     *
     * @param   $username     The user whose home directory to find.
     *
     * @return  mixed         User's Home (string) or false (error).
     */
    function getHome($username) {

        // connect to the ldap server
        $this->_ds = ldap_connect($this->_params['host'],
                                  $this->_params['port']);
        if (!$this->_ds) {
           return PEAR::raiseError(_("Could not connect to ldap server"));
        }

        // bind as anonymous
        $result = @ldap_bind($this->_ds);
        if (!$result) {
          return PEAR::raiseError(_("Could not bind to ldap server"));
        }

        // Get the home directory
        $searchResult = ldap_search($this->_ds, $this->_params['basedn'],
                        $this->_params['attr'] . '=' . $username);
        $information = ldap_get_entries($this->_ds, $searchResult);
        // FIXME: Need to check for errors here if nothing is returned above
        $homedir = $information[0]['homedirectory'][0];

        // disconnect from the ldap server
        @ldap_close($this->_ds);

        return (empty($homedir) ? false : $homedir);
    }

    /**
     * Find the user's shell
     *
     * @param   $username     The user whose shell to find.
     *
     * @return  mixed         User's Shell (string) or false (error).
     */
    function getShell($username) {

        // connect to the ldap server
        $this->_ds = ldap_connect($this->_params['host'],
                                  $this->_params['port']);
        if (!$this->_ds) {
           return PEAR::raiseError(_("Could not connect to ldap server"));
        }

        // bind as anonymous
        $result = @ldap_bind($this->_ds);
        if (!$result) {
          return PEAR::raiseError(_("Could not bind to ldap server"));
        }

        // Get the shell
        $searchResult = ldap_search($this->_ds, $this->_params['basedn'],
                        $this->_params['attr'] . '=' . $username);
        $information = ldap_get_entries($this->_ds, $searchResult);
        // FIXME: Need to check for errors here if nothing is returned above
        $shell = $information[0]['loginshell'][0];

        // disconnect from the ldap server
        @ldap_close($this->_ds);

        return (empty($shell) ? false : $shell);
     }

}
?>