Thu, 13 May 2010 09:31:01 -0500
Initial commit
0 | 1 | <?php |
2 | ||
3 | Class RegistrationBackend { | |
4 | ||
5 | protected $dbconn; | |
6 | ||
7 | protected $hostname = "localhost"; | |
8 | protected $port = 389; | |
9 | protected $bind_dn; | |
10 | protected $bind_pw; | |
11 | protected $base_dn; | |
12 | protected $objectClass = array( | |
13 | "cmuSaslUser", | |
14 | "mboxUser", | |
15 | "inetUser", | |
16 | "inetOrgPerson", | |
17 | "organizationalPerson", | |
18 | "person" | |
19 | ); | |
20 | ||
21 | protected $debug = FALSE; | |
22 | ||
23 | private $ldap; | |
24 | ||
25 | function init($config) | |
26 | { | |
27 | if(array_key_exists('ldapHostname', $config)) | |
28 | $this->hostname = $config['ldapHostname']; | |
29 | if(array_key_exists('ldapPort', $config)) | |
30 | $this->port = $config['ldapPort']; | |
31 | if(array_key_exists('ldapBindDN', $config)) | |
32 | $this->bind_dn = $config['ldapBindDN']; | |
33 | if(array_key_exists('ldapBindPass', $config)) | |
34 | $this->bind_pw = $config['ldapBindPass']; | |
35 | if(array_key_exists('ldapBaseDN', $config)) | |
36 | $this->base_dn = $config['ldapBaseDN']; | |
37 | ||
38 | if($config['debug']) | |
39 | $this->debug = TRUE; | |
40 | ||
41 | $this->ldap = ldap_connect($this->hostname, $this->port) | |
42 | or die("Cannot connect to DSA"); | |
43 | ||
44 | ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3); | |
45 | ||
46 | ldap_bind($this->ldap, $this->bind_dn, $this->bind_pw) | |
47 | or die("Cannot bind to DSA"); | |
48 | } | |
49 | ||
50 | function validate($user, $pass) | |
51 | { | |
52 | if(strcspn($user, " \"#+,;<=>\\") !== strlen($user)) | |
53 | return "Your username contains invalid characters."; | |
54 | return NULL; | |
55 | } | |
56 | ||
57 | function exists($user) | |
58 | { | |
59 | $dn = "uid=" . $user . "," . $this->base_dn; | |
60 | $res = @ldap_read($this->ldap, $dn, "objectClass=inetOrgPerson"); | |
61 | if($res === FALSE) | |
62 | return FALSE; | |
63 | return TRUE; | |
64 | } | |
65 | ||
66 | function create($user, $pass) | |
67 | { | |
68 | $dn = "uid=" . $user . "," . $this->base_dn; | |
69 | ||
70 | $attrs["objectClass"] = $this->objectClass; | |
71 | $attrs["uid"] = $user; | |
72 | $attrs["userPassword"] = $pass; | |
73 | ||
74 | # X.500 person class requires a Surname. | |
75 | $attrs["sn"] = $user; | |
76 | ||
77 | # Something else (?) requires a CommonName. | |
78 | $attrs["cn"] = $attrs["sn"]; | |
79 | ||
80 | if (@ldap_add($this->ldap, $dn, $attrs)) | |
81 | { | |
82 | return TRUE; | |
83 | } | |
84 | else | |
85 | { | |
86 | if($this->debug) | |
87 | { | |
88 | $errno = ldap_errno($this->ldap); | |
89 | echo "<p>Exciting error number " . $errno . ": <i>" . ldap_err2str($errno) . "</i></p>"; | |
90 | echo "<p>DN: " . $dn . "</p>"; | |
91 | echo "<p>Attrs: <pre>"; | |
92 | print_r($attrs); | |
93 | echo "</pre></p>"; | |
94 | } | |
95 | return FALSE; | |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | function close() | |
101 | { | |
102 | ldap_unbind($this->ldap); | |
103 | } | |
104 | } | |
105 | ||
106 | ?> |