BxBaseModConnectModule.php 12 KB

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