PageRenderTime 71ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pear/Auth/Container/SOAP5.php

https://github.com/yrchen/OPMS
PHP | 267 lines | 81 code | 24 blank | 162 comment | 14 complexity | 8b6e2c820f622b73a3ff60a941610f9f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3. /**
  4. * Storage driver for use against a SOAP service using PHP5 SoapClient
  5. *
  6. * PHP version 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 Based upon Auth_Container_SOAP by Bruno Pedro <bpedro@co.sapo.pt>
  17. * @author Marcel Oelke <puRe@rednoize.com>
  18. * @author Adam Ashley <aashley@php.net>
  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: SOAP5.php,v 1.4 2006/09/07 02:32:35 aashley Exp $
  22. * @since File available since Release 1.4.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. * Storage driver for fetching login data from SOAP using the PHP5 Builtin SOAP
  34. * functions. This is a modification of the SOAP Storage driver from Bruno Pedro
  35. * thats using the PEAR SOAP Package.
  36. *
  37. * This class takes one parameter (options), where
  38. * you specify the following fields:
  39. * * location and uri, or wsdl file
  40. * * method to call on the SOAP service
  41. * * usernamefield, the name of the parameter where the username is supplied
  42. * * passwordfield, the name of the parameter where the password is supplied
  43. * * matchpassword, whether to look for the password in the response from
  44. * the function call or assume that no errors means user
  45. * authenticated.
  46. *
  47. * See http://www.php.net/manual/en/ref.soap.php for further details
  48. * on options for the PHP5 SoapClient which are passed through.
  49. *
  50. * Example usage without WSDL:
  51. *
  52. * <?php
  53. *
  54. * $options = array (
  55. * 'wsdl' => NULL,
  56. * 'location' => 'http://your.soap.service/endpoint',
  57. * 'uri' => 'urn:/Your/Namespace',
  58. * 'method' => 'checkAuth',
  59. * 'usernamefield' => 'username',
  60. * 'passwordfield' => 'password',
  61. * 'matchpasswords' => false,
  62. * '_features' => array (
  63. * 'extra_parameter' => 'example_value',
  64. * 'another_parameter' => 'foobar'
  65. * )
  66. * );
  67. *
  68. * $auth = new Auth('SOAP5', $options);
  69. * $auth->start();
  70. *
  71. * ?>
  72. *
  73. * Example usage with WSDL:
  74. *
  75. * <?php
  76. *
  77. * $options = array (
  78. * 'wsdl' => 'http://your.soap.service/wsdl',
  79. * 'method' => 'checkAuth',
  80. * 'usernamefield' => 'username',
  81. * 'passwordfield' => 'password',
  82. * 'matchpasswords' => false,
  83. * '_features' => array (
  84. * 'extra_parameter' => 'example_value',
  85. * 'another_parameter' => 'foobar'
  86. * )
  87. * );
  88. *
  89. * $auth = new Auth('SOAP5', $options);
  90. * $auth->start();
  91. *
  92. * ?>
  93. *
  94. * @category Authentication
  95. * @package Auth
  96. * @author Based upon Auth_Container_SOAP by Bruno Pedro <bpedro@co.sapo.pt>
  97. * @author Marcel Oelke <puRe@rednoize.com>
  98. * @author Adam Ashley <aashley@php.net>
  99. * @copyright 2001-2006 The PHP Group
  100. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  101. * @version Release: 1.4.0 File: $Revision: 1.4 $
  102. * @since Class available since Release 1.4.0
  103. */
  104. class Auth_Container_SOAP5 extends Auth_Container
  105. {
  106. // {{{ properties
  107. /**
  108. * Required options for the class
  109. * @var array
  110. * @access private
  111. */
  112. var $_requiredOptions = array(
  113. 'location',
  114. 'uri',
  115. 'method',
  116. 'usernamefield',
  117. 'passwordfield',
  118. 'wsdl',
  119. );
  120. /**
  121. * Options for the class
  122. * @var array
  123. * @access private
  124. */
  125. var $_options = array();
  126. /**
  127. * Optional SOAP features
  128. * @var array
  129. * @access private
  130. */
  131. var $_features = array();
  132. /**
  133. * The SOAP response
  134. * @var array
  135. * @access public
  136. */
  137. var $soapResponse = array();
  138. // }}}
  139. // {{{ Auth_Container_SOAP5()
  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_SOAP5($options)
  147. {
  148. $this->_setDefaults();
  149. foreach ($options as $name => $value) {
  150. $this->_options[$name] = $value;
  151. }
  152. if (!empty($this->_options['_features'])) {
  153. $this->_features = $this->_options['_features'];
  154. unset($this->_options['_features']);
  155. }
  156. }
  157. // }}}
  158. // {{{ fetchData()
  159. /**
  160. * Fetch data from SOAP service
  161. *
  162. * Requests the SOAP service for the given username/password
  163. * combination.
  164. *
  165. * @param string Username
  166. * @param string Password
  167. * @return mixed Returns the SOAP response or false if something went wrong
  168. */
  169. function fetchData($username, $password)
  170. {
  171. $result = $this->_validateOptions();
  172. if (PEAR::isError($result))
  173. return $result;
  174. // create a SOAP client
  175. $soapClient = new SoapClient($this->_options["wsdl"], $this->_options);
  176. $params = array();
  177. // first, assign the optional features
  178. foreach ($this->_features as $fieldName => $fieldValue) {
  179. $params[$fieldName] = $fieldValue;
  180. }
  181. // assign username and password ...
  182. $params[$this->_options['usernamefield']] = $username;
  183. $params[$this->_options['passwordfield']] = $password;
  184. try {
  185. $this->soapResponse = $soapClient->__soapCall($this->_options['method'], $params);
  186. if ($this->_options['matchpasswords']) {
  187. // check if passwords match
  188. if ($password == $this->soapResponse[$this->_options['passwordfield']]) {
  189. return true;
  190. } else {
  191. return false;
  192. }
  193. } else {
  194. return true;
  195. }
  196. } catch (SoapFault $e) {
  197. return PEAR::raiseError("Error retrieving authentication data. Received SOAP Fault: ".$e->faultstring, $e->faultcode);
  198. }
  199. }
  200. // }}}
  201. // {{{ _validateOptions()
  202. /**
  203. * Validate that the options passed to the container class are enough for us to proceed
  204. *
  205. * @access private
  206. * @param array
  207. */
  208. function _validateOptions($array)
  209. {
  210. if ( ( is_null($this->options['wsdl'])
  211. && is_null($this->options['location'])
  212. && is_null($this->options['uri']))
  213. || ( is_null($this->options['wsdl'])
  214. && ( is_null($this->options['location'])
  215. || is_null($this->options['uri'])))) {
  216. return PEAR::raiseError('Either a WSDL file or a location/uri pair must be specified.');
  217. }
  218. if (is_null($this->options['method'])) {
  219. return PEAR::raiseError('A method to call on the soap service must be specified.');
  220. }
  221. return true;
  222. }
  223. // }}}
  224. // {{{ _setDefaults()
  225. /**
  226. * Set some default options
  227. *
  228. * @access private
  229. * @return void
  230. */
  231. function _setDefaults()
  232. {
  233. $this->options['wsdl'] = null;
  234. $this->options['location'] = null;
  235. $this->options['uri'] = null;
  236. $this->options['method'] = null;
  237. $this->options['usernamefield'] = 'username';
  238. $this->options['passwordfield'] = 'password';
  239. $this->options['matchpasswords'] = true;
  240. }
  241. // }}}
  242. }
  243. ?>