ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
IMAP.php
Go to the documentation of this file.
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3 
29 require_once "Auth/Container.php";
30 
34 require_once "PEAR.php";
35 
85 {
86 
87  // {{{ properties
88 
93  var $options = array();
94 
95  // }}}
96  // {{{ Auth_Container_IMAP() [constructor]
97 
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 
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 
150  function _checkServer() {
151  $this->log('Auth_Container_IMAP::_checkServer() called.', AUTH_LOG_DEBUG);
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 
173  function _parseOptions($array)
174  {
175  foreach ($array as $key => $value) {
176  $this->options[$key] = $value;
177  }
178  }
179 
180  // }}}
181  // {{{ fetchData()
182 
190  function fetchData($username, $password)
191  {
192  $this->log('Auth_Container_IMAP::fetchData() called.', AUTH_LOG_DEBUG);
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)) {
196  $this->log('Successfully connected to IMAP server.', AUTH_LOG_DEBUG);
197  $this->activeUser = $username;
198  @imap_close($conn);
199  return true;
200  } else {
201  $this->log('Connection to IMAP server failed.', AUTH_LOG_DEBUG);
202  $this->activeUser = '';
203  return false;
204  }
205  }
206 
207  // }}}
208 
209 }
210 ?>