| 1 | <?php |
|---|
| 2 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ |
|---|
| 3 | |
|---|
| 4 | /** |
|---|
| 5 | * Storage driver for use against a SOAP service |
|---|
| 6 | * |
|---|
| 7 | * PHP versions 4 and 5 |
|---|
| 8 | * |
|---|
| 9 | * LICENSE: This source file is subject to version 3.01 of the PHP license |
|---|
| 10 | * that is available through the world-wide-web at the following URI: |
|---|
| 11 | * http://www.php.net/license/3_01.txt. If you did not receive a copy of |
|---|
| 12 | * the PHP License and are unable to obtain it through the web, please |
|---|
| 13 | * send a note to [email protected] so we can mail you a copy immediately. |
|---|
| 14 | * |
|---|
| 15 | * @category Authentication |
|---|
| 16 | * @package Auth |
|---|
| 17 | * @author Bruno Pedro <[email protected]> |
|---|
| 18 | * @author Adam Ashley <[email protected]> |
|---|
| 19 | * @copyright 2001-2006 The PHP Group |
|---|
| 20 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 21 | * @version CVS: $Id: SOAP.php 8713 2006-12-01 05:08:34Z kakinaka $ |
|---|
| 22 | * @link http://pear.php.net/package/Auth |
|---|
| 23 | * @since File available since Release 1.2.0 |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Include Auth_Container base class |
|---|
| 28 | */ |
|---|
| 29 | require_once "Auth/Container.php"; |
|---|
| 30 | /** |
|---|
| 31 | * Include PEAR package for error handling |
|---|
| 32 | */ |
|---|
| 33 | require_once "PEAR.php"; |
|---|
| 34 | /** |
|---|
| 35 | * Include PEAR SOAP_Client |
|---|
| 36 | */ |
|---|
| 37 | require_once 'SOAP/Client.php'; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Storage driver for fetching login data from SOAP |
|---|
| 41 | * |
|---|
| 42 | * This class takes one parameter (options), where |
|---|
| 43 | * you specify the following fields: endpoint, namespace, |
|---|
| 44 | * method, encoding, usernamefield and passwordfield. |
|---|
| 45 | * |
|---|
| 46 | * You can use specify features of your SOAP service |
|---|
| 47 | * by providing its parameters in an associative manner by |
|---|
| 48 | * using the '_features' array through the options parameter. |
|---|
| 49 | * |
|---|
| 50 | * The 'matchpassword' option should be set to false if your |
|---|
| 51 | * webservice doesn't return (username,password) pairs, but |
|---|
| 52 | * instead returns error when the login is invalid. |
|---|
| 53 | * |
|---|
| 54 | * Example usage: |
|---|
| 55 | * |
|---|
| 56 | * <?php |
|---|
| 57 | * |
|---|
| 58 | * ... |
|---|
| 59 | * |
|---|
| 60 | * $options = array ( |
|---|
| 61 | * 'endpoint' => 'http://your.soap.service/endpoint', |
|---|
| 62 | * 'namespace' => 'urn:/Your/Namespace', |
|---|
| 63 | * 'method' => 'get', |
|---|
| 64 | * 'encoding' => 'UTF-8', |
|---|
| 65 | * 'usernamefield' => 'login', |
|---|
| 66 | * 'passwordfield' => 'password', |
|---|
| 67 | * 'matchpasswords' => false, |
|---|
| 68 | * '_features' => array ( |
|---|
| 69 | * 'example_feature' => 'example_value', |
|---|
| 70 | * 'another_example' => '' |
|---|
| 71 | * ) |
|---|
| 72 | * ); |
|---|
| 73 | * $auth = new Auth('SOAP', $options, 'loginFunction'); |
|---|
| 74 | * $auth->start(); |
|---|
| 75 | * |
|---|
| 76 | * ... |
|---|
| 77 | * |
|---|
| 78 | * ?> |
|---|
| 79 | * |
|---|
| 80 | * @category Authentication |
|---|
| 81 | * @package Auth |
|---|
| 82 | * @author Bruno Pedro <[email protected]> |
|---|
| 83 | * @author Adam Ashley <[email protected]> |
|---|
| 84 | * @copyright 2001-2006 The PHP Group |
|---|
| 85 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 86 | * @version Release: 1.4.2 File: $Revision: 8713 $ |
|---|
| 87 | * @link http://pear.php.net/package/Auth |
|---|
| 88 | * @since Class available since Release 1.2.0 |
|---|
| 89 | */ |
|---|
| 90 | class Auth_Container_SOAP extends Auth_Container |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | // {{{ properties |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Required options for the class |
|---|
| 97 | * @var array |
|---|
| 98 | * @access private |
|---|
| 99 | */ |
|---|
| 100 | var $_requiredOptions = array( |
|---|
| 101 | 'endpoint', |
|---|
| 102 | 'namespace', |
|---|
| 103 | 'method', |
|---|
| 104 | 'encoding', |
|---|
| 105 | 'usernamefield', |
|---|
| 106 | 'passwordfield', |
|---|
| 107 | ); |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Options for the class |
|---|
| 111 | * @var array |
|---|
| 112 | * @access private |
|---|
| 113 | */ |
|---|
| 114 | var $_options = array(); |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Optional SOAP features |
|---|
| 118 | * @var array |
|---|
| 119 | * @access private |
|---|
| 120 | */ |
|---|
| 121 | var $_features = array(); |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * The SOAP response |
|---|
| 125 | * @var array |
|---|
| 126 | * @access public |
|---|
| 127 | */ |
|---|
| 128 | var $soapResponse = array(); |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * The SOAP client |
|---|
| 132 | * @var mixed |
|---|
| 133 | * @access public |
|---|
| 134 | */ |
|---|
| 135 | var $soapClient = null; |
|---|
| 136 | |
|---|
| 137 | // }}} |
|---|
| 138 | // {{{ Auth_Container_SOAP() [constructor] |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Constructor of the container class |
|---|
| 142 | * |
|---|
| 143 | * @param $options, associative array with endpoint, namespace, method, |
|---|
| 144 | * usernamefield, passwordfield and optional features |
|---|
| 145 | */ |
|---|
| 146 | function Auth_Container_SOAP($options) |
|---|
| 147 | { |
|---|
| 148 | $this->_options = $options; |
|---|
| 149 | if (!isset($this->_options['matchpasswords'])) { |
|---|
| 150 | $this->_options['matchpasswords'] = true; |
|---|
| 151 | } |
|---|
| 152 | if (!empty($this->_options['_features'])) { |
|---|
| 153 | $this->_features = $this->_options['_features']; |
|---|
| 154 | unset($this->_options['_features']); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | // }}} |
|---|
| 159 | // {{{ fetchData() |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Fetch data from SOAP service |
|---|
| 163 | * |
|---|
| 164 | * Requests the SOAP service for the given username/password |
|---|
| 165 | * combination. |
|---|
| 166 | * |
|---|
| 167 | * @param string Username |
|---|
| 168 | * @param string Password |
|---|
| 169 | * @return mixed Returns the SOAP response or false if something went wrong |
|---|
| 170 | */ |
|---|
| 171 | function fetchData($username, $password) |
|---|
| 172 | { |
|---|
| 173 | // check if all required options are set |
|---|
| 174 | if (array_intersect($this->_requiredOptions, array_keys($this->_options)) != $this->_requiredOptions) { |
|---|
| 175 | return false; |
|---|
| 176 | } else { |
|---|
| 177 | // create a SOAP client and set encoding |
|---|
| 178 | $this->soapClient = new SOAP_Client($this->_options['endpoint']); |
|---|
| 179 | $this->soapClient->setEncoding($this->_options['encoding']); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | // set the trace option if requested |
|---|
| 183 | if (isset($this->_options['trace'])) { |
|---|
| 184 | $this->soapClient->__options['trace'] = true; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | // set the timeout option if requested |
|---|
| 188 | if (isset($this->_options['timeout'])) { |
|---|
| 189 | $this->soapClient->__options['timeout'] = $this->_options['timeout']; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | // assign username and password fields |
|---|
| 193 | $usernameField = new SOAP_Value($this->_options['usernamefield'],'string', $username); |
|---|
| 194 | $passwordField = new SOAP_Value($this->_options['passwordfield'],'string', $password); |
|---|
| 195 | $SOAPParams = array($usernameField, $passwordField); |
|---|
| 196 | |
|---|
| 197 | // assign optional features |
|---|
| 198 | foreach ($this->_features as $fieldName => $fieldValue) { |
|---|
| 199 | $SOAPParams[] = new SOAP_Value($fieldName, 'string', $fieldValue); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | // make SOAP call |
|---|
| 203 | $this->soapResponse = $this->soapClient->call( |
|---|
| 204 | $this->_options['method'], |
|---|
| 205 | $SOAPParams, |
|---|
| 206 | array('namespace' => $this->_options['namespace']) |
|---|
| 207 | ); |
|---|
| 208 | |
|---|
| 209 | if (!PEAR::isError($this->soapResponse)) { |
|---|
| 210 | if ($this->_options['matchpasswords']) { |
|---|
| 211 | // check if passwords match |
|---|
| 212 | if ($password == $this->soapResponse->{$this->_options['passwordfield']}) { |
|---|
| 213 | return true; |
|---|
| 214 | } else { |
|---|
| 215 | return false; |
|---|
| 216 | } |
|---|
| 217 | } else { |
|---|
| 218 | return true; |
|---|
| 219 | } |
|---|
| 220 | } else { |
|---|
| 221 | return false; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | // }}} |
|---|
| 226 | |
|---|
| 227 | } |
|---|
| 228 | ?> |
|---|