1
0

BxBaseModConnectModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseConnect Base classes for OAuth connect modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModConnectModule extends BxBaseModGeneralModule
  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. public function serviceGetPrivacyGroups ()
  26. {
  27. $oPrivacyQuery = new BxDolPrivacyQuery();
  28. $a = $oPrivacyQuery->getGroupsBy(array('type' => 'active'));
  29. $aGroups = array();
  30. foreach ($a as $r) {
  31. if (!(int)$r['active'])
  32. continue;
  33. $aGroups[$r['id']] = _t($r['title']);
  34. }
  35. return $aGroups;
  36. }
  37. /**
  38. * Logged profile
  39. *
  40. * @param $iProfileId integer
  41. * @param $sPassword string
  42. * @param $sCallbackUrl
  43. * @param $bRedirect boolean
  44. * @return void
  45. */
  46. function setLogged($iProfileId, $sCallbackUrl = '', $bRedirect = true, $bRememberMe = false)
  47. {
  48. $oProfile = BxDolProfile::getInstance($iProfileId);
  49. bx_login($oProfile->getAccountId(), $bRememberMe);
  50. if ($bRedirect) {
  51. $sCallbackUrl = $sCallbackUrl
  52. ? $sCallbackUrl
  53. : $this -> _oConfig -> sDefaultRedirectUrl;
  54. header('Location: ' . $sCallbackUrl);
  55. }
  56. }
  57. /**
  58. * Create new profile;
  59. *
  60. * @param : $aProfileInfo (array) - remote profile's information;
  61. *
  62. * @param : $sAlternativeName (string) - profiles alternative nickname;
  63. */
  64. function _createProfile($aProfileInfo, $sAlternativeName = '')
  65. {
  66. $mixed = $this->_createProfileRaw($aProfileInfo, $sAlternativeName);
  67. bx_alert($this->getName(), 'profile_created', 0, 0, array('override_result' => &$mixed, 'remote_profile_info' => $aProfileInfo));
  68. // display error
  69. if (is_string($mixed)) {
  70. $this->_oTemplate->getPage(_t($this->_oConfig->sDefaultTitleLangKey), DesignBoxContent(_t($this->_oConfig->sDefaultTitleLangKey), MsgBox($mixed)));
  71. exit;
  72. }
  73. // display join page
  74. if (is_array($mixed) && isset($mixed['join_page_redirect'])) {
  75. $this->_getJoinPage($mixed['profile_fields'], $mixed['remote_profile_info']['id']);
  76. exit;
  77. }
  78. // continue profile creation
  79. if (is_array($mixed) && isset($mixed['profile_id'])) {
  80. $iProfileId = (int)$mixed['profile_id'];
  81. //redirect to other page
  82. header('location:' . $this->_getRedirectUrl($iProfileId, $mixed['existing_profile']));
  83. exit;
  84. }
  85. $this->_oTemplate->getPage( _t($this->_oConfig->sDefaultTitleLangKey), MsgBox(_t('_Error Occured')) );
  86. exit;
  87. }
  88. /**
  89. * @param $aProfileInfo - remote profile info
  90. * @param $sAlternativeName - suffix to add to NickName to make it unique
  91. * @return profile array info, ready for the local database
  92. */
  93. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  94. {
  95. }
  96. /**
  97. * Create new profile;
  98. *
  99. * @param : $aProfileInfo (array) - remote profile's information;
  100. *
  101. * @param : $sAlternativeName (string) - profiles alternative nickname;
  102. * @return : error string or error or request invite form or profile info array on success
  103. */
  104. function _createProfileRaw($aProfileInfo, $sAlternativeName = '', $isAutoFriends = true, $isSetLoggedIn = true)
  105. {
  106. // join by invite only
  107. if (BxDolRequest::serviceExists('bx_invites', 'account_add_form_check') && $sCode = BxDolService::call('bx_invites', 'account_add_form_check'))
  108. return $sCode;
  109. // convert fields to unique format
  110. $aFieldsProfile = $aFieldsAccount = $this->_convertRemoteFields($aProfileInfo, $sAlternativeName);
  111. bx_alert($this->getName(), 'fields_converted', 0, 0, array('override_profile_fields' => &$aFieldsProfile, 'override_account_fields' => &$aFieldsAccount, 'remote_profile_info' => $aProfileInfo));
  112. if (empty($aFieldsProfile['email']))
  113. return _t('_Incorrect Email');
  114. // prepare fields for particular module
  115. $aFieldsAccount = BxDolService::call('system', 'prepare_fields', array($aFieldsAccount));
  116. $aFieldsProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'prepare_fields', array($aFieldsProfile));
  117. // check fields existence in Account
  118. $oFormHelperAccount = BxDolService::call('system', 'forms_helper');
  119. $oFormAccount = $oFormHelperAccount->getObjectFormAdd();
  120. foreach ($aFieldsAccount as $sKey => $mValue) {
  121. if (!$oFormAccount->isFieldExist($sKey))
  122. unset($aFieldsAccount[$sKey]);
  123. }
  124. // check fields existence in Profile
  125. if ('system' != $this->_oConfig->sProfilesModule && $oFormHelperProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'forms_helper')) {
  126. $oFormProfile = $oFormHelperProfile->getObjectFormEdit();
  127. foreach ($aFieldsProfile as $sKey => $mValue) {
  128. if (!$oFormProfile->isFieldExist($sKey))
  129. unset($aFieldsProfile[$sKey]);
  130. }
  131. }
  132. // antispam check
  133. $sErrorMsg = '';
  134. $bSetPendingApproval = false;
  135. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $aFieldsAccount['email'], 'approve' => &$bSetPendingApproval));
  136. if ($sErrorMsg)
  137. return $sErrorMsg;
  138. // check if user with the same email already exists
  139. $oExistingAccount = BxDolAccount::getInstance($aFieldsAccount['email']);
  140. // check redirect page
  141. if ('join' == $this->_oConfig->sRedirectPage && !$oExistingAccount)
  142. return array('remote_profile_info' => $aProfileInfo, 'profile_fields' => $aFieldsAccount, 'join_page_redirect' => true);
  143. // create new profile
  144. if ($oExistingAccount) {
  145. if (!($oExistingProfile = BxDolProfile::getInstanceByAccount($oExistingAccount->id(), true)))
  146. return _t('_sys_txt_error_account_creation');
  147. $iProfileId = $oExistingProfile->id();
  148. $this->setLogged($iProfileId);
  149. }
  150. else {
  151. // create account
  152. $aFieldsAccount['password'] = genRndPwd();
  153. $aFieldsAccount['email_confirmed'] = $this->_oConfig->isAlwaysConfirmEmail;
  154. if (!($iAccountId = $oFormAccount->insert($aFieldsAccount)))
  155. return _t('_sys_txt_error_account_creation');
  156. $isSetPendingApproval = $this->_oConfig->isAlwaysAutoApprove ? false : !(bool)getParam('sys_account_autoapproval');
  157. $iAccountProfileId = $oFormHelperAccount->onAccountCreated ($iAccountId, $isSetPendingApproval, BX_PROFILE_ACTION_EXTERNAL);
  158. // create profile
  159. if (isset($oFormProfile) && $oFormProfile) {
  160. $aFieldsProfile['picture'] = $this->_processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile);
  161. $_POST['picture'] = $aFieldsProfile['picture']; // set POST variable to correctly process images in processFiles method in form object
  162. if (!($iContentId = $oFormProfile->insert($aFieldsProfile)))
  163. return _t('_sys_txt_error_account_creation');
  164. $oFormHelperProfile->setAutoApproval($oFormHelperProfile->isAutoApproval() ? true : $this->_oConfig->isAlwaysAutoApprove);
  165. if ($sErrorMsg = $oFormHelperProfile->onDataAddAfter ($iAccountId, $iContentId))
  166. return $sErrorMsg;
  167. $oProfile = BxDolProfile::getInstanceByAccount($iAccountId, true);
  168. $iProfileId = $oProfile->id();
  169. }
  170. else {
  171. $iProfileId = $iAccountProfileId;
  172. }
  173. // send email with password
  174. sendMailTemplate($this->_oConfig->sEmailTemplatePasswordGenerated, $iAccountId, $iProfileId, array('password' => $aFieldsAccount['password']), BX_EMAIL_SYSTEM);
  175. }
  176. // remember remote profile id for created member
  177. $this ->_oDb->saveRemoteId($iProfileId, $aProfileInfo['id']);
  178. // auto-friend members if they are already friends on remote site
  179. if ($isAutoFriends && method_exists($this, '_makeFriends'))
  180. $this->_makeFriends($iProfileId);
  181. return array('remote_profile_info' => $aProfileInfo, 'profile_id' => $iProfileId, 'existing_profile' => $oExistingAccount ? true : false);
  182. }
  183. protected function _processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile)
  184. {
  185. if (!isset($aFieldsProfile['picture']) || !$aFieldsProfile['picture'])
  186. return 0;
  187. if (!($oStorage = $oFormHelperProfile->getObjectStorage()))
  188. return 0;
  189. if (!($iFileId = $oStorage->storeFileFromUrl($aFieldsProfile['picture'], false, $iAccountProfileId)))
  190. return 0;
  191. return $iFileId;
  192. }
  193. /**
  194. * Get join page
  195. *
  196. * @param $aProfileFields array
  197. * @param $iRemoteProfileId remote profile id
  198. * @return void
  199. */
  200. function _getJoinPage($aProfileFields, $iRemoteProfileId)
  201. {
  202. bx_import('BxDolSession');
  203. $oSession = BxDolSession::getInstance();
  204. $oSession->setValue($this->_oConfig->sSessionUid, $iRemoteProfileId);
  205. $oPage = BxDolPage::getObjectInstanceByURI('create-account');
  206. BxBaseAccountForms::$PROFILE_FIELDS = $aProfileFields;
  207. $this->_oTemplate->getPage(false, $oPage->getCode());
  208. }
  209. /**
  210. * get redirect URL
  211. *
  212. * @param $iProfileId integer - profile ID
  213. * @return string redirect URL
  214. */
  215. function _getRedirectUrl($iProfileId, $isExistingProfile = false)
  216. {
  217. $sRedirectUrl = $this->_oConfig->sDefaultRedirectUrl;
  218. switch($this->_oConfig->sRedirectPage) {
  219. case 'index':
  220. $sRedirectUrl = BX_DOL_URL_ROOT;
  221. break;
  222. case 'settings':
  223. if (!$isExistingProfile) {
  224. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-email');
  225. break;
  226. }
  227. case 'dashboard':
  228. default:
  229. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=dashboard');
  230. break;
  231. }
  232. return $sRedirectUrl;
  233. }
  234. protected function _redirect($sUrl, $iStatus = 302)
  235. {
  236. header("Location:{$sUrl}", true, $iStatus);
  237. exit;
  238. }
  239. protected function _genToken($bReturn = false)
  240. {
  241. $oSession = BxDolSession::getInstance();
  242. $sPrefix = $this->getName();
  243. $iCsrfTokenLifetime = 3600;
  244. if ($oSession->getValue($sPrefix . '_token') === false || (time() - (int)$oSession->getValue($sPrefix . '_token_time') > $iCsrfTokenLifetime)) {
  245. $sToken = genRndPwd(20, false);
  246. $oSession->setValue($sPrefix . '_token', $sToken);
  247. $oSession->setValue($sPrefix . '_token_time', time());
  248. }
  249. else {
  250. $sToken = $oSession->getValue($sPrefix . '_token');
  251. }
  252. return $sToken;
  253. }
  254. protected function _getToken()
  255. {
  256. $oSession = BxDolSession::getInstance();
  257. return $oSession->getValue($this->getName() . '_token');
  258. }
  259. }
  260. /** @} */