1<?php
2// +-----------------------------------------------------------------------+
3// | Copyright (c) 2002-2003 Richard Heyes                                 |
4// | All rights reserved.                                                  |
5// |                                                                       |
6// | Redistribution and use in source and binary forms, with or without    |
7// | modification, are permitted provided that the following conditions    |
8// | are met:                                                              |
9// |                                                                       |
10// | o Redistributions of source code must retain the above copyright      |
11// |   notice, this list of conditions and the following disclaimer.       |
12// | o Redistributions in binary form must reproduce the above copyright   |
13// |   notice, this list of conditions and the following disclaimer in the |
14// |   documentation and/or other materials provided with the distribution.|
15// | o The names of the authors may not be used to endorse or promote      |
16// |   products derived from this software without specific prior written  |
17// |   permission.                                                         |
18// |                                                                       |
19// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
20// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
21// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
23// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
25// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
28// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
30// |                                                                       |
31// +-----------------------------------------------------------------------+
32// | Author: Richard Heyes <richard@php.net>                               |
33// +-----------------------------------------------------------------------+
34//
35// $Id$
36
37/**
38* Client implementation of various SASL mechanisms
39*
40* @author  Richard Heyes <richard@php.net>
41* @access  public
42* @version 1.0
43* @package Auth_SASL2
44*/
45class Auth_SASL2
46{
47    /**
48    * Factory class. Returns an object of the request
49    * type.
50    *
51    * @param string $type One of: Anonymous
52    *                             Plain
53    *                             CramMD5
54    *                             DigestMD5
55    *                             SCRAM-* (any mechanism of the SCRAM family)
56    *                     Types are not case sensitive
57    */
58    function factory($type)
59    {
60        switch (strtolower($type)) {
61            case 'anonymous':
62                $filename  = 'Auth/SASL2/Anonymous.php';
63                $classname = 'Auth_SASL2_Anonymous';
64                break;
65
66            case 'login':
67                $filename  = 'Auth/SASL2/Login.php';
68                $classname = 'Auth_SASL2_Login';
69                break;
70
71            case 'plain':
72                $filename  = 'Auth/SASL2/Plain.php';
73                $classname = 'Auth_SASL2_Plain';
74                break;
75
76            case 'external':
77                $filename  = 'Auth/SASL2/External.php';
78                $classname = 'Auth_SASL2_External';
79                break;
80
81            case 'crammd5':
82                // $msg = 'Deprecated mechanism name. Use IANA-registered name: CRAM-MD5.';
83                // trigger_error($msg, E_USER_DEPRECATED);
84            case 'cram-md5':
85                $filename  = 'Auth/SASL2/CramMD5.php';
86                $classname = 'Auth_SASL2_CramMD5';
87                break;
88
89            case 'digestmd5':
90                // $msg = 'Deprecated mechanism name. Use IANA-registered name: DIGEST-MD5.';
91                // trigger_error($msg, E_USER_DEPRECATED);
92            case 'digest-md5':
93                // $msg = 'DIGEST-MD5 is a deprecated SASL mechanism as per RFC-6331. Using it could be a security risk.';
94                // trigger_error($msg, E_USER_NOTICE);
95                $filename  = 'Auth/SASL2/DigestMD5.php';
96                $classname = 'Auth_SASL2_DigestMD5';
97                break;
98
99            default:
100                $scram = '/^SCRAM-(.{1,9})$/i';
101                if (preg_match($scram, $type, $matches))
102                {
103                    $hash = $matches[1];
104                    $filename = dirname(__FILE__) .'/SASL2/SCRAM.php';
105                    $classname = 'Auth_SASL2_SCRAM';
106                    $parameter = $hash;
107                    break;
108                }
109                throw new InvalidArgumentException('Invalid SASL mechanism type');
110                break;
111        }
112
113        require_once $filename;
114        if (isset($parameter)) {
115            $obj = new $classname($parameter);
116        } else {
117            $obj = new $classname();
118        }
119
120        return $obj;
121    }
122}
123
124
125