1
0

BxBaseModConnectModule.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 = BxDolAccount::getInstance($aFieldsAccount['email']);
  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'] = genRndPwd();
  206. if (!($iAccountId = $oFormAccount->insert($aFieldsAccount)))
  207. return _t('_sys_txt_error_account_creation');
  208. $isSetPendingApproval = $this->_oConfig->isAlwaysAutoApprove ? false : !(bool)getParam('sys_account_autoapproval');
  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. $oFormHelperProfile->setAutoApproval($oFormHelperProfile->isAutoApproval() ? true : $this->_oConfig->isAlwaysAutoApprove);
  218. if ($sErrorMsg = $oFormHelperProfile->onDataAddAfter ($iAccountId, $iContentId))
  219. return $sErrorMsg;
  220. $oProfile = BxDolProfile::getInstanceByAccount($iAccountId, true);
  221. $iProfileId = $oProfile->id();
  222. }
  223. else {
  224. $iProfileId = $iAccountProfileId;
  225. }
  226. $oAccount = BxDolAccount::getInstance($iAccountId);
  227. if ($oAccount)
  228. $oAccount->updateEmailConfirmed($this->_oConfig->isAlwaysConfirmEmail);
  229. // send email with password
  230. if ($this->_oConfig->bSendPasswordGenerated)
  231. sendMailTemplate($this->_oConfig->sEmailTemplatePasswordGenerated, $iAccountId, $iProfileId, array('password' => $aFieldsAccount['password']), BX_EMAIL_SYSTEM);
  232. }
  233. // remember remote profile id for created member
  234. $this ->_oDb->saveRemoteId($iProfileId, $aProfileInfo['id']);
  235. // auto-friend members if they are already friends on remote site
  236. if ($isAutoFriends && method_exists($this, '_makeFriends'))
  237. $this->_makeFriends($iProfileId);
  238. return array('remote_profile_info' => $aProfileInfo, 'profile_id' => $iProfileId, 'existing_profile' => $oExistingAccount ? true : false);
  239. }
  240. protected function _processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile)
  241. {
  242. if (!isset($aFieldsProfile['picture']) || !$aFieldsProfile['picture'])
  243. return 0;
  244. if (!($oStorage = $oFormHelperProfile->getObjectStorage()))
  245. return 0;
  246. if (!($iFileId = $oStorage->storeFileFromUrl($aFieldsProfile['picture'], false, $iAccountProfileId)))
  247. return 0;
  248. return $iFileId;
  249. }
  250. /**
  251. * Get join page
  252. *
  253. * @param $aProfileFields array
  254. * @param $iRemoteProfileId remote profile id
  255. * @return void
  256. */
  257. function _getJoinPage($aProfileFields, $iRemoteProfileId)
  258. {
  259. bx_import('BxDolSession');
  260. $oSession = BxDolSession::getInstance();
  261. $oSession->setValue($this->_oConfig->sSessionUid, $iRemoteProfileId);
  262. $oPage = BxDolPage::getObjectInstanceByURI('create-account');
  263. BxBaseAccountForms::$PROFILE_FIELDS = $aProfileFields;
  264. $this->_oTemplate->getPage(false, $oPage->getCode());
  265. }
  266. /**
  267. * get redirect URL
  268. *
  269. * @param $iProfileId integer - profile ID
  270. * @return string redirect URL
  271. */
  272. function _getRedirectUrl($iProfileId, $isExistingProfile = false)
  273. {
  274. $sRedirectUrl = $this->_oConfig->sDefaultRedirectUrl;
  275. switch($this->_oConfig->sRedirectPage) {
  276. case 'index':
  277. $sRedirectUrl = BX_DOL_URL_ROOT;
  278. break;
  279. case 'settings':
  280. if (!$isExistingProfile) {
  281. $sRedirectUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-email'));
  282. break;
  283. }
  284. case 'dashboard':
  285. default:
  286. $sRedirectUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=dashboard'));
  287. break;
  288. }
  289. return $sRedirectUrl;
  290. }
  291. protected function _redirect($sUrl, $iStatus = 302)
  292. {
  293. header("Location:{$sUrl}", true, $iStatus);
  294. exit;
  295. }
  296. protected function _genToken($bReturn = false)
  297. {
  298. $oSession = BxDolSession::getInstance();
  299. $sPrefix = $this->getName();
  300. $iCsrfTokenLifetime = 3600;
  301. if ($oSession->getValue($sPrefix . '_token') === false || (time() - (int)$oSession->getValue($sPrefix . '_token_time') > $iCsrfTokenLifetime)) {
  302. $sToken = genRndPwd(20, false);
  303. $oSession->setValue($sPrefix . '_token', $sToken);
  304. $oSession->setValue($sPrefix . '_token_time', time());
  305. }
  306. else {
  307. $sToken = $oSession->getValue($sPrefix . '_token');
  308. }
  309. return $sToken;
  310. }
  311. protected function _getToken()
  312. {
  313. $oSession = BxDolSession::getInstance();
  314. return $oSession->getValue($this->getName() . '_token');
  315. }
  316. }
  317. /** @} */