[BUGFIX] TYPO3 tries to connect to typo3.org on every login
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / connection / class.tx_em_connection_extdirectsoap.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Steffen Kamper <steffen@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class tx_em_Connection_ExtDirectSoap {
29 /**
30 * @var tx_em_Repository
31 */
32 var $repository;
33
34 /**
35 * @var tx_em_Connection_Soap
36 */
37 var $soap = NULL;
38
39
40 /**
41 * Keeps instance of settings class.
42 *
43 * @var tx_em_Settings
44 */
45 static protected $objSettings;
46
47 /**
48 * @var array
49 */
50 protected $settings;
51
52 /**
53 * @var array
54 */
55 protected $accountData = NULL;
56
57 /**
58 * Constructor
59 */
60 public function __construct() {
61 $this->settings = $this->getSettingsObject()->getSettings();
62 /** @var $repository tx_em_Repository */
63 $this->repository = t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
64
65 if (isset($this->settings['fe_u']) && isset($this->settings['fe_p']) && $this->settings['fe_u'] !== '' && $this->settings['fe_p'] !== '' ) {
66 $this->setAccountData($this->settings['fe_u'], $this->settings['fe_p']);
67 }
68 }
69
70 /**
71 * Login test with user credentials
72 *
73 * @deprecated since 4.6, will be removed in 4.8
74 * @return array
75 */
76 public function testUserLogin() {
77
78 // There is a theory which states that if ever anybody discovers exactly what this method is for and why it is here,
79 // it will instantly disappear and be replaced by something even more bizarre and inexplicable.
80 // There is another theory which states that this has already happened.
81 t3lib_div::logDeprecatedFunction();
82
83 if (is_array($this->accountData)) {
84 $login = FALSE;
85 if ($login) {
86 $data = array(
87 'success' => TRUE,
88 'id' => $login
89 );
90 } else {
91 $data = array(
92 'success' => FALSE,
93 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_loginFailed')
94 );
95 }
96 } else {
97 $data = array(
98 'success' => FALSE,
99 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_noUserLoginData')
100 );
101 }
102
103 return $data;
104 }
105
106 /**
107 * Show Info of extension record
108 *
109 * @param array $record
110 * @return string
111 */
112 public function showRemoteExtInfo($record) {
113 return t3lib_utility_Debug::viewArray(array($record, $this->settings));
114 }
115
116 /**
117 * Checks validity of extension key
118 *
119 * @formHandler
120 * @param array $parameter
121 * @return array
122 */
123 public function checkExtensionkey($parameter) {
124 $this->initSoap();
125 $params = array(
126 'extensionKey' => $parameter['extkey']
127 );
128 $result = $this->soapCall('checkExtensionKey', $params);
129 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
130 if ($result['resultCode'] == 10501) {
131 $return = array(
132 'success' => TRUE,
133 'message' => $message,
134 'valid' => TRUE,
135 'raw' => $result
136 );
137 } else {
138 $return = array(
139 'success' => FALSE,
140 'message' => $message,
141 'raw' => $result
142 );
143 }
144
145 return $return;
146 }
147
148 /**
149 * Register extension key
150 *
151 * @formHandler
152 * @param array $parameter
153 * @return array
154 */
155 public function registerExtensionkey($parameter) {
156 $this->initSoap();
157 $params = array(
158 'registerExtensionKeyData' => array(
159 'extensionKey' => $parameter['extkey'],
160 'title' => $parameter['title'],
161 'description' => $parameter['description']
162 )
163 );
164 $result = $this->soapCall('registerExtensionKey', $params);
165 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
166
167 if ($result['resultCode'] == 10503) {
168 $return = array(
169 'success' => TRUE,
170 'message' => $message,
171 'valid' => TRUE,
172 'raw' => $result
173 );
174 } else {
175 $return = array(
176 'success' => FALSE,
177 'message' => $message,
178 'raw' => $result
179 );
180 }
181
182 return $return;
183 }
184
185 /**
186 * Get own extensions
187 *
188 * @return array
189 */
190 public function getExtensions() {
191 $this->initSoap();
192 $params = array(
193 'extensionKeyFilterOptions' => array(
194 'username' => $this->settings['fe_u']
195 )
196 );
197 $result = $this->soapCall('getExtensionKeys', $params);
198 $data = $this->addUploads($result['extensionKeyData']);
199
200 if ($result['simpleResult']['resultCode'] == 10000 && $data !== NULL) {
201 $return = array(
202 'success' => TRUE,
203 'total' => count($result['extensionKeyData']),
204 'data' => $data,
205 'raw' => $result
206 );
207 } else {
208 $return = array(
209 'success' => FALSE,
210 'raw' => $result
211 );
212 }
213
214 return $return;
215 }
216
217 /**
218 * Delete extension key
219 *
220 * @param string $key
221 * @return array
222 */
223 public function deleteExtensionKey($key) {
224 $this->initSoap();
225 $params = array(
226 'extensionKey' => $key
227 );
228 $result = $this->soapCall('deleteExtensionKey', $params);
229 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
230
231 if ($result['resultCode'] == 10000) {
232 $return = array(
233 'success' => TRUE,
234 'message' => $this->getSoapResultMessageFromCode(10505), // TER API doesn't send correct result code
235 'key' => $key
236 );
237 } else {
238 $return = array(
239 'success' => FALSE,
240 'message' => $message,
241 'key' => $key
242 );
243 }
244
245 return $return;
246 }
247
248 /**
249 * Transfer extension key to other user
250 *
251 * @param $key
252 * @param $user
253 * @return array
254 */
255 public function transferExtensionKey($key, $user) {
256 $this->initSoap();
257 $params = array(
258 'modifyExtensionKeyData' => array(
259 'extensionKey' => $key,
260 'ownerUsername' => $user
261 )
262 );
263 $result = $this->soapCall('modifyExtensionKey', $params);
264 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
265
266 if ($result['resultCode'] == 10000) {
267 $return = array(
268 'success' => TRUE,
269 'message' => $message,
270 'key' => $key,
271 'user' => $user
272 );
273 } else {
274 $return = array(
275 'success' => FALSE,
276 'message' => $message,
277 'key' => $key,
278 'user' => $user
279 );
280 }
281
282 return $return;
283 }
284
285
286 /*
287 * protected class functions
288 */
289
290 /**
291 * Sets the account data
292 *
293 * @param string $user
294 * @param string $password
295 * @return void
296 */
297 protected function setAccountData($user, $password) {
298 $this->accountData = array(
299 'accountData' => array(
300 'username' => $user,
301 'password' => $password
302 )
303 );
304 }
305
306 /**
307 * Init soap
308 *
309 * @return void
310 */
311 protected function initSoap() {
312 if(!is_object($this->soap) || !($this->soap instanceof tx_em_Connection_Soap)) {
313 if ($this->repository->getWsdlUrl()) {
314 /** @var $soap tx_em_Connection_Soap */
315 $this->soap = t3lib_div::makeInstance('tx_em_Connection_Soap');
316 $this->soap->init(
317 array(
318 'wsdl' => $this->repository->getWsdlUrl(),
319 'soapoptions' => array(
320 'trace' => 1,
321 'exceptions' => 1
322 )
323 ),
324 $this->settings['fe_u'],
325 $this->settings['fe_p']
326 );
327 }
328 }
329 }
330
331 /**
332 * @param $data
333 * @return bool|NULL|string|tx_em_Settings|unknown
334 */
335 protected function addUploads($data) {
336 if (count((array) $data) === 0) {
337 return NULL;
338 }
339
340 foreach ($data as $key => $extkey) {
341 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
342 'extkey, count(version) as uploads',
343 'cache_extensions',
344 'extkey="' . $extkey['extensionkey'] . '" AND repository=1',
345 'extkey'
346 );
347 $data[$key]['uploads'] = intval($row['uploads']);
348 $data[$key]['hasUploads'] = (intval($row['uploads']) > 0);
349 }
350
351 return $data;
352 }
353 /**
354 * Get settings object
355 *
356 * @return tx_em_Settings
357 */
358 protected function getSettingsObject() {
359 if (!is_object(self::$objSettings) && !(self::$objSettings instanceof tx_em_Settings)) {
360 self::$objSettings = t3lib_div::makeInstance('tx_em_Settings');
361 }
362 return self::$objSettings;
363 }
364
365 /**
366 * Executes a soap call
367 *
368 * @param $name
369 * @param $params
370 * @return string $response
371 */
372 protected function soapCall($name, $params) {
373 if (!is_object($this->soap)) {
374 return array(
375 'success' => FALSE,
376 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_noUserLoginData')
377 );
378 }
379
380 try {
381 $response = $this->soap->call(
382 $name,
383 array_merge($this->accountData, $params),
384 $this->accountData['accountData']['username'],
385 $this->accountData['accountData']['password']
386 );
387 return $response;
388 } catch (SoapFault $error) {
389 return array(
390 'success' => FALSE,
391 'error' => $error->faultstring
392 );
393 }
394
395
396 }
397
398 /**
399 * Translates SOAP return codes to messages
400 *
401 * @param int $code
402 * @return string
403 */
404 protected function getSoapResultMessageFromCode($code) {
405 switch ($code) {
406 case 10000:
407 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_ok');
408 break;
409 case 102:
410 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_userNotExists');
411 break;
412 case 10500:
413 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexExists');
414 break;
415 case 10501:
416 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexNotExists');
417 break;
418 case 10502:
419 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexNotValid');
420 break;
421 case 10503:
422 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexRegistered');
423 break;
424 case 10504:
425 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexUploadedSuccess');
426 break;
427 case 10505:
428 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexDeletedSuccess');
429 break;
430 default:
431 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_unknownError');
432
433 }
434 }
435 }
436
437 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_extdirectsoap.php'])) {
438 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_extdirectsoap.php']);
439 }
440
441 ?>