Subversion Repositories Applications.papyrus

Rev

Rev 1173 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1173 jp_milcent 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3
 
4
/**
5
 * Storage driver for use against IMAP servers
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: This source file is subject to version 3.01 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
15
 * @category   Authentication
16
 * @package    Auth
1713 jp_milcent 17
 * @author     Jeroen Houben <jeroen@terena.nl>
1173 jp_milcent 18
 * @author     Adam Ashley <aashley@php.net>
19
 * @copyright  2001-2006 The PHP Group
20
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
1713 jp_milcent 21
 * @version    CVS: $Id: IMAP.php,v 1.3 2007-11-19 15:11:00 jp_milcent Exp $
1173 jp_milcent 22
 * @link       http://pear.php.net/package/Auth
23
 * @since      File available since Release 1.2.0
24
 */
25
 
26
/**
1713 jp_milcent 27
 * Include Auth_Container base class
1173 jp_milcent 28
 */
29
require_once "Auth/Container.php";
30
 
31
/**
32
 * Include PEAR class for error handling
33
 */
34
require_once "PEAR.php";
35
 
36
/**
37
 * Storage driver for fetching login data from an IMAP server
38
 *
39
 * This class is based on LDAP containers, but it very simple.
40
 * By default it connects to localhost:143
41
 * The constructor will first check if the host:port combination is
42
 * actually reachable. This behaviour can be disabled.
43
 * It then tries to create an IMAP stream (without opening a mailbox)
44
 * If you wish to pass extended options to the connections, you may
45
 * do so by specifying protocol options.
46
 *
47
 * To use this storage containers, you have to use the
48
 * following syntax:
49
 *
50
 * <?php
51
 * ...
52
 * $params = array(
53
 * 'host'       => 'mail.example.com',
54
 * 'port'       => 143,
55
 * );
56
 * $myAuth = new Auth('IMAP', $params);
57
 * ...
58
 *
59
 * By default we connect without any protocol options set. However, some
60
 * servers require you to connect with the notls or norsh options set.
61
 * To do this you need to add the following value to the params array:
62
 * 'baseDSN'   => '/imap/notls/norsh'
63
 *
64
 * To connect to an SSL IMAP server:
65
 * 'baseDSN'   => '/imap/ssl'
66
 *
67
 * To connect to an SSL IMAP server with a self-signed certificate:
68
 * 'baseDSN'   => '/imap/ssl/novalidate-cert'
69
 *
70
 * Further options may be available and can be found on the php site at
71
 * http://www.php.net/manual/function.imap-open.php
72
 *
73
 * @category   Authentication
74
 * @package    Auth
75
 * @author     Jeroen Houben <jeroen@terena.nl>
76
 * @author     Cipriano Groenendal <cipri@campai.nl>
77
 * @author     Adam Ashley <aashley@php.net>
78
 * @copyright  2001-2006 The PHP Group
79
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
1713 jp_milcent 80
 * @version    Release: 1.5.4  File: $Revision: 1.3 $
1173 jp_milcent 81
 * @link       http://pear.php.net/package/Auth
82
 * @since      Class available since Release 1.2.0
83
 */
84
class Auth_Container_IMAP extends Auth_Container
85
{
86
 
87
    // {{{ properties
88
 
89
    /**
90
     * Options for the class
91
     * @var array
92
     */
93
    var $options = array();
94
 
95
    // }}}
96
    // {{{ Auth_Container_IMAP() [constructor]
97
 
98
    /**
99
     * Constructor of the container class
100
     *
101
     * @param  $params  associative array with host, port, baseDSN, checkServer
102
     *                  and userattr key
103
     * @return object Returns an error object if something went wrong
104
     * @todo Use PEAR Net_IMAP if IMAP extension not loaded
105
     */
106
    function Auth_Container_IMAP($params)
107
    {
108
        if (!extension_loaded('imap')) {
109
            return PEAR::raiseError('Cannot use IMAP authentication, '
110
                    .'IMAP extension not loaded!', 41, PEAR_ERROR_DIE);
111
        }
112
        $this->_setDefaults();
113
 
114
        // set parameters (if any)
115
        if (is_array($params)) {
116
            $this->_parseOptions($params);
117
        }
118
 
119
        if ($this->options['checkServer']) {
120
            $this->_checkServer($this->options['timeout']);
121
        }
122
        return true;
123
    }
124
 
125
    // }}}
126
    // {{{ _setDefaults()
127
 
128
    /**
129
     * Set some default options
130
     *
131
     * @access private
132
     */
133
    function _setDefaults()
134
    {
135
        $this->options['host'] = 'localhost';
136
        $this->options['port'] = 143;
137
        $this->options['baseDSN'] = '';
138
        $this->options['checkServer'] = true;
139
        $this->options['timeout'] = 20;
140
    }
141
 
142
    // }}}
143
    // {{{ _checkServer()
144
 
145
    /**
146
     * Check if the given server and port are reachable
147
     *
148
     * @access private
149
     */
150
    function _checkServer() {
1713 jp_milcent 151
        $this->log('Auth_Container_IMAP::_checkServer() called.', AUTH_LOG_DEBUG);
1173 jp_milcent 152
        $fp = @fsockopen ($this->options['host'], $this->options['port'],
153
                          $errno, $errstr, $this->options['timeout']);
154
        if (is_resource($fp)) {
155
            @fclose($fp);
156
        } else {
157
            $message = "Error connecting to IMAP server "
158
                . $this->options['host']
159
                . ":" . $this->options['port'];
160
            return PEAR::raiseError($message, 41);
161
        }
162
    }
163
 
164
    // }}}
165
    // {{{ _parseOptions()
166
 
167
    /**
168
     * Parse options passed to the container class
169
     *
170
     * @access private
171
     * @param  array
172
     */
173
    function _parseOptions($array)
174
    {
175
        foreach ($array as $key => $value) {
176
            $this->options[$key] = $value;
177
        }
178
    }
179
 
180
    // }}}
181
    // {{{ fetchData()
182
 
183
    /**
184
     * Try to open a IMAP stream using $username / $password
185
     *
186
     * @param  string Username
187
     * @param  string Password
188
     * @return boolean
189
     */
190
    function fetchData($username, $password)
191
    {
1713 jp_milcent 192
        $this->log('Auth_Container_IMAP::fetchData() called.', AUTH_LOG_DEBUG);
1173 jp_milcent 193
        $dsn = '{'.$this->options['host'].':'.$this->options['port'].$this->options['baseDSN'].'}';
194
        $conn = @imap_open ($dsn, $username, $password, OP_HALFOPEN);
195
        if (is_resource($conn)) {
1713 jp_milcent 196
            $this->log('Successfully connected to IMAP server.', AUTH_LOG_DEBUG);
1173 jp_milcent 197
            $this->activeUser = $username;
198
            @imap_close($conn);
199
            return true;
200
        } else {
1713 jp_milcent 201
            $this->log('Connection to IMAP server failed.', AUTH_LOG_DEBUG);
1173 jp_milcent 202
            $this->activeUser = '';
203
            return false;
204
        }
205
    }
206
 
207
    // }}}
208
 
209
}
210
?>