Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / connection / class.tx_em_connection_soap.php
1 <?php
2 /* **************************************************************
3 * Copyright notice
4 *
5 * (c) webservices.nl
6 * (c) 2006-2011 Karsten Dambekalns <karsten@typo3.org>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /* $Id: class.tx_em_Connection_Soap.php 1978 2010-03-09 03:18:50Z mkrause $ */
29
30 /**
31 * Enter description here...
32 *
33 */
34 class tx_em_Connection_Soap {
35 /**
36 * valid options passed to the constructor :
37 * wsdl : The WSDL location, can be a local file location or
38 * an URL.
39 * soapoptions : Associative array of SOAP options to be passed to
40 * the SOAP implementation constructor, only used for
41 * the phpsoap implement.
42 * authentication : method of authentication :
43 * 'headers' soap headers are used
44 * 'prefix' function prefixes are used
45 * prefix : optional prefix to be put in front of all methods.
46 * format : Which type of return structure :
47 * 'object' PHP objects
48 * 'array' PHP arrays, default
49 */
50 var $options = array();
51
52 /**
53 * SOAP client, instance of PHP SOAP class
54 *
55 * @var SoapClient
56 */
57 var $client = false;
58
59 var $error = false;
60 var $username = false;
61 var $password = false;
62 var $reactid = false;
63
64 /**
65 * Init Soap
66 *
67 * @param array $options
68 * @param string $username
69 * @param string $password
70 * @return [type] ...
71 */
72 function init($options = false, $username = false, $password = false) {
73 if ($username !== false) {
74 if ($password === false) {
75 $this->reactid = $username;
76 } else {
77 $this->username = $username;
78 $this->password = $password;
79 }
80 }
81
82 $options['format'] = $options['format'] == 'object' ? 'object' : 'array';
83
84 if ($options !== false) {
85 $this->options = (array) $options;
86 }
87
88 if (defined('SOAP_1_2')) {
89 $this->client = new SoapClient($options['wsdl'], (array) $options['soapoptions']);
90 } else {
91 $this->client = FALSE;
92 throw new Exception('PHP soap extension not available');
93 }
94 }
95
96 /**
97 * Login
98 *
99 * @param string $username
100 * @param string $password
101 * @return mixed false on failure, $reactid on success
102 */
103 function login($username, $password) {
104 $reactid = $this->call('login', array('username' => $username, 'password' => $password));
105
106 if ($this->error) {
107 return false;
108 }
109
110 $this->reactid = $reactid;
111 $this->username = $username;
112 $this->password = false;
113
114 return $reactid;
115 }
116
117 /**
118 * Logout
119 *
120 * @return unknown
121 */
122 function logout() {
123 $this->call('logout');
124 $this->reactid = false;
125 if ($this->error) {
126 return false;
127 }
128 return true;
129 }
130
131
132 /**
133 * Soapcall
134 *
135 * @param unknown_type $func
136 * @param unknown_type $param
137 * @param unknown_type $username
138 * @param unknown_type $password
139 * @return unknown
140 */
141 function call($func, $param = array(), $username = false, $password = false) {
142 if (!$this->client) {
143 $this->error = sprintf(
144 'Error in %s: No soap client implementation found. ' .
145 'Make sure PHP soap extension is available!', __FILE__);
146 return false;
147 }
148
149 if ($username !== false) {
150 if ($password === false) {
151 $this->reactid = $username;
152 } else {
153 $this->username = $username;
154 $this->password = $password;
155 }
156 }
157
158 if ($this->options['authentication'] == 'prefix') {
159 $param = array_merge(array('reactid' => $this->reactid), $param);
160 }
161
162 if ($this->options['prefix']) {
163 $func = $this->options['prefix'] . ucfirst($func);
164 }
165 $this->error = false;
166
167 return $this->callPhpSOAP($func, $param);
168 }
169
170 /**
171 * Call php soap
172 *
173 * @param unknown_type $func
174 * @param unknown_type $param
175 * @return unknown
176 */
177 function callPhpSOAP($func, $param) {
178 $header = null;
179 if ($this->options['authentication'] == 'headers') {
180 if ($this->reactid) {
181 $header = new SoapHeader(
182 '', 'HeaderAuthenticate',
183 (object) array('reactid' => $this->reactid), 1
184 );
185 } elseif ($this->username && $this->password) {
186 $header = new SoapHeader(
187 '', 'HeaderLogin',
188 (object) array(
189 'username' => $this->username,
190 'password' => $this->password
191 ), 1
192 );
193 $this->password = false;
194 }
195 }
196 /*return array(
197 'username' => $this->username,
198 'password' => $this->password,
199 'func' => $func
200 ); */
201
202 $result = $this->client->__soapCall($func, $param, NULL, $header);
203
204 if (is_soap_fault($result)) {
205 $this->error = $result;
206 return false;
207 }
208
209 if (is_a($this->client->headersIn['HeaderAuthenticate'], 'stdClass')) {
210 $this->reactid = $this->client->headersIn['HeaderAuthenticate']->reactid;
211 }
212
213 return $this->options['format'] == 'object' ? $result : $this->object2array($result);
214 }
215
216 /**
217 * Convert object to array
218 *
219 * @param object $object
220 * @return array
221 */
222 function object2array($object) {
223 if (!is_object($object) && !is_array($object)) {
224 return $object;
225 }
226
227 $array = (array) $object;
228 foreach ($array as $key => $value) {
229 $array[$key] = $this->object2array($value);
230 }
231 return $array;
232 }
233
234 /**
235 * Convert array to object
236 *
237 * @param unknown_type $array
238 * @return unknown
239 */
240 function array2object($array) {
241 if (!is_array($array)) {
242 return $array;
243 }
244
245 foreach ($array as $key => $value) {
246 $array[$key] = $this->array2object($value);
247 }
248 return (object) $array;
249 }
250
251 /**
252 * Get last request.
253 *
254 * @return unknown
255 */
256 function lastRequest() {
257 return $this->client->__getLastRequest();
258 }
259
260 /**
261 * Get last response
262 *
263 * @return unknown
264 */
265 function lastResponse() {
266 $this->client->__getLastResponse();
267 }
268
269 /**
270 * Get available functions
271 *
272 * @return unknown
273 */
274 function getFunctions() {
275 return $this->client->__getFunctions();
276 }
277 }
278
279 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_soap.php'])) {
280 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_soap.php']);
281 }
282 ?>