BxBaseModConnectModule.php 15 KB

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