/lib/include/Auth/Container/SOAP.php

https://github.com/usagi-project/mynets1 · PHP · 228 lines · 70 code · 21 blank · 137 comment · 14 complexity · 297b4fb1f1f0c51854a3e9eb8ec60608 MD5 · raw file

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