BxBaseModConnectModule.php 16 KB

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