1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14:
15: if (!function_exists('imap_open')) {
16: die("drivers_rimap: php-imap required.");
17: }
18:
19: require_once("auth-functions.php");
20:
21: 22: 23:
24: class rimapPamDriver
25: {
26: 27: 28:
29:
30:
31:
32:
33: 34: 35: 36: 37:
38: function __construct($imap_url)
39: {
40: global $c;
41: if (empty($imap_url)){
42: $c->messages[] = sprintf(i18n('drivers_rimap : imap_url parameter not configured in /etc/davical/*-conf.php'));
43: $this->valid=false;
44: return ;
45: }
46: }
47: }
48:
49:
50: 51: 52:
53: function RIMAP_check($username, $password ){
54: global $c;
55:
56: $imap_username = $username;
57: if ( function_exists('mb_convert_encoding') ) {
58: $imap_username = mb_convert_encoding($imap_username, "UTF7-IMAP",mb_detect_encoding($imap_username));
59: }
60: else {
61: $imap_username = imap_utf7_encode($imap_username);
62: }
63:
64:
65:
66: $imap_url = $c->authenticate_hook['config']['imap_url'];
67: $auth_result = "ERR";
68:
69: $imap_stream = @imap_open($imap_url, $imap_username, $password, OP_HALFOPEN);
70:
71: if ( $imap_stream ) {
72:
73: imap_close($imap_stream);
74:
75: $auth_result = "OK";
76: }
77:
78: if ( $auth_result == "OK") {
79: $principal = new Principal('username',$username);
80: if ( ! $principal->Exists() ) {
81: dbg_error_log( "PAM", "Principal '%s' doesn't exist in local DB, we need to create it",$username );
82: if ( strstr($username, '@') ) {
83: $name_arr = explode('@', $username);
84: $fullname = ucfirst(strtolower($name_arr[0]));
85: $email = $username;
86: }
87: else {
88: $fullname = ucfirst(strtolower($username));
89: $email = $username . "@" . $c->authenticate_hook['config']['email_base'];
90: }
91:
92: $principal->Create( array(
93: 'username' => $username,
94: 'user_active' => true,
95: 'email' => $email,
96: 'fullname' => ucfirst($fullname)
97: ));
98: if ( ! $principal->Exists() ) {
99: dbg_error_log( "PAM", "Unable to create local principal for '%s'", $username );
100: return false;
101: }
102: CreateHomeCollections($username, $c->default_timezone);
103: CreateDefaultRelationships($username);
104: }
105: return $principal;
106: }
107: else {
108: dbg_error_log( "PAM", "User %s is not a valid username (or password was wrong)", $username );
109: return false;
110: }
111:
112: }
113: