|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EGroupware API - Authentication agains mail server |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://www.egroupware.org |
|
6
|
|
|
* @author Dan Kuykendall <[email protected]> |
|
7
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall |
|
8
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php LGPL - GNU Lesser General Public License |
|
9
|
|
|
* @package api |
|
10
|
|
|
* @subpackage authentication |
|
11
|
|
|
* @version $Id$ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace EGroupware\Api\Auth; |
|
15
|
|
|
|
|
16
|
|
|
use Horde_Imap_Client_Socket, Horde_Imap_Client_Exception; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Authentication agains mail server |
|
20
|
|
|
*/ |
|
21
|
|
|
class Mail implements Backend |
|
22
|
|
|
{ |
|
23
|
|
|
var $previous_login = -1; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* password authentication |
|
27
|
|
|
* |
|
28
|
|
|
* We are always trying to establish a TLS connection, but we do not |
|
29
|
|
|
* (yet) validate certs, as most PHP installs dont validate them! |
|
30
|
|
|
* For imap/pop3 we are NOT adding notls to use STARTTLS if server supports it. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $username username of account to authenticate |
|
33
|
|
|
* @param string $passwd corresponding password |
|
34
|
|
|
* @param string $passwd_type ='text' 'text' for cleartext passwords (default) |
|
35
|
|
|
* @return boolean true if successful authenticated, false otherwise |
|
36
|
|
|
*/ |
|
37
|
|
|
function authenticate($username, $passwd, $passwd_type='text') |
|
38
|
|
|
{ |
|
39
|
|
|
unset($passwd_type); // not used but required by function signature |
|
40
|
|
|
|
|
41
|
|
|
switch ($GLOBALS['egw_info']['server']['mail_login_type']) |
|
42
|
|
|
{ |
|
43
|
|
|
case 'vmailmgr': |
|
44
|
|
|
$username = $username . '@' . $GLOBALS['egw_info']['server']['mail_suffix']; |
|
45
|
|
|
break; |
|
46
|
|
|
case 'email': |
|
47
|
|
|
$username = $GLOBALS['egw']->accounts->id2name($username, 'account_email'); |
|
48
|
|
|
break; |
|
49
|
|
|
case 'uidNumber': |
|
50
|
|
|
$username = 'u'.$GLOBALS['egw']->accounts->name2id($username); |
|
51
|
|
|
break; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
list($host, $port) = explode(':', $GLOBALS['egw_info']['server']['mail_server']); |
|
55
|
|
|
|
|
56
|
|
|
// use Horde_Imap_Client by default, to not require PHP imap extension anymore |
|
57
|
|
|
if (class_exists('Horde_Imap_Client_Socket') && !in_array($GLOBALS['egw_info']['server']['mail_server_type'], array('pop', 'pops'))) |
|
58
|
|
|
{ |
|
59
|
|
|
$imap = new Horde_Imap_Client_Socket(array( |
|
60
|
|
|
'username' => $username, |
|
61
|
|
|
'password' => $passwd, |
|
62
|
|
|
'hostspec' => $host, |
|
63
|
|
|
'port' => $port ? $port : ($GLOBALS['egw_info']['server']['mail_server_type'] == 'imaps' ? 993 : 143), |
|
64
|
|
|
'secure' => $GLOBALS['egw_info']['server']['mail_server_type'] == 'imaps' ? 'ssl' : 'tls', |
|
65
|
|
|
)); |
|
66
|
|
|
try { |
|
67
|
|
|
$imap->login(); |
|
68
|
|
|
$mailauth = true; |
|
69
|
|
|
$imap->logout(); |
|
70
|
|
|
} |
|
71
|
|
|
catch(Horde_Imap_Client_Exception $e) { |
|
72
|
|
|
// throw everything but authentication failed as exception |
|
73
|
|
|
if ($e->getCode() != Horde_Imap_Client_Exception::LOGIN_AUTHENTICATIONFAILED) throw $e; |
|
74
|
|
|
|
|
75
|
|
|
$mailauth = false; |
|
76
|
|
|
} |
|
77
|
|
|
//error_log(__METHOD__."('$username', \$passwd) checked via Horde code returning ".array2string($mailauth)); |
|
78
|
|
|
} |
|
79
|
|
|
else |
|
80
|
|
|
{ |
|
81
|
|
|
check_load_extension('imap', true); |
|
82
|
|
|
|
|
83
|
|
|
switch ($GLOBALS['egw_info']['server']['mail_server_type']) |
|
84
|
|
|
{ |
|
85
|
|
|
case 'imap': |
|
86
|
|
|
default: |
|
87
|
|
|
if (!isset($port)) $port = 143; |
|
88
|
|
|
$mailauth = imap_open('{'.$host.':'.$port.'/imap/novalidate-cert}INBOX', $username , $passwd); |
|
89
|
|
|
break; |
|
90
|
|
|
case 'imaps': |
|
91
|
|
|
if (!isset($port)) $port = 993; |
|
92
|
|
|
$mailauth = imap_open('{'.$host.'/imap/ssl/novalidate-cert:'.$port.'}INBOX', $username , $passwd); |
|
93
|
|
|
break; |
|
94
|
|
|
case 'pop3': |
|
95
|
|
|
if (!isset($port)) $port = 110; |
|
96
|
|
|
$mailauth = imap_open('{'.$host.'/pop3/novalidate-cert:'.$port.'}INBOX', $username , $passwd); |
|
97
|
|
|
break; |
|
98
|
|
|
case 'pop3s': |
|
99
|
|
|
if (!isset($port)) $port = 995; |
|
100
|
|
|
$mailauth = imap_open('{'.$host.'/pop3/ssl/novalidate-cert:'.$port.'}INBOX', $username , $passwd); |
|
101
|
|
|
break; |
|
102
|
|
|
} |
|
103
|
|
|
if ($mailauth) imap_close($mailauth); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
return !!$mailauth; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* changes password |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $old_passwd must be cleartext or empty to not to be checked |
|
112
|
|
|
* @param string $new_passwd must be cleartext |
|
113
|
|
|
* @param int $account_id =0 account id of user whose passwd should be changed |
|
114
|
|
|
* @return boolean true if password successful changed, false otherwise |
|
115
|
|
|
*/ |
|
116
|
|
|
function change_password($old_passwd, $new_passwd, $account_id=0) |
|
117
|
|
|
{ |
|
118
|
|
|
unset($old_passwd, $new_passwd, $account_id); // not used but required by function sigature |
|
119
|
|
|
|
|
120
|
|
|
return False; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|