summaryrefslogtreecommitdiff
path: root/auth/imap/auth.php
blob: 28872548b5c17cefba00409e487fc705bb662aeb (plain)
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
<?php
/**
 * $Header$
 *
 * @package users
 */

/**
 * Class that manages the imap autentication method
 *
 * @package users
 * @subpackage auth
 */
namespace Bitweaver\Users;

use Bitweaver\KernelTools;

class IMAPAuth extends BaseAuth {

	function __construct() {
		parent::__construct('imap');
	}

	function validate($user,$pass,$challenge,$response) {
		parent::validate($user,$pass,$challenge,$response);
		$mailbox = '{' . $this->mConfig['server'];
		if ($this->mConfig["ssl"]) {
			$mailbox .= "/ssl";
			if ($this->mConfig["sslvalidate"]) {
				$mailbox .= "/validate-cert";
			} else {
				$mailbox .= "/novalidate-cert";
			}
		}
		$mailbox .= ':'.$this->mConfig["port"].'}INBOX';

		$imapauth = @imap_open($mailbox,$user , $pass);
		if (!$imapauth) {
			$this->mErrors['login']=imap_errors();
			$ret=USER_NOT_FOUND;
		} else {
			$ret=USER_VALID;
			$this->mInfo["real_name"] = $user;
			if(empty($this->mConfig["email"])) {
				$this->mInfo["email"] = $user;
			} else {
				$info=['login'=>$user];
				$replace_func = create_function('$matches','$info = '.var_export($info,true).';
							$m = $matches[0];
							$m = substr($m,1,strlen($m)-2);
							if(empty($info[$m])) return "";
							return strtolower($info[$m]);');
				$this->mInfo["email"] = preg_replace_callback('/%.*?%/',$replace_func,$this->mConfig["email"]);
			}
			imap_close($imapauth);
		}
		return $ret;
	}

	function isSupported() {
		$ret = true;
		if (!function_exists('imap_open')) {
			$this->mErrors['support']=KernelTools::tra("IMAP Authentication is not supported as PHP IMAP Extention not loaded.");
			$ret = false;
		}
		return $ret;
	}

	function createUser(&$userattr) {
		$this->mErrors['create']=KernelTools::tra("Cannot create users in an IMAP Server.");
		return false;
	}

	function canManageAuth() {
		$this->mErrors[]=KernelTools::tra("Cannot create users in an IMAP Server.");
		return false;
	}

	function getSettings() {
		return [
		'users_imap_server' => [
			'label' => "IMAP Server",
			'type' => "text",
			'note' => "",
			'default' => '',
		],
		'users_imap_ssl' => [
			'label' => "Connect Using SSL",
			'type' => "checkbox",
			'note' => "",
			'default' => 'y',
		],
		'users_imap_sslvalidate' => [
			'label' => "Require SSL Certificate to be valid",
			'type' => "checkbox",
			'note' => "",
			'default' => 'n',
		],
		'users_imap_port' => [
			'label' => "IMAP Port",
			'type' => "text",
			'note' => "",
			'default' => '993',
		],
		'users_imap_email' => [
			'label' => "LDAP User E-Mail Address",
			'type' => "text",
			'note' => "If empty the login is used.<br />Otherwise all %login% is replaced with the login name, and the result used as the email address.<br />Please remember to include the @ sign",
			'default' => "%login%@redhat.com",
		],
	];
	}
}