Subversion Repositories Applications.papyrus

Rev

Rev 1087 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Jeroen Houben <jeroen@terena.nl>                             |
17
// +----------------------------------------------------------------------+
18
//
19
// $Id: IMAP.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
20
//
21
 
22
require_once "Auth/Container.php";
23
require_once "PEAR.php";
24
 
25
/**
26
 * Storage driver for fetching login data from an IMAP server
27
 *
28
 * This class is based on LDAP containers, but it very simple.
29
 * By default it connects to localhost:143
30
 * The constructor will first check if the host:port combination is
31
 * actually reachable. This behaviour can be disabled.
32
 * It then tries to create an IMAP stream (without opening a mailbox)
33
 * If you wish to pass extended options to the connections, you may
34
 * do so by specifying protocol options.
35
 *
36
 * To use this storage containers, you have to use the
37
 * following syntax:
38
 *
39
 * <?php
40
 * ...
41
 * $params = array(
42
 * 'host'       => 'mail.example.com',
43
 * 'port'       => 143,
44
 * );
45
 * $myAuth = new Auth('IMAP', $params);
46
 * ....
47
 *
48
 * By default we connect without any protocol options set. However, some
49
 * servers require you to connect with the notls or norsh options set.
50
 * To do this you need to add the following value to the params array:
51
 * 'baseDSN'   => '/imap/notls/norsh'
52
 *
53
 * To connect to an SSL IMAP server:
54
 * 'baseDSN'   => '/imap/ssl'
55
 *
56
 * To connect to an SSL IMAP server with a self-signed certificate:
57
 * 'baseDSN'   => '/imap/ssl/novalidate-cert'
58
 *
59
 * Further options may be available and can be found on the php site at
60
 * http://www.php.net/manual/function.imap-open.php
61
 *
62
 */
63
 
64
/*
65
 *
66
 * @author   Jeroen Houben <jeroen@terena.nl>, Cipriano Groenendal <cipri@campai.nl>
67
 * @package  Auth
68
 * @version  $Revision: 1.1 $
69
 */
70
class Auth_Container_IMAP extends Auth_Container
71
{
72
    /**
73
     * Options for the class
74
     * @var array
75
     */
76
    var $options = array();
77
 
78
    /**
79
     * Constructor of the container class
80
     *
81
     * @param  $params, associative hash with host,port,basedn and userattr key
82
     * @param  $params, associative array with host, port, baseDSN, checkServer key.
83
     * @return object Returns an error object if something went wrong
84
     */
85
    function Auth_Container_IMAP($params)
86
    {
87
        if (!extension_loaded('imap')) {
88
            return PEAR::raiseError("Cannot use IMAP authentication, IMAP extension not loaded!",
89
                                    41, PEAR_ERROR_DIE);
90
        }
91
        $this->_setDefaults();
92
 
93
        // set parameters (if any)
94
        if (is_array($params)) {
95
            $this->_parseOptions($params);
96
        }
97
        if ($this->options['checkServer']) {
98
            $this->_checkServer($this->options['timeout']);
99
        }
100
        return true;
101
    }
102
 
103
    /**
104
     * Set some default options
105
     *
106
     * @access private
107
     */
108
    function _setDefaults()
109
    {
110
        $this->options['host'] = 'localhost';
111
        $this->options['port'] = 143;
112
        $this->options['baseDSN'] = '';
113
        $this->options['checkServer'] = true;
114
        $this->options['timeout'] = 20;
115
    }
116
 
117
 
118
    /**
119
     * Check if the given server and port are reachable
120
     *
121
     * @access private
122
     */
123
    function _checkServer() {
124
        $fp = @fsockopen ($this->options['host'], $this->options['port'],
125
                          $errno, $errstr, $timeout);
126
        if (is_resource($fp)) {
127
            @fclose($fp);
128
        } else {
129
            $message = "Error connecting to IMAP server "
130
                . $this->options['host']
131
                . ":" . $this->options['port'];
132
            return PEAR::raiseError($message, 41, PEAR_ERROR_DIE);
133
        }
134
    }
135
 
136
    /**
137
     * Parse options passed to the container class
138
     *
139
     * @access private
140
     * @param  array
141
     */
142
    function _parseOptions($array)
143
    {
144
        foreach ($array as $key => $value) {
145
            $this->options[$key] = $value;
146
        }
147
    }
148
 
149
    /**
150
     * Try to open a IMAP stream using $username / $password
151
     *
152
     * @param  string Username
153
     * @param  string Password
154
     * @return boolean
155
     */
156
    function fetchData($username, $password)
157
    {
158
        $dsn = '{'.$this->options['host'].':'.$this->options['port'].$this->options['baseDSN'].'}';
159
        $conn = @imap_open ($dsn, $username, $password, OP_HALFOPEN);
160
        if (is_resource($conn)){
161
            $this->activeUser = $username;
162
            @imap_close($conn);
163
            return true;
164
        } else {
165
            $this->activeUser = '';
166
            return false;
167
        }
168
    }
169
}
170
?>