| 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 [email protected] so we can mail you a copy immediately. |
|---|
| 14 | * |
|---|
| 15 | * @category Authentication |
|---|
| 16 | * @package Auth |
|---|
| 17 | * @author Jeroen Houben <[email protected]> |
|---|
| 18 | * @author Adam Ashley <[email protected]> |
|---|
| 19 | * @copyright 2001-2006 The PHP Group |
|---|
| 20 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 21 | * @version CVS: $Id$ |
|---|
| 22 | * @link http://pear.php.net/package/Auth |
|---|
| 23 | * @since File available since Release 1.2.0 |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Include Auth_Container base class |
|---|
| 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 <[email protected]> |
|---|
| 76 | * @author Cipriano Groenendal <[email protected]> |
|---|
| 77 | * @author Adam Ashley <[email protected]> |
|---|
| 78 | * @copyright 2001-2006 The PHP Group |
|---|
| 79 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 80 | * @version Release: 1.4.2 File: $Revision: 8713 $ |
|---|
| 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() { |
|---|
| 151 | $fp = @fsockopen ($this->options['host'], $this->options['port'], |
|---|
| 152 | $errno, $errstr, $this->options['timeout']); |
|---|
| 153 | if (is_resource($fp)) { |
|---|
| 154 | @fclose($fp); |
|---|
| 155 | } else { |
|---|
| 156 | $message = "Error connecting to IMAP server " |
|---|
| 157 | . $this->options['host'] |
|---|
| 158 | . ":" . $this->options['port']; |
|---|
| 159 | return PEAR::raiseError($message, 41); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | // }}} |
|---|
| 164 | // {{{ _parseOptions() |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| 167 | * Parse options passed to the container class |
|---|
| 168 | * |
|---|
| 169 | * @access private |
|---|
| 170 | * @param array |
|---|
| 171 | */ |
|---|
| 172 | function _parseOptions($array) |
|---|
| 173 | { |
|---|
| 174 | foreach ($array as $key => $value) { |
|---|
| 175 | $this->options[$key] = $value; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | // }}} |
|---|
| 180 | // {{{ fetchData() |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Try to open a IMAP stream using $username / $password |
|---|
| 184 | * |
|---|
| 185 | * @param string Username |
|---|
| 186 | * @param string Password |
|---|
| 187 | * @return boolean |
|---|
| 188 | */ |
|---|
| 189 | function fetchData($username, $password) |
|---|
| 190 | { |
|---|
| 191 | $dsn = '{'.$this->options['host'].':'.$this->options['port'].$this->options['baseDSN'].'}'; |
|---|
| 192 | $conn = @imap_open ($dsn, $username, $password, OP_HALFOPEN); |
|---|
| 193 | if (is_resource($conn)) { |
|---|
| 194 | $this->activeUser = $username; |
|---|
| 195 | @imap_close($conn); |
|---|
| 196 | return true; |
|---|
| 197 | } else { |
|---|
| 198 | $this->activeUser = ''; |
|---|
| 199 | return false; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | // }}} |
|---|
| 204 | |
|---|
| 205 | } |
|---|
| 206 | ?> |
|---|