Cleanup: Updated copyright comments
[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-2011 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 * @return void
61 */
62 public function __construct() {
63 $this->settings = $this->getSettingsObject()->getSettings();
64 /** @var $repository tx_em_Repository */
65 $this->repository = t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
66
67 if (isset($this->settings['fe_u']) && isset($this->settings['fe_p']) && $this->settings['fe_u'] !== '' && $this->settings['fe_p'] !== '' ) {
68 $this->setAccountData($this->settings['fe_u'], $this->settings['fe_p']);
69 }
70
71
72 }
73
74 /**
75 * Login test with user credentials
76 *
77 * @return array
78 */
79 public function testUserLogin() {
80 if (is_array($this->accountData)) {
81 $login = false;
82 if ($login) {
83 $data = array(
84 'success' => TRUE,
85 'id' => $login
86 );
87 } else {
88 $data = array(
89 'success' => FALSE,
90 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_loginFailed')
91 );
92 }
93 } else {
94 $data = array(
95 'success' => FALSE,
96 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_noUserLoginData')
97 );
98 }
99
100 return $data;
101 }
102
103 /**
104 * Show Info of extension record
105 *
106 * @param array $record
107 * @return string
108 */
109 public function showRemoteExtInfo($record) {
110 return t3lib_div::view_array(array($record, $this->settings));
111 }
112
113 /**
114 * Checks validity of extension key
115 *
116 * @formHandler
117 * @param array $parameter
118 * @return array
119 */
120 public function checkExtensionkey($parameter) {
121 $params = array(
122 'extensionKey' => $parameter['extkey']
123 );
124 $result = $this->soapCall('checkExtensionKey', $params);
125 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
126 //debug(array($result,$parameter), $message);
127 if ($result['resultCode'] == 10501) {
128 $return = array(
129 'success' => TRUE,
130 'message' => $message,
131 'valid' => TRUE,
132 'raw' => $result
133 );
134 } else {
135 $return = array(
136 'success' => FALSE,
137 'message' => $message,
138 'raw' => $result
139 );
140 }
141
142 return $return;
143 }
144
145 /**
146 * Register extension key
147 *
148 * @formHandler
149 * @param array $parameter
150 * @return array
151 */
152 public function registerExtensionkey($parameter) {
153 $params = array(
154 'registerExtensionKeyData' => array(
155 'extensionKey' => $parameter['extkey'],
156 'title' => $parameter['title'],
157 'description' => $parameter['description']
158 )
159 );
160 $result = $this->soapCall('registerExtensionKey', $params);
161 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
162
163 if ($result['resultCode'] == 10503) {
164 $return = array(
165 'success' => TRUE,
166 'message' => $message,
167 'valid' => TRUE,
168 'raw' => $result
169 );
170 } else {
171 $return = array(
172 'success' => FALSE,
173 'message' => $message,
174 'raw' => $result
175 );
176 }
177
178 return $return;
179 }
180
181 /**
182 * Get own extensions
183 *
184 * @return array
185 */
186 public function getExtensions() {
187 $params = array(
188 'extensionKeyFilterOptions' => array(
189 'username' => $this->settings['fe_u']
190 )
191 );
192 $result = $this->soapCall('getExtensionKeys', $params);
193 $data = $this->addUploads($result['extensionKeyData']);
194
195 if ($result['simpleResult']['resultCode'] == 10000 && $data !== NULL) {
196 $return = array(
197 'success' => TRUE,
198 'total' => count($result['extensionKeyData']),
199 'data' => $data,
200 'raw' => $result
201 );
202 } else {
203 $return = array(
204 'success' => FALSE,
205 'raw' => $result
206 );
207 }
208
209 return $return;
210 }
211
212 /**
213 * Delete extension key
214 *
215 * @param string $key
216 * @return void
217 */
218 public function deleteExtensionKey($key) {
219 $params = array(
220 'extensionKey' => $key
221 );
222 $result = $this->soapCall('deleteExtensionKey', $params);
223 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
224
225 if ($result['resultCode'] == 10000) {
226 $return = array(
227 'success' => TRUE,
228 'message' => $this->getSoapResultMessageFromCode(10505), // TER API doesn't send correct result code
229 'key' => $key
230 );
231 } else {
232 $return = array(
233 'success' => FALSE,
234 'message' => $message,
235 'key' => $key
236 );
237 }
238
239 return $return;
240 }
241
242 /**
243 * Transfer extension key to other user
244 *
245 * @param $key
246 * @param $user
247 * @return void
248 */
249 public function transferExtensionKey($key, $user) {
250 $params = array(
251 'modifyExtensionKeyData' => array(
252 'extensionKey' => $key,
253 'ownerUsername' => $user
254 )
255 );
256 $result = $this->soapCall('modifyExtensionKey', $params);
257 $message = $this->getSoapResultMessageFromCode($result['resultCode']);
258
259 if ($result['resultCode'] == 10000) {
260 $return = array(
261 'success' => TRUE,
262 'message' => $message,
263 'key' => $key,
264 'user' => $user
265 );
266 } else {
267 $return = array(
268 'success' => FALSE,
269 'message' => $message,
270 'key' => $key,
271 'user' => $user
272 );
273 }
274
275 return $return;
276 }
277
278
279 /*
280 * protected class functions
281 */
282
283 /**
284 * Sets the account data
285 *
286 * @param string $user
287 * @param string $password
288 * @return void
289 */
290 protected function setAccountData($user, $password) {
291 $this->accountData = array(
292 'accountData' => array(
293 'username' => $user,
294 'password' => $password
295 )
296 );
297 $this->initSoap();
298 }
299
300 /**
301 * Init soap
302 *
303 * @return void
304 */
305 protected function initSoap() {
306 if ($this->repository->getWsdlUrl()) {
307 /** @var $soap tx_em_Connection_Soap */
308 $this->soap = t3lib_div::makeInstance('tx_em_Connection_Soap');
309 $this->soap->init(
310 array(
311 'wsdl' => $this->repository->getWsdlUrl(),
312 //'authentication' => 'headers',
313 'soapoptions' =>
314 array(
315 'trace' => 1,
316 'exceptions' => 1
317 )
318 ),
319 $this->settings['fe_u'],
320 $this->settings['fe_p']
321 );
322 }
323 }
324 /**
325 * @param $data
326 * @return bool|null|string|tx_em_Settings|unknown
327 */
328 protected function addUploads($data) {
329 if (count((array) $data) === 0) {
330 return NULL;
331 }
332
333 foreach ($data as $key => $extkey) {
334 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
335 'extkey, count(version) as uploads',
336 'cache_extensions',
337 'extkey="' . $extkey['extensionkey'] . '" AND repository=1',
338 'extkey'
339 );
340 $data[$key]['uploads'] = intval($row['uploads']);
341 $data[$key]['hasUploads'] = (intval($row['uploads']) > 0);
342 }
343
344 return $data;
345 }
346 /**
347 * Get settings object
348 *
349 * @return tx_em_Settings
350 */
351 protected function getSettingsObject() {
352 if (!is_object(self::$objSettings) && !(self::$objSettings instanceof tx_em_Settings)) {
353 self::$objSettings = t3lib_div::makeInstance('tx_em_Settings');
354 }
355 return self::$objSettings;
356 }
357
358 /**
359 * Executes a soap call
360 *
361 * @param $name
362 * @param $params
363 * @return string $response
364 */
365 protected function soapCall($name, $params) {
366 try {
367 $response = $this->soap->call(
368 $name,
369 array_merge($this->accountData, $params),
370 $this->accountData['accountData']['username'],
371 $this->accountData['accountData']['password']
372 );
373 return $response;
374 } catch (SoapFault $error) {
375 return array(
376 'success' => FALSE,
377 'error' => $error->faultstring
378 );
379 }
380
381
382 }
383
384 /**
385 * Translates SOAP return codes to messages
386 *
387 * @param int $code
388 * @return string
389 */
390 protected function getSoapResultMessageFromCode($code) {
391 switch ($code) {
392 case 10000:
393 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_ok');
394 break;
395 case 102:
396 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_userNotExists');
397 break;
398 case 10500:
399 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexExists');
400 break;
401 case 10501:
402 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexNotExists');
403 break;
404 case 10502:
405 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexNotValid');
406 break;
407 case 10503:
408 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexRegistered');
409 break;
410 case 10504:
411 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexUploadedSuccess');
412 break;
413 case 10505:
414 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexDeletedSuccess');
415 break;
416 default:
417 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_unknownError');
418
419 }
420 }
421 }
422
423 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_extdirectsoap.php'])) {
424 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_extdirectsoap.php']);
425 }
426
427 ?>