Subversion Repositories Applications.papyrus

Rev

Rev 1173 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2002 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: Bruno Pedro <bpedro@co.sapo.pt>                             |
17
// +----------------------------------------------------------------------+
18
//
19
// $Id: SOAP.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
20
//
21
 
22
require_once "Auth/Container.php";
23
require_once "PEAR.php";
24
require_once 'SOAP/Client.php';
25
 
26
/**
27
 * Storage driver for fetching login data from SOAP
28
 *
29
 * This class takes one parameter (options), where
30
 * you specify the following fields: endpoint, namespace,
31
 * method, encoding, usernamefield and passwordfield.
32
 *
33
 * You can use specify features of your SOAP service
34
 * by providing its parameters in an associative manner by
35
 * using the '_features' array through the options parameter.
36
 *
37
 * The 'matchpassword' option should be set to false if your
38
 * webservice doesn't return (username,password) pairs, but
39
 * instead returns error when the login is invalid.
40
 *
41
 * Example usage:
42
 *
43
 * <?php
44
 *
45
 * ...
46
 *
47
 * $options = array (
48
 *             'endpoint' => 'http://your.soap.service/endpoint',
49
 *             'namespace' => 'urn:/Your/Namespace',
50
 *             'method' => 'get',
51
 *             'encoding' => 'UTF-8',
52
 *             'usernamefield' => 'login',
53
 *             'passwordfield' => 'password',
54
 *             'matchpasswords' => false,
55
 *             '_features' => array (
56
 *                             'example_feature' => 'example_value',
57
 *                             'another_example'  => ''
58
 *                             )
59
 *             );
60
 * $auth = new Auth('SOAP', $options, 'loginFunction');
61
 * $auth->start();
62
 *
63
 * ...
64
 *
65
 * ?>
66
 *
67
 * @author   Bruno Pedro <bpedro@co.sapo.pt>
68
 * @package  Auth
69
 * @version  $Revision: 1.1 $
70
 */
71
class Auth_Container_SOAP extends Auth_Container
72
{
73
 
74
    /**
75
     * Required options for the class
76
     * @var array
77
     * @access private
78
     */
79
    var $_requiredOptions = array('endpoint', 'namespace', 'method', 'encoding', 'usernamefield', 'passwordfield');
80
 
81
    /**
82
     * Options for the class
83
     * @var array
84
     * @access private
85
     */
86
    var $_options = array();
87
 
88
    /**
89
     * Optional SOAP features
90
     * @var array
91
     * @access private
92
     */
93
    var $_features = array();
94
 
95
    /**
96
     * The SOAP response
97
     * @var array
98
     * @access public
99
     */
100
     var $soapResponse = array();
101
 
102
    /**
103
     * Constructor of the container class
104
     *
105
     * @param  $options, associative array with endpoint, namespace, method,
106
     *                   usernamefield, passwordfield and optional features
107
     */
108
    function Auth_Container_SOAP($options)
109
    {
110
        $this->_options = $options;
111
        if (!isset($this->_options['matchpasswords'])) {
112
            $this->_options['matchpasswords'] = true;
113
        }
114
        if (!empty($this->_options['_features'])) {
115
            $this->_features = $this->_options['_features'];
116
            unset($this->_options['_features']);
117
        }
118
    }
119
 
120
    /**
121
     * Fetch data from SOAP service
122
     *
123
     * Requests the SOAP service for the given username/password
124
     * combination.
125
     *
126
     * @param  string Username
127
     * @param  string Password
128
     * @return mixed Returns the SOAP response or false if something went wrong
129
     */
130
    function fetchData($username, $password)
131
    {
132
        // check if all required options are set
133
        if (array_intersect($this->_requiredOptions, array_keys($this->_options)) != $this->_requiredOptions) {
134
            return false;
135
        } else {
136
            // create a SOAP client and set encoding
137
            $soapClient = new SOAP_Client($this->_options['endpoint']);
138
            $soapClient->setEncoding($this->_options['encoding']);
139
        }
140
        // assign username and password fields
141
        $usernameField = new SOAP_Value($this->_options['usernamefield'],'string', $username);
142
        $passwordField = new SOAP_Value($this->_options['passwordfield'],'string', $password);
143
        $SOAPParams = array($usernameField, $passwordField);
144
        // assign optional features
145
        foreach ($this->_features as $fieldName => $fieldValue) {
146
            $SOAPParams[] = new SOAP_Value($fieldName, 'string', $fieldValue);
147
        }
148
        // make SOAP call
149
        $this->soapResponse = $soapClient->call(
150
                                  $this->_options['method'],
151
                                  $SOAPParams,
152
                                  array('namespace' => $this->_options['namespace'])
153
                                               );
154
        if (!PEAR::isError($this->soapResponse)) {
155
            if ($this->_options['matchpasswords']) {
156
                // check if passwords match
157
                if ($password == $this->soapResponse->{$this->_options['passwordfield']}) {
158
                    return true;
159
                } else {
160
                    return false;
161
                }
162
            } else {
163
                return true;
164
            }
165
        } else {
166
            return false;
167
        }
168
    }
169
}
170
?>