Auth
[ class tree: Auth ] [ index: Auth ] [ all elements ]

Source for file IMAP.php

Documentation is available at IMAP.php

  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.9 2004/03/28 23:41:09 yavo 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.9 $
  69.  */
  70. {
  71.     /**
  72.      * Options for the class
  73.      * @var array 
  74.      */
  75.     var $options = array();
  76.  
  77.     /**
  78.      * Constructor of the container class
  79.      *
  80.      * @param  $params, associative hash with host,port,basedn and userattr key
  81.      * @param  $params, associative array with host, port, baseDSN, checkServer key.
  82.      * @return object Returns an error object if something went wrong
  83.      */
  84.     function Auth_Container_IMAP($params)
  85.     {
  86.         if (!extension_loaded('imap')) {
  87.             return PEAR::raiseError("Cannot use IMAP authentication, IMAP extension not loaded!",
  88.                                     41PEAR_ERROR_DIE);
  89.         }
  90.         $this->_setDefaults();
  91.         
  92.         // set parameters (if any)
  93.         if (is_array($params)) {
  94.             $this->_parseOptions($params);
  95.         }
  96.         if ($this->options['checkServer']{
  97.             $this->_checkServer($this->options['timeout']);
  98.         }
  99.         return true;
  100.     }
  101.  
  102.     /**
  103.      * Set some default options
  104.      *
  105.      * @access private
  106.      */
  107.     function _setDefaults()
  108.     {
  109.         $this->options['host''localhost';
  110.         $this->options['port'= 143;
  111.         $this->options['baseDSN''';
  112.         $this->options['checkServer'= true;
  113.         $this->options['timeout'= 20;
  114.     }
  115.  
  116.  
  117.     /**
  118.      * Check if the given server and port are reachable
  119.      *
  120.      * @access private
  121.      */
  122.     function _checkServer({
  123.         $fp @fsockopen ($this->options['host']$this->options['port'],
  124.                           $errno$errstr$this->options['timeout']);
  125.         if (is_resource($fp)) {
  126.             @fclose($fp);
  127.         else {
  128.             $message "Error connecting to IMAP server "
  129.                 . $this->options['host']
  130.                 . ":" $this->options['port'];
  131.             return PEAR::raiseError($message41PEAR_ERROR_DIE);
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Parse options passed to the container class
  137.      *
  138.      * @access private
  139.      * @param  array 
  140.      */
  141.     function _parseOptions($array)
  142.     {
  143.         foreach ($array as $key => $value{
  144.             $this->options[$key$value;
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * Try to open a IMAP stream using $username / $password
  150.      *
  151.      * @param  string Username
  152.      * @param  string Password
  153.      * @return boolean 
  154.      */
  155.     function fetchData($username$password)
  156.     {
  157.         $dsn '{'.$this->options['host'].':'.$this->options['port'].$this->options['baseDSN'].'}';
  158.         $conn @imap_open ($dsn$username$passwordOP_HALFOPEN);
  159.         if (is_resource($conn)) {
  160.             $this->activeUser = $username;
  161.             @imap_close($conn);
  162.             return true;
  163.         else {
  164.             $this->activeUser = '';
  165.             return false;
  166.         }
  167.     }
  168. }
  169. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.