cleanup
[bMailZu.git] / lib / ExchAuth.class.php
blob30536699a7071cf2eaeec847d786b09f446a3bb1
1 <?php
2 /**
3 * ExchAuth class
4 * @version 07-23-2005
5 * @Author Bogdan Baliuc <b.baliuc@rogers.com>
6 * @package ExchAuth
8 * Copyright (C) 2005 - 2007 MailZu
9 * License: GPL, see LICENSE
11 /**
12 * Base directory of application
14 @define('BASE_DIR', dirname(__FILE__) . '/..');
15 /**
16 * CmnFns class
18 include_once('CmnFns.class.php');
20 /**
21 * Provide all database access/manipulation functionality for Exchange Auth
23 class ExchAuth {
25 // The exchange hostname with port (hostname[:port])
26 var $exchHost;
27 // The exchange LDAP URI (ldap://hostname[:port])
28 var $exchLDAP;
29 // The user's logon name
30 var $logonName;
31 // The user's first name
32 var $firstName;
33 // The user's mail address(es)
34 var $emailAddress;
36 var $err_msg = '';
38 /**
39 * Constructor to initialize object
40 * @param none
42 function ExchAuth() {
43 global $conf;
45 $this->exchHost = $conf['auth']['exch_host'];
46 $this->exchLDAP = $conf['auth']['exch_ldap'];
49 // User methods -------------------------------------------
51 /**
52 * Authenticates user
53 * @param string $username
54 * @param string $password
55 * @param string $domain
56 * @return boolean
58 function authUser($username, $password, $domain) {
60 $fulluser = $domain.'/'.$username;
61 $mbox = imap_open('{'.$this->exchHost.'/imap}Inbox', $fulluser, $password);
62 if ($mbox === false) {
63 $this->err_msg = translate('Invalid Username/Password');
64 return false;
65 } else {
66 $ignore = imap_errors();
67 imap_close($mbox);
69 $ldapconn = ldap_connect($this->exchLDAP);
70 if ($ldapconn === false) {
71 $this->err_msg = translate('Can not connect to LDAP server');
72 return false;
74 ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
75 ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
76 $ldapbind = ldap_bind($ldapconn);
77 if ($ldapbind === false) {
78 $this->err_msg = translate('Can not bind to LDAP server');
79 return false;
81 $ldapattr = array('cn', 'rfc822Mailbox' ,'otherMailbox');
82 $read = ldap_search($ldapconn, '', '(uid='.$username.')', $ldapattr);
83 if ($read === false) {
84 $this->err_msg = translate('Unable to search LDAP server');
85 return false;
87 $info = ldap_get_entries($ldapconn, $read);
88 $this->logonName = strtolower($username);
89 $this->firstName = $info[0]['cn'][0];
90 $this->emailAddress[] = strtolower($info[0]['rfc822mailbox'][0]);
91 for ($i=0; $i<$info[0]['othermailbox']['count']; $i++) {
92 $data = $info[0]['othermailbox'][$i];
93 if (strncasecmp($data, 'smtp$', 5) == 0) {
94 $this->emailAddress[] = strtolower(substr($data, 5));
97 ldap_close($ldapconn);
98 return true;
102 * Returns the last error message
103 * @param none
104 * @return last error message generated
106 function get_err() {
107 return $this->err_msg;
110 // Helper methods -------------------------------------------
113 * Returns user information
114 * @return array containing user information
116 function getUserData() {
117 $return = array(
118 'logonName' => $this->logonName,
119 'firstName' => $this->firstName,
120 'emailAddress' => $this->emailAddress
122 return $return;