Documentation TYPO3 par Ameos

class.em_soap.php

00001 <?php
00002 /* **************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) webservices.nl
00006 *  (c) 2006 Karsten Dambekalns <karsten@typo3.org>
00007 *  All rights reserved
00008 *
00009 *  This script is part of the TYPO3 project. The TYPO3 project is
00010 *  free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  The GNU General Public License can be found at
00016 *  http://www.gnu.org/copyleft/gpl.html.
00017 *  A copy is found in the textfile GPL.txt and important notices to the license
00018 *  from the author is found in LICENSE.txt distributed with these scripts.
00019 *
00020 *
00021 *  This script is distributed in the hope that it will be useful,
00022 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 *  GNU General Public License for more details.
00025 *
00026 *  This copyright notice MUST APPEAR in all copies of the script!
00027 ***************************************************************/
00028 /* $Id: class.em_soap.php,v 1.1.2.3 2006/03/24 21:50:08 k-fish Exp $ */
00029 
00034 class em_soap {
00058         var $options  = array();
00059         var $client   = false;
00060         var $error    = false;
00061 
00062         var $username = false;
00063         var $password = false;
00064         var $reactid  = false;
00065 
00074         function init($options=false, $username=false, $password=false) {
00075                 if ($username !== false) {
00076                         if ($password === false) {
00077                                 $this->reactid = $username;
00078                         } else {
00079                                 $this->username = $username;
00080                                 $this->password = $password;
00081                         }
00082                 }
00083 
00084                 if (!$options['implementation'] || $options['implementation'] == 'detect') {
00085                         if (defined('SOAP_1_2')) {
00086                                 $options['implementation'] = 'phpsoap';
00087                         } elseif (class_exists('soapclient')) {
00088                                 $options['implementation'] = 'nusoap';
00089                         } elseif (class_exists('SOAP_Client')) {
00090                                 $options['implementation'] = 'pearsoap';
00091                         }
00092                 }
00093 
00094                 $options['format'] = $options['format'] == 'object' ? 'object' : 'array';
00095 
00096                 if ($options !== false) {
00097                         $this->options = (array)$options;
00098                 }
00099 
00100                 switch ($this->options['implementation']) {
00101                         case 'nusoap':
00102                                 $this->client =& new soapclient($this->options['wsdl'], true);
00103                                 $this->client->getProxy();
00104                                 break;
00105                         case 'pearsoap':
00106                                 $this->client =& new SOAP_Client($this->options['wsdl'], true);
00107                                 break;
00108                         case 'phpsoap':
00109                                 $this->client =& new SoapClient($options['wsdl'],(array)$options['soapoptions']);
00110                                 break;
00111                         default:
00112                                 $this->client = false;
00113                 }
00114         }
00115 
00123         function login($username, $password) {
00124                 $reactid = $this->call('login', array('username' => $username, 'password' => $password));
00125 
00126                 if ($this->error) {
00127                         return false;
00128                 }
00129 
00130                 $this->reactid  = $reactid;
00131                 $this->username = $username;
00132                 $this->password = false;
00133 
00134                 return $reactid;
00135         }
00136 
00142         function logout() {
00143                 $this->call('logout');
00144                 $this->reactid = false;
00145                 if ($this->error) {
00146                         return false;
00147                 }
00148                 return true;
00149         }
00150 
00151 
00161         function call($func, $param=array(), $username=false, $password=false) {
00162                 if (!$this->client) {
00163                         $this->error = "Error in Webservices.class.php: No soap client implementation found. ".
00164                         "Make sure a SOAP library such as 'NuSoap.php' is included.";
00165                         return false;
00166                 }
00167 
00168                 if ($username !== false) {
00169                         if ($password === false) {
00170                                 $this->reactid = $username;
00171                         } else {
00172                                 $this->username = $username;
00173                                 $this->password = $password;
00174                         }
00175                 }
00176 
00177                 if ($this->options['authentication'] == 'prefix') {
00178                         $param = array_merge(array('reactid' => $this->reactid), $param);
00179                 }
00180 
00181                 if ($this->options['prefix']) {
00182                         $func = $this->options['prefix'].ucfirst($func);
00183                 }
00184 
00185                 $this->error = false;
00186 
00187                 switch ($this->options['implementation']) {
00188                         case 'nusoap'   : return $this->callNuSOAP($func, $param); break;
00189                         case 'pearsoap' : return $this->callPearSOAP($func, $param); break;
00190                         case 'phpsoap'  : return $this->callPhpSOAP($func, $param); break;
00191                 }
00192 
00193                 return false;
00194         }
00195 
00203         function callPhpSOAP($func, $param) {
00204                 $header = null;
00205                 if ($this->options['authentication'] == 'headers') {
00206                         if ($this->reactid) {
00207                                 $header =& new SoapHeader(
00208                                 '','HeaderAuthenticate',
00209                                 (object)array('reactid' => $this->reactid), 1
00210                                 );
00211                         } elseif ($this->username && $this->password) {
00212                                 $header =& new SoapHeader(
00213                                 '','HeaderLogin',
00214                                 (object)array(
00215                                 'username' => $this->username,
00216                                 'password' => $this->password
00217                                 ), 1
00218                                 );
00219                                 $this->password = false;
00220                         }
00221                 }
00222 
00223                 $result = $this->client->__soapCall($func, $param, NULL, $header);
00224 
00225                 if (is_soap_fault($result)) {
00226                         $this->error = $result;
00227                         return false;
00228                 }
00229 
00230                 if (is_a($this->client->headersIn['HeaderAuthenticate'],'stdClass')) {
00231                         $this->reactid = $this->client->headersIn['HeaderAuthenticate']->reactid;
00232                 }
00233 
00234                 return $this->options['format'] == 'object' ? $result : $this->object2array($result);
00235         }
00236 
00244         function callPearSOAP($func,$param) {
00245                 if ($this->options['authentication'] == 'headers') {
00246                         if ($this->reactid) {
00247                                 $this->client->addHeader(
00248                                 new SOAP_Header(
00249                                 'HeaderAuthenticate', NULL,
00250                                 array('reactid' => $this->reactid), 1
00251                                 )
00252                                 );
00253                         } elseif ($this->username && $this->password) {
00254                                 $this->client->addHeader(
00255                                 new SOAP_Header(
00256                                 'HeaderLogin', NULL,
00257                                 array(
00258                                 'username' => $this->username,
00259                                 'password' => $this->password
00260                                 ), 1
00261                                 )
00262                                 );
00263                                 $this->password = false;
00264                         }
00265                 }
00266 
00267 
00268                 $result = $this->client->call($func, $param);
00269 
00270                 if (PEAR::isError($result)) {
00271                         $this->error = $result;
00272                         return false;
00273                 }
00274 
00275                 if (is_a($this->client->headersIn['HeaderAuthenticate'],'stdClass')) {
00276                         $this->reactid = $this->client->headersIn['HeaderAuthenticate']->reactid;
00277                 }
00278 
00279                 return $this->options['format'] == 'object' ? $result : $this->object2array($result);
00280         }
00281 
00289         function callNuSOAP($func,$param) {
00290                 $header = false;
00291                 if ($this->options['authentication'] == 'headers') {
00292                         if ($this->reactid) {
00293                                 $header = (
00294                                 "<HeaderAuthenticate SOAP-ENV:mustUnderstand='1'>".
00295                                 "<reactid>".htmlspecialchars($this->reactid)."</reactid>".
00296                                 "</HeaderAuthenticate>"
00297                                 );
00298                         } elseif ($this->username && $this->password) {
00299                                 $header = (
00300                                 "<HeaderLogin SOAP-ENV:mustUnderstand='1'>".
00301                                 "<username>".htmlspecialchars($this->username)."</username>".
00302                                 "<password>".htmlspecialchars($this->password)."</password>".
00303                                 "</HeaderLogin>" //HeaderLogin
00304                                 );
00305                                 $this->password = false;
00306                         }
00307                 }
00308 
00309                 $result = $this->client->call($func, $param, false, false, $header);
00310 
00311                 if ($this->error = $this->client->getError()) {
00312                         return false;
00313                 }
00314 
00315                 // nusoap header support is very limited
00316                 $headers = $this->client->getHeaders();
00317                 $matches = array();
00318                 if (preg_match('~<([a-z0-9]+:)?reactid[^>]*>([^<]*)</([a-z0-9]+:)?reactid>~is', $headers, $matches)) {
00319                         $this->reactid = $matches[2];
00320                 }
00321 
00322                 return $this->options['format'] == 'object' ? $this->array2object($result) : $result;
00323         }
00324 
00331         function object2array($object) {
00332                 if (!is_object($object) && !is_array($object)) {
00333                         return $object;
00334                 }
00335 
00336                 $array = (array)$object;
00337                 foreach ($array as $key => $value) {
00338                         $array[$key] = $this->object2array($value);
00339                 }
00340                 return $array;
00341         }
00342 
00349         function array2object($array) {
00350                 if (!is_array($array)) {
00351                         return $array;
00352                 }
00353 
00354                 foreach ($array as $key => $value) {
00355                         $array[$key] = $this->array2object($value);
00356                 }
00357                 return (object)$array;
00358         }
00359 
00365         function lastRequest() {
00366                 switch ($this->options['implementation']) {
00367                         case 'nusoap'   : return $this->client->request; break;
00368                         case 'pearsoap' : return $this->client->__getlastrequest(); break;
00369                         case 'phpsoap'  : return $this->client->__getLastRequest(); break;
00370                 }
00371 
00372                 return false;
00373         }
00374 
00380         function lastResponse() {
00381                 switch ($this->options['implementation']) {
00382                         case 'nusoap'   : return $this->client->response; break;
00383                         case 'pearsoap' : return $this->client->__getlastresponse(); break;
00384                         case 'phpsoap'  : return $this->client->__getLastResponse(); break;
00385                 }
00386 
00387                 return false;
00388         }
00389 
00395         function getFunctions() {
00396                 switch ($this->options['implementation']) {
00397                         case 'nusoap'   : return array_keys($this->client->operations); break;
00398                         case 'pearsoap' : return false; break;
00399                         case 'phpsoap'  : return $this->client->__getFunctions(); break;
00400                 }
00401 
00402                 return false;
00403         }
00404 }
00405 
00406 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6