Cheetah
ChDolphConModule.php
Go to the documentation of this file.
1 <?php
2 
8 require_once( CH_DIRECTORY_PATH_INC . 'profiles.inc.php' );
9 
10 ch_import('ChWsbModuleDb');
11 ch_import('ChWsbConnectModule');
12 ch_import('ChWsbInstallerUtils');
13 ch_import('ChWsbProfilesController');
14 ch_import('ChWsbAlerts');
15 
17 {
18  function __construct(&$aModule)
19  {
20  parent::__construct($aModule);
21  }
22 
29  {
30  parent::_actionAdministration('ch_dolphcon_api_key', '_ch_dolphcon_settings', '_ch_dolphcon_information', '_ch_dolphcon_information_block');
31  }
32 
38  function actionStart()
39  {
40  if (isLogged())
41  $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
42 
43  if (!$this->_oConfig->sApiID || !$this->_oConfig->sApiSecret || !$this->_oConfig->sApiUrl) {
44  $sCode = MsgBox( _t('_ch_dolphcon_profile_error_api_keys') );
45  $this->_oTemplate->getPage(_t('_ch_dolphcon'), $sCode);
46  }
47  else {
48 
49  // define redirect URL to the remote Cheetah site
50  $sUrl = ch_append_url_params($this->_oConfig->sApiUrl . 'auth', array(
51  'response_type' => 'code',
52  'client_id' => $this->_oConfig->sApiID,
53  'redirect_uri' => $this->_oConfig->sPageHandle,
54  'scope' => $this->_oConfig->sScope,
55  'state' => $this->_genCsrfToken(),
56  ));
57  $this->_redirect($sUrl);
58  }
59  }
60 
61  function actionHandle()
62  {
63  // check CSRF token
64  if ($this->_getCsrfToken() != ch_get('state')) {
65  $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_ch_dolphcon_state_invalid')));
66  return;
67  }
68 
69  // check code
70  $sCode = ch_get('code');
71  if (!$sCode) {
72  $sErrorDescription = ch_get('error_description') ? ch_get('error_description') : _t('_Error occured');
73  $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
74  return;
75  }
76 
77  // make request for token
78  $s = ch_file_get_contents($this->_oConfig->sApiUrl . 'token', array(
79  'client_id' => $this->_oConfig->sApiID,
80  'client_secret' => $this->_oConfig->sApiSecret,
81  'grant_type' => 'authorization_code',
82  'code' => $sCode,
83  'redirect_uri' => $this->_oConfig->sPageHandle,
84  ), 'post');
85 
86  // handle error
87  if (!$s || NULL === ($aResponse = json_decode($s, true)) || !isset($aResponse['access_token']) || isset($aResponse['error'])) {
88  $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_Error occured');
89  $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
90  return;
91  }
92 
93  // get the data, especially access_token
94  $sAccessToken = $aResponse['access_token'];
95  $sExpiresIn = $aResponse['expires_in'];
96  $sExpiresAt = new \DateTime('+' . $sExpiresIn . ' seconds');
97  $sRefreshToken = $aResponse['refresh_token'];
98 
99  // request info about profile
100  $s = ch_file_get_contents($this->_oConfig->sApiUrl . 'api/me', array(), 'get', array(
101  'Authorization: Bearer ' . $sAccessToken,
102  ));
103 
104  // handle error
105  if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || isset($aResponse['error'])) {
106  $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_Error occured');
107  $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
108  return;
109  }
110 
111  $aRemoteProfileInfo = $aResponse;
112 
113  if ($aRemoteProfileInfo) {
114 
115  // check if user logged in before
116  $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['id']);
117 
118  if ($iLocalProfileId) {
119  // user already exists
120 
121  $aLocalProfileInfo = getProfileInfo($iLocalProfileId);
122 
123  $this->setLogged($iLocalProfileId, $aLocalProfileInfo['Password']);
124 
125  }
126  else {
127  // register new user
128  $sAlternativeNickName = '';
129  if (getID($aRemoteProfileInfo['NickName']))
130  $sAlternativeNickName = $this->getAlternativeName($aRemoteProfileInfo['NickName']);
131 
132  $this->getJoinAfterPaymentPage($aRemoteProfileInfo);
133 
134  $this->_createProfile($aRemoteProfileInfo, $sAlternativeNickName);
135  }
136  }
137  else {
138  $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_ch_dolphcon_profile_error_info')));
139  }
140  }
141 
142 
148  protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
149  {
150  $aProfileFields = $aProfileInfo;
151  $aProfileFields['NickName'] = $aProfileInfo['NickName'] . $sAlternativeName;
152  return $aProfileFields;
153  }
154 
155  protected function _genCsrfToken($bReturn = false)
156  {
157  if ($GLOBALS['MySQL']->getParam('sys_security_form_token_enable') != 'on' || defined('CH_WSB_CRON_EXECUTE'))
158  return false;
159 
160  $oSession = ChWsbSession::getInstance();
161 
162  $iCsrfTokenLifetime = (int)$this->_oDb->getParam('sys_security_form_token_lifetime');
163  if ($oSession->getValue('ch_dolphcon_csrf_token') === false || ($iCsrfTokenLifetime != 0 && time() - (int)$oSession->getValue('csrf_token_time') > $iCsrfTokenLifetime)) {
164  $sToken = genRndPwd(20, false);
165  $oSession->setValue('ch_dolphcon_csrf_token', $sToken);
166  $oSession->setValue('ch_dolphcon_csrf_token_time', time());
167  }
168  else {
169  $sToken = $oSession->getValue('ch_dolphcon_csrf_token');
170  }
171 
172  return $sToken;
173  }
174 
175  protected function _getCsrfToken()
176  {
177  $oSession = ChWsbSession::getInstance();
178  return $oSession->getValue('ch_dolphcon_csrf_token');
179  }
180 
181 }
$sToken
$sToken
Definition: get_file.php:13
MsgBox
MsgBox($sText, $iTimer=0)
Definition: design.inc.php:175
ChDolphConModule\actionStart
actionStart()
Definition: ChDolphConModule.php:38
ChWsbModule\isLogged
isLogged()
Definition: ChWsbModule.php:113
ChWsbConnectModule\getJoinAfterPaymentPage
getJoinAfterPaymentPage($aProfileInfo)
Definition: ChWsbConnectModule.php:116
$sCode
$sCode
Definition: explanation.php:19
$aModule
$aModule
Definition: classifieds.php:21
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
ch_get
ch_get($sName)
Definition: utils.inc.php:1664
$sUrl
$sUrl
Definition: cart.php:15
php
ChDolphConModule\__construct
__construct(&$aModule)
Definition: ChDolphConModule.php:18
ChDolphConModule\_genCsrfToken
_genCsrfToken($bReturn=false)
Definition: ChDolphConModule.php:155
ChWsbConnectModule\setLogged
setLogged($iProfileId, $sPassword, $sCallbackUrl='', $bRedirect=true)
Definition: ChWsbConnectModule.php:73
ChDolphConModule\_getCsrfToken
_getCsrfToken()
Definition: ChDolphConModule.php:175
ChWsbConnectModule\getAlternativeName
getAlternativeName($sNickName)
Definition: ChWsbConnectModule.php:93
ChWsbConnectModule\_redirect
_redirect($sUrl, $iStatus=302)
Definition: ChWsbConnectModule.php:453
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
ChDolphConModule\actionAdministration
actionAdministration()
Definition: ChDolphConModule.php:28
ChDolphConModule\actionHandle
actionHandle()
Definition: ChDolphConModule.php:61
ch_append_url_params
ch_append_url_params($sUrl, $mixedParams)
Definition: utils.inc.php:1697
ch_file_get_contents
ch_file_get_contents($sFileUrl, $aParams=array(), $sMethod='get', $aHeaders=array(), &$sHttpCode=null)
Definition: utils.inc.php:1357
genRndPwd
genRndPwd($iLength=8, $bSpecialCharacters=true)
Definition: utils.inc.php:1618
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
ChWsbSession\getInstance
static getInstance()
Definition: ChWsbSession.php:28
$s
$s
Definition: embed.php:13
$aProfileInfo
$aProfileInfo
Definition: short_profile_info.php:23
ChWsbConnectModule\_createProfile
_createProfile($aProfileInfo, $sAlternativeName='')
Definition: ChWsbConnectModule.php:193
getID
getID( $str, $with_email=1)
Definition: admin.inc.php:139
ChDolphConModule\_convertRemoteFields
_convertRemoteFields($aProfileInfo, $sAlternativeName='')
Definition: ChDolphConModule.php:148
getProfileInfo
getProfileInfo($iProfileID=0, $checkActiveStatus=false, $forceCache=false)
Definition: profiles.inc.php:249
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ChWsbConnectModule
Definition: ChWsbConnectModule.php:11
ChDolphConModule
Definition: ChDolphConModule.php:17