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

Source for file IMAP.php

Documentation is available at IMAP.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  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.12 2006/03/01 06:08:30 aashley 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.  * Storage driver for fetching login data from a IMAP server
  66.  *
  67.  * This storage driver authenticates uses against IMAP server.
  68.  *
  69.  * @author   Jeroen Houben <jeroen@terena.nl>
  70.  * @author   Cipriano Groenendal <cipri@campai.nl>
  71.  * @author   Adam Ashley <aashley@php.net>
  72.  * @package  Auth
  73.  * @version  $Revision: 1.12 $
  74.  */
  75. {
  76.  
  77.     // {{{ properties
  78.  
  79.     /**
  80.      * Options for the class
  81.      * @var array 
  82.      */
  83.     var $options = array();
  84.  
  85.     // }}}
  86.  
  87.     // {{{ Auth_Container_IMAP() [constructor]
  88.  
  89.     /**
  90.      * Constructor of the container class
  91.      *
  92.      * @param  $params  associative array with host, port, baseDSN, checkServer
  93.      *                   and userattr key
  94.      * @return object Returns an error object if something went wrong
  95.      * @todo Use PEAR Net_IMAP if IMAP extension not loaded
  96.      */
  97.     function Auth_Container_IMAP($params)
  98.     {
  99.         if (!extension_loaded('imap')) {
  100.             return PEAR::raiseError('Cannot use IMAP authentication, '
  101.                     .'IMAP extension not loaded!'41PEAR_ERROR_DIE);
  102.         }
  103.         $this->_setDefaults();
  104.  
  105.         // set parameters (if any)
  106.         if (is_array($params)) {
  107.             $this->_parseOptions($params);
  108.         }
  109.  
  110.         if ($this->options['checkServer']{
  111.             $this->_checkServer($this->options['timeout']);
  112.         }
  113.         return true;
  114.     }
  115.  
  116.     // }}}
  117.     // {{{ _setDefaults()
  118.  
  119.     /**
  120.      * Set some default options
  121.      *
  122.      * @access private
  123.      */
  124.     function _setDefaults()
  125.     {
  126.         $this->options['host''localhost';
  127.         $this->options['port'= 143;
  128.         $this->options['baseDSN''';
  129.         $this->options['checkServer'= true;
  130.         $this->options['timeout'= 20;
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ _checkServer()
  135.  
  136.     /**
  137.      * Check if the given server and port are reachable
  138.      *
  139.      * @access private
  140.      */
  141.     function _checkServer({
  142.         $fp @fsockopen ($this->options['host']$this->options['port'],
  143.                           $errno$errstr$this->options['timeout']);
  144.         if (is_resource($fp)) {
  145.             @fclose($fp);
  146.         else {
  147.             $message "Error connecting to IMAP server "
  148.                 . $this->options['host']
  149.                 . ":" $this->options['port'];
  150.             return PEAR::raiseError($message41);
  151.         }
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ _parseOptions()
  156.  
  157.     /**
  158.      * Parse options passed to the container class
  159.      *
  160.      * @access private
  161.      * @param  array 
  162.      */
  163.     function _parseOptions($array)
  164.     {
  165.         foreach ($array as $key => $value{
  166.             $this->options[$key$value;
  167.         }
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ fetchData()
  172.  
  173.     /**
  174.      * Try to open a IMAP stream using $username / $password
  175.      *
  176.      * @param  string Username
  177.      * @param  string Password
  178.      * @return boolean 
  179.      */
  180.     function fetchData($username$password)
  181.     {
  182.         $dsn '{'.$this->options['host'].':'.$this->options['port'].$this->options['baseDSN'].'}';
  183.         $conn @imap_open ($dsn$username$passwordOP_HALFOPEN);
  184.         if (is_resource($conn)) {
  185.             $this->activeUser = $username;
  186.             @imap_close($conn);
  187.             return true;
  188.         else {
  189.             $this->activeUser = '';
  190.             return false;
  191.         }
  192.     }
  193.  
  194.     // }}}
  195.  
  196. }
  197. ?>

Documentation generated on Mon, 11 Mar 2019 14:37:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.