TYPO3 API  SVNRelease
class.tx_em_connection_extdirectsoap.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010 Steffen Kamper <steffen@typo3.org>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 class tx_em_Connection_ExtDirectSoap {
00029     /**
00030      * @var tx_em_Repository
00031      */
00032     var $repository;
00033 
00034     /**
00035      * @var tx_em_Connection_Soap
00036      */
00037     var $soap = NULL;
00038 
00039 
00040     /**
00041      * Keeps instance of settings class.
00042      *
00043      * @var tx_em_Settings
00044      */
00045     static protected $objSettings;
00046 
00047     /**
00048      * @var array
00049      */
00050     protected $settings;
00051 
00052     /**
00053      * @var array
00054      */
00055     protected $accountData = NULL;
00056 
00057     /**
00058      * Constructor
00059      *
00060      * @return void
00061      */
00062     public function __construct() {
00063         $this->settings = $this->getSettingsObject()->getSettings();
00064         /** @var $repository tx_em_Repository */
00065         $this->repository = t3lib_div::makeInstance('tx_em_Repository', $this->settings['selectedRepository']);
00066 
00067         if (isset($this->settings['fe_u']) && isset($this->settings['fe_p']) && $this->settings['fe_u'] !== '' && $this->settings['fe_p'] !== '' ) {
00068             $this->setAccountData($this->settings['fe_u'], $this->settings['fe_p']);
00069         }
00070 
00071 
00072     }
00073 
00074     /**
00075      * Login test with user credentials
00076      *
00077      * @return array
00078      */
00079     public function testUserLogin() {
00080         if (is_array($this->accountData)) {
00081             $login = false;
00082             if ($login) {
00083                 $data = array(
00084                     'success' => TRUE,
00085                     'id' => $login
00086                 );
00087             } else {
00088                 $data = array(
00089                     'success' => FALSE,
00090                     'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_loginFailed')
00091                 );
00092             }
00093         } else {
00094             $data = array(
00095                 'success' => FALSE,
00096                 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_noUserLoginData')
00097             );
00098         }
00099 
00100         return $data;
00101     }
00102 
00103     /**
00104      * Show Info of extension record
00105      *
00106      * @param  array $record
00107      * @return string
00108      */
00109     public function  showRemoteExtInfo($record) {
00110         return t3lib_div::view_array(array($record, $this->settings));
00111     }
00112 
00113     /**
00114      * Checks validity of extension key
00115      *
00116      * @formHandler
00117      * @param  array $parameter
00118      * @return array
00119      */
00120     public function checkExtensionkey($parameter) {
00121         $params = array(
00122             'extensionKey' => $parameter['extkey']
00123         );
00124         $result = $this->soapCall('checkExtensionKey', $params);
00125         $message = $this->getSoapResultMessageFromCode($result['resultCode']);
00126         //debug(array($result,$parameter), $message);
00127         if ($result['resultCode'] == 10501) {
00128             $return =  array(
00129                 'success' => TRUE,
00130                 'message' => $message,
00131                 'valid' => TRUE,
00132                 'raw' => $result
00133             );
00134         } else {
00135             $return =  array(
00136                 'success' => FALSE,
00137                 'message' => $message,
00138                 'raw' => $result
00139             );
00140         }
00141 
00142         return $return;
00143     }
00144 
00145     /**
00146      * Register extension key
00147      *
00148      * @formHandler
00149      * @param  array $parameter
00150      * @return array
00151      */
00152     public function registerExtensionkey($parameter) {
00153         $params = array(
00154             'registerExtensionKeyData' => array(
00155                  'extensionKey' => $parameter['extkey'],
00156                  'title' => $parameter['title'],
00157                  'description' => $parameter['description']
00158             )
00159         );
00160         $result = $this->soapCall('registerExtensionKey', $params);
00161         $message = $this->getSoapResultMessageFromCode($result['resultCode']);
00162 
00163         if ($result['resultCode'] == 10503) {
00164             $return =  array(
00165                 'success' => TRUE,
00166                 'message' => $message,
00167                 'valid' => TRUE,
00168                 'raw' => $result
00169             );
00170         } else {
00171             $return =  array(
00172                 'success' => FALSE,
00173                 'message' => $message,
00174                 'raw' => $result
00175             );
00176         }
00177 
00178         return $return;
00179     }
00180 
00181     /**
00182      * Get own extensions
00183      *
00184      * @return array
00185      */
00186     public function getExtensions($parameter) {
00187         $params = array(
00188             'extensionKeyFilterOptions' => array(
00189                 'username' => $this->settings['fe_u']
00190             )
00191         );
00192         $result = $this->soapCall('getExtensionKeys', $params);
00193         $data = $this->addUploads($result['extensionKeyData']);
00194 
00195         if ($result['simpleResult']['resultCode'] == 10000 && $data !== NULL) {
00196             $return =  array(
00197                 'success' => TRUE,
00198                 'total' => count($result['extensionKeyData']),
00199                 'data' => $data,
00200                 'raw' => $result
00201             );
00202         } else {
00203             $return =  array(
00204                 'success' => FALSE,
00205                 'raw' => $result
00206             );
00207         }
00208 
00209         return $return;
00210     }
00211 
00212     /**
00213      * Delete extension key
00214      *
00215      * @param  string $key
00216      * @return void
00217      */
00218     public function deleteExtensionKey($key) {
00219         $params = array(
00220             'extensionKey' => $key
00221         );
00222         $result = $this->soapCall('deleteExtensionKey', $params);
00223         $message = $this->getSoapResultMessageFromCode($result['resultCode']);
00224 
00225         if ($result['resultCode'] == 10000) {
00226             $return =  array(
00227                 'success' => TRUE,
00228                 'message' => $this->getSoapResultMessageFromCode(10505), // TER API doesn't send correct result code
00229                 'key' => $key
00230             );
00231         } else {
00232             $return =  array(
00233                 'success' => FALSE,
00234                 'message' => $message,
00235                 'key' => $key
00236             );
00237         }
00238 
00239         return $return;
00240     }
00241 
00242     /**
00243      * Transfer extension key to other user
00244      *
00245      * @param  $key
00246      * @param  $user
00247      * @return void
00248      */
00249     public function transferExtensionKey($key, $user) {
00250         $params = array(
00251             'modifyExtensionKeyData' => array(
00252                  'extensionKey' => $key,
00253                  'ownerUsername' => $user
00254             )
00255         );
00256         $result = $this->soapCall('modifyExtensionKey', $params);
00257         $message = $this->getSoapResultMessageFromCode($result['resultCode']);
00258 
00259         if ($result['resultCode'] == 10000) {
00260             $return =  array(
00261                 'success' => TRUE,
00262                 'message' => $message,
00263                 'key' => $key,
00264                 'user' => $user
00265             );
00266         } else {
00267             $return =  array(
00268                 'success' => FALSE,
00269                 'message' => $message,
00270                 'key' => $key,
00271                 'user' => $user
00272             );
00273         }
00274 
00275         return $return;
00276     }
00277 
00278 
00279     /*
00280      * protected class functions
00281      */
00282 
00283     /**
00284      * Sets the account data
00285      *
00286      * @param  string  $user
00287      * @param  string  $password
00288      * @return void
00289      */
00290     protected function setAccountData($user, $password) {
00291         $this->accountData = array(
00292             'accountData' => array(
00293                 'username' => $user,
00294                 'password' => $password
00295             )
00296         );
00297         $this->initSoap();
00298     }
00299 
00300     /**
00301      * Init soap
00302      *
00303      * @return void
00304      */
00305     protected function initSoap() {
00306         if ($this->repository->getWsdlUrl()) {
00307             /** @var $soap tx_em_Connection_Soap */
00308             $this->soap = t3lib_div::makeInstance('tx_em_Connection_Soap');
00309             $this->soap->init(
00310                 array(
00311                     'wsdl' => $this->repository->getWsdlUrl(),
00312                     //'authentication' => 'headers',
00313                     'soapoptions' =>
00314                     array(
00315                         'trace' => 1,
00316                         'exceptions' => 1
00317                     )
00318                 ),
00319                 $this->settings['fe_u'],
00320                 $this->settings['fe_p']
00321             );
00322         }
00323     }
00324     /**
00325      * @param  $data
00326      * @return bool|null|string|tx_em_Settings|unknown
00327      */
00328     protected function addUploads($data) {
00329         if (count((array) $data) === 0) {
00330             return NULL;
00331         }
00332 
00333         foreach ($data as $key => $extkey) {
00334             $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00335                 'extkey, count(version) as uploads',
00336                 'cache_extensions',
00337                 'extkey="' . $extkey['extensionkey'] . '" AND repository=1',
00338                 'extkey'
00339             );
00340             $data[$key]['uploads'] = intval($row['uploads']);
00341             $data[$key]['hasUploads'] = (intval($row['uploads']) > 0);
00342         }
00343 
00344         return $data;
00345     }
00346     /**
00347      * Get settings object
00348      *
00349      * @return tx_em_Settings
00350      */
00351     protected function getSettingsObject() {
00352         if (!is_object(self::$objSettings) && !(self::$objSettings instanceof tx_em_Settings)) {
00353             self::$objSettings = t3lib_div::makeInstance('tx_em_Settings');
00354         }
00355         return self::$objSettings;
00356     }
00357 
00358     /**
00359      * Executes a soap call
00360      *
00361      * @param  $name
00362      * @param  $params
00363      * @return string $response
00364      */
00365     protected function soapCall($name, $params) {
00366         if (!is_object($this->soap)) {
00367             return array(
00368                 'success' => FALSE,
00369                 'error' => $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_noUserLoginData')
00370             );
00371         }
00372 
00373         try {
00374             $response = $this->soap->call(
00375                 $name,
00376                 array_merge($this->accountData, $params),
00377                 $this->accountData['accountData']['username'],
00378                 $this->accountData['accountData']['password']
00379             );
00380             return $response;
00381         } catch (SoapFault $error) {
00382             return array(
00383                 'success' => FALSE,
00384                 'error' => $error->faultstring
00385             );
00386         }
00387 
00388 
00389     }
00390 
00391     /**
00392      * Translates SOAP return codes to messages
00393      *
00394      * @param  int $code
00395      * @return string
00396      */
00397     protected function getSoapResultMessageFromCode($code) {
00398         switch ($code) {
00399             case 10000:
00400                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_ok');
00401             break;
00402             case 102:
00403                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_userNotExists');
00404             break;
00405             case 10500:
00406                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexExists');
00407             break;
00408             case 10501:
00409                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexNotExists');
00410             break;
00411             case 10502:
00412                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexNotValid');
00413             break;
00414             case 10503:
00415                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexRegistered');
00416             break;
00417             case 10504:
00418                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexUploadedSuccess');
00419             break;
00420             case 10505:
00421                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_extkexDeletedSuccess');
00422             break;
00423             default:
00424                 return $GLOBALS['LANG']->sL('LLL:EXT:em/language/locallang.xml:msg_unknownError');
00425 
00426         }
00427     }
00428 }
00429 
00430 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_extdirectsoap.php'])) {
00431     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/connection/class.tx_em_connection_extdirectsoap.php']);
00432 }
00433 
00434 ?>