TYPO3 API  SVNRelease
class.tx_em_connection_soap.php
Go to the documentation of this file.
00001 <?php
00002 /* **************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) webservices.nl
00006 *  (c) 2006-2010 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.tx_em_Connection_Soap.php 1978 2010-03-09 03:18:50Z mkrause $ */
00029 
00030 /**
00031  * Enter description here...
00032  *
00033  */
00034 class tx_em_Connection_Soap {
00035     /**
00036      * valid options passed to the constructor :
00037      * wsdl        : The WSDL location, can be a local file location or
00038      *                an URL.
00039      * soapoptions  : Associative array of SOAP options to be passed to
00040      *                the SOAP implementation constructor, only used for
00041      *                the phpsoap implement.
00042      * authentication : method of authentication :
00043      *                'headers'   soap headers are used
00044      *                'prefix'  function prefixes are used
00045      * prefix        : optional prefix to be put in front of all methods.
00046      * format        : Which type of return structure :
00047      *                'object'  PHP objects
00048      *                'array'    PHP arrays, default
00049      */
00050     var $options = array();
00051 
00052     /**
00053      * SOAP client, instance of PHP SOAP class
00054      *
00055      * @var SoapClient
00056      */
00057     var $client = false;
00058 
00059     var $error = false;
00060     var $username = false;
00061     var $password = false;
00062     var $reactid = false;
00063 
00064     /**
00065      * Init Soap
00066      *
00067      * @param   array       $options
00068      * @param   string      $username
00069      * @param   string      $password
00070      * @return  [type]      ...
00071      */
00072     function init($options = false, $username = false, $password = false) {
00073         if ($username !== false) {
00074             if ($password === false) {
00075                 $this->reactid = $username;
00076             } else {
00077                 $this->username = $username;
00078                 $this->password = $password;
00079             }
00080         }
00081 
00082         $options['format'] = $options['format'] == 'object' ? 'object' : 'array';
00083 
00084         if ($options !== false) {
00085             $this->options = (array) $options;
00086         }
00087 
00088         if (defined('SOAP_1_2')) {
00089             $this->client = new SoapClient($options['wsdl'], (array) $options['soapoptions']);
00090         } else {
00091             $this->client = FALSE;
00092             throw new Exception('PHP soap extension not available');
00093         }
00094     }
00095 
00096     /**
00097      * Login
00098      *
00099      * @param   string      $username
00100      * @param   string      $password
00101      * @return  mixed       false on failure, $reactid on success
00102      */
00103     function login($username, $password) {
00104         $reactid = $this->call('login', array('username' => $username, 'password' => $password));
00105 
00106         if ($this->error) {
00107             return false;
00108         }
00109 
00110         $this->reactid = $reactid;
00111         $this->username = $username;
00112         $this->password = false;
00113 
00114         return $reactid;
00115     }
00116 
00117     /**
00118      * Logout
00119      *
00120      * @return  unknown
00121      */
00122     function logout() {
00123         $this->call('logout');
00124         $this->reactid = false;
00125         if ($this->error) {
00126             return false;
00127         }
00128         return true;
00129     }
00130 
00131 
00132     /**
00133      * Soapcall
00134      *
00135      * @param   unknown_type        $func
00136      * @param   unknown_type        $param
00137      * @param   unknown_type        $username
00138      * @param   unknown_type        $password
00139      * @return  unknown
00140      */
00141     function call($func, $param = array(), $username = false, $password = false) {
00142         if (!$this->client) {
00143             $this->error = sprintf(
00144                 'Error in %s: No soap client implementation found. ' .
00145                         'Make sure PHP soap extension is available!', __FILE__);
00146             return false;
00147         }
00148 
00149         if ($username !== false) {
00150             if ($password === false) {
00151                 $this->reactid = $username;
00152             } else {
00153                 $this->username = $username;
00154                 $this->password = $password;
00155             }
00156         }
00157 
00158         if ($this->options['authentication'] == 'prefix') {
00159             $param = array_merge(array('reactid' => $this->reactid), $param);
00160         }
00161 
00162         if ($this->options['prefix']) {
00163             $func = $this->options['prefix'] . ucfirst($func);
00164         }
00165         $this->error = false;
00166 
00167         return $this->callPhpSOAP($func, $param);
00168     }
00169 
00170     /**
00171      * Call php soap
00172      *
00173      * @param   unknown_type        $func
00174      * @param   unknown_type        $param
00175      * @return  unknown
00176      */
00177     function callPhpSOAP($func, $param) {
00178         $header = null;
00179         if ($this->options['authentication'] == 'headers') {
00180             if ($this->reactid) {
00181                 $header = new SoapHeader(
00182                     '', 'HeaderAuthenticate',
00183                     (object) array('reactid' => $this->reactid), 1
00184                 );
00185             } elseif ($this->username && $this->password) {
00186                 $header = new SoapHeader(
00187                     '', 'HeaderLogin',
00188                     (object) array(
00189                         'username' => $this->username,
00190                         'password' => $this->password
00191                     ), 1
00192                 );
00193                 $this->password = false;
00194             }
00195         }
00196          /*return array(
00197                         'username' => $this->username,
00198                         'password' => $this->password,
00199                     'func' => $func
00200                     ); */
00201 
00202         $result = $this->client->__soapCall($func, $param, NULL, $header);
00203 
00204         if (is_soap_fault($result)) {
00205             $this->error = $result;
00206             return false;
00207         }
00208 
00209         if (is_a($this->client->headersIn['HeaderAuthenticate'], 'stdClass')) {
00210             $this->reactid = $this->client->headersIn['HeaderAuthenticate']->reactid;
00211         }
00212 
00213         return $this->options['format'] == 'object' ? $result : $this->object2array($result);
00214     }
00215 
00216     /**
00217      * Convert object to array
00218      *
00219      * @param   object  $object
00220      * @return  array
00221      */
00222     function object2array($object) {
00223         if (!is_object($object) && !is_array($object)) {
00224             return $object;
00225         }
00226 
00227         $array = (array) $object;
00228         foreach ($array as $key => $value) {
00229             $array[$key] = $this->object2array($value);
00230         }
00231         return $array;
00232     }
00233 
00234     /**
00235      * Convert array to object
00236      *
00237      * @param   unknown_type        $array
00238      * @return  unknown
00239      */
00240     function array2object($array) {
00241         if (!is_array($array)) {
00242             return $array;
00243         }
00244 
00245         foreach ($array as $key => $value) {
00246             $array[$key] = $this->array2object($value);
00247         }
00248         return (object) $array;
00249     }
00250 
00251     /**
00252      * Get last request.
00253      *
00254      * @return  unknown
00255      */
00256     function lastRequest() {
00257         return $this->client->__getLastRequest();
00258     }
00259 
00260     /**
00261      * Get last response
00262      *
00263      * @return  unknown
00264      */
00265     function lastResponse() {
00266         $this->client->__getLastResponse();
00267     }
00268 
00269     /**
00270      * Get available functions
00271      *
00272      * @return  unknown
00273      */
00274     function getFunctions() {
00275         return $this->client->__getFunctions();
00276     }
00277 }
00278 
00279 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_soap.php'])) {
00280     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_soap.php']);
00281 }
00282 ?>