BxBaseModConnectModule.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseConnect Base classes for OAuth connect modules
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModConnectModule extends BxDolModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function serviceGetProfilesModules ()
  18. {
  19. $aModules = array();
  20. $a = BxDolService::call('system', 'get_profiles_modules', array(), 'TemplServiceProfiles');
  21. foreach ($a as &$aModule)
  22. $aModules[$aModule['name']] = $aModule['title'];
  23. return $aModules;
  24. }
  25. /**
  26. * Logged profile
  27. *
  28. * @param $iProfileId integer
  29. * @param $sPassword string
  30. * @param $sCallbackUrl
  31. * @param $bRedirect boolean
  32. * @return void
  33. */
  34. function setLogged($iProfileId, $sCallbackUrl = '', $bRedirect = true)
  35. {
  36. $oProfile = BxDolProfile::getInstance($iProfileId);
  37. bx_login($oProfile->getAccountId());
  38. if ($bRedirect) {
  39. $sCallbackUrl = $sCallbackUrl
  40. ? $sCallbackUrl
  41. : $this -> _oConfig -> sDefaultRedirectUrl;
  42. header('Location: ' . $sCallbackUrl);
  43. }
  44. }
  45. /**
  46. * Create new profile;
  47. *
  48. * @param : $aProfileInfo (array) - remote profile's information;
  49. *
  50. * @param : $sAlternativeName (string) - profiles alternative nickname;
  51. */
  52. function _createProfile($aProfileInfo, $sAlternativeName = '')
  53. {
  54. $mixed = $this->_createProfileRaw($aProfileInfo, $sAlternativeName);
  55. // display error
  56. if (is_string($mixed)) {
  57. $this->_oTemplate->getPage(_t($this->_oConfig->sDefaultTitleLangKey), DesignBoxContent(_t($this->_oConfig->sDefaultTitleLangKey), MsgBox($mixed)));
  58. exit;
  59. }
  60. // display join page
  61. if (is_array($mixed) && isset($mixed['join_page_redirect'])) {
  62. $this->_getJoinPage($mixed['profile_fields'], $mixed['remote_profile_info']['id']);
  63. exit;
  64. }
  65. // continue profile creation
  66. if (is_array($mixed) && isset($mixed['profile_id'])) {
  67. $iProfileId = (int)$mixed['profile_id'];
  68. //redirect to other page
  69. header('location:' . $this->_getRedirectUrl($iProfileId, $mixed['existing_profile']));
  70. exit;
  71. }
  72. $this->_oTemplate->getPage( _t($this->_oConfig->sDefaultTitleLangKey), MsgBox(_t('_Error Occured')) );
  73. exit;
  74. }
  75. /**
  76. * @param $aProfileInfo - remote profile info
  77. * @param $sAlternativeName - suffix to add to NickName to make it unique
  78. * @return profile array info, ready for the local database
  79. */
  80. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  81. {
  82. }
  83. /**
  84. * Create new profile;
  85. *
  86. * @param : $aProfileInfo (array) - remote profile's information;
  87. *
  88. * @param : $sAlternativeName (string) - profiles alternative nickname;
  89. * @return : error string or error or request invite form or profile info array on success
  90. */
  91. function _createProfileRaw($aProfileInfo, $sAlternativeName = '', $isAutoFriends = true, $isSetLoggedIn = true)
  92. {
  93. // join by invite only
  94. if (BxDolRequest::serviceExists('bx_invites', 'account_add_form_check') && $sCode = BxDolService::call('bx_invites', 'account_add_form_check'))
  95. return $sCode;
  96. // convert fields to unique format
  97. $aFieldsProfile = $aFieldsAccount = $this->_convertRemoteFields($aProfileInfo, $sAlternativeName);
  98. if (empty($aFieldsProfile['email']))
  99. return _t('_Incorrect Email');
  100. // prepare fields for particular module
  101. $aFieldsAccount = BxDolService::call('system', 'prepare_fields', array($aFieldsAccount));
  102. $aFieldsProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'prepare_fields', array($aFieldsProfile));
  103. // check fields existence in Account
  104. $oFormHelperAccount = BxDolService::call('system', 'forms_helper');
  105. $oFormAccount = $oFormHelperAccount->getObjectFormAdd();
  106. foreach ($aFieldsAccount as $sKey => $mValue) {
  107. if (!$oFormAccount->isFieldExist($sKey))
  108. unset($aFieldsAccount[$sKey]);
  109. }
  110. // check fields existence in Profile
  111. if ('system' != $this->_oConfig->sProfilesModule && $oFormHelperProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'forms_helper')) {
  112. $oFormProfile = $oFormHelperProfile->getObjectFormAdd();
  113. foreach ($aFieldsProfile as $sKey => $mValue) {
  114. if (!$oFormProfile->isFieldExist($sKey))
  115. unset($aFieldsProfile[$sKey]);
  116. }
  117. }
  118. // antispam check
  119. $sErrorMsg = '';
  120. $bSetPendingApproval = false;
  121. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $aFieldsAccount['email'], 'approve' => &$bSetPendingApproval));
  122. if ($sErrorMsg)
  123. return $sErrorMsg;
  124. // check if user with the same email already exists
  125. $oExistingAccount = BxDolAccount::getInstance($aFieldsAccount['email']);
  126. // check redirect page
  127. if ('join' == $this->_oConfig->sRedirectPage && !$oExistingAccount)
  128. return array('remote_profile_info' => $aProfileInfo, 'profile_fields' => $aFieldsAccount, 'join_page_redirect' => true);
  129. // create new profile
  130. if ($oExistingAccount) {
  131. if (!($oExistingProfile = BxDolProfile::getInstanceByAccount($oExistingAccount->id())))
  132. return _t('_sys_txt_error_account_creation');
  133. $iProfileId = $oExistingProfile->id();
  134. $this->setLogged($iProfileId);
  135. }
  136. else {
  137. // create account
  138. $aFieldsAccount['password'] = genRndPwd();
  139. $aFieldsAccount['email_confirmed'] = $this->_oConfig->isAlwaysConfirmEmail;
  140. if (!($iAccountId = $oFormAccount->insert($aFieldsAccount)))
  141. return _t('_sys_txt_error_account_creation');
  142. $isSetPendingApproval = $this->_oConfig->isAlwaysAutoApprove ? false : !(bool)getParam('sys_account_autoapproval');
  143. $iAccountProfileId = $oFormHelperAccount->onAccountCreated ($iAccountId, $isSetPendingApproval, BX_PROFILE_ACTION_EXTERNAL);
  144. // create profile
  145. if (isset($oFormProfile) && $oFormProfile) {
  146. $aFieldsProfile['picture'] = $this->_processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile);
  147. if (!($iContentId = $oFormProfile->insert($aFieldsProfile)))
  148. return _t('_sys_txt_error_account_creation');
  149. $oFormHelperProfile->setAutoApproval($oFormHelperProfile->isAutoApproval() ? true : $this->_oConfig->isAlwaysAutoApprove);
  150. if ($sErrorMsg = $oFormHelperProfile->onDataAddAfter ($iAccountId, $iContentId))
  151. return $sErrorMsg;
  152. $oProfile = BxDolProfile::getInstanceByAccount($iAccountId);
  153. $iProfileId = $oProfile->id();
  154. }
  155. else {
  156. $iProfileId = $iAccountProfileId;
  157. }
  158. // send email with password
  159. sendMailTemplate($this->_oConfig->sEmailTemplatePasswordGenerated, $iAccountId, $iProfileId, array('password' => $aFieldsAccount['password']), BX_EMAIL_SYSTEM);
  160. }
  161. // remember remote profile id for created member
  162. $this ->_oDb->saveRemoteId($iProfileId, $aProfileInfo['id']);
  163. // auto-friend members if they are already friends on remote site
  164. if ($isAutoFriends && method_exists($this, '_makeFriends'))
  165. $this->_makeFriends($iProfileId);
  166. return array('remote_profile_info' => $aProfileInfo, 'profile_id' => $iProfileId, 'existing_profile' => $oExistingAccount ? true : false);
  167. }
  168. protected function _processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile)
  169. {
  170. if (!isset($aFieldsProfile['picture']) || !$aFieldsProfile['picture'])
  171. return 0;
  172. if (!($oStorage = $oFormHelperProfile->getObjectStorage()))
  173. return 0;
  174. if (!($iFileId = $oStorage->storeFileFromUrl($aFieldsProfile['picture'], false, $iAccountProfileId)))
  175. return 0;
  176. return $iFileId;
  177. }
  178. /**
  179. * Get join page
  180. *
  181. * @param $aProfileFields array
  182. * @param $iRemoteProfileId remote profile id
  183. * @return void
  184. */
  185. function _getJoinPage($aProfileFields, $iRemoteProfileId)
  186. {
  187. bx_import('BxDolSession');
  188. $oSession = BxDolSession::getInstance();
  189. $oSession->setValue($this->_oConfig->sSessionUid, $iRemoteProfileId);
  190. $oPage = BxDolPage::getObjectInstanceByURI('create-account');
  191. BxBaseAccountForms::$PROFILE_FIELDS = $aProfileFields;
  192. $this->_oTemplate->getPage(false, $oPage->getCode());
  193. }
  194. /**
  195. * get redirect URL
  196. *
  197. * @param $iProfileId integer - profile ID
  198. * @return string redirect URL
  199. */
  200. function _getRedirectUrl($iProfileId, $isExistingProfile = false)
  201. {
  202. $sRedirectUrl = $this->_oConfig->sDefaultRedirectUrl;
  203. switch($this->_oConfig->sRedirectPage) {
  204. case 'index':
  205. $sRedirectUrl = BX_DOL_URL_ROOT;
  206. break;
  207. case 'settings':
  208. if (!$isExistingProfile) {
  209. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-email');
  210. break;
  211. }
  212. case 'dashboard':
  213. default:
  214. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=dashboard');
  215. break;
  216. }
  217. return $sRedirectUrl;
  218. }
  219. }
  220. /** @} */