BxBaseModConnectModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. //set redirect from the session if it was added
  56. $oSession = BxDolSession::getInstance();
  57. $sSessionKey = $this->_oConfig->getName() . '_relocate';
  58. if ($oSession->isValue($sSessionKey))
  59. $sCallbackUrl = $sCallbackUrl ? $sCallbackUrl : $oSession->getValue($sSessionKey);
  60. $sCallbackUrl = $sCallbackUrl ? $sCallbackUrl : $this -> _oConfig -> sDefaultRedirectUrl;
  61. if($this->_bIsApi)
  62. return [bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url($sCallbackUrl)])];
  63. header('Location: ' . $sCallbackUrl);
  64. exit;
  65. }
  66. }
  67. /**
  68. * Create new profile;
  69. *
  70. * @param : $aProfileInfo (array) - remote profile's information;
  71. *
  72. * @param : $sAlternativeName (string) - profiles alternative nickname;
  73. */
  74. function _createProfile($aProfileInfo, $sAlternativeName = '')
  75. {
  76. $mixed = $this->_createProfileRaw($aProfileInfo, $sAlternativeName);
  77. /**
  78. * @hooks
  79. * @hookdef hook-bx_base_connect-profile_created '{module_name}', 'profile_created' - hook after a profile was created
  80. * - $unit_name - module name
  81. * - $action - equals `profile_created`
  82. * - $object_id - not used
  83. * - $sender_id - not used
  84. * - $extra_params - array of additional params with the following array keys:
  85. * - `override_result` - [string] or [array] by ref, result of profile creation, can be overridden in hook processing. If `string` then show error, if `array` with `join_page_redirect` key then show Create Account page, if `array` with `profile_id` key then redirect to profile page
  86. * - `remote_profile_info` - [array] array with remote profile info
  87. * @hook @ref hook-bx_base_connect-profile_created
  88. */
  89. bx_alert($this->getName(), 'profile_created', 0, 0, [
  90. 'override_result' => &$mixed,
  91. 'remote_profile_info' => $aProfileInfo
  92. ]);
  93. // display error
  94. if (is_string($mixed)) {
  95. if($this->_bIsApi)
  96. return [bx_api_get_msg($mixed)];
  97. $this->_oTemplate->getPage(_t($this->_oConfig->sDefaultTitleLangKey), DesignBoxContent(_t($this->_oConfig->sDefaultTitleLangKey), MsgBox($mixed)));
  98. exit;
  99. }
  100. // display join page
  101. if (is_array($mixed) && isset($mixed['join_page_redirect'])) {
  102. if($this->_bIsApi)
  103. return []; //--- Isn't currently supported for API.
  104. $this->_getJoinPage($mixed['profile_fields'], $mixed['remote_profile_info']['id']);
  105. exit;
  106. }
  107. // continue profile creation
  108. if (is_array($mixed) && isset($mixed['profile_id'])) {
  109. $iProfileId = (int)$mixed['profile_id'];
  110. $sRedirectUrl = $this->_getRedirectUrl($iProfileId, $mixed['existing_profile']);
  111. if($this->_bIsApi)
  112. return [bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url($sRedirectUrl)])];
  113. //redirect to other page
  114. header('location:' . $sRedirectUrl);
  115. exit;
  116. }
  117. if($this->_bIsApi)
  118. return [bx_api_get_msg(_t('_Error Occured'))];
  119. $this->_oTemplate->getPage( _t($this->_oConfig->sDefaultTitleLangKey), MsgBox(_t('_Error Occured')) );
  120. exit;
  121. }
  122. /**
  123. * @param $aProfileInfo - remote profile info
  124. * @param $sAlternativeName - suffix to add to NickName to make it unique
  125. * @return profile array info, ready for the local database
  126. */
  127. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  128. {
  129. }
  130. /**
  131. * Create new profile;
  132. *
  133. * @param : $aProfileInfo (array) - remote profile's information;
  134. *
  135. * @param : $sAlternativeName (string) - profiles alternative nickname;
  136. * @return : error string or error or request invite form or profile info array on success
  137. */
  138. function _createProfileRaw($aProfileInfo, $sAlternativeName = '', $isAutoFriends = true, $isSetLoggedIn = true)
  139. {
  140. // join by invite only
  141. if (BxDolRequest::serviceExists('bx_invites', 'account_add_form_check') && $sCode = BxDolService::call('bx_invites', 'account_add_form_check'))
  142. return $sCode;
  143. // convert fields to unique format
  144. $aFieldsProfile = $aFieldsAccount = $this->_convertRemoteFields($aProfileInfo, $sAlternativeName);
  145. /**
  146. * @hooks
  147. * @hookdef hook-bx_base_connect-fields_converted '{module_name}', 'fields_converted' - hook before a profile was created, which allows to modify account and/or profile fields before creation
  148. * - $unit_name - module name
  149. * - $action - equals `fields_converted`
  150. * - $object_id - not used
  151. * - $sender_id - not used
  152. * - $extra_params - array of additional params with the following array keys:
  153. * - `override_profile_fields` - [array] by ref, profile fields, can be overridden in hook processing
  154. * - `override_account_fields` - [array] by ref, account fields, can be overridden in hook processing
  155. * - `remote_profile_info` - [array] array with remote profile info
  156. * @hook @ref hook-bx_base_connect-fields_converted
  157. */
  158. bx_alert($this->getName(), 'fields_converted', 0, 0, [
  159. 'override_profile_fields' => &$aFieldsProfile,
  160. 'override_account_fields' => &$aFieldsAccount,
  161. 'remote_profile_info' => $aProfileInfo
  162. ]);
  163. if (empty($aFieldsProfile['email']))
  164. return _t('_Incorrect Email');
  165. // prepare fields for particular module
  166. $aFieldsAccount = BxDolService::call('system', 'prepare_fields', array($aFieldsAccount));
  167. $aFieldsProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'prepare_fields', array($aFieldsProfile));
  168. // check fields existence in Account
  169. $oFormHelperAccount = BxDolService::call('system', 'forms_helper');
  170. $oFormAccount = $oFormHelperAccount->getObjectFormAdd();
  171. foreach ($aFieldsAccount as $sKey => $mValue) {
  172. if (!$oFormAccount->isFieldExist($sKey))
  173. unset($aFieldsAccount[$sKey]);
  174. }
  175. // check fields existence in Profile
  176. if ('system' != $this->_oConfig->sProfilesModule && $oFormHelperProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'forms_helper')) {
  177. $oFormProfile = $oFormHelperProfile->getObjectFormEdit();
  178. foreach ($aFieldsProfile as $sKey => $mValue) {
  179. if (!$oFormProfile->isFieldExist($sKey))
  180. unset($aFieldsProfile[$sKey]);
  181. }
  182. }
  183. // antispam check
  184. $sErrorMsg = '';
  185. $bSetPendingApproval = false;
  186. /**
  187. * @hooks
  188. * @hookdef hook-account-check_join 'account', 'check_join' - hook to check email address for spam
  189. * - $unit_name - equals `account`
  190. * - $action - equals `check_join`
  191. * - $object_id - not used
  192. * - $sender_id - not used
  193. * - $extra_params - array of additional params with the following array keys:
  194. * - `error_msg` - [string] by ref, error message, can be overridden in hook processing
  195. * - `email` - [string] email address to check
  196. * - `approve` - [boolean] by ref, pending approval status for created profile, can be overridden in hook processing
  197. * @hook @ref hook-account-check_join
  198. */
  199. bx_alert('account', 'check_join', 0, false, [
  200. 'error_msg' => &$sErrorMsg,
  201. 'email' => $aFieldsAccount['email'],
  202. 'approve' => &$bSetPendingApproval
  203. ]);
  204. if ($sErrorMsg)
  205. return $sErrorMsg;
  206. // check if user with the same email already exists
  207. $oExistingAccount = $this->_getExistingAccount($aProfileInfo, $aFieldsAccount, $aFieldsProfile);
  208. // check redirect page
  209. if ('join' == $this->_oConfig->sRedirectPage && !$oExistingAccount)
  210. return array('remote_profile_info' => $aProfileInfo, 'profile_fields' => $aFieldsAccount, 'join_page_redirect' => true);
  211. // create new profile
  212. if ($oExistingAccount) {
  213. if (!($oExistingProfile = BxDolProfile::getInstanceByAccount($oExistingAccount->id(), true)))
  214. return _t('_sys_txt_error_account_creation');
  215. $iProfileId = $oExistingProfile->id();
  216. $this->setLogged($iProfileId);
  217. }
  218. else {
  219. // create account
  220. $aFieldsAccount['password'] = $this->_genPwd($aProfileInfo, $aFieldsAccount, $aFieldsProfile);
  221. if (!($iAccountId = $oFormAccount->insert($aFieldsAccount)))
  222. return _t('_sys_txt_error_account_creation');
  223. $isSetPendingApproval = $this->_isSetPendingApprovalAccount($aProfileInfo, $aFieldsAccount, $aFieldsProfile);
  224. $iAccountProfileId = $oFormHelperAccount->onAccountCreated ($iAccountId, $isSetPendingApproval, BX_PROFILE_ACTION_EXTERNAL);
  225. // create profile
  226. if (isset($oFormProfile) && $oFormProfile) {
  227. $aFieldsProfile['author'] = $iAccountProfileId;
  228. $aFieldsProfile['picture'] = $this->_processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile);
  229. $_POST['picture'] = $aFieldsProfile['picture']; // set POST variable to correctly process images in processFiles method in form object
  230. if (!($iContentId = $oFormProfile->insert($aFieldsProfile)))
  231. return _t('_sys_txt_error_account_creation');
  232. $isSetPendingApprovalProfile = $this->_isSetPendingApprovalProfile($aProfileInfo, $oFormHelperProfile, $aFieldsAccount, $aFieldsProfile);
  233. $oFormHelperProfile->setAutoApproval($isSetPendingApprovalProfile);
  234. if ($sErrorMsg = $oFormHelperProfile->onDataAddAfter ($iAccountId, $iContentId))
  235. return $sErrorMsg;
  236. $oProfile = BxDolProfile::getInstanceByAccount($iAccountId, true);
  237. $iProfileId = $oProfile->id();
  238. }
  239. else {
  240. $iProfileId = $iAccountProfileId;
  241. }
  242. $oAccount = BxDolAccount::getInstance($iAccountId);
  243. if ($oAccount)
  244. $oAccount->updateEmailConfirmed($this->_oConfig->isAlwaysConfirmEmail);
  245. // send email with password
  246. if ($this->_oConfig->bSendPasswordGenerated)
  247. sendMailTemplate($this->_oConfig->sEmailTemplatePasswordGenerated, $iAccountId, $iProfileId, array('password' => $aFieldsAccount['password']), BX_EMAIL_SYSTEM);
  248. }
  249. // remember remote profile id for created member
  250. $this ->_oDb->saveRemoteId($iProfileId, $aProfileInfo['id']);
  251. // auto-friend members if they are already friends on remote site
  252. if ($isAutoFriends && method_exists($this, '_makeFriends'))
  253. $this->_makeFriends($iProfileId);
  254. return array('remote_profile_info' => $aProfileInfo, 'profile_id' => $iProfileId, 'existing_profile' => $oExistingAccount ? true : false);
  255. }
  256. protected function _processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile)
  257. {
  258. if (!isset($aFieldsProfile['picture']) || !$aFieldsProfile['picture'])
  259. return 0;
  260. if (!($oStorage = $oFormHelperProfile->getObjectStorage()))
  261. return 0;
  262. if (!($iFileId = $oStorage->storeFileFromUrl($aFieldsProfile['picture'], false, $iAccountProfileId)))
  263. return 0;
  264. return $iFileId;
  265. }
  266. /**
  267. * Get join page
  268. *
  269. * @param $aProfileFields array
  270. * @param $iRemoteProfileId remote profile id
  271. * @return void
  272. */
  273. function _getJoinPage($aProfileFields, $iRemoteProfileId)
  274. {
  275. bx_import('BxDolSession');
  276. $oSession = BxDolSession::getInstance();
  277. $oSession->setValue($this->_oConfig->sSessionUid, $iRemoteProfileId);
  278. $oPage = BxDolPage::getObjectInstanceByURI('create-account');
  279. BxBaseAccountForms::$PROFILE_FIELDS = $aProfileFields;
  280. $this->_oTemplate->getPage(false, $oPage->getCode());
  281. }
  282. /**
  283. * get redirect URL
  284. *
  285. * @param $iProfileId integer - profile ID
  286. * @return string redirect URL
  287. */
  288. function _getRedirectUrl($iProfileId, $isExistingProfile = false)
  289. {
  290. $sRedirectUrl = $this->_oConfig->sDefaultRedirectUrl;
  291. switch($this->_oConfig->sRedirectPage) {
  292. case 'index':
  293. $sRedirectUrl = BX_DOL_URL_ROOT;
  294. break;
  295. case 'settings':
  296. if (!$isExistingProfile) {
  297. $sRedirectUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-email'));
  298. break;
  299. }
  300. case 'dashboard':
  301. default:
  302. $sRedirectUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=dashboard'));
  303. break;
  304. }
  305. return $sRedirectUrl;
  306. }
  307. protected function _redirect($sUrl, $iStatus = 302)
  308. {
  309. header("Location:{$sUrl}", true, $iStatus);
  310. exit;
  311. }
  312. protected function _genToken($bReturn = false)
  313. {
  314. $oSession = BxDolSession::getInstance();
  315. $sPrefix = $this->getName();
  316. $iCsrfTokenLifetime = 3600;
  317. if ($oSession->getValue($sPrefix . '_token') === false || (time() - (int)$oSession->getValue($sPrefix . '_token_time') > $iCsrfTokenLifetime)) {
  318. $sToken = genRndPwd(20, false);
  319. $oSession->setValue($sPrefix . '_token', $sToken);
  320. $oSession->setValue($sPrefix . '_token_time', time());
  321. }
  322. else {
  323. $sToken = $oSession->getValue($sPrefix . '_token');
  324. }
  325. return $sToken;
  326. }
  327. protected function _getToken()
  328. {
  329. $oSession = BxDolSession::getInstance();
  330. return $oSession->getValue($this->getName() . '_token');
  331. }
  332. protected function _getExistingAccount($aProfileInfo, &$aFieldsAccount, &$aFieldsProfile)
  333. {
  334. return BxDolAccount::getInstance($aFieldsAccount['email']);
  335. }
  336. protected function _genPwd($aProfileInfo, &$aFieldsAccount, &$aFieldsProfile)
  337. {
  338. return genRndPwd();
  339. }
  340. protected function _isSetPendingApprovalAccount($aProfileInfo, &$aFieldsAccount, &$aFieldsProfile)
  341. {
  342. return $this->_oConfig->isAlwaysAutoApprove ? false : !(bool)getParam('sys_account_autoapproval');
  343. }
  344. protected function _isSetPendingApprovalProfile($aProfileInfo, $oFormHelperProfile, &$aFieldsAccount, &$aFieldsProfile)
  345. {
  346. return $oFormHelperProfile->isAutoApproval() ? true : $this->_oConfig->isAlwaysAutoApprove;
  347. }
  348. }
  349. /** @} */