[TASK] Replace Space Indent into Tab indent (CGL Cleanup)
[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-2010 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
29 /**
30 * Enter description here...
31 *
32 */
33 class tx_em_Connection_Soap {
34 /**
35 * valid options passed to the constructor :
36 * wsdl : The WSDL location, can be a local file location or
37 * an URL.
38 * soapoptions : Associative array of SOAP options to be passed to
39 * the SOAP implementation constructor, only used for
40 * the phpsoap implement.
41 * authentication : method of authentication :
42 * 'headers' soap headers are used
43 * 'prefix' function prefixes are used
44 * prefix : optional prefix to be put in front of all methods.
45 * format : Which type of return structure :
46 * 'object' PHP objects
47 * 'array' PHP arrays, default
48 */
49 var $options = array();
50
51 /**
52 * SOAP client, instance of PHP SOAP class
53 *
54 * @var SoapClient
55 */
56 var $client = FALSE;
57
58 var $error = FALSE;
59 var $username = FALSE;
60 var $password = FALSE;
61 var $reactid = FALSE;
62
63 /**
64 * Init Soap
65 *
66 * @param array $options
67 * @param string $username
68 * @param string $password
69 * @return [type] ...
70 */
71 function init($options = FALSE, $username = FALSE, $password = FALSE) {
72 if ($username !== FALSE) {
73 if ($password === FALSE) {
74 $this->reactid = $username;
75 } else {
76 $this->username = $username;
77 $this->password = $password;
78 }
79 }
80
81 $options['format'] = $options['format'] == 'object' ? 'object' : 'array';
82
83 if ($options !== FALSE) {
84 $this->options = (array) $options;
85 }
86
87 if (defined('SOAP_1_2')) {
88 $this->client = new SoapClient($options['wsdl'], (array) $options['soapoptions']);
89 } else {
90 $this->client = FALSE;
91 throw new RuntimeException('PHP soap extension not available', 1333754714);
92 }
93 }
94
95 /**
96 * Login
97 *
98 * @param string $username
99 * @param string $password
100 * @return mixed FALSE on failure, $reactid on success
101 */
102 function login($username, $password) {
103 $reactid = $this->call('login', array('username' => $username, 'password' => $password));
104
105 if ($this->error) {
106 return FALSE;
107 }
108
109 $this->reactid = $reactid;
110 $this->username = $username;
111 $this->password = FALSE;
112
113 return $reactid;
114 }
115
116 /**
117 * Logout
118 *
119 * @return unknown
120 */
121 function logout() {
122 $this->call('logout');
123 $this->reactid = FALSE;
124 if ($this->error) {
125 return FALSE;
126 }
127 return TRUE;
128 }
129
130
131 /**
132 * Soapcall
133 *
134 * @param unknown_type $func
135 * @param unknown_type $param
136 * @param unknown_type $username
137 * @param unknown_type $password
138 * @return unknown
139 */
140 function call($func, $param = array(), $username = FALSE, $password = FALSE) {
141 if (!$this->client) {
142 $this->error = sprintf(
143 'Error in %s: No soap client implementation found. ' .
144 'Make sure PHP soap extension is available!', __FILE__);
145 return FALSE;
146 }
147
148 if ($username !== FALSE) {
149 if ($password === FALSE) {
150 $this->reactid = $username;
151 } else {
152 $this->username = $username;
153 $this->password = $password;
154 }
155 }
156
157 if ($this->options['authentication'] == 'prefix') {
158 $param = array_merge(array('reactid' => $this->reactid), $param);
159 }
160
161 if ($this->options['prefix']) {
162 $func = $this->options['prefix'] . ucfirst($func);
163 }
164 $this->error = FALSE;
165
166 return $this->callPhpSOAP($func, $param);
167 }
168
169 /**
170 * Call php soap
171 *
172 * @param unknown_type $func
173 * @param unknown_type $param
174 * @return unknown
175 */
176 function callPhpSOAP($func, $param) {
177 $header = NULL;
178 if ($this->options['authentication'] == 'headers') {
179 if ($this->reactid) {
180 $header = new SoapHeader(
181 '', 'HeaderAuthenticate',
182 (object) array('reactid' => $this->reactid), 1
183 );
184 } elseif ($this->username && $this->password) {
185 $header = new SoapHeader(
186 '', 'HeaderLogin',
187 (object) array(
188 'username' => $this->username,
189 'password' => $this->password
190 ), 1
191 );
192 $this->password = FALSE;
193 }
194 }
195 /*return array(
196 'username' => $this->username,
197 'password' => $this->password,
198 'func' => $func
199 );*/
200
201 $result = $this->client->__soapCall($func, $param, NULL, $header);
202
203 if (is_soap_fault($result)) {
204 $this->error = $result;
205 return FALSE;
206 }
207
208 if (is_a($this->client->headersIn['HeaderAuthenticate'], 'stdClass')) {
209 $this->reactid = $this->client->headersIn['HeaderAuthenticate']->reactid;
210 }
211
212 return $this->options['format'] == 'object' ? $result : $this->object2array($result);
213 }
214
215 /**
216 * Convert object to array
217 *
218 * @param object $object
219 * @return array
220 */
221 function object2array($object) {
222 if (!is_object($object) && !is_array($object)) {
223 return $object;
224 }
225
226 $array = (array) $object;
227 foreach ($array as $key => $value) {
228 $array[$key] = $this->object2array($value);
229 }
230 return $array;
231 }
232
233 /**
234 * Convert array to object
235 *
236 * @param unknown_type $array
237 * @return unknown
238 */
239 function array2object($array) {
240 if (!is_array($array)) {
241 return $array;
242 }
243
244 foreach ($array as $key => $value) {
245 $array[$key] = $this->array2object($value);
246 }
247 return (object) $array;
248 }
249
250 /**
251 * Get last request.
252 *
253 * @return unknown
254 */
255 function lastRequest() {
256 return $this->client->__getLastRequest();
257 }
258
259 /**
260 * Get last response
261 *
262 * @return unknown
263 */
264 function lastResponse() {
265 $this->client->__getLastResponse();
266 }
267
268 /**
269 * Get available functions
270 *
271 * @return unknown
272 */
273 function getFunctions() {
274 return $this->client->__getFunctions();
275 }
276 }
277 ?>